File: addingToolkit.Rnw

package info (click to toggle)
r-cran-gwidgets 0.0-54.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,360 kB
  • sloc: sh: 13; makefile: 2
file content (444 lines) | stat: -rw-r--r-- 13,086 bytes parent folder | download | duplicates (2)
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
\documentclass[12pt]{article}
\newcommand{\VERSION}{0.0-2}
%\VignetteIndexEntry{addingToolkit}
%\VignettePackage{addingToolkit}

\usepackage{times}              % for fonts
\usepackage[]{geometry}
\usepackage{mathptm}            % for math fonts type 1
\usepackage{graphicx}           % for graphics files
%%\usepackage{floatflt}           % for ``floating boxes''
%%\usepackage{index}
%%\usepackage{relsize}            % for relative size fonts
\usepackage{amsmath}            % for amslatex stuff
\usepackage{amsfonts}           % for amsfonts
\usepackage{url}                % for \url,
\usepackage{color}
%%\usepackage{fancyvrb}
%%\usepackage{fancyhdr}
%%\usepackage{jvfloatstyle}       % redefine float.sty for my style. Hack


%% squeeze in stuff
%%\floatstyle{jvstyle}
%%\restylefloat{table}
%%\restylefloat{figure}
%%\renewcommand\floatpagefraction{.9}
\renewcommand\topfraction{.9}
\renewcommand\bottomfraction{.9}
\renewcommand\textfraction{.1}
\setcounter{totalnumber}{50}
\setcounter{topnumber}{50}
\setcounter{bottomnumber}{50}

%% Fill these in
% \pagestyle{fancy}
% \usepackage{fancyhdr}
% \pagestyle{fancy}
% \fancyhf{}
% \fancyhead[L]{\RCode{gWidgets}}
% \fancyhead[C]{}
% \fancyhead[R]{\sectionmark}
% \fancyfoot[L]{}
% \fancyfoot[C]{- page \thepage\/ -}
% \fancyfoot[R]{}
% \renewcommand{\headrulewidth}{0.1pt}
% \renewcommand{\footrulewidth}{0.0pt}

