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

« back to all changes in this revision

Viewing changes to ext/tk/sample/demos-en/ruler.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
# ruler.rb
 
2
#
 
3
# This demonstration script creates a canvas widget that displays a ruler
 
4
# with tab stops that can be set, moved, and deleted.
 
5
#
 
6
# ruler widget demo (called by 'widget')
 
7
#
 
8
 
 
9
# rulerMkTab --
 
10
# This method creates a new triangular polygon in a canvas to
 
11
# represent a tab stop.
 
12
#
 
13
# Arguments:
 
14
# c -           The canvas window.
 
15
# x, y -        Coordinates at which to create the tab stop.
 
16
 
 
17
def rulerMkTab(c,x,y)
 
18
  v = $demo_rulerInfo
 
19
  TkcPolygon.new(c, x, y, x+v.size, y+v.size, x-v.size, y+v.size)
 
20
end
 
21
 
 
22
# toplevel widget
 
23
if defined?($ruler_demo) && $ruler_demo
 
24
  $ruler_demo.destroy 
 
25
  $ruler_demo = nil
 
26
end
 
27
 
 
28
# demo toplevel widget
 
29
$ruler_demo = TkToplevel.new {|w|
 
30
  title("Ruler Demonstration")
 
31
  iconname("ruler")
 
32
  positionWindow(w)
 
33
}
 
34
 
 
35
# label
 
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."){
 
38
  pack('side'=>'top')
 
39
}
 
40
 
 
41
# frame
 
42
$ruler_buttons = TkFrame.new($ruler_demo) {|frame|
 
43
  TkButton.new(frame) {
 
44
    text 'Dismiss'
 
45
    command proc{
 
46
      tmppath = $ruler_demo
 
47
      $ruler_demo = nil
 
48
      tmppath.destroy
 
49
    }
 
50
  }.pack('side'=>'left', 'expand'=>'yes')
 
51
 
 
52
  TkButton.new(frame) {
 
53
    text 'Show Code'
 
54
    command proc{showCode 'ruler'}
 
55
  }.pack('side'=>'left', 'expand'=>'yes')
 
56
}
 
57
$ruler_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
 
58
 
 
59
# canvas 
 
60
$ruler_canvas = TkCanvas.new($ruler_demo, 'width'=>'14.8c', 'height'=>'2.5c')
 
61
$ruler_canvas.pack('side'=>'top', 'fill'=>'x')
 
62
 
 
63
 
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
 
68
end
 
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)}
 
81
else
 
82
  $demo_rulerInfo.activeStyle = {'fill'=>'black', 'stipple'=>''}
 
83
  $demo_rulerInfo.deleteStyle = {'fill'=>'black', 
 
84
    'stipple'=>'@'+[$demo_dir, '..', 
 
85
                     'images', 'gray25.xbm'].join(File::Separator)}
 
86
end
 
87
 
 
88
TkcLine.new($ruler_canvas, 
 
89
            '1c', '0.5c', '1c', '1c', '13c', '1c', '13c', '0.5c', 'width'=>1)
 
90
(0..11).each{|i|
 
91
  x = i+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')
 
97
}
 
98
 
 
99
$rulerTag_well = TkcTag.new($ruler_canvas)
 
100
$ruler_canvas\
 
101
.addtag_withtag($rulerTag_well,
 
102
                TkcRectangle.new($ruler_canvas, 
 
103
                                 '13.2c', '1c', '13.8c', '0.5c', 
 
104
                                 'outline'=>'black', 
 
105
                                 'fill'=>($ruler_canvas\
 
106
                                          .configinfo('background'))[4]) )
 
107
$ruler_canvas\
 
108
.addtag_withtag($rulerTag_well,
 
109
                rulerMkTab($ruler_canvas, 
 
110
                           TkWinfo.pixels($ruler_canvas, '13.5c'), 
 
111
                           TkWinfo.pixels($ruler_canvas, '.65c') ) )
 
112
 
 
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)})
 
119
 
 
120
# rulerNewTab --
 
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.
 
123
#
 
124
# Arguments:
 
125
# c -           The canvas window.
 
126
# x, y -        The coordinates of the tab stop.
 
127
 
 
128
def rulerNewTab(c,x,y)
 
129
  v = $demo_rulerInfo
 
130
  c.addtag_withtag('active', rulerMkTab(c,x,y))
 
131
  c.addtag_withtag('tab', 'active')
 
132
  v.x = x
 
133
  v.y = y
 
134
  rulerMoveTab(c,x,y)
 
135
end
 
136
 
 
137
# rulerSelectTab --
 
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.
 
141
#
 
142
# Arguments:
 
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).
 
146
 
 
147
def rulerSelectTab(c,x,y)
 
148
  v = $demo_rulerInfo
 
149
  v.x = c.canvasx(x, v.grid)
 
150
  v.y = v.top+2
 
151
  c.addtag_withtag('active', 'current')
 
152
  c.itemconfigure('active', v.activeStyle)
 
153
  c.raise('active')
 
154
end
 
155
 
 
156
# rulerMoveTab --
 
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.
 
160
#
 
161
# Arguments:
 
162
# c -           The canvas widget.
 
163
# x, y -        The coordinates of the mouse.
 
164
 
 
165
def rulerMoveTab(c,x,y)
 
166
  v = $demo_rulerInfo
 
167
  return if c.find_withtag('active') == []
 
168
  cx = c.canvasx(x,v.grid)
 
169
  cy = c.canvasy(y)
 
170
  cx = v.left if cx < v.left
 
171
  cx = v.right if cx > v.right
 
172
  if (cy >= v.top && cy <= v.bottom)
 
173
    cy = v.top+2
 
174
    c.itemconfigure('active', v.activeStyle)
 
175
  else
 
176
    cy = cy-v.size-2
 
177
    c.itemconfigure('active', v.deleteStyle)
 
178
  end
 
179
  c.move('active', cx-v.x, cy-v.y)
 
180
  v.x = cx
 
181
  v.y = cy
 
182
end
 
183
 
 
184
# rulerReleaseTab --
 
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.
 
188
#
 
189
# Arguments:
 
190
# c -           The canvas widget.
 
191
# x, y -        The coordinates of the mouse.
 
192
 
 
193
def rulerReleaseTab(c)
 
194
  v = $demo_rulerInfo
 
195
  return if c.find_withtag('active') == []
 
196
  if v.y != v.top+2
 
197
    c.delete('active')
 
198
  else
 
199
    c.itemconfigure('active', v.normalStyle)
 
200
    c.dtag('active')
 
201
  end
 
202
end
 
203