summaryrefslogtreecommitdiff
path: root/ruby_1_8_6/ext/tk/sample/tcltklib/sample1.rb
blob: 13df4407518c7774fc939a77aa258ebb865143e9 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#! /usr/local/bin/ruby -d
#! /usr/local/bin/ruby
# -d ¥ª¥×¥·¥ç¥ó¤òÉÕ¤±¤ë¤È, ¥Ç¥Ð¥Ã¥°¾ðÊó¤òɽ¼¨¤¹¤ë.

# tcltk ¥é¥¤¥Ö¥é¥ê¤Î¥µ¥ó¥×¥ë

# ¤Þ¤º, ¥é¥¤¥Ö¥é¥ê¤ò require ¤¹¤ë.
require "tcltk"

# °Ê²¼¤Ï, Test1 ¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Î initialize() ¤Ç,
# tcl/tk ¤Ë´Ø¤¹¤ë½èÍý¤ò¹Ô¤¦Îã¤Ç¤¢¤ë.
# ɬ¤º¤·¤â¤½¤Î¤è¤¦¤Ë¤¹¤ëɬÍפÏ̵¤¯,
# (¤â¤·, ¤½¤¦¤·¤¿¤±¤ì¤Ð) class ¤Î³°¤Ç tcl/tk ¤Ë´Ø¤¹¤ë½èÍý¤ò¹Ô¤Ã¤Æ¤âÎɤ¤.