%% My abbreviations
\newcommand{\RCode}[1]{\texttt{#1}}
\newcommand{\RFunc}[1]{\texttt{#1()}}
\newcommand{\RPackage}[1]{\textbf{#1}}
\newcommand{\RArg}[1]{\texttt{#1=}}
\newcommand{\RListel}[1]{\texttt{\$#1}}


\newenvironment{RArgs}{\begin{list}{}{}}{\end{list}}


\begin{document}
\thispagestyle{plain}
\title{Adding a toolkit to gWidgets}

\author{John Verzani, \url{gWidgetsRGtk@gmail.com}}
\maketitle

\section*{Abstract:}
[\textbf{This package is now out of date. The \RPackage{gWidgetstcltk} package
has been written. This is here in case someone wants to port a
different toolkit (RwxWidgets say).}]
\\

This little vignette illustrates what is required to write a toolkit
for the \RPackage{gWidgets} package. Since the \RPackage{gWidgetsRGtk}
package is written this sketches out what a toolkit would possibly
look like using the \RPackage{tcltk} package.

\setcounter{tocdepth}{3}
\tableofcontents

\section{Basics of gWidgets}

The gWidgets implementation is simply a set of functions that dispatch
to similarly named functions in a toolkit. That is the
\RCode{glabel(...,toolkit=guiToolkit())} function dispatches to the
\RCode{.glabel(toolkit, ...)} function in the appropriate toolkit, and
the \RCode{svalue(obj,...)} method dispatches to the
\RCode{.svalue(obj@widget,obj@toolkit, ...)} function in the
appropriate toolkit. In the first case, a constructor, the dispatch is
done by the class of the toolkit. For the method, the dispatch is
based on both the toolkit and the class of the object, and perhaps
other arguments in the signature of the method.

The classes for the toolkits \RCode{RGtk2}, \RCode{tcltk}, \RCode{rJava}, and \RCode{SJava} are already defined by gWidgets. These are named \RCode{guiWidgetsToolkit} plus the package name.


As such, the basic structure of a gWidgets implementation is to set up
some classes, and a set of methods for dispatch.~\footnote{Although it
likely wasn't necessary, at the time of first writing a package, the
dispatch was to a "dot" file. This caused an extra level to the
dispatch, that while unfortunate, does not seem to be worth rewriting
to avoid.}


\section{An example}


This shows some of what is necessary to write an implementation of
gWidgets for the \RPackage{tcltk} package.  It only assumes as much
about \RPackage{tcltk} as was learned by browsing Peter Dalgaard's
informative RNews article and a quick glance at the examples provided
by James Wettenhall.




First we load the package.
\begin{Scode}
  ## load toolkit 
  options("guiToolkit"=NA)
  library(gWidgets)
  library(tcltk)
\end{Scode}

The options setting ensures no toolkit for gWidgets gets loaded.

We note the subclass of \RCode{guiWidgetsToolkit},
\RCode{guiWidgetsToolkittcltk} is already defined in \RCode{gWidgets}.

Now we make a base class for the tcltk widgets created here.
\begin{Scode}
  ## some classes
  setClass("gWidgetTcltk")
  ## A virtual class to hold tcltk object or guiWidget or gWidgetTcltk
  setClass("guiWidgetORgWidgetTcltkORtcltk")
  setIs("guiWidget","guiWidgetORgWidgetTcltkORtcltk")
  setIs("gWidgetTcltk","guiWidgetORgWidgetTcltkORtcltk")
\end{Scode}

Finally, we promote the \RCode{tkwin} class to an S4 class and add it
to our virtual class. This would be done for all possible classes of
\RCode{tcltk} objects.

\begin{Scode}
  oldclasses = c("tkwin")
  for(i in oldclasses) {
    setOldClass(i)
    setIs(i,"guiWidgetORgWidgetTcltkORtcltk")
  }
\end{Scode}


The \RCode{gWidgetTcltk} class is a virtual class, here are two
subclasses. We create slots for the widget and the toolkit here, but
perhaps should add others such as an ID slot for storing a unique ID
per widget.

\begin{Scode}
### Make some base classes
setClass("gComponentTcltk",
representation(
widget="guiWidgetORgWidgetTcltkORtcltk",
toolkit="guiWidgetsToolkit"
),
contains="gWidgetTcltk",
)
setClass("gContainerTcltk",
representation(
widget="guiWidgetORgWidgetTcltkORtcltk",
toolkit="guiWidgetsToolkit"
),
contains="gWidgetTcltk",
)
\end{Scode}

Now we define some necessary functions to implement \RFunc{gwindow} in
the toolkit. This involves defining a class, making a constructor
(\RFunc{.gwindow}) and defining some methods.

\begin{Scode}
## top level window
setClass("gWindowTcltk",
contains="gContainerTcltk",
prototype=prototype(new("gContainerTcltk"))
)
\end{Scode}

This implementation of the constructor should have a handler for the
window destroy event.
\begin{Scode}
  setMethod(".gwindow",
  signature(toolkit="guiWidgetsToolkittcltk"),
  function(toolkit,
  title="Window", visible=TRUE,
  handler=NULL, action = NULL,
  ...
  ) {
    win <- tktoplevel()
    tktitle(win) <- title
    
    obj = new("gWindowTcltk", widget=win, toolkit=toolkit)
    return(obj)
  })
\end{Scode}

The \RFunc{svalue} method for \RFunc{gwindow} objects is used to
retrieve and set the title of the window. 

\begin{Scode}
setMethod(".svalue",
signature(toolkit="guiWidgetsToolkittcltk",obj="gWindowTcltk"),
function(obj, toolkit, index=NULL, drop=NULL, ..) {
  tktitle(obj@widget)
})

setMethod(".svalue<-",
signature(toolkit="guiWidgetsToolkittcltk",obj="gWindowTcltk"),
function(obj, toolkit, index=NULL,..., value) {
  ## set the title
  tktitle(obj@widget) <- value
  return(obj)
})
\end{Scode}

The \RFunc{add} method is used to add a widget to a container. This is
where we run into problems with \RPackage{tcltk} as the constructors
there require a ``parent'' container at the time of construction. As
such, we don't have both a container (\RCode{obj} below) and  widget
(\RCode{value}) needed when we add, rather we only specify how things
are packed in.

\begin{Scode}
setMethod(".add",
signature(toolkit="guiWidgetsToolkittcltk",obj="gWindowTcltk",
value="guiWidget"),
function(obj, toolkit, value, ...) {
  ## how to add?
  tkpack(value@widget@widget)
})
\end{Scode}

[To avoid this, the \RPackage{gWidgetstcltk} package requires a
container be specified when a widget is constructed. This container
stores the top-level container so that a \RPackage{tcltk} widget can
be constructed. The \RCode{add} method is then rarely used publicly,
but is useful when writing the constructor. It's
arguments for placement of the widget are specified during the
construction of the widget.]

The \RCode{dispose} method closes the window
\begin{Scode}
setMethod(".dispose",
          signature(toolkit="guiWidgetsToolkittcltk",obj="gWindowTcltk"),
          function(obj, toolkit, ...) {
            tkdestroy(obj@widget)
          })
\end{Scode}

Below we implement the basics of \RFunc{glabel}. No attempt is made to
add a click handler to this or editing or markup. For now, just
setting of text in a label.

First a class
\begin{Scode}
###########
## label class
setClass("gLabelTcltk",
contains="gComponentTcltk",
prototype=prototype(new("gComponentTcltk"))
)
\end{Scode}


Next the constructor
\begin{Scode}
  ## constructor
  setMethod(".glabel",
  signature(toolkit="guiWidgetsToolkittcltk"),
  function(toolkit,
  text= "", markup = FALSE, editable = FALSE, handler = NULL, 
  action = NULL, container = NULL, 
  ...
  ) {
    
    ## if container is non null, we can evaluate expression
    if(is.null(container)) {
      cat("Can't have an NULL container with tcltk")
    }
    ## find tk container
    if(is(container,"guiWidget")) container=container@widget
    if(is(container,"gContainerTcltk")) container=container@widget
    
    label = tklabel(container, text=text)
    
    obj = new("gLabelTcltk",widget=label, toolkit=toolkit)
    
    ## pack into container
    tkpack(label)
    
    ## add callback
    ## no callbacks for labels
    return(obj)
  })
\end{Scode}

The \RFunc{svalue} method returns the label text, it requires a little
tcltk voodoo.

\begin{Scode}
  setMethod(".svalue",
  signature(toolkit="guiWidgetsToolkittcltk",obj="gLabelTcltk"),
  function(obj, toolkit, index=NULL, drop=NULL, ..) {
    as.character(tkcget(obj@widget,"-text"))
  })
  
  ## svalue<-
  setReplaceMethod(".svalue",
  signature(toolkit="guiWidgetsToolkittcltk",obj="gLabelTcltk"),
  function(obj, toolkit, index=NULL, ..., value) {
    ## set the text
    tkconfigure(obj@widget, text=value)
    return(obj)
  })
\end{Scode}

For the \RFunc{gbutton} implementation we show how to add a handler in
addition to implementing the \RFunc{svalue<-} method.

\begin{Scode}
  ### button class
  setClass("gButtonTcltk",
  contains="gComponentTcltk",
  prototype=prototype(new("gComponentTcltk"))
  )
\end{Scode}

As for the constructor we have:
\begin{Scode}
  setMethod(".gbutton",
  signature(toolkit="guiWidgetsToolkittcltk"),
  function(toolkit,
  text="", border=TRUE, handler=NULL, action=NULL, container=NULL,...
  ) {
    
    if(!is.null(container)) {
      topwin = container@widget@widget
    } else {
      topwin = gwindow(toolkit=toolkit)@widget
    }
    
    button = tkbutton(topwin, text=text)
    
    obj = new("gButtonTcltk",widget=button, toolkit=toolkit)
  
    tkpack(obj@widget)
    
    if(!is.null(handler))
    .addhandlerclicked(obj, toolkit, handler=handler)
    
    return(obj)
    
  })
\end{Scode}
In dealing with the handler, we used the private method defined below,
rather than \RFunc{addHandlerClicked} as that method is for objects of
class \RCode{guiWidget}, and not \RCode{gWidgetTcltk}. This awkwardness
can be avoided by defining a method \RCode{addHandlerClicked} for
objects of class \RCode{gWidgetTcltk} within the
toolkit.~\footnote{This is a result of the way dispatch was
  designed. The toolkit information is stored in a slot separate from
  the widget provided by the toolkit in the gWidget object. Dispatch
  occurs on both the object and the toolkit. The method with these
  signatures is the ``dot'' one implemented in the toolkit package.} For instance,

\begin{Scode}
setMethod("addHandlerClicked",signature(obj="gWidgetTcltk"),
          function(obj, handler=NULL, action=NULL, ...) {
            .addhandlerclicked(obj, obj@toolkit,handler, action, ...)
          })
\end{Scode}
(The internal function, \RCode{.addhandlerclicked}, uses lower case
letters for now.)

The handler could be written as follows
\begin{Scode}
setMethod(".addhandlerclicked",
          signature(toolkit="guiWidgetsToolkittcltk",obj="gWidgettcltk"),
          function(obj, toolkit,
                   handler, action=NULL, ...) {
                     tkbind(getWidget(obj),<Button-1>,
                     function(...) {
                       h = list(ref=obj, obj=obj, action=action)
                       handler(h,...)
                     })
})
\end{Scode}



Again, \RFunc{svalue} should retrieve the text and \RFunc{svalue<-}
should set the text. Again, a little tcltk voodoo is used.

\begin{Scode}
  setMethod(".svalue",
  signature(toolkit="guiWidgetsToolkittcltk",obj="gButtonTcltk"),
  function(obj, toolkit, index=NULL, drop=NULL, ...) {
    val = paste(as.character(tkcget(obj@widget,"-text")))
    return(val)
  })
  
  setReplaceMethod(".svalue",
  signature(toolkit="guiWidgetsToolkittcltk",obj="gButtonTcltk"),
  function(obj, toolkit, index=NULL, ..., value) {
    tkconfigure(obj@widget, text=value)
    return(obj)
  })
\end{Scode}


\begin{figure}[htbp]
  \centering
  \includegraphics[width=.4\textwidth]{helloWorld}
  \caption{Hello world, how are you?}
  \label{fig:hello-world}
\end{figure}




Well, that will let us make the following simple dialog
(Figure~\ref{fig:hello-world}).


\begin{Scode}
  guitoolkit = new("guiWidgetsToolkittcltk")
  win = gwindow("Hello world", toolkit=guitoolkit)
  label=glabel("Hello world, how are you?", container=win, toolkit=guitoolkit)
  button=gbutton("Close", handler=function(h,...) dispose(win),
  container=win, toolkit=guitoolkit)
\end{Scode}




\end{document}