~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/demos-jp/arrow.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# arrowhead widget demo (called by 'widget')
 
3
#
 
4
 
 
5
# arrowSetup --
 
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
 
9
# interactively.
 
10
#
 
11
# Arguments:
 
12
# c -           Name of the canvas widget.
 
13
 
 
14
def arrowSetup(c)
 
15
  v = $demo_arrowInfo
 
16
 
 
17
  # Remember the current box, if there is one.
 
18
  tags = c.gettags('current')
 
19
  if tags != []
 
20
    cur = tags.find{|t| t.kind_of?(String) && t =~ /^box[1-3]$/ }
 
21
  else
 
22
    cur = nil
 
23
  end
 
24
 
 
25
  # Create the arrow and outline.
 
26
  c.delete('all')
 
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], 
 
30
                'arrow'=>'last' 
 
31
              }.update(v.bigLineStyle) )
 
32
  xtip = v.x2 - 10*v.b
 
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')
 
37
 
 
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
 
46
 
 
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)
 
49
  tmp = v.x2+100
 
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])
 
56
 
 
57
  # Create a bunch of other arrows and text items showing the 
 
58
  # current dimensions.
 
59
  tmp = v.x2+10
 
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')
 
63
  tmp = v.x1-10
 
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')
 
71
  tmp = tmp+25
 
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')
 
75
 
 
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-*-*-*-*-*-*')
 
81
 
 
82
  v.count += 1
 
83
end
 
84
 
 
85
# toplevel widget ��¸�ߤ���к������
 
86
if defined?($arrow_demo) && $arrow_demo
 
87
  $arrow_demo.destroy 
 
88
  $arrow_demo = nil
 
89
end
 
90
 
 
91
# demo �Ѥ� toplevel widget ������
 
92
$arrow_demo = TkToplevel.new {|w|
 
93
  title("Arrowhead Editor Demonstration")
 
94
  iconname("arrow")
 
95
  positionWindow(w)
 
96
}
 
97
 
 
98
# label ����
 
99
TkLabel.new($arrow_demo, 'font'=>$font, 'wraplength'=>'5i', 'justify'=>'left', 
 
100
            'text'=>"���� widget �ǡ������Х��ǻȤ���饤��ˤĤ����͡������������Ƭ�η����Ƥߤ뤳�Ȥ��Ǥ��ޤ���������������η����Ѥ���ˤϡ����礵�줿����ˤĤ��Ƥ��� 3�ĤλͳѤ�ɥ�å����Ƥ�����������¦����������̤��礭���ǤΥ���ץ�򼨤��Ƥ��ޤ������Υƥ����Ȥϥ饤�󥢥��ƥ���Ф������ꥪ�ץ����Ǥ���"){
 
101
  pack('side'=>'top')
 
102
}
 
103
 
 
104
# frame ����
 
105
$arrow_buttons = TkFrame.new($arrow_demo) {|frame|
 
106
  TkButton.new(frame) {
 
107
    #text 'λ��'
 
108
    text '�Ĥ���'
 
109
    command proc{
 
110
      tmppath = $arrow_demo
 
111
      $arrow_demo = nil
 
112
      tmppath.destroy
 
113
    }
 
114
  }.pack('side'=>'left', 'expand'=>'yes')
 
115
 
 
116
  TkButton.new(frame) {
 
117
    text '�����ɻ���'
 
118
    command proc{showCode 'arrow'}
 
119
  }.pack('side'=>'left', 'expand'=>'yes')
 
120
}
 
121
$arrow_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
 
122
 
 
123
# canvas ����
 
124
$arrow_canvas = TkCanvas.new($arrow_demo, 'width'=>500, 'height'=>350, 
 
125
                             'relief'=>'sunken', 'borderwidth'=>2)
 
126
$arrow_canvas.pack('expand'=>'yes', 'fill'=>'both')
 
127
 
 
128
# ������
 
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
 
133
end
 
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}
 
148
else
 
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}
 
153
end
 
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})
 
172
 
 
173
# arrowMove1 --
 
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.
 
177
#
 
178
# Arguments:
 
179
# c -           The name of the canvas window.
 
180
# x, y -        The coordinates of the mouse.
 
181
 
 
182
def arrowMove1(c,x,y)
 
183
  v = $demo_arrowInfo
 
184
  newA = (v.x2+5-c.canvasx(x).round)/10
 
185
  newA = 0 if newA < 0
 
186
  newA = 25 if newA > 25
 
187
  if newA != v.a
 
188
    c.move('box1', 10*(v.a-newA), 0)
 
189
    v.a = newA
 
190
  end
 
191
end
 
192
 
 
193
# arrowMove2 --
 
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.
 
197
#
 
198
# Arguments:
 
199
# c -           The name of the canvas window.
 
200
# x, y -        The coordinates of the mouse.
 
201
 
 
202
def arrowMove2(c,x,y)
 
203
  v = $demo_arrowInfo
 
204
  newB = (v.x2+5-c.canvasx(x).round)/10
 
205
  newB = 0 if newB < 0
 
206
  newB = 25 if newB > 25
 
207
  newC = (v.y+5-c.canvasy(y).round-5*v.width)/10
 
208
  newC = 0 if newC < 0
 
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))
 
212
    v.b = newB
 
213
    v.c = newC
 
214
  end
 
215
end
 
216
 
 
217
# arrowMove3 --
 
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.
 
221
#
 
222
# Arguments:
 
223
# c -           The name of the canvas window.
 
224
# x, y -        The coordinates of the mouse.
 
225
 
 
226
def arrowMove3(c,x,y)
 
227
  v = $demo_arrowInfo
 
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))
 
233
    v.width = newWidth
 
234
  end
 
235
end
 
236