3
# This demonstration script creates a canvas widget that displays a ruler
4
# with tab stops that can be set, moved, and deleted.
6
# ruler widget demo (called by 'widget')
10
# This method creates a new triangular polygon in a canvas to
11
# represent a tab stop.
14
# c - The canvas window.
15
# x, y - Coordinates at which to create the tab stop.
19
TkcPolygon.new(c, x, y, x+v.size, y+v.size, x-v.size, y+v.size)
23
if defined?($ruler_demo) && $ruler_demo
28
# demo toplevel widget
29
$ruler_demo = TkToplevel.new {|w|
30
title("Ruler Demonstration")
36
TkLabel.new($ruler_demo, 'font'=>$font, 'wraplength'=>'5i', 'justify'=>'left',
37
'text'=>"This canvas widget shows a mock-up of a ruler. You can create tab stops by dragging them out of the well to the right of the ruler. You can also drag existing tab stops. If you drag a tab stop far enough up or down so that it turns dim, it will be deleted when you release the mouse button."){
42
$ruler_buttons = TkFrame.new($ruler_demo) {|frame|
50
}.pack('side'=>'left', 'expand'=>'yes')
54
command proc{showCode 'ruler'}
55
}.pack('side'=>'left', 'expand'=>'yes')
57
$ruler_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
60
$ruler_canvas = TkCanvas.new($ruler_demo, 'width'=>'14.8c', 'height'=>'2.5c')
61
$ruler_canvas.pack('side'=>'top', 'fill'=>'x')
64
unless Struct.const_defined?("RulerInfo")
65
$demo_rulerInfo = Struct.new("RulerInfo", :grid, :left, :right, :x, :y,
66
:top, :bottom, :size, :normalStyle,
67
:activeStyle, :deleteStyle).new
69
$demo_rulerInfo.grid = '.25c'
70
$demo_rulerInfo.left = TkWinfo.fpixels($ruler_canvas, '1c')
71
$demo_rulerInfo.right = TkWinfo.fpixels($ruler_canvas, '13c')
72
$demo_rulerInfo.top = TkWinfo.fpixels($ruler_canvas, '1c')
73
$demo_rulerInfo.bottom = TkWinfo.fpixels($ruler_canvas, '1.5c')
74
$demo_rulerInfo.size = TkWinfo.fpixels($ruler_canvas, '.2c')
75
$demo_rulerInfo.normalStyle = {'fill'=>'black'}
76
if TkWinfo.depth($ruler_canvas) > 1
77
$demo_rulerInfo.activeStyle = {'fill'=>'red', 'stipple'=>''}
78
$demo_rulerInfo.deleteStyle = {'fill'=>'red',
79
'stipple'=>'@'+[$demo_dir, '..',
80
'images', 'gray25.xbm'].join(File::Separator)}
82
$demo_rulerInfo.activeStyle = {'fill'=>'black', 'stipple'=>''}
83
$demo_rulerInfo.deleteStyle = {'fill'=>'black',
84
'stipple'=>'@'+[$demo_dir, '..',
85
'images', 'gray25.xbm'].join(File::Separator)}
88
TkcLine.new($ruler_canvas,
89
'1c', '0.5c', '1c', '1c', '13c', '1c', '13c', '0.5c', 'width'=>1)
92
TkcLine.new($ruler_canvas, "#{x}c", '1c', "#{x}c", '0.6c', 'width'=>1)
93
TkcLine.new($ruler_canvas, "#{x}.25c", '1c', "#{x}.25c", '0.8c', 'width'=>1)
94
TkcLine.new($ruler_canvas, "#{x}.5c", '1c', "#{x}.5c", '0.7c', 'width'=>1)
95
TkcLine.new($ruler_canvas, "#{x}.75c", '1c', "#{x}.75c", '0.8c', 'width'=>1)
96
TkcText.new($ruler_canvas, "#{x}.15c", '0.75c', 'text'=>i, 'anchor'=>'sw')
99
$rulerTag_well = TkcTag.new($ruler_canvas)
101
.addtag_withtag($rulerTag_well,
102
TkcRectangle.new($ruler_canvas,
103
'13.2c', '1c', '13.8c', '0.5c',
105
'fill'=>($ruler_canvas\
106
.configinfo('background'))[4]) )
108
.addtag_withtag($rulerTag_well,
109
rulerMkTab($ruler_canvas,
110
TkWinfo.pixels($ruler_canvas, '13.5c'),
111
TkWinfo.pixels($ruler_canvas, '.65c') ) )
113
$rulerTag_well.bind('1', proc{|x,y| rulerNewTab($ruler_canvas,x,y)}, '%x %y')
114
$ruler_canvas.itembind('tab', '1',
115
proc{|x,y| rulerSelectTab($ruler_canvas,x,y)}, '%x %y')
116
$ruler_canvas.bind('B1-Motion',
117
proc{|x,y| rulerMoveTab($ruler_canvas,x,y)}, '%x %y')
118
$ruler_canvas.bind('Any-ButtonRelease-1', proc{rulerReleaseTab($ruler_canvas)})
121
# Does all the work of creating a tab stop, including creating the
122
# triangle object and adding tags to it to give it tab behavior.
125
# c - The canvas window.
126
# x, y - The coordinates of the tab stop.
128
def rulerNewTab(c,x,y)
130
c.addtag_withtag('active', rulerMkTab(c,x,y))
131
c.addtag_withtag('tab', 'active')
138
# This method is invoked when mouse button 1 is pressed over
139
# a tab. It remembers information about the tab so that it can
140
# be dragged interactively.
143
# c - The canvas widget.
144
# x, y - The coordinates of the mouse (identifies the point by
145
# which the tab was picked up for dragging).
147
def rulerSelectTab(c,x,y)
149
v.x = c.canvasx(x, v.grid)
151
c.addtag_withtag('active', 'current')
152
c.itemconfigure('active', v.activeStyle)
157
# This method is invoked during mouse motion events to drag a tab.
158
# It adjusts the position of the tab, and changes its appearance if
159
# it is about to be dragged out of the ruler.
162
# c - The canvas widget.
163
# x, y - The coordinates of the mouse.
165
def rulerMoveTab(c,x,y)
167
return if c.find_withtag('active') == []
168
cx = c.canvasx(x,v.grid)
170
cx = v.left if cx < v.left
171
cx = v.right if cx > v.right
172
if (cy >= v.top && cy <= v.bottom)
174
c.itemconfigure('active', v.activeStyle)
177
c.itemconfigure('active', v.deleteStyle)
179
c.move('active', cx-v.x, cy-v.y)
185
# This method is invoked during button release events that end
186
# a tab drag operation. It deselects the tab and deletes the tab if
187
# it was dragged out of the ruler.
190
# c - The canvas widget.
191
# x, y - The coordinates of the mouse.
193
def rulerReleaseTab(c)
195
return if c.find_withtag('active') == []
199
c.itemconfigure('active', v.normalStyle)