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

« back to all changes in this revision

Viewing changes to ext/tk/sample/demos-jp/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
#
 
2
# ruler widget demo (called by 'widget')
 
3
#
 
4
 
 
5
# rulerMkTab --
 
6
# This method creates a new triangular polygon in a canvas to
 
7
# represent a tab stop.
 
8
#
 
9
# Arguments:
 
10
# c -           The canvas window.
 
11
# x, y -        Coordinates at which to create the tab stop.
 
12
 
 
13
def rulerMkTab(c,x,y)
 
14
  v = $demo_rulerInfo
 
15
  TkcPolygon.new(c, x, y, x+v.size, y+v.size, x-v.size, y+v.size)
 
16
end
 
17
 
 
18
# toplevel widget ��¸�ߤ���к������
 
19
if defined?($ruler_demo) && $ruler_demo
 
20
  $ruler_demo.destroy 
 
21
  $ruler_demo = nil
 
22
end
 
23
 
 
24
# demo �Ѥ� toplevel widget ������
 
25
$ruler_demo = TkToplevel.new {|w|
 
26
  title("Ruler Demonstration")
 
27
  iconname("ruler")
 
28
  positionWindow(w)
 
29
}
 
30
 
 
31
# label ����
 
32
TkLabel.new($ruler_demo, 'font'=>$font, 'wraplength'=>'5i', 'justify'=>'left', 
 
33
            'text'=>"���Υ����Х�widget�ϥ롼�顼���Ϸ��Ǥ����롼�顼�α��ˤ���Τϥ��֥��ȥåפΰ�ͤǡ������������ĥ�äƤ��뤳�Ȥˤ�äƥ��֥��ȥåפ��뤳�Ȥ��Ǥ��ޤ����ޤ������Ǥˤ��륿�֥��ȥåפ�ư�������Ȥ�Ǥ��ޤ������֥��ȥåפ�����ޤ��ϲ����ˤ������ɽ�������ޤǥɥ�å�����ȡ��ޥ����ܥ����Υ�������ˤ��Υ��֥��ȥåפϾä��ޤ���"){
 
34
  pack('side'=>'top')
 
35
}
 
36
 
 
37
# frame ����
 
38
$ruler_buttons = TkFrame.new($ruler_demo) {|frame|
 
39
  TkButton.new(frame) {
 
40
    #text 'λ��'
 
41
    text '�Ĥ���'
 
42
    command proc{
 
43
      tmppath = $ruler_demo
 
44
      $ruler_demo = nil
 
45
      tmppath.destroy
 
46
    }
 
47
  }.pack('side'=>'left', 'expand'=>'yes')
 
48
 
 
49
  TkButton.new(frame) {
 
50
    text '�����ɻ���'
 
51
    command proc{showCode 'ruler'}
 
52
  }.pack('side'=>'left', 'expand'=>'yes')
 
53
}
 
54
$ruler_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
 
55
 
 
56
# canvas ����
 
57
$ruler_canvas = TkCanvas.new($ruler_demo, 'width'=>'14.8c', 'height'=>'2.5c')
 
58
$ruler_canvas.pack('side'=>'top', 'fill'=>'x')
 
59
 
 
60
# ������
 
61
unless Struct.const_defined?("RulerInfo")
 
62
  $demo_rulerInfo = Struct.new("RulerInfo", :grid, :left, :right, :x, :y, 
 
63
                               :top, :bottom, :size, :normalStyle, 
 
64
                               :activeStyle, :deleteStyle).new
 
65
end
 
66
$demo_rulerInfo.grid = '.25c'
 
67
$demo_rulerInfo.left = TkWinfo.fpixels($ruler_canvas, '1c')
 
68
$demo_rulerInfo.right = TkWinfo.fpixels($ruler_canvas, '13c')
 
69
$demo_rulerInfo.top = TkWinfo.fpixels($ruler_canvas, '1c')
 
70
$demo_rulerInfo.bottom = TkWinfo.fpixels($ruler_canvas, '1.5c')
 
71
$demo_rulerInfo.size = TkWinfo.fpixels($ruler_canvas, '.2c')
 
72
$demo_rulerInfo.normalStyle = {'fill'=>'black'}
 
73
if TkWinfo.depth($ruler_canvas) > 1
 
74
  $demo_rulerInfo.activeStyle = {'fill'=>'red', 'stipple'=>''}
 
75
  $demo_rulerInfo.deleteStyle = {'fill'=>'red', 
 
76
    'stipple'=>'@'+[$demo_dir, '..', 
 
77
                     'images', 'gray25.xbm'].join(File::Separator)}
 
78
else
 
79
  $demo_rulerInfo.activeStyle = {'fill'=>'black', 'stipple'=>''}
 
80
  $demo_rulerInfo.deleteStyle = {'fill'=>'black', 
 
81
    'stipple'=>'@'+[$demo_dir, '..', 
 
82
                     'images', 'gray25.xbm'].join(File::Separator)}
 
83
end
 
84
 
 
85
TkcLine.new($ruler_canvas, 
 
86
            '1c', '0.5c', '1c', '1c', '13c', '1c', '13c', '0.5c', 'width'=>1)
 
