DEFINITIONS
This source file includes following functions.
1 #
2 # tkmenubar.rb
3 #
4 # Copyright (C) 1998 maeda shugo. All rights reserved.
5 # This file can be distributed under the terms of the Ruby.
6
7 # Usage:
8 #
9 # menu_spec = [
10 # [['File', 0],
11 # ['Open', proc{puts('Open clicked')}, 0],
12 # '---',
13 # ['Quit', proc{exit}, 0]],
14 # [['Edit', 0],
15 # ['Cut', proc{puts('Cut clicked')}, 2],
16 # ['Copy', proc{puts('Copy clicked')}, 0],
17 # ['Paste', proc{puts('Paste clicked')}, 0]]
18 # ]
19 # menubar = TkMenubar.new(nil, menu_spec,
20 # 'tearoff'=>false,
21 # 'foreground'=>'grey40',
22 # 'activeforeground'=>'red',
23 # 'font'=>'-adobe-helvetica-bold-r-*--12-*-iso8859-1')
24 # menubar.pack('side'=>'top', 'fill'=>'x')
25 #
26 #
27 # OR
28 #
29 #
30 # menubar = TkMenubar.new
31 # menubar.add_menu([['File', 0],
32 # ['Open', proc{puts('Open clicked')}, 0],
33 # '---',
34 # ['Quit', proc{exit}, 0]])
35 # menubar.add_menu([['Edit', 0],
36 # ['Cut', proc{puts('Cut clicked')}, 2],
37 # ['Copy', proc{puts('Copy clicked')}, 0],
38 # ['Paste', proc{puts('Paste clicked')}, 0]])
39 # menubar.configure('tearoff', false)
40 # menubar.configure('foreground', 'grey40')
41 # menubar.configure('activeforeground', 'red')
42 # menubar.configure('font', '-adobe-helvetica-bold-r-*--12-*-iso8859-1')
43 # menubar.pack('side'=>'top', 'fill'=>'x')
44
45 # The format of the menu_spec is:
46 # [
47 # [
48 # [button text, underline, accelerator],
49 # [menu label, command, underline, accelerator],
50 # '---', # separator
51 # ...
52 # ],
53 # ...
54 # ]
55
56 # underline and accelerator are optional parameters.
57 # Hashes are OK instead of Arrays.
58
59 # To use add_menu, configuration must be done by calling configure after
60 # adding all menus by add_menu, not by the constructor arguments.
61
62 require "tk"
63
64 class TkMenubar<TkFrame
65
66 include TkComposite
67
68 def initialize(parent = nil, spec = nil, options = nil)
69 if parent.kind_of? Hash
70 options = _symbolkey2str(parent)
71 spec = options.delete('spec')
72 super(options)
73 else
74 super(parent, options)
75 end
76
77 @menus = []
78
79 if spec
80 for menu_info in spec
81 add_menu(menu_info)
82 end
83 end
84
85 if options
86 for key, value in options
87 configure(key, value)
88 end
89 end
90 end
91
92 def add_menu(menu_info)
93 btn_info = menu_info.shift
94 mbtn = TkMenubutton.new(@frame)
95
96 if btn_info.kind_of?(Hash)
97 for key, value in btn_info
98 mbtn.configure(key, value)
99 end
100 elsif btn_info.kind_of?(Array)
101 mbtn.configure('text', btn_info[0]) if btn_info[0]
102 mbtn.configure('underline', btn_info[1]) if btn_info[1]
103 mbtn.configure('accelerator', btn_info[2]) if btn_info[2]
104 else
105 mbtn.configure('text', btn_info)
106 end
107
108 menu = TkMenu.new(mbtn)
109
110 for item_info in menu_info
111 if item_info.kind_of?(Hash)
112 menu.add('command', item_info)
113 elsif item_info.kind_of?(Array)
114 options = {}
115 options['label'] = item_info[0] if item_info[0]
116 options['command'] = item_info[1] if item_info[1]
117 options['underline'] = item_info[2] if item_info[2]
118 options['accelerator'] = item_info[3] if item_info[3]
119 menu.add('command', options)
120 elsif /^-+$/ =~ item_info
121 menu.add('sep')
122 else
123 menu.add('command', 'label' => item_info)
124 end
125 end
126
127 mbtn.menu(menu)
128 @menus.push([mbtn, menu])
129 delegate('tearoff', menu)
130 delegate('foreground', mbtn, menu)
131 delegate('background', mbtn, menu)
132 delegate('disabledforeground', mbtn, menu)
133 delegate('activeforeground', mbtn, menu)
134 delegate('activebackground', mbtn, menu)
135 delegate('font', mbtn, menu)
136 delegate('kanjifont', mbtn, menu)
137 mbtn.pack('side' => 'left')
138 end
139
140 def [](index)
141 return @menus[index]
142 end
143 end