1 #
2 # tk/listbox.rb : treat listbox widget
3 #
4 require 'tk'
5 require 'tk/itemconfig'
6 require 'tk/scrollable'
7 require 'tk/txtwin_abst'
8
9 module TkListItemConfig
10 include TkItemConfigMethod
11
12 def __item_listval_optkeys(id)
13 []
14 end
15 private :__item_listval_optkeys
16 end
17
18 class Tk::Listbox<TkTextWin
19 include TkListItemConfig
20 include Scrollable
21
22 TkCommandNames = ['listbox'.freeze].freeze
23 WidgetClassName = 'Listbox'.freeze
24 WidgetClassNames[WidgetClassName] = self
25
26 #def create_self(keys)
27 # if keys and keys != None
28 # tk_call_without_enc('listbox', @path, *hash_kv(keys, true))
29 # else
30 # tk_call_without_enc('listbox', @path)
31 # end
32 #end
33 #private :create_self
34
35 def __tkvariable_optkeys
36 super() << 'listvariable'
37 end
38 private :__tkvariable_optkeys
39
40 def tagid(id)
41 #id.to_s
42 _get_eval_string(id)
43 end
44
45 def activate(y)
46 tk_send_without_enc('activate', y)
47 self
48 end
49 def curselection
50 list(tk_send_without_enc('curselection'))
51 end
52 def get(first, last=nil)
53 if last
54 # tk_split_simplelist(_fromUTF8(tk_send_without_enc('get', first, last)))
55 tk_split_simplelist(tk_send_without_enc('get', first, last), false, true)
56 else
57 _fromUTF8(tk_send_without_enc('get', first))
58 end
59 end
60 def nearest(y)
61 tk_send_without_enc('nearest', y).to_i
62 end
63 def size
64 tk_send_without_enc('size').to_i
65 end
66 def selection_anchor(index)
67 tk_send_without_enc('selection', 'anchor', index)
68 self
69 end
70 def selection_clear(first, last=None)
71 tk_send_without_enc('selection', 'clear', first, last)
72 self
73 end
74 def selection_includes(index)
75 bool(tk_send_without_enc('selection', 'includes', index))
76 end
77 def selection_set(first, last=None)
78 tk_send_without_enc('selection', 'set', first, last)
79 self
80 end
81
82 def index(idx)
83 tk_send_without_enc('index', idx).to_i
84 end
85
86 def value
87 get('0', 'end')
88 end
89
90 def value= (vals)
91 unless vals.kind_of?(Array)
92 fail ArgumentError, 'an Array is expected'
93 end
94 tk_send_without_enc('delete', '0', 'end')
95 tk_send_without_enc('insert', '0',
96 *(vals.collect{|v| _get_eval_enc_str(v)}))
97 vals
98 end
99
100 def clear
101 tk_send_without_enc('delete', '0', 'end')
102 self
103 end
104 alias erase clear
105
106 =begin
107 def itemcget(index, key)
108 case key.to_s
109 when 'text', 'label', 'show'
110 _fromUTF8(tk_send_without_enc('itemcget', index, "-#{key}"))
111 when 'font', 'kanjifont'
112 #fnt = tk_tcl2ruby(tk_send('itemcget', index, "-#{key}"))
113 fnt = tk_tcl2ruby(_fromUTF8(tk_send_without_enc('itemcget', index,
114 '-font')))
115 unless fnt.kind_of?(TkFont)
116 fnt = tagfontobj(index, fnt)
117 end
118 if key.to_s == 'kanjifont' && JAPANIZED_TK && TK_VERSION =~ /^4\.*/
119 # obsolete; just for compatibility
120 fnt.kanji_font
121 else
122 fnt
123 end
124 else
125 tk_tcl2ruby(_fromUTF8(tk_send_without_enc('itemcget', index, "-#{key}")))
126 end
127 end
128 def itemconfigure(index, key, val=None)
129 if key.kind_of? Hash
130 if (key['font'] || key[:font] ||
131 key['kanjifont'] || key[:kanjifont] ||
132 key['latinfont'] || key[:latinfont] ||
133 key['asciifont'] || key[:asciifont] )
134 tagfont_configure(index, _symbolkey2str(key))
135 else
136 tk_send_without_enc('itemconfigure', index, *hash_kv(key, true))
137 end
138
139 else
140 if (key == 'font' || key == :font ||
141 key == 'kanjifont' || key == :kanjifont ||
142 key == 'latinfont' || key == :latinfont ||
143 key == 'asciifont' || key == :asciifont )
144 if val == None
145 tagfontobj(index)
146 else
147 tagfont_configure(index, {key=>val})
148 end
149 else
150 tk_call('itemconfigure', index, "-#{key}", val)
151 end
152 end
153 self
154 end
155
156 def itemconfiginfo(index, key=nil)
157 if TkComm::GET_CONFIGINFO_AS_ARRAY
158 if key
159 case key.to_s
160 when 'text', 'label', 'show'
161 conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
162 when 'font', 'kanjifont'
163 conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
164 conf[4] = tagfont_configinfo(index, conf[4])
165 else
166 conf = tk_split_list(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
167 end
168 conf[0] = conf[0][1..-1]
169 conf
170 else
171 ret = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure', index))).collect{|conflist|
172 conf = tk_split_simplelist(conflist)
173 conf[0] = conf[0][1..-1]
174 case conf[0]
175 when 'text', 'label', 'show'
176 else
177 if conf[3]
178 if conf[3].index('{')
179 conf[3] = tk_split_list(conf[3])
180 else
181 conf[3] = tk_tcl2ruby(conf[3])
182 end
183 end
184 if conf[4]
185 if conf[4].index('{')
186 conf[4] = tk_split_list(conf[4])
187 else
188 conf[4] = tk_tcl2ruby(conf[4])
189 end
190 end
191 end
192 conf[1] = conf[1][1..-1] if conf.size == 2 # alias info
193 conf
194 }
195 fontconf = ret.assoc('font')
196 if fontconf
197 ret.delete_if{|item| item[0] == 'font' || item[0] == 'kanjifont'}
198 fontconf[4] = tagfont_configinfo(index, fontconf[4])
199 ret.push(fontconf)
200 else
201 ret
202 end
203 end
204 else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
205 if key
206 case key.to_s
207 when 'text', 'label', 'show'
208 conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
209 when 'font', 'kanjifont'
210 conf = tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
211 conf[4] = tagfont_configinfo(index, conf[4])
212 else
213 conf = tk_split_list(_fromUTF8(tk_send_without_enc('itemconfigure',index,"-#{key}")))
214 end
215 key = conf.shift[1..-1]
216 { key => conf }
217 else
218 ret = {}
219 tk_split_simplelist(_fromUTF8(tk_send_without_enc('itemconfigure', index))).each{|conflist|
220 conf = tk_split_simplelist(conflist)
221 key = conf.shift[1..-1]
222 case key
223 when 'text', 'label', 'show'
224 else
225 if conf[2]
226 if conf[2].index('{')
227 conf[2] = tk_split_list(conf[2])
228 else
229 conf[2] = tk_tcl2ruby(conf[2])
230 end
231 end
232 if conf[3]
233 if conf[3].index('{')
234 conf[3] = tk_split_list(conf[3])
235 else
236 conf[3] = tk_tcl2ruby(conf[3])
237 end
238 end
239 end
240 if conf.size == 1
241 ret[key] = conf[0][1..-1] # alias info
242 else
243 ret[key] = conf
244 end
245 }
246 fontconf = ret['font']
247 if fontconf
248 ret.delete('font')
249 ret.delete('kanjifont')
250 fontconf[3] = tagfont_configinfo(index, fontconf[3])
251 ret['font'] = fontconf
252 end
253 ret
254 end
255 end
256 end
257
258 def current_itemconfiginfo(index, key=nil)
259 if TkComm::GET_CONFIGINFO_AS_ARRAY
260 if key
261 conf = itemconfiginfo(index, key)
262 {conf[0] => conf[4]}
263 else
264 ret = {}
265 itemconfiginfo(index).each{|conf|
266 ret[conf[0]] = conf[4] if conf.size > 2
267 }
268 ret
269 end
270 else # ! TkComm::GET_CONFIGINFO_AS_ARRAY
271 ret = {}
272 itemconfiginfo(index, key).each{|k, conf|
273 ret[k] = conf[-1] if conf.kind_of?(Array)
274 }
275 ret
276 end
277 end
278 =end
279 end
280
281 #TkListbox = Tk::Listbox unless Object.const_defined? :TkListbox
282 Tk.__set_toplevel_aliases__(:Tk, Tk::Listbox, :TkListbox)