summaryrefslogtreecommitdiff
path: root/ruby_1_8_5/ext/tk/sample/demos-jp/search.rb
blob: f5268f987fc891f5742956b36d66198ac72ad18f (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
#
# Text Search widget demo (called by 'widget')
#

# textLoadFile --
# This method below loads a file into a text widget, discarding
# the previous contents of the widget. Tags for the old widget are
# not affected, however.
#
# Arguments:
# w -           The window into which to load the file.  Must be a
#               text widget.
# file -        The name of the file to load.  Must be readable.

def textLoadFile(w,file)
  w.delete('1.0', 'end')
  f = open(file, 'r')
  while(!f.eof?)
    w.insert('end', f.read(1000))
  end
  f.close
end

# textSearch --
# Search for all instances of a given string in a text widget and
# apply a given tag to each instance found.
#
# Arguments:
# w -           The window in which to search.  Must be a text widget.
# string -      The string to search for.  The search is done using
#               exact matching only;  no special characters.
# tag -         Tag to apply to each instance of a matching string.

def textSearch(w, string, tag)
  tag.remove('0.0', 'end')
  return if string == ""
  cur = '1.0'
  loop {
    cur, len = w.search_with_length(string, cur, 'end')
    break if cur == ""
    tag.add(cur, "#{cur} + #{len} char")
    cur = w.index("#{cur} + #{len} char")
  }
end

# textToggle --
# This method is invoked repeatedly to invoke two commands at
# periodic intervals.  It normally reschedules itself after each
# execution but if an error occurs (e.g. because the window was
# deleted) then it doesn't reschedule itself.
#
# Arguments:
# cmd1 -        Command to execute when method is called.
# sleep1 -      Ms to sleep after executing cmd1 before executing cmd2.
# cmd2 -        Command to execute in the *next* invocation of this method.
# sleep2 -      Ms to sleep after executing cmd2 before executing cmd1 again.

def textToggle(cmd1,sleep1,cmd2,sleep2)
  sleep_list = [sleep2, sleep1]
  TkAfter.new(proc{sleep = sleep_list.shift; sleep_list.push(sleep); sleep}, 
              -1, cmd1, cmd2).start(sleep1)
end

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

# demo ÍѤΠtoplevel widget ¤òÀ¸À®
$search_demo = TkToplevel.new {|w|
  title("Text Demonstration - Search and Highlight")
  iconname("search")
  positionWindow(w)
}

# frame À¸À®
$search_buttons = TkFrame.new($search_demo) {|frame|
  TkButton.new(frame) {
    #text 'λ²ò'
    text 'ÊĤ¸¤ë'
    command proc{
      tmppath = $search_demo
      $search_demo = nil
      tmppath.destroy
    }
  }.pack('side'=>'left', 'expand'=>'yes')

  TkButton.new(frame) {
    text '¥³¡¼¥É»²¾È'
    command proc{showCode 'search'}
  }.pack('side'=>'left', 'expand'=>'yes')
}
$search_buttons.pack('side'=>'bottom', 'fill'=>'x', 'pady'=>'2m')

# frame À¸À®
TkFrame.new($search_demo) {|f|
  TkLabel.new(f, 'text'=>'¥Õ¥¡¥¤¥ë̾:', 
              'width'=>13, 'anchor'=>'w').pack('side'=>'left')
  $search_fileName = TkVariable.new
  TkEntry.new(f, 'width'=>40, 
              'textvariable'=>$search_fileName) {
    pack('side'=>'left')
    bind('Return', proc{textLoadFile($search_text, $search_fileName.value)
                        $search_string_entry.focus})
    focus
  }
  TkButton.new(f, 'text'=>'Æɤ߹þ¤ß', 
               'command'=>proc{textLoadFile($search_text, 
                                            $search_fileName.value)})\
  .pack('side'=>'left', 'pady'=>5, 'padx'=>10)
}.pack('side'=>'top', 'fill'=>'x')

TkFrame.new($search_demo) {|f|
  TkLabel.new(f, 'text'=>'¸¡º÷ʸ»úÎó:', 
              'width'=>13, 'anchor'=>'w').pack('side'=>'left')
  $search_searchString = TkVariable.new
  $search_string_entry = TkEntry.new(f, 'width'=>40, 
                                     'textvariable'=>$search_searchString) {
    pack('side'=>'left')
    bind('Return', proc{textSearch($search_text, $search_searchString.value, 
                                   $search_Tag)})
  }
  TkButton.new(f, 'text'=>'ȿž', 
               'command'=>proc{textSearch($search_text, 
                                          $search_searchString.value, 
                                          $search_Tag)}) {
    pack('side'=>'left', 'pady'=>5, 'padx'=>10)
  }
}.pack('side'=>'top', 'fill'=>'x')

$search_text = TkText.new($search_demo, 'setgrid'=>true) {|t|
  $search_Tag = TkTextTag.new(t)
  TkScrollbar.new($search_demo, 'command'=>proc{|*args| t.yview(*args)}) {|sc|
    t.yscrollcommand(proc{|first,last| sc.set first,last})
    pack('side'=>'right', 'fill'=>'y')
  }
  pack('expand'=>'yes', 'fill'=>'both')
}  

# Set up display styles for text highlighting.

if TkWinfo.depth($search_demo) > 1
  textToggle(proc{
               $search_Tag.configure('background'=>'#ce5555', 
                                     'foreground'=>'white')
             },
             800, 
             proc{
               $search_Tag.configure('background'=>'', 'foreground'=>'')
             },
             200 )
else
  textToggle(proc{
               $search_Tag.configure('background'=>'black', 
                                     'foreground'=>'white')
             },
             800, 
             proc{
               $search_Tag.configure('background'=>'', 'foreground'=>'')
             },
             200 )
end
$search_text.insert('1.0', "\
¤³¤Î¥¦¥£¥ó¥É¥¦¤Ï¸¡º÷µ¡¹½¤ò¼Â¸½¤¹¤ë¤Î¤Ë¥Æ¥­¥¹¥È widget ¤Î¥¿¥°µ¡Ç½¤¬¤É¤Î \
¤è¤¦¤Ë»È¤ï¤ì¤ë¤Î¤«¤ò¥Ç¥â¤¹¤ë¤â¤Î¤Ç¤¹¡£¤Þ¤º¾å¤Î¥¨¥ó¥È¥ê¤Ë¥Õ¥¡¥¤¥ë̾¤òÆþ \
¤ì¡¢<¥ê¥¿¡¼¥ó> ¤ò²¡¤¹¤«¡Ö¥í¡¼¥É¡×¥Ü¥¿¥ó¤ò²¡¤·¤Æ¤¯¤À¤µ¤¤¡£¼¡¤Ë¤½¤Î²¼¤Î \
¥¨¥ó¥È¥ê¤Ëʸ»úÎó¤òÆþÎϤ·¡¢<¥ê¥¿¡¼¥ó> ¤ò²¡¤¹¤«¡Öȿž¡×¥Ü¥¿¥ó¤ò²¡¤·¤Æ¤¯ \
¤À¤µ¤¤¡£¤¹¤ë¤È¥Õ¥¡¥¤¥ëÃæ¤Î¡¢¸¡º÷ʸ»úÎó¤È°ìÃפ¹¤ëÉôʬ¤ËÁ´¤Æ \"search_Tag\" \
¤È¤¤¤¦¥¿¥°¤¬¤Ä¤±¤é¤ì¡¢¥¿¥°¤Îɽ¼¨Â°À­¤È¤·¤Æ¤½¤Îʸ»úÎó¤¬ÅÀÌǤ¹¤ë¤è¤¦¤Ë \
ÀßÄꤵ¤ì¤Þ¤¹¡£")
$search_text.set_insert '0.0'

$search_fileName.value = ''
$search_searchString.value = ''