1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: mainwin - main application window for Rappture |
---|
3 | # |
---|
4 | # This widget acts as the main window for a Rappture application. |
---|
5 | # It can be configured to run in two modes: 1) normal desktop |
---|
6 | # application, and 2) web-based application. In web-based mode, |
---|
7 | # the application window runs inside a VNC window, and it takes |
---|
8 | # the full screen and blends in with the web page. |
---|
9 | # ====================================================================== |
---|
10 | # AUTHOR: Michael McLennan, Purdue University |
---|
11 | # Copyright (c) 2004-2005 |
---|
12 | # Purdue Research Foundation, West Lafayette, IN |
---|
13 | # ====================================================================== |
---|
14 | package require Itk |
---|
15 | package require BLT |
---|
16 | |
---|
17 | option add *MainWin.mode desktop widgetDefault |
---|
18 | option add *MainWin.borderWidth 0 widgetDefault |
---|
19 | option add *MainWin.relief raised widgetDefault |
---|
20 | option add *MainWin.anchor center widgetDefault |
---|
21 | option add *MainWin.titleFont \ |
---|
22 | -*-helvetica-bold-o-normal-*-*-140-* widgetDefault |
---|
23 | |
---|
24 | itcl::class Rappture::MainWin { |
---|
25 | inherit itk::Toplevel |
---|
26 | |
---|
27 | itk_option define -mode mode Mode "" |
---|
28 | itk_option define -anchor anchor Anchor "center" |
---|
29 | itk_option define -bgscript bgScript BgScript "" |
---|
30 | |
---|
31 | constructor {args} { # defined below } |
---|
32 | |
---|
33 | public method draw {option args} |
---|
34 | public method syncCutBuffer {option args} |
---|
35 | |
---|
36 | protected method _redraw {} |
---|
37 | |
---|
38 | private variable _contents "" ;# frame containing app |
---|
39 | private variable _sync ;# to sync current selection and cut buffer |
---|
40 | private variable _bgscript "" ;# script of background drawing cmds |
---|
41 | private variable _bgparser "" ;# parser for bgscript |
---|
42 | } |
---|
43 | |
---|
44 | itk::usual MainWin { |
---|
45 | keep -background -cursor foreground -font |
---|
46 | } |
---|
47 | |
---|
48 | # ---------------------------------------------------------------------- |
---|
49 | # CONSTRUCTOR |
---|
50 | # ---------------------------------------------------------------------- |
---|
51 | itcl::body Rappture::MainWin::constructor {args} { |
---|
52 | itk_component add area { |
---|
53 | canvas $itk_interior.area |
---|
54 | } { |
---|
55 | usual |
---|
56 | rename -background -bgcolor bgColor Background |
---|
57 | } |
---|
58 | pack $itk_component(area) -expand yes -fill both |
---|
59 | bind $itk_component(area) <Configure> [itcl::code $this _redraw] |
---|
60 | |
---|
61 | itk_component add app { |
---|
62 | frame $itk_component(area).app |
---|
63 | } { |
---|
64 | usual |
---|
65 | keep -borderwidth -relief |
---|
66 | } |
---|
67 | bind $itk_component(app) <Configure> " |
---|
68 | after cancel [itcl::code $this _redraw] |
---|
69 | after idle [itcl::code $this _redraw] |
---|
70 | " |
---|
71 | |
---|
72 | itk_component add menu { |
---|
73 | menu $itk_interior.menu |
---|
74 | } |
---|
75 | itk_component add filemenu { |
---|
76 | menu $itk_component(menu).file |
---|
77 | } |
---|
78 | $itk_component(menu) add cascade -label "File" -underline 0 \ |
---|
79 | -menu $itk_component(filemenu) |
---|
80 | $itk_component(filemenu) add command -label "Exit" -underline 1 \ |
---|
81 | -command exit |
---|
82 | |
---|
83 | # |
---|
84 | # Create a parser for the -bgscript option that can |
---|
85 | # execute drawing commands on the canvas. This allows |
---|
86 | # us to draw a background that blends in with web pages. |
---|
87 | # |
---|
88 | set _bgparser [interp create -safe] |
---|
89 | $_bgparser alias rectangle [itcl::code $this draw rectangle] |
---|
90 | $_bgparser alias oval [itcl::code $this draw oval] |
---|
91 | $_bgparser alias line [itcl::code $this draw line] |
---|
92 | $_bgparser alias polygon [itcl::code $this draw polygon] |
---|
93 | $_bgparser alias text [itcl::code $this draw text] |
---|
94 | $_bgparser alias image [itcl::code $this draw image] |
---|
95 | |
---|
96 | eval itk_initialize $args |
---|
97 | |
---|
98 | bind RapptureMainWin <Destroy> { exit } |
---|
99 | set btags [bindtags $itk_component(hull)] |
---|
100 | bindtags $itk_component(hull) [lappend btags RapptureMainWin] |
---|
101 | |
---|
102 | set _sync(cutbuffer) "" |
---|
103 | set _sync(selection) "" |
---|
104 | syncCutBuffer ifneeded |
---|
105 | } |
---|
106 | |
---|
107 | # ---------------------------------------------------------------------- |
---|
108 | # USAGE: syncCutBuffer ifneeded |
---|
109 | # USAGE: syncCutBuffer transfer <offset> <maxchars> |
---|
110 | # USAGE: syncCutBuffer lostselection |
---|
111 | # |
---|
112 | # Invoked automatically whenever the mouse pointer enters or leaves |
---|
113 | # a main window to sync the cut buffer with the primary selection. |
---|
114 | # This helps applications work properly with the "Copy/Paste with |
---|
115 | # Desktop" option on the VNC applet for the nanoHUB. |
---|
116 | # |
---|
117 | # The "ifneeded" option syncs the cutbuffer and the primary selection |
---|
118 | # if either one has new data. |
---|
119 | # |
---|
120 | # The "fromselection" option syncs from the primary selection to the |
---|
121 | # cut buffer. If there's a primary selection, it gets copied to the |
---|
122 | # cut buffer. |
---|
123 | # ---------------------------------------------------------------------- |
---|
124 | itcl::body Rappture::MainWin::syncCutBuffer {option args} { |
---|
125 | set mainwin $itk_component(hull) |
---|
126 | switch -- $option { |
---|
127 | ifneeded { |
---|
128 | # |
---|
129 | # See if the incoming cut buffer has changed. |
---|
130 | # If so, then sync the new input to the primary selection. |
---|
131 | # |
---|
132 | set s [blt::cutbuffer get] |
---|
133 | if {"" != $s && ![string equal $s $_sync(cutbuffer)]} { |
---|
134 | set _sync(cutbuffer) $s |
---|
135 | |
---|
136 | if {![string equal $s $_sync(selection)] |
---|
137 | && [selection own -selection PRIMARY] != $mainwin} { |
---|
138 | set _sync(selection) $s |
---|
139 | |
---|
140 | clipboard clear |
---|
141 | clipboard append -- $s |
---|
142 | selection handle -selection PRIMARY $mainwin \ |
---|
143 | [itcl::code $this syncCutBuffer transfer] |
---|
144 | selection own -selection PRIMARY -command \ |
---|
145 | [itcl::code $this syncCutBuffer lostselection] \ |
---|
146 | $mainwin |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | # |
---|
151 | # See if the selection has changed. If so, then sync |
---|
152 | # the new input to the cut buffer, so it's available |
---|
153 | # outside the VNC client. |
---|
154 | # |
---|
155 | set s "" |
---|
156 | if {[catch {selection get -selection PRIMARY} s] || "" == $s} { |
---|
157 | if {[catch {clipboard get} s]} { |
---|
158 | set s "" |
---|
159 | } |
---|
160 | } |
---|
161 | if {"" != $s && ![string equal $s $_sync(selection)]} { |
---|
162 | set _sync(selection) $s |
---|
163 | blt::cutbuffer set $s |
---|
164 | } |
---|
165 | |
---|
166 | # do this again soon |
---|
167 | after 1000 [itcl::code $this syncCutBuffer ifneeded] |
---|
168 | } |
---|
169 | transfer { |
---|
170 | if {[llength $args] != 2} { |
---|
171 | error "wrong # args: should be \"syncCutBuffer transfer offset max\"" |
---|
172 | } |
---|
173 | set offset [lindex $args 0] |
---|
174 | set maxchars [lindex $args 1] |
---|
175 | return [string range $_currseln $offset [expr {$offset+$maxchars-1}]] |
---|
176 | } |
---|
177 | lostselection { |
---|
178 | # nothing to do |
---|
179 | } |
---|
180 | default { |
---|
181 | error "bad option \"$option\": should be ifneeded, transfer, or lostselection" |
---|
182 | } |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | # ---------------------------------------------------------------------- |
---|
187 | # USAGE: draw <option> ?<arg> <arg>...? |
---|
188 | # |
---|
189 | # Used by the -bgscript to draw items in the background area behind |
---|
190 | # the app when "-mode web" is active. This allows an application |
---|
191 | # to create a background that blends seamlessly with the underlying |
---|
192 | # web page. |
---|
193 | # ---------------------------------------------------------------------- |
---|
194 | itcl::body Rappture::MainWin::draw {option args} { |
---|
195 | set w $itk_component(hull) |
---|
196 | regsub -all {<w>} $args [winfo screenwidth $w] args |
---|
197 | regsub -all {<h>} $args [winfo screenheight $w] args |
---|
198 | eval $itk_component(area) create $option $args |
---|
199 | } |
---|
200 | |
---|
201 | # ---------------------------------------------------------------------- |
---|
202 | # USAGE: _redraw |
---|
203 | # |
---|
204 | # Used internally to redraw the widget whenever it changes size. |
---|
205 | # This matters only when "-mode web" is active, when the background |
---|
206 | # area is actually visible. |
---|
207 | # ---------------------------------------------------------------------- |
---|
208 | itcl::body Rappture::MainWin::_redraw {} { |
---|
209 | $itk_component(area) delete all |
---|
210 | if {$itk_option(-mode) == "web"} { |
---|
211 | if {[catch {$_bgparser eval $itk_option(-bgscript)} result]} { |
---|
212 | bgerror "$result\n (while redrawing application background)" |
---|
213 | } |
---|
214 | |
---|
215 | set bd 0 ;# optional border |
---|
216 | set sw [winfo width $itk_component(area)] |
---|
217 | set sh [winfo height $itk_component(area)] |
---|
218 | |
---|
219 | set clip 0 |
---|
220 | set w [winfo reqwidth $itk_component(app)] |
---|
221 | set h [winfo reqheight $itk_component(app)] |
---|
222 | if {$w > $sw-2*$bd} { |
---|
223 | set $w [expr {$sw-2*$bd}] |
---|
224 | set clip 1 |
---|
225 | } |
---|
226 | |
---|
227 | switch -- $itk_option(-anchor) { |
---|
228 | n { |
---|
229 | set x [expr {$sw/2}] |
---|
230 | set y $bd |
---|
231 | } |
---|
232 | s { |
---|
233 | set x [expr {$sw/2}] |
---|
234 | set y [expr {$sh-$bd}] |
---|
235 | } |
---|
236 | center { |
---|
237 | set x [expr {$sw/2}] |
---|
238 | set y [expr {$sh/2}] |
---|
239 | } |
---|
240 | w { |
---|
241 | set x $bd |
---|
242 | set y [expr {$sh/2}] |
---|
243 | } |
---|
244 | e { |
---|
245 | set x [expr {$sw-$bd}] |
---|
246 | set y [expr {$sh/2}] |
---|
247 | } |
---|
248 | nw { |
---|
249 | set x $bd |
---|
250 | set y $bd |
---|
251 | } |
---|
252 | ne { |
---|
253 | set x [expr {$sw-$bd}] |
---|
254 | set y $bd |
---|
255 | } |
---|
256 | sw { |
---|
257 | set x $bd |
---|
258 | set y [expr {$sh-$bd}] |
---|
259 | } |
---|
260 | se { |
---|
261 | set x [expr {$sw-$bd}] |
---|
262 | set y [expr {$sh-$bd}] |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | # if the app is too big, use w/h. otherwise, 0,0 for default size |
---|
267 | if {!$clip} { |
---|
268 | set w 0 |
---|
269 | set h 0 |
---|
270 | } |
---|
271 | |
---|
272 | $itk_component(area) create window $x $y \ |
---|
273 | -anchor $itk_option(-anchor) -window $itk_component(app) \ |
---|
274 | -width $w -height $h |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | # ---------------------------------------------------------------------- |
---|
279 | # OPTION: -mode |
---|
280 | # ---------------------------------------------------------------------- |
---|
281 | itcl::configbody Rappture::MainWin::mode { |
---|
282 | switch -- $itk_option(-mode) { |
---|
283 | desktop { |
---|
284 | component hull configure -menu $itk_component(hull).menu |
---|
285 | pack $itk_component(app) -expand yes -fill both |
---|
286 | wm geometry $itk_component(hull) "" |
---|
287 | } |
---|
288 | web { |
---|
289 | component hull configure -menu "" |
---|
290 | pack forget $itk_component(app) |
---|
291 | set wx [winfo screenwidth $itk_component(hull)] |
---|
292 | set wy [winfo screenheight $itk_component(hull)] |
---|
293 | wm geometry $itk_component(hull) ${wx}x${wy}+0+0 |
---|
294 | _redraw |
---|
295 | } |
---|
296 | default { |
---|
297 | error "bad value \"$itk_option(-mode)\": should be desktop or web" |
---|
298 | } |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | # ---------------------------------------------------------------------- |
---|
303 | # OPTION: -bgscript |
---|
304 | # ---------------------------------------------------------------------- |
---|
305 | itcl::configbody Rappture::MainWin::bgscript { |
---|
306 | _redraw |
---|
307 | } |
---|
308 | |
---|
309 | # ---------------------------------------------------------------------- |
---|
310 | # OPTION: -anchor |
---|
311 | # ---------------------------------------------------------------------- |
---|
312 | itcl::configbody Rappture::MainWin::anchor { |
---|
313 | if {[lsearch {n s e w ne nw se sw center} $itk_option(-anchor)] < 0} { |
---|
314 | error "bad anchor \"$itk_option(-anchor)\"" |
---|
315 | } |
---|
316 | _redraw |
---|
317 | } |
---|