1#
2#  tkextlib/iwidgets/scrolledlistbox.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tk/listbox'
8require 'tkextlib/iwidgets.rb'
9
10module Tk
11  module Iwidgets
12    class Scrolledlistbox < Tk::Iwidgets::Scrolledwidget
13    end
14  end
15end
16
17class Tk::Iwidgets::Scrolledlistbox
18  TkCommandNames = ['::iwidgets::scrolledlistbox'.freeze].freeze
19  WidgetClassName = 'Scrolledlistbox'.freeze
20  WidgetClassNames[WidgetClassName] ||= self
21
22  def __strval_optkeys
23    super() << 'textbackground'
24  end
25  private :__strval_optkeys
26
27  def __tkvariable_optkeys
28    super() << 'listvariable'
29  end
30  private :__tkvariable_optkeys
31
32  def __font_optkeys
33    super() << 'textfont'
34  end
35  private :__font_optkeys
36
37  ################################
38
39  def initialize(*args)
40    super(*args)
41    @listbox = component_widget('listbox')
42  end
43
44  def method_missing(id, *args)
45    if @listbox.respond_to?(id)
46      @listbox.__send__(id, *args)
47    else
48      super(id, *args)
49    end
50  end
51
52  ################################
53
54  def clear
55    tk_call(@path, 'clear')
56    self
57  end
58
59  def get_curselection
60    tk_call(@path, 'getcurselection')
61  end
62
63  def justify(dir)
64    tk_call(@path, 'justify', dir)
65    self
66  end
67
68  def selected_item_count
69    number(tk_call(@path, 'selecteditemcount'))
70  end
71
72  def sort(*params, &b)
73    # see 'lsort' man page about params
74    if b
75      tk_call(@path, 'sort', '-command', proc(&b), *params)
76    else
77      tk_call(@path, 'sort', *params)
78    end
79    self
80  end
81  def sort_ascending
82    tk_call(@path, 'sort', 'ascending')
83    self
84  end
85  def sort_descending
86    tk_call(@path, 'sort', 'descending')
87    self
88  end
89
90  #####################################
91
92  def bbox(index)
93    list(tk_send_without_enc('bbox', index))
94  end
95  def delete(first, last=None)
96    tk_send_without_enc('delete', first, last)
97    self
98  end
99  def get(*index)
100    _fromUTF8(tk_send_without_enc('get', *index))
101  end
102  def insert(index, *args)
103    tk_send('insert', index, *args)
104    self
105  end
106  def scan_mark(x, y)
107    tk_send_without_enc('scan', 'mark', x, y)
108    self
109  end
110  def scan_dragto(x, y)
111    tk_send_without_enc('scan', 'dragto', x, y)
112    self
113  end
114  def see(index)
115    tk_send_without_enc('see', index)
116    self
117  end
118
119  #####################################
120
121  include TkListItemConfig
122
123  def tagid(tag)
124    if tag.kind_of?(Tk::Itk::Component)
125      tag.name
126    else
127      super(tag)
128    end
129  end
130  private :tagid
131
132  #####################################
133
134  def activate(y)
135    tk_send_without_enc('activate', y)
136    self
137  end
138  def curselection
139    list(tk_send_without_enc('curselection'))
140  end
141  def get(first, last=nil)
142    if last
143      # tk_split_simplelist(_fromUTF8(tk_send_without_enc('get', first, last)))
144      tk_split_simplelist(tk_send_without_enc('get', first, last),
145                          false, true)
146    else
147      _fromUTF8(tk_send_without_enc('get', first))
148    end
149  end
150  def nearest(y)
151    tk_send_without_enc('nearest', y).to_i
152  end
153  def size
154    tk_send_without_enc('size').to_i
155  end
156  def selection_anchor(index)
157    tk_send_without_enc('selection', 'anchor', index)
158    self
159  end
160  def selection_clear(first, last=None)
161    tk_send_without_enc('selection', 'clear', first, last)
162    self
163  end
164  def selection_includes(index)
165    bool(tk_send_without_enc('selection', 'includes', index))
166  end
167  def selection_set(first, last=None)
168    tk_send_without_enc('selection', 'set', first, last)
169    self
170  end
171
172  def index(idx)
173    tk_send_without_enc('index', idx).to_i
174  end
175
176  #####################################
177
178  def xview(*index)
179    if index.size == 0
180      list(tk_send_without_enc('xview'))
181    else
182      tk_send_without_enc('xview', *index)
183      self
184    end
185  end
186  def xview_moveto(*index)
187    xview('moveto', *index)
188  end
189  def xview_scroll(*index)
190    xview('scroll', *index)
191  end
192
193  def yview(*index)
194    if index.size == 0
195      list(tk_send_without_enc('yview'))
196    else
197      tk_send_without_enc('yview', *index)
198      self
199    end
200  end
201  def yview_moveto(*index)
202    yview('moveto', *index)
203  end
204  def yview_scroll(*index)
205    yview('scroll', *index)
206  end
207end
208