File: CanvasText.tcl

package info (click to toggle)
coccinella 0.96.20-9
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 13,184 kB
  • sloc: tcl: 124,744; xml: 206; makefile: 66; sh: 62
file content (902 lines) | stat: -rw-r--r-- 23,338 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
#  CanvasText.tcl ---
#  
#      This file is part of The Coccinella application. It implements the
#      text commands associated with the text tool.
#      
#  Copyright (c) 2000-2005  Mats Bengtsson
#  
#   This program is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#   
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#   
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#  
# $Id: CanvasText.tcl,v 1.15 2008-03-25 08:52:31 matben Exp $

package require sha1

package provide CanvasText 1.0

namespace eval ::CanvasText:: {

    # Array that holds private stuff.
    variable priv
    set priv(font)  {}
    set priv(sha1)  {}
    set priv(magic) 069819a0dfa9f2171e86c03a281f43f208ac3516
}


proc ::CanvasText::Init {wcan} {
    
    namespace eval [namespace current]::${wcan} {
	set buffer(str)  ""
	set buffer(text) ""
    }
    # @@@ window destroyed before this!
    bind $wcan <Destroy> [list [namespace current]::OnDestroy %W]
}

proc ::CanvasText::OnDestroy {wcan} {
    
    EvalBufferedText $wcan
    
    # Remove the namespace with the widget.
    namespace delete [namespace current]::${wcan}
}

# This could be done with 'bindtags' instead!!!!!!
 
# Perhaps canvas text item stuff should go in a more general package
# with specials like undo and network handling using callbacks.

# ::CanvasText::EditBind --
#
#       Sets up all canvas and canvas text item bindings.
#       Typically when the user clicks the text tool button.
#       
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       none

# Could this be done with 'bindtags' instead???

