summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tk/canvastag.rb
blob: 4a14cd78a91c7b1d3e302cf5ac28a491dc70665f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# frozen_string_literal: false
#
# tk/canvastag.rb - methods for treating canvas tags
#
require 'tk'
require 'tk/tagfont'

module TkcTagAccess
  include TkComm
  include TkTreatTagFont
end

require 'tk/canvas'

module TkcTagAccess
  def addtag(tag)
    @c.addtag(tag, 'withtag', @id)
    self
  end

  def bbox
    @c.bbox(@id)
  end

  #def bind(seq, cmd=Proc.new, *args)
  #  @c.itembind(@id, seq, cmd, *args)
  #  self
  #end
  def bind(seq, *args)
    # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
    if TkComm._callback_entry?(args[0]) || !block_given?
      cmd = args.shift
    else
      cmd = Proc.new
    end
    @c.itembind(@id, seq, cmd, *args)
    self
  end

  #def bind_append(seq, cmd=Proc.new, *args)
  #  @c.itembind_append(@id, seq, cmd, *args)
  #  self
  #end
  def bind_append(seq, *args)
    # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
    if TkComm._callback_entry?(args[0]) || !block_given?
      cmd = args.shift
    else
      cmd = Proc.new
    end
    @c.itembind_append(@id, seq, cmd, *args)
    self
  end

  def bind_remove(seq)
    @c.itembind_remove(@id, seq)
    self
  end

  def bindinfo(seq=nil)
    @c.itembindinfo(@id, seq)
  end

  def cget_tkstring(option)
    @c.itemcget_tkstring(@id, option)
  end
  def cget(option)
    @c.itemcget(@id, option)
  end
  def cget_strict(option)
    @c.itemcget_strict(@id, option)
  end

  def configure(key, value=None)
    @c.itemconfigure(@id, key, value)
    self
  end
#  def configure(keys)
#    @c.itemconfigure @id, keys
#  end

  def configinfo(key=nil)
    @c.itemconfiginfo(@id, key)
  end

  def current_configinfo(key=nil)
    @c.current_itemconfiginfo(@id, key)
  end

  def coords(*args)
    @c.coords(@id, *args)
  end

  def dchars(first, last=None)
    @c.dchars(@id, first, last)
    self
  end

  def dtag(tag_to_del=None)
    @c.dtag(@id, tag_to_del)
    self
  end
  alias deltag dtag

  def find
    @c.find('withtag', @id)
  end
  alias list find

  def focus
    @c.itemfocus(@id)
  end

  def gettags
    @c.gettags(@id)
  end

  def icursor(index)
    @c.icursor(@id, index)
    self
  end

  def imove(idx, x, y)
    # Tcl/Tk 8.6 or later
    @c.imove(@id, idx, x, y)
    self
  end
  alias i_move imove

  def index(idx)
    @c.index(@id, idx)
  end

  def insert(beforethis, string)
    @c.insert(@id, beforethis, string)
    self
  end

  def lower(belowthis=None)
    @c.lower(@id, belowthis)
    self
  end

  def move(xamount, yamount)
    @c.move(@id, xamount, yamount)
    self
  end

  def moveto(x, y)
    # Tcl/Tk 8.6 or later
    @c.moveto(@id, x, y)
    self
  end
  alias move_to moveto

  def raise(abovethis=None)
    @c.raise(@id, abovethis)
    self
  end

  def scale(xorigin, yorigin, xscale, yscale)
    @c.scale(@id, xorigin, yorigin, xscale, yscale)
    self
  end

  def rchars(first, last, str_or_coords)
    # Tcl/Tk 8.6 or later
    @c.rchars(@id, first, last, str_or_coords)
    self
  end
  alias replace_chars rchars
  alias replace_coords rchars

  def select_adjust(index)
    @c.select('adjust', @id, index)
    self
  end
  def select_from(index)
    @c.select('from', @id, index)
    self
  end
  def select_to(index)
    @c.select('to', @id, index)
    self
  end

  def itemtype
    @c.itemtype(@id)
  end

  # Following operators support logical expressions of canvas tags
  # (for Tk8.3+).
  # If tag1.path is 't1' and tag2.path is 't2', then
  #      ltag = tag1 & tag2; ltag.path => "(t1)&&(t2)"
  #      ltag = tag1 | tag2; ltag.path => "(t1)||(t2)"
  #      ltag = tag1 ^ tag2; ltag.path => "(t1)^(t2)"
  #      ltag = - tag1;      ltag.path => "!(t1)"
  def & (tag)
    if tag.kind_of? TkObject
      TkcTagString.new(@c, '(' + @id + ')&&(' + tag.path + ')')
    else
      TkcTagString.new(@c, '(' + @id + ')&&(' + tag.to_s + ')')
    end
  end

  def | (tag)
    if tag.kind_of? TkObject
      TkcTagString.new(@c, '(' + @id + ')||(' + tag.path + ')')
    else
      TkcTagString.new(@c, '(' + @id + ')||(' + tag.to_s + ')')
    end
  end

  def ^ (tag)
    if tag.kind_of? TkObject
      TkcTagString.new(@c, '(' + @id + ')^(' + tag.path + ')')
    else
      TkcTagString.new(@c, '(' + @id + ')^(' + tag.to_s + ')')
    end
  end

  def -@
    TkcTagString.new(@c, '!(' + @id + ')')
  end
end

