summaryrefslogtreecommitdiff
path: root/ruby_1_8_6/ext/tk/sample/demos-jp/twind2.rb
blob: b634f07b4b3c44e7a8c6987573bb6aee4645124d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#
# text (embedded windows) widget demo 2 (called by 'widget')
#

# toplevel widget ¤¬Â¸ºß¤¹¤ì¤Ðºï½ü¤¹¤ë
if defined?($twind2_demo) && $twind2_demo
  $twind2_demo.destroy 
  $twind2_demo = nil
end

# demo ÍѤΠtoplevel widget ¤òÀ¸À®
$twind2_demo = TkToplevel.new {|w|
  title("Text Demonstration - Embedded Windows 2")
  iconname("Embedded Windows")
  positionWindow(w)
}

# frame À¸À®
$twind2_buttons = TkFrame.new($twind2_demo) {|frame|
  TkGrid(TkFrame.new(frame, :height=>2, :relief=>:sunken, :bd=>2), 
         :columnspan=>4, :row=>0, :sticky=>'ew', :pady=>2)
  TkGrid('x', 
         TkButton.new(frame, :text=>'¥³¡¼¥É»²¾È', 
                      :image=>$image['view'], :compound=>:left, 
                      :command=>proc{showCode 'twind2'}), 
         TkButton.new(frame, :text=>'ÊĤ¸¤ë', 
                      :image=>$image['delete'], :compound=>:left, 
                      :command=>proc{
                        tmppath = $twind2_demo
                        $twind2_demo = nil
                        $showVarsWin[tmppath.path] = nil
                        tmppath.destroy
                      }), 
         :padx=>4, :pady=>4)
  frame.grid_columnconfigure(0, :weight=>1)
}
$twind2_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')

# frame À¸À®
$twind2_text = nil
TkFrame.new($twind2_demo, 'highlightthickness'=>2, 'borderwidth'=>2, 
            'relief'=>'sunken') {|f|
  $twind2_text = TkText.new(f, 'setgrid'=>true, 'font'=>$font,
                            # 'width'=>'70', 'height'=>35, 'wrap'=>'word', 
                            'width'=>'70', 'height'=>35, 'wrap'=>'char', 
                            'highlightthickness'=>0, 'borderwidth'=>0 ){|t|
    TkScrollbar.new(f) {|s|
      command proc{|*args| t.yview(*args)}
      t.yscrollcommand proc{|first,last| s.set first,last}
    }.pack('side'=>'right', 'fill'=>'y')
  }.pack('expand'=>'yes', 'fill'=>'both')
}.pack('expand'=>'yes', 'fill'=>'both')

# ¥¿¥°À¸À®
$tag2_center = TkTextTag.new($twind2_text, 
                            'justify' =>'center',
                            'spacing1'=>'5m',
                            'spacing3'=>'5m'  )
$tag2_buttons = TkTextTag.new($twind2_text, 
                             'lmargin1'=>'1c',
                             'lmargin2'=>'1c',
                             'rmargin' =>'1c',
                             'spacing1'=>'3m',
                             'spacing2'=>0,
                             'spacing3'=>0 )

