2003-07-31 16:52:40 -04:00
|
|
|
# search.rb
|
|
|
|
#
|
|
|
|
# This demonstration script creates a collection of widgets that
|
|
|
|
# allow you to load a file into a text widget, then perform searches
|
|
|
|
# on that file.
|
|
|
|
#
|
|
|
|
# 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:
|
2004-10-11 00:51:21 -04:00
|
|
|
# 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.
|
2003-07-31 16:52:40 -04:00
|
|
|
|
|
|
|
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:
|
2004-10-11 00:51:21 -04:00
|
|
|
# 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.
|
2003-07-31 16:52:40 -04:00
|
|
|
|
|
|
|
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:
|
2004-10-11 00:51:21 -04:00
|
|
|
# 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.
|
2003-07-31 16:52:40 -04:00
|
|
|
|
|
|
|
def textToggle(cmd1,sleep1,cmd2,sleep2)
|
|
|
|
sleep_list = [sleep2, sleep1]
|
|
|
|
TkAfter.new(proc{sleep = sleep_list.shift; sleep_list.push(sleep); sleep},
|
2004-10-11 00:51:21 -04:00
|
|
|
-1, cmd1, cmd2).start(sleep1)
|
2003-07-31 16:52:40 -04:00
|
|
|
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 'Dismiss'
|
|
|
|
command proc{
|
|
|
|
tmppath = $search_demo
|
|
|
|
$search_demo = nil
|
|
|
|
tmppath.destroy
|
|
|
|
}
|
|
|
|
}.pack('side'=>'left', 'expand'=>'yes')
|
|
|
|
|
|
|
|
TkButton.new(frame) {
|
|
|
|
text 'Show Code'
|
|
|
|
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'=>'File name:',
|
2004-10-11 00:51:21 -04:00
|
|
|
'width'=>13, 'anchor'=>'w').pack('side'=>'left')
|
2003-07-31 16:52:40 -04:00
|
|
|
$search_fileName = TkVariable.new
|
|
|
|
TkEntry.new(f, 'width'=>40,
|
2004-10-11 00:51:21 -04:00
|
|
|
'textvariable'=>$search_fileName) {
|
2003-07-31 16:52:40 -04:00
|
|
|
pack('side'=>'left')
|
|
|
|
bind('Return', proc{textLoadFile($search_text, $search_fileName.value)
|
2004-10-11 00:51:21 -04:00
|
|
|
$search_string_entry.focus})
|
2003-07-31 16:52:40 -04:00
|
|
|
focus
|
|
|
|
}
|
|
|
|
TkButton.new(f, 'text'=>'Load File',
|
2004-10-11 00:51:21 -04:00
|
|
|
'command'=>proc{textLoadFile($search_text,
|
|
|
|
$search_fileName.value)})\
|
2003-07-31 16:52:40 -04:00
|
|
|
.pack('side'=>'left', 'pady'=>5, 'padx'=>10)
|
|
|
|
}.pack('side'=>'top', 'fill'=>'x')
|
|
|
|
|
|
|
|
TkFrame.new($search_demo) {|f|
|
|
|
|
TkLabel.new(f, 'text'=>'Search string:',
|
2004-10-11 00:51:21 -04:00
|
|
|
'width'=>13, 'anchor'=>'w').pack('side'=>'left')
|
2003-07-31 16:52:40 -04:00
|
|
|
$search_searchString = TkVariable.new
|
|
|
|
$search_string_entry = TkEntry.new(f, 'width'=>40,
|
2004-10-11 00:51:21 -04:00
|
|
|
'textvariable'=>$search_searchString) {
|
2003-07-31 16:52:40 -04:00
|
|
|
pack('side'=>'left')
|
|
|
|
bind('Return', proc{textSearch($search_text, $search_searchString.value,
|
2004-10-11 00:51:21 -04:00
|
|
|
$search_Tag)})
|
2003-07-31 16:52:40 -04:00
|
|
|
}
|
|
|
|
TkButton.new(f, 'text'=>'Highlight',
|
2004-10-11 00:51:21 -04:00
|
|
|
'command'=>proc{textSearch($search_text,
|
|
|
|
$search_searchString.value,
|
|
|
|
$search_Tag)}) {
|
2003-07-31 16:52:40 -04:00
|
|
|
pack('side'=>'left', 'pady'=>5, 'padx'=>10)
|
|
|
|
}
|
|
|
|
}.pack('side'=>'top', 'fill'=>'x')
|
|
|
|
|
2004-05-01 12:09:54 -04:00
|
|
|
$search_text = TkText.new($search_demo, 'setgrid'=>true, 'wrap'=>'word') {|t|
|
2003-07-31 16:52:40 -04:00
|
|
|
$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')
|
* ext/tk/*: full update Ruby/Tk to support Ruby(1.9|1.8) and Tc/Tk8.5.
* ext/tk/lib/tkextlib/tile.rb: [incompatible] remove TileWidgets'
instate/state/identify method to avoid the conflict with standard
widget options. Those methods are renamed to ttk_instate/ttk_state/
ttk_identify (tile_instate/tile_state/tile_identify are available
too). Although I don't recommend, if you realy need old methods,
please define "Tk::USE_OBSOLETE_TILE_STATE_METHOD = true" before
"require 'tkextlib/tile'".
* ext/tk/lib/tkextlib/tile.rb: "Tk::Tile::__Import_Tile_Widgets__!"
is obsolete. It outputs warning. To control default widget set,
use "Tk.default_widget_set = :Ttk".
* ext/tk/lib/tk.rb: __IGNORE_UNKNOWN_CONFIGURE_OPTION__ method and
__set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!(mode) method are defind
as module methods of TkConfigMethod. It may help users to wrap old
Ruby/Tk scripts (use standard widgets) to force to use Ttk widgets.
Ttk widgets don't have some options of standard widgets which are
control the view of widgets. When set ignore-mode true, configure
method tries to ignoure such unknown options with no exception.
Of course, it may raise other troubles on the GUI design.
So, those are a little danger methods.
* ext/tk/lib/tk/itemconfig.rb: __IGNORE_UNKNOWN_CONFIGURE_OPTION__
method and __set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!(mode) method
are defind as module methods of TkItemConfigMethod as the same
purpose as TkConfigMethod's ones.
* ext/tk/sample/ttk_wrapper.rb: A new example. This is a tool for
wrapping old Ruby/Tk scripts (which use standard widgets) to use
Ttk (Tile) widgets as default.
* ext/tk/sample/tkextlib/tile/demo.rb: use ttk_instate/ttk_state
method instead of instate/state method.
* ext/tk/lib/tk/root, ext/tk/lib/tk/namespace.rb,
ext/tk/lib/tk/text.rb, ext/tk/lib/tkextlib/*: some 'instance_eval's
are replaced to "instance_exec(self)".
* ext/tk/lib/tk/event.rb: bug fix on KEY_TBL and PROC_TBL (?x is not
a character code on Ruby1.9).
* ext/tk/lib/tk/variable.rb: support new style of operation argument
on Tcl/Tk's 'trace' command for variables.
* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget: bug fix
* ext/tk/sammple/demos-jp/textpeer.rb,
ext/tk/sammple/demos-en/textpeer.rb: new widget demo.
* ext/tk/tcltklib.c: decrase SEGV troubles (probably)
* ext/tk/lib/tk.rb: remove Thread.critical access if Ruby1.9
* ext/tk/lib/tk/multi-tk.rb: support Ruby1.9 (probably)
* ext/tk/lib/tkextlib/tile.rb: add method to define Tcl/Tk command
to make Tcl/Tk theme sources (based on different version of Tile
extension) available.
(Tk::Tile::__define_LoadImages_proc_for_comaptibility__)
* ext/tk/lib/tk.rb, ext/tk/lib/tk/wm.rb: support dockable frames
(Tcl/Tk8.5 feature). 'wm' command can treat many kinds of widgets
as toplevel widgets.
* ext/tk/lib/tkextlib/tile/style.rb: ditto.
(Tk::Tile::Style.__define_wrapper_proc_for_compatibility__)
* ext/tk/lib/tk/font.rb: add actual_hash and metrics_hash to get
properties as a hash. metrics_hash method returns a boolean value
for 'fixed' option. But metrics method returns numeric value
(0 or 1) for 'fixed' option, because of backward compatibility.
* ext/tk/lib/tk/timer.rb: somtimes fail to set callback procedure.
* ext/tk/lib/tk.rb: add Tk.sleep and Tk.wakeup method. Tk.sleep
doesn't block the eventloop. It will be better to use the method
in event callbacks.
* ext/tk/sample/tksleep_sample.rb: sample script about Tk.sleep.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15848 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-03-29 01:25:12 -04:00
|
|
|
}
|
2003-07-31 16:52:40 -04:00
|
|
|
|
|
|
|
# Set up display styles for text highlighting.
|
|
|
|
|
|
|
|
if TkWinfo.depth($search_demo) > 1
|
|
|
|
textToggle(proc{
|
2004-10-11 00:51:21 -04:00
|
|
|
$search_Tag.configure('background'=>'#ce5555',
|
|
|
|
'foreground'=>'white')
|
|
|
|
},
|
|
|
|
800,
|
|
|
|
proc{
|
|
|
|
$search_Tag.configure('background'=>'', 'foreground'=>'')
|
|
|
|
},
|
|
|
|
200 )
|
2003-07-31 16:52:40 -04:00
|
|
|
else
|
|
|
|
textToggle(proc{
|
2004-10-11 00:51:21 -04:00
|
|
|
$search_Tag.configure('background'=>'black',
|
|
|
|
'foreground'=>'white')
|
|
|
|
},
|
|
|
|
800,
|
|
|
|
proc{
|
|
|
|
$search_Tag.configure('background'=>'', 'foreground'=>'')
|
|
|
|
},
|
|
|
|
200 )
|
2003-07-31 16:52:40 -04:00
|
|
|
end
|
2004-05-01 12:09:54 -04:00
|
|
|
$search_text.insert('1.0', "\
|
|
|
|
This window demonstrates how to use the tagging facilities in text \
|
|
|
|
widgets to implement a searching mechanism. First, type a file name \
|
|
|
|
in the top entry, then type <Return> or click on \"Load File\". Then \
|
|
|
|
type a string in the lower entry and type <Return> or click on \
|
|
|
|
\"Load File\". This will cause all of the instances of the string to \
|
|
|
|
be tagged with the tag \"search\", and it will arrange for the tag\'s \
|
|
|
|
display attributes to change to make all of the strings blink.")
|
* ext/tk/*: full update Ruby/Tk to support Ruby(1.9|1.8) and Tc/Tk8.5.
* ext/tk/lib/tkextlib/tile.rb: [incompatible] remove TileWidgets'
instate/state/identify method to avoid the conflict with standard
widget options. Those methods are renamed to ttk_instate/ttk_state/
ttk_identify (tile_instate/tile_state/tile_identify are available
too). Although I don't recommend, if you realy need old methods,
please define "Tk::USE_OBSOLETE_TILE_STATE_METHOD = true" before
"require 'tkextlib/tile'".
* ext/tk/lib/tkextlib/tile.rb: "Tk::Tile::__Import_Tile_Widgets__!"
is obsolete. It outputs warning. To control default widget set,
use "Tk.default_widget_set = :Ttk".
* ext/tk/lib/tk.rb: __IGNORE_UNKNOWN_CONFIGURE_OPTION__ method and
__set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!(mode) method are defind
as module methods of TkConfigMethod. It may help users to wrap old
Ruby/Tk scripts (use standard widgets) to force to use Ttk widgets.
Ttk widgets don't have some options of standard widgets which are
control the view of widgets. When set ignore-mode true, configure
method tries to ignoure such unknown options with no exception.
Of course, it may raise other troubles on the GUI design.
So, those are a little danger methods.
* ext/tk/lib/tk/itemconfig.rb: __IGNORE_UNKNOWN_CONFIGURE_OPTION__
method and __set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!(mode) method
are defind as module methods of TkItemConfigMethod as the same
purpose as TkConfigMethod's ones.
* ext/tk/sample/ttk_wrapper.rb: A new example. This is a tool for
wrapping old Ruby/Tk scripts (which use standard widgets) to use
Ttk (Tile) widgets as default.
* ext/tk/sample/tkextlib/tile/demo.rb: use ttk_instate/ttk_state
method instead of instate/state method.
* ext/tk/lib/tk/root, ext/tk/lib/tk/namespace.rb,
ext/tk/lib/tk/text.rb, ext/tk/lib/tkextlib/*: some 'instance_eval's
are replaced to "instance_exec(self)".
* ext/tk/lib/tk/event.rb: bug fix on KEY_TBL and PROC_TBL (?x is not
a character code on Ruby1.9).
* ext/tk/lib/tk/variable.rb: support new style of operation argument
on Tcl/Tk's 'trace' command for variables.
* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget: bug fix
* ext/tk/sammple/demos-jp/textpeer.rb,
ext/tk/sammple/demos-en/textpeer.rb: new widget demo.
* ext/tk/tcltklib.c: decrase SEGV troubles (probably)
* ext/tk/lib/tk.rb: remove Thread.critical access if Ruby1.9
* ext/tk/lib/tk/multi-tk.rb: support Ruby1.9 (probably)
* ext/tk/lib/tkextlib/tile.rb: add method to define Tcl/Tk command
to make Tcl/Tk theme sources (based on different version of Tile
extension) available.
(Tk::Tile::__define_LoadImages_proc_for_comaptibility__)
* ext/tk/lib/tk.rb, ext/tk/lib/tk/wm.rb: support dockable frames
(Tcl/Tk8.5 feature). 'wm' command can treat many kinds of widgets
as toplevel widgets.
* ext/tk/lib/tkextlib/tile/style.rb: ditto.
(Tk::Tile::Style.__define_wrapper_proc_for_compatibility__)
* ext/tk/lib/tk/font.rb: add actual_hash and metrics_hash to get
properties as a hash. metrics_hash method returns a boolean value
for 'fixed' option. But metrics method returns numeric value
(0 or 1) for 'fixed' option, because of backward compatibility.
* ext/tk/lib/tk/timer.rb: somtimes fail to set callback procedure.
* ext/tk/lib/tk.rb: add Tk.sleep and Tk.wakeup method. Tk.sleep
doesn't block the eventloop. It will be better to use the method
in event callbacks.
* ext/tk/sample/tksleep_sample.rb: sample script about Tk.sleep.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15848 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-03-29 01:25:12 -04:00
|
|
|
$search_text.insert('end', "\
|
|
|
|
The current directory to load a file is \"#{Dir.pwd}\".\
|
|
|
|
")
|
2003-07-31 16:52:40 -04:00
|
|
|
$search_text.set_insert '0.0'
|
|
|
|
|
|
|
|
$search_fileName.value = ''
|
|
|
|
$search_searchString.value = ''
|
|
|
|
|
* ext/tk/*: full update Ruby/Tk to support Ruby(1.9|1.8) and Tc/Tk8.5.
* ext/tk/lib/tkextlib/tile.rb: [incompatible] remove TileWidgets'
instate/state/identify method to avoid the conflict with standard
widget options. Those methods are renamed to ttk_instate/ttk_state/
ttk_identify (tile_instate/tile_state/tile_identify are available
too). Although I don't recommend, if you realy need old methods,
please define "Tk::USE_OBSOLETE_TILE_STATE_METHOD = true" before
"require 'tkextlib/tile'".
* ext/tk/lib/tkextlib/tile.rb: "Tk::Tile::__Import_Tile_Widgets__!"
is obsolete. It outputs warning. To control default widget set,
use "Tk.default_widget_set = :Ttk".
* ext/tk/lib/tk.rb: __IGNORE_UNKNOWN_CONFIGURE_OPTION__ method and
__set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!(mode) method are defind
as module methods of TkConfigMethod. It may help users to wrap old
Ruby/Tk scripts (use standard widgets) to force to use Ttk widgets.
Ttk widgets don't have some options of standard widgets which are
control the view of widgets. When set ignore-mode true, configure
method tries to ignoure such unknown options with no exception.
Of course, it may raise other troubles on the GUI design.
So, those are a little danger methods.
* ext/tk/lib/tk/itemconfig.rb: __IGNORE_UNKNOWN_CONFIGURE_OPTION__
method and __set_IGNORE_UNKNOWN_CONFIGURE_OPTION__!(mode) method
are defind as module methods of TkItemConfigMethod as the same
purpose as TkConfigMethod's ones.
* ext/tk/sample/ttk_wrapper.rb: A new example. This is a tool for
wrapping old Ruby/Tk scripts (which use standard widgets) to use
Ttk (Tile) widgets as default.
* ext/tk/sample/tkextlib/tile/demo.rb: use ttk_instate/ttk_state
method instead of instate/state method.
* ext/tk/lib/tk/root, ext/tk/lib/tk/namespace.rb,
ext/tk/lib/tk/text.rb, ext/tk/lib/tkextlib/*: some 'instance_eval's
are replaced to "instance_exec(self)".
* ext/tk/lib/tk/event.rb: bug fix on KEY_TBL and PROC_TBL (?x is not
a character code on Ruby1.9).
* ext/tk/lib/tk/variable.rb: support new style of operation argument
on Tcl/Tk's 'trace' command for variables.
* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget: bug fix
* ext/tk/sammple/demos-jp/textpeer.rb,
ext/tk/sammple/demos-en/textpeer.rb: new widget demo.
* ext/tk/tcltklib.c: decrase SEGV troubles (probably)
* ext/tk/lib/tk.rb: remove Thread.critical access if Ruby1.9
* ext/tk/lib/tk/multi-tk.rb: support Ruby1.9 (probably)
* ext/tk/lib/tkextlib/tile.rb: add method to define Tcl/Tk command
to make Tcl/Tk theme sources (based on different version of Tile
extension) available.
(Tk::Tile::__define_LoadImages_proc_for_comaptibility__)
* ext/tk/lib/tk.rb, ext/tk/lib/tk/wm.rb: support dockable frames
(Tcl/Tk8.5 feature). 'wm' command can treat many kinds of widgets
as toplevel widgets.
* ext/tk/lib/tkextlib/tile/style.rb: ditto.
(Tk::Tile::Style.__define_wrapper_proc_for_compatibility__)
* ext/tk/lib/tk/font.rb: add actual_hash and metrics_hash to get
properties as a hash. metrics_hash method returns a boolean value
for 'fixed' option. But metrics method returns numeric value
(0 or 1) for 'fixed' option, because of backward compatibility.
* ext/tk/lib/tk/timer.rb: somtimes fail to set callback procedure.
* ext/tk/lib/tk.rb: add Tk.sleep and Tk.wakeup method. Tk.sleep
doesn't block the eventloop. It will be better to use the method
in event callbacks.
* ext/tk/sample/tksleep_sample.rb: sample script about Tk.sleep.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@15848 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-03-29 01:25:12 -04:00
|
|
|
$search_text.width = 60
|
|
|
|
$search_text.height = 20
|