summaryrefslogtreecommitdiff
path: root/jni/ruby/ext/tk/sample/tktextframe.rb
blob: d6584beeb830459fd65fe752dc3d55bc9c0af703 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#
#  tktextframe.rb : a sample of TkComposite
#
#                         by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
require 'tk'

module Tk::ScrollbarComposite
  include TkComposite

  def component_construct_keys
    # If a component requires options for construction,
    # return an Array of option-keys.
    []
  end
  private :component_construct_keys

  def create_component(keys={})
    # This method must return the created component widget.
  end
  private :create_component

  def component_delegates
    # if want to override default option-methods or delegates,
    # please define here.
  end
  private :component_delegates

  def define_delegates
    # option methods for scrollbars
    option_methods([:scrollbarwidth, :get_scrollbarwidth])

    # set receiver widgets for configure methods (with alias)
    delegate_alias('scrollbarrelief', 'relief', @h_scroll, @v_scroll)
    delegate_alias('framebackground', 'background',
                   @frame, @h_scroll, @v_scroll)
    delegate_alias('activeframebackground', 'activebackground',
                   @h_scroll, @v_scroll)

    # set receiver widgets for configure methods
    delegate('DEFAULT', @component)
    delegate('troughcolor', @h_scroll, @v_scroll)
    delegate('repeatdelay', @h_scroll, @v_scroll)
    delegate('repeatinterval', @h_scroll, @v_scroll)
    delegate('borderwidth', @frame)
    delegate('relief', @frame)

    component_delegates
  end
  private :define_delegates

  DEFAULT_VSCROLL = true
  DEFAULT_HSCROLL = true

  def initialize_composite(keys={})
    keys = _symbolkey2str(keys)

    # create scrollbars
    @v_scroll = TkScrollbar.new(@frame, 'orient'=>'vertical')
    @h_scroll = TkScrollbar.new(@frame, 'orient'=>'horizontal')

    # create a component
    construct_keys = {}
    ((component_construct_keys.map{|k| k.to_s}) & keys.keys).each{|k|
      construct_keys[k] = keys.delete(k)
    }

    # create a component (the component must be scrollable)
    @component = create_component(construct_keys)

    # set default receiver of method calls
    @path = @component.path

    # assign scrollbars
    @component.xscrollbar(@h_scroll)
    @component.yscrollbar(@v_scroll)

    # allignment
    TkGrid.rowconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
    TkGrid.columnconfigure(@frame, 0, 'weight'=>1, 'minsize'=>0)
    @component.grid('row'=>0, 'column'=>0, 'sticky'=>'news')

    # scrollbars ON
    vscroll(keys.delete('vscroll'){self.class::DEFAULT_VSCROLL})
    hscroll(keys.delete('hscroll'){self.class::DEFAULT_HSCROLL})

    # do configure
    define_delegates

    # do configure
    configure keys unless keys.empty?
  end
  private :initialize_composite

  # get/set width of scrollbar
  def get_scrollbarwidth
    @v_scroll.width
  end
  def set_scrollbarwidth(width)
    @v_scroll.width(width)
    @h_scroll.width(width)
  end
  alias :scrollbarwidth :set_scrollbarwidth

  def hook_vscroll_on(*args); end
  def hook_vscroll_off(*args); end
  def hook_hscroll_on(*args); end
  def hook_hscroll_off(*args); end
  private :hook_vscroll_on,:hook_vscroll_off,:hook_hscroll_on,:hook_hscroll_off

  # vertical scrollbar : ON/OFF
  def vscroll(mode, *args)
    st = TkGrid.info(@v_scroll)
    if mode && st.size == 0 then
      @v_scroll.grid('row'=>0, 'column'=>1, 'sticky'=>'ns')
      hook_vscroll_on(*args)
    elsif !mode && st.size != 0 then
      @v_scroll.ungrid
      hook_vscroll_off(*args)
    end
    self
  end

  # horizontal scrollbar : ON/OFF
  def hscroll(mode, *args)
    st = TkGrid.info(@h_scroll)
    if mode && st.size == 0 then
      @h_scroll.grid('row'=>1, 'column'=>0, 'sticky'=>'ew')
      hook_hscroll_on(*args)
    elsif !mode && st.size != 0 then
      @h_scroll.ungrid
      hook_hscroll_off(*args)
    end
    self
  end
end

################################################