# ¥Æ¥­¥¹¥È¤ÎÀ¸À®
$twind2_text.insert('end', '¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤Ë¤Ï¿§¡¹¤Ê¼ïÎà¤Î¥¢¥¤¥Æ¥à')
$twind2_text.insert('end', '¡ÊưŪ¤Ê¤â¤Î¤äÀÅŪ¤Ê¤â¤Î¤ÎξÊý¤¬¤¢¤ê¤Þ¤¹¡Ë¤ò')
$twind2_text.insert('end', '¿ô¿¤¯´Þ¤á¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£¤½¤¦¤·¤¿¥¢¥¤¥Æ¥à¤Ï')
$twind2_text.insert('end', '¹ÔÂؤ¨¡¢¥¿¥Ö¡¢Ãæ±û·¤¨¤Ê¤É¤ÎÍÍ¡¹¤ÊÊýË¡¤Ç')
$twind2_text.insert('end', 'ÇÛÃÖ¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£')
$twind2_text.insert('end', '²Ã¤¨¤Æ¡¢¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤ÎÆâÍÆʪ¤¬')
$twind2_text.insert('end', '¥¦¥£¥ó¥É¥¦¥µ¥¤¥º¤ËÈæ¤Ù¤ÆÂ礭¤¹¤®¤ë¾ì¹ç¤Ç¤â')
$twind2_text.insert('end', '¤¹¤Ù¤Æ¤ÎÊý¸þ¤Ë¥¹¥à¡¼¥º¤Ë¥¹¥¯¥í¡¼¥ë¤µ¤»¤Æ')
$twind2_text.insert('end', '³Îǧ¤¹¤ë¤³¤È¤¬²Äǽ¤Ç¤¹¡£')
$twind2_text.insert('end', "\n\n")
$twind2_text.insert('end', '¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¾å¤Ë¤Ï¾¤Î¥¦¥£¥¸¥§¥Ã¥È¤ò')
$twind2_text.insert('end', '´Þ¤á¤ë¤³¤È¤â¤Ç¤­¤Þ¤¹¡£¤³¤¦¤·¤¿¤â¤Î¤Ï')
$twind2_text.insert('end', '¡ÖËä¤á¹þ¤ß¥¦¥£¥ó¥É¥¦¡×¤È¸Æ¤Ð¤ì¡¢¤½¤ÎÃæ¤Ë')
$twind2_text.insert('end', '¤¤¤«¤Ê¤ë¥¦¥£¥¸¥§¥Ã¥È¤Ç¤âµÍ¤á¹þ¤à¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£')
$twind2_text.insert('end', 'Î㤨¤Ð¡¤¤³¤³¤Ë¤Ï£²¤Ä¤Î')
$twind2_text.insert('end', '¥Ü¥¿¥ó¥¦¥£¥¸¥§¥Ã¥È¤¬Ëä¤á¹þ¤Þ¤ì¤Æ¤¤¤Þ¤¹¡£')
$twind2_text.insert('end', 'ºÇ½é¤Î¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤¹¤ë¤È¡¢')
$twind2_text.insert('end', '¿åÊ¿Êý¸þ¤Î¥¹¥¯¥í¡¼¥ë¤ò ')
TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text) {
                   #text 'ON'
                   text '¥ª¥ó'
                   command proc{textWindOn2 $twind2_text,$twind2_buttons}
                   cursor 'top_left_arrow'
                 })
$twind2_text.insert('end', "¤Ë¤·¤Þ¤¹¡£¤Þ¤¿¡¢£²¤Ä¤á¤Î¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤¹¤ë¤È\n")
$twind2_text.insert('end', '¿åÊ¿Êý¸þ¤Î¥¹¥¯¥í¡¼¥ë¤ò')
TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text) {
                   #text 'OFF'
                   text '¥ª¥Õ'
                   command proc{textWindOff2 $twind2_text}
                   cursor 'top_left_arrow'
                 })
$twind2_text.insert('end', "¤Ë¤·¤Þ¤¹¡£\n\n")

$twind2_text.insert('end', '¼¡¤Ï¤â¤¦¤Ò¤È¤Ä¤ÎÎã¤Ç¤¹¡£')
TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text) {
                   text '¤³¤³¤ò¥¯¥ê¥Ã¥¯'
                   command proc{textWindPlot2 $twind2_text}
                   cursor 'top_left_arrow'
                 })
$twind2_text.insert('end', '¤¹¤ë¤È¡¢x-y¥×¥í¥Ã¥È¤¬¤³¤³¤Ë¸½¤ì¤Þ¤¹¡£')
$mark2_plot = TkTextMark.new($twind2_text, 'insert')
$mark2_plot.gravity='left'
$twind2_text.insert('end', '¥Þ¥¦¥¹¤Ç¥É¥é¥Ã¥°¤¹¤ë¤³¤È¤Ç¡¢')
$twind2_text.insert('end', '¥×¥í¥Ã¥È¾å¤Î¥Ç¡¼¥¿ÅÀ¤ò°ÜÆ°¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£')
TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text) {
                   text '¾Ãµî'
                   command proc{textWindDel2 $twind2_text}
                   cursor 'top_left_arrow'
                 })
