1#
2#  tkextlib/iwidgets/scrolledcanvas.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tk/canvas'
8require 'tkextlib/iwidgets.rb'
9
10module Tk
11  module Iwidgets
12    class Scrolledcanvas < Tk::Iwidgets::Scrolledwidget
13    end
14  end
15end
16
17class Tk::Iwidgets::Scrolledcanvas
18  TkCommandNames = ['::iwidgets::scrolledcanvas'.freeze].freeze
19  WidgetClassName = 'Scrolledcanvas'.freeze
20  WidgetClassNames[WidgetClassName] ||= self
21
22  ################################
23
24  def __boolval_optkeys
25    super() << 'autoresize'
26  end
27  private :__boolval_optkeys
28
29  def __strval_optkeys
30    super() << 'textbackground'
31  end
32  private :__strval_optkeys
33
34  def initialize(*args)
35    super(*args)
36    @canvas = component_widget('canvas')
37  end
38
39  def method_missing(id, *args)
40    if @canvas.respond_to?(id)
41      @canvas.__send__(id, *args)
42    else
43      super(id, *args)
44    end
45  end
46
47  ################################
48
49  def child_site
50    window(tk_call(@path, 'childsite'))
51  end
52
53  def justify(dir)
54    tk_call(@path, 'justify', dir)
55    self
56  end
57
58  ##########################
59  include TkCanvasItemConfig
60
61  def tagid(tag)
62    if tag.kind_of?(TkcItem) || tag.kind_of?(TkcTag)
63      tag.id
64    elsif tag.kind_of?(Tk::Itk::Component)
65      tag.name
66    else
67      tag  # maybe an Array of configure paramters
68    end
69  end
70  private :tagid
71
72  # create a canvas item without creating a TkcItem object
73  def create(type, *args)
74    type.create(self, *args)
75  end
76
77  #######################
78
79  def addtag(tag, mode, *args)
80    tk_send_without_enc('addtag', tagid(tag), mode, *args)
81    self
82  end
83  def addtag_above(tagOrId, target)
84    addtag(tagOrId, 'above', tagid(target))
85  end
86  def addtag_all(tagOrId)
87    addtag(tagOrId, 'all')
88  end
89  def addtag_below(tagOrId, target)
90    addtag(tagOrId, 'below', tagid(target))
91  end
92  def addtag_closest(tagOrId, x, y, halo=None, start=None)
93    addtag(tagOrId, 'closest', x, y, halo, start)
94  end
95  def addtag_enclosed(tagOrId, x1, y1, x2, y2)
96    addtag(tagOrId, 'enclosed', x1, y1, x2, y2)
97  end
98  def addtag_overlapping(tagOrId, x1, y1, x2, y2)
99    addtag(tagOrId, 'overlapping', x1, y1, x2, y2)
100  end
101  def addtag_withtag(tagOrId, tag)
102    addtag(tagOrId, 'withtag', tagid(tag))
103  end
104
105  def bbox(tagOrId, *tags)
106    list(tk_send_without_enc('bbox', tagid(tagOrId),
107                             *tags.collect{|t| tagid(t)}))
108  end
109
110  #def itembind(tag, context, cmd=Proc.new, *args)
111  #  _bind([path, "bind", tagid(tag)], context, cmd, *args)
112  #  self
113  #end
114  def itembind(tag, context, *args)
115    # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
116    if TkComm._callback_entry?(args[0]) || !block_given?
117      cmd = args.shift
118    else
119      cmd = Proc.new
120    end
121    _bind([path, "bind", tagid(tag)], context, cmd, *args)
122    self
123  end
124
125  #def itembind_append(tag, context, cmd=Proc.new, *args)
126  #  _bind_append([path, "bind", tagid(tag)], context, cmd, *args)
127  #  self
128  #end
129  def itembind_append(tag, context, *args)
130    # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
131    if TkComm._callback_entry?(args[0]) || !block_given?
132      cmd = args.shift
133    else
134      cmd = Proc.new
135    end
136    _bind_append([path, "bind", tagid(tag)], context, cmd, *args)
137    self
138  end
139
140  def itembind_remove(tag, context)
141    _bind_remove([path, "bind", tagid(tag)], context)
142    self
143  end
144
145  def itembindinfo(tag, context=nil)
146    _bindinfo([path, "bind", tagid(tag)], context)
147  end
148
149  def canvasx(screen_x, *args)
150    #tk_tcl2ruby(tk_send_without_enc('canvasx', screen_x, *args))
151    number(tk_send_without_enc('canvasx', screen_x, *args))
152  end
153  def canvasy(screen_y, *args)
154    #tk_tcl2ruby(tk_send_without_enc('canvasy', screen_y, *args))
155    number(tk_send_without_enc('canvasy', screen_y, *args))
156  end
157
158  def coords(tag, *args)
159    if args == []
160      tk_split_list(tk_send_without_enc('coords', tagid(tag)))
161    else
162      tk_send_without_enc('coords', tagid(tag), *(args.flatten))
163      self
164    end
165  end
166
167  def dchars(tag, first, last=None)
168    tk_send_without_enc('dchars', tagid(tag),
169                        _get_eval_enc_str(first), _get_eval_enc_str(last))
170    self
171  end
172
173  def delete(*args)
174    tbl = nil
175    TkcItem::CItemID_TBL.mutex.synchronize{
176      tbl = TkcItem::CItemID_TBL[self.path]
177    }
178    if tbl
179      find('withtag', *args).each{|item|
180        if item.kind_of?(TkcItem)
181          TkcItem::CItemID_TBL.mutex.synchronize{
182            tbl.delete(item.id)
183          }
184        end
185      }
186    end
187    tk_send_without_enc('delete', *args.collect{|t| tagid(t)})
188    self
189  end
190  alias remove delete
191
192  def dtag(tag, tag_to_del=None)
193    tk_send_without_enc('dtag', tagid(tag), tag_to_del)
194    self
195  end
196
197  def find(mode, *args)
198    list(tk_send_without_enc('find', mode, *args)).collect!{|id|
199      TkcItem.id2obj(self, id)
200    }
201  end
202  def find_above(target)
203    find('above', tagid(target))
204  end
205  def find_all
206    find('all')
207  end
208  def find_below(target)
209    find('below', tagid(target))
210  end
211  def find_closest(x, y, halo=None, start=None)
212    find('closest', x, y, halo, start)
213  end
214  def find_enclosed(x1, y1, x2, y2)
215    find('enclosed', x1, y1, x2, y2)
216  end
217  def find_overlapping(x1, y1, x2, y2)
218    find('overlapping', x1, y1, x2, y2)
219  end
220  def find_withtag(tag)
221    find('withtag', tag)
222  end
223
224  def itemfocus(tagOrId=nil)
225    if tagOrId
226      tk_send_without_enc('focus', tagid(tagOrId))
227      self
228    else
229      ret = tk_send_without_enc('focus')
230      if ret == ""
231        nil
232      else
233        TkcItem.id2obj(self, ret)
234      end
235    end
236  end
237
238  def gettags(tagOrId)
239    list(tk_send_without_enc('gettags', tagid(tagOrId))).collect{|tag|
240      TkcTag.id2obj(self, tag)
241    }
242  end
243
244  def icursor(tagOrId, index)
245    tk_send_without_enc('icursor', tagid(tagOrId), index)
246    self
247  end
248
249  def index(tagOrId, idx)
250    number(tk_send_without_enc('index', tagid(tagOrId), idx))
251  end
252
253  def insert(tagOrId, index, string)
254    tk_send_without_enc('insert', tagid(tagOrId), index,
255                        _get_eval_enc_str(string))
256    self
257  end
258
259  def lower(tag, below=nil)
260    if below
261      tk_send_without_enc('lower', tagid(tag), tagid(below))
262    else
263      tk_send_without_enc('lower', tagid(tag))
264    end
265    self
266  end
267
268  def move(tag, x, y)
269    tk_send_without_enc('move', tagid(tag), x, y)
270    self
271  end
272
273  def postscript(keys)
274    tk_send("postscript", *hash_kv(keys))
275  end
276
277  def raise(tag, above=nil)
278    if above
279      tk_send_without_enc('raise', tagid(tag), tagid(above))
280    else
281      tk_send_without_enc('raise', tagid(tag))
282    end
283    self
284  end
285
286  def scale(tag, x, y, xs, ys)
287    tk_send_without_enc('scale', tagid(tag), x, y, xs, ys)
288    self
289  end
290
291  def scan_mark(x, y)
292    tk_send_without_enc('scan', 'mark', x, y)
293    self
294  end
295  def scan_dragto(x, y)
296    tk_send_without_enc('scan', 'dragto', x, y)
297    self
298  end
299
300  def select(mode, *args)
301    r = tk_send_without_enc('select', mode, *args)
302    (mode == 'item')? TkcItem.id2obj(self, r): self
303  end
304  def select_adjust(tagOrId, index)
305    select('adjust', tagid(tagOrId), index)
306  end
307  def select_clear
308    select('clear')
309  end
310  def select_from(tagOrId, index)
311    select('from', tagid(tagOrId), index)
312  end
313  def select_item
314    select('item')
315  end
316  def select_to(tagOrId, index)
317    select('to', tagid(tagOrId), index)
318  end
319
320  def itemtype(tag)
321    TkcItem.type2class(tk_send('type', tagid(tag)))
322  end
323
324  def xview(*index)
325    if index.size == 0
326      list(tk_send_without_enc('xview'))
327    else
328      tk_send_without_enc('xview', *index)
329      self
330    end
331  end
332  def xview_moveto(*index)
333    xview('moveto', *index)
334  end
335  def xview_scroll(*index)
336    xview('scroll', *index)
337  end
338
339  def yview(*index)
340    if index.size == 0
341      list(tk_send_without_enc('yview'))
342    else
343      tk_send_without_enc('yview', *index)
344      self
345    end
346  end
347  def yview_moveto(*index)
348    yview('moveto', *index)
349  end
350  def yview_scroll(*index)
351    yview('scroll', *index)
352  end
353end
354