Debian Codegraph @

library/treeview.tcl

raw
1
2 # ======================================================================
3 #
4 # treeview.tcl
5 #
6 # ----------------------------------------------------------------------
7 # Bindings for the BLT treeview widget
8 # ----------------------------------------------------------------------
9 #
10 # AUTHOR: George Howlett
11 # Bell Labs Innovations for Lucent Technologies
12 # [email protected]
13 # http://www.tcltk.com/blt
14 #
15 # RCS: $Id: treeview.tcl,v 1.24 2010/05/06 22:26:17 pcmacdon Exp $
16 #
17 # ----------------------------------------------------------------------
18 # Copyright (c) 1998 Lucent Technologies, Inc.
19 # ----------------------------------------------------------------------
20 #
21 # Permission to use, copy, modify, and distribute this software and its
22 # documentation for any purpose and without fee is hereby granted,
23 # provided that the above copyright notice appear in all copies and that
24 # both that the copyright notice and warranty disclaimer appear in
25 # supporting documentation, and that the names of Lucent Technologies
26 # any of their entities not be used in advertising or publicity
27 # pertaining to distribution of the software without specific, written
28 # prior permission.
29 #
30 # Lucent Technologies disclaims all warranties with regard to this
31 # software, including all implied warranties of merchantability and
32 # fitness. In no event shall Lucent be liable for any special, indirect
33 # or consequential damages or any damages whatsoever resulting from loss
34 # of use, data or profits, whether in an action of contract, negligence
35 # or other tortuous action, arising out of or in connection with the use
36 # or performance of this software.
37 #
38 # ======================================================================
39
40 namespace eval ::blt::tv {
41 variable afterId ""
42 variable scroll 0
43 variable column ""
44 variable space off
45 variable x 0
46 variable y 0
47 variable script [info script]
48 variable dirname [file dirname [info script]]
49 if {[info exists ::tcl_warn(level)] && $::tcl_warn(level)} {
50 source [file join $dirname tvutil.tcl]
51 } else {
52 set ::auto_index(::blt::tv::TableWid) [list source [file join $dirname tvutil.tcl]]
53 set ::auto_index(::blt::tv::TableLoad) [list source [file join $dirname tvutil.tcl]]
54 set ::auto_index(::blt::tv::TreeLoad) [list source [file join $dirname tvutil.tcl]]
55 set ::auto_index(::blt::tv::TreeDump) [list source [file join $dirname tvutil.tcl]]
56 set ::auto_index(::blt::tv::TreeFill) [list source [file join $dirname tvutil.tcl]]
57 }
58 }
59
60
61 image create photo blt::tv::normalCloseFolder -format gif -data {
62 R0lGODlhEAANAMIAAAAAAH9/f///////AL+/vwAA/wAAAAAAACH5BAEAAAUALAAAAAAQAA0A
63 AAM8WBrM+rAEQWmIb5KxiWjNInCkV32AJHRlGQBgDA7vdN4vUa8tC78qlrCWmvRKsJTquHkp
64 ZTKAsiCtWq0JADs=
65 }
66 image create photo blt::tv::normalOpenFolder -format gif -data {
67 R0lGODlhEAANAMIAAAAAAH9/f///////AL+/vwAA/wAAAAAAACH5BAEAAAUALAAAAAAQAA0A
68 AAM1WBrM+rAEMigJ8c3Kb3OSII6kGABhp1JnaK1VGwjwKwtvHqNzzd263M3H4n2OH1QBwGw6
69 nQkAOw==
70 }
71 image create photo blt::tv::activeCloseFolder -format gif -data {
72 R0lGODlhEAANAMIAAAAAAH9/f/////+/AL+/vwAA/wAAAAAAACH5BAEAAAUALAAAAAAQAA0A
73 AAM8WBrM+rAEQWmIb5KxiWjNInCkV32AJHRlGQBgDA7vdN4vUa8tC78qlrCWmvRKsJTquHkp
74 ZTKAsiCtWq0JADs=
75 }
76 image create photo blt::tv::activeOpenFolder -format gif -data {
77 R0lGODlhEAANAMIAAAAAAH9/f/////+/AL+/vwAA/wAAAAAAACH5BAEAAAUALAAAAAAQAA0A
78 AAM1WBrM+rAEMigJ8c3Kb3OSII6kGABhp1JnaK1VGwjwKwtvHqNzzd263M3H4n2OH1QBwGw6
79 nQkAOw==
80 }
81
82 image create photo blt::tv::normalFile -format gif -data {
83 R0lGODlhFAAQAMIAAP///wAAALq2VYKCgtvb2wAAAAAAAAAAACH5BAEAAAAA
84 LAAAAAAUABAAAAM7CLrcriHK8BxlsWIgOqCXFkKkyHnT2KjYUFFdjLoPuwS0
85 dcrCl6+i34Y3ewWNL+EtlVIuBtCodEBgJAAAOw==
86 }
87
88 image create photo blt::tv::openFile -format gif -data {
89 R0lGODlhEQAQAIUAAPwCBFxaXNze3Ly2rJyanPz+/Ozq7GxqbPz6/PT29HRy
90 dMzOzDQyNExGBIyKhERCRPz+hPz+BPz29MTCBPzy7PTq3NS+rPz+xPz27Pzu
91 5PTi1My2pPTm1PTezPTm3PTaxMyynMyqjPTizOzWvOzGrOTi3OzOtMSehNTK
92 xNTCtAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
93 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAAAALAAAAAARABAAAAaj
94 QIBQGCgajcMkICBoNgcBQkBJFBSuBcMBIaUysYWEYsFlKBtWsENxeJgBDTik
95 UaBfE4KBw9yYNyINfXYFEnlvfxMRiYMSFBUWfA0TFxAXE4EFGBkVGhsMfRER
96 EAUQoXObHB2ecJKUloEJHB4aHyCHirgNGRmzHx8hfH6Agh4iHyMkwEJxghkN
97 HCXHJiQnb0MNCwsoKRYbICEh1UoBDOXm5wx2QQA7
98 }
99
100 image create photo blt::tv::empty
101
102 image create photo blt::tv::downarrow -format gif -data {
103 R0lGODlhEQAJAPABAAAAAP///yH5BAEAAAEALAAAAAARAAkAAAJXTJgwYcKE
104 CRMmTJgQIECAAAEiTJgwIUCAAAEiTJgwYUKAAAEiTJgwYcKEAAEiTJgwYcKE
105 CQEiTJgwYcKECRMiTJgwYcKECRMmTJgwYcKECRMmTJgwYcIUADs=
106 }
107
108 image create photo blt::tv::rightarrow -format gif -data {
109 R0lGODlhEAAQAIAAAPwCBAQCBCH5BAEAAAAALAAAAAAQABAAAAIdhI+pyxCt
110 woNHTmpvy3rxnnwQh1mUI52o6rCu6hcAIf5oQ3JlYXRlZCBieSBCTVBUb0dJ
111 RiBQcm8gdmVyc2lvbiAyLjUNCqkgRGV2ZWxDb3IgMTk5NywxOTk4LiBBbGwg
112 cmlnaHRzIHJlc2VydmVkLg0KaHR0cDovL3d3dy5kZXZlbGNvci5jb20AOw==
113 }
114
115 image create photo blt::tv::ball -format gif -data {
116 R0lGODlhEQAJAPABAAAAAP///yH5BAEAAAEALAAAAAARAAkAAAJXTJgwYcKE
117 CRMmTJgwYcKECRMmTJgwYcKAABMmTJgwYcKAAAEmTJgwYcKEAAEiTJgwYcKE
118 CQEiTJgwYcKECRMmTJgwYcKECRMmTJgwYcKECRMmTJgwYcIUADs=
119 }
120
121 # Seems to be a memory leak in @cursors and another in binds.
122 if { $tcl_platform(platform) == "windows" } {
123 if { $tk_version >= 8.3 && ![string match /zvfs* $blt_library]} {
124 set cursor "@[file join $blt_library treeview.cur]"
125 } else {
126 set cursor "size_we"
127 }
128 option add *${className}.ResizeCursor [list $cursor]
129 } else {
130 option add *${className}.ResizeCursor \
131 "@$blt_library/treeview.xbm $blt_library/treeview_m.xbm black white"
132 }
133
134 # ----------------------------------------------------------------------
135 #
136 # Initialize --
137 #
138 # Invoked by internally by Treeview_Init routine. Initializes
139 # the default bindings for the treeview widget entries. These
140 # are local to the widget, so they can't be set through the
141 # widget's class bind tags.
142 #
143 # TODO: get rid of most of this in favor of class binds.
144 #
145 # ----------------------------------------------------------------------
146 variable ::blt::tv::oldedit 0
147 proc ::blt::tv::Initialize { w } {
148 #
149 # Active entry bindings
150 #
151 variable oldedit
152 $w bind Entry <Enter> {
153 %W entry activate current
154 }
155 $w bind Entry <Leave> {
156 %W entry activate ""
157 }
158
159 #
160 # Button bindings
161 #
162 $w button bind all <ButtonRelease-1> {
163 blt::tv::Toggle %W current
164 }
165 $w button bind all <Enter> {
166 %W button activate current
167 }
168 $w button bind all <Leave> {
169 %W button activate ""
170 }
171
172 #
173 # ButtonPress-1
174 #
175 # Performs the following operations:
176 #
177 # 1. Clears the previous selection.
178 # 2. Selects the current entry.
179 # 3. Sets the focus to this entry.
180 # 4. Scrolls the entry into view.
181 # 5. Sets the selection anchor to this entry, just in case
182 # this is "multiple" mode.
183 #
184
185 $w bind Entry <ButtonPress-1> {
186 blt::tv::SetSelectionSetAnchor %W %x %y
187 set blt::tv::scroll 1
188 }
189
190 #$w bind Entry <Double-ButtonPress-1> { %W toggle current }
191
192 #
193 # B1-Motion
194 #
195 # For "multiple" mode only. Saves the current location of the
196 # pointer for auto-scrolling. Resets the selection mark.
197 #
198 $w bind Entry <B1-Motion> {
199 set blt::tv::x %x
200 set blt::tv::y %y
201 set index [%W nearest %x %y]
202 if { [%W cget -selectmode] == "multiple" } {
203 %W selection mark $index
204 } elseif { [%W cget -selectmode] != "none" } {
205 blt::tv::SetSelectionAnchor %W $index
206 }
207 }
208
209 #
210 # ButtonRelease-1
211 #
212 # For "multiple" mode only.
213 #
214 $w bind Entry <ButtonRelease-1> {
215 if { [%W cget -selectmode] == "multiple" } {
216 %W selection anchor current
217 }
218 after cancel $blt::tv::afterId
219 set blt::tv::scroll 0
220 }
221
222 #
223 # Shift-ButtonPress-1
224 #
225 # For "multiple" mode only.
226 #
227
228 $w bind Entry <Shift-ButtonPress-1> {
229 blt::tv::SetSelectionExtendAnchor %W %x %y
230 }
231 $w bind Entry <Shift-Double-ButtonPress-1> {
232 # do nothing
233 }
234 $w bind Entry <Shift-B1-Motion> {
235 # do nothing
236 }
237 $w bind Entry <Shift-ButtonRelease-1> {
238 after cancel $blt::tv::afterId
239 set blt::tv::scroll 0
240 }
241
242 #
243 # Control-ButtonPress-1
244 #
245 # For "multiple" mode only.
246 #
247 $w bind Entry <Control-ButtonPress-1> {
248 blt::tv::SetSelectionAdd %W %x %y
249 }
250 $w bind Entry <Control-Double-ButtonPress-1> {
251 # do nothing
252 }
253 $w bind Entry <Control-B1-Motion> {
254 # do nothing
255 }
256 $w bind Entry <Control-ButtonRelease-1> {
257 after cancel $blt::tv::afterId
258 set blt::tv::scroll 0
259 }
260
261 $w bind Entry <Control-Shift-ButtonPress-1> {
262 if { [%W cget -selectmode] == "multiple" && [%W selection present] } {
263 if { [%W index anchor] == "" } {
264 %W selection anchor current
265 }
266 if { [%W selection includes anchor] } {
267 %W selection set anchor current
268 } else {
269 %W selection clear anchor current
270 %W selection set current
271 }
272 } elseif { [%W cget -selectmode] != "none" } {
273 blt::tv::SetSelectionAnchor %W current
274 }
275 }
276 $w bind Entry <Control-Shift-Double-ButtonPress-1> {
277 # do nothing
278 }
279 $w bind Entry <Control-Shift-B1-Motion> {
280 # do nothing
281 }
282
283 $w bind Entry <Shift-ButtonPress-3> {
284 blt::tv::EditColumn %W %X %Y
285 }
286
287 $w column bind all <Enter> {
288 %W column activate [%W column current]
289 }
290 $w column bind all <Leave> {
291 %W column activate ""
292 }
293 $w column bind Rule <Enter> {
294 %W column activate [%W column current]
295 %W column resize activate [%W column current]
296 }
297 $w column bind Rule <Leave> {
298 %W column activate ""
299 %W column resize activate ""
300 }
301 $w column bind Rule <ButtonPress-1> {
302 %W column resize anchor %x
303 }
304 $w column bind Rule <B1-Motion> {
305 %W column resize mark %x
306 }
307 $w column bind Rule <ButtonRelease-1> {
308 %W column configure [%W column current] -width [%W column resize set]
309 }
310 set ::blt::tv::curRelief raised
311 $w column bind all <ButtonPress-1> {
312 set blt::tv::column [%W column current]
313 set blt::tv::curRelief [%W column cget $blt::tv::column -titlerelief]
314 %W column configure $blt::tv::column -titlerelief sunken
315 }
316 $w column bind all <ButtonRelease-1> {
317 set column [%W column current]
318 if { $column != "" } {
319 %W column invoke $column
320 }
321 %W column configure $blt::tv::column -titlerelief $blt::tv::curRelief
322 }
323 if {$oldedit} {
324 $w bind TextBoxStyle <Alt-ButtonPress-3> {
325 if { [%W edit -root -test %X %Y] } {
326 break
327 }
328 }
329 $w bind TextBoxStyle <Shift-ButtonRelease-1> {
330 if { [%W edit -root -test %X %Y] } {
331 blt::tv::EditColumn %W %X %Y
332 break
333 }
334 }
335 $w bind TextBoxStyle <Double-1> {
336 if { [%W edit -root -test %X %Y] } {
337 blt::tv::EditColumn %W %X %Y
338 break
339 }
340 }
341 }
342 # $w bind CheckBoxStyle <Enter> {
343 # set column [%W column current]
344 # if { [%W column cget $column -edit] } {
345 # %W style activate current $column
346 # }
347 # }
348 # $w bind CheckBoxStyle <Leave> {
349 # %W style activate ""
350 # }
351 $w bind CheckBoxStyle <ButtonPress-1> {
352 if { [%W edit -root -test %X %Y] } {
353 event generate %W <<TreeViewEditStart>> -x [%W col index [%W col current]] -y [%W index @%x,%y]
354 break
355 }
356 }
357 $w bind CheckBoxStyle <B1-Motion> {
358 if { [%W column cget [%W column current] -edit] } {
359 break
360 }
361 }
362 $w bind CheckBoxStyle <ButtonRelease-1> {
363 if { [%W edit -root -test %X %Y] } {
364 %W edit -root %X %Y
365 event generate %W <<TreeViewEditEnd>> -x [%W col index [%W col current]] -y [%W index @%x,%y]
366 break
367 }
368 }
369 if 0 {
370 $w bind ComboBoxStyle <ButtonPress-1> {
371 set column [%W column current]
372 %W style activate focus $column
373 if { [%W column cget $column -edit] } {
374 break
375 }
376 }
377 $w bind ComboBoxStyle <ButtonRelease-1> {
378 %W style activate 0
379 if { [%W edit -root -test %X %Y] } {
380 %W edit -root %X %Y
381 break
382 }
383 }
384
385 $w bind ComboBoxStyle <Double-1> {
386 if { [%W edit -root -test %X %Y] } {
387 blt::tv::EditColumn %W %X %Y
388 break
389 }
390 }
391 }
392 if {$oldedit} {
393 $w bind ComboBoxStyle <Shift-ButtonRelease-1> {
394 if { [%W edit -root -test %X %Y] } {
395 blt::tv::EditColumn %W %X %Y
396 break
397 }
398 }
399
400 } else {
401 $w bind ComboBoxStyle <ButtonPress-1> {}
402 }
403 $w bind ComboBoxStyle <ButtonRelease-1> {
404 if { [%W edit -root -test %X %Y] } {
405 blt::tv::Combobox-List %W %X %Y
406 }
407 }
408
409 }
410
411
412 proc ::blt::tv::Combobox-List-Done {W entry col args} {
413 set i [$W index active]
414 set v [$W get $i]
415 set l [winfo parent $W]
416 set w [winfo parent $l]
417 $w entry set $entry $col $v
418 event generate $w <<TreeViewEditEnd>> -x [$w col index $col] -y $entry
419 destroy $l
420 $w style activate 0
421 }
422
423 proc ::blt::tv::Combobox-List-Close {l} {
424 set w [winfo parent $l]
425 destroy $l
426 $w style activate 0
427 }
428
429 proc ::blt::tv::Combobox-List {w x y} {
430 # Popup combo-list for combobox.
431 # TODO: could put frame in toplevel so not clipped.
432 set Opts {
433 { -height 6 "Listbox height" }
434 { -leafs False "Edit only leaf nodes" }
435 { -leave 1 "Setup handler for leave" }
436 { -conf {} "Listbox widget configuration options" }
437 { -optscmd {} "Callback to get edit options" -type {cmd w r c} }
438 { -readonly False "Do not allow editing" }
439 { -useframe 1 "Use a frame below treeview widget" }
440 { -usetopframe 0 "Use a frame at toplevel" }
441 { -withouttag {} "Edit only entries without tag"}
442 { -withtag {} "Edit only entries with tag"}
443 }
444 if {[winfo exists $w.edit]} return
445 set lx [expr {$x-[winfo rootx $w]}]
446 set ly [expr {$y-[winfo rooty $w]}]
447 set ind [$w index @$lx,$ly]
448 #if {[llength [set lst [$w cget -values]]] == 0} return
449 set col [$w column current]
450 if {![$w column cget $col -edit]} return
451 set widopts [$w column cget $col -editopts]
452 set cellstyle [lindex [$w style get $col $ind] 0]
453 if {$cellstyle != {}} {
454 if {[$w style cget $cellstyle -readonly]} return
455 set widopts [concat $widopts [$w style cget $cellstyle -editopts]]
456 }
457 set edopts {}
458 foreach i $Opts {
459 set q([lindex $i 0]) [lindex $i 1]
460 lappend edopts [lindex $i 0] [lindex $i 1]
461 }
462 set opts { -activestyle dotbox -bd 2 -pad 10 -relief sunken -selectmode single }
463 set style $cellstyle
464 if {$style == {}} {
465 set style [$w column cget $col -style]
466 }
467 set ckey [$w style cget $style -choicekey]
468 set cmd [$w style cget $style -choicecmd]
469 set lst [$w style cget $style -choices]
470 if {$ckey != {}} {
471 set lst [$w entry get $ind $ckey {}]
472 }
473 if {$cmd != {} && $lst == {}} {
474 set cmd [string map [list %% % %W $w %X $x %Y $y %# $ind %C $col] $cmd]
475 set lst [namespace eval :: $cmd]
476 }
477 set offs [$w column offsets]
478 set cind [$w column index $col]
479 set xstart [lindex $offs $cind]
480 if {$cind >= ([llength $offs]-1)} {
481 set xend [winfo width $w]
482 } else {
483 set xend [lindex $offs [expr {$cind+1}]]
484 }
485 if {$q(-optscmd) != {}} {
486 set ewopts [eval $q(-optscmd) $w $ind $col]
487 if {[llength $ewopts]%2} {
488 tclLog "TreeView -optscmd: odd length: '$ewopts' for $w"
489 } else {
490 array set q $ewopts
491 }
492 }
493 if {$q(-readonly)} return
494 if {$q(-leafs) && [llength [$w entry children $ind]]} return
495 set tags [$w tag names $ind]
496 if {$q(-withtag) != {}} {
497 if {[lsearch -exact $tags $q(-withtag)]<0} return
498 }
499 if {$q(-withouttag) != {}} {
500 if {[lsearch -exact $tags $q(-withouttag)]>=0} return
501 }
502 set xsiz [expr {$xend-$xstart}]
503 set entry $ind
504 set bb [$w bbox -screen $ind]
505 if {[llength $bb]!=4} return
506 foreach {xbegin ystart xwidth ywidth} $bb break
507 #set ystart [lindex $bb 1]
508 set yend [expr {$ywidth+$ystart}]
509 #tclLog "COL: $col, $lst, entry=$entry, offs=$offs, cind=$cind, xsiz=$xsiz, xstart=$xstart, bb=$bb"
510 if {$q(-usetopframe)} {
511 set wl [string trimright [winfo toplevel $w] .]._list
512 } else {
513 set wl $w._list
514 }
515 if {[winfo exists $wl]} {
516 destroy $wl
517 focus -force $w
518 $w style activate 0
519 return
520 }
521 event generate $w <<TreeViewEditStart>> -x $cind -y $ind
522 $w style activate focus $cind
523 focus $w
524 if {$q(-useframe) || $q(-usetopframe)} {
525 canvas $wl
526 if {$q(-leave)} {
527 bind $wl <Leave> { ::blt::tv::Combobox-List-Close %W }
528 }
529 } else {
530 toplevel $wl
531 wm withdraw $wl
532 wm transient $wl [winfo toplevel $w]
533 raise [winfo toplevel $w]
534 wm overrideredirect $wl 1
535 if {$q(-leave)} {
536 bind $wl <Leave> { if {[winfo toplevel %W] == "%W"} { ::blt::tv::Combobox-List-Close %W } }
537 }
538 }
539 set h $q(-height)
540 if {[llength $lst]<=$h} {
541 set h [expr {1+[llength $lst]}]
542 }
543 set opts [concat -height $h $opts]
544 set l $wl._l
545 listbox $l
546 foreach {i j} $opts {
547 catch { $l conf $i $j }
548 }
549 if {$q(-conf) != {}} {
550 catch { eval $l conf $q(-conf) }
551 }
552 bindtags $l [concat [bindtags $l] TVComboBox::List]
553 $l conf -yscrollcommand [list $wl._vscroll set]
554 scrollbar $wl._vscroll -orient vertical -command [list $l yview]
555 if {[llength $lst]>$h} {
556 grid $wl._vscroll -row 1 -column 2 -sticky ns
557 }
558 grid $l -row 1 -column 1 -sticky news
559 grid columnconf $wl 1 -weight 1
560 grid rowconf $wl 1 -weight 1
561 set val [$w get]
562 foreach i $lst {
563 $l insert end $i
564 if {[string equal $i $val]} { $l activate end; $l see end }
565 }
566 bind $l <Visibility> [subst -nocommands {
567 bind $l <Visibility> {}
568 if {[$l xview] != "0 1"} {
569 $l conf -xscrollcommand [list $wl._hscroll set]
570 scrollbar $wl._hscroll -orient horizontal -command [list $wl xview]
571 grid $wl._hscroll -row 2 -column 1 -sticky we
572 grid $wl._vscroll -row 1 -column 2 -sticky ns
573 focus -force $l
574 }
575 }]
576 bind $l <<TVComboBox-List-Done>> [list [namespace current]::Combobox-List-Done $l $entry $col]
577 set H [winfo reqheight $l]
578 set Xstart [expr {$xstart+[winfo x $w]}]
579 set Yend [expr {$yend+[winfo x $w]}]
580 set wwhig [winfo height $w]
581 if {!(($ystart+$H) <= $wwhig || ($ystart-$H)<0)} {
582 set Yend [expr {$ystart-$H}]
583 }
584 if {$q(-usetopframe)} {
585 place $wl -in $w -width ${xsiz} -height $H -x $Xstart -y $Yend
586 $wl conf -width $xsiz -height $H
587 } elseif {$q(-useframe)} {
588 place $wl -in $w -width ${xsiz} -height $H -x $Xstart -y $Yend
589 $wl conf -width $xsiz -height $H
590 } else {
591 wm geometry $wl ${xsiz}x${H}+$Xstart+$Yend
592 wm deiconify $wl
593 }
594 $w edit -noscroll
595 if {$q(-usetopframe)} {
596 bind $e <Destroy> +[list catch "$wl edit -scroll" ]
597 } else {
598 set tag [namespace current]
599 bindtags $wl [concat $tag [bindtags $wl]]
600 bind $tag <Destroy> {catch {[winfo parent %W] edit -scroll}}
601 }
602 after idle [list catch "focus -force $l"]
603 return -code break
604 }
605
606 proc ::blt::tv::Combobox-List-Key {l ch args} {
607 # Navigate to the first item starting with char ch.
608 array set p { -now 0 }
609 array set p $args
610 if {![string is alpha -strict $ch]} return
611 set cur [$l index active]
612 set e [$l index end]
613 foreach i {0 1} {
614 set n -1
615 while {[incr n]<$e} {
616 set c [string index [$l get $n] 0]
617 if {$i} { set c [string toupper $c] }
618 if {[string equal $c $ch]} {
619 $l activate $n
620 $l see $n
621 if {$n == $cur || $p(-now)} {
622 event generate $l <Return>
623 }
624 return
625 }
626 }
627 set ch [string toupper $ch]
628 }
629 return -code break
630 }
631
632 proc ::blt::tv::SortColumn {t {column {}} args} {
633 # Provide sorting for a column.
634 array set p {-hold 1 -see 1 -highlight 0}
635 array set p $args
636 set do 1
637 if {$column == {}} {
638 set column [$t column current]
639 }
640 if {[string equal $column [$t sort cget -column]]} {
641 set decr [expr {![$t sort cget -decreasing]}]
642 if {!$decr} {
643 $t sort conf -column {} -decreasing 0
644 if {[$t sort cget -setflat] } {
645 $t configure -flat no
646 $t sort configure -setflat no
647 }
648 set do 0
649 }
650 } else {
651 set decr 0
652 }
653 if {$do} {
654 set mode [$t column cget $column -sortmode]
655 if {$mode != "none"} {
656 $t sort configure -mode $mode
657 }
658 $t sort configure -decreasing $decr -column $column
659 if {![$t cget -flat] } {
660 $t configure -flat yes
661 $t sort configure -setflat yes
662 }
663 $t sort auto yes
664 if {$p(-hold)} {
665 blt::busy hold $t
666 update
667 blt::busy release $t
668 }
669 if {$p(-highlight)} {
670 after 300 [list $t column activate $column]
671 }
672 } else {
673 if {$p(-highlight)} {
674 $t column activate {}
675 }
676 }
677 if {$p(-see)} {
678 set sel [$t curselection]
679 if {$sel != {}} {
680 after idle [list $t see [lindex $sel 0]]
681 }
682 }
683 set cind [$t column index $column]
684 event generate $t <<TreeViewSortColumn>> -x $cind
685 }
686
687
688 bind TVComboBox <3> [list blt::tv::Combobox-List %W %x %y]
689
690 bind TVComboBox::List <Enter> { focus -force %W }
691 bind TVComboBox::List <KeyRelease-Escape> { destroy [winfo parent %W] }
692 bind TVComboBox::List <Return> { event generate %W <<TVComboBox-List-Done>>}
693 bind TVComboBox::List <space> [bind TVComboBox::List <Return>]
694 bind TVComboBox::List <ButtonRelease-1> [bind TVComboBox::List <Return>]
695 bind TVComboBox::List <KeyRelease-space> [bind TVComboBox::List <Return>]
696 bind TVComboBox::List <Control-n> [bind Listbox <Down>]
697 bind TVComboBox::List <Control-p> [bind Listbox <Up>]
698 bind TVComboBox::List <Control-d> [bind Listbox <Next>]
699 bind TVComboBox::List <Control-u> [bind Listbox <Prior>]
700 bind TVComboBox::List <KeyPress> [list blt::tv::Combobox-List-Key %W %A]
701
702 proc blt::tv::SetSelectionAnchor { w tagOrId } {
703 if {$tagOrId == ""} return
704 set index [$w index $tagOrId]
705 # If the anchor hasn't changed, don't do anything
706 if { $index != [$w index anchor] } {
707 $w selection clearall
708 $w see $index
709 $w focus $index
710 $w selection set $index
711 $w selection anchor $index
712 }
713 }
714
715 proc blt::tv::SetSelectionSetAnchor { w x y} {
716 set mode [$w cget -selectmode]
717 switch -- $mode {
718 none return
719 cell { }
720 multicell {}
721 default {
722 set blt::tv::x $x
723 set blt::tv::y $y
724 SetSelectionAnchor $w @$x,$y
725 return
726 }
727 }
728 $w selection clearall
729 set index [$w index @$x,$y]
730 set col [$w column nearest $x]
731 if {$index != {} && $col != {}} {
732 $w selection set $index $index $col
733 $w selection anchor $index $col
734 $w focus $index
735 }
736 }
737
738 proc blt::tv::SetSelectionAdd {w x y} {
739 set mode [$w cget -selectmode]
740 set index [$w index @$x,$y]
741 switch -- $mode {
742 none return
743 cell { $w selection clearall }
744 multicell { }
745 multiple {
746 set index [$w index current]
747 $w selection toggle $index
748 $w selection anchor $index
749 return
750 }
751 default {
752 SetSelectionAnchor $w current
753 return
754 }
755 }
756 set col [$w column nearest $x]
757 if {$index != {} && $col != {}} {
758 $w selection toggle $index $index $col
759 }
760 }
761
762 proc blt::tv::SetSelectionExtendAnchor {w x y} {
763 set mode [$w cget -selectmode]
764 switch -- $mode {
765 none {}
766 multiple {
767 if {[$w selection present] } {
768 if { [$w index anchor] == "" } {
769 $w selection anchor current
770 }
771 set index [$w index anchor]
772 $w selection clearall
773 $w selection set $index current
774 }
775 }
776 single {
777 blt::tv::SetSelectionAnchor $w current
778 }
779 cell {
780 SetSelectionSetAnchor $w $x $y
781 }
782 multicell {
783 # Select range.
784 set col [$w column nearest $x]
785 set oanch [$w selection anchor]
786 set anch [$w index anchor]
787 set ocell [lindex $oanch 1]
788 set index [$w index @$x,$y]
789 if {$col == {} || $ocell == {}} {
790 return [SetSelectionSetAnchor $w $x $y]
791 }
792 set cols [$w column names]
793 set coli [lsearch $cols $col]
794 set ocelli [lsearch $cols $ocell]
795 if {$coli<0 || $ocelli<0} {
796 return [SetSelectionSetAnchor $w $x $y]
797 }
798 if {$coli<$ocelli} {
799 set sci $coli
800 set coli $ocelli
801 set ocelli $sci
802 }
803 set clst {}
804 foreach c [lrange $cols $ocelli $coli] {
805 if {$c == "#0"} continue
806 if {[$w column cget $c -hide]} continue
807 lappend clst $c
808 }
809 set nlst {}
810 foreach n [$w find $index $anch] {
811 if {[$w entry cget $n -hide]} continue
812 lappend nlst $n
813 }
814 $w selection clearall
815 foreach n $nlst {
816 foreach c $clst {
817 $w selection set $n $n $c
818 }
819 }
820 }
821 }
822 }
823
824 # ----------------------------------------------------------------------
825 #
826 # AutoScroll --
827 #
828 # Invoked when the user is selecting elements in a treeview
829 # widget and drags the mouse pointer outside of the widget.
830 # Scrolls the view in the direction of the pointer.
831 #
832 # ----------------------------------------------------------------------
833 proc blt::tv::AutoScroll { w } {
834 if { ![winfo exists $w] } {
835 return
836 }
837 set x $blt::tv::x
838 set y $blt::tv::y
839
840 set index [$w nearest $x $y]
841
842 if {$y >= [winfo height $w]} {
843 $w yview scroll 1 units
844 set neighbor down
845 } elseif {$y < 0} {
846 $w yview scroll -1 units
847 set neighbor up
848 } else {
849 set neighbor $index
850 }
851 if { [$w cget -selectmode] == "single" } {
852 blt::tv::SetSelectionAnchor $w $neighbor
853 } elseif { [$w cget -selectmode] != "none" } {
854 catch {$w selection mark $index}
855 }
856 set ::blt::tv::afterId [after 50 blt::tv::AutoScroll $w]
857 }
858
859 proc blt::tv::SetFocus { w tagOrId } {
860 # Set focus at index given by tagOrId.
861 if {[catch {$w index $tagOrId} t]} return
862 if {[catch {$w focus $t}]} return
863 $w selection clearall
864 if {[catch {$w selection set $t}]} return
865 $w selection anchor $t
866 $w entry activate $t
867 $w see $t
868 event generate $w <<TreeViewFocusEvent>>
869 return $t
870 }
871 # ----------------------------------------------------------------------
872 #
873 # MoveFocus --
874 #
875 # Invoked by KeyPress bindings. Moves the active selection to
876 # the entry <where>, which is an index such as "up", "down",
877 # "prevsibling", "nextsibling", etc.
878 #
879 # ----------------------------------------------------------------------
880 proc blt::tv::MoveFocus { w tagOrId {flag 0}} {
881 set mode [$w cget -selectmode]
882 switch -- $mode {
883 multiple {
884 catch {$w focus $tagOrId}
885 if {!$flag} {
886 $w selection clearall
887 }
888 if {[catch {$w selection set focus}]} return
889 $w selection anchor focus
890 }
891 single {
892 catch {$w focus $tagOrId}
893 $w selection clearall
894 if {[catch {$w selection set focus}]} return
895 $w selection anchor focus
896 }
897 multicell -
898 cell {
899 set cells [$w selection cells]
900 catch {$w focus $tagOrId}
901 $w selection clearall
902 if {[catch {$w selection set focus}]} return
903 $w selection anchor focus
904 set ind [$w index focus]
905 if {$cells != {}} {
906 set col [lindex $cells 1]
907 } else {
908 set vcols [$w column names -visible]
909 set col [lindex $vcols 0]
910 }
911 $w selection set focus focus $col
912 }
913 }
914 $w see focus
915 event generate $w <<TreeViewFocusEvent>>
916 }
917
918 # ----------------------------------------------------------------------
919 #
920 # MovePage --
921 #
922 # Invoked by KeyPress bindings. Pages the current view up or
923 # down. The <where> argument should be either "top" or
924 # "bottom".
925 #
926 # ----------------------------------------------------------------------
927 proc blt::tv::MovePage { w where } {
928
929 # If the focus is already at the top/bottom of the window, we want
930 # to scroll a page. It's really one page minus an entry because we
931 # want to see the last entry on the next/last page.
932 set focus [$w index focus]
933 if {$where == "top"} {
934 if {[$w index up] != $focus} {
935 $w yview scroll -1 pages
936 $w yview scroll 1 units
937 }
938 if {[$w index focus] == $focus} {
939 catch { $w entry select up }
940 }
941 } else {
942 if {[$w index down] != $focus} {
943 $w yview scroll 1 pages
944 $w yview scroll -1 units
945 }
946 if {[$w index focus] == $focus} {
947 catch { $w entry select down }
948 }
949 }
950 update
951
952 # Adjust the entry focus and the view. Also activate the entry.
953 # just in case the mouse point is not in the widget.
954 $w entry activate view.$where
955 $w focus view.$where
956 $w see view.$where
957 if { [$w cget -selectmode] == "single" } {
958 $w selection clearall
959 catch {$w selection set focus}
960 }
961 event generate $w <<TreeViewFocusEvent>>
962 }
963
964 # ----------------------------------------------------------------------
965 #
966 # NextMatch --
967 #
968 # Invoked by KeyPress bindings. Searches for an entry that
969 # starts with the letter <char> and makes that entry active.
970 #
971 # ----------------------------------------------------------------------
972 proc blt::tv::NextMatch { w key state} {
973 if {$state != 0 && $state != 1} return
974 if {[string match {[ -~]} $key]} {
975 set last [$w index focus]
976 set next [$w index next]
977 while { $next != {} && $next != $last } {
978 set label [$w entry cget $next -label]
979 set label [string index $label 0]
980 if { [string tolower $label] == [string tolower $key] } {
981 break
982 }
983 set next [$w index -at $next next]
984 }
985 if {$next == {}} return
986 $w focus $next
987 if {[$w cget -selectmode] == "single"} {
988 $w selection clearall
989 $w selection set focus
990 event generate $w <<TreeViewFocusEvent>>
991 }
992 $w see focus
993 }
994 }
995
996 #------------------------------------------------------------------------
997 #
998 # InsertText --
999 #
1000 # Inserts a text string into an entry at the insertion cursor.
1001 # If there is a selection in the entry, and it covers the point
1002 # of the insertion cursor, then delete the selection before
1003 # inserting.
1004 #
1005 # Arguments:
1006 # w Widget where to insert the text.
1007 # text Text string to insert (usually just a single character)
1008 #
1009 #------------------------------------------------------------------------
1010 proc blt::tv::InsertText { w text } {
1011 if { [string length $text] > 0 } {
1012 set index [$w index insert]
1013 if { ($index >= [$w index sel.first]) &&
1014 ($index <= [$w index sel.last]) } {
1015 $w delete sel.first sel.last
1016 }
1017 $w insert $index $text
1018 }
1019 }
1020
1021 #------------------------------------------------------------------------
1022 #
1023 # Transpose -
1024 #
1025 # This procedure implements the "transpose" function for entry
1026 # widgets. It tranposes the characters on either side of the
1027 # insertion cursor, unless the cursor is at the end of the line.
1028 # In this case it transposes the two characters to the left of
1029 # the cursor. In either case, the cursor ends up to the right
1030 # of the transposed characters.
1031 #
1032 # Arguments:
1033 # w The entry window.
1034 #
1035 #------------------------------------------------------------------------
1036 proc blt::tv::Transpose { w } {
1037 set i [$w index insert]
1038 if {$i < [$w index end]} {
1039 incr i
1040 }
1041 set first [expr {$i-2}]
1042 if {$first < 0} {
1043 return
1044 }
1045 set new [string index [$w get] [expr {$i-1}]][string index [$w get] $first]
1046 $w delete $first $i
1047 $w insert insert $new
1048 }
1049
1050 #------------------------------------------------------------------------
1051 #
1052 # GetSelection --
1053 #
1054 # Returns the selected text of the entry with respect to the
1055 # -show option.
1056 #
1057 # Arguments:
1058 # w Entry window from which the text to get
1059 #
1060 #------------------------------------------------------------------------
1061
1062 proc blt::tv::GetSelection { w } {
1063 set text [string range [$w get] [$w index sel.first] \
1064 [expr {[$w index sel.last] - 1}]]
1065 if {[$w cget -show] != ""} {
1066 regsub -all . $text [string index [$w cget -show] 0] text
1067 }
1068 return $text
1069 }
1070
1071 proc blt::tv::TextCopy {w {edit 0} {aslist 0}} {
1072 # Handle <<Copy>> event, copying selection/focus to clipboard.
1073 if {!$edit} {
1074 catch {
1075 set inds [$w curselection]
1076 if {$inds == {}} {
1077 set inds [$w index focus]
1078 }
1079 set all {}
1080 set n -1
1081 foreach ind $inds {
1082 incr n
1083 set data {}
1084 foreach i [$w column names] {
1085 if {[$w col cget $i -hid]} continue
1086 if {$i == "#0"} {
1087 set val [$w entry cget $ind -label]
1088 } else {
1089 set val [$w entry set $ind $i]
1090 }
1091 if {$aslist} {
1092 lappend data $val
1093 } else {
1094 append data " " $val
1095 }
1096 }
1097 if {$aslist} {
1098 lappend all $data
1099 } else {
1100 if {$n} { append all \n }
1101 append all $data
1102 }
1103
1104 }
1105 clipboard clear -displayof $w
1106 clipboard append -displayof $w $all
1107 }
1108 } else {
1109 catch {
1110 set w [winfo parent $w]
1111 set ind [$w index focus]
1112 set col [$w column current]
1113 if {$col == {}} {
1114 set col $::blt::tv::curCol
1115 }
1116 set data [$w entry set $ind $col]
1117 clipboard clear -displayof $w
1118 clipboard append -displayof $w $data
1119 }
1120 }
1121 }
1122
1123 proc ::blt::tv::Toggle {w ind} {
1124 # Toggle and set view.
1125 set ind [$w index $ind]
1126 if {$ind == {}} return
1127 $w toggle $ind
1128 if {[$w entry isopen $ind] && [$w cget -openanchor] != {} && [$w entry children $ind] != {}} {
1129 $w see -anchor [$w cget -openanchor] $ind
1130 } else {
1131 $w see $ind
1132 }
1133 }
1134
1135 proc ::blt::tv::Click {w x y} {
1136 if {[focus] != $w} { focus $w }
1137 set ::blt::tv::space off
1138 #if {[winfo exists $w.edit]} { destroy $w.edit }
1139 event generate $w <<TreeViewFocusEvent>>
1140 }
1141
1142 bind $className <ButtonRelease-1> {::blt::tv::Click %W %x %y}
1143 bind $className <Double-ButtonPress-1> {blt::tv::EditCol %W %x %y }
1144 bind $className <Alt-ButtonPress-1> {blt::tv::EditCol %W %x %y }
1145 bind $className <Control-minus> {if {[%W index parent]>0} { after idle "%W entry select [%W entry parent focus]"; %W close [%W entry parent focus] }}
1146 bind $className <Control-o> { ::blt::tv::Toggle %W focus }
1147 bind $className <Control-a> { blt::tv::MoveFocus %W parent }
1148 bind $className <Control-Shift-O> { %W open -recurse focus }
1149 bind $className <Control-Shift-C> { %W close -recurse focus }
1150
1151 bind TreeViewEditWin <KeyRelease-Escape> {focus [winfo parent %W]; destroy %W; break}
1152 bind TreeViewEditWin <KeyPress-Return> {event generate %W <<TreeViewEditComplete>>; break}
1153 #bind TreeViewEditWin <KeyPress-Return> {break}
1154
1155 proc ::blt::tv::EditDone {w e x y ind col cind data styledata cellstyle ied endcmd treelabel vcmd} {
1156 # # Handle edit completion: call $endcmd and widget -vcmd if req.
1157 switch -- [winfo class $e] {
1158 Entry - Spinbox {
1159 set newdata [$e get]
1160 }
1161 Text {
1162 set newdata [string trimright [$e get 1.0 end]]
1163 }
1164 default {
1165 set newdata $data
1166 }
1167 }
1168 # Invoke validation for Entry/Spinbox/Text if string changed.
1169 set ok 1
1170 if {![string equal $data $newdata]} {
1171 if {$vcmd == {}} {
1172 set vcmd [$w column cget $col -validatecmd]
1173 }
1174 if {$vcmd != {}} {
1175 if {[string first % $vcmd]>=0} {
1176 set ccmd [string map [list %% % %W $w %X $x %Y $y %# $ind %C $cind %V [list $newdata]] $vcmd]
1177 } else {
1178 set ccmd [concat $vcmd [list $w $newdata $data $ind $col]]
1179 }
1180 set newdata [namespace eval :: $ccmd]
1181 }
1182 if {![winfo exists $w]} return
1183 }
1184 if {![string equal $data $newdata]} {
1185 set istree [$w column istree $cind]
1186 if {$ind == -1} {
1187 $w col conf $cind -title $newdata
1188 } elseif {$istree} {
1189 if {$treelabel} {
1190 [$w cget -tree] label $ind $newdata
1191 } else {
1192 $w entry conf $ind -label $newdata
1193 }
1194 } else {
1195 if {$styledata != {}} {
1196 set newdata [list $styledata $newdata]
1197 }
1198 $w entry set $ind $col $newdata
1199 }
1200 if {$endcmd != {}} {
1201 if {[string first % $endcmd]>=0} {
1202 set ccmd [string map [list %% % %W $w %X $x %Y $y %# $ind %C $cind %V [list $newdata]] $endcmd]
1203 set ccmd [concat $endcmd [list $w $newdata $data $ind $col]]
1204 }
1205 namespace eval :: $ccmd
1206 }
1207 if {![winfo exists $w]} return
1208 event generate $w <<TreeViewEditEnd>> -x $cind -y $ind
1209 }
1210 if {$ied} {
1211 catch { bind $e <Destroy> {} }
1212 $w style set $cellstyle $col $ind
1213 } else {
1214 catch { place forget $e }
1215 }
1216 destroy $e
1217 catch { focus $w }
1218 #after idle [list destroy $e]
1219 }
1220
1221 proc ::blt::tv::TabMove {w ind cind args} {
1222 # Handle Tab char.
1223 #Opts p $args {
1224 # { -cmd {} "Callback to get next cell" -type {cmd ind cind} }
1225 # { -endcol {} "Maximum column (defaults to last col)" }
1226 # { -startcol 0 "Column to start new row at" }
1227 # { -wrap True "At last row return to top" }
1228 #}
1229 array set p { -cmd {} -endcol {} -startcol 0 -wrap True -opts {}}
1230 array set p $args
1231 set vis [$w column names -visible]
1232 set maxc [expr {[llength $vis]-1}]
1233 if {$p(-endcol) == {} || $p(-endcol) > $maxc} {
1234 set p(-endcol) $maxc
1235 }
1236 set maxr [$w index end]
1237 if {$p(-cmd) != {}} {
1238 set ncol [eval $p(-cmd) $ind,$cind]
1239 if {$ncol == {}} return
1240 foreach {ind col} $ncol break
1241 EditCell $w $ind $col
1242 return
1243
1244 }
1245 set down [expr {$p(-wrap)?"next":"down"}]
1246 set cnt 100
1247 while 1 {
1248 if {[incr cnt -1] == 0} return
1249 incr cind 1
1250 if {$cind > $p(-endcol)} {
1251 set cind $p(-startcol)
1252 set ind [$w index $down]
1253 $w focus $ind
1254 }
1255 if {[$w column cget $cind -edit] && ![$w column cget $cind -hide]} break
1256 }
1257 EditCell $w $ind $cind
1258 return
1259 }
1260
1261 proc ::blt::tv::EditCell { w ind col {x {}} {y {}}} {
1262 # Handle text editing of a cell.
1263 if {![winfo exists $w]} return
1264 # Option choices for -editopts.
1265 set Opts {
1266 { -allowtypes textbox "List of types to allow text editing for (or *)" }
1267 { -autonl False "Default to text widget if newlines in data"}
1268 { -choices {} "Choices for combo/spinbox" }
1269 { -conf {} "Extra entry/text widget options to set" }
1270 { -embed False "Use an embedded window style for edit window" }
1271 { -endcmd {} "Command to invoke at end of edit" -type cmd }
1272 { -leafs False "Edit only leaf nodes" }
1273 { -nlkeys {<Control-r> <Shift-Return>} "Keys for inserting newline" }
1274 { -notnull False "Field may not be null" }
1275 { -optscmd {} "Callback to get edit options" -type {cmd w r c} }
1276 { -readonly False "Do not allow editing" }
1277 { -sel True "Value is selected on edit" }
1278 { -startcmd {} "Command to invoke at start of edit" -type {cmd w r c} }
1279 { -tab {} "bind Tab char in edit (bool or args passed to TabMove)" }
1280 { -titles False "Allow edit of titles" }
1281 { -treelabel True "Edit -tree cmd label rather than treeview label" }
1282 { -type {} "Support basic Wize types like bool, int, and choice" }
1283 { -typecol {} "Column/key to get -type from" }
1284 { -undo True "Text widget enables undo" }
1285 { -vcmd {} "Validate command to override -validatecmd" -type cmd }
1286 { -widget {} "Widget to use (defaults to entry)" }
1287 { -withouttag {} "Edit only entries without tag"}
1288 { -withtag {} "Edit only entries with tag"}
1289 { -wrap none "Wrap mode for text widget" }
1290 }
1291 if {[winfo exists $w._list]} return
1292 $w see current
1293 set e $w.edit
1294 if { [winfo exists $e] } { destroy $e }
1295 set ind [$w index $ind]
1296 set cind [$w column index $col]
1297 set ed [$w column cget $col -edit]
1298 if { !$ed } return
1299 set intitle 0
1300 if {$x == {}} {
1301 set bb [$w col bbox $col $ind]
1302 set x [lindex $bb 0]
1303 set y [lindex $bb 1]
1304 }
1305 set istree [$w column istree $col]
1306 set edopts {}
1307 foreach i $Opts {
1308 set q([lindex $i 0]) [lindex $i 1]
1309 lappend edopts [lindex $i 0] [lindex $i 1]
1310 }
1311 set widopts [$w column cget $col -editopts]
1312 set cellstyle [lindex [$w style get $cind $ind] 0]
1313 if {$cellstyle != {}} {
1314 if {[$w style cget $cellstyle -readonly]} return
1315 set widopts [concat $widopts [$w style cget $cellstyle -editopts]]
1316 }
1317 if {$widopts != {}} {
1318 if {[llength $widopts]%2} {
1319 tclLog "TreeView -editopts: odd length: '$widopts' for $w"
1320 } else {
1321 array set q $widopts
1322 if {$q(-optscmd) != {}} {
1323 set ewopts [eval $q(-optscmd) $w $ind $col]
1324 if {[llength $ewopts]%2} {
1325 tclLog "TreeView -optscmd: odd length: '$ewopts' for $w"
1326 } else {
1327 array set q $ewopts
1328 }
1329 }
1330 if {[array size q] != ([llength $edopts]/2)} {
1331 set bad {}
1332 array set r $edopts
1333 set good [lsort [array names r]]
1334 foreach {i j} $widopts {
1335 if {![info exists r($i)]} { lappend bad $i }
1336 }
1337 tclLog "TreeView -editopts: bad option: '$bad' not in '$good'"
1338 }
1339 }
1340 }
1341 if {$q(-typecol) != {} && $q(-type) == {}} {
1342 if {[catch { set q(-type) [$w entry set $ind $q(-typecol)] }] &&
1343 [catch { set q(-type) [[$w cget -tree] get $ind $q(-typecol)] }]} {
1344 tclLog "Failed to get -typecol $q(-typecol)"
1345 }
1346 }
1347 set wopts {}
1348 if {$q(-type) != {}} {
1349 switch -- [lindex $q(-type) 0] {
1350 bool {
1351 set q(-choices) {"" True False}
1352 set q(-widget) spinbox
1353 }
1354 Bool {
1355 lset q(-choices) {True False}
1356 set q(-widget) spinbox
1357 }
1358 int - Int -
1359 double - Double {
1360 array set qq {-min -999999999 -max 99999999 -incr 1}
1361 array set qq [lrange $q(-type) 1 end]
1362 set wopts [list -from $qq(-min) -to $qq(-max) -increment $qq(-incr)]
1363 set q(-widget) spinbox
1364 }
1365 Choice {
1366 set q(-choices) [lrange $q(-type) 1 end]
1367 set q(-widget) spinbox
1368 }
1369 }
1370 }
1371 if {$q(-readonly)} return
1372 if {!$q(-titles) && [set intitle [expr {[$w column nearest $x $y] != {}}]]} return
1373 if {$q(-leafs) && [llength [$w entry children $ind]]} return
1374 set tags [$w tag names $ind]
1375 if {$q(-withtag) != {}} {
1376 if {[lsearch -exact $tags $q(-withtag)]<0} return
1377 }
1378 if {$q(-withouttag) != {}} {
1379 if {[lsearch -exact $tags $q(-withouttag)]>=0} return
1380 }
1381 set styledata {}
1382 if {$intitle} {
1383 set data [$w column cget $col -title]
1384 set ind -1
1385 } elseif {$istree } {
1386 if {$q(-treelabel) && [namespace which [$w cget -tree]] == {}} {
1387 set q(-treelabel) 0
1388 }
1389 if {$q(-treelabel)} {
1390 set data [[$w cget -tree] label $ind]
1391 } else {
1392 set data [$w entry cget $ind -label]
1393 }
1394 } else {
1395 set data [$w entry set $ind $col]
1396 if {[$w cget -inlinedata] && [string first @ $data]>=0} {
1397 if {![catch {llength $data} len] && $len <= 2 &&
1398 [string match @?* [lindex $data 0]] &&
1399 [lsearch -exact [$w style names] [string range [lindex $data 0] 1 end]]>=0} {
1400 #set styledata [lindex $data 0]
1401 set data [lindex $data 1]
1402 }
1403 }
1404 }
1405 set bbox [$w column bbox -visible $col $ind]
1406 if {![llength $bbox]} return
1407 foreach {X Y W H} $bbox break
1408 set wid entry
1409 set style [$w col cget $cind -style]
1410 set rstyle [expr {$cellstyle == {} ? $style : $cellstyle }]
1411 set stype [expr {$rstyle == {} ? {} : [$w style type $rstyle]}]
1412 #if {[$w style cget $style -readonly]} return
1413 if {[lsearch $q(-allowtypes) $stype]<0 && $q(-allowtypes) != "*"} return
1414 if {$widopts != {}} {
1415 if {$q(-widget) != {}} {
1416 set wid $q(-widget)
1417 } elseif {$q(-autonl)} {
1418 if {$stype == "combobox"} {
1419 set wid spinbox
1420 } elseif {[string first \n $data]>=0} {
1421 set wid text
1422 } else {
1423 set wid entry
1424 }
1425 }
1426 }
1427 if {[catch {eval $wid $e} err]} {
1428 entry $e
1429 }
1430 foreach {i j} $wopts {
1431 catch { $e conf $i $j}
1432 }
1433 catch { $e conf -font [$w cget -font] }
1434 set ied 0
1435 if {$q(-embed) && !$istree} {
1436 catch {$w style create windowbox editwin}
1437 if {$bbox != {}} {
1438 #TODO: embedded should temporarily set col width, if currently is 0.
1439 set mwid $W
1440 if {$mwid>16} { incr mwid -10 }
1441 set mhig $H
1442 $w style conf editwin -minheight $mhig -minwidth $mwid
1443 }
1444 $w style set editwin $cind $ind
1445 $w entry set $ind $col $e
1446 bind $e <Destroy> [list $w entry set $ind $col $data]
1447 bind $e <Destroy> +[list $w style set $cellstyle $col $ind]
1448 set ied 1
1449 } else {
1450 place $e -x $X -y $Y -width $W -height $H
1451 }
1452 switch -- [winfo class $e] {
1453 Entry {
1454 $e insert end $data
1455 if {$q(-sel)} { $e selection range 0 end }
1456 foreach i $q(-nlkeys) {
1457 bind $e $i "$e insert insert {\n}; break"
1458 }
1459 }
1460 Spinbox {
1461 $e insert end $data
1462 if {$q(-choices) != {}} {
1463 $e conf -values $q(-choices)
1464 }
1465 if {$q(-sel)} { $e selection range 0 end }
1466 set style [$w col cget $cind -style]
1467 if {$stype == "combobox"} {
1468 set ch [$w style cget $style -choices]
1469 if {$ch == {} && [set ccmd [$w style cget $style -choicecmd]] != {}} {
1470 set ccmd [string map [list %% % %W $w %X $x %Y $y %# $ind %C $cind] $ccmd]
1471 set ch [namespace eval :: $ccmd]
1472 }
1473 if {$ch != {}} {
1474 if {[set nn [lsearch -exact $ch $data]]<0} {
1475 set ch [concat [list $data] $ch]
1476 } elseif {$n != 0} {
1477 set ch [concat [list $data] [lreplace $ch $nn $nn]]
1478 }
1479 $e conf -values $ch
1480 }
1481 }
1482 foreach i $q(-nlkeys) {
1483 bind $e $i "$e insert insert {\n}; break"
1484 }
1485 }
1486 Text {
1487 $e conf -highlightthick 0 -padx 0 -pady 0 -bd 1
1488 $e conf -undo $q(-undo) -wrap $q(-wrap)
1489 $e insert end $data
1490 if {$q(-sel)} { $e tag add sel 1.0 end }
1491 foreach i $q(-nlkeys) {
1492 bind $e $i "$e insert insert {\n}; $e see insert; break"
1493 }
1494 }
1495 }
1496 catch {$e conf -highlightthick 0}
1497 if {$q(-conf) != {}} {
1498 if {[catch {eval $e conf $q(-conf)} err]} {
1499 tclLog "Opts err: $err"
1500 }
1501 }
1502 bind $e <1> [list focus $e]
1503 bindtags $e [concat TreeViewEditWin [bindtags $e]]
1504 tkwait visibility $e
1505 focus $e
1506 after 100 [list catch [list focus $e]]
1507 after 300 [list catch [list focus $e]]
1508 bind $e <<TreeViewEditComplete>> [list ::blt::tv::EditDone $w $e $x $y $ind $col $cind $data $styledata $cellstyle $ied $q(-endcmd) $q(-treelabel) $q(-vcmd)]
1509 if {![string is false $q(-tab)]} {
1510 set topts {}
1511 if {![string is true $q(-tab)]} {
1512 set topts $q(-tab)
1513 }
1514 bind $e <Tab> "event generate $e <<TreeViewEditComplete>>; [namespace current]::TabMove $w $ind $cind $topts; break"
1515 }
1516 event generate $w <<TreeViewEditStart>> -x $cind -y $ind
1517 if {[winfo exists $e] && $q(-startcmd) != {}} {
1518 if {[string first % $vcmd]>=0} {
1519 set ccmd [string map [list %% % %W $w %X $x %Y $y %# $ind %C $cind %V [list $data]] $q(-startcmd)]
1520 } else {
1521 set ccmd [concat $q(-startcmd) [list $w $col $ind] ]
1522 }
1523 namespace eval :: $ccmd
1524 }
1525 if {![winfo exists $e]} return
1526 $w edit -noscroll
1527 set tag [namespace current]
1528 bindtags $e [concat $tag [bindtags $e]]
1529 bind $tag <Destroy> { catch {[winfo parent %W] edit -scroll} }
1530 return
1531 }
1532
1533 proc ::blt::tv::commify {num {sep ,}} {
1534 # Make number comma seperated every 3 digits.
1535 while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}
1536 return $num
1537 }
1538
1539 proc ::blt::tv::EditCol { w x y } {
1540 # Main handler for cell-edit/toggle-open.
1541 if {![winfo exists $w]} return
1542 $w see current
1543 set e $w.edit
1544 if { [winfo exists $e] } { destroy $e }
1545 set ::blt::tv::curCol [set col [$w column nearest $x]]
1546 if {$col == {}} return
1547 set cind [$w column index $col]
1548 set ind [$w index @$x,$y]
1549 if { ![$w column cget $col -edit] } {
1550 if {$cind == 0} {
1551 Toggle $w $ind
1552 }
1553 return
1554 } elseif {$cind == 0} {
1555 set object {}
1556 $w nearest $x $y object
1557 if {$object != "label"} {
1558 Toggle $w $ind
1559 return
1560 }
1561 }
1562 EditCell $w $ind $col $x $y
1563 }
1564
1565
1566 proc blt::tv::EditColumn { w x y } {
1567 # Old edit function.
1568 $w see current
1569 if { [winfo exists $w.edit] } {
1570 destroy $w.edit
1571 }
1572 set col [$w column current]
1573 if {$col == {}} return
1574 set ::blt::tv::curCol $col
1575 if { ![$w edit -root -test $x $y] } {
1576 return
1577 }
1578 set ind [$w index @$x,$y]
1579 if {$ind == {}} return
1580 set data [$w entry set $ind $col]
1581 $w edit -root $x $y
1582 update
1583 focus $w.edit
1584 $w.edit selection range 0 end
1585 event generate $w <<TreeViewEditStart>> -x $x -y $y
1586 grab set $w.edit
1587 tkwait window $w.edit
1588 grab release $w.edit
1589 if {[winfo exists $w]} {
1590 event generate $w <<TreeViewEditEnd>> -x $x -y $y
1591 }
1592 }
1593
1594 proc ::blt::tv::SortTree {t column {ind {}} {uselabel 1} {see 1}} {
1595 # Sort the children of tree.
1596 set istree [$t column istree $column]
1597 if {$ind == {}} {
1598 set ind [lindex [$t curselection] 0]
1599 if {$ind == {}} {
1600 if {$istree} { set ind 0 } else { set ind focus }
1601 }
1602 }
1603 set ind [$t index $ind]
1604 set clst [$t entry children $ind]
1605 if {$clst == {}} return
1606 set slst {}
1607 foreach i $clst {
1608
1609 if {$istree} {
1610 set txt [expr {$uselabel?[$t entry cget $i -label]:[$t get $i]}]
1611 } else {
1612 set txt [$t entry set $i $column]
1613 }
1614 lappend slst [list $txt $i]
1615 }
1616 if {[set decreasing [$t sort cget -decreasing]]} {
1617 set dec -decreasing
1618 } else {
1619 set dec -increasing
1620 }
1621 #set decreasing [expr {!$decreasing}]
1622 #$t sort conf -decreasing $decreasing
1623 set mode [$t column cget $column -sortmode]
1624 if {$mode == "none"} {
1625 set mode [$t sort cget -mode]
1626 }
1627 if {$mode == "none"} return
1628 if {$mode == "command"} {
1629 set slst [lsort $dec -command [$t column cget $column -sortcommand] $slst]
1630 } else {
1631 set slst [lsort $dec -$mode $slst]
1632 }
1633 foreach i $slst {
1634 set oi [lindex $i 1]
1635 $t move $oi into $ind
1636 }
1637 if {$see} {
1638 set sel [$t curselection]
1639 if {$sel != {}} {
1640 after idle [list $t see [lindex $sel 0]]
1641 }
1642 }
1643 set cind [$t column index $column]
1644 event generate $t <<TreeViewSortTree>> -x $cind -y $ind
1645 }
1646
1647
1648 #
1649 # ButtonPress assignments
1650 #
1651 # B1-Enter start auto-scrolling
1652 # B1-Leave stop auto-scrolling
1653 # ButtonPress-2 start scan
1654 # B2-Motion adjust scan
1655 # ButtonRelease-2 stop scan
1656 #
1657 bind ${className} <ButtonPress-2> {
1658 focus %W
1659 }
1660
1661 bind ${className} <ButtonPress-2> {
1662 set blt::tv::cursor [%W cget -cursor]
1663 %W configure -cursor hand1
1664 %W scan mark %x %y
1665 }
1666
1667 bind ${className} <B2-Motion> {
1668 catch { %W scan dragto %x %y }
1669 }
1670
1671 bind ${className} <ButtonRelease-2> {
1672 catch { %W configure -cursor $blt::tv::cursor }
1673 }
1674
1675 bind ${className} <B1-Leave> {
1676 if { $blt::tv::scroll } {
1677 blt::tv::AutoScroll %W
1678 }
1679 }
1680
1681 bind ${className} <B1-Enter> {
1682 after cancel $blt::tv::afterId
1683 }
1684
1685 #
1686 # KeyPress assignments
1687 #
1688 # Up
1689 # Down
1690 # Shift-Up
1691 # Shift-Down
1692 # Prior (PageUp)
1693 # Next (PageDn)
1694 # Left
1695 # Right
1696 # space Start selection toggle of entry currently with focus.
1697 # Return Start selection toggle of entry currently with focus.
1698 # Home
1699 # End
1700 # F1
1701 # F2
1702 # ASCII char Go to next open entry starting with character.
1703 #
1704 # KeyRelease
1705 #
1706 # space Stop selection toggle of entry currently with focus.
1707 # Return Stop selection toggle of entry currently with focus.
1708
1709
1710 bind ${className} <KeyPress-Up> {
1711 blt::tv::MoveFocus %W up
1712 if { $blt::tv::space } {
1713 %W selection toggle focus
1714 }
1715 }
1716
1717 bind ${className} <KeyPress-Down> {
1718 blt::tv::MoveFocus %W down
1719 if { $blt::tv::space } {
1720 %W selection toggle focus
1721 }
1722 }
1723 bind ${className} <Control-KeyPress-n> [bind ${className} <KeyPress-Down>]
1724 bind ${className} <Control-KeyPress-p> [bind ${className} <KeyPress-Up>]
1725
1726 bind ${className} <Shift-KeyPress-Up> {
1727 blt::tv::MoveFocus %W up 1
1728 }
1729
1730 bind ${className} <Shift-KeyPress-Down> {
1731 blt::tv::MoveFocus %W down 1
1732 }
1733
1734 bind ${className} <KeyPress-Prior> {
1735 blt::tv::MovePage %W top
1736 }
1737
1738 bind ${className} <KeyPress-Next> {
1739 blt::tv::MovePage %W bottom
1740 }
1741 bind ${className} <Control-KeyPress-d> [bind ${className} <KeyPress-Next>]
1742 bind ${className} <Control-KeyPress-u> [bind ${className} <KeyPress-Prior>]
1743
1744 #bind ${className} <KeyPress-Left> {
1745 # %W close focus
1746 #}
1747 #bind ${className} <KeyPress-Right> {
1748 # %W open focus
1749 # %W see focus -anchor w
1750 #}
1751 proc blt::tv::MoveKey {w cnt} {
1752 set mode [$w cget -selectmode]
1753 set iscell [expr {$mode == "cell" || $mode == "multicell"}]
1754 if {!$iscell} { return [$w xview scroll $cnt unit] }
1755 set cells [$w selection cells]
1756 if {$cells != {}} {
1757 set vcols [$w col names -visible]
1758 foreach {ind col} $cells break
1759 set cind [lsearch $vcols $col]
1760 if {$cind >= 0} {
1761 set cind [expr {$cind+$cnt}]
1762 if {$cind>=[llength $vcols]} {
1763 set cind [expr {[llength $vcols]-1}]
1764 } elseif {$cind < 0} {
1765 set cind 0
1766 }
1767 set ncol [lindex $vcols $cind]
1768 $w selection clearall
1769 $w selection set $ind $ind $ncol
1770 $w column see $ncol
1771 }
1772 }
1773 }
1774
1775 proc blt::tv::MarkPos {w} {
1776 if { [$w cget -selectmode] == "single" } {
1777 if { [$w selection includes focus] } {
1778 $w selection clearall
1779 } else {
1780 $w selection clearall
1781 $w selection set focus
1782 }
1783 } elseif { [$w cget -selectmode] != "none" } {
1784 $w selection toggle focus
1785 }
1786 set blt::tv::space on
1787 }
1788
1789 bind ${className} <KeyPress-Left> { blt::tv::MoveKey %W -1}
1790 bind ${className} <KeyPress-Right> { blt::tv::MoveKey %W 1}
1791 #bind ${className} <KeyPress-Left> { %W xview scroll -1 unit}
1792 #bind ${className} <KeyPress-Right> { %W xview scroll 1 unit}
1793 bind ${className} <Control-KeyPress-Left> { %W xview scroll -1 page}
1794 bind ${className} <Control-KeyPress-Right> { %W xview scroll 1 page}
1795
1796 bind ${className} <KeyPress-space> { catch {blt::tv::MarkPos %W } }
1797
1798 bind ${className} <KeyRelease-space> {
1799 set blt::tv::space off
1800 }
1801
1802 #bind ${className} <KeyPress-Return> {
1803 # blt::tv::MoveFocus %W focus
1804 # set blt::tv::space on
1805 #}
1806
1807 #bind ${className} <KeyRelease-Return> {
1808 # set blt::tv::space off
1809 #}
1810
1811 bind ${className} <KeyPress-Return> {
1812 #set blt::tv::space on
1813 blt::tv::Toggle %W focus
1814 #set blt::tv::space off
1815 }
1816
1817 bind ${className} <KeyRelease-Return> {
1818 #set blt::tv::space off
1819 }
1820
1821 bind ${className} <KeyPress> {
1822 blt::tv::NextMatch %W %A %s
1823 }
1824
1825 bind ${className} <KeyPress-Home> {
1826 blt::tv::MoveFocus %W top
1827 }
1828
1829 bind ${className} <KeyPress-End> {
1830 blt::tv::MoveFocus %W bottom
1831 }
1832
1833 bind ${className} <Control-F1> {
1834 %W open -trees root
1835 }
1836
1837 bind ${className} <Control-F2> {
1838 eval %W close -trees root
1839 }
1840
1841 bind ${className} <Control-F3> {
1842 %W conf -flat [expr {![%W cget -flat]}]
1843 }
1844
1845 bind ${className} <Control-F4> {
1846 eval %W col conf [%W col names] -width 0
1847 }
1848
1849 bind ${className} <MouseWheel> {
1850 if {%D >= 0} {
1851 %W yview scroll [expr {-%D/30}] units
1852 } else {
1853 %W yview scroll [expr {(2-%D)/30}] units
1854 }
1855 }
1856
1857 if {[tk windowingsystem] == "x11"} {
1858 bind ${className} <4> { %W yview scroll -3 unit }
1859 bind ${className} <5> { %W yview scroll 3 unit }
1860 }
1861
1862 #
1863 # Differences between id "current" and operation nearest.
1864 #
1865 # set index [$w index current]
1866 # set index [$w nearest $x $y]
1867 #
1868 # o Nearest gives you the closest entry.
1869 # o current is "" if
1870 # 1) the pointer isn't over an entry.
1871 # 2) the pointer is over a open/close button.
1872 # 3)
1873 #
1874
1875 #
1876 # Edit mode assignments
1877 #
1878 # ButtonPress-3 Enables/disables edit mode on entry. Sets focus to
1879 # entry.
1880 #
1881 # KeyPress
1882 #
1883 # Left Move insertion position to previous.
1884 # Right Move insertion position to next.
1885 # Up Move insertion position up one line.
1886 # Down Move insertion position down one line.
1887 # Return End edit mode.
1888 # Shift-Return Line feed.
1889 # Home Move to first position.
1890 # End Move to last position.
1891 # ASCII char Insert character left of insertion point.
1892 # Del Delete character right of insertion point.
1893 # Delete Delete character left of insertion point.
1894 # Ctrl-X Cut
1895 # Ctrl-V Copy
1896 # Ctrl-P Paste
1897 #
1898 # KeyRelease
1899 #
1900 # ButtonPress-1 Start selection if in entry, otherwise clear selection.
1901 # B1-Motion Extend/reduce selection.
1902 # ButtonRelease-1 End selection if in entry, otherwise use last
1903 # selection.
1904 # B1-Enter Disabled.
1905 # B1-Leave Disabled.
1906 # ButtonPress-2 Same as above.
1907 # B2-Motion Same as above.
1908 # ButtonRelease-2 Same as above.
1909 #
1910 #
1911
1912
1913 # Standard Motif bindings:
1914
1915 bind ${className}Editor <ButtonPress-1> {
1916 %W icursor @%x,%y
1917 %W selection clear
1918 }
1919
1920 bind ${className}Editor <Left> {
1921 %W icursor prev
1922 %W selection clear
1923 }
1924
1925 bind ${className}Editor <Right> {
1926 %W icursor next
1927 %W selection clear
1928 }
1929
1930 bind ${className}Editor <Shift-Left> {
1931 set new [expr {[%W index insert] - 1}]
1932 if {![%W selection present]} {
1933 %W selection from insert
1934 %W selection to $new
1935 } else {
1936 %W selection adjust $new
1937 }
1938 %W icursor $new
1939 }
1940
1941 bind ${className}Editor <Shift-Right> {
1942 set new [expr {[%W index insert] + 1}]
1943 if {![%W selection present]} {
1944 %W selection from insert
1945 %W selection to $new
1946 } else {
1947 %W selection adjust $new
1948 }
1949 %W icursor $new
1950 }
1951
1952 bind ${className}Editor <Home> {
1953 %W icursor 0
1954 %W selection clear
1955 }
1956 bind ${className}Editor <Shift-Home> {
1957 set new 0
1958 if {![%W selection present]} {
1959 %W selection from insert
1960 %W selection to $new
1961 } else {
1962 %W selection adjust $new
1963 }
1964 %W icursor $new
1965 }
1966 bind ${className}Editor <End> {
1967 %W icursor end
1968 %W selection clear
1969 }
1970 bind ${className}Editor <Shift-End> {
1971 set new end
1972 if {![%W selection present]} {
1973 %W selection from insert
1974 %W selection to $new
1975 } else {
1976 %W selection adjust $new
1977 }
1978 %W icursor $new
1979 }
1980
1981 bind ${className}Editor <Delete> {
1982 if { [%W selection present]} {
1983 %W delete sel.first sel.last
1984 } else {
1985 %W delete insert
1986 }
1987 }
1988
1989 bind ${className}Editor <BackSpace> {
1990 if { [%W selection present] } {
1991 %W delete sel.first sel.last
1992 } else {
1993 set index [expr [%W index insert] - 1]
1994 if { $index >= 0 } {
1995 %W delete $index $index
1996 }
1997 }
1998 }
1999
2000 bind ${className}Editor <Control-space> {
2001 %W selection from insert
2002 }
2003
2004 bind ${className}Editor <Select> {
2005 %W selection from insert
2006 }
2007
2008 bind ${className}Editor <Control-Shift-space> {
2009 %W selection adjust insert
2010 }
2011
2012 bind ${className}Editor <Shift-Select> {
2013 %W selection adjust insert
2014 }
2015
2016 bind ${className}Editor <Control-slash> {
2017 %W selection range 0 end
2018 }
2019
2020 bind ${className}Editor <Control-backslash> {
2021 %W selection clear
2022 }
2023
2024 bind ${className}Editor <KeyPress> {
2025 blt::tv::InsertText %W %A
2026 }
2027
2028 # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
2029 # Otherwise, if a widget binding for one of these is defined, the
2030 # <KeyPress> class binding will also fire and insert the character,
2031 # which is wrong. Ditto for Escape, Return, and Tab.
2032
2033 bind ${className}Editor <Alt-KeyPress> {
2034 # nothing
2035 }
2036
2037 bind ${className}Editor <Meta-KeyPress> {
2038 # nothing
2039 }
2040
2041 bind ${className}Editor <Control-KeyPress> {
2042 # nothing
2043 }
2044
2045 bind ${className}Editor <KeyRelease-Escape> {
2046 %W cancel
2047 }
2048
2049 bind ${className}Editor <Return> {
2050 %W apply
2051 }
2052
2053 bind ${className}Editor <Shift-Return> {
2054 blt::tv::InsertText %W "\n"
2055 }
2056
2057 bind ${className}Editor <KP_Enter> {
2058 # nothing
2059 }
2060
2061 bind ${className}Editor <Tab> {
2062 # nothing
2063 }
2064
2065 if {![string compare $tcl_platform(platform) "macintosh"]} {
2066 bind ${className}Editor <Command-KeyPress> {
2067 # nothing
2068 }
2069 }
2070
2071 # On Windows, paste is done using Shift-Insert. Shift-Insert already
2072 # generates the <<Paste>> event, so we don't need to do anything here.
2073 if { [string compare $tcl_platform(platform) "windows"] != 0 } {
2074 bind ${className}Editor <Insert> {
2075 catch {blt::tv::InsertText %W [::tk::GetSelection %W CLIPBOARD]}
2076 #catch {blt::tv::InsertText %W [selection get -displayof %W]}
2077 }
2078 }
2079 bind ${className}Editor <<Paste>> {
2080 catch {blt::tv::InsertText %W [::tk::GetSelection %W CLIPBOARD]}
2081 }
2082
2083 bind ${className}Editor <<Copy>> { ::blt::tv::TextCopy %W 1 }
2084
2085 bind ${className} <<Copy>> { ::blt::tv::TextCopy %W }
2086
2087
2088 # Additional emacs-like bindings:
2089 bind ${className}Editor <Double-1> {
2090 set parent [winfo parent %W]
2091 %W cancel
2092 after idle {
2093 blt::tv::EditColumn $parent %X %Y
2094 }
2095 }
2096 bind ${className}Editor <ButtonPress-3> {
2097 set parent [winfo parent %W]
2098 %W cancel
2099 after idle {
2100 blt::tv::EditColumn $parent %X %Y
2101 }
2102 }
2103
2104 bind ${className}Editor <Control-a> {
2105 %W icursor 0
2106 %W selection clear
2107 }
2108
2109 bind ${className}Editor <Control-b> {
2110 catch {%W icursor [expr {[%W index insert] - 1}]}
2111 %W selection clear
2112 }
2113
2114 bind ${className}Editor <Control-d> {
2115 %W delete insert
2116 }
2117
2118 bind ${className}Editor <Control-e> {
2119 %W icursor end
2120 %W selection clear
2121 }
2122
2123 bind ${className}Editor <Control-f> {
2124 %W icursor [expr {[%W index insert] + 1}]
2125 %W selection clear
2126 }
2127
2128 bind ${className}Editor <Control-h> {
2129 if {[%W selection present]} {
2130 %W delete sel.first sel.last
2131 } else {
2132 set index [expr [%W index insert] - 1]
2133 if { $index >= 0 } {
2134 %W delete $index $index
2135 }
2136 }
2137 }
2138
2139 bind ${className}Editor <Control-k> {
2140 %W delete insert end
2141 }
2142
2143 if 0 {
2144 bind ${className}Editor <Control-t> {
2145 blt::tv::Transpose %W
2146 }
2147 bind ${className}Editor <Meta-b> {
2148 %W icursor [blt::tv::PreviousWord %W insert]
2149 %W selection clear
2150 }
2151 bind ${className}Editor <Meta-d> {
2152 %W delete insert [blt::tv::NextWord %W insert]
2153 }
2154 bind ${className}Editor <Meta-f> {
2155 %W icursor [blt::tv::NextWord %W insert]
2156 %W selection clear
2157 }
2158 bind ${className}Editor <Meta-BackSpace> {
2159 %W delete [blt::tv::PreviousWord %W insert] insert
2160 }
2161 bind ${className}Editor <Meta-Delete> {
2162 %W delete [blt::tv::PreviousWord %W insert] insert
2163 }
2164 # tkEntryNextWord -- Returns the index of the next word position
2165 # after a given position in the entry. The next word is platform
2166 # dependent and may be either the next end-of-word position or the
2167 # next start-of-word position after the next end-of-word position.
2168 #
2169 # Arguments:
2170 # w - The entry window in which the cursor is to move.
2171 # start - Position at which to start search.
2172
2173 if {![string compare $tcl_platform(platform) "windows"]} {
2174 proc ::blt::tv::NextWord {w start} {
2175 set pos [tcl_endOfWord [$w get] [$w index $start]]
2176 if {$pos >= 0} {
2177 set pos [tcl_startOfNextWord [$w get] $pos]
2178 }
2179 if {$pos < 0} {
2180 return end
2181 }
2182 return $pos
2183 }
2184 } else {
2185 proc ::blt::tv::NextWord {w start} {
2186 set pos [tcl_endOfWord [$w get] [$w index $start]]
2187 if {$pos < 0} {
2188 return end
2189 }
2190 return $pos
2191 }
2192 }
2193
2194 # PreviousWord --
2195 #
2196 # Returns the index of the previous word position before a given
2197 # position in the entry.
2198 #
2199 # Arguments:
2200 # w - The entry window in which the cursor is to move.
2201 # start - Position at which to start search.
2202
2203 proc ::blt::tv::PreviousWord {w start} {
2204 set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
2205 if {$pos < 0} {
2206 return 0
2207 }
2208 return $pos
2209 }
2210
2211 }
2212
2213 proc ::blt::tv::FmtStr {str {len 12}} {
2214 # Break a string into fixed size chunks.
2215 if {[string length $str]<=$len} {
2216 return $str
2217 }
2218 set lm [expr {$len-1}]
2219 set rc {}
2220 while {[string length $str]>0} {
2221 append rc [string range $str 0 $lm]
2222 set str [string range $str $len end]
2223 if {$str != {}} { append rc \n }
2224 }
2225 return $rc
2226 }
2227
2228 proc ::blt::tv::FmtString {str {len 12} {class alnum}} {
2229 # Wrap long strings at word boundries.
2230 if {[string length $str]<=$len} {
2231 return $str
2232 }
2233 if {[string is $class $str]} {
2234 return [FmtStr $str $len]
2235 }
2236 set rc {}
2237 set crc {}
2238 set lw 1
2239 foreach i [split $str {}] {
2240 set isw [string is $class $i]
2241 if {(($isw && $lw) || (!$isw && !$lw)) && [string length $crc]<$len} {
2242 append crc $i
2243 } else {
2244 lappend rc $crc
2245 set crc $i
2246 }
2247 set lw $isw
2248 }
2249 if {$crc != {}} {
2250 lappend rc $crc
2251 }
2252 set src {}
2253 set cln {}
2254 foreach i $rc {
2255 if {[string length $cln$i]<=$len} {
2256 append cln $i
2257 } else {
2258 if {$src != {}} { append src \n }
2259 append src $cln
2260 set cln $i
2261 }
2262 }
2263 append src \n $cln
2264 return $src
2265 }
2266
2267
2268