/
gdroplist.R
291 lines (222 loc) · 9.27 KB
/
gdroplist.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
## Poor mans version of this, do it yourself version
## editable has entry widget that can be edited
setClass("gDroplisttcltk",
contains="gComponenttcltk",
prototype=prototype(new("gComponenttcltk"))
)
setMethod(".gdroplist",
signature(toolkit="guiWidgetsToolkittcltk"),
function(toolkit,
items, selected = 1, # use 0 for blank
editable=FALSE,
coerce.with = NULL,
handler=NULL, action=NULL,
container=NULL,
... # do.quote = TRUE for quote of answer
) {
force(toolkit)
if(is(container,"logical") && container)
container = gwindow()
if(!is(container,"guiWidget")) {
warning("Container is not correct. No NULL containers possible\n" )
return()
}
## items can be vector of items or data frame with
## one col: items
## two cols: items, icons
## three cols: items, icons, tooltip
## four or more cols: toolkit specific
if(inherits(items,"data.frame")) {
items <- items[,1, drop=TRUE]
}
## no icons in tcltk
## items must be a vector here
items = as.vector(items) # undoes factor
items = unique(items) # unique
theArgs = list(...)
## keep this, but don't advertise
if(!is.null(theArgs$do.quote)) {
coerce.with = function(x) paste("'",x,"'",sep="") # no space
}
if(editable)
state <- "normal"
else
state <- "readonly"
if(!is.null(theArgs$width))
width <- theArgs$width
else
width <- max(sapply(items,nchar)) + 5
tt <- getWidget(container)
gp <- ttkframe(tt)
cbVar <- tclVar()
cb <- ttkcombobox(gp,
values = as.character(items),
textvariable = cbVar,
width = width,
state = state)
tkgrid(cb,row=0, column=0, sticky="news")
tkgrid.columnconfigure(gp,0, weight=1)
obj = new("gDroplisttcltk",block=gp,widget=cb,
toolkit=toolkit,ID=getNewID(), e = new.env())
tag(obj,"coerce.with") <- coerce.with
tag(obj,"editable") <- editable
tag(obj,"tclVar") <- cbVar
tag(obj,"items") <- items
addDropTarget(obj, handler = function(h,...)
svalue(obj) <- h$dropdata)
add(container, obj, ...)
if(!is.null(theArgs$width))
size(obj) <- c(theArgs$width,0)
svalue(obj, index=TRUE) <- as.numeric(selected)
if (!is.null(handler)) {
id <- addhandlerchanged(obj, handler, action)
tag(obj, "handler.id") <- id
}
invisible(obj)
})
### methods
## value is for getting/setting the selected value
setMethod(".svalue",
signature(toolkit="guiWidgetsToolkittcltk",obj="gDroplisttcltk"),
function(obj, toolkit, index=NULL, drop=NULL, ...) {
widget <- getWidget(obj)
ind <- as.numeric(tclvalue(tcl(widget, "current"))) + 1 # 0-based
## if index
if(!is.null(index) && index) {
return(ind)
}
if(tag(obj,"editable")) {
val <- tclvalue(tcl(widget,"get"))
} else {
if(ind == 0) {
## no selection
return(NA)
}
## else get values from items -- not get to avoid conversion
items <- tag(obj,"items")
val <- items[ind]
}
## add in an as.numeric flag, getwidget when editable
theArgs = list(...) # deprecated
coerce.with = tag(obj, "coerce.with")
## do we coerce return value?
if(is.null(coerce.with))
return(val)
else if(is.function(coerce.with))
return(coerce.with(val))
else if(is.character(coerce.with))
return(do.call(coerce.with,list(val)))
else
return(val) # what else?
})
## set the displayed value to value
## if index=TRUE and value=0, seet to no state
setReplaceMethod(".svalue",
signature(toolkit="guiWidgetsToolkittcltk",obj="gDroplisttcltk"),
function(obj, toolkit, index=NULL, ..., value) {
## we can only handle vectors for value -- not data frame
## with value, label, icon info
theArgs = list(...)
widget <- getWidget(obj)
n <- length(obj)
if(n <= 1) return(obj)
if(is.null(index))
index <- FALSE
index <- as.logical(index)
## if editable do differently
## editable not implented
editable <- tag(obj,"editable")
## if index, set
if(index) {
if(value > 0 && value <= n)
tcl(widget,"current", as.numeric(value) - 1)
else # set to no state
tcl(widget,"set", "") # aka -1 for get
} else {
if(!is.null(editable) && editable) {
## editable
tclvalue(tcl(widget,"set",as.character(value)))
} else {
## not editable, check its there
vals <- tag(obj,"items")
if(value %in% vals) {
tclvalue(tcl(widget,"set",as.character(value)))
} else {
cat(sprintf("%s is not a valid item",value),"\n")
}
}
}
tkevent.generate(getWidget(obj),"<<ValueChanged>>")
return(obj)
})
setMethod("length",
signature(x="gDroplisttcltk"),
function(x) {
.length(x, x@toolkit)
})
setMethod(".length",
signature(toolkit="guiWidgetsToolkittcltk",x="gDroplisttcltk"),
function(x, toolkit) {
return(length(tag(x,"items")))
})
## the methods [ and [<- refer to the pre-defined values in the drop list.
## [
setMethod("[",
signature(x="gDroplisttcltk"),
function(x, i, j, ..., drop=TRUE) {
.leftBracket(x, x@toolkit, i, j, ..., drop=drop)
})
setMethod(".leftBracket",
signature(toolkit="guiWidgetsToolkittcltk",x="gDroplisttcltk"),
function(x, toolkit, i, j, ..., drop=TRUE) {
n = length(x) # no. items
if(n == 0)
return(NA)
items = tag(x,"items")
if(missing(i))
return(items)
else
return(items[i])
})
## replaces the values in droplist
## values is a vector of values -- not a dataframe
#set.values.gDropList = function(obj, values, ...) {
setReplaceMethod("[",
signature(x="gDroplisttcltk"),
function(x, i, j,..., value) {
.leftBracket(x, x@toolkit, i, j, ...) <- value
return(x)
})
setReplaceMethod(".leftBracket",
signature(toolkit="guiWidgetsToolkittcltk",x="gDroplisttcltk"),
function(x, toolkit, i, j, ..., value) {
if(is.data.frame(value))
value <- value[,1,drop=TRUE]
widget <- getWidget(x)
ind <- svalue(x, index=TRUE)
if(missing(i)) {
tcl(widget,"configure",values=value)
tag(x,"items") <- value
if(ind > 0)
svalue(x, index=TRUE) <- ind
} else {
items = x[]
items[i] <- value
x[] <- items ## recurse
}
return(x)
})
###################################################
### handlers
setMethod(".addhandlerchanged",
signature(toolkit="guiWidgetsToolkittcltk",obj="gDroplisttcltk"),
function(obj, toolkit, handler, action=NULL, ...) {
.addHandler(obj,toolkit,"<<ComboboxSelected>>",handler,action,...)
if(tag(obj,"editable"))
.addHandler(obj, toolkit, signal="<Return>", handler, action)
})
setMethod(".addhandlerclicked",
signature(toolkit="guiWidgetsToolkittcltk",obj="gDroplisttcltk"),
function(obj, toolkit, handler, action=NULL, ...) {
.addhandlerchanged(obj,toolkit, handler,action)
})