proc ::CanvasText::EditBind {wcan} {
    global  this
    
    $wcan bind text <Button-1> {
	::CanvasText::Hit %W [%W canvasx %x] [%W canvasy %y]
    }
    $wcan bind text <B1-Motion> {
	::CanvasText::Drag %W [%W canvasx %x] [%W canvasy %y]
    }
    $wcan bind text <Double-Button-1> {
	::CanvasText::SelectWord %W [%W canvasx %x] [%W canvasy %y]
    }
    $wcan bind text <Delete> {
	::CanvasText::Delete %W
    }
    
    # Swallow any commands on mac's. 
    if {[string match "mac*" $this(platform)]} {
	$wcan bind text <Command-Any-Key> {# nothing}
	# Testing... for chinese input method...
	$wcan bind text <Command-t> {
	    break
	}
    }
    $wcan bind text <BackSpace> {
	::CanvasText::Delete %W
    }
    $wcan bind text <Return> {
	::CanvasText::NewLine %W
    }
    $wcan bind text <KeyPress> {
	::CanvasText::Insert %W %A
    }
    $wcan bind text <Right> {
	::CanvasText::MoveRight %W
    }
    $wcan bind text <Left> {
	::CanvasText::MoveLeft %W
    }
    $wcan bind text <Up> {
	::CanvasText::SetCursor %W [::CanvasText::UpDownLine %W -1]
    }
    $wcan bind text <Down> {
	::CanvasText::SetCursor %W [::CanvasText::UpDownLine %W 1]
    }
    $wcan bind text <Shift-Left> {
	::CanvasText::KeySelect %W [::CanvasText::PrevPos %W insert]
    }
    $wcan bind text <Shift-Right> {
	::CanvasText::KeySelect %W [::CanvasText::NextPos %W insert]
    }

    # Ignore all Alt, Meta, and Control keypresses unless explicitly bound.
    # Otherwise, if a widget binding for one of these is defined, the
    # <KeyPress> class binding will also fire and insert the character,
    # which is wrong.  Ditto for Escape, and Tab.
    
    $wcan bind text <Alt-KeyPress>      {# nothing}
    $wcan bind text <Meta-KeyPress>     {# nothing}
    $wcan bind text <Control-KeyPress>  {# nothing}
    $wcan bind text <Escape>            {# nothing}
    $wcan bind text <KP_Enter>          {# nothing}
    $wcan bind text <Tab>               {# nothing}

    # Additional emacs-like bindings.
    $wcan bind text <Control-d> {
	::CanvasText::Delete %W 1
    }
    $wcan bind text <Control-a> {
	::CanvasText::InsertBegin %W
    }
    $wcan bind text <Control-e> {
	::CanvasText::InsertEnd %W
    }
    
    # These may interfere with canvas widget bindings for scrolling.
    $wcan bind text <Control-Left> {
	::CanvasText::SetCursor %W [::CanvasText::PrevWord %W]
    }
    $wcan bind text <Control-Right> {
	::CanvasText::SetCursor %W [::CanvasText::NextWord %W]
    }
    $wcan bind text <Control-Up> {
	::CanvasText::SetCursor %W 0
    }
    $wcan bind text <Control-Down> {
	::CanvasText::SetCursor %W end
    }
    $wcan bind text <Home> {
	::CanvasText::InsertBegin %W
    }
    $wcan bind text <End> {
	::CanvasText::InsertEnd %W
    }
}

# ::CanvasText::Copy --
#  
#       Just copies text from text items. If selected text, copy that,
#       else if text item has focus copy complete text item.
#       
# Arguments:
#       wcan      the canvas widget.
#       
# Results:
#       none

proc ::CanvasText::Copy {wcan} {
    variable priv
    
    Debug 2 "::CanvasText::Copy select item=[$wcan select item]"

    if {[$wcan select item] ne {}} { 
	clipboard clear
	set t [$wcan select item]
	set text [$wcan itemcget $t -text]
	set start [$wcan index $t sel.first]
	set end [$wcan index $t sel.last]
	set str [string range $text $start $end]
	clipboard append $str
	
	# Keep track of font in clipboard and a hash to see if changed
	# before pasting it.
	set priv(font) [$wcan itemcget $t -font]
	set priv(sha1) [sha1::sha1 "$priv(magic)$str"]
	#OwnClipboard $wcan
    }
}

proc ::CanvasText::OwnClipboard {wcan} {
    
    # this creates some weird behaviour???
    selection own -command [list [namespace current]::LostClipboard $wcan] \
      -selection CLIPBOARD $wcan
}

proc ::CanvasText::LostClipboard {wcan} {
    variable priv
    
    puts "::CanvasText::LostClipboard"
    set priv(font) {}
}

# ::CanvasText::SetFocus --
#
#       Puts a text insert bar in the canvas. If already text item under 
#       the mouse then give focus to that item. If 'forceNew', then always 
#       make a new item.
#
# Arguments:
#       wcan   the canvas widget.
#       x,y    the mouse coordinates.
#       forceNew   make new item regardless if text item under the mouse.
#       
# Results:
#       none

proc ::CanvasText::SetFocus {wcan x y {forceNew 0}} {
    global  prefs fontSize2Points
    
    set w [winfo toplevel $wcan]
    array set state [::WB::GetStateArray $w]

    focus $wcan
    set id [::CanvasUtils::FindIdFromOverlapping $wcan $x $y "text"]

    Debug 2 "SetFocus:: id=$id"
    
    # If we have an unsent buffer, be sure to send it first.
    if {$prefs(batchText)} {
	EvalBufferedText $wcan
    }
    if {($id eq "") || ([$wcan type $id] ne "text") || $forceNew} {
	
	# No text item under cursor, make a new empty text item.
	set utag [::CanvasUtils::NewUtag]
	set y [expr {$y - [font metrics [list $state(font)] -linespace]/2}]
	set cmd [list create text $x $y -text ""   \
	  -tags [list std text $utag] -anchor nw -fill $state(fgCol)]
	set fontsLocal [list $state(font) $fontSize2Points($state(fontSize)) \
	  $state(fontWeight)]
	
	# If 'useHtmlSizes', then transport the html sizes instead of point sizes.
	if {$prefs(useHtmlSizes)} {
	    set fontsRemote  \
	      [list $state(font) $state(fontSize) $state(fontWeight)]
	} else {
	    set fontsRemote $fontsLocal
	}
	set cmdlocal  [concat $cmd -font [list $fontsLocal]]
	set cmdremote [concat $cmd -font [list $fontsRemote]]
	set undocmd   [list delete $utag]

	set redo [list ::CanvasUtils::CommandExList $w  \
	  [list [list $cmdlocal local] [list $cmdremote remote]]]
	set undo [list ::CanvasUtils::Command $w $undocmd]
	eval $redo
	undo::add [::WB::GetUndoToken $wcan] $undo $redo
	::CanvasFile::SetUnsaved $wcan

	$wcan focus $utag
	$wcan select clear
	$wcan icursor $utag 0
    }
}

# ::CanvasText::Insert --
#
#       Inserts text string 'char' at the insert point of the text item
#       with focus. Handles newlines as well.
#
# Arguments:
#       wcan   the canvas widget.
#       char   the char or text string to insert.
#       
# Results:
#       none

proc ::CanvasText::Insert {wcan char} {
    global  this prefs
    variable priv
        
    upvar ::CanvasText::${wcan}::buffer buffer
        
    set punct {[.,;?!]}
    set nl_ "\\n"
    
    # First, find out if there are any text item with focus.
    # If not, then make one.
    if {[$wcan focus] eq ""} {
	
    }
    set w [winfo toplevel $wcan]
    
    # Find the 'utag'.
    set utag [::CanvasUtils::GetUtag $wcan focus]
    if {$utag eq "" || $char eq ""}	 {
	Debug 4 "Insert:: utag == {}"
	return
    }
    set itfocus [$wcan focus]
    
    # The index of the insertion point.
    set ind [$wcan index $itfocus insert]

    # Mac text bindings: delete selection before inserting.
    if {[string match "mac*" $this(platform)] ||   \
      ($this(platform) eq "windows")} {
	if {![catch {selection get} s]} {
	    if {$s ne ""} {
		Delete $wcan
		selection clear
	    }
	}
    }
    
    # If this is an empty text item then reuse any cached font.
    if {([$wcan itemcget $itfocus -text] eq "") && ($priv(font) ne {})} {
	if {[string equal $priv(sha1) [sha1::sha1 "$priv(magic)$char"]]} {
	    ::CanvasUtils::ItemConfigure $wcan $itfocus -font $priv(font)
	}
    }
    
    # The actual canvas text insertion; note that 'ind' is found above.
    set cmd [list insert $itfocus insert $char]
    set undocmd [list dchars $utag $ind [expr {$ind + [string length $char]}]]
    set redo [list ::CanvasUtils::Command $w $cmd]
    set undo [list ::CanvasUtils::Command $w $undocmd]    
    eval {$wcan} $cmd
    undo::add [::WB::GetUndoToken $wcan] $undo $redo

    ::CanvasFile::SetUnsaved $wcan
    
    # Need to treat the case with actual newlines in char string.
    # Write to all other clients; need to make a one liner first.
    regsub -all "\n" $char $nl_ oneliner
    if {$prefs(batchText)} {	
	if {[string length $buffer(str)] == 0} {
	    set buffer(ind)  $ind
	    set buffer(utag) $utag
	}
	append buffer(str) $oneliner
	set buffer(text) [$wcan itemcget $utag -text]
	
	if {[string match *${punct}* $char]} {
	    EvalBufferedText $wcan
	} else {
	    ScheduleTextBuffer $wcan
	}
    } else {
	::WB::SendMessageList $w [list [list insert $utag $ind $oneliner]]
    }
}

# ::CanvasText::SetCursor --
# 
# 
#
# Arguments:
# w -		The canvas window.
# pos -		The desired new position for the cursor in the text item.

proc ::CanvasText::SetCursor {wcan pos} {

    set id [$wcan focus]
    $wcan select clear
    $wcan icursor $id $pos
}

proc ::CanvasText::PrevPos {wcan pos} {
    
    set id [$wcan focus]
    set ind [$wcan index $id $pos]
    if {$ind == 0} {
	return 0
    } else {
	return [expr {$ind-1}]
    }
}

proc ::CanvasText::NextPos {wcan pos} {
    
    set id [$wcan focus]
    set str [$wcan itemcget $id -text]
    set ind [$wcan index $id $pos]
    if {$ind == [string length $str]} {
	return $ind
    } else {
	return [expr {$ind+1}]
    }
}

proc ::CanvasText::NextWord {wcan} {
    
    set id [$wcan focus]
    set str [$wcan itemcget $id -text]
    set ind [$wcan index $id insert]
    set next [tcl_startOfNextWord $str $ind]
    if {$next == -1} {
	set next end
    }
    return $next
}

proc ::CanvasText::PrevWord {wcan} {
    
    set id [$wcan focus]
    set str [$wcan itemcget $id -text]
    set ind [$wcan index $id insert]
    set prev [tcl_startOfPreviousWord $str $ind]
    if {$prev == -1} {
	set prev 0
    }
    return $prev
}

# ::CanvasText::MoveRight --
#
#       Move insert cursor one step to the right.
#
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       none

proc ::CanvasText::MoveRight {wcan} {
    global  this
    
    set foc [$wcan focus]
    
    # Mac text bindings: remove selection then move insert to end.
    if {[string match "mac*" $this(platform)] ||  \
      $this(platform) eq "windows"} {
	
	# If selection.
	if {![catch {selection get} s]} {
	    if {$s ne ""} {
		$wcan icursor $foc [expr {[$wcan index $foc sel.last] + 1}]
		$wcan select clear
	    }
	} else {
	    $wcan icursor $foc [expr {[$wcan index $foc insert] + 1}]
	}
    } else {
	$wcan icursor $foc [expr {[$wcan index $foc insert] + 1}]
    }
}

# ::CanvasText::MoveLeft --
#
#       Move insert cursor one step to the left.
#
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       none

proc ::CanvasText::MoveLeft {wcan} {
    global  this
    
    set foc [$wcan focus]
    
    # Mac text bindings: remove selection then move insert to first.
    if {[string match "mac*" $this(platform)] ||  \
      [string equal $this(platform) "windows"]} {
	
	# If selection.
	if {![catch {selection get} s]} {
	    if {$s ne ""} {
		$wcan icursor $foc [expr {[$wcan index $foc sel.first] + 0}]
		$wcan select clear
	    }
	} else {
	    $wcan icursor $foc [expr {[$wcan index $foc insert] - 1}]
	}
    } else {
	$wcan icursor $foc [expr {[$wcan index $foc insert] - 1}]
    }
}

# Find index of new character. Only for left justified text.

proc ::CanvasText::UpDownLine {wcan dir} {
    
    set id [$wcan focus]
    set ind [$wcan index $id insert]
    set str [$wcan itemcget $id -text]
    
    # Up one line.
    if {$dir == -1} {
	set prevNL  [string last \n $str [expr {$ind - 1}]]
	set prev2NL [string last \n $str [expr {$prevNL - 1}]]
	
	# If first line.
	if {$prevNL == -1} {
	    return $ind
	}
	set ncharLeft [expr {$ind - $prevNL - 1}]
	set new [min [expr {$prev2NL + $ncharLeft + 1}] $prevNL]
    } else {
	
	# Down one line.
	set prevNL  [string last \n $str [expr {$ind - 1}]]
	set nextNL  [string first \n $str [expr {$prevNL + 1}]]
	set next2NL [string first \n $str [expr {$nextNL + 1}]]
	
	# If last line.
	if {$nextNL == -1} {
	    return $ind
	}
	set ncharLeft [expr {$ind - $prevNL - 1}]
	if {$next2NL == -1} {
	    set new [expr {$nextNL + $ncharLeft + 1}]
	} else {
	    set new [min [expr {$nextNL + $ncharLeft + 1}] $next2NL]
	}
    }
    return $new
}

# ::CanvasText::KeySelect --
# 
#       It moves the cursor to the new position, then extends the selection
#       to that position.

proc ::CanvasText::KeySelect {wcan new} {
    
    set id [$wcan focus]
    set insert [$wcan index $id insert]
    if {$new == $insert} {
	return
    }
    if {[$wcan select item] eq ""} {
	$wcan select from $id insert
	if {$new >= $insert} {
	    $wcan select to $id [expr {$new - 1}]
	} else {
	    $wcan select to $id $new 
	}
    } else {
	set first [$wcan index $id sel.first]
	set last  [$wcan index $id sel.last]
	incr last
	set right  [expr {$new > $insert ? 1 : 0}]
	set inside [expr {($new >= $first) && ($new <= $last) ? 1 : 0}]
	if {$new == $first} {
	    $wcan select clear
	} elseif {$new == $last && $right} {
	    $wcan select clear
	} else {
	    if {$new >= $first} {
		if {$right && $inside} {
		    set to $new
		} else {
		    set to [expr {$new - 1}]
		}
	    } else {
		set to $new
	    }
	    $wcan select to $id $to
	}
    }
    $wcan icursor $id $new
}

proc ::CanvasText::InsertBegin {wcan} {
    
    set foc [$wcan focus]
    
    # Find index of new character. Only for left justified text.
    set ind [expr {[$wcan index $foc insert] - 1}]
    set str [$wcan itemcget $foc -text]
    set prevNL [expr {[string last \n $str $ind] + 1}]
    if {$prevNL == -1} {
	$wcan icursor $foc 0
    } else {
	$wcan icursor $foc $prevNL
    }
}

proc ::CanvasText::InsertEnd {wcan} {
    
    set foc [$wcan focus]
    
    # Find index of new character. Only for left justified text.
    set ind [$wcan index $foc insert]
    set str [$wcan itemcget $foc -text]
    set nextNL [string first \n $str $ind]
    if {$nextNL == -1} {
	$wcan icursor $foc end
    } else {
	$wcan icursor $foc $nextNL
    }
}

# ::CanvasText::Hit --
#
#       Called when clicking a text item with the text tool selected.
#
# Arguments:
#       wcan   the canvas widget.
#       x,y    the mouse coordinates.
#       select   
#       
# Results:
#       none

proc ::CanvasText::Hit {wcan x y {select 1}} {

    Debug 2 "::CanvasText::Hit select=$select"

    $wcan focus current
    $wcan icursor current @$x,$y
    $wcan select clear
    $wcan select from current @$x,$y
}

# ::CanvasText::Drag --
#
#       Text selection when dragging the mouse over a text item.
#
# Arguments:
#       wcan   the canvas widget.
#       x,y    the mouse coordinates.
#       
# Results:
#       none

proc ::CanvasText::Drag {wcan x y} {
    global  this
    
    ::CanvasCmd::DeselectAll $wcan
    $wcan select to current @$x,$y
    
    # Mac text bindings.????
    if {[string match "mac*" $this(platform)]} {
	#$wcan focus
    }
}

# ::CanvasText::SelectWord --
#
#       Typically selects wholw word when double clicking it.
#
# Arguments:
#       wcan   the canvas widget.
#       x,y    the mouse coordinates.
#       
# Results:
#       none

proc ::CanvasText::SelectWord {wcan x y} {
    
    ::CanvasCmd::DeselectAll $wcan
    $wcan focus current
    
    set id [$wcan find withtag current]
    if {$id eq ""} {
	return
    }
    if {[$wcan type $id] ne "text"} {
	return
    }
    set txt [$wcan itemcget $id -text]
    set ind [$wcan index $id @$x,$y]
    
    # Find the boundaries of the word and select word.
    $wcan select from   $id [string wordstart $txt $ind]
    $wcan select adjust $id [expr {[string wordend $txt $ind] - 1}]
}

# ::CanvasText::NewLine --
#
#       Insert a newline in a text item. Careful when sending it to remote
#       clients; double escaped.
#
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       none

proc ::CanvasText::NewLine {wcan} {
    global  prefs fontPoints2Size
    
    set nl_ "\\n"
    set w [winfo toplevel $wcan]
    
    # Find the 'utag'.
    set utag [::CanvasUtils::GetUtag $wcan focus]
    if {$utag eq ""}	 {
	return
    }
    
    # If we are buffering text, be sure to send buffer now if any.
    if {$prefs(batchText)} {
	EvalBufferedText $wcan
    }
    set ind [$wcan index [$wcan focus] insert]
    if {$prefs(wb,nlNewText)} {
	set str [$wcan itemcget $utag -text]
	set right [string range $str $ind end]
	set cmds {}
	set undocmds {}
	if {$right ne ""} {
	    lappend cmds [list [list dchars $utag $ind end] all]
	    lappend undocmds [list insert $utag end $right]
	}
	
	array set opts [::CanvasUtils::GetItemOpts $wcan $utag]
	set utagNew [::CanvasUtils::NewUtag]
	set opts(-tags) [list std text $utagNew]
	set opts(-text) $right
	lassign [$wcan bbox $utag] x1 y1 x2 y2
	array set fmetrics [font metrics $opts(-font)]
	set y [expr {$y2 + $fmetrics(-descent)}]
	
	# Local command.
	set cmd [concat create text $x1 $y [array get opts]]
	lappend cmds [list $cmd local]
	
	# Remote command.
	set size [lindex $opts(-font) 1]
	lset opts(-font) 1 $fontPoints2Size($size)
	set cmd [concat create text $x1 $y [array get opts]]
	lappend cmds [list $cmd remote]
	lappend undocmds [list delete $utagNew]
	set redo [list ::CanvasUtils::CommandExList $w $cmds]
	set undo [list ::CanvasUtils::CommandList $w $undocmds]
      } else {
	set cmdlocal [list insert $utag $ind \n]
	set cmdremote [list insert $utag $ind $nl_]
	set undocmd [list dchars $utag $ind]	
	set redo [list ::CanvasUtils::CommandExList $w  \
	  [list [list $cmdlocal local] [list $cmdremote remote]]]
	set undo [list ::CanvasUtils::Command $w $undocmd]
    }

    eval $redo
    undo::add [::WB::GetUndoToken $wcan] $undo $redo
    ::CanvasFile::SetUnsaved $wcan

    if {$prefs(wb,nlNewText)} {
	$wcan focus $utagNew
    }
}

# ::CanvasText::Delete --
#
#       Called when doing text 'cut' or pressing the Delete key.
#       A backspace if selected text deletes that text.
#       A backspace if text item has focus deletes text left of insert cursor.
#
# Arguments:
#       w      the canvas widget.
#       offset (D=0) is 1 if we want to delete a character right of insertion
#              point (control-d)
#       
# Results:
#       none

proc ::CanvasText::Delete {wcan {offset 0}} {
    global  prefs
    variable priv
    
    Debug 2 "::CanvasText::Delete"

    set idfocus [$wcan focus]
    set utag [::CanvasUtils::GetUtag $wcan focus]
    if {$utag eq ""} {
	return
    }
    set w [winfo toplevel $wcan]
	
    # If we have an unsent buffer, be sure to send it first.
    if {$prefs(batchText)} {
	EvalBufferedText $wcan
    }
    
    if {[$wcan select item] ne ""} {
	set sfirst [$wcan index $idfocus sel.first]
	set slast [$wcan index $idfocus sel.last]
	set str [$wcan itemcget $idfocus -text]
	set str [string range $str $sfirst $slast]
	set cmd [list dchars $utag $sfirst $slast]
	set undocmd [list insert $utag $sfirst $str]
    } elseif {$idfocus ne {}} {
	set ind [expr {[$wcan index $idfocus insert] - 1 + $offset}]
	set str [$wcan itemcget $idfocus -text]
	set str [string index $str $ind]
	set cmd [list dchars $utag $ind]
	set undocmd [list insert $utag $ind $str]
    }
    if {[info exists cmd]} {
	set redo [list ::CanvasUtils::Command $w $cmd]
	set undo [list ::CanvasUtils::Command $w $undocmd]    
	eval $redo
	undo::add [::WB::GetUndoToken $wcan] $undo $redo
	::CanvasFile::SetUnsaved $wcan
    }
}

# ::CanvasText::Paste --
#
#       Unix style paste using button 2.
#       
# Arguments:
#       wcan    the canvas widget.
#       x,y
#       
# Results:
#       none

proc ::CanvasText::Paste {wcan {x {}} {y {}}} {
    
    Debug 2 "::CanvasText::Paste"
    
    # If no selection just return.
    if {[catch {selection get} _s] &&   \
      [catch {selection get -selection CLIPBOARD} _s]} {
	Debug 2 "\t no selection"
	return
    }
    Debug 2 "\t CanvasTextPaste:: selection=$_s"
    
    # Once the text string is found use...
    Insert $wcan $_s
}

# ::CanvasText::ScheduleTextBuffer --
#
#       Schedules a send operation for our text inserts.
#       
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       none.

proc ::CanvasText::ScheduleTextBuffer {wcan} {
    global  prefs
    
    upvar ::CanvasText::${wcan}::buffer buffer
    
    if {[info exists buffer(afterid)]} {
	after cancel $buffer(afterid)
    }
    set buffer(afterid) [after [expr {$prefs(batchTextms)}]   \
      [list [namespace current]::EvalBufferedText $wcan]]
}

# ::CanvasText::EvalBufferedText --
#
#       This is the proc where buffered text are sent to clients.
#       Buffer emptied.
#       Can be called *after* widget is destroyed!
#       
# Arguments:
#       wcan   the canvas widget.
#       
# Results:
#       any registered send message hook invoked.

proc ::CanvasText::EvalBufferedText {wcan} {

    upvar ::CanvasText::${wcan}::buffer buffer
    
    if {[info exists buffer(afterid)]} {
	after cancel $buffer(afterid)
	unset buffer(afterid)
    }
    
    # Run all registered hooks like speech.
    # @@@ This is not good since it repeates the complete string.
    if {[info exists buffer(utag)] && [string length $buffer(str)]} {
	::hooks::run whiteboardTextInsertHook me $buffer(text)
    }
    
    if {[string length $buffer(str)]} {
	set w ".[lindex [split $wcan .] 1]"
	::WB::SendMessageList $w  \
	  [list [list insert $buffer(utag) $buffer(ind) $buffer(str)]]
    }    
    set buffer(str) ""
}

#-------------------------------------------------------------------------------