1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/ext/tk/sample/demos-en/search.rb
nagai e6697a6405 * ext/tk/tcltklib.c: SEGV when tcltk-stubs is enabled.
* ext/tk/tcltklib.c: avoid error on a shared object.

* ext/tk/extconf.rb: support --with-tcltkversion

* ext/tk/README.tcltklib: add document about --with-tcltkversion

* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb, ext/tk/lib/remote-tk.rb: 
  not work on $SAFE==4

* ext/tk/lib/multi-tk.rb: Object#methods returns Symbols on Ruby1.9.

* ext/tk/lib/tk/timer.rb: add TkTimer#at_end(proc) to register the 
  procedure which called at end of the timer.

* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemfont.rb, ext/tk/lib/font.rb: 
  support __IGNORE_UNKNOWN_CONFIGURE_OPTION__ about font options.

* ext/tk/lib/*: treat __IGNORE_UNKNOWN_CONFIGURE_OPTION__

* ext/tk/lib/tkextlib/iwidgets/scrolledcanvas.rb, 
  ext/tk/lib/tkextlib/iwidgets/scrolledlistbox.rb, 
  ext/tk/lib/tkextlib/iwidgets/scrolledtext.rb: bug fix. 

* ext/tk/lib/tk/text.rb: typo. call a wrong method.

* ext/tk/lib/tk/itemconfig.rb: ditto.

* ext/tk/lib/tk.rb, ext/tk/lib/tk/itemconfig.rb, 
  ext/tk/lib/tk/canvas.rb: support alias names of option keys.

* ext/tk/lib/tk/grid.rb: lack of module-method definitions.

* ext/tk/lib/tk/pack.rb, ext/tk/lib/tk/grid.rb: increase supported 
  parameter patterns of configure method.

* ext/tk/lib/tk.rb: add TkWindow#grid_anchor, grid_column, grid_row.

* ext/tk/lib/tk/wm.rb: methods of Tk::Wm_for_General module cannot
  pass the given block to methods of Tk::Wm module.

* ext/tk/lib/tk/wm.rb: Wm#overrideredirect overwrites arguemnt to 
  an invalid value.

* ext/tk/lib/tk.rb: fix memory (object) leak bug.

* ext/tk/tcltklib.c, ext/tk/tkutil/tkutil.c: fix memory leak.

* ext/tk/sample/demos-jp/aniwave.rb, ext/tk/sample/demos-en/aniwave.rb:
  bug fix.

* ext/tk/lib/tkextlib/blt/component.rb, 
  ext/tk/lib/tkextlib/tile/tentry.rb, 
  ext/tk/lib/tkextlib/tile/treeview.rb: ditto.

* ext/tk/lib/tkextlib/tile/tpaned.rb: improve TPaned#add.

* ext/tk/sample/demos-jp/widget, ext/tk/sample/demos-en/widget, 
  ext/tk/sample/demos-jp/style.rb, ext/tk/sample/demos-en/style.rb, 
  ext/tk/sample/demos-jp/bind.rb, ext/tk/sample/demos-en/bind.rb: 
  bug fix.

* ext/tk/sample/ttk_wrapper.rb: ditto.

* ext/tk/sample/ttk_wrapper.rb: support "if __FILE__ == $0" idiom.

* ext/tk/sample/tktextio.rb: add binding for 'Ctrl-u' at console mode.

* ext/tk/lib/tkextlib/tile.rb, ext/tk/lib/tkextlib/tile/style.rb, 
  ext/tk/sample/ttk_wrapper.rb: improve treating and control themes. 
  add Tk::Tile.themes and Tk::Tile.set_theme(theme).

* ext/tk/lib/tkextlib/tile.rb: lack of autoload definitions.

* ext/tk/lib/tkextlib/tile/tnotebook.rb: cannot use kanji (not UTF-8) 
  characters for headings.

* ext/tk/lib/tkextlib/tkDND/shape.rb: wrong package name.

* ext/tk/tkutil/tkutil.c: improve handling callback-subst-keys. 
  Now, support longnam-keys (e.g. '%CTT' on tkdnd-2.0; however, still
  not support tkdnd-2.0 on tkextlib), and symbols of parameters (e.g. 
  :widget=>'%W', :keycode=>'%k', '%x'=>:x, '%X'=>:root_x, and so on; 
  those are attributes of event object). It means that Ruby/Tk accepts
  not only "widget.bind(ev, '%W', '%k', ...){|w, k, ...| ... }", but 
  also "widget.bind(ev, :widget, :keycode, ...){|w, k, ...| ... }". 
  It is potentially incompatible, when user passes symbols to the
  arguments of the callback block (the block receives the symbols as
  strings). I think that is very rare case (probably, used by Ruby/Tk
  experts only). When causes such trouble, please give strings instead
  of such symbol parameters (e.g. call Symbol#to_s method).

* ext/tk/lib/tk/event.rb, ext/tk/lib/tk/validation.rb, 
  ext/tk/lib/tkextlib/blt/treeview.rb, 
  ext/tk/lib/tkextlib/winico/winico.rb: ditto.

* ext/tk/tkutil/tkutil.c: strings are available on subst_tables on 
  TkUtil::CallbackSubst class (it is useful on Ruby 1.9). 

* ext/tk/lib/tk/spinbox.rb, ext/tk/lib/tkextlib/iwidgets/hierarchy.rb, 
  ext/tk/lib/tkextlib/iwidgets/spinner.rb, 
  ext/tk/lib/tkextlib/iwidgets/entryfield.rb, 
  ext/tk/lib/tkextlib/iwidgets/calendar.rb, 
  ext/tk/lib/tkextlib/blt/dragdrop.rb, 
  ext/tk/lib/tkextlib/tkDND/tkdnd.rb, 
  ext/tk/lib/tkextlib/treectrl/tktreectrl.rb, 
  ext/tk/lib/tkextlib/tktable/tktable.rb: disable code piece became 
  unnecessary by reason of the changes of ext/tk/tkutil/tkutil.c.

* ext/tk/lib/tk.rb, ext/tk/lib/multi-tk.rb: change strategy to define
  the constant WITH_ENCODING.

* ext/tk/lib/tk.rb: fix bug on Tk::Encoding.tk_encoding_names.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@17083 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2008-06-10 20:59:10 +00:00

187 lines
6 KiB
Ruby

# 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:
# 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)
}
base_frame = TkFrame.new($search_demo).pack(:fill=>:both, :expand=>true)
# frame
$search_buttons = TkFrame.new(base_frame) {|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(base_frame) {|f|
TkLabel.new(f, 'text'=>'File name:',
'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'=>'Load File',
'command'=>proc{textLoadFile($search_text,
$search_fileName.value)})\
.pack('side'=>'left', 'pady'=>5, 'padx'=>10)
}.pack('side'=>'top', 'fill'=>'x')
TkFrame.new(base_frame) {|f|
TkLabel.new(f, 'text'=>'Search string:',
'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'=>'Highlight',
'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(base_frame, 'setgrid'=>true, 'wrap'=>'word') {|t|
$search_Tag = TkTextTag.new(t)
TkScrollbar.new(base_frame, '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', "\
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.")
$search_text.insert('end', "\
The current directory to load a file is \"#{Dir.pwd}\".\
")
$search_text.set_insert '0.0'
$search_fileName.value = ''
$search_searchString.value = ''
$search_text.width = 60
$search_text.height = 20