summaryrefslogtreecommitdiff
path: root/ext/tk/sample/tkcombobox.rb
blob: 08a2dd73a7d49e17574c2ccacc1dc9ddd5c7c994 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
#
#  tkcombobox.rb : TkAutoScrollbox & TkCombobox
# 
#                         by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#
require 'tk'

class TkAutoScrollbox < TkListbox
  include TkComposite

  @@up_bmp = TkBitmapImage.new(:data=><<EOD)
#define up_arrow_width 9
#define up_arrow_height 9
static unsigned char up_arrow_bits[] = {
   0x00, 0x00, 0x10, 0x00, 0x10, 0x00, 0x38, 0x00, 0x38, 0x00, 0x7c, 0x00,
   0x7c, 0x00, 0xfe, 0x00, 0x00, 0x00};
EOD

  @@down_bmp = TkBitmapImage.new(:data=><<EOD)
#define up_arrow_width 9
#define up_arrow_height 9
static unsigned char down_arrow_bits[] = {
   0x00, 0x00, 0xfe, 0x00, 0x7c, 0x00, 0x7c, 0x00, 0x38, 0x00, 0x38, 0x00,
   0x10, 0x00, 0x10, 0x00, 0x00, 0x00};
EOD

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

    @initwait = keys.delete('startwait'){300}
    @interval = keys.delete('interval'){150}
    @initwait -= @interval
    @initwait = 0 if @initwait < 0

    @lbox = TkListbox.new(@frame, :borderwidth=>0)
    @path = @lbox.path
    TkPack.propagate(@lbox, false)

    @scr = TkScrollbar.new(@frame, :width=>10)

    @lbox.yscrollcommand(proc{|*args| @scr.set(*args); _config_proc})
    @scr.command(proc{|*args| @lbox.yview(*args); _config_proc})

    @up_arrow   = TkLabel.new(@lbox, :image=>@@up_bmp, 
			      :relief=>:raised, :borderwidth=>1)
    @down_arrow = TkLabel.new(@lbox, :image=>@@down_bmp, 
			      :relief=>:raised, :borderwidth=>1)

    _init_binding

    @lbox.pack(:side=>:left, :fill=>:both, :expand=>:true)

    delegate('DEFAULT', @lbox)
    delegate('background', @frame, @scr)
    delegate('activebackground', @scr)
    delegate('troughcolor', @scr)
    delegate('repeatdelay', @scr)
    delegate('repeatinterval', @scr)
    delegate('relief', @frame)
    delegate('borderwidth', @frame)

    scrollbar(keys.delete('scrollbar')){false}

    configure keys unless keys.empty?
  end

  ############################
  private
  ############################
  def _show_up_arrow
    unless @up_arrow.winfo_mapped?
      @up_arrow.pack(:side=>:top, :fill=>:x)
    end
  end

  def _show_down_arrow
    unless @down_arrow.winfo_mapped?
      @down_arrow.pack(:side=>:bottom, :fill=>:x) 
    end
  end

  def _set_sel(idx)
      @lbox.activate(idx)
      @lbox.selection_clear(0, 'end')
      @lbox.selection_set(idx)
  end

  def _check_sel(cidx, tidx = nil, bidx = nil)
    _set_sel(cidx)
    unless tidx
      tidx = @lbox.nearest(0) 
      tidx += 1 if tidx > 0
    end
    unless bidx
      bidx = @lbox.nearest(10000) 
      bidx -= 1 if bidx < @lbox.index('end') - 1
    end
    if cidx > bidx
      _set_sel(bidx)
    end
    if cidx < tidx
      _set_sel(tidx)
    end
  end

  def _up_proc
    cidx = @lbox.curselection[0]
    idx = @lbox.nearest(0)
    if idx >= 0
      @lbox.see(idx - 1)
      _set_sel(idx)
      @up_arrow.pack_forget if idx == 1
      @up_timer.stop if idx == 0
      _show_down_arrow if @lbox.bbox('end') == []
    end
    if cidx && cidx > 0 && (idx == 0 || cidx == @lbox.nearest(10000))
      _set_sel(cidx - 1)
    end
  end

  def _down_proc
    cidx = @lbox.curselection[0]
    eidx = @lbox.index('end') - 1
    idx = @lbox.nearest(10000)
    if idx <= eidx
      @lbox.see(idx + 1)
      _set_sel(cidx + 1) if cidx < eidx
      @down_arrow.pack_forget if idx + 1 == eidx
      @down_timer.stop if idx == eidx
      _show_up_arrow if @lbox.bbox(0) == []
    end
    if cidx && cidx < eidx && (eidx == idx || cidx == @lbox.nearest(0))
      _set_sel(cidx + 1)
    end
  end

  def _key_UP_proc
    cidx = @lbox.curselection[0]
    _set_sel(cidx = @lbox.index('activate')) unless cidx
    cidx -= 1
    if cidx == 0
      @up_arrow.pack_forget
    elsif cidx == @lbox.nearest(0)
      @lbox.see(cidx - 1)
    end
  end

  def _key_DOWN_proc
    cidx = @lbox.curselection[0]
    _set_sel(cidx = @lbox.index('activate')) unless cidx
    cidx += 1
    if cidx == @lbox.index('end') - 1
      @down_arrow.pack_forget
    elsif cidx == @lbox.nearest(10000)
      @lbox.see(cidx + 1)
    end
  end

  def _config_proc
    if @lbox.size == 0
      @up_arrow.pack_forget
      @down_arrow.pack_forget
      return
    end
    tidx = @lbox.nearest(0)
    bidx = @lbox.nearest(10000)
    if tidx > 0
      _show_up_arrow
      tidx += 1
    else
      @up_arrow.pack_forget unless @up_timer.running?
    end
    if bidx < @lbox.index('end') - 1
      _show_down_arrow
      bidx -= 1
    else
      @down_arrow.pack_forget unless @down_timer.running?
    end
    cidx = @lbox.curselection[0]
    _check_sel(cidx, tidx, bidx) if cidx
  end

  def _init_binding
    @up_timer = TkAfter.new(@interval, -1, proc{_up_proc})
    @down_timer = TkAfter.new(@interval, -1, proc{_down_proc})

    @up_timer.set_start_proc(@initwait, proc{})
    @down_timer.set_start_proc(@initwait, proc{})

    @up_arrow.bind('Enter', proc{@up_timer.start})
    @up_arrow.bind('Leave', proc{@up_timer.stop if @up_arrow.winfo_mapped?})
    @down_arrow.bind('Enter', proc{@down_timer.start})
    @down_arrow.bind('Leave', proc{@down_timer.stop if @down_arrow.winfo_mapped?})

    @lbox.bind('Configure', proc{_config_proc})
    @lbox.bind('Enter', proc{|y| _set_sel(@lbox.nearest(y))}, '%y')
    @lbox.bind('Motion', proc{|y| 
		 @up_timer.stop if @up_timer.running?
		 @down_timer.stop if @down_timer.running?
		 _check_sel(@lbox.nearest(y))
	       }, '%y')

    @lbox.bind('Up', proc{_key_UP_proc})
    @lbox.bind('Down', proc{_key_DOWN_proc})
  end

  ############################
  public
  ############################
  def scrollbar(mode)
    if mode
      @scr.pack(:side=>:right, :fill=>:y)
    else
      @scr.pack_forget
    end
  end
