2
# arrowhead widget demo (called by 'widget')
6
# This method regenerates all the text and graphics in the canvas
7
# window. It's called when the canvas is initially created, and also
8
# whenever any of the parameters of the arrow head are changed
12
# c - Name of the canvas widget.
17
# Remember the current box, if there is one.
18
tags = c.gettags('current')
20
cur = tags.find{|t| t.kind_of?(String) && t =~ /^box[1-3]$/ }
25
# Create the arrow and outline.
27
TkcLine.new(c, v.x1, v.y, v.x2, v.y,
28
{ 'width'=>10 * v.width,
29
'arrowshape'=>[10*v.a, 10*v.b, 10*v.c],
31
}.update(v.bigLineStyle) )
33
deltaY = 10*v.c + 5*v.width
34
TkcLine.new(c, v.x2, v.y, xtip, v.y + deltaY,
35
v.x2 - 10*v.a, v.y, xtip, v.y - deltaY, v.x2, v.y,
36
'width'=>2, 'capstyle'=>'round', 'joinstyle'=>'round')
38
# Create the boxes for reshaping the line and arrowhead.
39
TkcRectangle.new(c, v.x2-10*v.a-5, v.y-5, v.x2-10*v.a+5, v.y+5,
40
{'tags'=>['box1', $arrowTag_box]}.update(v.boxStyle) )
41
TkcRectangle.new(c, xtip-5, v.y-deltaY-5, xtip+5, v.y-deltaY+5,
42
{'tags'=>['box2', $arrowTag_box]}.update(v.boxStyle) )
43
TkcRectangle.new(c, v.x1-5, v.y-5*v.width-5, v.x1+5, v.y-5*v.width+5,
44
{'tags'=>['box3', $arrowTag_box]}.update(v.boxStyle) )
45
c.itemconfigure cur, v.activeStyle if cur
47
# Create three arrows in actual size with the same parameters
48
TkcLine.new(c, v.x2+50, 0, v.x2+50, 1000, 'width'=>2)
50
TkcLine.new(c, tmp, v.y-125, tmp, v.y-75, 'width'=>v.width,
51
'arrow'=>'both', 'arrowshape'=>[v.a, v.b, v.c])
52
TkcLine.new(c, tmp-25, v.y, tmp+25, v.y, 'width'=>v.width,
53
'arrow'=>'both', 'arrowshape'=>[v.a, v.b, v.c])
54
TkcLine.new(c, tmp-25, v.y+75, tmp+25, v.y+125, 'width'=>v.width,
55
'arrow'=>'both', 'arrowshape'=>[v.a, v.b, v.c])
57
# Create a bunch of other arrows and text items showing the
60
TkcLine.new(c, tmp, v.y-5*v.width, tmp, v.y-deltaY,
61
'arrow'=>'both', 'arrowshape'=>v.smallTips)
62
TkcText.new(c, v.x2+15, v.y-deltaY+5*v.c, 'text'=>v.c, 'anchor'=>'w')
64
TkcLine.new(c, tmp, v.y-5*v.width, tmp, v.y+5*v.width,
65
'arrow'=>'both', 'arrowshape'=>v.smallTips)
66
TkcText.new(c, v.x1-15, v.y, 'text'=>v.width, 'anchor'=>'e')
67
tmp = v.y+5*v.width+10*v.c+10
68
TkcLine.new(c, v.x2-10*v.a, tmp, v.x2, tmp,
69
'arrow'=>'both', 'arrowshape'=>v.smallTips)
70
TkcText.new(c, v.x2-5*v.a, tmp+5, 'text'=>v.a, 'anchor'=>'n')
72
TkcLine.new(c, v.x2-10*v.b, tmp, v.x2, tmp,
73
'arrow'=>'both', 'arrowshape'=>v.smallTips)
74
TkcText.new(c, v.x2-5*v.b, tmp+5, 'text'=>v.b, 'anchor'=>'n')
76
TkcText.new(c, v.x1, 310, 'text'=>"'width'=>#{v.width}", 'anchor'=>'w',
77
'font'=>'-*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*')
78
TkcText.new(c, v.x1, 330,
79
'text'=>"'arrowshape'=>[#{v.a}, #{v.b}, #{v.c}]", 'anchor'=>'w',
80
'font'=>'-*-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*')
85
# toplevel widget ��¸�ߤ���к������
86
if defined?($arrow_demo) && $arrow_demo
91
# demo �Ѥ� toplevel widget ������
92
$arrow_demo = TkToplevel.new {|w|
93
title("Arrowhead Editor Demonstration")
99
TkLabel.new($arrow_demo, 'font'=>$font, 'wraplength'=>'5i', 'justify'=>'left',
100
'text'=>"���� widget �ǡ������Х��ǻȤ���饤��ˤĤ����͡������������Ƭ�η����Ƥߤ뤳�Ȥ��Ǥ��ޤ���������������η����Ѥ���ˤϡ����礵�줿����ˤĤ��Ƥ��� 3�ĤλͳѤ�ɥ�å����Ƥ�����������¦����������̤��礭���ǤΥ���ץ���Ƥ��ޤ������Υƥ����Ȥϥ饤���ƥ���Ф������ꥪ�ץ����Ǥ���"){
105
$arrow_buttons = TkFrame.new($arrow_demo) {|frame|
106
TkButton.new(frame) {
110
tmppath = $arrow_demo
114
}.pack('side'=>'left', 'expand'=>'yes')
116
TkButton.new(frame) {
118
command proc{showCode 'arrow'}
119
}.pack('side'=>'left', 'expand'=>'yes')
121
$arrow_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
124
$arrow_canvas = TkCanvas.new($arrow_demo, 'width'=>500, 'height'=>350,
125
'relief'=>'sunken', 'borderwidth'=>2)
126
$arrow_canvas.pack('expand'=>'yes', 'fill'=>'both')
129
unless Struct.const_defined?("ArrowInfo")
130
$demo_arrowInfo = Struct.new("ArrowInfo", :a, :b, :c, :width, :motionProc,
131
:x1, :x2, :y, :smallTips, :count,
132
:bigLineStyle, :boxStyle, :activeStyle).new
134
$demo_arrowInfo.a = 8
135
$demo_arrowInfo.b = 10
136
$demo_arrowInfo.c = 3
137
$demo_arrowInfo.width = 2
138
$demo_arrowInfo.motionProc = proc{}
139
$demo_arrowInfo.x1 = 40
140
$demo_arrowInfo.x2 = 350
141
$demo_arrowInfo.y = 150
142
$demo_arrowInfo.smallTips = [5, 5, 2]
143
$demo_arrowInfo.count = 0
144
if TkWinfo.depth($arrow_canvas) > 1
145
$demo_arrowInfo.bigLineStyle = {'fill'=>'SkyBlue1'}
146
$demo_arrowInfo.boxStyle = {'fill'=>'', 'outline'=>'black', 'width'=>1}
147
$demo_arrowInfo.activeStyle = {'fill'=>'red', 'outline'=>'black', 'width'=>1}
149
$demo_arrowInfo.bigLineStyle = {'fill'=>'black',
150
'stipple'=>'@'+[$demo_dir,'..','images','grey.25'].join(File::Separator)}
151
$demo_arrowInfo.boxStyle = {'fill'=>'', 'outline'=>'black', 'width'=>1}
152
$demo_arrowInfo.activeStyle = {'fill'=>'black','outline'=>'black','width'=>1}
154
$arrowTag_box = TkcTag.new($arrow_canvas)
155
arrowSetup $arrow_canvas
156
$arrowTag_box.bind('Enter', proc{$arrow_canvas.itemconfigure('current', $demo_arrowInfo.activeStyle)})
157
$arrowTag_box.bind('Leave', proc{$arrow_canvas.itemconfigure('current', $demo_arrowInfo.boxStyle)})
158
$arrowTag_box.bind('B1-Enter', proc{})
159
$arrowTag_box.bind('B1-Leave', proc{})
160
$arrow_canvas.itembind('box1', '1',
161
proc{$demo_arrowInfo.motionProc \
162
= proc{|x,y| arrowMove1 $arrow_canvas, x, y}})
163
$arrow_canvas.itembind('box2', '1',
164
proc{$demo_arrowInfo.motionProc \
165
= proc{|x,y| arrowMove2 $arrow_canvas, x, y}})
166
$arrow_canvas.itembind('box3', '1',
167
proc{$demo_arrowInfo.motionProc \
168
= proc{|x,y| arrowMove3 $arrow_canvas, x, y}})
169
$arrowTag_box.bind('B1-Motion',
170
proc{|x,y| $demo_arrowInfo.motionProc.call(x,y)}, "%x %y")
171
$arrow_canvas.bind('Any-ButtonRelease-1', proc{arrowSetup $arrow_canvas})
174
# This method is called for each mouse motion event on box1 (the
175
# one at the vertex of the arrow). It updates the controlling parameters
176
# for the line and arrowhead.
179
# c - The name of the canvas window.
180
# x, y - The coordinates of the mouse.
182
def arrowMove1(c,x,y)
184
newA = (v.x2+5-c.canvasx(x).round)/10
186
newA = 25 if newA > 25
188
c.move('box1', 10*(v.a-newA), 0)
194
# This method is called for each mouse motion event on box2 (the
195
# one at the trailing tip of the arrowhead). It updates the controlling
196
# parameters for the line and arrowhead.
199
# c - The name of the canvas window.
200
# x, y - The coordinates of the mouse.
202
def arrowMove2(c,x,y)
204
newB = (v.x2+5-c.canvasx(x).round)/10
206
newB = 25 if newB > 25
207
newC = (v.y+5-c.canvasy(y).round-5*v.width)/10
209
newC = 20 if newC > 20
210
if newB != v.b || newC != v.c
211
c.move('box2', 10*(v.b-newB), 10*(v.c-newC))
218
# This method is called for each mouse motion event on box3 (the
219
# one that controls the thickness of the line). It updates the
220
# controlling parameters for the line and arrowhead.
223
# c - The name of the canvas window.
224
# x, y - The coordinates of the mouse.
226
def arrowMove3(c,x,y)
228
newWidth = (v.y+2-c.canvasy(y).round)/5
229
newWidth = 0 if newWidth < 0
230
newWidth = 20 if newWidth > 20
231
if newWidth != v.width
232
c.move('box3', 0, 5*(v.width-newWidth))