1 # -*- coding: euc-jp -*-
3 # Text Search widget demo (called by 'widget')
7 # This method below loads a file into a text widget, discarding
8 # the previous contents of the widget. Tags for the old widget are
9 # not affected, however.
12 # w - The window into which to load the file. Must be a
14 # file - The name of the file to load. Must be readable.
16 def textLoadFile(w,file)
17 w.delete('1.0', 'end')
20 w.insert('end', f.read(1000))
26 # Search for all instances of a given string in a text widget and
27 # apply a given tag to each instance found.
30 # w - The window in which to search. Must be a text widget.
31 # string - The string to search for. The search is done using
32 # exact matching only; no special characters.
33 # tag - Tag to apply to each instance of a matching string.
35 def textSearch(w, string, tag)
36 tag.remove('0.0', 'end')
37 return if string == ""
40 cur, len = w.search_with_length(string, cur, 'end')
42 tag.add(cur, "#{cur} + #{len} char")
43 cur = w.index("#{cur} + #{len} char")
48 # This method is invoked repeatedly to invoke two commands at
49 # periodic intervals. It normally reschedules itself after each
50 # execution but if an error occurs (e.g. because the window was
51 # deleted) then it doesn't reschedule itself.
54 # cmd1 - Command to execute when method is called.
55 # sleep1 - Ms to sleep after executing cmd1 before executing cmd2.
56 # cmd2 - Command to execute in the *next* invocation of this method.
57 # sleep2 - Ms to sleep after executing cmd2 before executing cmd1 again.
59 def textToggle(cmd1,sleep1,cmd2,sleep2)
60 sleep_list = [sleep2, sleep1]
61 TkAfter.new(proc{sleep = sleep_list.shift; sleep_list.push(sleep); sleep},
62 -1, cmd1, cmd2).start(sleep1)
65 # toplevel widget ¤¬Â¸ºß¤¹¤ì¤Ðºï½ü¤¹¤ë
66 if defined?($search_demo) && $search_demo
71 # demo ÍѤΠtoplevel widget ¤òÀ¸À®
72 $search_demo = TkToplevel.new {|w|
73 title("Text Demonstration - Search and Highlight")
78 base_frame = TkFrame.new($search_demo).pack(:fill=>:both, :expand=>true)
81 $search_buttons = TkFrame.new(base_frame) {|frame|
86 tmppath = $search_demo
90 }.pack('side'=>'left', 'expand'=>'yes')
94 command proc{showCode 'search'}
95 }.pack('side'=>'left', 'expand'=>'yes')
97 $search_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')
100 TkFrame.new(base_frame) {|f|
101 TkLabel.new(f, 'text'=>'¥Õ¥¡¥¤¥ë̾:',
102 'width'=>13, 'anchor'=>'w').pack('side'=>'left')
103 $search_fileName = TkVariable.new
104 TkEntry.new(f, 'width'=>40,
105 'textvariable'=>$search_fileName) {
107 bind('Return', proc{textLoadFile($search_text, $search_fileName.value)
108 $search_string_entry.focus})
111 TkButton.new(f, 'text'=>'Æɤ߹þ¤ß',
112 'command'=>proc{textLoadFile($search_text,
113 $search_fileName.value)})\
114 .pack('side'=>'left', 'pady'=>5, 'padx'=>10)
115 }.pack('side'=>'top', 'fill'=>'x')
117 TkFrame.new(base_frame) {|f|
118 TkLabel.new(f, 'text'=>'¸¡º÷ʸ»úÎó:',
119 'width'=>13, 'anchor'=>'w').pack('side'=>'left')
120 $search_searchString = TkVariable.new
121 $search_string_entry = TkEntry.new(f, 'width'=>40,
122 'textvariable'=>$search_searchString) {
124 bind('Return', proc{textSearch($search_text, $search_searchString.value,
127 TkButton.new(f, 'text'=>'ȿž',
128 'command'=>proc{textSearch($search_text,
129 $search_searchString.value,
131 pack('side'=>'left', 'pady'=>5, 'padx'=>10)
133 }.pack('side'=>'top', 'fill'=>'x')
135 $search_text = TkText.new(base_frame, 'setgrid'=>true) {|t|
136 $search_Tag = TkTextTag.new(t)
137 TkScrollbar.new(base_frame, 'command'=>proc{|*args| t.yview(*args)}) {|sc|
138 t.yscrollcommand(proc{|first,last| sc.set first,last})
139 pack('side'=>'right', 'fill'=>'y')
141 pack('expand'=>'yes', 'fill'=>'both')
144 # Set up display styles for text highlighting.
146 if TkWinfo.depth($search_demo) > 1
148 $search_Tag.configure('background'=>'#ce5555',
149 'foreground'=>'white')
153 $search_Tag.configure('background'=>'', 'foreground'=>'')
158 $search_Tag.configure('background'=>'black',
159 'foreground'=>'white')
163 $search_Tag.configure('background'=>'', 'foreground'=>'')
167 $search_text.insert('1.0', "\
168 ¤³¤Î¥¦¥£¥ó¥É¥¦¤Ï¸¡º÷µ¡¹½¤ò¼Â¸½¤¹¤ë¤Î¤Ë¥Æ¥¥¹¥È widget ¤Î¥¿¥°µ¡Ç½¤¬¤É¤Î \
169 ¤è¤¦¤Ë»È¤ï¤ì¤ë¤Î¤«¤ò¥Ç¥â¤¹¤ë¤â¤Î¤Ç¤¹¡£¤Þ¤º¾å¤Î¥¨¥ó¥È¥ê¤Ë¥Õ¥¡¥¤¥ë̾¤òÆþ \
170 ¤ì¡¢<¥ê¥¿¡¼¥ó> ¤ò²¡¤¹¤«¡Ö¥í¡¼¥É¡×¥Ü¥¿¥ó¤ò²¡¤·¤Æ¤¯¤À¤µ¤¤¡£¼¡¤Ë¤½¤Î²¼¤Î \
171 ¥¨¥ó¥È¥ê¤Ëʸ»úÎó¤òÆþÎϤ·¡¢<¥ê¥¿¡¼¥ó> ¤ò²¡¤¹¤«¡Öȿž¡×¥Ü¥¿¥ó¤ò²¡¤·¤Æ¤¯ \
172 ¤À¤µ¤¤¡£¤¹¤ë¤È¥Õ¥¡¥¤¥ëÃæ¤Î¡¢¸¡º÷ʸ»úÎó¤È°ìÃפ¹¤ëÉôʬ¤ËÁ´¤Æ \"search_Tag\" \
173 ¤È¤¤¤¦¥¿¥°¤¬¤Ä¤±¤é¤ì¡¢¥¿¥°¤Îɽ¼¨Â°À¤È¤·¤Æ¤½¤Îʸ»úÎó¤¬ÅÀÌǤ¹¤ë¤è¤¦¤Ë \
175 $search_text.insert('end', "\
176 ¥Õ¥¡¥¤¥ëÆɤ߹þ¤ß¤Î¥«¥ì¥ó¥È¥Ç¥£¥ì¥¯¥È¥ê¤Ï \"#{Dir.pwd}\" ¤Ç¤¹¡£\
178 $search_text.set_insert '0.0'
180 $search_fileName.value = ''
181 $search_searchString.value = ''
183 $search_text.width = 60
184 $search_text.height = 20