1#
2#  tkextlib/tcllib/tablelist_core.rb
3#
4#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
5#
6#   * Part of tcllib extension
7#   * This file is required by 'tkextlib/tcllib/tablelist.rb' or
8#     'tkextlib/tcllib/tablelist_tile.rb'.
9#
10
11module Tk
12  module Tcllib
13    class Tablelist < TkWindow
14      if Tk::Tcllib::Tablelist_usingTile
15        PACKAGE_NAME = 'Tablelist_tile'.freeze
16      else
17        PACKAGE_NAME = 'Tablelist'.freeze
18      end
19      def self.package_name
20        PACKAGE_NAME
21      end
22
23      def self.package_version
24        begin
25          TkPackage.require(self.package_name)
26        rescue
27          ''
28        end
29      end
30
31      def self.use_Tile?
32        (Tk::Tcllib::Tablelist_usingTile)? true: false
33      end
34    end
35    TableList = Tablelist
36  end
37end
38
39module Tk::Tcllib::TablelistItemConfig
40  include TkItemConfigMethod
41
42  def _to_idx(idx)
43    if idx.kind_of?(Array)
44      idx.collect{|elem| _get_eval_string(elem)}.join(',')
45    else
46      idx
47    end
48  end
49  def _from_idx(idx)
50    return idx unless idx.kind_of?(String)
51
52    if idx[0] == ?@  # '@x,y'
53      idx
54    elsif idx =~ /([^,]+),([^,]+)/
55      row = $1, column = $2
56      [num_or_str(row), num_or_str(column)]
57    else
58      num_or_str(idx)
59    end
60  end
61  private :_to_idx, :_from_idx
62
63  def __item_cget_cmd(mixed_id)
64    [self.path, mixed_id[0] + 'cget', _to_idx(mixed_id[1])]
65  end
66  def __item_config_cmd(mixed_id)
67    [self.path, mixed_id[0] + 'configure', _to_idx(mixed_id[1])]
68  end
69
70  def cell_cget_tkstring(tagOrId, option)
71    itemcget_tkstring(['cell', tagOrId], option)
72  end
73  def cell_cget(tagOrId, option)
74    itemcget(['cell', tagOrId], option)
75  end
76  def cell_cget_strict(tagOrId, option)
77    itemcget_strict(['cell', tagOrId], option)
78  end
79  def cell_configure(tagOrId, slot, value=None)
80    itemconfigure(['cell', tagOrId], slot, value)
81  end
82  def cell_configinfo(tagOrId, slot=nil)
83    itemconfiginfo(['cell', tagOrId], slot)
84  end
85  def current_cell_configinfo(tagOrId, slot=nil)
86    current_itemconfiginfo(['cell', tagOrId], slot)
87  end
88  alias cellcget_tkstring cell_cget_tkstring
89  alias cellcget cell_cget
90  alias cellcget_strict cell_cget_strict
91  alias cellconfigure cell_configure
92  alias cellconfiginfo cell_configinfo
93  alias current_cellconfiginfo current_cell_configinfo
94
95  def column_cget_tkstring(tagOrId, option)
96    itemcget_tkstring(['column', tagOrId], option)
97  end
98  def column_cget(tagOrId, option)
99    itemcget(['column', tagOrId], option)
100  end
101  def column_cget_strict(tagOrId, option)
102    itemcget_strict(['column', tagOrId], option)
103  end
104  def column_configure(tagOrId, slot, value=None)
105    itemconfigure(['column', tagOrId], slot, value)
106  end
107  def column_configinfo(tagOrId, slot=nil)
108    itemconfiginfo(['column', tagOrId], slot)
109  end
110  def current_column_configinfo(tagOrId, slot=nil)
111    current_itemconfiginfo(['column', tagOrId], slot)
112  end
113  alias columncget_tkstring column_cget_tkstring
114  alias columncget column_cget
115  alias columncget_strict column_cget_strict
116  alias columnconfigure column_configure
117  alias columnconfiginfo column_configinfo
118  alias current_columnconfiginfo current_column_configinfo
119
120  def row_cget_tkstring(tagOrId, option)
121    itemcget_tkstring(['row', tagOrId], option)
122  end
123  def row_cget(tagOrId, option)
124    itemcget(['row', tagOrId], option)
125  end
126  def row_cget_strict(tagOrId, option)
127    itemcget_strict(['row', tagOrId], option)
128  end
129  def row_configure(tagOrId, slot, value=None)
130    itemconfigure(['row', tagOrId], slot, value)
131  end
132  def row_configinfo(tagOrId, slot=nil)
133    itemconfiginfo(['row', tagOrId], slot)
134  end
135  def current_row_configinfo(tagOrId, slot=nil)
136    current_itemconfiginfo(['row', tagOrId], slot)
137  end
138  alias rowcget_tkstring row_cget_tkstring
139  alias rowcget row_cget
140  alias rowcget_strict row_cget_strict
141  alias rowconfigure row_configure
142  alias rowconfiginfo row_configinfo
143  alias current_rowconfiginfo current_row_configinfo
144
145  private :itemcget_tkstring, :itemcget, :itemcget_strict
146  private :itemconfigure, :itemconfiginfo, :current_itemconfiginfo
147end
148
149class Tk::Tcllib::Tablelist
150  include Tk::Tcllib::TablelistItemConfig
151  include Scrollable
152
153  TkCommandNames = ['::tablelist::tablelist'.freeze].freeze
154  WidgetClassName = 'Tablelist'.freeze
155  WidgetClassNames[WidgetClassName] ||= self
156
157  def create_self(keys)
158    if keys and keys != None
159      tk_call_without_enc(self.class::TkCommandNames[0], @path,
160                          *hash_kv(keys, true))
161    else
162      tk_call_without_enc(self.class::TkCommandNames[0], @path)
163    end
164  end
165  private :create_self
166
167  ##########################
168
169  def __numval_optkeys
170    super() + ['titlecolumns']
171  end
172  private :__numval_optkeys
173
174  def __strval_optkeys
175    super() + ['snipstring']
176  end
177  private :__strval_optkeys
178
179  def __boolval_optkeys
180    super() - ['takefocus'] + [
181      'forceeditendcommand', 'movablecolumns', 'movablerows',
182      'protecttitlecolumns', 'resizablecolumns', 'setfocus',
183      'showarrow', 'showlabels', 'showseparators'
184    ]
185  end
186  private :__boolval_optkeys
187
188  def __listval_optkeys
189    super() + ['columns', 'columntitles']
190  end
191  private :__listval_optkeys
192
193  def __tkvariable_optkeys
194    super() + ['listvariable']
195  end
196  private :__tkvariable_optkeys
197
198  def __val2ruby_optkeys  # { key=>proc, ... }
199    # The method is used to convert a opt-value to a ruby's object.
200    # When get the value of the option "key", "proc.call(value)" is called.
201    super().update('stretch'=>proc{|v|
202                     (v == 'all')? v: simplelist(v)
203                   },
204                   'takefocus'=>proc{|v|
205                     case v
206                     when '1'
207                       true
208                     when '0'
209                       false
210                     when ''
211                       nil
212                     else # cmd
213                       tk_tcl2ruby(cmd)
214                       end
215                   })
216  end
217  private :__val2ruby_optkeys
218
219  def __ruby2val_optkeys  # { key=>proc, ... }
220    # The method is used to convert a ruby's object to a opt-value.
221    # When set the value of the option "key", "proc.call(value)" is called.
222    # That is, "-#{key} #{proc.call(value)}".
223    super().update('stretch'=>proc{|v|
224                     (v.kind_of?(Array))? v.collect{|e| _to_idx(e)}: v
225                   },
226                   'takefocus'=>proc{|v|
227                     case v
228                     when true
229                       '1'
230                     when false
231                       '0'
232                     when nil
233                       ''
234                     else
235                       _get_eval_string(v)
236                     end
237                   })
238  end
239  private :__ruby2val_optkeys
240
241  def __font_optkeys
242    super() + ['labelfont']
243  end
244  private :__font_optkeys
245
246  ##########################
247
248  def __item_strval_optkeys(id)
249    if id[0] == 'cell'
250      super(id) + ['title']
251    else
252      super(id) - ['text'] + ['title', 'name']
253    end
254  end
255  private :__item_strval_optkeys
256
257  def __item_boolval_optkeys(id)
258    super(id) + [
259      'changesnipside', 'editable', 'hide', 'resizable', 'selectable',
260      'showarrow', 'showlinenumbers', 'stretchable', 'stretchwindow', 'wrap'
261    ]
262  end
263  private :__item_boolval_optkeys
264
265  def __item_listval_optkeys(id)
266    if id[0] == 'cell'
267      super(id)
268    else
269      super(id) + ['text']
270    end
271  end
272  private :__item_listval_optkeys
273
274  def __item_font_optkeys(id)
275    # maybe need to override
276    super(id) + ['labelfont']
277  end
278  private :__item_font_optkeys
279
280  ##########################
281
282  def activate(index)
283    tk_send('activate', _to_idx(index))
284    self
285  end
286
287  def activate_cell(index)
288    tk_send('activatecell', _to_idx(index))
289    self
290  end
291  alias activatecell activate_cell
292
293  def get_attrib(name=nil)
294    if name && name != None
295      tk_send('attrib', name)
296    else
297      ret = []
298      lst = simplelist(tk_send('attrib'))
299      until lst.empty?
300        ret << ( [lst.shift] << lst.shift )
301      end
302      ret
303    end
304  end
305  def set_attrib(*args)
306    tk_send('attrib', *(args.flatten))
307    self
308  end
309
310  def bbox(index)
311    list(tk_send('bbox', _to_idx(index)))
312  end
313
314  def bodypath
315    window(tk_send('bodypath'))
316  end
317
318  def bodytag
319    TkBindTag.new_by_name(tk_send('bodytag'))
320  end
321
322  def cancel_editing
323    tk_send('cancelediting')
324    self
325  end
326  alias cancelediting cancel_editing
327
328  def get_cellattrib(name=nil)
329    if name && name != None
330      tk_send('cellattrib', name)
331    else
332      ret = []
333      lst = simplelist(tk_send('cellattrib'))
334      until lst.empty?
335        ret << ( [lst.shift] << lst.shift )
336      end
337      ret
338    end
339  end
340  def set_cellattrib(*args)
341    tk_send('cellattrib', *(args.flatten))
342    self
343  end
344
345  def cellindex(idx)
346    _from_idx(tk_send('cellindex', _to_idx(idx)))
347  end
348
349  def cellselection_anchor(idx)
350    tk_send('cellselection', 'anchor', _to_idx(idx))
351    self
352  end
353
354  def cellselection_clear(first, last=nil)
355    if first.kind_of?(Array)
356      tk_send('cellselection', 'clear', first.collect{|idx| _to_idx(idx)})
357    else
358      first = _to_idx(first)
359      last = (last)? _to_idx(last): first
360      tk_send('cellselection', 'clear', first, last)
361    end
362    self
363  end
364
365  def cellselection_includes(idx)
366    bool(tk_send('cellselection', 'includes', _to_idx(idx)))
367  end
368
369  def cellselection_set(first, last=nil)
370    if first.kind_of?(Array)
371      tk_send('cellselection', 'set', first.collect{|idx| _to_idx(idx)})
372    else
373      first = _to_idx(first)
374      last = (last)? _to_idx(last): first
375      tk_send('cellselection', 'set', first, last)
376    end
377    self
378  end
379
380  def get_columnattrib(name=nil)
381    if name && name != None
382      tk_send('columnattrib', name)
383    else
384      ret = []
385      lst = simplelist(tk_send('columnattrib'))
386      until lst.empty?
387        ret << ( [lst.shift] << lst.shift )
388      end
389      ret
390    end
391  end
392  def set_columnattrib(*args)
393    tk_send('columnattrib', *(args.flatten))
394    self
395  end
396
397  def columncount
398    number(tk_send('columncount'))
399  end
400
401  def columnindex(idx)
402    number(tk_send('columnindex', _to_idx(idx)))
403  end
404
405  def columnwidth(idx, opt=nil)
406    if opt
407      number(tk_send('columnwidth', _to_idx(idx), "-#{opt}"))
408    else
409      number(tk_send('columnwidth', _to_idx(idx)))
410    end
411  end
412  def requested_columnwidth(idx)
413    columnwidth(idx, 'requested')
414  end
415  def stretched_columnwidth(idx)
416    columnwidth(idx, 'stretched')
417  end
418  def total_columnwidth(idx)
419    columnwidth(idx, 'total')
420  end
421
422  def configcelllist(lst) # lst ==> [idx, opt, val, idx, opt, val, ...]
423    ary = []
424    lst.slice(3){|idx, opt, val|
425      ary << _to_idx(idx) << "-#{opt}" << val
426    }
427    tk_send('configcelllist', ary)
428    self
429  end
430  alias config_celllist configcelllist
431
432  def configcells(*args) # args ==> idx, opt, val, idx, opt, val, ...
433    ary = []
434    args.slice(3){|idx, opt, val|
435      ary << _to_idx(idx) << "-#{opt}" << val
436    }
437    tk_send('configcells', *ary)
438    self
439  end
440  alias config_cells configcells
441
442  def configcolumnlist(lst) # lst ==> [idx, opt, val, idx, opt, val, ...]
443    ary = []
444    lst.slice(3){|idx, opt, val|
445      ary << _to_idx(idx) << "-#{opt}" << val
446    }
447    tk_send('configcolumnlist', ary)
448    self
449  end
450  alias config_columnlist configcolumnlist
451
452  def configcolumns(*args) # args ==> idx, opt, val, idx, opt, val, ...
453    ary = []
454    args.slice(3){|idx, opt, val|
455      ary << _to_idx(idx) << "-#{opt}" << val
456    }
457    tk_send('configcolumns', *ary)
458    self
459  end
460  alias config_columns configcolumns
461
462  def configrowlist(lst) # lst ==> [idx, opt, val, idx, opt, val, ...]
463    ary = []
464    lst.slice(3){|idx, opt, val|
465      ary << _to_idx(idx) << "-#{opt}" << val
466    }
467    tk_send('configrowlist', ary)
468    self
469  end
470  alias config_rowlist configrowlist
471
472  def configrows(*args) # args ==> idx, opt, val, idx, opt, val, ...
473    ary = []
474    args.slice(3){|idx, opt, val|
475      ary << _to_idx(idx) << "-#{opt}" << val
476    }
477    tk_send('configrows', *ary)
478    self
479  end
480  alias config_rows configrows
481
482  def containing(y)
483    idx = num_or_str(tk_send('containing', y))
484    (idx.kind_of?(Fixnum) && idx < 0)?  nil: idx
485  end
486
487  def containing_cell(x, y)
488    idx = _from_idx(tk_send('containingcell', x, y))
489    if idx.kind_of?(Array)
490      [
491        ((idx[0].kind_of?(Fixnum) && idx[0] < 0)?  nil: idx[0]),
492        ((idx[1].kind_of?(Fixnum) && idx[1] < 0)?  nil: idx[1])
493      ]
494    else
495      idx
496    end
497  end
498  alias containingcell containing_cell
499
500  def containing_column(x)
501    idx = num_or_str(tk_send('containingcolumn', x))
502    (idx.kind_of?(Fixnum) && idx < 0)?  nil: idx
503  end
504  alias containingcolumn containing_column
505
506  def curcellselection
507    simplelist(tk_send('curcellselection')).collect!{|idx| _from_idx(idx)}
508  end
509
510  def curselection
511    list(tk_send('curselection'))
512  end
513
514  def delete_items(first, last=nil)
515    if first.kind_of?(Array)
516      tk_send('delete', first.collect{|idx| _to_idx(idx)})
517    else
518      first = _to_idx(first)
519      last = (last)? _to_idx(last): first
520      tk_send('delete', first, last)
521    end
522    self
523  end
524  alias delete delete_items
525  alias deleteitems delete_items
526
527  def delete_columns(first, last=nil)
528    if first.kind_of?(Array)
529      tk_send('deletecolumns', first.collect{|idx| _to_idx(idx)})
530    else
531      first = _to_idx(first)
532      last = (last)? _to_idx(last): first
533      tk_send('deletecolumns', first, last)
534    end
535    self
536  end
537  alias deletecolumns delete_columns
538
539  def edit_cell(idx)
540    tk_send('editcell', _to_idx(idx))
541    self
542  end
543  alias editcell edit_cell
544
545  def editwintag
546    TkBindTag.new_by_name(tk_send('editwintag'))
547  end
548
549  def editwinpath
550    window(tk_send('editwinpath'))
551  end
552
553  def entrypath
554    window(tk_send('entrypath'))
555  end
556
557  def fill_column(idx, txt)
558    tk_send('fillcolumn', _to_idx(idx), txt)
559    self
560  end
561  alias fillcolumn fill_column
562
563  def finish_editing
564    tk_send('finishediting')
565    self
566  end
567  alias finishediting finish_editing
568
569  def formatinfo
570    key, row, col = simplelist(tk_send('formatinfo'))
571    [key, number(row), number(col)]
572  end
573
574  def get(first, last=nil)
575    if first.kind_of?(Array)
576      simplelist(tk_send('get', first.collect{|idx| _to_idx(idx)})).collect!{|elem| simplelist(elem) }
577    else
578      first = _to_idx(first)
579      last = (last)? _to_idx(last): first
580      simplelist(tk_send('get', first, last))
581    end
582  end
583
584  def get_cells(first, last=nil)
585    if first.kind_of?(Array)
586      simplelist(tk_send('getcells', first.collect{|idx| _to_idx(idx)})).collect!{|elem| simplelist(elem) }
587    else
588      first = _to_idx(first)
589      last = (last)? _to_idx(last): first
590      simplelist(tk_send('getcells', first, last))
591    end
592  end
593  alias getcells get_cells
594
595  def get_columns(first, last=nil)
596    if first.kind_of?(Array)
597      simplelist(tk_send('getcolumns', first.collect{|idx| _to_idx(idx)})).collect!{|elem| simplelist(elem) }
598    else
599      first = _to_idx(first)
600      last = (last)? _to_idx(last): first
601      simplelist(tk_send('getcolumns', first, last))
602    end
603  end
604  alias getcolumns get_columns
605
606  def get_keys(first, last=nil)
607    if first.kind_of?(Array)
608      simplelist(tk_send('getkeys', first.collect{|idx| _to_idx(idx)})).collect!{|elem| simplelist(elem) }
609    else
610      first = _to_idx(first)
611      last = (last)? _to_idx(last): first
612      simplelist(tk_send('getkeys', first, last))
613    end
614  end
615  alias getkeys get_keys
616
617  def has_attrib?(name)
618    bool(tk_send('hasattrib', name))
619  end
620
621  def has_cellattrib?(idx, name)
622    bool(tk_send('hascellattrib', _to_idx(idx), name))
623  end
624
625  def has_columnattrib?(idx, name)
626    bool(tk_send('hascolumnattrib', _to_idx(idx), name))
627  end
628
629  def has_rowattrib?(idx, name)
630    bool(tk_send('hasrowattrib', _to_idx(idx), name))
631  end
632
633  def imagelabelpath(idx)
634    window(tk_send('imagelabelpath', _to_idx(idx)))
635  end
636
637  def index(idx)
638    number(tk_send('index', _to_idx(idx)))
639  end
640
641  def insert(idx, *items)
642    tk_send('insert', _to_idx(idx), *items)
643    self
644  end
645
646  def insert_columnlist(idx, columnlist)
647    tk_send('insertcolumnlist', _to_idx(idx), columnlist)
648    self
649  end
650  alias insertcolumnlist insert_columnlist
651
652  def insert_columns(idx, *args)
653    tk_send('insertcolums', _to_idx(idx), *args)
654    self
655  end
656  alias insertcolumns insert_columns
657
658  def insert_list(idx, list)
659    tk_send('insertlist', _to_idx(idx), list)
660    self
661  end
662  alias insertlist insert_list
663
664  def is_elem_snipped?(cellidx, tkvar)
665    bool(tk_send('iselemsnipped', _to_idx(cellidx), tkvar))
666  end
667  alias elem_snipped? is_elem_snipped?
668
669  def is_title_snipped?(colidx, tkvar)
670    bool(tk_send('istitlesnipped', _to_idx(colidx), tkvar))
671  end
672  alias title_snipped? is_title_snipped?
673
674  def itemlistvar
675    TkVarAccess.new(tk_send('itemlistvar'))
676  end
677
678  def labelpath(idx)
679    window(tk_send('labelpath', _to_idx(idx)))
680  end
681
682  def labels
683    simplelist(tk_send('labels'))
684  end
685
686  def labeltag
687    TkBindTag.new_by_name(tk_send('labeltag'))
688  end
689
690  def move(src, target)
691    tk_send('move', _to_idx(src), _to_idx(target))
692    self
693  end
694
695  def move_column(src, target)
696    tk_send('movecolumn', _to_idx(src), _to_idx(target))
697    self
698  end
699  alias movecolumn move_column
700
701  def nearest(y)
702    _from_idx(tk_send('nearest', y))
703  end
704
705  def nearest_cell(x, y)
706    _from_idx(tk_send('nearestcell', x, y))
707  end
708  alias nearestcell nearest_cell
709
710  def nearest_column(x)
711    _from_idx(tk_send('nearestcolumn', x))
712  end
713  alias nearestcolumn nearest_column
714
715  def reject_input
716    tk_send('rejectinput')
717    self
718  end
719  alias rejectinput reject_input
720
721  def reset_sortinfo
722    tk_send('resetsortinfo')
723    self
724  end
725  alias resetsortinfo reset_sortinfo
726
727  def get_rowattrib(name=nil)
728    if name && name != None
729      tk_send('rowattrib', name)
730    else
731      ret = []
732      lst = simplelist(tk_send('rowattrib'))
733      until lst.empty?
734        ret << ( [lst.shift] << lst.shift )
735      end
736      ret
737    end
738  end
739  def set_rowattrib(*args)
740    tk_send('rowattrib', *(args.flatten))
741    self
742  end
743
744  def scan_mark(x, y)
745    tk_send('scan', 'mark', x, y)
746    self
747  end
748
749  def scan_dragto(x, y)
750    tk_send('scan', 'dragto', x, y)
751    self
752  end
753
754  def see(idx)
755    tk_send('see', _to_idx(idx))
756    self
757  end
758
759  def see_cell(idx)
760    tk_send('seecell', _to_idx(idx))
761    self
762  end
763  alias seecell see_cell
764
765  def see_column(idx)
766    tk_send('seecolumn', _to_idx(idx))
767    self
768  end
769  alias seecolumn see_column
770
771  def selection_anchor(idx)
772    tk_send('selection', 'anchor', _to_idx(idx))
773    self
774  end
775
776  def selection_clear(first, last=nil)
777    if first.kind_of?(Array)
778      tk_send('selection', 'clear', first.collect{|idx| _to_idx(idx)})
779    else
780      first = _to_idx(first)
781      last = (last)? _to_idx(last): first
782      tk_send('selection', 'clear', first, last)
783    end
784    self
785  end
786
787  def selection_includes(idx)
788    bool(tk_send('selection', 'includes', _to_idx(idx)))
789  end
790
791  def selection_set(first, last=nil)
792    if first.kind_of?(Array)
793      tk_send('selection', 'set', first.collect{|idx| _to_idx(idx)})
794    else
795      first = _to_idx(first)
796      last = (last)? _to_idx(last): first
797      tk_send('selection', 'set', first, last)
798    end
799    self
800  end
801
802  def separatorpath(idx=nil)
803    if idx
804      window(tk_send('separatorpath', _to_idx(idx)))
805    else
806      window(tk_send('separatorpath'))
807    end
808  end
809
810  def separators
811    simplelist(tk_send('separators')).collect!{|w| window(w)}
812  end
813
814  def size
815    number(tk_send('size'))
816  end
817
818  def sort(order=nil)
819    if order
820      order = order.to_s
821      order = '-' << order if order[0] != ?-
822      if order.length < 2
823        order = nil
824      end
825    end
826    if order
827      tk_send('sort', order)
828    else
829      tk_send('sort')
830    end
831    self
832  end
833  def sort_increasing
834    tk_send('sort', '-increasing')
835    self
836  end
837  def sort_decreasing
838    tk_send('sort', '-decreasing')
839    self
840  end
841
842
843  # default of 'labelcommand' option
844  DEFAULT_labelcommand_value =
845    DEFAULT_sortByColumn_cmd = '::tablelist::sortByColumn'
846
847  # default of 'labelcommand2' option
848  DEFAULT_labelcommand2_value =
849    DEFAULT_addToSortColumns_cmd = '::tablelist::addToSortColumns'
850
851  def sortByColumn_with_event_generate(idx)
852    tk_call('::tablelist::sortByColumn', @path, _to_idx(idx))
853  end
854
855  def addToSortColumns_with_event_generate(idx)
856    tk_call('::tablelist::addToSortColumns', @path, _to_idx(idx))
857  end
858
859  def sort_by_column(idx, order=nil)
860    if order
861      order = order.to_s
862      order = '-' << order if order[0] != ?-
863      if order.length < 2
864        order = nil
865      end
866    end
867    if order
868      tk_send('sortbycolumn', _to_idx(idx), order)
869    else
870      tk_send('sortbycolumn', _to_idx(idx))
871    end
872    self
873  end
874  def sort_by_column_increasing(idx)
875    tk_send('sortbycolumn', _to_idx(idx), '-increasing')
876    self
877  end
878  def sort_by_column_decreasing(idx)
879    tk_send('sortbycolumn', _to_idx(idx), '-decreasing')
880    self
881  end
882
883  def sort_by_columnlist(idxlist, orderlist=None)
884    # orderlist :: list of 'increasing' or 'decreasing'
885    tk_send('sortbycolumnlist', idxlist.map{|idx| _to_idx(idx)}, orderlist)
886    self
887  end
888
889  def sortcolumn
890    idx = num_or_str(tk_send('sortcolum'))
891    (idx.kind_of?(Fixnum) && idx < 0)?  nil: idx
892  end
893
894  def sortcolumnlist
895    simplelist(tk_send('sortcolumlist')).map{|col| num_or_str(col)}
896  end
897
898  def sortorder
899    tk_send('sortorder')
900  end
901
902  def sortorderlist
903    simplelist(tk_send('sortorderlist'))
904  end
905
906  def toggle_columnhide(first, last=nil)
907    if first.kind_of?(Array)
908      tk_send('togglecolumnhide', first.collect{|idx| _to_idx(idx)})
909    else
910      first = _to_idx(first)
911      last = (last)? _to_idx(last): first
912      tk_send('togglecolumnhide', first, last)
913    end
914  end
915
916  def toggle_rowhide(first, last=nil)
917    if first.kind_of?(Array)
918      tk_send('togglerowhide', first.collect{|idx| _to_idx(idx)})
919    else
920      first = _to_idx(first)
921      last = (last)? _to_idx(last): first
922      tk_send('togglerowhide', first, last)
923    end
924  end
925
926  def toggle_visibility(first, last=nil)
927    if first.kind_of?(Array)
928      tk_send('togglevisibility', first.collect{|idx| _to_idx(idx)})
929    else
930      first = _to_idx(first)
931      last = (last)? _to_idx(last): first
932      tk_send('togglevisibility', first, last)
933    end
934    self
935  end
936  alias togglevisibility toggle_visibility
937
938  def unset_attrib(name)
939    tk_send('unsetattrib', name)
940    self
941  end
942
943  def unset_cellattrib(idx, name)
944    tk_send('unsetcellattrib', _to_idx(idx), name)
945    self
946  end
947
948  def unset_columnattrib(idx, name)
949    tk_send('unsetcolumnattrib', _to_idx(idx), name)
950    self
951  end
952
953  def unset_rowattrib(idx, name)
954    tk_send('unsetrowattrib', _to_idx(idx), name)
955    self
956  end
957
958  def windowpath(idx)
959    window(tk_send('windowpath', _to_idx(idx)))
960  end
961end
962
963class << Tk::Tcllib::Tablelist
964  ############################################################
965  # helper commands
966  def getTablelistPath(descendant)
967    window(Tk.tk_call('::tablelist::getTablelistPath', descendant))
968  end
969
970  def getTablelistColumn(descendant)
971    num_or_str(Tk.tk_call('::tablelist::getTablelistColumn', headerlabel))
972  end
973
974
975  def convEventFields(descendant, x, y)
976    window(Tk.tk_call('::tablelist::convEventFields', descendant, x, y))
977  end
978
979
980  ############################################################
981  # with the BWidget package
982  def addBWidgetEntry(name=None)
983    Tk.tk_call('::tablelist::addBWidgetEntry', name)
984  end
985
986  def addBWidgetSpinBox(name=None)
987    Tk.tk_call('::tablelist::addBWidgetSpinBox', name)
988  end
989
990  def addBWidgetComboBox(name=None)
991    Tk.tk_call('::tablelist::addBWidgetComboBox', name)
992  end
993
994
995  ############################################################
996  # with the Iwidgets ([incr Widgets]) package
997  def addIncrEntryfield(name=None)
998    Tk.tk_call('::tablelist::addIncrEntry', name)
999  end
1000
1001  def addIncrDateTimeWidget(type, seconds=false, name=None)
1002    # type := 'datefield'|'dateentry'|timefield'|'timeentry'
1003    if seconds && seconds != None
1004      seconds = '-seconds'
1005    else
1006      seconds = None
1007    end
1008    Tk.tk_call('::tablelist::addDateTimeWidget', type, seconds, name)
1009  end
1010
1011  def addIncrSpinner(name=None)
1012    Tk.tk_call('::tablelist::addIncrSpinner', name)
1013  end
1014
1015  def addIncrSpinint(name=None)
1016    Tk.tk_call('::tablelist::addIncrSpinint', name)
1017  end
1018
1019  def addIncrCombobox(name=None)
1020    Tk.tk_call('::tablelist::addIncrCombobox', name)
1021  end
1022
1023
1024  ############################################################
1025  # with Bryan Oakley's combobox package
1026  def addOakleyCombobox(name=None)
1027    Tk.tk_call('::tablelist::addOakleyCombobox', name)
1028  end
1029
1030  ############################################################
1031  # with the multi-entry package Mentry is a library extension
1032  def addDateMentry(format, separator, gmt=false, name=None)
1033    if gmt && gmt != None
1034      gmt = '-gmt'
1035    else
1036      gmt = None
1037    end
1038    Tk.tk_call('::tablelist::addDateMentry', format, separator, gmt, name)
1039  end
1040
1041  def addTimeMentry(format, separator, gmt=false, name=None)
1042    if gmt && gmt != None
1043      gmt = '-gmt'
1044    else
1045      gmt = None
1046    end
1047    Tk.tk_call('::tablelist::addTimeMentry', format, separator, gmt, name)
1048  end
1049
1050  def addDateTimeMentry(format, date_sep, time_sep, gmt=false, name=None)
1051    if gmt && gmt != None
1052      gmt = '-gmt'
1053    else
1054      gmt = None
1055    end
1056    Tk.tk_call('::tablelist::addDateTimeMentry',
1057               format, date_sep, time_sep, gmt, name)
1058  end
1059
1060  def addFixedPointMentry(count1, count2, comma=false, name=None)
1061    if comma && comma != None
1062      comma = '-comma'
1063    else
1064      comma = None
1065    end
1066    Tk.tk_call('::tablelist::addFixedPoingMentry', count1, count2, comma, name)
1067  end
1068
1069  def addIPAddrMentry(name=None)
1070    Tk.tk_call('::tablelist::addIPAddrMentry', name)
1071  end
1072end
1073