[Sciviews-commits] r435 - in pkg/svDialogs: . R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Feb 19 10:33:43 CET 2012
Author: phgrosjean
Date: 2012-02-19 10:33:43 +0100 (Sun, 19 Feb 2012)
New Revision: 435
Added:
pkg/svDialogs/R/dlgForm.R
pkg/svDialogs/man/dlgForm.Rd
Modified:
pkg/svDialogs/DESCRIPTION
pkg/svDialogs/NAMESPACE
pkg/svDialogs/NEWS
Log:
added dlgForm in svDialogs
Modified: pkg/svDialogs/DESCRIPTION
===================================================================
--- pkg/svDialogs/DESCRIPTION 2012-02-12 20:54:54 UTC (rev 434)
+++ pkg/svDialogs/DESCRIPTION 2012-02-19 09:33:43 UTC (rev 435)
@@ -5,8 +5,8 @@
SystemRequirements: TODO!!!
Description: Rapidly construct dialog boxes for your GUI, including an automatic
function assistant
-Version: 0.9-48
-Date: 2012-02-05
+Version: 0.9-49
+Date: 2012-02-18
Author: Philippe Grosjean
Maintainer: Philippe Grosjean <phgrosjean at sciviews.org>
License: GPL-2
Modified: pkg/svDialogs/NAMESPACE
===================================================================
--- pkg/svDialogs/NAMESPACE 2012-02-12 20:54:54 UTC (rev 434)
+++ pkg/svDialogs/NAMESPACE 2012-02-19 09:33:43 UTC (rev 435)
@@ -2,6 +2,7 @@
import(tcltk, svMisc, svGUI)
export(dlgDir,
+ dlgForm,
dlgInput,
dlgList,
dlgMessage,
@@ -33,6 +34,10 @@
S3method(dlgDir, textCLI)
S3method(dlgDir, nativeGUI)
+S3method(dlgForm, gui)
+S3method(dlgForm, textCLI)
+S3method(dlgForm, nativeGUI)
+
S3method(dlgInput, gui)
S3method(dlgInput, textCLI)
S3method(dlgInput, nativeGUI)
Modified: pkg/svDialogs/NEWS
===================================================================
--- pkg/svDialogs/NEWS 2012-02-12 20:54:54 UTC (rev 434)
+++ pkg/svDialogs/NEWS 2012-02-19 09:33:43 UTC (rev 435)
@@ -1,5 +1,11 @@
= svDialogs News
+== Changes in svDialogs 0.9-49
+
+* Added dlgForm() for flexible form dialog box. Only the Linuxc implementation
+ using yad and the textual version are currently implemented.
+
+
== Changes in svDialogs 0.9-48
* Argument message is changed to title in dlgDir() function, to match
Added: pkg/svDialogs/R/dlgForm.R
===================================================================
--- pkg/svDialogs/R/dlgForm.R (rev 0)
+++ pkg/svDialogs/R/dlgForm.R 2012-02-19 09:33:43 UTC (rev 435)
@@ -0,0 +1,186 @@
+## The form is defined as a list
+## TXT, H, RO, NUM, CHK, CB, CBE, FL, SFL, DIR, CDIR, FN, MFL, DT, CLR, BTN or LBL
+## TXT = simple text entry (default type)
+## H = hidden text (password)
+## RO = read-only text
+## NUM = null of positive integers with up/down arrows
+## CHK = checkbox, return TRUE or FALSE!
+## CB = read-only combobox
+## CBE = editable combobox
+## FL = open file item
+## SFL = save file item
+## DIR = select directory
+## CDIR = select or create directory
+## FN = select font and size
+## MFL = select mutliple files
+## DT = date (use --date-format=%F for YYYY-MM-DD)
+## CLR = select a color (but no palette!)
+## BTN = display a button... content is shell command to execute!
+## LBL = display a label
+## ex:
+#Form <- list(
+# "Name:TXT" = "some text",
+# "Age:NUM" = 25,
+# "Sex:CB" = c("male", "female"),
+# "Married:CHK"=TRUE
+#)
+
+## TODO: implement --image= argument of yad
+
+## Define the S3 method
+dlgForm <- function (form, title = "Fill the form", message = NULL,
+columns = 1, strip.type = TRUE, ..., gui = .GUI)
+{
+ if (!gui$startUI("dlgForm", call = match.call(),
+ default = lapply(form, "[", 1),
+ msg = "Displaying a modal form dialog box",
+ msg.no.ask = "A modal form dialog box was by-passed"))
+ return(invisible(gui))
+
+ ## Check and rework main arguments and place them in gui$args
+ if (!is.list(form) || is.null(names(form)))
+ stop("'form' must be a named list")
+ if (!length(title)) title <- "Fill the form" else
+ title <- as.character(title)[1]
+ if (length(message)) message <- paste(message, sep = "", collapse = "\n")
+ columns <- as.integer(columns)
+ if (!length(columns)) columns <- 1 else columns <- columns[1]
+ if (columns < 1) columns <- 1
+ strip.type <- isTRUE(as.logical(strip.type))
+ gui$setUI(args = list(form = form, title = title, message = message,
+ columns = columns, strip.type = strip.type))
+
+ ## ... and dispatch to the method
+ UseMethod("dlgForm", gui)
+}
+
+## Used to break the chain of NextMethod(), searching for a usable method
+## in the current context
+dlgForm.gui <- function (form, title = "Fill the form", message = NULL,
+columns = 1, strip.type = TRUE, ..., gui = .GUI)
+{
+ msg <- paste("No workable method available to display a form dialog box using:",
+ paste(guiWidgets(gui), collapse = ", "))
+ gui$setUI(status = "error", msg = msg, widgets = "none")
+ stop(msg)
+}
+
+## The pure textual version used a fallback in case no GUI could be used
+dlgForm.textCLI <- function (form, title = "Fill the form", message = NULL,
+columns = 1, strip.type = TRUE, ..., gui = .GUI)
+{
+ gui$setUI(widgets = "textCLI")
+ ## Ask for the input for each form field using readline()
+ cat("==", gui$args$title, "==\n")
+ if (length(gui$args$message)) cat(gui$args$message, "\n", sep = "")
+ form <- gui$args$form
+ if (is.null(form)) return(list())
+ res <- form
+ ## Special treatment for :CHK type
+ isCHK <- grepl(":CHK$", names(form))
+ names(form) <- sub(":[A-Z]+$", "", names(form))
+ ## Do we strip type?
+ if (strip.type) res <- form
+ for (i in 1:length(form)) {
+ if (isCHK[i]) {
+ if (isTRUE(as.logical(form[[i]])[1]))
+ def <- "[Y]/n" else def <- "[N]/y"
+ ans <- readline(paste(names(form)[i], " ", def, ": ", sep = ""))
+ if (ans == "") ans <- def
+ ## Put TRUE if answer starts with y or Y, FALSE otherwise
+ res[[i]] <- (grepl("^[yY]", ans))
+ } else {
+ def <- as.character(form[[i]])
+ def[1] <- paste("[", def[1], "]", sep = "")
+ res[[i]] <- readline(paste(names(form)[i],
+ " ", paste(def, collapse = "/"), ": ", sep = ""))
+ ## TODO: how to enter multi-items here?
+ ## TODO: eliminate surrounding single or double quotes
+ if (res[[i]] == "") res[[i]] <- form[[i]][1] else
+ res[[i]] <- as(res[[i]], class(form[[i]])[1])
+ }
+ }
+ ## OK, Cancel or Redo?
+ ## TODO: the redo feature...
+ ans <- readline("==\n[OK]/Cancel ")
+ if (ans != "" && !grepl("^[oO]", ans)) res <- list()
+ gui$setUI(res = res, status = NULL)
+ return(invisible(gui))
+}
+
+## The native version of the input box
+dlgForm.nativeGUI <- function (form, title = "Fill the form", message = NULL,
+columns = 1, strip.type = TRUE, ..., gui = .GUI)
+{
+ gui$setUI(widgets = "nativeGUI")
+ ## A simple text input box using native window
+ ## Return either a string, or character(0) if 'Cancel' clicked
+ res <- switch(Sys.info()["sysname"],
+ Windows = .winDlgForm(gui$args$form, gui$args$title, gui$args$message,
+ gui$args$columns, gui$args$strip.type),
+ Darwin = .macDlgForm(gui$args$form, gui$args$title, gui$args$message,
+ gui$args$columns, gui$args$strip.type),
+ .unixDlgForm(gui$args$form, gui$args$title, gui$args$message,
+ gui$args$columns, gui$args$strip.type)
+ )
+
+ ## Do we need to further dispatch?
+ if (is.null(res)) NextMethod("dlgForm", gui) else {
+ gui$setUI(res = res, status = NULL)
+ return(invisible(gui))
+ }
+}
+
+## Windows version
+.winDlgForm <- function (form, title, message, columns, strip.type)
+{
+ ## Not yet => return NULL
+ return(NULL)
+}
+
+## Mac OS X version
+.macDlgForm <- function (form, title, message, columns, strip.type)
+{
+ ## Not yet => return NULL
+ return(NULL)
+}
+
+## Linux/Unix version
+.unixDlgForm <- function (form, title, message, columns, strip.type)
+{
+ ## yad must be installed on this machine!
+ if (Sys.which("yad") == "") return(NULL)
+ ## Avoid displaying warning message in case user clicks on Cancel
+ owarn <- getOption("warn")
+ on.exit(options(warn = owarn))
+ options(warn = -1)
+ ## Use yad to display the form dialog box
+ if (!length(message)) {
+ msg <- paste("yad --form --title=\"", title,
+ "\" --on-top --center --skip-taskbar --separator=\"@@@\"",
+ " --item-separator=\"&&&\" --date-format=%F --columns=", columns,
+ sep = "")
+ } else { # Message is provided
+ msg <- paste("yad --form --title=\"", title, "\" --text=\"", message,
+ "\" --on-top --center --skip-taskbar --separator=\"@@@\"",
+ " --item-separator=\"&&&\" --date-format=%F --columns=", columns,
+ sep = "")
+ }
+ ## Add the definition of the various fields
+ fields <- paste("--field=", shQuote(names(form)), sep = "", collapse = " ")
+ ## Add the default values
+ default <- paste(shQuote(sapply(form, paste, collapse = "&&&")), collapse = " ")
+ ## Display the dialog box
+ res <- system(paste(msg, fields, default), intern = TRUE)
+ ## Did the user cancelled the dialog box
+ if (!length(res)) return(list())
+ ## Reformat the result
+ res <- strsplit(res, "@@@", fixed = TRUE)[[1]]
+ ## Replace results in initial form
+ for (i in 1:length(form))
+ form[[i]] <- as(strsplit(res[i], "&&&", fixed = TRUE)[[1]],
+ class(form[[i]])[1])
+ ## Do we strip type?
+ if (strip.type) names(form) <- sub(":[A-Z]+$", "", names(form))
+ return(form)
+}
Added: pkg/svDialogs/man/dlgForm.Rd
===================================================================
--- pkg/svDialogs/man/dlgForm.Rd (rev 0)
+++ pkg/svDialogs/man/dlgForm.Rd 2012-02-19 09:33:43 UTC (rev 435)
@@ -0,0 +1,88 @@
+\name{dlgForm}
+\alias{dlgForm}
+\alias{dlgForm.gui}
+\alias{dlgForm.textCLI}
+\alias{dlgForm.nativeGUI}
+
+\title{ A flexible form dialog box }
+\description{
+ Fill a form in a dialog box.
+}
+
+\usage{
+dlgForm(form, title = "Fill the form", message = NULL, columns = 1,
+ strip.type = TRUE, \dots, gui = .GUI)
+
+## These should not be called directly
+\method{dlgForm}{gui}(form, title = "Fill the form", message = NULL, columns = 1,
+ strip.type = TRUE, \dots, gui = .GUI)
+\method{dlgForm}{textCLI}(form, title = "Fill the form", message = NULL, columns = 1,
+ strip.type = TRUE, \dots, gui = .GUI)
+\method{dlgForm}{nativeGUI}(form, title = "Fill the form", message = NULL, columns = 1,
+ strip.type = TRUE, \dots, gui = .GUI)
+}
+
+\arguments{
+ \item{form}{ named list of default values, or list of possible items. Names
+ are the labels of corresponding entries in the dialog box, followed by an
+ indicator of the type of entry to place in the dialog box (see details). }
+ \item{title}{ the tile of the dialog box. }
+ \item{message}{ an optional message to display in the dialog box. Use
+ \code{\\n} for line break, or provide a vector of character strings, one for
+ each line. }
+ \item{columns}{ arrange the entries on this number of columns (by row). }
+ \item{strip.type}{ do we strip the type from the names in results. }
+ \item{\dots}{ pass further arguments to methods. }
+ \item{gui}{ the 'gui' object concerned by this dialog box. }
+}
+
+\details{
+ The form content is defined by a named list. Items are default values, or a
+ list of possible items, e.g., for the combobox. Names are labels displayed
+ in front of each field in the form. Follow them by a code that represents the
+ type of entry you want to use:
+
+ \code{:TXT} for simple (default) textual box,
+ \code{:H} for hidden text (password),
+ \code{:RO} for read-only text,
+ \code{:NUM} for null of positive integers with up/down arrows,
+ \code{:CHK} for checkbox: \code{TRUE} or \code{FALSE},
+ \code{:CB} for read-only combobox,
+ \code{:CBE} for editable combobox,
+ \code{:FL} to select one existing file,
+ \code{:MFL} to select multiple existing files,
+ \code{:SFL} to select or create one file,
+ \code{:DIR} to select a directory,
+ \code{:CDIR} to select or create a directory,
+ \code{:FN} to select font and font size,
+ \code{:DT} to enter a date,
+ \code{:CLR} to enter a RGB color,
+ \code{:BTN} to create a button that execute some code,
+ \code{:LBL} to add a label.
+}
+
+\value{
+ The modified 'gui' object is returned invisibly. The items entered by the
+ user, or an empty list if the dialog box was cancelled can be
+ obtained from \code{gui$res} (see example).
+}
+
+\author{Philippe Grosjean (\email{phgrosjean at sciviews.org})}
+
+\seealso{ \code{\link{dlgInput}} } %, \code{\link{dlgText}} }
+
+\examples{
+## Ask a series of items at once in a dialog box
+Form <- list(
+ "Name:TXT" = "John Smith",
+ "Age:NUM" = 25,
+ "Sex:CB" = c("male", "female"),
+ "Married:CHK"=FALSE
+)
+dlgForm(Form, "My form")$res
+
+}
+
+\keyword{ misc }
+
+\concept{ GUI API dialog boxes }
Property changes on: pkg/svDialogs/man/dlgForm.Rd
___________________________________________________________________
Added: svn:executable
+ *
More information about the Sciviews-commits
mailing list