class TkTextFrame < TkText
  include Tk::ScrollbarComposite

  # def component_construct_keys; []; end
  # private :component_construct_keys

  def create_component(keys={})
    # keys has options which are listed by component_construct_keys method.
    @text = TkText.new(@frame, 'wrap'=>'none')
    @text.configure(keys) unless keys.empty?

    # option methods for component
    option_methods(
       [:textbackground, nil, :textbg_info],
       :textborderwidth,
       :textrelief
    )

    # return the created componet
    @text
  end
  private :create_component

  # def component_delegates; end
  # private :component_delegates

  def hook_hscroll_on(wrap_mode=nil)
    if wrap_mode
      wrap wrap_mode
    else
      wrap 'none'  # => self.wrap('none')
    end
  end
  def hook_hscroll_off(wrap_mode)
    wrap wrap_mode  # => self.wrap(wrap_mode)
  end
  def hscroll(mode, wrap_mode="char")
    super
  end

  # set background color of text widget
  def textbackground(color = nil)
    if color
      @text.background(color)
    else
      @text.background
    end
  end

  def textbg_info
    info = @text.configinfo(:background)
    if TkComm::GET_CONFIGINFO_AS_ARRAY
      info[0] = 'textbackground'
      info
    else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
      {'textbackground' => info['background']}
    end
  end

  # get/set borderwidth of text widget
  def set_textborderwidth(width)
    @text.borderwidth(width)
  end
  def get_textborderwidth
    @text.borderwidth
  end
  def textborderwidth(width = nil)
    if width
      set_textborderwidth(width)
    else
      get_textborderwidth
    end
  end

  # set relief of text widget
  def textrelief(type)
    @text.relief(type)
  end
end

################################################
# test
################################################
if __FILE__ == $0
  TkLabel.new(:text=>'TkTextFrame is an example of Tk::ScrollbarComposite module.').pack
  f = TkFrame.new.pack('fill'=>'x')
  #t = TkTextFrame.new.pack
  t = TkTextFrame.new(:textborderwidth=>3,
                      :textrelief=>:ridge,
                      :scrollbarrelief=>:ridge).pack
  p t.configinfo
  TkButton.new(f, 'text'=>'vscr OFF',
               'command'=>proc{t.vscroll(false)}).pack('side'=>'right')
  TkButton.new(f, 'text'=>'vscr ON',
               'command'=>proc{t.vscroll(true)}).pack('side'=>'right')
  TkButton.new(f, 'text'=>'hscr ON',
               'command'=>proc{t.hscroll(true)}).pack('side'=>'left')
  TkButton.new(f, 'text'=>'hscr OFF',
               'command'=>proc{t.hscroll(false)}).pack('side'=>'left')

  ############################################

  # Tk.default_widget_set = :Ttk

  TkFrame.new.pack(:pady=>10)
  TkLabel.new(:text=>'The following is another example of Tk::ScrollbarComposite module.').pack

  #----------------------------------
  class ScrListbox < TkListbox
    include Tk::ScrollbarComposite

    DEFAULT_HSCROLL = false

    def create_component(keys={})
      TkListbox.new(@frame, keys)
    end
    private :create_component
  end
  #----------------------------------

  f = TkFrame.new.pack(:pady=>5)
  lbox = ScrListbox.new(f).pack(:side=>:left)
  lbox.value = %w(aa bb cc dd eeeeeeeeeeeeeeeeeeeeeeeeee ffffffffff gg hh ii jj kk ll mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm nn oo pp qq)
  fb = TkFrame.new(f).pack(:expand=>true, :fill=>:y, :padx=>5)
  TkButton.new(fb, 'text'=>'lbox hscr OFF',
               'command'=>proc{lbox.hscroll(false)}).pack(:side=>:bottom,
                                                          :fill=>:x)
  TkButton.new(fb, 'text'=>'lbox hscr ON',
               'command'=>proc{lbox.hscroll(true)}).pack(:side=>:bottom,
                                                          :fill=>:x)
  TkFrame.new(fb).pack(:pady=>5, :side=>:bottom)
  TkButton.new(fb, 'text'=>'lbox vscr OFF',
               'command'=>proc{lbox.vscroll(false)}).pack(:side=>:bottom,
                                                          :fill=>:x)
  TkButton.new(fb, 'text'=>'lbox vscr ON',
               'command'=>proc{lbox.vscroll(true)}).pack(:side=>:bottom,
                                                          :fill=>:x)

  ############################################

  Tk.mainloop
end