end

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

class TkCombobox < TkEntry
  include TkComposite

  @@down_btn_bmp = TkBitmapImage.new(:data=><<EOD)
#define down_arrow_width 11
#define down_arrow_height 11
static unsigned char down_arrow_bits[] = {
   0x00, 0x00, 0xfe, 0x03, 0xfc, 0x01, 0xfc, 0x01, 0xf8, 0x00, 0xf8, 0x00,
   0x70, 0x00, 0x70, 0x00, 0x20, 0x00, 0x20, 0x00, 0x00, 0x00};
EOD

  @@up_btn_bmp = TkBitmapImage.new(:data=><<EOD)
#define up_arrow_width 11
#define up_arrow_height 11
static unsigned char up_arrow_bits[] = {
   0x00, 0x00, 0x20, 0x00, 0x20, 0x00, 0x70, 0x00, 0x70, 0x00, 0xf8, 0x00,
   0xf8, 0x00, 0xfc, 0x01, 0xfc, 0x01, 0xfe, 0x03, 0x00, 0x00};
EOD

  def _button_proc(dir = true)
    @btn.relief(:sunken)
    x = @frame.winfo_rootx
    y = @frame.winfo_rooty
    if dir
      @top.geometry("+#{x}+#{y + @frame.winfo_height}")
    else
      @btn.image(@@up_btn_bmp)
      @top.geometry("+#{x}+#{y - @top.winfo_reqheight}")
    end
    @top.deiconify
    @lst.focus

    if (idx = values.index(@ent.value))
      @lst.see(idx - 1)
      @lst.activate(idx)
      @lst.selection_set(idx)
    elsif @lst.size > 0
      @lst.see(0)
      @lst.activate(0)
      @lst.selection_set(0)
    end
    @top.grab

    begin
      @var.tkwait
      if (idx = @var.to_i) >= 0
	@ent.value = @lst.get(idx)
      end
      @top.withdraw
      @btn.relief(:raised)
      @btn.image(@@down_btn_bmp)
    rescue
    ensure
      begin
	@top.grab(:release)
	@ent.focus
      rescue
      end
    end
  end
  private :_button_proc

  def _init_bindings
    @btn.bind('1', proc{_button_proc(true)})
    @btn.bind('3', proc{_button_proc(false)})

    @lst.bind('1', proc{|y| @var.value = @lst.nearest(y)}, '%y')
    @lst.bind('Return', proc{@var.value = @lst.curselection[0]})

    cancel = TkVirtualEvent.new('2', '3', 'Escape')
    @lst.bind(cancel, proc{@var.value = -1})
  end
  private :_init_bindings

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

    @btn = TkLabel.new(@frame, :relief=>:raised, :borderwidth=>3, 
		       :image=>@@down_btn_bmp).pack(:side=>:right, 
						    :ipadx=>2, :fill=>:y)
    @ent = TkEntry.new(@frame).pack(:side=>:left)
    @path = @ent.path

    @top = TkToplevel.new(@btn, :borderwidth=>1, :relief=>:raised) {
      withdraw
      transient
      overrideredirect(true)
    }

    startwait = keys.delete('startwait'){300}
    interval = keys.delete('interval'){150}
    @lst = TkAutoScrollbox.new(@top, 
			       :startwait=>startwait, 
			       :interval=>interval).pack(:fill=>:both, 
							 :expand=>true)
    @ent_list = []

    @var = TkVariable.new

    _init_bindings

    delegate('DEFAULT', @ent)
    delegate('height', @lst)
    delegate('relief', @frame)
    delegate('borderwidth', @frame)

    if mode = keys.delete('scrollbar')
      scrollbar(mode)
    end

    configure keys unless keys.empty?
  end

  def scrollbar(mode)
    @lst.scrollbar(mode)
  end

  def _reset_width
    len = @ent.width
    @lst.get(0, 'end').each{|l| len = l.length if l.length > len}
    @lst.width(len + 1)
  end
  private :_reset_width

  def add(ent)
    ent = ent.to_s
    unless @ent_list.index(ent)
      @ent_list << ent
      @lst.insert('end', ent)
    end
    _reset_width
    self
  end

  def remove(ent)
    ent = ent.to_s
    @ent_list.delete(ent)
    if idx = @lst.get(0, 'end').index(ent)
      @lst.delete(idx)
    end
    _reset_width
    self
  end

  def values(ary = nil)
    if ary
      @lst.delete(0, 'end')
      @ent_list.clear
      ary.each{|ent| add(ent)}
      _reset_width
      self
    else
      @lst.get(0, 'end')
    end
  end

  def see(idx)
    @lst.see(@lst.index(idx) - 1)
  end

  def list_index(idx)
    @lst.index(idx)
  end
