summaryrefslogtreecommitdiff
path: root/lib/tkcanvas.rb
blob: a02db097fdf70915580a2a7fe8230bfcd922b4e7 (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
#
#		tkcanvas.rb - Tk canvas classes
#			$Date$
#			by Yukihiro Matsumoto <matz@caelum.co.jp>

require "tk"

class TkCanvas<TkWindow
  def create_self
    tk_call 'canvas', path
  end
  def tagid(tag)
    if tag.kind_of?(TkcItem)
      tag.id
    else
      tag
    end
  end
  private :tagid
  def addtag(tag, *args)
    tk_send 'addtag', tagid(tag), *args
  end
  def addtag_above(tagOrId)
    addtag('above', tagOrId)
  end
  def addtag_all
    addtag('all')
  end
  def addtag_below(tagOrId)
    addtag('below', tagOrId)
  end
  def addtag_closest(x, y, halo=None, start=None)
    addtag('closest', x, y, halo, start)
  end
  def addtag_enclosed(x1, y1, x2, y2)
    addtag('enclosed', x1, y1, x2, y2)
  end
  def addtag_overlapping(x1, y1, x2, y2)
    addtag('overlapping', x1, y1, x2, y2)
  end
  def addtag_withtag(tagOrId)
    addtag('withtag', tagOrId)
  end
  def bbox(tag)
    list(tk_send('bbox', tagid(tag)))
  end
  def itembind(tag, seq, cmd=Proc.new)
    id = install_cmd(cmd)
    tk_send 'bind', tagid(tag), "<#{seq}>", id
    @cmdtbl.push id
  end
  def canvasx(x, *args)
    tk_send 'canvasx', x, *args
  end
  def canvasy(y, *args)
    tk_send 'canvasy', y, *args
  end
  def coords(tag, *args)
    tk_send 'coords', tagid(tag), *args
  end
  def dchars(tag, first, last=None)
    tk_send 'dchars', tagid(tag), first, last
  end
  def delete(*args)
    tk_send 'delete', *args
  end
  alias remove delete
  def dtag(tag, tag_to_del=None)
    tk_send 'dtag', tagid(tag), tag_to_del
  end
  def find(*args)
    tk_send 'find', *args
  end
  def itemfocus(tag)
    tk_send 'find', tagid(tag)
  end
  def gettags(tag)
    tk_send 'gettags', tagid(tag)
  end
  def icursor(tag, index)
    tk_send 'icursor', tagid(tag), index
  end
  def index(tag)
    tk_send 'index', tagid(tag), index
  end
 def lower(tag, below=None)
    tk_send 'lower', tagid(tag), below
  end
  def move(tag, x, y)
    tk_send 'move', tagid(tag), x, y
  end
  def itemtype(tag)
    tk_send 'type', tagid(tag)
  end
  def postscript(keys)
    tk_send "postscript", *hash_kv(keys)
  end
  def raise(tag, above=None)
    tk_send 'raise', tagid(tag), above
  end
  def scale(tag, x, y, xs, ys)
    tk_send 'scale', tagid(tag), x, y, xs, ys
  end
  def scan_mark(x, y)
    tk_send 'scan', 'mark', x, y
  end
  def scan_dragto(x, y)
    tk_send 'scan', 'dragto', x, y
  end
  def select(*args)
    tk_send 'select', *args
  end
  def xview(*index)
    tk_send 'xview', *index
  end
  def yview(*index)
    tk_send 'yview', *index
  end
end

class TkcItem<TkObject
  def initialize(parent, *args)
    if not parent.kind_of?(TkCanvas)
      fail format("%s need to be TkCanvas", parent.inspect)
    end
    @c = parent
    @path = parent.path
    if args[-1].kind_of? Hash
      keys = args.pop
    end
    @id = create_self(*args)
    if keys
      tk_call @path, 'itemconfigure', @id, *hash_kv(keys)
    end
  end
  def create_self(*args) end
  private :create_self
  def id
    return @id
  end

  def configure(slot, value)
    tk_call path, 'itemconfigure', @id, "-#{slot}", value
  end

  def addtag(tag)
    @c.addtag(tag, 'withtag', @id)
  end
  def bbox
    @c.bbox(@id)
  end
  def bind(seq, cmd=Proc.new)
    @c.itembind @id, seq, cmd
  end
  def coords(*args)
    @c.coords @id, *args
  end
  def dchars(first, last=None)
    @c.dchars @id, first, last
  end
  def dtag(ttd)
    @c.dtag @id, ttd
  end
  def focus
    @c.focus @id
  end
  def gettags
    @c.gettags @id
  end
  def icursor
    @c.icursor @id
  end
  def index
    @c.index @id
  end
  def insert(beforethis, string)
    @c.insert @id, beforethis, string
  end
  def lower(belowthis=None)
    @c.lower @id, belowthis
  end
  def move(xamount, yamount)
    @c.move @id, xamount, yamount
  end
  def raise(abovethis=None)
    @c.raise @id, abovethis
  end
  def scale(xorigin, yorigin, xscale, yscale)
    @c.scale @id, xorigin, yorigin, xscale, yscale
  end
  def itemtype
    @c.itemtype @id
  end
  def destroy
    tk_call path, 'delete', @id
  end
end

class TkcArc<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'arc', *args)
  end
end
class TkcBitmap<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'bitmap', *args)
  end
end
class TkcImage<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'image', *args)
  end
end
class TkcLine<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'line', *args)
  end
end
class TkcOval<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'oval', *args)
  end
end
class TkcPolygon<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'polygon', *args)
  end
end
class TkcRectangle<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'rectangle', *args)
  end
end
class TkcText<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'text', *args)
  end
end
class TkcWindow<TkcItem
  def create_self(*args)
    tk_call(@path, 'create', 'window', *args)
  end
end
class TkcGroup<TkcItem
  $tk_group_id = 'tkg00000'
  def create_self(*args)
    @id = $tk_group_id
    $tk_group_id = $tk_group_id.succ
  end
  
  def add(*tags)
    for i in tags
      i.addtag @id
    end
  end
  def add(*tags)
    for i in tags
      i.addtag @id
    end
  end
  def delete(*tags)
    for i in tags
      i.delete @id
    end
  end
  def list
    @c.find 'withtag', @id
  end
  alias remove delete
end


class TkImage<TkObject
  include Tk

  $tk_image_id = 'i00000'
  def initialize(keys=nil)
    @path = $tk_image_id
    $tk_image_id = $tk_image_id.succ
    tk_call 'image', 'create', @type, @path, *hash_kv(keys)
  end

  def height
    number(tk_call('image', 'height', @path))
  end
  def itemtype
    tk_call('image', 'type', @path)
  end
  def width
    number(tk_call('image', 'height', @path))
  end

  def TkImage.names
    tk_call('image', 'names', @path).split
  end
  def TkImage.types
    tk_call('image', 'types', @path).split
  end
end

class TkBitmapImage<TkImage
  def initialize(*args)
    @type = 'bitmap'
    super
  end
end

class TkPhotoImage<TkImage
  def initialize(*args)
    @type = 'photo'
    super
  end

  def blank
    tk_send 'blank'
  end
  def cget
    tk_send 'cget'
  end
  def get(x, y)
    tk_send 'get', x, y
  end
  def put(data, to=None)
    tk_send 'put', data, to
  end
end