Newer
Older
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
module Fox
#
# The Canvas module defines a framework similar to that provided by Tk's Canvas
# widget (and subsequent improvements, such as GNOME's Canvas and wxWindows'
# Object Graphics Library).
#
# Links
# =====
#
# Tk's Canvas Widget
# http://starship.python.net/crew/fredrik/tkmanual/canvas.html
# http://www.dci.clrc.ac.uk/Publications/Cookbook/chap4.html
#
# GNOME's Canvas Widget
# http://developer.gnome.org/doc/whitepapers/canvas/canvas.html
#
module Canvas
class CanvasError < Exception
end
class Shape
attr_accessor :x, :y, :foreground, :target, :selector
def initialize(x, y)
@enabled = true
@visible = true
@selected = false
@draggable = false
@x = x
@y = y
@foreground = FXRGB(0, 0, 0)
@target = nil
@selector = 0
end
# Return the bounding box for this shape
def bounds
FXRectangle.new(x, y, width, height)
end
# Hit test
def hit?(xpos, ypos)
(xpos >= x) && (xpos < x+width) && (ypos >= y) && (ypos < y+height)
end
# Move shape to specified position
def move(x, y)
@x, @y = x, y
end
# Resize shape to specified width and height
def resize(w, h)
end
# Move and resize the shape
def position(x, y, w, h)
move(x, y)
resize(w, h)
end
# Enable this shape
def enable
@enabled = true
end
# Disable this shape
def disable
@enabled = false
end
# Is this shape enabled?
def enabled?
@enabled
end
# Show this shape
def show
@visible = true
end
# Hide this shape
def hide
@visible = false
end
# Is this shape visible?
def visible?
@visible
end
# Select this shape
def select
@selected = true
end
# Deselect this shape
def deselect
@selected = false
end
# Is this shape selected?
def selected?
@selected
end
# Set this shape's draggability
def draggable=(d)
@draggable = d
end
# Is this shape draggable?
def draggable?
@draggable
end
# Draw this shape into the specificed device context
def draw(dc)
end
# Draws outline
def drawOutline(dc, x, y, w, h)
points = []
points << FXPoint.new(x - 0.5*w, y - 0.5*h)
points << FXPoint.new(x + 0.5*w, y)
points << FXPoint.new(x + 0.5*w, y + 0.5*h)
points << FXPoint.new(x - 0.5*w, y + 0.5*h)
points << points[0]
dc.drawLines(points)
end
# Default: make 6 control points
def makeControlPoints
end
end
class ShapeGroup
include Enumerable
# Initialize this shape group
def initialize
@shapes = []
end
# Does the group contain this shape?
def include?(shape)
@shapes.include?(shape)
end
# Add a shape to this group
def addShape(shape)
@shapes << shape
end
# Remove a shape from this group
def removeShape(shape)
@shapes.remove(shape)
end
def each
@shapes.each { |shape| yield shape }
end
def reverse_each
@shapes.reverse_each { |shape| yield shape }
end
end
class LineShape < Shape
attr_accessor :lineWidth, :lineCap, :lineJoin, :lineStyle
attr_accessor :x1, :y1, :x2, :y2
def initialize(x1, y1, x2, y2)
super(x1, y1)
@x1, @y1, @x2, @y2 = x1, y1, x2, y2
@lineWidth = 1
@lineCap = CAP_NOT_LAST
@lineJoin = JOIN_MITER
@lineStyle = LINE_SOLID
end
def width
0
end
def height
0
end
def draw(dc)
# Save old values
oldForeground = dc.foreground
oldLineWidth = dc.lineWidth
oldLineCap = dc.lineCap
oldLineJoin = dc.lineJoin
oldLineStyle = dc.lineStyle
# Set properties for this line
dc.foreground = foreground
dc.lineWidth = lineWidth
dc.lineCap = lineCap
dc.lineJoin = lineJoin
dc.lineStyle = lineStyle
# Draw the line
dc.drawLine(x1, y1, x2, y2)
# Restore old properties
dc.lineWidth = oldLineWidth
dc.lineCap = oldLineCap
dc.lineJoin = oldLineJoin
dc.lineStyle = oldLineStyle
dc.foreground = oldForeground
end
end
class RectangleShape < Shape
attr_accessor :width, :height
def initialize(x, y, w, h)
super(x, y)
@width = w
@height = h
end
def draw(dc)
oldForeground = dc.foreground
dc.foreground = foreground
dc.drawRectangle(x, y, width, height)
dc.foreground = oldForeground
end
end
class TextShape < RectangleShape
attr_reader :font, :text
def initialize(x, y, w, h, text=nil)
super(x, y, w, h)
@text = text
@font = FXApp.instance.normalFont
end
def draw(dc)
super(dc)
oldForeground = dc.foreground
oldTextFont = dc.textFont
dc.textFont = @font
dc.drawImageText(x, y, text)
dc.textFont = oldTextFont if oldTextFont
dc.foreground = oldForeground
end
end
class CircleShape < Shape
attr_accessor :radius
def initialize(x, y, radius)
super(x, y)
@radius = radius
end
def width
2*radius
end
def height
2*radius
end
def draw(dc)
oldForeground = dc.foreground
oldLineWidth = dc.lineWidth
dc.foreground = foreground
dc.lineWidth = 5 if selected?
dc.drawArc(x, y, width, height, 0, 64*180)
dc.drawArc(x, y, width, height, 64*180, 64*360)
dc.foreground = oldForeground
dc.lineWidth = oldLineWidth
end
end
class PolygonShape < Shape
end
class ImageShape < Shape
attr_accessor :image
def initialize(x, y, image)
@image = image
end
def draw(dc)
dc.drawImage(image)
end
end
# Base class for canvas selection policies
class SelectionPolicy
def initialize(canvas)
@canvas = canvas
end
def selectShape(shape, notify)
unless shape.selected?
shape.select
@canvas.updateShape(shape)
if notify && (@canvas.target != nil)
@canvas.target.handle(@canvas, MKUINT(@canvas.message, SEL_SELECTED), shape)
end
end
end
def deselectShape(shape, notify)
if shape.selected?
shape.deselect
@canvas.updateShape(shape)
if notify && (@canvas.target != nil)
@canvas.target.handle(@canvas, MKUINT(@canvas.message, SEL_DESELECTED), shape)
end
end
end
end
# Single shape selected at one time
class SingleSelectionPolicy < SelectionPolicy
def initialize(canvas)
super
end
def selectShape(shape, notify)
unless shape.selected?
@canvas.killSelection(notify)
end
super
end
end
class ShapeCanvas < FXCanvas
# Window state flags
FLAG_SHOWN = 0x00000001 # Is shown
FLAG_ENABLED = 0x00000002 # Able to receive input
FLAG_UPDATE = 0x00000004 # Is subject to GUI update
FLAG_DROPTARGET = 0x00000008 # Drop target
FLAG_FOCUSED = 0x00000010 # Has focus
FLAG_DIRTY = 0x00000020 # Needs layout
FLAG_RECALC = 0x00000040 # Needs recalculation
FLAG_TIP = 0x00000080 # Show tip
FLAG_HELP = 0x00000100 # Show help
FLAG_DEFAULT = 0x00000200 # Default widget
FLAG_INITIAL = 0x00000400 # Initial widget
FLAG_SHELL = 0x00000800 # Shell window
FLAG_ACTIVE = 0x00001000 # Window is active
FLAG_PRESSED = 0x00002000 # Button has been pressed
FLAG_KEY = 0x00004000 # Keyboard key pressed
FLAG_CARET = 0x00008000 # Caret is on
FLAG_CHANGED = 0x00010000 # Window data changed
FLAG_LASSO = 0x00020000 # Lasso mode
FLAG_TRYDRAG = 0x00040000 # Tentative drag mode
FLAG_DODRAG = 0x00080000 # Doing drag mode
FLAG_SCROLLINSIDE = 0x00100000 # Scroll only when inside
FLAG_SCROLLING = 0x00200000 # Right mouse scrolling
include Responder
attr_accessor :scene
def initialize(p, tgt=nil, sel=0, opts=FRAME_NORMAL, x=0, y=0, w=0, h=0)
# Initialize base class
super(p, tgt, sel, opts, x, y, w, h)
# Start with an empty group
@scene = ShapeGroup.new
# Selection policy
@selectionPolicy = SingleSelectionPolicy.new(self)
@flags = 0
# Map
FXMAPFUNC(SEL_PAINT, 0, "onPaint")
FXMAPFUNC(SEL_MOTION, 0, "onMotion")
FXMAPFUNC(SEL_LEFTBUTTONPRESS, 0, "onLeftBtnPress")
FXMAPFUNC(SEL_LEFTBUTTONRELEASE, 0, "onLeftBtnRelease")
FXMAPFUNC(SEL_CLICKED,0,"onClicked")
FXMAPFUNC(SEL_DOUBLECLICKED,0,"onDoubleClicked")
FXMAPFUNC(SEL_TRIPLECLICKED,0,"onTripleClicked")
FXMAPFUNC(SEL_COMMAND,0,"onCommand")
end
# Find the shape of the least depth containing this point
def findShape(x, y)
@scene.reverse_each do |shape|
return shape if shape.hit?(x, y)
end
nil
end
# Repaint
def updateShape(shape)
if @scene.include?(shape)
update
else
raise CanvasError
end
end
# Enable one shape
def enableShape(shape)
if @scene.include?(shape)
unless shape.enabled?
shape.enable
updateShape(shape)
end
else
raise CanvasError
end
end
# Disable one shape
def disableShape(shape)
if @scene.include?(shape)
if shape.enabled?
shape.disable
updateShape(shape)
end
else
raise CanvasError
end
end
# Select one shape
def selectShape(shape, notify=false)
if @scene.include?(shape)
@selectionPolicy.selectShape(shape, notify)
else
raise CanvasError
end
end
# Deselect one shape
def deselectShape(shape, notify=false)
if @scene.include?(shape)
@selectionPolicy.deselectShape(shape, notify)
else
raise CanvasError
end
end
# Kill selection
def killSelection(notify)
changes = false
@scene.each do |shape|
if shape.selected?
shape.deselect
updateShape(shape)
changes = true
if notify && (target != nil)
target.handle(self, MKUINT(message, SEL_DESELECTED), shape)
end
end
end
changes
end
# Paint
def onPaint(sender, sel, evt)
dc = FXDCWindow.new(self, evt)
dc.foreground = backColor
dc.fillRectangle(evt.rect.x, evt.rect.y, evt.rect.w, evt.rect.h)
@scene.each do |shape|
shape.draw(dc)
end
dc.end
return 1
end
# Motion
def onMotion(sender, sel, evt)
# Drag and drop mode
if (@flags & FLAG_DODRAG) != 0
handle(self, MKUINT(0, SEL_DRAGGED), evt)
return 1
end
# Tentative drag and drop
if (@flags & FLAG_TRYDRAG) != 0
if evt.moved?
@flags &= ~FLAG_TRYDRAG
if handle(this, MKUINT(0, SEL_BEGINDRAG), evt) != 0
@flags |= FLAG_DODRAG
end
end
return 1
end
end
# Left button press
def onLeftBtnPress(sender, sel, evt)
handle(self, MKUINT(0, SEL_FOCUS_SELF), evt)
if enabled?
grab
flags &= ~FLAG_UPDATE
# Give target the first chance at handling this
return 1 if target && (target.handle(self, MKUINT(message, SEL_LEFTBUTTONPRESS), evt) != 0)
# Locate shape
shape = findShape(evt.win_x, evt.win_y)
# No shape here
if shape.nil?
return 1
end
# Change current shape
@currentShape = shape
# Change item selection
if shape.enabled? && !shape.selected?
selectShape(shape, true)
end
# Are we dragging?
if shape.selected? && shape.draggable?
flags |= FLAG_TRYDRAG
end
flags |= FLAG_PRESSED
return 1
end
return 0
end
# Left button release
def onLeftBtnRelease(sender, sel, evt)
flg = @flags
if enabled?
ungrab
@flags |= FLAG_UPDATE
@flags &= ~(FLAG_PRESSED|FLAG_TRYDRAG|FLAG_LASSO|FLAG_DODRAG)
# First chance callback
return 1 if target && target.handle(self, MKUINT(message, SEL_LEFTBUTTONRELEASE), evt) != 0
# Was dragging
if (flg & FLAG_DODRAG) != 0
handle(self, MKUINT(0, SEL_ENDDRAG), evt)
return 1
end
# Must have pressed
if (flg & FLAG_PRESSED) != 0
# Change selection
if @currentShape && @currentShape.enabled?
deselectShape(@currentShape, true)
end
# Generate clicked callbacks
if evt.click_count == 1
handle(self, MKUINT(0, SEL_CLICKED), @currentShape)
elsif evt.click_count == 2
handle(self, MKUINT(0, SEL_DOUBLECLICKED), @currentShape)
elseif evt.click_count == 3
handle(self, MKUINT(0, SEL_TRIPLECLICKED), @currentShape)
end
# Generate command callback only when clicked on item
if @currentShape && @currentShape.enabled?
handle(self, MKUINT(0, SEL_COMMAND), @currentShape)
end
return 1
end
return 0
end
end
# Command message
def onCommand(sender, sel, ptr)
return target && target.handle(self, MKUINT(message, SEL_COMMAND), ptr)
end
# Clicked on canvas
def onClicked(sender, sel, ptr)
return target && target.handle(self, MKUINT(message, SEL_CLICKED), ptr)
end
# Double-clicked on canvas
def onDoubleClicked(sender, sel, ptr)
return target && target.handle(self, MKUINT(message, SEL_DOUBLECLICKED), ptr)
end
# Triple-clicked on canvas
def onTripleClicked(sender, sel, ptr)
return target && target.handle(self, MKUINT(message, SEL_TRIPLECLICKED), ptr)
end
end
end
end