$twind2_text.insert('end', '¤ò¥¯¥ê¥Ã¥¯¤¹¤ë¤È¸µ¤ËÌá¤ê¤Þ¤¹¡£')
$twind2_text.insert('end', "\n\n")

$twind2_text.insert('end', 'ɽ¼¨¤¹¤ë¥Æ¥­¥¹¥È¤Ê¤·¤ËËä¤á¹þ¤ß¥¦¥£¥ó¥É¥¦¤À¤±¤ò')
$twind2_text.insert('end', '¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤Ë´Þ¤á¤ë¤³¤È¤âÍ­ÍѤǤ·¤ç¤¦¡£')
$twind2_text.insert('end', '¤³¤Î¾ì¹ç¡¢¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤Ï')
$twind2_text.insert('end', '¥¸¥ª¥á¥È¥ê¥Þ¥Í¡¼¥¸¥ã¤Î¤è¤¦¤ËƯ¤­¤Þ¤¹¡£')
$twind2_text.insert('end', 'Î㤨¤Ð¡¢¤³¤³¤Ë¤Ï¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤Ë')
$twind2_text.insert('end', '¤è¤Ã¤Æ¥Ü¥¿¥ó¤¬¤­¤ì¤¤¤ËÀ°Îó¤·¤ÆÇÛÃÖ¤µ¤ì¤Æ¤¤¤Þ¤¹¡£')
$twind2_text.insert('end', '¤³¤ì¤é¤Î¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤¹¤ë¤³¤È¤Ç¡¢')
$twind2_text.insert('end', '¤³¤Î¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤ÎÇØ·Ê¿§¤ò')
$twind2_text.insert('end', 'ÊѤ¨¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹("¥Ç¥Õ¥©¥ë¥È"¥Ü¥¿¥ó¤Ç')
$twind2_text.insert('end', '¸µ¤Î¿§¤ËÌ᤹¤³¤È¤¬¤Ç¤­¤Þ¤¹)¡£')
$twind2_text.insert('end', '"Short"¤È¤¤¤¦¥Ü¥¿¥ó¤ò¥¯¥ê¥Ã¥¯¤¹¤ë¤Èʸ»úÎó¤ÎŤµ¤¬')
$twind2_text.insert('end', 'ÊѤï¤ê¡¢¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤¬¼«Æ°Åª¤Ë')
$twind2_text.insert('end', '¥ì¥¤¥¢¥¦¥È¤òÀ°¤¨¤ëÍͻҤò¸«¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£')
$twind2_text.insert('end', '¤â¤¦°ìÅÙƱ¤¸¥Ü¥¿¥ó¤ò²¡¤¹¤È¸µ¤ËÌá¤ê¤Þ¤¹¡£')
$twind2_text.insert('end', "\n")

btn_default = TkButton.new($twind2_text) {|b|
  text '¥Ç¥Õ¥©¥ë¥È'
  command proc{embDefBg2 $twind2_text}
  cursor 'top_left_arrow'
}
TkTextWindow.new($twind2_text, 'end', 'window'=>btn_default, 'padx'=>3)
embToggle = TkVariable.new('Short')
TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkCheckButton.new($twind2_text) {
                   textvariable embToggle
                   indicatoron 0
                   variable embToggle
                   onvalue 'A much longer string'
                   offvalue 'Short'
                   cursor 'top_left_arrow'
                   pady 5
                   padx 2
                 },
                 'padx'=>3, 
                 'pady'=>2 )

[ 'AntiqueWhite3', 'Bisque1', 'Bisque2', 'Bisque3', 'Bisque4', 
  'SlateBlue3', 'RoyalBlue1', 'SteelBlue2', 'DeepSkyBlue3', 'LightBlue1', 
  'DarkSlateGray1', 'Aquamarine2', 'DarkSeaGreen2', 'SeaGreen1', 
  'Yellow1', 'IndianRed1', 'IndianRed2', 'Tan1', 'Tan4' 
].each{|twind_color|
  TkTextWindow.new($twind2_text, 'end', 
                   'window'=>TkButton.new($twind2_text) {
                     text twind_color
                     cursor 'top_left_arrow'
                     command proc{$twind2_text.bg twind_color}
                   },
                   'padx'=>3, 
                   'pady'=>2 )
}

