/
tk-menu.R
327 lines (283 loc) · 9.35 KB
/
tk-menu.R
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
all_valid_children_for_menu <- function(objs) {
res <- vapply(objs, function(x) {
x$type %in% c('menuitem', 'submenu')
}, logical(1))
res
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Button that pops down a menu when pressed
#'
#' A menubutton widget displays a textual label and/or image, and displays a
#' menu when pressed.
#'
#' Include items in this menu using \code{tic_menuitem()} and \code{tic_submenu()}
#'
#' @inheritParams tic_button
#' @param pack how this object should be packed into its parent. \code{pack} has
#' no effect for \code{tic_menu()} or \code{tic_menu_item()}.
#'
#' @return handle on the tcl/tk object
#' @export
#'
#' @family widgets
#'
#' @examples
#' tic_menubutton(
#' text = "Press for menu",
#' tic_menuitem("Run a command", menutype = "command", command = function() {
#' message("This is where a command is run")
#' }),
#' tic_submenu(
#' label = "Sub menu here",
#' tic_menuitem("Run this", "command", command = function() { message("Hello")})
#' )
#' )
#'
#'
#' @section tcl/tk:
#'
#' See tcl/tk documentation for more information on this element
#' \url{https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_menubutton.htm}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tic_menubutton <- function(..., text,
bind = NULL, pack = NULL) {
args <- find_args(...)
check <- all_valid_children_for_menu(args$unnamed)
if (any(!check)) {
stop("All child objects to a 'tic_menubutton()' must be 'tic_menuitem()' or 'tic_submenu()'")
}
spec <- list(
args = args$named,
type = 'menubutton',
children = args$unnamed,
binding = bind,
pack = pack
)
class(spec) <- 'tic_spec'
spec
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render a menubutton
#
# mbutt <- ttkmenubutton(window, text = "MenuButton")
# actual <- tkmenu ( mbutt )
# tkpack(mbutt)
# tkconfigure(mbutt, menu = actual)
# tkadd(actual, "command", label = "Hello", command = function() { message("MenuButton hello")})
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
render_tic_menubutton <- function(parent, spec) {
stopifnot(identical(spec$type, 'menubutton'))
spec$args <- handle_images(spec$args)
container <- do.call(tcltk::ttkmenubutton, c(list(parent), spec$args))
menu <- tcltk::tkmenu(container)
tcltk::tkconfigure(container, menu = menu)
for (item in spec$children) {
stopifnot(item$type %in% c('menuitem', 'submenu'))
if (identical(item$type, 'menuitem')) {
call_tcltk(
tcltk::tkadd,
c(list(menu), list(item$menutype), item$args),
item
)
} else if (identical(item$type, 'submenu')) {
render_tic_submenu(menu, item)
}
}
container$type <- spec$type
render_binding(container, spec)
as_tic_ui(container)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a toplevel menu bar.
#'
#' This element must an immediate child of the main \code{tic_window()}
#'
#' @inheritParams tic_button
#' @param tearoff Can the menu be torn off? Default: FALSE
#'
#' @return handle on the tcl/tk object
#' @export
#'
#' @family widgets
#'
#' @examples
#' \dontrun{
#' tic_window(
#' tic_menu(
#' text = "Press for menu",
#' tic_menuitem("Run a command", menutype = "command", command = function() {
#' message("This is where a command is run")
#' }),
#' tic_submenu(
#' label = "Sub menu here",
#' tic_menuitem("Run this", "command", command = function() { message("Hello")})
#' )
#' )
#' )
#' }
#'
#'
#' @section tcl/tk:
#'
#' See tcl/tk documentation for more information on this element
#' \url{https://www.tcl.tk/man/tcl8.6/TkCmd/menu.htm}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tic_menu <- function(..., text, tearoff = FALSE,
bind = NULL, pack = NULL) {
args <- find_args(...)
check <- all_valid_children_for_menu(args$unnamed)
if (any(!check)) {
stop("All child objects to a 'tic_menu()' must be 'tic_menuitem()' or 'tic_submenu()'")
}
args$named$background <- args$named$background %||% 'white'
spec <- list(
args = args$named,
type = 'menu',
children = args$unnamed,
binding = bind,
pack = pack
)
class(spec) <- 'tic_spec'
spec
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render a menu
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
render_tic_menu <- function(parent, spec) {
stopifnot(identical(spec$type, 'menu'))
spec$args <- handle_images(spec$args)
menu_bar <- do.call(tcltk::tkmenu, c(list(parent), spec$args))
tcltk::tkconfigure(parent, menu = menu_bar)
for (item in spec$children) {
stopifnot(item$type %in% c('menuitem', 'submenu'))
if (identical(item$type, 'menuitem')) {
call_tcltk(
tcltk::tkadd,
c(list(menu_bar), list(item$menutype), item$args),
item
)
} else if (identical(item$type, 'submenu')) {
render_tic_submenu(menu_bar, item)
}
}
menu_bar$type <- spec$type
render_binding(menu_bar, spec)
as_tic_ui(menu_bar)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a submenu under a menubutton or menu
#'
#' @inheritParams tic_button
#' @param label submenu label
#' @param tearoff FALSE
#'
#' @return handle on the tcl/tk object
#' @export
#'
#' @family widgets
#'
#'
#' @section tcl/tk:
#'
#' See tcl/tk documentation for more information on this element
#' \url{https://www.tcl.tk/man/tcl8.6/TkCmd/menu.htm}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tic_submenu <- function(..., label, tearoff = FALSE,
bind = NULL, pack = NULL) {
args <- find_args(...)
check <- all_valid_children_for_menu(args$unnamed)
if (any(!check)) {
stop("All child objects to a 'tic_submenu' must be 'tic_menuitem()' or 'tic_submenu()'")
}
args$named$label <- NULL
args$named$background <- args$named$background %||% 'white'
spec <- list(
args = args$named,
type = 'submenu',
children = args$unnamed,
binding = bind,
label = label,
pack = pack
)
class(spec) <- 'tic_spec'
spec
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Render a submenu.
# submenus can have submenus
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
render_tic_submenu <- function(parent, spec) {
stopifnot(identical(spec$type, 'submenu'))
spec$args <- handle_images(spec$args)
submenu <- do.call(tcltk::tkmenu, c(list(parent), spec$args))
# tcltk::tkconfigure(parent, menu = submenu)
tkadd(parent, "cascade", label = spec$label, menu = submenu, background = 'white')
for (item in spec$children) {
stopifnot(item$type %in% c('menuitem', 'submenu'))
if (identical(item$type, 'menuitem')) {
call_tcltk(
tcltk::tkadd,
c(list(submenu), list(item$menutype), item$args),
item
)
} else if (identical(item$type, 'submenu')) {
render_tic_submenu(submenu, item)
}
}
submenu$type <- spec$type
render_binding(submenu, spec)
as_tic_ui(submenu)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#' Create a menu item
#'
#' This element must an immediate child of a \code{tic_menubuton},
#' \code{tic_menu} or \code{tic_submenu}
#'
#' @inheritParams tic_button
#' @param label text to display
#' @param menutype one of the following:
#' \describe{
#' \item{\code{command}}{When menu item is selected, run the function given
#' by the \code{command} argument}
#' \item{\code{cascade}}{Do not use. Use \code{tic_submenu()}} to create
#' a submenu.
#' \item{\code{separator}}{Horizontal separato between menu items}
#' \item{\code{checkbutton}}{A menuitem which functions as a checkbutton.
#' Use this in conjunction with \code{variable} to specify the
#' \code{reactive_lgl} variable to store the state of the button
#' (i.e. TRUE/FALSE)}. Use in conjunctionwith \code{command} argument
#' to run a function each time the button is selected.
#' \item{\code{radiobutton}}{Same as the \code{checkbutton} menu item, but
#' fo defining a mutually exclusive set of selection options.
#' See \code{tic_radiobutton()} for more information and examples}
#' }
#'
#' @return handle on the tcl/tk object
#' @export
#'
#' @family widgets
#'
#'
#' @section tcl/tk:
#'
#' See tcl/tk documentation for more information on this element
#' \url{https://www.tcl.tk/man/tcl8.6/TkCmd/menu.htm}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tic_menuitem <- function(label, menutype, command, image,
bind = NULL, pack = NULL, ...) {
args <- find_args(...)
stopifnot(`No child elements allowed for 'tic_menuitem()'` = length(args$unnamed) == 0)
args$named$menutype <- NULL
args$named$background <- args$named$background %||% 'white'
spec <- list(
args = args$named,
type = 'menuitem',
menutype = menutype,
children = args$unnamed,
binding = bind,
pack = pack
)
class(spec) <- 'tic_spec'
spec
}