87
(0..11).each{|i|
 
88
  x = i+1
 
89
  TkcLine.new($ruler_canvas, "#{x}c", '1c', "#{x}c", '0.6c', 'width'=>1)
 
90
  TkcLine.new($ruler_canvas, "#{x}.25c", '1c', "#{x}.25c", '0.8c', 'width'=>1)
 
91
  TkcLine.new($ruler_canvas, "#{x}.5c", '1c', "#{x}.5c", '0.7c', 'width'=>1)
 
92
  TkcLine.new($ruler_canvas, "#{x}.75c", '1c', "#{x}.75c", '0.8c', 'width'=>1)
 
93
  TkcText.new($ruler_canvas, "#{x}.15c", '0.75c', 'text'=>i, 'anchor'=>'sw')
 
94
}
 
95
 
 
96
$rulerTag_well = TkcTag.new($ruler_canvas)
 
97
$ruler_canvas\
 
98
.addtag_withtag($rulerTag_well,
 
99
                TkcRectangle.new($ruler_canvas, 
 
100
                                 '13.2c', '1c', '13.8c', '0.5c', 
 
101
                                 'outline'=>'black', 
 
102
                                 'fill'=>($ruler_canvas\
 
103
                                          .configinfo('background'))[4]) )
 
104
$ruler_canvas\
 
105
.addtag_withtag($rulerTag_well,
 
106
                rulerMkTab($ruler_canvas, 
 
107
                           TkWinfo.pixels($ruler_canvas, '13.5c'), 
 
108
                           TkWinfo.pixels($ruler_canvas, '.65c') ) )
 
109
 
 
110
$rulerTag_well.bind('1', proc{|x,y| rulerNewTab($ruler_canvas,x,y)}, '%x %y')
 
111
$ruler_canvas.itembind('tab', '1', 
 
112
                       proc{|x,y| rulerSelectTab($ruler_canvas,x,y)}, '%x %y')
 
113
$ruler_canvas.bind('B1-Motion', 
 
114
                   proc{|x,y| rulerMoveTab($ruler_canvas,x,y)}, '%x %y')
 
115
$ruler_canvas.bind('Any-ButtonRelease-1', proc{rulerReleaseTab($ruler_canvas)})
 
116
 
 
117
# rulerNewTab --
 
118
# Does all the work of creating a tab stop, including creating the
 
119
# triangle object and adding tags to it to give it tab behavior.
 
120
#
 
121
# Arguments:
 
122
# c -           The canvas window.
 
123
# x, y -        The coordinates of the tab stop.
 
124
 
 
125
def rulerNewTab(c,x,y)
 
126
  v = $demo_rulerInfo
 
127
  c.addtag_withtag('active', rulerMkTab(c,x,y))
 
128
  c.addtag_withtag('tab', 'active')
 
129
  v.x = x
 
130
  v.y = y
 
131
  rulerMoveTab(c,x,y)
 
132
end
 
133
 
 
134
# rulerSelectTab --
 
135
# This method is invoked when mouse button 1 is pressed over
 
136
# a tab.  It remembers information about the tab so that it can
 
137
# be dragged interactively.
 
138
#
 
139
# Arguments:
 
140
# c -           The canvas widget.
 
141
# x, y -        The coordinates of the mouse (identifies the point by
 
142
#               which the tab was picked up for dragging).
 
143
 
 
144
def rulerSelectTab(c,x,y)
 
145
  v = $demo_rulerInfo
 
146
  v.x = c.canvasx(x, v.grid)
 
147
  v.y = v.top+2
 
148
  c.addtag_withtag('active', 'current')
 
149
  c.itemconfigure('active', v.activeStyle)
 
150
  c.raise('active')
 
151
end
 
152
 
 
153
# rulerMoveTab --
 
154
# This method is invoked during mouse motion events to drag a tab.
 
155
# It adjusts the position of the tab, and changes its appearance if
 
156
# it is about to be dragged out of the ruler.
 
157
#
 
158
# Arguments:
 
159
# c -           The canvas widget.
 
160
# x, y -        The coordinates of the mouse.
 
161
 
 
162
def rulerMoveTab(c,x,y)
 
163
  v = $demo_rulerInfo
 
164
  return if c.find_withtag('active') == []
 
165
  cx = c.canvasx(x,v.grid)
 
166
  cy = c.canvasy(y)
 
167
  cx = v.left if cx < v.left
 
168
  cx = v.right if cx > v.right
 
169
  if (cy >= v.top && cy <= v.bottom)
 
170
    cy = v.top+2
 
171
    c.itemconfigure('active', v.activeStyle)
 
172
  else
 
173
    cy = cy-v.size-2
 
174
    c.itemconfigure('active', v.deleteStyle)
 
175
  end
 
176
  c.move('active', cx-v.x, cy-v.y)
 
177
  v.x = cx
 
178
  v.y = cy
 
179
end
 
180
 
 
181
# rulerReleaseTab --
 
182
# This method is invoked during button release events that end
 
183
# a tab drag operation.  It deselects the tab and deletes the tab if
 
184
# it was dragged out of the ruler.
 
185
#
 
186
# Arguments:
 
187
# c -           The canvas widget.
 
188
# x, y -        The coordinates of the mouse.
 
189
 
 
190
def rulerReleaseTab(c)
 
191
  v = $demo_rulerInfo
 
192
  return if c.find_withtag('active') == []
 
193
  if v.y != v.top+2
 
194
    c.delete('active')
 
195
  else
 
196
    c.itemconfigure('active', v.normalStyle)
 
197
    c.dtag('active')
 
198
  end
 
199
end
 
200