OSDN Git Service

ruby-1.9.1-rc1
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / ext / tk / lib / tkextlib / bwidget / tree.rb
1 #
2 #  tkextlib/bwidget/tree.rb
3 #                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4 #
5
6 require 'tk'
7 require 'tk/canvas'
8 require 'tkextlib/bwidget.rb'
9
10 module Tk
11   module BWidget
12     class Tree < TkWindow
13       class Node < TkObject
14       end
15     end
16   end
17 end
18
19 class Tk::BWidget::Tree
20   include TkItemConfigMethod
21   include Scrollable
22
23   TkCommandNames = ['Tree'.freeze].freeze
24   WidgetClassName = 'Tree'.freeze
25   WidgetClassNames[WidgetClassName] = self
26
27   class Event_for_Items < TkEvent::Event
28     def self._get_extra_args_tbl
29       [ 
30         TkComm.method(:string)   # item idenfier
31       ]
32     end
33   end
34
35   def __strval_optkeys
36     super() << 'crossfill' << 'linesfill'
37   end
38   private :__strval_optkeys
39
40   def __boolval_optkeys
41     super() << 'dragenabled' << 'dropenabled' << 
42       'redraw' << 'selectfill' << 'showlines'
43   end
44   private :__boolval_optkeys
45
46   def __tkvariable_optkeys
47     super() << 'helpvar'
48   end
49   private :__tkvariable_optkeys
50
51   def tagid(tag)
52     if tag.kind_of?(Tk::BWidget::Tree::Node)
53       tag.id
54     else
55       # tag
56       _get_eval_string(tag)
57     end
58   end
59
60   #def imagebind(*args)
61   #  _bind_for_event_class(Event_for_Items, [path, 'bindImage'], *args)
62   #  self
63   #end
64   def imagebind(context, *args)
65     #if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
66     if TkComm._callback_entry?(args[0]) || !block_given?
67       cmd = args.shift
68     else
69       cmd = Proc.new
70     end
71     _bind_for_event_class(Event_for_Items, [path, 'bindImage'], 
72                           context, cmd, *args)
73     self
74   end
75
76   #def imagebind_append(*args)
77   #  _bind_append_for_event_class(Event_for_Items, [path, 'bindImage'], *args)
78   #  self
79   #end
80   def imagebind_append(context, *args)
81     #if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
82     if TkComm._callback_entry?(args[0]) || !block_given?
83       cmd = args.shift
84     else
85       cmd = Proc.new
86     end
87     _bind_append_for_event_class(Event_for_Items, [path, 'bindImage'], 
88                                  context, cmd, *args)
89     self
90   end
91
92   def imagebind_remove(*args)
93     _bind_remove_for_event_class(Event_for_Items, [path, 'bindImage'], *args)
94     self
95   end
96
97   def imagebindinfo(*args)
98     _bindinfo_for_event_class(Event_for_Items, [path, 'bindImage'], *args)
99   end
100
101   #def textbind(*args)
102   #  _bind_for_event_class(Event_for_Items, [path, 'bindText'], *args)
103   #  self
104   #end
105   def textbind(context, *args)
106     #if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
107     if TkComm._callback_entry?(args[0]) || !block_given?
108       cmd = args.shift
109     else
110       cmd = Proc.new
111     end
112     _bind_for_event_class(Event_for_Items, [path, 'bindText'], 
113                           context, cmd, *args)
114     self
115   end
116
117   #def textbind_append(*args)
118   #  _bind_append_for_event_class(Event_for_Items, [path, 'bindText'], *args)
119   #  self
120   #end
121   def textbind_append(context, *args)
122     #if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
123     if TkComm._callback_entry?(args[0]) || !block_given?
124       cmd = args.shift
125     else
126       cmd = Proc.new
127     end
128     _bind_append_for_event_class(Event_for_Items, [path, 'bindText'], 
129                                  context, cmd, *args)
130     self
131   end
132
133   def textbind_remove(*args)
134     _bind_remove_for_event_class(Event_for_Items, [path, 'bindText'], *args)
135     self
136   end
137
138   def textbindinfo(*args)
139     _bindinfo_for_event_class(Event_for_Items, [path, 'bindText'], *args)
140   end
141
142   def close_tree(node, recurse=None)
143     tk_send('closetree', tagid(node), recurse)
144     self
145   end
146
147   def delete(*args)
148     tk_send('delete', *(args.collect{|node| tagid(node)}))
149     self
150   end
151
152   def edit(node, text, *args)
153     tk_send('edit', tagid(node), text, *args)
154     self
155   end
156
157   def exist?(node)
158     bool(tk_send('exists', tagid(node)))
159   end
160
161   def index(node)
162     num_or_str(tk_send('index', tagid(node)))
163   end
164
165   def insert(idx, parent, node, keys={})
166     tk_send('insert', idx, tagid(parent), tagid(node), *hash_kv(keys))
167     self
168   end
169
170   def move(parent, node, idx)
171     tk_send('move', tagid(parent), tagid(node), idx)
172     self
173   end
174
175   def get_node(node, idx)
176     Tk::BWidget::Tree::Node.id2obj(self, tk_send('nodes', tagid(node), idx))
177   end
178
179   def nodes(node, first=None, last=None)
180     simplelist(tk_send('nodes', tagid(node), first, last)).collect{|node|
181       Tk::BWidget::Tree::Node.id2obj(self, node)
182     }
183   end
184
185   def open?(node)
186     bool(@tree.itemcget(tagid(node), 'open'))
187   end
188
189   def open_tree(node, recurse=None)
190     tk_send('opentree', tagid(node), recurse)
191     self
192   end
193
194   def parent(node)
195     Tk::BWidget::Tree::Node.id2obj(self, tk_send('parent', tagid(node)))
196   end
197
198   def reorder(node, neworder)
199     tk_send('reorder', tagid(node), neworder)
200     self
201   end
202
203   def see(node)
204     tk_send('see', tagid(node))
205     self
206   end
207
208   def selection_add(*args)
209     tk_send_without_enc('selection', 'add', 
210                         *(args.collect{|node| tagid(node)}))
211     self
212   end
213
214   def selection_clear
215     tk_send_without_enc('selection', 'clear')
216     self
217   end
218
219   def selection_get
220     list(tk_send_without_enc('selection', 'get'))
221   end
222
223   def selection_include?(*args)
224     bool(tk_send_without_enc('selection', 'get', 
225                              *(args.collect{|node| tagid(node)})))
226   end
227
228   def selection_range(*args)
229     tk_send_without_enc('selection', 'range', 
230                         *(args.collect{|node| tagid(node)}))
231     self
232   end
233
234   def selection_remove(*args)
235     tk_send_without_enc('selection', 'remove', 
236                         *(args.collect{|node| tagid(node)}))
237     self
238   end
239
240   def selection_set(*args)
241     tk_send_without_enc('selection', 'set', 
242                         *(args.collect{|node| tagid(node)}))
243     self
244   end
245
246   def selection_toggle(*args)
247     tk_send_without_enc('selection', 'toggle', 
248                         *(args.collect{|node| tagid(node)}))
249     self
250   end
251
252   def toggle(node)
253     tk_send_without_enc('toggle', tagid(node))
254     self
255   end
256
257   def visible(node)
258     bool(tk_send_without_enc('visible', tagid(node)))
259   end
260 end
261
262 class Tk::BWidget::Tree::Node
263   include TkTreatTagFont
264
265   TreeNode_TBL = TkCore::INTERP.create_table
266
267   (TreeNode_ID = ['bw:node'.freeze, '00000'.taint]).instance_eval{
268     @mutex = Mutex.new
269     def mutex; @mutex; end
270     freeze
271   }
272
273   TkCore::INTERP.init_ip_env{
274     TreeNode_TBL.mutex.synchronize{ TreeNode_TBL.clear }
275   }
276
277   def self.id2obj(tree, id)
278     tpath = tree.path
279     TreeNode_TBL.mutex.synchronize{
280       if TreeNode_TBL[tpath]
281         TreeNode_TBL[tpath][id]? TreeNode_TBL[tpath][id]: id
282       else
283         id
284       end
285     }
286   end
287
288   def initialize(tree, *args)
289     if tree.kind_of?(Tk::BWidget::Tree)
290       @tree = tree
291       parent = args.shift
292       if parent.kind_of?(Tk::BWidget::Tree::Node)
293         if parent.tree.path != @tree.path
294           fail RuntimeError, 'tree of parent node is not match'
295         end
296       end
297     elsif tree.kind_of?(Tk::BWidget::Tree::Node)
298       @tree = tree.tree
299       parent = tree.parent
300     else
301       fail RuntimeError, 
302         "expect Tk::BWidget::Tree or Tk::BWidget::Tree::Node for 1st argument"
303     end
304
305     if args[-1].kind_of?(Hash)
306       keys = _symbolkey2str(args.pop)
307     else
308       keys = {}
309     end
310
311     index = keys.delete('index')
312     unless args.empty?
313       index = args.shift
314     end
315     index = 'end' unless index
316
317     unless args.empty?
318       fail RuntimeError, 'too much arguments'
319     end
320
321     @tpath = @tree.path
322
323     if keys.key?('nodename')
324       @path = @id = keys.delete('nodename')
325     else
326       TreeNode_ID.mutex.synchronize{
327         @path = @id = TreeNode_ID.join(TkCore::INTERP._ip_id_)
328         TreeNode_ID[1].succ!
329       }
330     end
331
332     TreeNode_TBL.mutex.synchronize{
333       TreeNode_TBL[@id] = self
334       TreeNode_TBL[@tpath] = {} unless TreeNode_TBL[@tpath]
335       TreeNode_TBL[@tpath][@id] = self
336     }
337
338     @tree.insert(index, parent, @id, keys)
339   end
340
341   def tree
342     @tree
343   end
344
345   def id
346     @id
347   end
348
349   def [](key)
350     cget(key)
351   end
352
353   def []=(key, val)
354     configure(key, val)
355     val
356   end
357
358   def cget(key)
359     @tree.itemcget(@id, key)
360   end
361   def cget_strict(key)
362     @tree.itemcget_strict(@id, key)
363   end
364
365   def configure(key, val=None)
366     @tree.itemconfigure(@id, key, val)
367   end
368
369   def configinfo(key=nil)
370     @tree.itemconfiginfo(@id, key)
371   end
372
373   def current_configinfo(key=nil)
374     @tree.current_itemconfiginfo(@id, key)
375   end
376
377   def close_tree(recurse=None)
378     @tree.close_tree(@id, recurse)
379     self
380   end
381
382   def delete
383     @tree.delete(@id)
384     self
385   end
386
387   def edit(*args)
388     @tree.edit(@id, *args)
389     self
390   end
391
392   def exist?
393     @tree.exist?(@id)
394   end
395
396   def index
397     @tree.index(@id)
398   end
399
400   def move(index, parent=nil)
401     if parent
402       @tree.move(parent, @id, index)
403     else
404       @tree.move(self.parent, @id, index)
405     end
406   end
407
408   def open_tree(recurse=None)
409     @tree.open_tree(@id, recurse)
410     self
411   end
412
413   def open?
414     bool(@tree.itemcget(@id, 'open'))
415   end
416
417   def parent
418     @tree.parent(@id)
419   end
420
421   def reorder(neworder)
422     @tree.reorder(@id, neworder)
423   end
424
425   def see
426     @tree.see(@id)
427   end
428
429   def selection_add
430     @tree.selection_add(@id)
431   end
432
433   def selection_remove
434     @tree.selection_remove(@id)
435   end
436
437   def selection_set
438     @tree.selection_set(@id)
439   end
440
441   def selection_toggle
442     @tree.selection_toggle(@id)
443   end
444
445   def toggle
446     @tree.toggle(@id)
447   end
448
449   def visible
450     @tree.visible(@id)
451   end
452 end
453