1#
2#  tkextlib/iwidgets/menubar.rb
3#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
4#
5
6require 'tk'
7require 'tkextlib/iwidgets.rb'
8
9module Tk
10  module Iwidgets
11    class Menubar < Tk::Itk::Widget
12    end
13  end
14end
15
16class Tk::Iwidgets::Menubar
17  TkCommandNames = ['::iwidgets::menubar'.freeze].freeze
18  WidgetClassName = 'Menubar'.freeze
19  WidgetClassNames[WidgetClassName] ||= self
20
21  def __strval_optkeys
22    super() << 'menubuttons'
23  end
24  private :__strval_optkeys
25
26  def __tkvariable_optkeys
27    super() << 'helpvariable'
28  end
29  private :__tkvariable_optkeys
30
31  ####################################
32
33  include TkItemConfigMethod
34
35  def __item_cget_cmd(id)
36    [self.path, 'menucget', id]
37  end
38  private :__item_cget_cmd
39
40  def __item_config_cmd(id)
41    [self.path, 'menuconfigure', id]
42  end
43  private :__item_config_cmd
44
45  def __item_strval_optkeys(id)
46    super(id) << 'selectcolor'
47  end
48  private :__item_strval_optkeys
49
50  def __item_tkvariable_optkeys(id)
51    super(id) << 'helpstr'
52  end
53  private :__item_tkvariable_optkeys
54
55  def tagid(tagOrId)
56    if tagOrId.kind_of?(Tk::Itk::Component)
57      tagOrId.name
58    else
59      #_get_eval_string(tagOrId)
60      tagOrId
61    end
62  end
63
64  alias menucget_tkstring itemcget_tkstring
65  alias menucget itemcget
66  alias menucget_strict itemcget_strict
67  alias menuconfigure itemconfigure
68  alias menuconfiginfo itemconfiginfo
69  alias current_menuconfiginfo current_itemconfiginfo
70
71  private :itemcget_tkstring, :itemcget, :itemcget_strict
72  private :itemconfigure, :itemconfiginfo, :current_itemconfiginfo
73
74  ####################################
75
76  def __methodcall_optkeys
77    {'menubuttons'=>'menubuttons'}
78  end
79
80  def menubuttons(val = nil)
81    unless val
82      return tk_call(@path, 'cget', '-menubuttons')
83    end
84
85    tk_call(@path, 'configure', '-menubuttons', _parse_menu_spec(val))
86    self
87  end
88
89  def _parse_menu_spec(menu_spec)
90    ret = ''
91    menu_spec.each{|spec|
92      next unless spec
93
94      if spec.kind_of?(Hash)
95        args = [spec]
96        type = 'options'
97      else
98        type, *args = spec
99      end
100
101      type = type.to_s
102      case type
103      when 'options'
104        keys = args[0]
105        ary = [type]
106        ary.concat(hash_kv(keys))
107        ret << array2tk_list(ary) << "\n"
108
109      when 'menubutton', 'cascade'
110        name, keys = args
111        if keys
112          ary = [type, name]
113          keys = _symbolkey2str(keys)
114          keys['menu'] = _parse_menu_spec(keys['menu']) if keys.key?('menu')
115          ary.concat(hash_kv(keys))
116          ret << array2tk_list(ary) << "\n"
117        else
118          ret << array2tk_list([type, name]) << "\n"
119        end
120
121      else
122        name, keys = args
123        if keys
124          ary = [type, name]
125          ary.concat(hash_kv(keys))
126          ret << array2tk_list(ary) << "\n"
127        else
128          ret << array2tk_list([type, name]) << "\n"
129        end
130      end
131    }
132    ret
133  end
134
135  ####################################
136
137  def add(type, tag=nil, keys={})
138    if tag.kind_of?(Hash)
139      keys = tag
140      tag = nil
141    end
142    if tag
143      tag = Tk::Itk::Component.new(self, tagid(tag))
144    else
145      tag = Tk::Itk::Component.new(self)
146    end
147    keys = _symbolkey2str(keys)
148    keys['menu'] = _parse_menu_spec(keys['menu']) if keys.key?('menu')
149    tk_call(@path, 'add', type, tagid(tag), *hash_kv(keys))
150    tag
151  end
152
153  def delete(path1, path2=nil)
154    if path2
155    else
156      tk_call(@path, 'delete', index(idx))
157    end
158    self
159  end
160
161  def index(idx)
162    number(tk_call(@path, 'index', tagid(idx)))
163  end
164
165  def insert(idx, type, tag=nil, keys={})
166    if tag.kind_of?(Hash)
167      keys = tag
168      tag = nil
169    end
170    if tag
171      tag = Tk::Itk::Component.new(self, tagid(tag))
172    else
173      tag = Tk::Itk::Component.new(self)
174    end
175    keys = _symbolkey2str(keys)
176    keys['menu'] = _parse_menu_spec(keys['menu']) if keys.key?('menu')
177    tk_call(@path, 'insert', index(idx), type, tagid(tag), *hash_kv(keys))
178    tag
179  end
180
181  def invoke(idx)
182    tk_call(@path, 'invoke', index(idx))
183    self
184  end
185
186  def menupath(pat)
187    if (win = tk_call(@path, 'path', pat)) == '-1'
188      return nil
189    end
190    window(win)
191  end
192  def menupath_glob(pat)
193    if (win = tk_call(@path, 'path', '-glob', pat)) == '-1'
194      return nil
195    end
196    window(win)
197  end
198  def menupath_tclregexp(pat)
199    if (win = tk_call(@path, 'path', '-regexp', pat)) == '-1'
200      return nil
201    end
202    window(win)
203  end
204
205  def type(path)
206    tk_call(@path, 'type', path)
207  end
208
209  def yposition(path)
210    number(tk_call(@path, 'yposition', path))
211  end
212end
213