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 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
|
### tk2widgets.R - Support for the ttk widgets
### Copyright (c), Philippe Grosjean (phgrosjean@sciviews.org)
### Licensed under LGPL 3 or above
###
### Changes:
### - 2012-01-09: reworked tk2label to provide "full options"
###
### - 2012-01-07: listbox now behaves like a ttk widget, although it is not!
###
### - 2009-06-30: only use ttk (no tile or plain widgets)
###
### - 2007-01-01: first version (for tcltk2_1.0-0)
###
### To do:
### - Rework all this and add new widgets like sizegrip, tkplot, ...
tk2button <- function (parent, tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::button", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2button", "tk2widget", class(w))
return(w)
}
tk2canvas <- function (parent, tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
### TODO: use autoscroll here!
## Default background to fieldbackground
if (any(names(list(...)) == "background")) {
w <- tkwidget(parent, "canvas", ...)
} else {
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
w <- tkwidget(parent, "canvas",
background = background, ...)
}
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2canvas", "tk2widget", class(w))
return(w)
}
tk2checkbutton <- function (parent, tip = "", ...)
{
### TODO: associate with a variable and set both states values
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <-tkwidget(parent, "ttk::checkbutton", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2checkbutton", "tk2widget", class(w))
return(w)
}
tk2combobox <- function (parent, tip = "", ...)
{
### TODO: associate the list and results with a variable and intialize the widget
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::combobox", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2combobox", "tk2widget", class(w))
return(w)
}
### TODO: a centry widget
tk2entry <- function (parent, tip = "", ...)
{
### TODO: add cut/copy/paste/select all/clear context menu
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::entry", cursor = "xterm", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2entry", "tk2widget", class(w))
return(w)
}
tk2frame <- function (parent, ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::frame", ...)
class(w) <- c("tk2frame", "tk2widget", class(w))
return(w)
}
tk2label <- function (parent, tip, label, tag, cfglist, wrap = FALSE, ...)
{
## Also image, text, textvariable, label & tag for data
## width = -10, compound = "left", justify = "left", wrap = FALSE for config
## Special treatment from wrap (wraplength)
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
## If a config list is provided, rework arguments
if (!missing(cfglist)) {
args <- .mergeList(cfglist, list(...))
args$parent <- parent
if (!missing(tip)) args$tip <- tip
if (!missing(label)) args$label <- label
if (!missing(tag)) args$tag <- tag
return(do.call(tk2label, args))
}
## Create the widget and the corresponding R object
w <- tkwidget(parent, "ttk::label", ...)
class(w) <- c("tk2label", "tk2widget", class(w))
if (!missing(tip)) tip(w) <- tip
if (!missing(label)) label(w) <- label
if (!missing(tag)) tag(w) <- tag
## Special treatment for 'wrap' argument that does not exists in ttk::label
if (!missing(wrap)) {
## We need width
width <- abs(as.integer(tclvalue(tkcget(w, "-width"))))
if (isTRUE(wrap)) wraplength <- .wraplength(w, width) else wraplength <- 0
tkconfigure(w, wraplength = wraplength)
## If width is not reapplied after wraplength, the text is not always
## wrapped in the widget (is this a bug?)
if (wraplength > 0 && length(width)) tkconfigure(w, width = width)
}
return(w)
}
tk2labelframe <- function (parent, ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::labelframe", ...)
class(w) <- c("ttk2labelframe", "tk2widget", class(w))
return(w)
}
## TODO: I need to rework this on the same scheme as tk2label
tk2listbox <- function (parent, values, value, selection,
selectmode = c("extended", "single", "browse", "multiple"), height = 5,
tip = "", scroll = "both", autoscroll = "x", enabled = TRUE, ...)
{
## Check conditions and arguments
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
selectmode <- match.arg(selectmode)
scrolls <- c("x", "y", "both", "none")
scroll <- match.arg(scroll, scrolls)
autoscroll <- match.arg(autoscroll, scrolls)
if (missing(values) || length(values) == 0) {
values <- NULL
} else {
values <- as.character(values)
}
## We provide either value or selection... translate value into selection
if (missing(selection)) selection <- NULL
if (!missing(value)) {
if (is.null(values) || length(values) < 1) selection <- NULL else
selection <- (1:length(values))[values %in% as.character(value)]
}
if (length(selection) == 0 || is.null(values) || length(values) < 1) {
selection <- NULL
} else {
selection <- sort(as.integer(round(selection)))
if (selection[1] < 1)
stop("Numerical selections must be indices > 0")
if (selection[1] > length(values)) selection <- NULL
if (selectmode == "single" && length(selection) > 1) {
warning("Only lowest selection used in single selection mode")
selection <- selection[1]
}
}
## Location of the widget depends if we add scrollbars or not
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
if (scroll == "none") {
w <- tkwidget(parent, "listbox", font = "TkDefaultFont",
borderwidth = 1, relief = "sunken", activestyle = "dotbox",
selectmode = selectmode, height = height, exportselection = 0,
background = background, ...)
} else { # We need to create a tk2frame as parent of the listbox
wf <- tk2frame(parent)
w <- tkwidget(wf, "listbox", font = "TkDefaultFont",
borderwidth = 1, relief = "sunken", activestyle = "dotbox",
selectmode = selectmode, height = height, exportselection = 0,
background = background, ...)
}
## Make it react to tk2theme changes, and integrate the listbox as much
## as possible with current ttk theme
#restyleListbox <- function (W) {
# ## Restyle the listbox according to current ttk style
# ## Note: font is set to TkDefaultFont => already managed there!
# tkconfigure(W,
# foreground = tk2style("tk2entry", "foreground",
# default = "#000000"),
# borderwidth = tk2style("", "borderwidth", default = 0),
# disabledforeground = tk2style("tk2entry", "foreground",
# "disabled", default = "#a3a3a3"),
# highlightbackground = tk2style("tk2entry", "selectbackground",
# default = "#c3c3c3"),
# highlightcolor = tk2style("tk2entry", "selectbackground",
# default = "#c3c3c3"),
# selectbackground = tk2style("tk2entry", "selectbackground",
# default = "#c3c3c3"),
# selectforeground = tk2style("tk2entry", "selectforeground",
# default = "#ffffff")
# )
#}
## Restyle it now
#restyleListbox(w)
restyleListbox <- function (W) {
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
tkconfigure(W, background = background)
}
## If there are values and/or selections, populate the list now
for (item in values)
tkinsert(w, "end", item)
if (!is.null(selection)) {
for (sel in selection)
tkselection.set(w, sel - 1) # Because Tcl uses 0-based indexing!
tksee(w, selection[1]) # Ensure that the first selected item is visible
}
## Possibly add a tooltip
if (tip != "") tk2tip(w, tip)
## Do we add scrollbars?
if (scroll == "none") {
## Apply bindings to original listbox
tkbind(w, "<<ThemeChanged>>", restyleListbox)
tkbind(w, "<1>", function (W) tkfocus(W)) # Needed for mouseweel action
## Do we disable it?
if (!isTRUE(enabled)) tkconfigure(w, state = "disabled")
## Done... just return the widget
class(w) <- c("tk2listbox", "tk2widget", class(w))
return(w)
} else {
## Add (autohide) scrollbar(s)
tcl("scrolledWidget", w, wf, scroll, autoscroll)
## Apply bindings to frame container
tkbind(wf, "<<ThemeChanged>>", restyleListbox)
tkbind(wf, "<1>", function (W) tkfocus(W)) # Needed for mouseweel action
class(wf) <- c("tk2listbox", "tk2widget", class(w))
## Do we disable it?
if (!isTRUE(enabled)) tkconfigure(wf, state = "disabled")
return(wf)
}
}
tk2mclistbox <- function (parent, tip ="", ...)
{
### TODO: a tile equivalent of this widget
### TODO: or adjust the header: font, color and frame
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
res <- tclRequire("mclistbox")
if (!inherits(res, "tclObj"))
stop("Impossible to load the Tcl mclistbox package; check your Tcl/Tk installation")
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
w <- tkwidget(parent, "mclistbox::mclistbox", font = "TkDefaultFont",
background = background, ...)
tkconfigure(w, relief = "flat")
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2mclistbox", "tk2widget", class(w))
return(w)
}
tk2menu <- function (parent, ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "menu", ...)
class(w) <- c("tk2menu", "tk2widget", class(w))
return(w)
}
tk2menubutton <- function (parent, tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::menubutton", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2menubutton", "tk2widget", class(w))
return(w)
}
tk2message <- function (parent, text = "", justify = c("left", "center", "right"),
width = -1, aspect = 150, tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
justify <- as.character(justify[1])
w <- tkwidget(parent, "message", text = text, justify = justify,
width = width, aspect = aspect, font = "TkDefaultFont", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2message", "tk2widget", class(w))
return(w)
}
tk2notebook <- function (parent, tabs, ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <- tkwidget(parent, "ttk::notebook", ...)
## Add pages
tabs <- as.character(tabs)
for (tab in tabs) {
tframe <- tk2frame(w)
tkadd(w, tframe, text = tab, sticky = "nsew")
}
tk2notetraverse(w) # Enable keyboard traversal for this notebook
class(w) <- c("tk2notebook", "tk2widget", class(w))
return(w)
}
tk2panedwindow <- function (parent, orientation = c("horizontal", "vertical"), ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
orientation <- as.character(orientation[1])
w <- tkwidget(parent, "ttk::panedwindow", orient = orientation, ...)
class(w) <- c("tk2panedwindow", "tk2widget", class(w))
return(w)
}
tk2progress <- function (parent, orientation = c("horizontal", "vertical"),
tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
orientation <- as.character(orientation[1])
w <- tkwidget(parent, "ttk::progressbar", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2progress", "tk2widget", class(w))
return(w)
}
tk2radiobutton <- function (parent, tip = "", ...)
{
### TODO: associate with a variable and set both states values
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
w <-tkwidget(parent, "ttk::radiobutton", ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2radiobutton", "tk2widget", class(w))
return(w)
}
tk2scale <- function (parent, orientation = c("horizontal", "vertical"),
tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
orientation <- as.character(orientation[1])
w <- tkwidget(parent, "ttk::scale", orient = orientation, ...)
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2scale", "tk2widget", class(w))
return(w)
}
tk2scrollbar <- function (parent, orientation = c("horizontal", "vertical"), ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
orientation <- as.character(orientation[1])
w <- tkwidget(parent, "ttk::scrollbar", orient = orientation, ...)
class(w) <- c("tk2scrollbar", "tk2widget", class(w))
return(w)
}
tk2separator <- function (parent, orientation = c("horizontal", "vertical"), ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
orientation <- as.character(orientation[1])
w <-tkwidget(parent, "ttk::separator", orient = orientation, ...)
class(w) <- c("tk2separator", "tk2widget", class(w))
return(w)
}
tk2spinbox <- function (parent, tip = "", ...)
{
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
## Default background to fieldbackground
if (any(names(list(...)) == "background")) {
w <- tkwidget(parent, "spinbox", font = "TkDefaultFont",
relief = "solid", borderwidth = 1, ...)
} else {
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
w <- tkwidget(parent, "spinbox", font = "TkDefaultFont",
relief = "solid", borderwidth = 1,
background = background, ...)
}
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2spinbox", "tk2widget", class(w))
return(w)
}
tk2table <- function (parent, ...)
{
### TODO: a pure Tcl equivalent
### TODO: a tile equivalent (some customization, if possible)
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
if (inherits(tclRequire("Tktable", warn = FALSE), "tclObj")) {
w <- tkwidget(parent, "table", font = "TkDefaultFont", ...)
class(w) <- c("tk2table", "tk2widget", class(w))
return(w)
} else stop("Tcl package 'Tktable' must be installed first")
}
tk2tablelist <- function (parent, ...)
{
if (!is.ttk())
stop("Tcl/Tk >= 8.5 is required")
if (inherits(tclRequire("tablelist_tile", warn = FALSE), "tclObj")) {
## Default background to fieldbackground
if (any(names(list(...)) == "background")) {
w <- tkwidget(parent, "tablelist::tablelist",
font = "TkDefaultFont", ...)
} else {
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
w <- tkwidget(parent, "tablelist::tablelist",
font = "TkDefaultFont", background = background, ...)
}
class(w) <- c("tk2tablelist", "tk2widget", class(w))
return(w)
}
else stop("Tcl package 'tablelist' must be installed first")
}
tk2text <- function (parent, tip = "", ...)
{
### TODO: autohide scrollbars
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
## Default background to fieldbackground
if (any(names(list(...)) == "background")) {
w <- tkwidget(parent, "text", font = "TkTextFont", ...)
} else {
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
w <- tkwidget(parent, "text", font = "TkTextFont",
background = background, ...)
}
tkconfigure(w, relief = "flat")
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2text", "tk2widget", class(w))
return(w)
}
tk2ctext <- function (parent, tip = "", ...)
{
### TODO: autohide scrollbars
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
tclRequire("ctext")
## Default background to fieldbackground
if (any(names(list(...)) == "background")) {
w <- tkwidget(parent, "ctext", font = "TkFixedFont", ...)
} else {
background <- tclvalue(.Tcl("ttk::style lookup TEntry -fieldbackground"))
if (background == "") background <- "white"
w <- tkwidget(parent, "ctext", font = "TkFixedFont",
background = background, ...)
}
tkconfigure(w, relief = "flat")
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2ctext", "tk2widget", class(w))
return(w)
}
### TODO: rework this, using ttk::treeview
tk2tree <- function (parent, tip = "", ...)
{
### TODO: better icons!
## Reasonable default icons for files and folders
if (!is.ttk()) stop("Tcl/Tk >= 8.5 is required")
images <- as.character(tcl("image", "names"))
if (!"Tree:dir" %in% images)
.Tcl("image create photo Tree:dir -data {R0lGODdhEAAQAPIAAAAAAHh4eLi4uPj4APj4+P///wAAAAAAACwAAAAAEAAQAAADPVi63P4wLkKCtTTnUsXwQqBtAfh910UU4ugGAEucpgnLNY3Gop7folwNOBOeiEYQ0acDpp6pGAFArVqthQQAO///}")
if (!"Tree:file" %in% images)
.Tcl("image create photo Tree:file -data {R0lGODdhEAAQAPIAAAAAAHh4eLi4uPj4+P///wAAAAAAAAAAACwAAAAAEAAQAAADPkixzPODyADrWE8qC8WN0+BZAmBq1GMOqwigXFXCrGk/cxjjr27fLtout6n9eMIYMTXsFZsogXRKJf6uP0kCADv/}")
### TODO: correct support of font
w <- tkwidget(parent, "Tree:create") #, font = "TkDefaultFont", ...)
tkconfigure(w, relief = "flat")
if (tip != "") tk2tip(w, tip)
class(w) <- c("tk2tree", "tk2widget", class(w))
return(w)
}
|