1#
2#  tkextlib/tcllib/toolbar.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5#   * Part of tcllib extension
6#   * toolbar widget
7#
8
9require 'tk'
10require 'tkextlib/tcllib.rb'
11
12# TkPackage.require('widget::toolbar', '1.2')
13TkPackage.require('widget::toolbar')
14
15module Tk::Tcllib
16  module Widget
17    class Toolbar < TkWindow
18      PACKAGE_NAME = 'widget::toolbar'.freeze
19      def self.package_name
20        PACKAGE_NAME
21      end
22
23      def self.package_version
24        begin
25          TkPackage.require('widget::toolbar')
26        rescue
27          ''
28        end
29      end
30    end
31
32    module ToolbarItemConfig
33      include TkItemConfigMethod
34    end
35  end
36end
37
38
39class Tk::Tcllib::Widget::ToolbarItem < TkObject
40  include TkTreatTagFont
41
42  ToolbarItemID_TBL = TkCore::INTERP.create_table
43
44  TkCore::INTERP.init_ip_env{
45    TTagID_TBL.mutex.synchronize{ TTagID_TBL.clear }
46  }
47
48  def ToolbarItem.id2obj(tbar, id)
49    tpath = tbar.path
50    ToolbarItemID_TBL.mutex.synchronize{
51      if ToolbarItemID_TBL[tpath]
52        ToolbarItemID_TBL[tpath][id]? ToolbarItemID_TBL[tpath][id]: id
53      else
54        id
55      end
56    }
57  end
58
59  def initaialize(parent, *args)
60    @parent = @t = parent
61    @tpath = parent.path
62
63    @path = @id = @t.tk_send('add', *args)
64    # A same id is rejected by the Tcl function.
65
66    ToolbarItemID_TBL.mutex.synchronize{
67      ToolbarItemID_TBL[@id] = self
68      ToolbarItemID_TBL[@tpath] = {} unless ToolbarItemID_TBL[@tpath]
69      ToolbarItemID_TBL[@tpath][@id] = self
70    }
71  end
72
73  def [](key)
74    cget key
75  end
76
77  def []=(key,val)
78    configure key, val
79    val
80  end
81
82  def cget_tkstring(option)
83    @t.itemcget_tkstring(@id, option)
84  end
85  def cget(option)
86    @t.itemcget(@id, option)
87  end
88  def cget_strict(option)
89    @t.itemcget_strict(@id, option)
90  end
91
92  def configure(key, value=None)
93    @t.itemconfigure(@id, key, value)
94    self
95  end
96
97  def configinfo(key=nil)
98    @t.itemconfiginfo(@id, key)
99  end
100
101  def current_configinfo(key=nil)
102    @t.current_itemconfiginfo(@id, key)
103  end
104
105  def delete
106    @t.delete(@id)
107  end
108
109  def itemid
110    @t.itemid(@id)
111  end
112
113  def remove
114    @t.remove(@id)
115  end
116  def remove_with_destroy
117    @t.remove_with_destroy(@id)
118  end
119end
120
121class Tk::Tcllib::Widget::Toolbar
122  include Tk::Tcllib::Widget::ToolbarItemConfig
123
124  TkCommandNames = ['::widget::toolbar'.freeze].freeze
125
126  def __destroy_hook__
127    Tk::Tcllib::Widget::ToolbarItem::ToolbarItemID_TBL.mutex.synchronize{
128      Tk::Tcllib::Widget::ToolbarItem::ToolbarItemID_TBL.delete(@path)
129    }
130  end
131
132  def create_self(keys)
133    if keys and keys != None
134      tk_call_without_enc(self.class::TkCommandNames[0], @path,
135                          *hash_kv(keys, true))
136    else
137      tk_call_without_enc(self.class::TkCommandNames[0], @path)
138    end
139  end
140  private :create_self
141
142  def getframe
143    window(tk_send('getframe'))
144  end
145  alias get_frame getframe
146
147  def add(*args)
148    Tk::Tcllib::Widget::Toolbar.new(self, *args)
149  end
150
151  def itemid(item)
152    window(tk_send('itemid'))
153  end
154
155  def items(pattern)
156    tk_split_simplelist(tk_send('items', pattern)).map{|id|
157      Tk::Tcllib::Widget::ToolbarItem.id2obj(self, id)
158    }
159  end
160
161  def remove(*items)
162    tk_send('remove', *items)
163    self
164  end
165
166  def remove_with_destroy(*items)
167    tk_send('remove', '-destroy', *items)
168    self
169  end
170
171  def delete(*items)
172    tk_send('delete', *items)
173    self
174  end
175end
176