Debian Codegraph @

library/hierbox.tcl

raw
1 #
2 # hierbox.tcl
3 # ----------------------------------------------------------------------
4 # Bindings for the BLT hierbox widget
5 # ----------------------------------------------------------------------
6 # AUTHOR: George Howlett
7 # Bell Labs Innovations for Lucent Technologies
8 # [email protected]
9 # http://www.tcltk.com/blt
10 #
11 # RCS: $Id: hierbox.tcl,v 1.1.1.1 2009/05/09 16:27:21 pcmacdon Exp $
12 #
13 # ----------------------------------------------------------------------
14 # Copyright (c) 1998 Lucent Technologies, Inc.
15 # ======================================================================
16 #
17 # Permission to use, copy, modify, and distribute this software and its
18 # documentation for any purpose and without fee is hereby granted,
19 # provided that the above copyright notice appear in all copies and that
20 # both that the copyright notice and warranty disclaimer appear in
21 # supporting documentation, and that the names of Lucent Technologies
22 # any of their entities not be used in advertising or publicity
23 # pertaining to distribution of the software without specific, written
24 # prior permission.
25 #
26 # Lucent Technologies disclaims all warranties with regard to this
27 # software, including all implied warranties of merchantability and
28 # fitness. In no event shall Lucent be liable for any special, indirect
29 # or consequential damages or any damages whatsoever resulting from loss
30 # of use, data or profits, whether in an action of contract, negligence
31 # or other tortuous action, arising out of or in connection with the use
32 # or performance of this software.
33 #
34 # ======================================================================
35
36 array set bltHierbox {
37 afterId ""
38 scroll 0
39 space off
40 x 0
41 y 0
42 }
43
44 catch {
45 namespace eval blt::Hierbox {}
46 }
47
48 #
49 # ButtonPress assignments
50 #
51 # B1-Enter start auto-scrolling
52 # B1-Leave stop auto-scrolling
53 # ButtonPress-2 start scan
54 # B2-Motion adjust scan
55 # ButtonRelease-2 stop scan
56 #
57
58 bind Hierbox <ButtonPress-2> {
59 set bltHierbox(cursor) [%W cget -cursor]
60 %W configure -cursor hand1
61 %W scan mark %x %y
62 }
63
64 bind Hierbox <B2-Motion> {
65 %W scan dragto %x %y
66 }
67
68 bind Hierbox <ButtonRelease-2> {
69 %W configure -cursor $bltHierbox(cursor)
70 }
71
72 bind Hierbox <B1-Leave> {
73 if { $bltHierbox(scroll) } {
74 blt::Hierbox::AutoScroll %W
75 }
76 }
77
78 bind Hierbox <B1-Enter> {
79 after cancel $bltHierbox(afterId)
80 }
81
82
83 #
84 # KeyPress assignments
85 #
86 # Up
87 # Down
88 # Shift-Up
89 # Shift-Down
90 # Prior (PageUp)
91 # Next (PageDn)
92 # Left
93 # Right
94 # space Start selection toggle of entry currently with focus.
95 # Return Start selection toggle of entry currently with focus.
96 # Home
97 # End
98 # F1
99 # F2
100 # ASCII char Go to next open entry starting with character.
101 #
102 # KeyRelease
103 #
104 # space Stop selection toggle of entry currently with focus.
105 # Return Stop selection toggle of entry currently with focus.
106
107
108 bind Hierbox <KeyPress-Up> {
109 blt::Hierbox::MoveFocus %W up
110 if { $bltHierbox(space) } {
111 %W selection toggle focus
112 }
113 }
114
115 bind Hierbox <KeyPress-Down> {
116 blt::Hierbox::MoveFocus %W down
117 if { $bltHierbox(space) } {
118 %W selection toggle focus
119 }
120 }
121
122 bind Hierbox <Shift-KeyPress-Up> {
123 blt::Hierbox::MoveFocus %W prevsibling
124 }
125
126 bind Hierbox <Shift-KeyPress-Down> {
127 blt::Hierbox::MoveFocus %W nextsibling
128 }
129
130 bind Hierbox <KeyPress-Prior> {
131 blt::Hierbox::MovePage %W top
132 }
133
134 bind Hierbox <KeyPress-Next> {
135 blt::Hierbox::MovePage %W bottom
136 }
137
138 bind Hierbox <KeyPress-Left> {
139 %W close focus
140 }
141 bind Hierbox <KeyPress-Right> {
142 %W open focus
143 %W see focus -anchor w
144 }
145
146 bind Hierbox <KeyPress-space> {
147 blt::HierboxToggle %W focus
148 set bltHierbox(space) on
149 }
150
151 bind Hierbox <KeyRelease-space> {
152 set bltHierbox(space) off
153 }
154
155 bind Hierbox <KeyPress-Return> {
156 blt::HierboxToggle %W focus
157 set bltHierbox(space) on
158 }
159
160 bind Hierbox <KeyRelease-Return> {
161 set bltHierbox(space) off
162 }
163
164 bind Hierbox <KeyPress> {
165 blt::Hierbox::NextMatchingEntry %W %A
166 }
167
168 bind Hierbox <KeyPress-Home> {
169 blt::Hierbox::MoveFocus %W root
170 }
171
172 bind Hierbox <KeyPress-End> {
173 blt::Hierbox::MoveFocus %W end
174 }
175
176 bind Hierbox <KeyPress-F1> {
177 %W open -r root
178 }
179
180 bind Hierbox <KeyPress-F2> {
181 eval %W close -r [%W entry children root 0 end]
182 }
183
184 # ----------------------------------------------------------------------
185 # USAGE: blt::HierboxToggle <hierbox> <index>
186 # Arguments: hierbox hierarchy widget
187 #
188 # Invoked when the user presses the space bar. Toggles the selection
189 # for the entry at <index>.
190 # ----------------------------------------------------------------------
191 proc blt::HierboxToggle { widget index } {
192 switch -- [$widget cget -selectmode] {
193 single {
194 if { [$widget selection includes $index] } {
195 $widget selection clearall
196 } else {
197 $widget selection set $index
198 }
199 }
200 multiple {
201 $widget selection toggle $index
202 }
203 }
204 }
205
206
207 # ----------------------------------------------------------------------
208 # USAGE: blt::Hierbox::MovePage <hierbox> <where>
209 # Arguments: hierbox hierarchy widget
210 #
211 # Invoked by KeyPress bindings. Pages the current view up or down.
212 # The <where> argument should be either "top" or "bottom".
213 # ----------------------------------------------------------------------
214
215 proc blt::Hierbox::MovePage { widget where } {
216 # If the focus is already at the top/bottom of the window, we want
217 # to scroll a page. It's really one page minus an entry because we
218 # want to see the last entry on the next/last page.
219 if { [$widget index focus] == [$widget index view.$where] } {
220 if {$where == "top"} {
221 $widget yview scroll -1 pages
222 $widget yview scroll 1 units
223 } else {
224 $widget yview scroll 1 pages
225 $widget yview scroll -1 units
226 }
227 }
228 update
229
230 # Adjust the entry focus and the view. Also activate the entry.
231 # just in case the mouse point is not in the widget.
232 $widget entry highlight view.$where
233 $widget focus view.$where
234 $widget see view.$where
235 if { [$widget cget -selectmode] == "single" } {
236 $widget selection clearall
237 $widget selection set focus
238 }
239 }
240
241 #
242 # Edit mode assignments
243 #
244 # ButtonPress-3 Enables/disables edit mode on entry. Sets focus to
245 # entry.
246 #
247 # KeyPress
248 #
249 # Left Move insertion position to previous.
250 # Right Move insertion position to next.
251 # Up Move insertion position up one line.
252 # Down Move insertion position down one line.
253 # Return End edit mode.
254 # Shift-Return Line feed.
255 # Home Move to first position.
256 # End Move to last position.
257 # ASCII char Insert character left of insertion point.
258 # Del Delete character right of insertion point.
259 # Delete Delete character left of insertion point.
260 # Ctrl-X Cut
261 # Ctrl-V Copy
262 # Ctrl-P Paste
263 #
264 # KeyRelease
265 #
266 # ButtonPress-1 Start selection if in entry, otherwise clear selection.
267 # B1-Motion Extend/reduce selection.
268 # ButtonRelease-1 End selection if in entry, otherwise use last selection.
269 # B1-Enter Disabled.
270 # B1-Leave Disabled.
271 # ButtonPress-2 Same as above.
272 # B2-Motion Same as above.
273 # ButtonRelease-2 Same as above.
274 #
275 # All bindings in editting mode will "break" to override other bindings.
276 #
277 #
278
279 bind Hierbox <ButtonPress-3> {
280 set node [%W nearest %x %y]
281 %W entry insert $node @%x,%y ""
282 # %W entry insert $node 2 ""
283 }
284
285
286 proc blt::Hierbox::Init { widget } {
287 #
288 # Active entry bindings
289 #
290 $widget bind Entry <Enter> {
291 %W entry highlight current
292 }
293 $widget bind Entry <Leave> {
294 %W entry highlight ""
295 }
296
297 #
298 # Button bindings
299 #
300 $widget button bind all <ButtonRelease-1> {
301 %W see current
302 %W toggle current
303 }
304 $widget button bind all <Enter> {
305 %W button highlight current
306 }
307 $widget button bind all <Leave> {
308 %W button highlight ""
309 }
310
311 #
312 # ButtonPress-1
313 #
314 # Performs the following operations:
315 #
316 # 1. Clears the previous selection.
317 # 2. Selects the current entry.
318 # 3. Sets the focus to this entry.
319 # 4. Scrolls the entry into view.
320 # 5. Sets the selection anchor to this entry, just in case
321 # this is "multiple" mode.
322 #
323
324 $widget bind Entry <ButtonPress-1> {
325 blt::Hierbox::SetSelectionAnchor %W current
326 set bltHierbox(scroll) 1
327 }
328
329 $widget bin Entry <Double-ButtonPress-1> {
330 %W toggle current
331 }
332
333 #
334 # B1-Motion
335 #
336 # For "multiple" mode only. Saves the current location of the
337 # pointer for auto-scrolling.
338 #
339 $widget bind Entry <B1-Motion> {
340 set bltHierbox(x) %x
341 set bltHierbox(y) %y
342 set index [%W nearest %x %y]
343 if { [%W cget -selectmode] == "multiple" } {
344 %W selection mark $index
345 } else {
346 blt::Hierbox::SetSelectionAnchor %W $index
347 }
348 }
349
350 #
351 # ButtonRelease-1
352 #
353 # For "multiple" mode only.
354 #
355 $widget bind Entry <ButtonRelease-1> {
356 if { [%W cget -selectmode] == "multiple" } {
357 %W selection anchor current
358 }
359 after cancel $bltHierbox(afterId)
360 set bltHierbox(scroll) 0
361 }
362
363 #
364 # Shift-ButtonPress-1
365 #
366 # For "multiple" mode only.
367 #
368 $widget bind Entry <Shift-ButtonPress-1> {
369 if { [%W cget -selectmode] == "multiple" && [%W selection present] } {
370 if { [%W index anchor] == "" } {
371 %W selection anchor current
372 }
373 set index [%W index anchor]
374 %W selection clearall
375 %W selection set $index current
376 } else {
377 blt::Hierbox::SetSelectionAnchor %W current
378 }
379 }
380 $widget bind Entry <Shift-B1-Motion> {
381 # do nothing
382 }
383 $widget bind Entry <Shift-ButtonRelease-1> {
384 after cancel $bltHierbox(afterId)
385 set bltHierbox(scroll) 0
386 }
387
388 #
389 # Control-ButtonPress-1
390 #
391 # For "multiple" mode only.
392 #
393 $widget bind Entry <Control-ButtonPress-1> {
394 if { [%W cget -selectmode] == "multiple" } {
395 set index [%W index current]
396 %W selection toggle $index
397 %W selection anchor $index
398 } else {
399 blt::Hierbox::SetSelectionAnchor %W current
400 }
401 }
402 $widget bind Entry <Control-B1-Motion> {
403 # do nothing
404 }
405 $widget bind Entry <Control-ButtonRelease-1> {
406 after cancel $bltHierbox(afterId)
407 set bltHierbox(scroll) 0
408 }
409 #
410 # Control-Shift-ButtonPress-1
411 #
412 # For "multiple" mode only.
413 #
414 $widget bind Entry <Control-Shift-ButtonPress-1> {
415 if { [%W cget -selectmode] == "multiple" && [%W selection present] } {
416 if { [%W index anchor] == "" } {
417 %W selection anchor current
418 }
419 if { [%W selection includes anchor] } {
420 %W selection set anchor current
421 } else {
422 %W selection clear anchor current
423 %W selection set current
424 }
425 } else {
426 blt::Hierbox::SetSelectionAnchor %W current
427 }
428 }
429 $widget bind Entry <Control-Shift-B1-Motion> {
430 # do nothing
431 }
432 }
433
434
435 # ----------------------------------------------------------------------
436 # USAGE: blt::Hierbox::AutoScroll <hierbox>
437 #
438 # Invoked when the user is selecting elements in a hierbox widget
439 # and drags the mouse pointer outside of the widget. Scrolls the
440 # view in the direction of the pointer.
441 #
442 # Arguments: hierbox hierarchy widget
443 #
444 # ----------------------------------------------------------------------
445 proc blt::Hierbox::AutoScroll { widget } {
446 global bltHierbox
447 if { ![winfo exists $widget] } {
448 return
449 }
450 set x $bltHierbox(x)
451 set y $bltHierbox(y)
452 set index [$widget nearest $x $y]
453 if { $y >= [winfo height $widget] } {
454 $widget yview scroll 1 units
455 set neighbor down
456 } elseif { $y < 0 } {
457 $widget yview scroll -1 units
458 set neighbor up
459 } else {
460 set neighbor $index
461 }
462 if { [$widget cget -selectmode] == "single" } {
463 blt::Hierbox::SetSelectionAnchor $widget $neighbor
464 } else {
465 $widget selection mark $index
466 }
467 set bltHierbox(afterId) [after 10 blt::Hierbox::AutoScroll $widget]
468 }
469
470 proc blt::Hierbox::SetSelectionAnchor { widget index } {
471 set index [$widget index $index]
472 $widget selection clearall
473 $widget see $index
474 $widget focus $index
475 $widget selection set $index
476 $widget selection anchor $index
477 }
478
479
480 # ----------------------------------------------------------------------
481 # USAGE: blt::Hierbox::NextMatchingEntry <hierbox> <char>
482 # Arguments: hierbox hierarchy widget
483 #
484 # Invoked by KeyPress bindings. Searches for an entry that starts
485 # with the letter <char> and makes that entry active.
486 # ----------------------------------------------------------------------
487
488 proc blt::Hierbox::NextMatchingEntry { widget key } {
489 if {[string match {[ -~]} $key]} {
490 set last [$widget index focus]
491 set next [$widget index next]
492 while { $next != $last } {
493 set label [$widget entry cget $next -label]
494 if { [string index $label 0] == $key } {
495 break
496 }
497 set next [$widget index -at $next next]
498 }
499 $widget focus $next
500 if {[$widget cget -selectmode] == "single"} {
501 $widget selection clearall
502 $widget selection set focus
503 }
504 $widget see focus
505 }
506 }
507
508 # ----------------------------------------------------------------------
509 # USAGE: blt::Hierbox::MoveFocus <hierbox> <where>
510 #
511 # Invoked by KeyPress bindings. Moves the active selection to the
512 # entry <where>, which is an index such as "up", "down", "prevsibling",
513 # "nextsibling", etc.
514 # ----------------------------------------------------------------------
515 proc blt::Hierbox::MoveFocus { widget where } {
516 catch {$widget focus $where}
517 if { [$widget cget -selectmode] == "single" } {
518 $widget selection clearall
519 $widget selection set focus
520 }
521 $widget see focus
522 }