-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path08_1_Menubars_Menus.rb
204 lines (168 loc) · 4.55 KB
/
08_1_Menubars_Menus.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# coding: utf-8
# Copyright (C) 2019 Mark D. Blackwell.
require 'tk'
require 'tkextlib/tile'
module ::Menubars
module GraphicalHelper
def f_content
$f_content_private ||= begin
f = ::Tk::Tile::Frame.new root
f.grid sticky: :wnes
end
end
def me_menubar
# There only can be a single fallback menubar in the program, so use a global:
$me_menubar_private ||= ::TkMenu.new root
end
def root
$root_private ||= begin
tell_tk_which_encoding_to_use
::TkRoot.new
end
end
def tear_off_prevent
# See:
# http://tkdocs.com/tutorial/menus.html
# http://wiki.tcl-lang.org/page/Tearoff
::TkOption.add '*tearOff', false
nil
end
def weights_column_and_row_default_set_up(*args)
first = 0
args.reverse_each do |e|
::TkGrid.columnconfigure e, first, weight: 1
::TkGrid. rowconfigure e, first, weight: 1
end
nil
end
private
def tell_tk_which_encoding_to_use
::Tk::Encoding.encoding = ''.encoding
nil
end
end
end
module ::Menubars
module GraphicalObjects
def me_edit
@me_edit_private ||= ::TkMenu.new me_menubar
end
def me_file
@me_file_private ||= ::TkMenu.new me_menubar
end
def v_check
@v_check_private ||= ::TkVariable.new
end
def v_radio
@v_radio_private ||= ::TkVariable.new
end
end
end
module ::Menubars
module Graphical
extend GraphicalHelper
extend GraphicalObjects
extend self
def main
f_content.padding '3 3 3 3'
weights_column_and_row_set_up
menu_items_add
::Tk.mainloop
nil
end
private
def lambda_check
@lambda_check_private ||= ::Kernel.lambda do
print "lambda_check invoked.\n"
nil
end
end
def lambda_file_close
@lambda_file_close_private ||= ::Kernel.lambda do
print "lambda_file_close invoked.\n"
nil
end
end
def lambda_file_new
@lambda_file_new_private ||= ::Kernel.lambda do
print "lambda_file_new invoked.\n"
nil
end
end
def lambda_file_open
@lambda_file_open_private ||= ::Kernel.lambda do
print "lambda_file_open invoked.\n"
nil
end
end
def lambda_four
@lambda_four_private ||= ::Kernel.lambda do
print "lambda_four invoked.\n"
nil
end
end
def lambda_one
@lambda_one_private ||= ::Kernel.lambda do
print "lambda_one invoked.\n"
nil
end
end
def lambda_three
@lambda_three_private ||= ::Kernel.lambda do
print "lambda_three invoked.\n"
nil
end
end
def lambda_two
@lambda_two_private ||= ::Kernel.lambda do
print "lambda_two invoked.\n"
nil
end
end
def menu_edit_items_add
me_edit.add :command, label: 'Three', command: lambda_three, underline: 0
me_edit.add :command, label: 'Four', command: lambda_four, underline: 0
nil
end
def menu_file_items_add
menu_file_items_file_actions_add
me_file.add :separator
menu_file_items_checkbutton_add
menu_file_items_radiobuttons_add
nil
end
def menu_file_items_checkbutton_add
me_file.add :checkbutton, label: 'Check', command: lambda_check, underline: 1, variable: v_check, onvalue: 1, offvalue: 0
nil
end
def menu_file_items_file_actions_add
me_file.add :command, label: 'New', command: lambda_file_new, underline: 0
me_file.add :command, label: 'Open...', command: lambda_file_open, underline: 0
me_file.add :command, label: 'Close', command: lambda_file_close, underline: 0
nil
end
def menu_file_items_radiobuttons_add
me_file.add :radiobutton, label: 'One', command: lambda_one, underline: 2, variable: v_radio, value: 1
me_file.add :radiobutton, label: 'Two', command: lambda_two, underline: 0, variable: v_radio, value: 2
nil
end
def menu_items_add
menubar_create
menu_edit_items_add
menu_file_items_add
nil
end
def menubar_create
tear_off_prevent # Keep before any menu creation.
root[:menu] = me_menubar
me_menubar.add :cascade, menu: me_file, label: 'File', underline: 0
me_menubar.add :cascade, menu: me_edit, label: 'Edit', underline: 0
nil
end
def weights_column_and_row_set_up
weights_column_and_row_default_set_up f_content, root
nil
end
end
end
::Menubars::Graphical.main