File: gui-sliderMenu.R

package info (click to toggle)
fbasics 4041.97-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,016 kB
  • sloc: ansic: 740; makefile: 14
file content (273 lines) | stat: -rw-r--r-- 9,194 bytes parent folder | download | duplicates (4)
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

# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Library General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Library General Public License for more details.
#
# You should have received A copy of the GNU Library General
# Public License along with this library; if not, write to the
# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA  02111-1307  USA


###############################################################################
# FUNCTION:                 SLIDER MENU:
#  .sliderMenu               Opens a teching demo slider menu
#  .tdSliderMenu             Opens a teching demo slider and button menu
###############################################################################


.slider.env = new.env()


# ------------------------------------------------------------------------------

.sliderMenu <- function(refresh.code, names, minima, maxima,
                        resolutions, starts,
                        title = "Slider", no = 0, set.no.value = 0)
{
  # A function implemented by Diethelm Wuertz
  
  # Description:
  #   Starts a slider menu
  
  # Source:
  #   Built on code written by Peter Wolf
  
  # FUNCTION:
  
  # Requirement:
  if (!requireNamespace("tcltk", quietly = TRUE))
    stop("Package 'tcltk' not available (should *not* happen, please report!)\n")
  
  # Environment:
  if (!exists(".slider.env")) {
    .slider.env <<- new.env()
  }
  if (no != 0) {
    options(show.error.messages = FALSE)
    ans <- as.numeric(tcltk::tclvalue(get(paste("slider", no, sep = ""),
                                          envir = .slider.env)))
    options(show.error.messages = TRUE)
    return(ans)
  }
  if (set.no.value[1] != 0) {
    try(eval(parse(text =
                     paste("tclvalue(slider", set.no.value[1],
                           ")<-", set.no.value[2], sep = "")),
             envir = .slider.env),
        silent = TRUE)
    return(set.no.value[2])
  }
  
  # Toplevel:
  nt = tcltk::tktoplevel()
  tcltk::tkwm.title(nt, title)
  
  
  # Slider:
  for (i in seq(names)) {
    eval(parse(text = paste("assign(\"slider", i, "\",
            tclVar(starts[i]), envir = .slider.env)", sep = "")))
    tcltk::tkpack(fr<-tcltk::tkframe(nt), anchor = "sw")
    lab = tcltk::tklabel(fr, text = names[i], anchor = "sw")
    sc = tcltk::tkscale(fr, command = refresh.code, from = minima[i],
                        to = maxima[i], showvalue = TRUE, resolution =
                          resolutions[i], orient = "horiz")
    assign("sc", sc, envir = .slider.env)
    tcltk::tkgrid(sc, lab)
    eval(parse(text = paste("tkconfigure(sc, variable = slider", i, ")",
                            sep = "")), envir = .slider.env)
  }
  tcltk::tkpack(fr<-tcltk::tkframe(nt), anchor = "sw")
  
  # Quit:
  quitButton = tcltk::tkbutton(fr, text = "   Quit   ",
                               command = function() {
                                 tcltk::tkdestroy(nt)
                               } )
  
  # Reset:
  resetButton = tcltk::tkbutton(fr, text = "   Start | Reset   ",
                                command = function() {
                                  for (i in seq(starts))
                                    eval(parse(text =
                                                 paste("tclvalue(slider", i, ")<-", starts[i], sep="")),
                                         envir = .slider.env)
                                  refresh.code()
                                }  )
  
  # Compose:
  tcltk::tkgrid(resetButton, quitButton, sticky = "sew")
}


# ------------------------------------------------------------------------------


.tdSliderMenu <-
  function(sl.functions, names, minima, maxima, resolutions, starts,
           but.functions, but.names, no, set.no.value, obj.name, obj.value,
           reset.function, title)
  {
    # A function implemented by Diethelm Wuertz
    
    # Description
    #   Opens a teching demo slider menu
    
    # Notes:
    #   Build on ideas and code from:
    #   R Package: TeachingDemos
    #   Title: Demonstrations for teaching and learning
    #   Version: 1.5
    #   Author: Greg Snow
    #   Description: This package is a set of demonstration functions
    #       that can be used in a classroom to demonstrate statistical
    #       concepts, or on your own to better understand the concepts
    #       or the programming.
    #   Maintainer: Greg Snow <greg.snow@intermountainmail.org>
    #   License: Artistic
    
    # FUNCTION:
    
    # Requirement:
    if (!requireNamespace("tcltk", quietly = TRUE))
      stop("Package 'tcltk' not available (should *not* happen, please report!)\n")
    
    # Setup:
    if(!missing(no)) {
      return(as.numeric(tcltk::tclvalue(get(paste(".tdSlider", no, sep=""),
                                            envir = .slider.env))))
    }
    if(!missing(set.no.value)){
      try(eval(parse(text=paste("tclvalue(.tdSlider", set.no.value[1],")<-",
                                set.no.value[2], sep = "")), envir = .slider.env))
      return(set.no.value[2])
    }
    if(!exists(".slider.env")) {
      .slider.env <<- new.env()
    }
    if(!missing(obj.name)){
      if(!missing(obj.value)) {
        assign(obj.name, obj.value, envir = .slider.env)
      } else {
        obj.value <- get(obj.name, envir = .slider.env)
      }
      return(obj.value)
    }
    if(missing(title)) {
      title = "Control Widget"
    }
    
    # GUI Settings:
    nt <- tcltk::tktoplevel()
    tcltk::tkwm.title(nt, title)
    tcltk::tkwm.geometry(nt, "+0+0")
    
    # Buttons:
    tcltk::tkpack(
      f.but <- tcltk::tkframe(nt), fill = "x")
    
    # Quit Button:
    quitCMD = function() {
      tcltk::tkdestroy(nt)
    }
    tcltk::tkpack(
      tcltk::tkbutton(f.but, text = "Quit", command = quitCMD, anchor = "sw"),
      side = "right",
      fill = "y")
    
    # Reset Button:
    if(missing(reset.function)) {
      reset.function <- function(...) print("relax")
    }
    if(!is.function(reset.function)) {
      reset.function<-eval(parse(text =
                                   paste("function(...){",reset.function,"}")))
    }
    resetCMD = function()
    {
      for(i in seq(names))
        eval(parse(text = paste("tclvalue(.tdSlider",i,")<-",
                                starts[i], sep = "")),
             envir = .slider.env)
      reset.function()
    }
    tcltk::tkpack(
      tcltk::tkbutton(f.but, text = "Reset", command = resetCMD, anchor = "sw"),
      side = "right",
      fill = "y")
    if (missing(but.names)) {
      but.names <- NULL
    }
    for (i in seq(but.names)) {
      but.fun <-
        if (length(but.functions) > 1)
          but.functions[[i]]
      else
        but.functions
      if (!is.function(but.fun)) {
        but.fun <-
          eval(parse(text = paste("function(...){", but.fun, "}")))
      }
      tcltk::tkpack(
        tcltk::tkbutton(f.but, text = but.names[i], command = but.fun,
                        anchor = "nw"),
        # side = "right",
        fill = "x"
      )
    }
    
    # Sliders:
    if(missing(names)) {
      names <- NULL
    }
    if(missing(sl.functions)) {
      sl.functions <- function(...){}
    }
    for(i in seq(names)){
      eval(parse(text = paste("assign('.tdSlider",i,"',
            tclVar(starts[i]), env = .slider.env)", sep = "")))
      tcltk::tkpack(fr <- tcltk::tkframe(nt))
      lab <- tcltk::tklabel(fr,
                            text = names[i],
                            anchor = "sw",
                            width = "35")
      sc <- tcltk::tkscale(fr,
                           from = minima[i],
                           to = maxima[i],
                           showvalue = TRUE,
                           resolution = resolutions[i],
                           orient = "horiz")
      tcltk::tkpack(lab,
                    sc,
                    anchor = "sw",
                    side = "right");
      assign("sc", sc, envir = .slider.env)
      
      eval(parse(text=paste("tkconfigure(sc,variable=.tdSlider",i,")",
                            sep="")), envir = .slider.env)
      sl.fun <-
        if(length(sl.functions)>1)
          sl.functions[[i]]
      else
        sl.functions
      if(!is.function(sl.fun))
        sl.fun<-eval(parse(text=paste("function(...){", sl.fun,"}")))
      
      tcltk::tkconfigure(sc, command = sl.fun)
    }
    assign("slider.values.old", starts, envir = .slider.env)
    
    # Return Value:
    invisible(nt)
  }


################################################################################