class Test1
  # ½é´ü²½(¥¤¥ó¥¿¥×¥ê¥¿¤òÀ¸À®¤·¤Æ¥¦¥£¥¸¥§¥Ã¥È¤òÀ¸À®¤¹¤ë).
  def initialize()

    #### »È¤¦Á°¤Î¤ª¤Þ¤¸¤Ê¤¤

    # ¥¤¥ó¥¿¥×¥ê¥¿¤ÎÀ¸À®.
    ip = TclTkInterpreter.new()
    # ¥³¥Þ¥ó¥É¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤ò c ¤ËÀßÄꤷ¤Æ¤ª¤¯.
    c = ip.commands()
    # »ÈÍѤ¹¤ë¥³¥Þ¥ó¥É¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È¤ÏÊÑ¿ô¤ËÆþ¤ì¤Æ¤ª¤¯.
    append, bind, button, destroy, incr, info, label, place, set, wm =
      c.values_at(
      "append", "bind", "button", "destroy", "incr", "info", "label", "place",
      "set", "wm")

    #### tcl/tk ¤Î¥³¥Þ¥ó¥É¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È(TclTkCommand)¤ÎÁàºî

    # ¼Â¹Ô¤¹¤ë»þ¤Ï, e() ¥á¥½¥Ã¥É¤ò»È¤¦.
    # (°Ê²¼¤Ï, tcl/tk ¤Ë¤ª¤±¤ë info command r* ¤ò¼Â¹Ô.)
    print info.e("command", "r*"), "\n"
    # °ú¿ô¤Ï, ¤Þ¤È¤á¤¿Ê¸»úÎó¤Ë¤·¤Æ¤âƱ¤¸.
    print info.e("command r*"), "\n"
    # ÊÑ¿ô¤òÍѤ¤¤Ê¤¯¤È¤â¼Â¹Ô¤Ç¤­¤ë¤¬, ¸«¤¿¤á¤¬°­¤¤.
    print c["info"].e("command", "r*"), "\n"
    # ¥¤¥ó¥¿¥×¥ê¥¿¤Î¥á¥½¥Ã¥É¤È¤·¤Æ¤â¼Â¹Ô¤Ç¤­¤ë¤¬, ¸úΨ¤¬°­¤¤.
    print ip.info("command", "r*"), "\n"

    ####

    # °Ê²¼, À¸À®¤·¤¿¥ª¥Ö¥¸¥§¥¯¥È¤ÏÊÑ¿ô¤ËÂåÆþ¤·¤Æ¤ª¤«¤Ê¤¤¤È
    # GC ¤ÎÂоݤˤʤäƤ·¤Þ¤¦.

    #### tcl/tk ¤ÎÊÑ¿ô¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È(TclTkVariable)¤ÎÁàºî

    # À¸À®¤ÈƱ»þ¤ËÃͤòÀßÄꤹ¤ë.
    v1 = TclTkVariable.new(ip, "20")
    # Æɤ߽Ф·¤Ï get ¥á¥½¥Ã¥É¤ò»È¤¦.
    print v1.get(), "\n"
    # ÀßÄê¤Ï set ¥á¥½¥Ã¥É¤ò»È¤¦.
    v1.set(40)
    print v1.get(), "\n"
    # set ¥³¥Þ¥ó¥É¤ò»È¤Ã¤ÆÆɤ߽Ф·, ÀßÄê¤Ï²Äǽ¤À¤¬¸«¤¿¤á¤¬°­¤¤.
    # e() ¥á¥½¥Ã¥ÉÅù¤Î°ú¿ô¤ËľÀÜ TclTkObject ¤ä¿ôÃͤò½ñ¤¤¤Æ¤âÎɤ¤.
    set.e(v1, 30)
    print set.e(v1), "\n"
    # tcl/tk ¤Î¥³¥Þ¥ó¥É¤ÇÊÑ¿ô¤òÁàºî¤Ç¤­¤ë.
    incr.e(v1)
    print v1.get(), "\n"
    append.e(v1, 10)
    print v1.get(), "\n"

    #### tcl/tk ¤Î¥¦¥£¥¸¥§¥Ã¥È¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È(TclTkWidget)¤ÎÁàºî

    # ¥ë¡¼¥È¥¦¥£¥¸¥§¥Ã¥È¤ò¼è¤ê½Ð¤¹.
    root = ip.rootwidget()
    # ¥¦¥£¥¸¥§¥Ã¥È¤ÎÁàºî.
    root.e("configure -height 300 -width 300")
    # ¥¿¥¤¥È¥ë¤òÉÕ¤±¤ë¤È¤­¤Ï wm ¤ò»È¤¦.
    wm.e("title", root, $0)
    # ¿Æ¥¦¥£¥¸¥§¥Ã¥È¤È¥³¥Þ¥ó¥É¤ò»ØÄꤷ¤Æ, ¥¦¥£¥¸¥§¥Ã¥È¤òºî¤ë.
    l1 = TclTkWidget.new(ip, root, label, "-text {type `x' to print}")
    # place ¤¹¤ë¤Èɽ¼¨¤µ¤ì¤ë.
    place.e(l1, "-x 0 -rely 0.0 -relwidth 1 -relheight 0.1")
    # ¥³¥Þ¥ó¥É̾¤Ïʸ»úÎó¤Ç»ØÄꤷ¤Æ¤âÎɤ¤¤¬, ¸«¤¿¤á¤¬°­¤¤.
    # (¥³¥Þ¥ó¥É̾¤ÏÆÈΩ¤·¤¿°ú¿ô¤Ç¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤.)
    l2 = TclTkWidget.new(ip, root, "label")
    # ¥¦¥£¥¸¥§¥Ã¥È¤ÎÁàºî.
    l2.e("configure -text {type `q' to exit}")
    place.e(l2, "-x 0 -rely 0.1 -relwidth 1 -relheight 0.1")

    #### tcl/tk ¤Î¥³¡¼¥ë¥Ð¥Ã¥¯¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È(TclTkCallback)¤ÎÁàºî

    # ¥³¡¼¥ë¥Ð¥Ã¥¯¤òÀ¸À®¤¹¤ë.
    c1 = TclTkCallback.new(ip, proc{sample(ip, root)})
    # ¥³¡¼¥ë¥Ð¥Ã¥¯¤ò»ý¤Ä¥¦¥£¥¸¥§¥Ã¥È¤òÀ¸À®¤¹¤ë.
    b1 = TclTkWidget.new(ip, root, button, "-text sample -command", c1)
    place.e(b1, "-x 0 -rely 0.2 -relwidth 1 -relheight 0.1")
    # ¥¤¥Ù¥ó¥È¥ë¡¼¥×¤òÈ´¤±¤ë¤Ë¤Ï destroy.e(root) ¤¹¤ë.
    c2 = TclTkCallback.new(ip, proc{destroy.e(root)})
    b2 = TclTkWidget.new(ip, root, button, "-text exit -command", c2)
    place.e(b2, "-x 0 -rely 0.3 -relwidth 1 -relheight 0.1")

    #### ¥¤¥Ù¥ó¥È¤Î¥Ð¥¤¥ó¥É
    # script ¤ÎÄɲà (bind tag sequence +script) ¤Ïº£¤Î¤È¤³¤í¤Ç¤­¤Ê¤¤.
    # (¥¤¥Æ¥ì¡¼¥¿ÊÑ¿ô¤ÎÀßÄ꤬¤¦¤Þ¤¯¤¤¤«¤Ê¤¤.)

    # ´ðËÜŪ¤Ë¤Ï¥¦¥£¥¸¥§¥Ã¥È¤ËÂФ¹¤ë¥³¡¼¥ë¥Ð¥Ã¥¯¤ÈƱ¤¸.
    c3 = TclTkCallback.new(ip, proc{print("q pressed\n"); destroy.e(root)})
    bind.e(root, "q", c3)
    # bind ¥³¥Þ¥ó¥É¤Ç % ÃÖ´¹¤Ë¤è¤ê¥Ñ¥é¥á¡¼¥¿¤ò¼õ¤±¼è¤ê¤¿¤¤¤È¤­¤Ï,
    # proc{} ¤Î¸å¤í¤Ëʸ»úÎó¤Ç»ØÄꤹ¤ë¤È,
    # ÃÖ´¹·ë²Ì¤ò¥¤¥Æ¥ì¡¼¥¿ÊÑ¿ô¤òÄ̤·¤Æ¼õ¤±¼è¤ë¤³¤È¤¬¤Ç¤­¤ë.
    # ¤¿¤À¤· proc{} ¤Î¸å¤í¤Îʸ»úÎó¤Ï,
    # bind ¥³¥Þ¥ó¥É¤ËÍ¿¤¨¤ë¥³¡¼¥ë¥Ð¥Ã¥¯°Ê³°¤Ç»ØÄꤷ¤Æ¤Ï¤¤¤±¤Ê¤¤.
    c4 = TclTkCallback.new(ip, proc{|i| print("#{i} pressed\n")}, "%A")
    bind.e(root, "x", c4)
    # TclTkCallback ¤ò GC ¤ÎÂоݤˤ·¤¿¤±¤ì¤Ð,
    # dcb() (¤Þ¤¿¤Ï deletecallbackkeys()) ¤¹¤ëɬÍפ¬¤¢¤ë.
    cb = [c1, c2, c3, c4]
    c5 = TclTkCallback.new(ip, proc{|w| TclTk.dcb(cb, root, w)}, "%W")
    bind.e(root, "<Destroy>", c5)
    cb.push(c5)

    #### tcl/tk ¤Î¥¤¥á¡¼¥¸¤ËÂбþ¤¹¤ë¥ª¥Ö¥¸¥§¥¯¥È(TclTkImage)¤ÎÁàºî

    # ¥Ç¡¼¥¿¤ò»ØÄꤷ¤ÆÀ¸À®¤¹¤ë.
    i1 = TclTkImage.new(ip, "photo", "-file maru.gif")
    # ¥é¥Ù¥ë¤ËÄ¥¤êÉÕ¤±¤Æ¤ß¤ë.
    l3 = TclTkWidget.new(ip, root, label, "-relief raised -image", i1)
    place.e(l3, "-x 0 -rely 0.4 -relwidth 0.2 -relheight 0.2")
    # ¶õ¤Î¥¤¥á¡¼¥¸¤òÀ¸À®¤·¤Æ¸å¤ÇÁàºî¤¹¤ë.
    i2 = TclTkImage.new(ip, "photo")
    # ¥¤¥á¡¼¥¸¤òÁàºî¤¹¤ë.
    i2.e("copy", i1)
    i2.e("configure -gamma 0.5")
    l4 = TclTkWidget.new(ip, root, label, "-relief raised -image", i2)
    place.e(l4, "-relx 0.2 -rely 0.4 -relwidth 0.2 -relheight 0.2")

    ####
  end

  # ¥µ¥ó¥×¥ë¤Î¤¿¤á¤Î¥¦¥£¥¸¥§¥Ã¥È¤òÀ¸À®¤¹¤ë.
  def sample(ip, parent)
    bind, button, destroy, grid, toplevel, wm = ip.commands().values_at(
      "bind", "button", "destroy", "grid", "toplevel", "wm")

    ## toplevel

    # ¿·¤·¤¤¥¦¥¤¥ó¥É¥¦¤ò³«¤¯¤Ë¤Ï, toplevel ¤ò»È¤¦.
    t1 = TclTkWidget.new(ip, parent, toplevel)
    # ¥¿¥¤¥È¥ë¤òÉÕ¤±¤Æ¤ª¤¯
    wm.e("title", t1, "sample")

    # ¥¦¥£¥¸¥§¥Ã¥È¤¬Ç˲õ¤µ¤ì¤¿¤È¤­, ¥³¡¼¥ë¥Ð¥Ã¥¯¤¬ GC ¤ÎÂоݤˤʤë¤è¤¦¤Ë¤¹¤ë.
    cb = []
    cb.push(c = TclTkCallback.new(ip, proc{|w| TclTk.dcb(cb, t1, w)}, "%W"))
    bind.e(t1, "<Destroy>", c)

    # ¥Ü¥¿¥ó¤ÎÀ¸À®.
    wid = []
    # toplevel ¥¦¥£¥¸¥§¥Ã¥È¤òÇ˲õ¤¹¤ë¤Ë¤Ï destroy ¤¹¤ë.
    cb.push(c = TclTkCallback.new(ip, proc{destroy.e(t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text close -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_label(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text label -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_button(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text button -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_checkbutton(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text checkbutton -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_radiobutton(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text radiobutton -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_scale(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text scale -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_entry(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text entry -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_text(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text text -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_raise(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text raise/lower -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_modal(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text message/modal -command",
      c))
    cb.push(c = TclTkCallback.new(ip, proc{test_menu(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text menu -command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_listbox(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text listbox/scrollbar",
      "-command", c))
    cb.push(c = TclTkCallback.new(ip, proc{test_canvas(ip, t1)}))
    wid.push(TclTkWidget.new(ip, t1, button, "-text canvas -command", c))

    # grid ¤Çɽ¼¨¤¹¤ë.
    ro = co = 0
    wid.each{|w|
      grid.e(w, "-row", ro, "-column", co, "-sticky news")
      ro += 1
      if ro == 7
        ro = 0
        co += 1
      end
    }
  end

  # inittoplevel(ip, parent, title)
  #   °Ê²¼¤Î½èÍý¤ò¤Þ¤È¤á¤Æ¹Ô¤¦.
  #       1. toplevel ¥¦¥£¥¸¥§¥Ã¥È¤òºîÀ®¤¹¤ë.
  #       2. ¥³¡¼¥ë¥Ð¥Ã¥¯¤òÅÐÏ¿¤¹¤ëÇÛÎó¤òÍÑ°Õ¤·, toplevel ¥¦¥£¥¸¥§¥Ã¥È¤Î
  #         <Destroy> ¥¤¥Ù¥ó¥È¤Ë¥³¡¼¥ë¥Ð¥Ã¥¯¤òºï½ü¤¹¤ë¼ê³¤­¤òÅÐÏ¿¤¹¤ë.
  #       3. ¥¯¥í¡¼¥º¥Ü¥¿¥ó¤òºî¤ë.
  #     ºîÀ®¤·¤¿ toplevel ¥¦¥£¥¸¥§¥Ã¥È, ¥¯¥í¡¼¥º¥Ü¥¿¥ó, ¥³¡¼¥ë¥Ð¥Ã¥¯ÅÐÏ¿ÍÑÊÑ¿ô
  #     ¤òÊÖ¤¹.
  #   ip: ¥¤¥ó¥¿¥×¥ê¥¿
  #   parent: ¿Æ¥¦¥£¥¸¥§¥Ã¥È
  #   title: toplevel ¥¦¥£¥¸¥§¥Ã¥È¤Î¥¦¥¤¥ó¥É¥¦¤Î¥¿¥¤¥È¥ë
  def inittoplevel(ip, parent, title)
    bind, button, destroy, toplevel, wm = ip.commands().values_at(
      "bind", "button", "destroy", "toplevel", "wm")

    # ¿·¤·¤¤¥¦¥¤¥ó¥É¥¦¤ò³«¤¯¤Ë¤Ï, toplevel ¤ò»È¤¦.
    t1 = TclTkWidget.new(ip, parent, toplevel)
    # ¥¿¥¤¥È¥ë¤òÉÕ¤±¤Æ¤ª¤¯
    wm.e("title", t1, title)

    # ¥¦¥£¥¸¥§¥Ã¥È¤¬Ç˲õ¤µ¤ì¤¿¤È¤­, ¥³¡¼¥ë¥Ð¥Ã¥¯¤¬ GC ¤ÎÂоݤˤʤë¤è¤¦¤Ë¤¹¤ë.
    cb = []
    cb.push(c = TclTkCallback.new(ip, proc{|w| TclTk.dcb(cb, t1, w)}, "%W"))
    bind.e(t1, "<Destroy>", c)
    # close ¥Ü¥¿¥ó¤òºî¤Ã¤Æ¤ª¤¯.
    # toplevel ¥¦¥£¥¸¥§¥Ã¥È¤òÇ˲õ¤¹¤ë¤Ë¤Ï destroy ¤¹¤ë.
    cb.push(c = TclTkCallback.new(ip, proc{destroy.e(t1)}))
    b1 = TclTkWidget.new(ip, t1, button, "-text close -command", c)

    return t1, b1, cb
  end

  # label ¤Î¥µ¥ó¥×¥ë.
  def test_label(ip, parent)
    button, global, label, pack = ip.commands().values_at(
      "button", "global", "label", "pack")
    t1, b1, cb = inittoplevel(ip, parent, "label")

    ## label

    # ¤¤¤í¤¤¤í¤Ê·Á¤Î¥é¥Ù¥ë.
    l1 = TclTkWidget.new(ip, t1, label, "-text {default(flat)}")
    l2 = TclTkWidget.new(ip, t1, label, "-text raised -relief raised")
    l3 = TclTkWidget.new(ip, t1, label, "-text sunken -relief sunken")
    l4 = TclTkWidget.new(ip, t1, label, "-text groove -relief groove")
    l5 = TclTkWidget.new(ip, t1, label, "-text ridge -relief ridge")
    l6 = TclTkWidget.new(ip, t1, label, "-bitmap error")
    l7 = TclTkWidget.new(ip, t1, label, "-bitmap questhead")

    # pack ¤·¤Æ¤âɽ¼¨¤µ¤ì¤ë.
    pack.e(b1, l1, l2, l3, l4, l5, l6, l7, "-pady 3")

    ## -textvariable

    # tcltk ¥é¥¤¥Ö¥é¥ê¤Î¼ÂÁõ¤Ç¤Ï, ¥³¡¼¥ë¥Ð¥Ã¥¯¤Ï tcl/tk ¤Î``¼ê³¤­''¤òÄ̤·¤Æ
    # ¸Æ¤Ð¤ì¤ë. ¤·¤¿¤¬¤Ã¤Æ, ¥³¡¼¥ë¥Ð¥Ã¥¯¤ÎÃæ¤Ç(Âç°è)ÊÑ¿ô¤Ë¥¢¥¯¥»¥¹¤¹¤ë¤È¤­¤Ï,
    # global ¤¹¤ëɬÍפ¬¤¢¤ë.
    # global ¤¹¤ëÁ°¤ËÊÑ¿ô¤ËÃͤòÀßÄꤷ¤Æ¤·¤Þ¤¦¤È¥¨¥é¡¼¤Ë¤Ê¤ë¤Î¤Ç,
    # tcl/tk ¤Ë¤ª¤±¤ëɽ¸½·Á¤À¤±À¸À®¤·¤Æ, ¼ÂºÝ¤ËÃͤòÀßÄꤷ¤Ê¤¤¤è¤¦¤Ë,
    # 2 ÈÖÌܤΰú¿ô¤Ë¤Ï nil ¤òÍ¿¤¨¤ë.
    v1 = TclTkVariable.new(ip, nil)
    global.e(v1)
    v1.set(100)
    # -textvariable ¤ÇÊÑ¿ô¤òÀßÄꤹ¤ë.
    l6 = TclTkWidget.new(ip, t1, label, "-textvariable", v1)
    # ¥³¡¼¥ë¥Ð¥Ã¥¯¤ÎÃ椫¤éÊÑ¿ô¤òÁàºî¤¹¤ë.
    cb.push(c = TclTkCallback.new(ip, proc{
      global.e(v1); v1.set(v1.get().to_i + 10)}))
    b2 = TclTkWidget.new(ip, t1, button, "-text +10 -command", c)
    cb.push(c = TclTkCallback.new(ip, proc{
      global.e(v1); v1.set(v1.get().to_i - 10)}))
    b3 = TclTkWidget.new(ip, t1, button, "-text -10 -command", c)
    pack.e(l6, b2, b3)
  end

  # button ¤Î¥µ¥ó¥×¥ë.
  def test_button(ip, parent)
    button, pack = ip.commands().values_at("button", "pack")
    t1, b1, cb = inittoplevel(ip, parent, "button")

    ## button

    # ¥³¡¼¥ë¥Ð¥Ã¥¯Æâ¤Ç»²¾È¤¹¤ëÊÑ¿ô¤ÏÀè¤ËÀë¸À¤·¤Æ¤ª¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤.
    b3 = b4 = nil
    cb.push(c = TclTkCallback.new(ip, proc{b3.e("flash"); b4.e("flash")}))
    b2 = TclTkWidget.new(ip, t1, button, "-text flash -command", c)
    cb.push(c = TclTkCallback.new(ip, proc{b2.e("configure -state normal")}))
    b3 = TclTkWidget.new(ip, t1, button, "-text normal -command", c)
    cb.push(c = TclTkCallback.new(ip, proc{b2.e("configure -state disabled")}))
    b4 = TclTkWidget.new(ip, t1, button, "-text disable -command", c)
    pack.e(b1, b2, b3, b4)
  end

  # checkbutton ¤Î¥µ¥ó¥×¥ë.
  def test_checkbutton(ip, parent)
    checkbutton, global, pack = ip.commands().values_at(
      "checkbutton", "global", "pack")
    t1, b1, cb = inittoplevel(ip, parent, "checkbutton")

    ## checkbutton

    v1 = TclTkVariable.new(ip, nil)
    global.e(v1)
    # -variable ¤ÇÊÑ¿ô¤òÀßÄꤹ¤ë.
    ch1 = TclTkWidget.new(ip, t1, checkbutton, "-onvalue on -offvalue off",
      "-textvariable", v1, "-variable", v1)
    pack.e(b1, ch1)
  end

  # radiobutton ¤Î¥µ¥ó¥×¥ë.
  def test_radiobutton(ip, parent)
    global, label, pack, radiobutton = ip.commands().values_at(
      "global", "label", "pack", "radiobutton")
    t1, b1, cb = inittoplevel(ip, parent, "radiobutton")

    ## radiobutton

    v1 = TclTkVariable.new(ip, nil)
    global.e(v1)
    # ¥Ì¥ë¥¹¥È¥ê¥ó¥°¤Ï "{}" ¤Ç»ØÄꤹ¤ë.
    v1.set("{}")
    l1 = TclTkWidget.new(ip, t1, label, "-textvariable", v1)
    # -variable ¤ÇƱ¤¸ÊÑ¿ô¤ò»ØÄꤹ¤ë¤ÈƱ¤¸¥°¥ë¡¼¥×¤Ë¤Ê¤ë.
    ra1 = TclTkWidget.new(ip, t1, radiobutton,
      "-text radio1 -value r1 -variable", v1)
    ra2 = TclTkWidget.new(ip, t1, radiobutton,
      "-text radio2 -value r2 -variable", v1)
    cb.push(c = TclTkCallback.new(ip, proc{global.e(v1); v1.set("{}")}))
    ra3 = TclTkWidget.new(ip, t1, radiobutton,
      "-text clear -value r3 -variable", v1, "-command", c)
    pack.e(b1, l1, ra1, ra2, ra3)
  end

  # scale ¤Î¥µ¥ó¥×¥ë.
  def test_scale(ip, parent)
    global, pack, scale = ip.commands().values_at(
      "global", "pack", "scale")
    t1, b1, cb = inittoplevel(ip, parent, "scale")

    ## scale

    v1 = TclTkVariable.new(ip, nil)
    global.e(v1)
    v1.set(219)
    # ¥³¡¼¥ë¥Ð¥Ã¥¯Æâ¤Ç»²¾È¤¹¤ëÊÑ¿ô¤ÏÀè¤ËÀë¸À¤·¤Æ¤ª¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤.
    sca1 = nil
    cb.push(c = TclTkCallback.new(ip, proc{global.e(v1); v = v1.get();
      sca1.e("configure -background", format("#%02x%02x%02x", v, v, v))}))
    sca1 = TclTkWidget.new(ip, t1, scale,
      "-label scale -orient h -from 0 -to 255 -variable", v1, "-command", c)
    pack.e(b1, sca1)
  end

  # entry ¤Î¥µ¥ó¥×¥ë.
  def test_entry(ip, parent)
    button, entry, global, pack = ip.commands().values_at(
      "button", "entry", "global", "pack")
    t1, b1, cb = inittoplevel(ip, parent, "entry")

    ## entry

    v1 = TclTkVariable.new(ip, nil)
    global.e(v1)
    # ¥Ì¥ë¥¹¥È¥ê¥ó¥°¤Ï "{}" ¤Ç»ØÄꤹ¤ë.
    v1.set("{}")
    en1 = TclTkWidget.new(ip, t1, entry, "-textvariable", v1)
    cb.push(c = TclTkCallback.new(ip, proc{
      global.e(v1); print(v1.get(), "\n"); v1.set("{}")}))
    b2 = TclTkWidget.new(ip, t1, button, "-text print -command", c)
    pack.e(b1, en1, b2)
  end

  # text ¤Î¥µ¥ó¥×¥ë.
  def test_text(ip, parent)
    button, pack, text = ip.commands().values_at(
      "button", "pack", "text")
    t1, b1, cb = inittoplevel(ip, parent, "text")

    ## text

    te1 = TclTkWidget.new(ip, t1, text)
    cb.push(c = TclTkCallback.new(ip, proc{
      # 1 ¹ÔÌܤΠ0 ʸ»úÌܤ«¤éºÇ¸å¤Þ¤Ç¤òɽ¼¨¤·, ºï½ü¤¹¤ë.
      print(te1.e("get 1.0 end")); te1.e("delete 1.0 end")}))
    b2 = TclTkWidget.new(ip, t1, button, "-text print -command", c)
    pack.e(b1, te1, b2)
  end

  # raise/lower ¤Î¥µ¥ó¥×¥ë.
  def test_raise(ip, parent)
    button, frame, lower, pack, raise = ip.commands().values_at(
      "button", "frame", "lower", "pack", "raise")
    t1, b1, cb = inittoplevel(ip, parent, "raise/lower")

    ## raise/lower

    # button ¤ò±£¤¹¥Æ¥¹¥È¤Î¤¿¤á¤Ë, frame ¤ò»È¤¦.
    f1 = TclTkWidget.new(ip, t1, frame)
    # ¥³¡¼¥ë¥Ð¥Ã¥¯Æâ¤Ç»²¾È¤¹¤ëÊÑ¿ô¤ÏÀè¤ËÀë¸À¤·¤Æ¤ª¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤.
    b2 = nil
    cb.push(c = TclTkCallback.new(ip, proc{raise.e(f1, b2)}))
    b2 = TclTkWidget.new(ip, t1, button, "-text raise -command", c)
    cb.push(c = TclTkCallback.new(ip, proc{lower.e(f1, b2)}))
    b3 = TclTkWidget.new(ip, t1, button, "-text lower -command", c)
    lower.e(f1, b3)

    pack.e(b2, b3, "-in", f1)
    pack.e(b1, f1)
  end

  # modal ¤Ê¥¦¥£¥¸¥§¥Ã¥È¤Î¥µ¥ó¥×¥ë.
  def test_modal(ip, parent)
    button, frame, message, pack, tk_chooseColor, tk_getOpenFile,
      tk_messageBox = ip.commands().values_at(
      "button", "frame", "message", "pack", "tk_chooseColor",
      "tk_getOpenFile", "tk_messageBox")
    # ºÇ½é¤Ë load ¤µ¤ì¤Æ¤¤¤Ê¤¤¥é¥¤¥Ö¥é¥ê¤Ï ip.commands() ¤Ë¸ºß¤·¤Ê¤¤¤Î¤Ç,
    # TclTkLibCommand ¤òÀ¸À®¤¹¤ëɬÍפ¬¤¢¤ë.
    tk_dialog = TclTkLibCommand.new(ip, "tk_dialog")
    t1, b1, cb = inittoplevel(ip, parent, "message/modal")

    ## message

    mes = "¤³¤ì¤Ï message ¥¦¥£¥¸¥§¥Ã¥È¤Î¥Æ¥¹¥È¤Ç¤¹."
    mes += "°Ê²¼¤Ï modal ¤Ê¥¦¥£¥¸¥§¥Ã¥È¤Î¥Æ¥¹¥È¤Ç¤¹."
    me1 = TclTkWidget.new(ip, t1, message, "-text {#{mes}}")

    ## modal

    # tk_messageBox
    cb.push(c = TclTkCallback.new(ip, proc{
      print tk_messageBox.e("-type yesnocancel -message messageBox",
      "-icon error -default cancel -title messageBox"), "\n"}))
    b2 = TclTkWidget.new(ip, t1, button, "-text messageBox -command", c)
    # tk_dialog
    cb.push(c = TclTkCallback.new(ip, proc{
      # ¥¦¥£¥¸¥§¥Ã¥È̾¤òÀ¸À®¤¹¤ë¤¿¤á¤Ë¥À¥ß¡¼¤Î frame ¤òÀ¸À®.
      print tk_dialog.e(TclTkWidget.new(ip, t1, frame),
      "dialog dialog error 2 yes no cancel"), "\n"}))
    b3 = TclTkWidget.new(ip, t1, button, "-text dialog -command", c)
    # tk_chooseColor
    cb.push(c = TclTkCallback.new(ip, proc{
      print tk_chooseColor.e("-title chooseColor"), "\n"}))
    b4 = TclTkWidget.new(ip, t1, button, "-text chooseColor -command", c)
    # tk_getOpenFile
    cb.push(c = TclTkCallback.new(ip, proc{
      print tk_getOpenFile.e("-defaultextension .rb",
      "-filetypes {{{Ruby Script} {.rb}} {{All Files} {*}}}",
      "-title getOpenFile"), "\n"}))
    b5 = TclTkWidget.new(ip, t1, button, "-text getOpenFile -command", c)

    pack.e(b1, me1, b2, b3, b4, b5)
  end

  # menu ¤Î¥µ¥ó¥×¥ë.
  def test_menu(ip, parent)
    global, menu, menubutton, pack = ip.commands().values_at(
      "global", "menu", "menubutton", "pack")
    tk_optionMenu = TclTkLibCommand.new(ip, "tk_optionMenu")
    t1, b1, cb = inittoplevel(ip, parent, "menu")

    ## menu

    # menubutton ¤òÀ¸À®¤¹¤ë.
    mb1 = TclTkWidget.new(ip, t1, menubutton, "-text menu")
    # menu ¤òÀ¸À®¤¹¤ë.
    me1 = TclTkWidget.new(ip, mb1, menu)
    # mb1 ¤«¤é me1 ¤¬µ¯Æ°¤µ¤ì¤ë¤è¤¦¤Ë¤¹¤ë.
    mb1.e("configure -menu", me1)

    # cascade ¤Çµ¯Æ°¤µ¤ì¤ë menu ¤òÀ¸À®¤¹¤ë.
    me11 = TclTkWidget.new(ip, me1, menu)
    # radiobutton ¤Î¥µ¥ó¥×¥ë.
    v1 = TclTkVariable.new(ip, nil); global.e(v1); v1.set("r1")
    me11.e("add radiobutton -label radio1 -value r1 -variable", v1)
    me11.e("add radiobutton -label radio2 -value r2 -variable", v1)
    me11.e("add radiobutton -label radio3 -value r3 -variable", v1)
    # cascade ¤Ë¤è¤ê mb11 ¤¬µ¯Æ°¤µ¤ì¤ë¤è¤¦¤Ë¤¹¤ë.
    me1.e("add cascade -label cascade -menu", me11)

    # checkbutton ¤Î¥µ¥ó¥×¥ë.
    v2 = TclTkVariable.new(ip, nil); global.e(v2); v2.set("none")
    me1.e("add checkbutton -label check -variable", v2)
    # separator ¤Î¥µ¥ó¥×¥ë.
    me1.e("add separator")
    # command ¤Î¥µ¥ó¥×¥ë.
    v3 = nil
    cb.push(c = TclTkCallback.new(ip, proc{
      global.e(v1, v2, v3); print "v1: ", v1.get(), ", v2: ", v2.get(),
      ", v3: ", v3.get(), "\n"}))
    me1.e("add command -label print -command", c)

    ## tk_optionMenu

    v3 = TclTkVariable.new(ip, nil); global.e(v3); v3.set("opt2")
    om1 = TclTkWidget.new(ip, t1, tk_optionMenu, v3, "opt1 opt2 opt3 opt4")

    pack.e(b1, mb1, om1, "-side left")
  end

  # listbox ¤Î¥µ¥ó¥×¥ë.
  def test_listbox(ip, parent)
    clipboard, frame, grid, listbox, lower, menu, menubutton, pack, scrollbar,
      selection = ip.commands().values_at(
      "clipboard", "frame", "grid", "listbox", "lower", "menu", "menubutton",
      "pack", "scrollbar", "selection")
    t1, b1, cb = inittoplevel(ip, parent, "listbox")

    ## listbox/scrollbar

    f1 = TclTkWidget.new(ip, t1, frame)
    # ¥³¡¼¥ë¥Ð¥Ã¥¯Æâ¤Ç»²¾È¤¹¤ëÊÑ¿ô¤ÏÀè¤ËÀë¸À¤·¤Æ¤ª¤«¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤.
    li1 = sc1 = sc2 = nil
    # ¼Â¹Ô»þ¤Ë, ¸å¤í¤Ë¥Ñ¥é¥á¡¼¥¿¤¬¤Ä¤¯¥³¡¼¥ë¥Ð¥Ã¥¯¤Ï,
    # ¥¤¥Æ¥ì¡¼¥¿ÊÑ¿ô¤Ç¤½¤Î¥Ñ¥é¥á¡¼¥¿¤ò¼õ¤±¼è¤ë¤³¤È¤¬¤Ç¤­¤ë.
    # (Ê£¿ô¤Î¥Ñ¥é¥á¡¼¥¿¤Ï¤Ò¤È¤Ä¤Îʸ»úÎó¤Ë¤Þ¤È¤á¤é¤ì¤ë.)
    cb.push(c1 = TclTkCallback.new(ip, proc{|i| li1.e("xview", i)}))
    cb.push(c2 = TclTkCallback.new(ip, proc{|i| li1.e("yview", i)}))
    cb.push(c3 = TclTkCallback.new(ip, proc{|i| sc1.e("set", i)}))
    cb.push(c4 = TclTkCallback.new(ip, proc{|i| sc2.e("set", i)}))
    # listbox
    li1 = TclTkWidget.new(ip, f1, listbox,
      "-xscrollcommand", c3, "-yscrollcommand", c4,
      "-selectmode extended -exportselection true")
    for i in 1..20
      li1.e("insert end {line #{i} line #{i} line #{i} line #{i} line #{i}}")
    end
    # scrollbar
    sc1 = TclTkWidget.new(ip, f1, scrollbar, "-orient horizontal -command", c1)
    sc2 = TclTkWidget.new(ip, f1, scrollbar, "-orient vertical -command", c2)

    ## selection/clipboard

    mb1 = TclTkWidget.new(ip, t1, menubutton, "-text edit")
    me1 = TclTkWidget.new(ip, mb1, menu)
    mb1.e("configure -menu", me1)
    cb.push(c = TclTkCallback.new(ip, proc{
      # clipboard ¤ò¥¯¥ê¥¢.
      clipboard.e("clear")
      # selection ¤«¤éʸ»úÎó¤òÆɤ߹þ¤ß clipboard ¤ËÄɲ乤ë.
      clipboard.e("append {#{selection.e('get')}}")}))
    me1.e("add command -label {selection -> clipboard} -command",c)
    cb.push(c = TclTkCallback.new(ip, proc{
      # li1 ¤ò¥¯¥ê¥¢.
      li1.e("delete 0 end")
      # clipboard ¤«¤éʸ»úÎó¤ò¼è¤ê½Ð¤·, 1 ¹Ô¤º¤Ä
      selection.e("get -selection CLIPBOARD").split(/\n/).each{|line|
        # li1 ¤ËÁÞÆþ¤¹¤ë.
        li1.e("insert end {#{line}}")}}))
    me1.e("add command -label {clipboard -> listbox} -command",c)

    grid.e(li1, "-row 0 -column 0 -sticky news")
    grid.e(sc1, "-row 1 -column 0 -sticky ew")
    grid.e(sc2, "-row 0 -column 1 -sticky ns")
    grid.e("rowconfigure", f1, "0 -weight 100")
    grid.e("columnconfigure", f1, "0 -weight 100")
    f2 = TclTkWidget.new(ip, t1, frame)
    lower.e(f2, b1)
    pack.e(b1, mb1, "-in", f2, "-side left")
    pack.e(f2, f1)
  end

  # canvas ¤Î¥µ¥ó¥×¥ë.
  def test_canvas(ip, parent)
    canvas, lower, pack = ip.commands().values_at("canvas", "lower", "pack")
    t1, b1, cb = inittoplevel(ip, parent, "canvas")

    ## canvas

    ca1 = TclTkWidget.new(ip, t1, canvas, "-width 400 -height 300")
    lower.e(ca1, b1)
    # rectangle ¤òºî¤ë.
    idr = ca1.e("create rectangle 10 10 20 20")
    # oval ¤òºî¤ë.
    ca1.e("create oval 60 10 100 50")
    # polygon ¤òºî¤ë.
    ca1.e("create polygon 110 10 110 30 140 10")
    # line ¤òºî¤ë.
    ca1.e("create line 150 10 150 30 190 10")
    # arc ¤òºî¤ë.
    ca1.e("create arc 200 10 250 50 -start 0 -extent 90 -style pieslice")
    # i1 ¤ÏËÜÅö¤Ï, ¤É¤³¤«¤ÇÇ˲õ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¤¬, ÌÌÅݤʤΤÇÊü¤Ã¤Æ¤¢¤ë.
    i1 = TclTkImage.new(ip, "photo", "-file maru.gif")
    # image ¤òºî¤ë.
    ca1.e("create image 100 100 -image", i1)
    # bitmap ¤òºî¤ë.
    ca1.e("create bitmap 260 50 -bitmap questhead")
    # text ¤òºî¤ë.
    ca1.e("create text 320 50 -text {drag rectangle}")
    # window ¤òºî¤ë(¥¯¥í¡¼¥º¥Ü¥¿¥ó).
    ca1.e("create window 200 200 -window", b1)

    # bind ¤Ë¤è¤ê rectangle ¤ò drag ¤Ç¤­¤ë¤è¤¦¤Ë¤¹¤ë.
    cb.push(c = TclTkCallback.new(ip, proc{|i|
      # i ¤Ë x ¤È y ¤ò¼õ¤±¼è¤ë¤Î¤Ç, ¼è¤ê½Ð¤¹.
      x, y = i.split(/ /); x = x.to_f; y = y.to_f
      # ºÂɸ¤òÊѹ¹¤¹¤ë.
      ca1.e("coords current #{x - 5} #{y - 5} #{x + 5} #{y + 5}")},
      # x, y ºÂɸ¤ò¶õÇò¤Ç¶èÀڤ俤â¤Î¤ò¥¤¥Æ¥ì¡¼¥¿ÊÑ¿ô¤ØÅϤ¹¤è¤¦¤Ë»ØÄê.
      "%x %y"))
    # rectangle ¤Ë bind ¤¹¤ë.
    ca1.e("bind", idr, "<B1-Motion>", c)

    pack.e(ca1)
  end
end

# test driver

if ARGV.size == 0
  print "#{$0} n ¤Ç, n ¸Ä¤Î¥¤¥ó¥¿¥×¥ê¥¿¤òµ¯Æ°¤·¤Þ¤¹.\n"
  n = 1
else
  n = ARGV[0].to_i
end

print "start\n"
ip = []

# ¥¤¥ó¥¿¥×¥ê¥¿, ¥¦¥£¥¸¥§¥Ã¥ÈÅù¤ÎÀ¸À®.
for i in 1 .. n
  ip.push(Test1.new())
end

# ÍÑ°Õ¤¬¤Ç¤­¤¿¤é¥¤¥Ù¥ó¥È¥ë¡¼¥×¤ËÆþ¤ë.
TclTk.mainloop()
print "exit from mainloop\n"

# ¥¤¥ó¥¿¥×¥ê¥¿¤¬ GC ¤µ¤ì¤ë¤«¤Î¥Æ¥¹¥È.
ip = []
print "GC.start\n" if $DEBUG
GC.start() if $DEBUG
print "end\n"

exit

# end