~ubuntu-branches/ubuntu/hardy/ruby1.8/hardy-updates

« back to all changes in this revision

Viewing changes to ext/tk/sample/tktree.rb

  • Committer: Bazaar Package Importer
  • Author(s): akira yamada
  • Date: 2007-03-13 22:11:58 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20070313221158-h3oql37brlaf2go2
Tags: 1.8.6-1
* new upstream version, 1.8.6.
* libruby1.8 conflicts with libopenssl-ruby1.8 (< 1.8.6) (closes: #410018)
* changed packaging style to cdbs from dbs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##########################################################################
 
2
# TkTree widget class
 
3
#
 
4
#    see <http://wiki.tcl.tk/10615>
 
5
#
 
6
#  Note:  optional argument '-font' of the Tcl library is changed to 
 
7
#         'itemfont' on this Ruby library, because of avoiding font 
 
8
#         operation trouble in 'initialize' method  ( see the following 
 
9
#         test script ). 
 
10
#
 
11
##########################################################################
 
12
require 'tk'
 
13
 
 
14
class TkTree < TkCanvas
 
15
  TCL_SCRIPT_PATH = File.join(File.dirname(__FILE__), 'tktree.tcl')
 
16
 
 
17
  def create_self(keys)
 
18
    args = [@path]
 
19
    if keys.kind_of?(Hash)
 
20
      font = keys.delete('itemfont')
 
21
#      font = hash_kv(font) if font.kind_of?(Hash)
 
22
      keys['font'] = font if font
 
23
#      args.concat(hash_kv(keys))
 
24
      args << keys
 
25
    end
 
26
    begin
 
27
      tk_call('::tktree::treecreate', *args)
 
28
    rescue NameError, RuntimeError
 
29
      Tk.load_tclscript(TkTree::TCL_SCRIPT_PATH)
 
30
      tk_call('::tktree::treecreate', *args)
 
31
    end
 
32
  end
 
33
 
 
34
  def newitem(itempath, keys = nil)
 
35
    if keys.kind_of?(Hash)
 
36
      keys = _symbolkey2str(keys)
 
37
      font = keys.delete('itemfont')
 
38
#      font = hash_kv(font) if font.kind_of?(Hash)
 
39
      keys['font'] = font if font
 
40
#      tk_call('::tktree::newitem', @path, itempath, *hash_kv(keys))
 
41
      tk_call('::tktree::newitem', @path, itempath, keys)
 
42
    else
 
43
      tk_call('::tktree::newitem', @path, itempath)
 
44
    end
 
45
  end
 
46
 
 
47
  def delitem(itempath)
 
48
    tk_call('::tktree::delitem', @path, itempath)
 
49
  end
 
50
 
 
51
  def labelat(xpos, ypos)
 
52
    tk_call('::tktree::delitem', @path, xpos, ypos)
 
53
  end
 
54
 
 
55
  def getselection
 
56
    tk_call('::tktree::getselection', @path)
 
57
  end
 
58
 
 
59
  def setselection(itempath)
 
60
    tk_call('::tktree::getselection', @path, itempath)
 
61
  end
 
62
end
 
63
 
 
64
 
 
65
##########################################################################
 
66
# test script
 
67
##########################################################################
 
68
if __FILE__ == $0
 
69
  TkLabel.new(:text=><<EOL, :relief=>:ridge, :justify=>:left).pack
 
70
 
 
71
 This is a sample to use a Tcl library script on Ruby/Tk. 
 
72
 This sample loads tktree.tcl (see <http://wiki.tcl.tk/10615>) 
 
73
 and calls functions of the Tcl script. 
 
74
EOL
 
75
 
 
76
  items = %w(/group1/item1 /group1/item2 /group1/subgroup/item1 /group2/item1 /item1)
 
77
 
 
78
  tr1 = TkTree.new.pack(:expand=>true, :fill=>:both)
 
79
  tr1.focus
 
80
 
 
81
  items.each{|item|
 
82
    tr1.newitem(item, 
 
83
                :command=>proc{Tk.messageBox(:message=>"#{item} executed")})
 
84
  }
 
85
 
 
86
  f = TkFrame.new.pack(:expand=>true, :fill=>:both)
 
87
  tr2 = TkTree.new(f, :bg=>'black', #:itemfont=>{:family=>'Times', :size=>14}, 
 
88
                   :textcolor=>'red', :bd=>4, :relief=>:ridge, 
 
89
                   :selectbackground=>'darkBlue', :selectforeground=>'yellow', 
 
90
                   :selectborderwidth=>3, :linecolor=>'yellow') {
 
91
    yscrollbar(TkScrollbar.new(f, :width=>10).pack(:side=>:right, :fill=>:y))
 
92
    xscrollbar(TkScrollbar.new(f, :width=>10).pack(:side=>:bottom, :fill=>:x))
 
93
    pack(:expand=>true, :fill=>:both)
 
94
  }
 
95
 
 
96
  items.each{|item|
 
97
    tr2.newitem(item, :textcolor=>'green', :image=>'', 
 
98
                :itemfont=>{:family=>'Times', :size=>10}, 
 
99
                :command=>proc{Tk.messageBox(:message=>"#{item} executed")})
 
100
  }
 
101
 
 
102
  Tk.mainloop
 
103
end