ruby/ext/tk/sample/tktree.rb
nagai 41b74c6e20 * ext/tk/extconf.rb: New strategy for searching Tcl/Tk libraries.
* ext/tk/*: Support new features of Tcl/Tk8.6b1 and minor bug fixes.
     ( [KNOWN BUG] Ruby/Tk on Ruby 1.9 will not work on Cygwin. )
* ext/tk/*: Unify sources between Ruby 1.8 & 1.9.
            Improve default_widget_set handling.
* ext/tk/*: Multi-TkInterpreter (multi-tk.rb) works on Ruby 1.8 & 1.9.
     ( [KNOWN BUG] On Ruby 1.8, join to a long term Thread on Tk
       callbacks may freeze. On Ruby 1.9, cannot create a second 
       master interpreter (creating slaves are OK); supported master
       interpreter is the default master interpreter only. )
* ext/tk/lib/tkextlib/*: Update supported versions of Tk extensions.
         Tcllib 1.8/Tklib 0.4.1  ==>  Tcllib 1.11.1/Tklib 0.5
         BWidgets 1.7            ==>  BWidgets 1.8
         TkTable 2.9             ==>  TkTable 2.10
         TkTreeCtrl 2005-12-02   ==>  TkTreeCtrl 2.2.9
         Tile 0.8.0/8.5.1        ==>  Tile 0.8.3/8.6b1
         IncrTcl 2005-02-14      ==>  IncrTcl 2008-12-15
         TclX 2005-02-07         ==>  TclX 2008-12-15
         Trofs 0.4.3             ==>  Trofs 0.4.4


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@24064 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-07-12 23:09:52 +00:00

103 lines
3.2 KiB
Ruby

##########################################################################
# TkTree widget class
#
# see <http://wiki.tcl.tk/10615>
#
# Note: optional argument '-font' of the Tcl library is changed to
# 'itemfont' on this Ruby library, because of avoiding font
# operation trouble in 'initialize' method ( see the following
# test script ).
#
##########################################################################
require 'tk'
class TkTree < TkCanvas
TCL_SCRIPT_PATH = File.join(File.dirname(__FILE__), 'tktree.tcl')
def create_self(keys)
args = [@path]
if keys.kind_of?(Hash)
font = keys.delete('itemfont')
# font = hash_kv(font) if font.kind_of?(Hash)
keys['font'] = font if font
# args.concat(hash_kv(keys))
args << keys
end
begin
tk_call('::tktree::treecreate', *args)
rescue NameError, RuntimeError
Tk.load_tclscript(TkTree::TCL_SCRIPT_PATH)
tk_call('::tktree::treecreate', *args)
end
end
def newitem(itempath, keys = nil)
if keys.kind_of?(Hash)
keys = _symbolkey2str(keys)
font = keys.delete('itemfont')
# font = hash_kv(font) if font.kind_of?(Hash)
keys['font'] = font if font
# tk_call('::tktree::newitem', @path, itempath, *hash_kv(keys))
tk_call('::tktree::newitem', @path, itempath, keys)
else
tk_call('::tktree::newitem', @path, itempath)
end
end
def delitem(itempath)
tk_call('::tktree::delitem', @path, itempath)
end
def labelat(xpos, ypos)
tk_call('::tktree::delitem', @path, xpos, ypos)
end
def getselection
tk_call('::tktree::getselection', @path)
end
def setselection(itempath)
tk_call('::tktree::getselection', @path, itempath)
end
end
##########################################################################
# test script
##########################################################################
if __FILE__ == $0
TkLabel.new(:text=><<EOL, :relief=>:ridge, :justify=>:left).pack
This is a sample to use a Tcl library script on Ruby/Tk.
This sample loads tktree.tcl (see <http://wiki.tcl.tk/10615>)
and calls functions of the Tcl script.
EOL
items = %w(/group1/item1 /group1/item2 /group1/subgroup/item1 /group2/item1 /item1)
tr1 = TkTree.new.pack(:expand=>true, :fill=>:both)
tr1.focus
items.each{|item|
tr1.newitem(item,
:command=>proc{Tk.messageBox(:message=>"#{item} executed")})
}
f = TkFrame.new.pack(:expand=>true, :fill=>:both)
tr2 = TkTree.new(f, :bg=>'black', #:itemfont=>{:family=>'Times', :size=>14},
:textcolor=>'red', :bd=>4, :relief=>:ridge,
:selectbackground=>'darkBlue', :selectforeground=>'yellow',
:selectborderwidth=>3, :linecolor=>'yellow') {
yscrollbar(TkScrollbar.new(f, :width=>10).pack(:side=>:right, :fill=>:y))
xscrollbar(TkScrollbar.new(f, :width=>10).pack(:side=>:bottom, :fill=>:x))
pack(:expand=>true, :fill=>:both)
}
items.each{|item|
tr2.newitem(item, :textcolor=>'green', :image=>'',
:itemfont=>{:family=>'Times', :size=>10},
:command=>proc{Tk.messageBox(:message=>"#{item} executed")})
}
Tk.mainloop
end