$tag2_buttons.add(btn_default, 'end')

$text_normal2 = {}
$text_normal2['border'] = $twind2_text.cget('borderwidth')
$text_normal2['highlight'] = $twind2_text.cget('highlightthickness')
$text_normal2['pad'] = $twind2_text.cget('padx')

$twind2_text.insert('end', "\nborder width ¤ä highlightthickness, ")
$twind2_text.insert('end', "padding ¤òÄ̾ï¤ÎÃͤ«¤éÊѹ¹¤¹¤ë¤³¤È¤â²Äǽ¤Ç¤¹¡£\n")

TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text, :text=>"Big borders", 
                                        :cursor=>'top_left_arrow', 
                                        'command'=>proc{
                                          textWinBigB2 $twind2_text
                                        }))

TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text, :text=>"Small borders", 
                                        :cursor=>'top_left_arrow', 
                                        'command'=>proc{
                                          textWinSmallB2 $twind2_text
                                        }))

TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text, :text=>"Big highlight", 
                                        :cursor=>'top_left_arrow', 
                                        'command'=>proc{
                                          textWinBigH2 $twind2_text
                                        }))

TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text, :text=>"Small highlight",
                                        :cursor=>'top_left_arrow', 
                                        'command'=>proc{
                                          textWinSmallH2 $twind2_text
                                        }))

TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text, :text=>"Big pad", 
                                        :cursor=>'top_left_arrow', 
                                        'command'=>proc{
                                          textWinBigP2 $twind2_text
                                        }))

TkTextWindow.new($twind2_text, 'end', 
                 'window'=>TkButton.new($twind2_text, :text=>"Small pad", 
                                        :cursor=>'top_left_arrow', 
                                        'command'=>proc{
                                          textWinSmallP2 $twind2_text
                                        }))

$twind2_text.insert('end', "\n\n¹¹¤Ë¥¤¥á¡¼¥¸¤â¥Æ¥­¥¹¥È¥¦¥£¥¸¥§¥Ã¥È¤Ë")
$twind2_text.insert('end', "¤¦¤Þ¤¯ÇÛÃ֤Ǥ­¤Þ¤¹¡§")

TkTextImage.new($twind2_text, 'end', 
                'image'=>TkBitmapImage.new(:file=>[
                                             $demo_dir, '..', 
                                             'images', 'face.xbm'
                                           ].join(File::Separator)))

# ¥á¥½¥Ã¥ÉÄêµÁ
def textWinBigB2(w)
  w.borderwidth 15
end
def textWinSmallB2(w)
  w.borderwidth $text_normal2['border']
end
def textWinBigH2(w)
  w.highlightthickness 15
end
def textWinSmallH2(w)
  w.highlightthickness $text_normal2['highlight']
end
def textWinBigP2(w)
  w.configure(:padx=>15, :pady=>15)
end
def textWinSmallP2(w)
  w.configure(:padx=>$text_normal2['pad'], :pady=>$text_normal2['pad'])
end

def textWindOn2 (w,f)
  if defined? $twind2_scroll
    begin
      $twind2_scroll.destroy
    rescue
    end
    $twind2_scroll = nil
  end

  base = TkWinfo.parent( TkWinfo.parent(w) )
  $twind2_scroll = TkScrollbar.new(base) {|s|
    orient 'horizontal'
    command proc{|*args| w.xview(*args)}
    w.xscrollcommand proc{|first,last| s.set first,last}
    w.wrap 'none'
    pack('after'=>f, 'side'=>'bottom', 'fill'=>'x')
  }

  return nil
end