end


################################################
# test
################################################
if __FILE__ == $0
  v = TkVariable.new
  e = TkCombobox.new(:height=>7, :scrollbar=>true, :textvariable=>v, 
		     :startwait=>400, :interval=>200).pack
  e.values(%w(aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu))
  #e.see(e.list_index('end') - 2)
  e.value = 'cc'
  TkFrame.new{|f|
    fnt = TkFont.new('Helvetica 10')
    TkLabel.new(f, :font=>fnt, :text=>'TkCombobox value :').pack(:side=>:left)
    TkLabel.new(f, :font=>fnt, :textvariable=>v).pack(:side=>:left)
  }.pack

  TkFrame.new(:relief=>:raised, :borderwidth=>2, 
	      :height=>3).pack(:fill=>:x, :expand=>true, :padx=>5, :pady=>3)

  l = TkAutoScrollbox.new(nil, :relief=>:groove, :borderwidth=>4, 
			  :width=>20).pack(:fill=>:both, :expand=>true)
  (0..20).each{|i| l.insert('end', "line #{i}")}

  TkFrame.new(:relief=>:ridge, :borderwidth=>3){
    TkButton.new(self, :text=>'ON', 
		 :command=>proc{l.scrollbar(true)}).pack(:side=>:left)
    TkButton.new(self, :text=>'OFF', 
		 :command=>proc{l.scrollbar(false)}).pack(:side=>:right)
    pack(:fill=>:x)
  }
  Tk.mainloop
end