OSDN Git Service

05ca732cfd398f1d35162f4a6969b7ca89f9fecf
[splhack/AndroidRuby.git] / lib / ruby-1.9.1-rc1 / ext / tk / sample / demos-jp / ctext.rb
1 # -*- coding: euc-jp -*-
2 #
3 # Canvas Text widget demo (called by 'widget')
4 #
5
6 # toplevel widget ¤¬Â¸ºß¤¹¤ì¤Ðºï½ü¤¹¤ë
7 if defined?($ctext_demo) && $ctext_demo
8   $ctext_demo.destroy 
9   $ctext_demo = nil
10 end
11
12 # demo ÍѤΠtoplevel widget ¤òÀ¸À®
13 $ctext_demo = TkToplevel.new {|w|
14   title("Canvas Text Demonstration")
15   iconname("Text")
16   positionWindow(w)
17 }
18
19 base_frame = TkFrame.new($ctext_demo).pack(:fill=>:both, :expand=>true)
20
21 # label À¸À®
22 TkLabel.new(base_frame, 'font'=>$font, 'wraplength'=>'5i', 'justify'=>'left', 
23             'text'=>"¤³¤Î¥¦¥£¥ó¥É¥¦¤Ë¤Ï¥­¥ã¥ó¥Ð¥¹widget¤Î¥Æ¥­¥¹¥Èµ¡Ç½¤ò¥Ç¥â¤¹¤ë¤¿¤á¤Î¥Æ¥­¥¹¥Èʸ»úÎó¤¬É½¼¨¤µ¤ì¤Æ¤¤¤Þ¤¹¡£¥Þ¥¦¥¹¤ò»Í³Ñ¤ÎÃæ¤Ë»ý¤Ã¤Æ¤¤¤­¡¢¥¯¥ê¥Ã¥¯¤¹¤ë¤È°ÌÃÖ¤®¤áÍѤÎÅÀ¤«¤é¤ÎÁêÂаÌÃÖ¤òÊѤ¨¤¿¤ê¡¢¹Ô·¤¨¤òÊѤ¨¤¿¤ê¤¹¤ë¤³¤È¤¬¤Ç¤­¤Þ¤¹¡£¤Þ¤¿°Ê²¼¤Î¤è¤¦¤ÊÊÔ½¸¤Î¤¿¤á¤Î´Êñ¤Ê¥Ð¥¤¥ó¥Ç¥£¥ó¥°¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Þ¤¹¡£
24
25   1. ¥Þ¥¦¥¹¤ò»ý¤Ã¤Æ¤¤¤­¡¢¥¯¥ê¥Ã¥¯¤·¡¢ÆþÎϤǤ­¤Þ¤¹¡£
26   2. ¥Ü¥¿¥ó1¤ÇÁªÂò¤Ç¤­¤Þ¤¹¡£
27   3. ¥Þ¥¦¥¹¤Î°ÌÃ֤˥ܥ¿¥ó2¤ÇÁªÂò¤·¤¿¥Æ¥­¥¹¥È¤ò¥³¥Ô¡¼¤Ç¤­¤Þ¤¹¡£
28   4.¥Ð¥Ã¥¯¥¹¥Ú¡¼¥¹¤ò¥³¥ó¥È¥í¡¼¥ë-H¤ÇÁÞÆþ¥«¡¼¥½¥ë¤ÎľÁ°¤Îʸ»ú¤òºï½ü¤·¤Þ¤¹¡£
29   5. Delete¥­¡¼¤ÏÁÞÆþ¥«¡¼¥½¥ë¤Îľ¸å¤Îʸ»ú¤òºï½ü¤·¤Þ¤¹¡£"){
30   pack('side'=>'top')
31 }
32
33 # frame À¸À®
34 $ctext_buttons = TkFrame.new(base_frame) {|frame|
35   TkButton.new(frame) {
36     #text 'λ²ò'
37     text 'ÊĤ¸¤ë'
38     command proc{
39       tmppath = $ctext_demo
40       $ctext_demo = nil
41       tmppath.destroy
42     }
43   }.pack('side'=>'left', 'expand'=>'yes')
44
45   TkButton.new(frame) {
46     text '¥³¡¼¥É»²¾È'
47     command proc{showCode 'ctext'}
48   }.pack('side'=>'left', 'expand'=>'yes')
49 }
50 $ctext_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
51
52 # canvas À¸À®
53 $ctext_canvas = TkCanvas.new(base_frame, 'relief'=>'flat', 
54                              'borderwidth'=>0, 'width'=>500, 'height'=>350)
55 $ctext_canvas.pack('side'=>'top', 'expand'=>'yes', 'fill'=>'both')
56
57 # font ÀßÄê
58 if $tk_version =~ /^4.*/
59   textFont = '-*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*'
60 else
61   textFont = 'Helvetica 24'
62 end
63
64 # canvas ÀßÄê
65 TkcRectangle.new($ctext_canvas, 245, 195, 255, 205, 
66                  'outline'=>'black', 'fill'=>'red')
67
68 ctag_text_param = {
69   'text'=>"¤³¤ì¤Ï¥­¥ã¥ó¥Ð¥¹widget¤Î¥Æ¥­¥¹¥Èµ¡Ç½¤ò¥Ç¥â¤¹¤ë¤¿¤á¤Îʸ»úÎó¤Ç¤¹¡£\n¾å¤Ç½Ò¤Ù¤¿¤è¤¦¤ÊÊÔ½¸¤ò²Äǽ¤È¤¹¤ë¤¿¤á¤Î¥Ð¥¤¥ó¥Ç¥£¥ó¥°¤ò»Ü¤·¤Æ¤¤¤Þ¤¹¡£",
70   'width'=>440, 'anchor'=>'n', 'justify'=>'left'
71 }
72 if $tk_version =~ /^4.*/
73   ctag_text_param['font'] = '-*-Helvetica-Medium-R-Normal--*-240-*-*-*-*-*-*'
74   ctag_text_param['kanjifont'] = '-*-r-*--24-*-jisx0208.1983-0'
75 else
76   ctag_text_param['font'] = 'Helvetica 24'
77 end
78 $ctag_text = TkcTag.new($ctext_canvas)
79 $ctag_text.withtag(TkcText.new($ctext_canvas, 250, 200, ctag_text_param))
80
81 $ctag_text.bind('1', proc{|x,y| textB1Press $ctext_canvas,x,y}, "%x %y")
82 $ctag_text.bind('B1-Motion', proc{|x,y| textB1Move $ctext_canvas,x,y}, "%x %y")
83 $ctag_text.bind('Shift-1', 
84         proc{|x,y| $ctext_canvas.seleect_adjust 'current', "@#{x},#{y}"}, 
85         "%x %y")
86 $ctag_text.bind('Shift-B1-Motion', 
87                 proc{|x,y| textB1Move $ctext_canvas,x,y}, "%x %y")
88 $ctag_text.bind('KeyPress', proc{|a| textInsert $ctext_canvas,a}, "%A")
89 $ctag_text.bind('Return', proc{textInsert $ctext_canvas,"\n"})
90 $ctag_text.bind('Control-h', proc{textBs $ctext_canvas})
91 $ctag_text.bind('BackSpace', proc{textBs $ctext_canvas})
92 $ctag_text.bind('Delete', proc{textDel $ctext_canvas})
93 $ctag_text.bind('2', proc{|x,y| textPaste $ctext_canvas, "@#{x},#{y}"}, 
94                 "%x %y")
95
96 # Next, create some items that allow the text's anchor position 
97 # to be edited.
98
99 def mkTextConfig(w,x,y,option,value,color)
100   item = TkcRectangle.new(w, x, y, x+30, y+30, 
101                           'outline'=>'black', 'fill'=>color, 'width'=>1)
102   item.bind('1', proc{$ctag_text.configure option, value})
103   w.addtag_withtag('config', item)
104 end
105
106 x = 50
107 y = 50
108 color = 'LightSkyBlue1'
109 mkTextConfig $ctext_canvas, x, y, 'anchor', 'se', color
110 mkTextConfig $ctext_canvas, x+30, y, 'anchor', 's', color
111 mkTextConfig $ctext_canvas, x+60, y, 'anchor', 'sw', color
112 mkTextConfig $ctext_canvas, x, y+30, 'anchor', 'e', color
113 mkTextConfig $ctext_canvas, x+30, y+30, 'anchor', 'center', color
114 mkTextConfig $ctext_canvas, x+60, y+30, 'anchor', 'w', color
115 mkTextConfig $ctext_canvas, x, y+60, 'anchor', 'ne', color
116 mkTextConfig $ctext_canvas, x+30, y+60, 'anchor', 'n', color
117 mkTextConfig $ctext_canvas, x+60, y+60, 'anchor', 'nw', color
118 item = TkcRectangle.new($ctext_canvas, x+40, y+40, x+50, y+50, 
119                         'outline'=>'black', 'fill'=>'red')
120 item.bind('1', proc{$ctag_text.configure 'anchor', 'center'})
121 if $tk_version =~ /^4.*/
122   TkcText.new($ctext_canvas, x+45, y-5, 'text'=>'Text Position', 
123               'font'=>'-*-times-medium-r-normal--*-240-*-*-*-*-*-*', 
124               'anchor'=>'s', 'fill'=>'brown')
125 else
126   TkcText.new($ctext_canvas, x+45, y-5, 'text'=>'Text Position', 
127               'font'=>'Times 24', 'anchor'=>'s', 'fill'=>'brown')
128 end
129
130 # Lastly, create some items that allow the text's justification to be
131 # changed.
132
133 x = 350
134 y = 50
135 color = 'SeaGreen2'
136 mkTextConfig $ctext_canvas, x, y, 'justify', 'left', color
137 mkTextConfig $ctext_canvas, x+30, y, 'justify', 'center', color
138 mkTextConfig $ctext_canvas, x+60, y, 'justify', 'right', color
139 if $tk_version =~ /^4.*/
140   TkcText.new($ctext_canvas, x+45, y-5, 'text'=>'Justification', 
141               'font'=>'-*-times-medium-r-normal--*-240-*-*-*-*-*-*', 
142               'anchor'=>'s', 'fill'=>'brown')
143 else
144   TkcText.new($ctext_canvas, x+45, y-5, 'text'=>'Justification', 
145               'font'=>'Times 24', 'anchor'=>'s', 'fill'=>'brown')
146 end
147
148 $ctext_canvas.itembind('config', 'Enter', proc{textEnter $ctext_canvas})
149 $ctext_canvas.itembind('config', 'Leave', 
150                        proc{$ctext_canvas\
151                              .itemconfigure('current', 
152                                             'fill'=>$textConfigFill)})
153
154 $textConfigFill = ''
155
156 def textEnter(w)
157   $textConfigFill = (w.itemconfiginfo 'current', 'fill')[4]
158   w.itemconfigure 'current', 'fill', 'black'
159 end
160
161 def textInsert(w, string)
162   return if string == ""
163   begin
164     $ctag_text.dchars 'sel.first', 'sel.last'
165   rescue
166   end
167   $ctag_text.insert 'insert', string
168 end
169
170 def textPaste(w, pos)
171   begin
172     $ctag_text.insert pos, TkSelection.get
173   rescue
174   end
175 end
176
177 def textB1Press(w,x,y)
178   w.icursor 'current', "@#{x},#{y}"
179   w.itemfocus 'current'
180   w.focus
181   w.select_from 'current', "@#{x},#{y}"
182 end
183
184 def textB1Move(w,x,y)
185   w.select_to 'current', "@#{x},#{y}"
186 end
187
188 def textBs(w)
189   begin
190     $ctag_text.dchars 'sel.first', 'sel.last'
191   rescue
192     char = $ctag_text.index('insert').to_i - 1
193     $ctag_text.dchars(char) if char >= 0
194   end
195 end
196
197 def textDel(w)
198   begin
199     $ctag_text.dchars 'sel.first', 'sel.last'
200   rescue
201     $ctag_text.dchars 'insert'
202   end
203 end
204