def textWindOff2 (w)
  if defined? $twind2_scroll
    begin
      $twind2_scroll.destroy
    rescue
    end
    $twind2_scroll = nil
  end
  w.xscrollcommand ''
  #w.wrap 'word'
  w.wrap 'char'
end

def textWindPlot2 (t)
  if (defined? $twind2_plot) && (TkWinfo.exist?($twind2_plot))
    return
  end

  $twind2_plot = TkCanvas.new(t) {
    relief 'sunken'
    width  450
    height 300
    cursor 'top_left_arrow'
  }

  #font = '-Adobe-Helvetica-Medium-R-Normal--*-180-*-*-*-*-*-*'
  font = 'Helvetica 18'

  TkcLine.new($twind2_plot, 100, 250, 400, 250, 'width'=>2)
  TkcLine.new($twind2_plot, 100, 250, 100,  50, 'width'=>2)
  TkcText.new($twind2_plot, 225, 20, 
              'text'=>"A Simple Plot", 'font'=>font, 'fill'=>'brown')

  (0..10).each {|i|
    x = 100 + (i * 30)
    TkcLine.new($twind2_plot, x, 250, x, 245, 'width'=>2)
    TkcText.new($twind2_plot, x, 254, 
                'text'=>10*i, 'font'=>font, 'anchor'=>'n')
  }
  (0..5).each {|i|
    y = 250 - (i * 40)
    TkcLine.new($twind2_plot, 100, y, 105, y, 'width'=>2)
    TkcText.new($twind2_plot, 96, y, 
                'text'=>"#{i*50}.0", 'font'=>font, 'anchor'=>'e')
  }

  for xx, yy in [[12,56],[20,94],[33,98],[32,120],[61,180],[75,160],[98,223]]
    x = 100 + (3*xx)
    y = 250 - (4*yy)/5
    item = TkcOval.new($twind2_plot, x-6, y-6, x+6, y+6, 
                       'width'=>1, 'outline'=>'black', 'fill'=>'SkyBlue2')
    item.addtag 'point'
  end

  $twind2_plot.itembind('point', 'Any-Enter', 
                        proc{$twind2_plot.itemconfigure 'current', 'fill', 'red'})
  $twind2_plot.itembind('point', 'Any-Leave', 
                        proc{$twind2_plot.itemconfigure 'current', 'fill', 'SkyBlue2'})
  $twind2_plot.itembind('point', '1', 
                        proc{|x,y| embPlotDown2 $twind2_plot,x,y}, "%x %y")
  $twind2_plot.itembind('point', 'ButtonRelease-1', 
                        proc{$twind2_plot.dtag 'selected'})
  $twind2_plot.bind('B1-Motion', 
                    proc{|x,y| embPlotMove2 $twind2_plot,x,y}, "%x %y")
  while ($twind2_text.get($mark2_plot) =~ /[ \t\n]/)
    $twind2_text.delete $mark2_plot
  end
  $twind2_text.insert $mark2_plot,"\n"
  TkTextWindow.new($twind2_text, $mark2_plot, 'window'=>$twind2_plot)
  $tag2_center.add $mark2_plot
  $twind2_text.insert $mark2_plot,"\n"
end

$embPlot2 = {'lastX'=>0, 'lastY'=>0}

def embPlotDown2 (w, x, y)
  w.dtag 'selected'
  w.addtag_withtag 'selected', 'current'
  w.raise 'current'
  $embPlot2['lastX'] = x
  $embPlot2['lastY'] = y
end

def embPlotMove2 (w, x, y)
  w.move 'selected', x - $embPlot2['lastX'], y - $embPlot2['lastY']
  $embPlot2['lastX'] = x
  $embPlot2['lastY'] = y
end

def textWindDel2 (w)
  if (defined? $twind2_text) && TkWinfo.exist?($twind2_plot)
    $twind2_text.delete $twind2_plot
    $twind2_plot = nil
    while ($twind2_text.get($mark2_plot) =~ /[ \t\n]/)
      $twind2_text.delete $mark2_plot
    end
    $twind2_text.insert $mark2_plot,"  "
  end
end

def embDefBg2 (w)
  w['background'] = w.configinfo('background')[3]
end