class TkcTag<TkObject
  include TkcTagAccess

  CTagID_TBL = TkCore::INTERP.create_table

  (Tk_CanvasTag_ID = ['ctag'.freeze, TkUtil.untrust('00000')]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }

  TkCore::INTERP.init_ip_env{
    CTagID_TBL.mutex.synchronize{ CTagID_TBL.clear }
  }

  def TkcTag.id2obj(canvas, id)
    cpath = canvas.path
    CTagID_TBL.mutex.synchronize{
      if CTagID_TBL[cpath]
        CTagID_TBL[cpath][id]? CTagID_TBL[cpath][id]: id
      else
        id
      end
    }
  end

  def initialize(parent, mode=nil, *args)
    #unless parent.kind_of?(TkCanvas)
    #  fail ArgumentError, "expect TkCanvas for 1st argument"
    #end
    @c = parent
    @cpath = parent.path
    Tk_CanvasTag_ID.mutex.synchronize{
      # @path = @id = Tk_CanvasTag_ID.join('')
      @path = @id = Tk_CanvasTag_ID.join(TkCore::INTERP._ip_id_)
      Tk_CanvasTag_ID[1].succ!
    }
    CTagID_TBL.mutex.synchronize{
      CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath]
      CTagID_TBL[@cpath][@id] = self
    }
    if mode
      tk_call_without_enc(@c.path, "addtag", @id, mode, *args)
    end
  end
  def id
    @id
  end

  def exist?
    if @c.find_withtag(@id)
      true
    else
      false
    end
  end

  def delete
    @c.delete @id
    CTagID_TBL.mutex.synchronize{
      CTagID_TBL[@cpath].delete(@id) if CTagID_TBL[@cpath]
    }
    self
  end
  alias remove  delete
  alias destroy delete

  def set_to_above(target)
    @c.addtag_above(@id, target)
    self
  end
  alias above set_to_above

  def set_to_all
    @c.addtag_all(@id)
    self
  end
  alias all set_to_all

  def set_to_below(target)
    @c.addtag_below(@id, target)
    self
  end
  alias below set_to_below

  def set_to_closest(x, y, halo=None, start=None)
    @c.addtag_closest(@id, x, y, halo, start)
    self
  end
  alias closest set_to_closest

  def set_to_enclosed(x1, y1, x2, y2)
    @c.addtag_enclosed(@id, x1, y1, x2, y2)
    self
  end
  alias enclosed set_to_enclosed

  def set_to_overlapping(x1, y1, x2, y2)
    @c.addtag_overlapping(@id, x1, y1, x2, y2)
    self
  end
  alias overlapping set_to_overlapping

  def set_to_withtag(target)
    @c.addtag_withtag(@id, target)
    self
  end
  alias withtag set_to_withtag
end

class TkcTagString<TkcTag
  def self.new(parent, name, mode=nil, *args)
    obj = nil
    CTagID_TBL.mutex.synchronize{
      if CTagID_TBL[parent.path] && CTagID_TBL[parent.path][name]
        obj = CTagID_TBL[parent.path][name]
      else
        # super(parent, name, *args)
        (obj = self.allocate).instance_eval{
          @c = parent
          @cpath = parent.path
          @path = @id = name
          CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath]
          CTagID_TBL[@cpath][@id] = self
        }
      end
    }
    if obj && mode
      tk_call_without_enc(@c.path, "addtag", @id, mode, *args)
    end
    obj
  end

  def initialize(parent, name, mode=nil, *args)
    # dummy:: not called by 'new' method

    #unless parent.kind_of?(TkCanvas)
    #  fail ArgumentError, "expect TkCanvas for 1st argument"
    #end
    @c = parent
    @cpath = parent.path
    @path = @id = name

    if mode
      tk_call_without_enc(@c.path, "addtag", @id, mode, *args)
    end
  end
end
TkcNamedTag = TkcTagString

class TkcTagAll<TkcTagString
  def self.new(parent)
    super(parent, 'all')
  end
=begin
  def initialize(parent)
    #unless parent.kind_of?(TkCanvas)
    #  fail ArgumentError, "expect TkCanvas for 1st argument"
    #end
    @c = parent
    @cpath = parent.path
    @path = @id = 'all'
    CTagID_TBL.mutex.synchronize{
      CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath]
      CTagID_TBL[@cpath][@id] = self
    }
  end
=end
end

class TkcTagCurrent<TkcTagString
  def self.new(parent)
    super(parent, 'current')
  end
=begin
  def initialize(parent)
    #unless parent.kind_of?(TkCanvas)
    #  fail ArgumentError, "expect TkCanvas for 1st argument"
    #end
    @c = parent
    @cpath = parent.path
    @path = @id = 'current'
    CTagID_TBL.mutex.synchronize{
      CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath]
      CTagID_TBL[@cpath][@id] = self
    }
  end
=end
end

class TkcGroup<TkcTag
  (Tk_cGroup_ID = ['tkcg'.freeze, TkUtil.untrust('00000')]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }

  #def create_self(parent, *args)
  def initialize(parent, *args)
    #unless parent.kind_of?(TkCanvas)
    #  fail ArgumentError, "expect TkCanvas for 1st argument"
    #end
    @c = parent
    @cpath = parent.path
    Tk_cGroup_ID.mutex.synchronize{
      # @path = @id = Tk_cGroup_ID.join('')
      @path = @id = Tk_cGroup_ID.join(TkCore::INTERP._ip_id_)
      Tk_cGroup_ID[1].succ!
    }
    CTagID_TBL.mutex.synchronize{
      CTagID_TBL[@cpath] = {} unless CTagID_TBL[@cpath]
      CTagID_TBL[@cpath][@id] = self
    }
    include(*args) if args != []
  end
  #private :create_self

  def include(*tags)
    for i in tags
      #i.addtag(@id)
      @c.addtag_withtag(@id, i)
    end
    self
  end
  alias add include

  def exclude(*tags)
    for i in tags
      #i.dtag(@id)
      @c.dtag(i, @id)
    end
    self
  end
end