summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/listbox.rb
blob: 611226682aebab7e3f856dc993bc12c10e28d3ac (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
#
# tk/listbox.rb : treat listbox widget
#
require 'tk'
require 'tk/scrollable'
require 'tk/txtwin_abst'

module TkTreatListItemFont
  include TkTreatItemFont

  ItemCMD = ['itemconfigure'.freeze, TkComm::None].freeze
  def __conf_cmd(idx)
    ItemCMD[idx]
  end

  def __item_pathname(tagOrId)
    self.path + ';' + tagOrId.to_s
  end

  private :__conf_cmd, :__item_pathname
end


class TkListbox<TkTextWin
  include TkTreatListItemFont
  include Scrollable

  TkCommandNames = ['listbox'.freeze].freeze
  WidgetClassName = 'Listbox'.freeze
  WidgetClassNames[WidgetClassName] = self

  def create_self(keys)
    if keys and keys != None
      tk_call_without_enc('listbox', @path, *hash_kv(keys, true))
    else
      tk_call_without_enc('listbox', @path)
    end
  end
  private :create_self

  def activate(y)
    tk_send_without_enc('activate', y)
    self
  end
  def curselection
    list(tk_send_without_enc('curselection'))
  end
  def get(first, last=nil)
    if last
      tk_split_simplelist(_fromUTF8(tk_send_without_enc('get', first, last)))
    else
      _fromUTF8(tk_send_without_enc('get', first))
    end
  end
  def nearest(y)
    tk_send_without_enc('nearest', y).to_i
  end
  def size
    tk_send_without_enc('size').to_i
  end
  def selection_anchor(index)
    tk_send_without_enc('selection', 'anchor', index)
    self
  end
  def selection_clear(first, last=None)
    tk_send_without_enc('selection', 'clear', first, last)
    self
  end
  def selection_includes(index)
    bool(tk_send_without_enc('selection', 'includes', index))
  end
  def selection_set(first, last=None)
    tk_send_without_enc('selection', 'set', first, last)
    self
  end

  def index(index)
    tk_send_without_enc('index', index).to_i
  end

  def itemcget(index, key)
    case key.to_s
    when 'text', 'label', 'show'
      _fromUTF8(tk_send_without_enc('itemcget', index, "-#{key}"))
    when 'font', 'kanjifont'
      #fnt = tk_tcl2ruby(tk_send('itemcget', index, "-#{key}"))
      fnt = tk_tcl2ruby(_fromUTF8(tk_send_without_enc('itemcget', index, 
						      '-font')))
      unless fnt.kind_of?(TkFont)
	fnt = tagfontobj(index, fnt)
      end
      if key.to_s == 'kanjifont' && JAPANIZED_TK && TK_VERSION =~ /^4\.*/
	# obsolete; just for compatibility
	fnt.kanji_font
      else
	fnt
      end
    else
      tk_tcl2ruby(_fromUTF8(tk_send_without_enc('itemcget', index, "-#{key}")))
    end
  end
  def itemconfigure(index, key, val=None)
    if key.kind_of? Hash
      if (key['font'] || key[:font] || 
          key['kanjifont'] || key[:kanjifont] || 
	  key['latinfont'] || key[:latinfont] || 
          key['asciifont'] || key[:asciifont] )
	tagfont_configure(index, _symbolkey2str(key))
      else
	tk_send_without_enc('itemconfigure', index, *hash_kv(key, true))
      end

    else
      if (key == 'font' || key == :font || 
          key == 'kanjifont' || key == :kanjifont || 
	  key == 'latinfont' || key == :latinfont || 
          key == 'asciifont' || key == :asciifont )
	if val == None
	  tagfontobj(index)
	else
	  tagfont_configure(index, {key=>val})
	end
      else
	tk_call('itemconfigure', index, "-#{key}", val)
      end
    end
    self
  end

  def itemconfiginfo(index, key=nil)
    if TkComm::GET_CONFIGINFO_AS_ARRAY
      if key
	case key.to_s
	when 'text', 'label', 'show'
	  conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
	when 'font', 'kanjifont'
	  conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
	  conf[4] = tagfont_configinfo(index, conf[4])
	else
	  conf = tk_split_list(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
	end
	conf[0] = conf[0][1..-1]
	conf
      else
	ret = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure', index))).collect{|conflist|
	  conf = tk_split_simplelist(conflist)
	  conf[0] = conf[0][1..-1]
	  case conf[0]
	  when 'text', 'label', 'show'
	  else
	    if conf[3]
	      if conf[3].index('{')
		conf[3] = tk_split_list(conf[3]) 
	      else
		conf[3] = tk_tcl2ruby(conf[3]) 
	      end
	    end
	    if conf[4]
	      if conf[4].index('{')
		conf[4] = tk_split_list(conf[4]) 
	      else
		conf[4] = tk_tcl2ruby(conf[4]) 
	      end
	    end
	  end
	  conf[1] = conf[1][1..-1] if conf.size == 2 # alias info
	  conf
	}
	fontconf = ret.assoc('font')
	if fontconf
	  ret.delete_if{|item| item[0] == 'font' || item[0] == 'kanjifont'}
	  fontconf[4] = tagfont_configinfo(index, fontconf[4])
	  ret.push(fontconf)
	else
	  ret
	end
      end
    else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
      if key
	case key.to_s
	when 'text', 'label', 'show'
	  conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
	when 'font', 'kanjifont'
	  conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
	  conf[4] = tagfont_configinfo(index, conf[4])
	else
	  conf = tk_split_list(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
	end
	key = conf.shift[1..-1]
	{ key => conf }
      else
	ret = {}
	tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure', index))).each{|conflist|
	  conf = tk_split_simplelist(conflist)
	  key = conf.shift[1..-1]
	  case key
	  when 'text', 'label', 'show'
	  else
	    if conf[2]
	      if conf[2].index('{')
		conf[2] = tk_split_list(conf[2]) 
	      else
		conf[2] = tk_tcl2ruby(conf[2]) 
	      end
	    end
	    if conf[3]
	      if conf[3].index('{')
		conf[3] = tk_split_list(conf[3]) 
	      else
		conf[3] = tk_tcl2ruby(conf[3]) 
	      end
	    end
	  end
	  if conf.size == 1
	    ret[key] = conf[0][1..-1]  # alias info
	  else
	    ret[key] = conf
	  end
	}
	fontconf = ret['font']
	if fontconf
	  ret.delete('font')
	  ret.delete('kanjifont')
	  fontconf[3] = tagfont_configinfo(index, fontconf[3])
	  ret['font'] = fontconf
	end
	ret
      end
    end
  end

  def current_itemconfiginfo(index, key=nil)
    if TkComm::GET_CONFIGINFO_AS_ARRAY
      if key
	conf = itemconfiginfo(index, key)
	{conf[0] => conf[4]}
      else
	ret = {}
	itemconfiginfo(index).each{|conf|
	  ret[conf[0]] = conf[4] if conf.size > 2
	}
	ret
      end
    else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
      ret = {}
      itemconfiginfo(index, key).each{|k, conf|
	ret[k] = conf[-1] if conf.kind_of?(Array)
      }
      ret
    end
  end
end