File: spreadsheet.tcl

package info (click to toggle)
tkgate 2.1%2Brepack-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,384 kB
  • sloc: ansic: 62,300; tcl: 20,345; xml: 2,731; yacc: 1,177; lex: 839; sh: 664; makefile: 180; perl: 39
file content (1300 lines) | stat: -rw-r--r-- 30,478 bytes parent folder | download | duplicates (5)
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
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
#   Copyright (C) 1987-2015 by Jeffery P. Hansen
#
#   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 2 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, write to the Free Software Foundation, Inc.,
#   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Last edit by hansen on Sun Jan 18 15:10:19 2009
#

#
# If we run this file standalone, enter test mode.
#
if {![info exists tkg_progName]} {
  set spreadsheet_test 1
  source "misc.tcl"
  source "dropbox.tcl"
} else {
  set spreadsheet_test 0
}

#############################################################################
#
# Public methods for SpreadSheet
#     size w			Get number of rows
#     getselection w		Get list of selected rows
#     setselection w idx	Set selected item
#     see w idx			Make sure the item idx is selected
#     clearselection w		Clear the selection
#     get w p			Get row at the specified position
#     put w p item		Put a row at a specified position
#     getcell w p c		Get cell at the specified position
#     putcell w p c item	Put cell at the specified position
#     insert w p item		Insert a row at a specified position
#     delete w items		Delete an item
#     create w options		Create a new spreadsheet
#     addcolumn w options	Add a column
#     flush w                   Deletes all items
#     entryValue		Value of cell that is being entered
#
namespace eval SpreadSheet {
  variable parms
  variable focusSave
  variable cell
  variable lastclick 0
  variable dragspot ""
  variable asActive 0
  variable asEvent ""
  variable asSpeed 250
  variable entryValue

  proc shift {v d} {
    set L {}

    foreach e $v {
      lappend L [expr $e + $d]
    }

    return $L
  }


  #
  # Map a physical row to the effective data row
  #
  proc getEffectiveRow {w r} {
    variable parms
    set P $parms($w:position)
    return [expr $r + $P - 1]
  }

  #
  # Map an effective data row to a physical row
  #
  proc getPhysicalRow {w r} {
    variable parms
    set P $parms($w:position)
    return [expr $r - $P + 1]
  }

  #
  # Show the current selection assuming all cells are painted the unselected color
  #
  proc show_selection {w} {
    variable parms
    variable cell

    set P $parms($w:position)
    set Rmax $parms($w:height)
    set Cmax [llength $parms($w:colwidth)]

    if { $parms($w:grab) } {
       set color $parms($w:grabcolor)
    } else {
      set color $parms($w:selectcolor)
    }



    foreach er $parms($w:selection) {
      set r [expr $er - $P + 1]
      if { $r > 0 && $r < $Rmax } {
	for { set c 0 } { $c < $Cmax } { incr c } {
	  $w.c${r}_$c configure -bg $color
	}
      }
    }
  }

  #
  # Add a new row to the selection.
  #
  proc addto_selection {w er} {
    variable parms

    #
    # If row is already selected, then do nothing
    #
    if { [lsearch $parms($w:selection) $er] >= 0 } {
    }

    set P $parms($w:position)
    set Rmax $parms($w:height)
    set Cmax [llength $parms($w:colwidth)]

    #
    # If requested row is not in current data range, then do nothing
    #
    if { $er < 0 || $er >= $parms($w:numrows) } {
      return
    }

    if { $parms($w:selectmode) == "single" } {
      clearselection $w
      set parms($w:selection) $er
    } else {
      lappend parms($w:selection) $er
    }


    set r [expr $er - $P + 1]
    if { $r > 0 && $r < $Rmax } {
      for { set c 0 } { $c < $Cmax } { incr c } {
	$w.c${r}_$c configure -bg $parms($w:selectcolor)
      }
    }
  }

  proc advanceEntry {w K r c} {
    variable parms

    clearEntrySelect $w

    set Rmax $parms($w:height)
    set er [getEffectiveRow $w $r]

    switch $K {
      Tab {
	while {1} {
	  incr c
	  if { $c >= [llength $parms($w:colwidth)] } {
	    set c 0
	    if {[expr $r + 1] >= $Rmax } {
	      SpreadSheet::yview $w scroll 1 unit
	      update
	    } else {
	      incr r
	    }
	  }
	  if {![entrySelect $w $r $c] } { break }
	}
      }
      Up {
	if { $er == 0 } {
	  entrySelect $w $r $c
	} else {
	  if { $r > 1 } {
	    incr r -1
	    entrySelect $w $r $c
	  } else {
	    clearEntrySelect $w
	    incr parms($w:position) -1
	    repaint $w
	    entrySelect $w $r $c
	  }
	}
      }
      Down {
	if { $er >= $parms($w:numrows) } {
	  entrySelect $w $r $c
	} else {
	  if { [expr $r + 1] < $parms($w:height) } {
	    incr r
	    entrySelect $w $r $c
	  } else {
	    clearEntrySelect $w
	    incr parms($w:position)
	    repaint $w
	    entrySelect $w $r $c
	  }
	}
      }
    }
  }

  #############################################################################
  #
  # Select a cell for entry.  Returns 1 if this is an entry that should be
  # skipped by the tab key
  #
  proc entrySelect {w r c} {
    variable parms
    variable cell
    variable focusSave

    set focusSave($w) [focus]
#    puts "entrySelect focus=$focusSave($w) takefocus=[$focusSave($w) cget -takefocus]"


    clearselection $w

    if { $r >= $parms($w:height) } { return 0 }

    set er [getEffectiveRow $w $r]

    if { $parms($w:entrycommand) != ""} {
      if { $er == $parms($w:numrows) } {
#       focus is not working correctly
#	set er $parms($w:numrows)
#	set r [getPhysicalRow $w $er]

	if {[$parms($w:entrycommand) canappend $w $er $c ""]} {
	  incr parms($w:numrows)
	  set xx [$parms($w:entrycommand) initentry $w $er $c ""]
	  put $w $er $xx
	  repaint $w

	  if {![$parms($w:entrycommand) canenter $w $er $c $cell($w:$er:$c)]} {
	    set c 0
	    while {![$parms($w:entrycommand) canenter $w $er $c $cell($w:$er:$c)]} { incr c }
	  }
	}
      } elseif { $er < $parms($w:numrows) } {
	if {![$parms($w:entrycommand) canenter $w $er $c $cell($w:$er:$c)]} {
	  return 1
	}
      }
    }

    if { $er >= $parms($w:numrows) } { return 0 }

    set width [lindex $parms($w:colwidth) $c]
    addto_selection $w $er

    destroy $w.c${r}_$c

    set SpreadSheet::entryValue $SpreadSheet::cell($w:$er:$c)

    set widget_ok 0
    if { $parms($w:entrycommand) != ""} {
      if { [$parms($w:entrycommand) entrywidget $w $r $c $w.c${r}_$c $width SpreadSheet::entryValue] != 0 } {
	set widget_ok 1
      }
    }
    if { !$widget_ok } {
      entry $w.c${r}_$c  -bd 1 -relief sunken -width $width
      update
      $w.c${r}_$c configure -bg $parms($w:selectcolor) -textvariable SpreadSheet::entryValue -highlightthickness 1
      update

      focus $w.c${r}_$c
      $w.c${r}_$c icursor end
      $w.c${r}_$c selection range 0 end
    }

    bind $w.c${r}_$c <Destroy> "SpreadSheet::clearEntrySelect $w"

    tabBindings $w.c${r}_$c $w $r $c

    grid $w.c${r}_$c -row $r -column $c -ipadx 0 -ipady 0 -sticky ew
    update
    set parms($w:entryselection)    [list $r $c]

    return 0
  }

  #############################################################################
  #
  # End entering data into a cell
  #
  proc clearEntrySelect {w} {
    variable parms
    variable cell
    variable focusSave

    if { $parms($w:entryselection) == "" } { return }

    set r [lindex $parms($w:entryselection) 0]
    set c [lindex $parms($w:entryselection) 1]
    set er [getEffectiveRow $w $r]

    set width [lindex $parms($w:colwidth) $c]

    if { $parms($w:entrycommand) != ""} {
      set cell($w:$er:$c) [$parms($w:entrycommand) close $w $r $c $SpreadSheet::entryValue]
    } else {
      set cell($w:$er:$c) $SpreadSheet::entryValue
    }
    $parms($w:entrycommand) closenotify $w

    #
    # Unbind the deletion call to this function.
    #
#    puts "clearEntrySelect lastfor=[focus -lastfor .]"
    bind $w.c${r}_$c <Destroy> ""
    destroy $w.c${r}_$c
#    puts "clearEntrySelect focus=[focus]"

    #
    # Try to restore the focus
    #
    catch {
      focus $focusSave($w)
#      puts "clearEntrySelect refocus=[focus]"
    }



    #
    # We may have called this function on a destroy event in which case the following
    # code will fail.
    #
    catch {
      label $w.c${r}_$c  -bd 1 -relief raised -width $width \
	  -bg $parms($w:entrycolor) -anchor w -text $SpreadSheet::cell($w:$er:$c)
      grid $w.c${r}_$c -row $r -column $c -ipadx 1 -ipady 1 -sticky ew
      setBindings $w $r $c
    }

    set parms($w:entryselection)    ""
  }


  proc size {w} {
    variable parms
    return $parms($w:numrows)
  }

  proc setselection {w sel} {
    variable parms

    set parms($w:selection) $sel
    reqRepaint $w setselction
  }

  #
  # Make sure that entry idx is in the visible range
  #
  proc see {w idx} {
    variable parms

    set Rmax [expr $parms($w:height) - 1]
    set P $parms($w:position)

    if {$idx < $P} {
      # Index is above top of visible range
      set parms($w:position) $idx
      reqRepaint $w see-1
    } elseif { [expr $idx - $P + 1] >= $Rmax } {
      # Index is below bottom of visible range

      set pos [expr $idx - $Rmax + 1]

      if {[expr [winfo y $w.c${Rmax}_0] + [winfo height $w.c${Rmax}_0]] > [winfo height $w] } {
	if {$pos < $Rmax} {
	  incr pos
	}
      }

      set parms($w:position) $pos
      reqRepaint $w see-2
    }

  }

  proc getselection {w} {
    variable parms
    return $parms($w:selection)
  }

  proc clearselection {w} {
    variable parms
    variable cell

    set P $parms($w:position)
    set Rmax $parms($w:height)
    set Cmax [llength $parms($w:colwidth)]

    clearEntrySelect $w

    foreach er $parms($w:selection) {
      set r [expr $er - $P + 1]
      if { $r > 0 && $r < $Rmax } {
	for { set c 0 } { $c < $Cmax } { incr c } {
	  $w.c${r}_$c configure -bg $parms($w:entrycolor)
	}
      }
    }
    set parms($w:selection) ""
  }


  proc reqRepaint {w args} {
    variable parms

#    puts "reqRepaint $w $args"

    if { !$parms($w:updatepending) } {
      set parms($w:updatepending) 1
      after idle "SpreadSheet::repaint $w"
    }
  }


  #############################################################################
  #
  # Repaint the display from the stored cell data.
  #
 proc repaint {w} {
    variable parms
    variable cell


    set parms($w:updatepending) 0

    set P $parms($w:position)
    set Rmax $parms($w:height)
    set N $parms($w:numrows)
    set Cmax [llength $parms($w:colwidth)]
    set types $parms($w:types)

#    puts "repaint $w  [winfo width $w]x[winfo height $w]  Rmax=$Rmax"

    for { set r 1 } { $r < $Rmax } { incr r } {
      for { set c 0 } { $c < $Cmax } { incr c } {
	set er [expr $r + $P - 1]

	if {$er < $N} {
	  set text $cell($w:$er:$c)
	} else {
	  set text ""
	}
	catch {
	  set type [lindex $types $c]

	  if {$type == "image" } {
	    $w.c${r}_$c configure -image $text -bg $parms($w:entrycolor) -anchor center
	  } else {
	    $w.c${r}_$c configure -text $text -bg $parms($w:entrycolor)
	  }

	}
      }
    }
    show_selection $w

    if { $parms($w:yscrollcommand) != "" } {
      set N $parms($w:numrows)

      set start [expr ($P+0.0)/($N+1.0)]
      set stop [expr ($P + $Rmax+0.0)/($N+1.0)]

      eval "$parms($w:yscrollcommand) $start $stop"
    }
  }

  proc yview {w args} {
    variable parms

    set cmd [lindex $args 0]
    set n [lindex $args 1]

    set P $parms($w:position)
    set Rmax $parms($w:height)
    set N $parms($w:numrows)

    clearEntrySelect $w

    if {$cmd == "moveto" } {
      set P [expr int($N*$n)]
    } elseif {$cmd == "scroll" } {
      set P [expr int($P + $n)]
    }

    if { $P < 0 } { set P 0 }
    if { $P >= $N } { set P $N }
    set parms($w:position) $P
    reqRepaint $w yview
  }

  #
  # Delete items
  #
  proc delete {w args} {
    variable parms
    variable cell

    set dlist {}
    foreach ilist $args {
      foreach item $ilist {
	lappend dlist $item
      }
    }
    set dlist [lsort -integer $dlist]
    set first [lindex $dlist 0]

    if { [llength $dlist] == 0 } { return }

    set Cmax [llength $parms($w:colwidth)]

    set dst_r [lindex $dlist 0]
    set src_r [expr $dst_r + 1]
    set N $parms($w:numrows)

    set Dcount 1

    while { $src_r < $N } {
      if { [lsearch $dlist $src_r] >= 0 } {
	incr src_r
	incr Dcount
	continue
      }

      for { set c 0 } { $c < $Cmax } { incr c } {
	set cell($w:$dst_r:$c) $cell($w:$src_r:$c)
      }
      incr dst_r
      incr src_r
    }

    incr parms($w:numrows) [expr -$Dcount]
    clearselection $w
    if { $first < [size $w] } {
      setselection $w $first
    }
    reqRepaint $w delete

    catch { $parms($w:entrycommand) deletenotify $w }
  }

  proc flush {w} {
    variable parms

    clearselection $w
    set parms($w:numrows) 0
    reqRepaint $w flush
  }


  #
  # Get item at a postion
  #
  proc get {w er} {
    variable parms
    variable cell

    set Cmax [llength $parms($w:colwidth)]

    set L {}
    for { set c 0 } { $c < $Cmax } { incr c } {
      lappend L $cell($w:$er:$c)
    }
    return $L
  }

  #
  # Get a single cell
  #
  proc getcell {w er c} {
    variable cell

    return $cell($w:$er:$c)
  }


  #
  # Put item at a postion
  #
  proc put {w er item} {
    variable parms
    variable cell

    set Cmax [llength $parms($w:colwidth)]

    for { set c 0 } { $c < $Cmax } { incr c } {
      set cell($w:$er:$c) [lindex $item $c]
    }
    reqRepaint $w put
  }

  #
  # Put a single cell
  #
  proc putcell {w pos c item} {
    variable cell
    variable parms

    if {$pos=="end"} {
      set pos $parms($w:numrows)
    }

    set cell($w:$pos:$c) $item
    reqRepaint $w putcell
  }

  #
  # Add an item to the spreadsheet
  #
  proc insert {w pos value} {
    variable parms
    variable cell

    if {$pos=="end"} {
      set pos $parms($w:numrows)
    }

    set Cmax [llength $parms($w:colwidth)]

    for { set r $parms($w:numrows) } { $r > $pos } { incr r -1 } {
      for { set c 0 } { $c < $Cmax } { incr c } {
	set cell($w:$r:$c) $cell($w:[expr $r - 1]:$c)
      }
    }

    for { set c 0 } { $c < $Cmax } { incr c } {
      set cell($w:$pos:$c) [lindex $value $c]
    }
    incr parms($w:numrows)

    reqRepaint $w insert
  }

  proc grabMotion {w r c} {
    variable parms

    set er_delta [expr $r - [lindex $parms($w:mousedown) 0]]


    if { $er_delta == 0 } {
      return
    }


    set selection [getselection $w]
#    puts "grabMotion $w : $parms($w:mousedown) : $r $c sel=$selection"

    set N $parms($w:numrows)
    set min_er [vmin $selection]
    set max_er [vmax $selection]


    if { [expr $min_er + $er_delta] < 0 } { set er_delta [expr -$min_er]}
    if { [expr $max_er + $er_delta+1] >= $N } { set er_delta [expr $N-$max_er-1]}

    # puts "sel=[list $selection] min=$min_er max=$max_er  delta=$er_delta"

    set newselection [shift $selection $er_delta]


    set min_er [min [expr $min_er + $er_delta] $min_er]
    set max_er [max [expr $max_er + $er_delta] $max_er]

    set SI {}
    set NSI {}
    for {set xr $min_er } { $xr <= $max_er } { incr xr } {
      if {[lsearch $selection $xr] < 0} {
	lappend NSI [get $w $xr]
      } else {
	lappend SI [get $w $xr]
      }
    }

    for {set xr $min_er } { $xr <= $max_er } { incr xr } {
      if {[lsearch $newselection $xr] < 0} {
	put $w $xr [lindex $NSI 0]
	set NSI [lrange $NSI 1 end]
      } else {
	put $w $xr [lindex $SI 0]
	set SI [lrange $SI 1 end]
      }
    }

    set parms($w:selection) $newselection
    set parms($w:mousedown) [list $r $c]

    catch { $parms($w:entrycommand) reorder $w }

    reqRepaint $w grabMotion
  }

  proc speedMap {d} {
    if { $d > 40} {
      return 50
    } else {
      return  [expr int(250-(40-$d)*200.0/40.0-50)]
    }
  }

  proc doAutoScroll {w} {
    variable asEvent
    variable asActive
    variable asSpeed
    variable asX
    variable asY
    variable parms

    set H $parms($w:numrows)
    set P $parms($w:position)

    if { $asActive < 0} {
      if {$P < [expr $H-1]} {
	incr parms($w:position)
	reqRepaint $w autoScroll
	seeB1Motion $w $asX $asY -autoscroll
      }
    } else {
      if {$P > 0} {
	incr parms($w:position) -1
	reqRepaint $w autoScroll
	seeB1Motion $w $asX $asY -autoscroll
      }
    }

    set asEvent [after $asSpeed "SpreadSheet::doAutoScroll $w"]
  }

  proc checkAutoScroll {w X Y args} {
    variable asEvent
    variable asActive
    variable asX
    variable asY
    variable asSpeed

    set asX $X
    set asY $Y

    set cancel 0
    if {[lsearch $args -cancel] >= 0} { set cancel 1 }


    set asc 0
    set rootY [winfo rooty $w]
    set height [winfo height $w]
    if { $Y < [expr $rootY + 40]} {
      set asY [max $Y 1]
      set asc 1

      set asSpeed [speedMap [expr $rootY + 40 - $Y]]
    } elseif { $Y > [expr $rootY + $height - 20]} {
      set asY [min $Y [expr $rootY + $height - 1]]
      set asc -1

      set asSpeed [speedMap [expr $Y - ($rootY + $height - 20)]]
    }

    #
    # Check for event cancelation
    #
    if {$cancel || $asc == 0 || ($asActive != 0 && $asc != $asActive)} {
      if {$asEvent != ""} {
	after cancel $asEvent
      }
      set asEvent ""
      set asActive 0
      return
    }

    set asActive $asc
    if { $asEvent == "" } {
      set asEvent [after 500 "SpreadSheet::doAutoScroll $w"]
    }
  }

  #
  # See press of B1
  #
  proc seeB1Press {w r c s X Y} {
    variable parms
    variable lastclick
    variable dragspot

    set dragspot ""

    checkAutoScroll $w $X $Y

    if { $s != 0 } {
      clearselection $w
      set parms($w:mousedown) {}
    }

    if {![catch { set clicktime [clock clicks -milliseconds] }]} {
      if { $lastclick != 0 && [expr $clicktime - $lastclick] < 350} {
	seeB1Double $w $r $c
	set lastclick 0
	return
      } else {
	set lastclick $clicktime
      }
    }

    set er [getEffectiveRow $w $r]

    if { [lsearch [getselection $w] $er] >= 0 } {
      set parms($w:mousedown) [list $er $c]
      set parms($w:grab) 1
      clearEntrySelect $w
      show_selection $w
      return
    }

    clearselection $w

    set P $parms($w:position)
    set Rmax $parms($w:height)
    set Cmax [llength $parms($w:colwidth)]

    set direct_move 0
    if { $parms($w:selectmode) == "single"
	 || ($parms($w:selectmode) == "shift-multiple" && $s == 0) } {
      set direct_move 1
    }

    if { $er >= 0 && $er < $parms($w:numrows) } {
      set parms($w:mousedown) [list $er $c]
      addto_selection $w $er

      if {!$parms($w:grab) && $direct_move} {
	set parms($w:mousedown) [list $er $c]
	set parms($w:grab) 1
	clearEntrySelect $w
	show_selection $w
      }

      return
    }

    set parms($w:mousedown) {}
  }

  proc seeB1Motion {w X Y args} {
    variable parms
    variable dragspot

    #
    # -autoscroll flag means we are being called from within autoscroll and should avoid
    # a recursive call.
    #
    if {[lsearch $args -autoscroll] < 0 } {
      checkAutoScroll $w $X $Y
    }

    set W [winfo containing $X $Y]
    if { [scan $W $w.c%d_%d r c] != 2} {
      catch { $parms($w:entrycommand) dragout zoom }
      return
    }

    if {[llength $parms($w:mousedown)] == 0} {return }

    set er [getEffectiveRow $w $r]

    if { $parms($w:dragcommand) != "" } {
      if { $dragspot == "" } {
	set dragspot [list $X $Y]
      } else {
	set dsX [lindex $dragspot 0]
	set dsY [lindex $dragspot 1]

	set delta [expr ($X-$dsX)*($X-$dsX) + ($Y-$dsY)*($Y-$dsY)]

	if { $delta > 7 } {
	  set er [getselection $w]
	  if {[llength $er] == 1 } {
	    $parms($w:dragcommand) $w $er
	  }
	  set dragspot ""
	}
      }
      return
    }


    if { $parms($w:grab) } {
      if { $parms($w:dograb) } {
	grabMotion $w $er $c
      }
      return
    }


    set P $parms($w:position)
    set Rmax $parms($w:height)
    set Cmax [llength $parms($w:colwidth)]

    set br [lindex $parms($w:mousedown) 0]

    if { $er < $br } {
      set x $er
      set er $br
      set br $x
    }

    clearselection $w
    for { set xr $br } { $xr <= $er } { incr xr } {
      addto_selection $w $xr
    }
  }

  proc seeB1Release {w X Y} {
    variable parms
    variable dragspot

    set dragspot ""

#    focus $w

    if { $parms($w:grab) } {
      set parms($w:mousedown) ""
      set parms($w:grab) 0
      show_selection $w
      return
    }
    seeB1Motion $w $X $Y
    checkAutoScroll $w 0 0 -cancel
  }

  proc seeB1Double {w r c } {
    variable parms
    variable dragspot

    set dragspot ""

    checkAutoScroll $w 0 0 -cancel


    set er [getEffectiveRow $w $r]
    catch { $parms($w:entrycommand) doublePress $w $er $c }
    entrySelect $w $r $c
  }

  proc seeB3Press {w r c s X Y} {
    variable parms
    variable lastclick
    variable dragspot

    set dragspot ""

    #
    # The double calls to seeB1Press cause the selected item to be shown as grabbed.
    # we clear lastclick so that it will not be treated as a double click.
    #
    seeB1Press $w $r $c $s $X $Y
    set lastclick 0
    seeB1Press $w $r $c $s $X $Y
    checkAutoScroll $w 0 0 -cancel

    catch { $parms($w:entrycommand) rightclick $w }
    set parms($w:mousedown) ""
    set parms($w:grab) 0
  }

  #############################################################################
  #
  # Send a request to delete an entry to the entry manager.
  #
  proc requestDelete {w args} {
    variable parms

    catch { $parms($w:entrycommand) delete $w }
  }

  #############################################################################
  #
  # Set bindings on cells that are not active for entry
  #
  proc setBindings {w r c} {
    bind $w.c${r}_$c <1> "SpreadSheet::seeB1Press $w $r $c %s %X %Y"
    bind $w.c${r}_$c <3> "SpreadSheet::seeB3Press $w $r $c %s %X %Y"
    bind $w.c${r}_$c <B1-Motion> "SpreadSheet::seeB1Motion $w %X %Y"
    bind $w.c${r}_$c <ButtonRelease-1> "SpreadSheet::seeB1Release $w %X %Y"
#    bind $w.c${r}_$c <Double-ButtonPress-1> "SpreadSheet::seeB1Double $w $r $c"
  }

  #############################################################################
  #
  # Set bindings on cells that are active for entry
  #
  proc tabBindings {W w r c} {
    bindtags $W [ldelete [bindtags $W] all]
    bind $W <Tab> "SpreadSheet::advanceEntry $w %K $r $c"
    bind $W <Escape> "SpreadSheet::clearEntrySelect $w; SpreadSheet::reqRepaint $w tab-escape"
    bind $W <Return> "SpreadSheet::clearEntrySelect $w; SpreadSheet::reqRepaint $w tab-return"
#    bind $W <Up> "SpreadSheet::advanceEntry $w %K $r $c"
#    bind $W <Down> "SpreadSheet::advanceEntry $w %K $r $c"
#    bind $W <KeyPress> { puts "KeyPress %K" }
  }


  #
  # Add a spreadsheet column
  #
  proc addcolumn {w args} {
    variable parms
    variable cell

    set width 5
    set header ""
    set type text
    parseargs $args {-width -header -type}

    set c [llength $parms($w:colwidth)]

    lappend parms($w:colwidth) $width
    lappend parms($w:headers) $header
    lappend parms($w:types) $type


    if {$parms($w:expandcol) == $c} {
      grid columnconfigure $w $c -weight 1
    }

    # column header
    label $w.h${c} -bd 1 -relief raised -width $width -bg $parms($w:headercolor) -text $header -font dialogBigExpFont -takefocus 0
    grid $w.h${c} -row 0 -column $c -ipadx 1 -ipady 1 -sticky ew

    # column rows
    for {set r 1 } {$r < $parms($w:height) } {incr r } {
      label $w.c${r}_$c -bd 1 -relief raised -width $width -bg $parms($w:entrycolor) -anchor w -takefocus 0
      grid $w.c${r}_$c -row $r -column $c  -ipadx 1 -ipady 1 -sticky ew
      set cell($w:$r:$c) ""
      setBindings $w $r $c
    }
  }

  proc reduceSize {w new_height} {
    variable parms

#    puts "reduceSize $w $new_height"

    set Ncol [llength $parms($w:colwidth)]
    set height $parms($w:height)
    for  { set r $new_height } { $r < $height } { incr r } {
#      puts "    delrow - $r"
      for { set c 0} { $c < $Ncol } { incr c } {
	destroy $w.c${r}_$c
      }
    }
    set parms($w:height) $new_height
  }

  proc expandSize {w H} {
    variable parms

    set Ncol [llength $parms($w:colwidth)]

    set H [winfo reqheight [winfo parent $w]]
#    puts "expandSize $w $H"

    set rowHeight [winfo height $w.h0]

    for {set r $parms($w:height) } {[expr $r*$rowHeight] < $H } {incr r } {
#      puts "    addrow - $r"
      for { set c 0} { $c < $Ncol } { incr c } {
	set width [lindex $parms($w:colwidth) $c]
	label $w.c${r}_$c -bd 1 -relief raised -width $width -bg $parms($w:entrycolor) -anchor w
	grid $w.c${r}_$c -row $r -column $c  -ipadx 1 -ipady 1 -sticky ew
	set cell($w:$r:$c) ""
	setBindings $w $r $c
      }
    }
#    puts "set parms($w:height) [expr $r + 1]"
    set parms($w:height) $r
  }

  proc updateSize {w} {
    variable parms

    set W [winfo width $w]
    set H [winfo height $w]

#    puts "SpreadSheet::updateSize $w $W $H"

    if { $parms($w:resize) } {
      set new_height 0

      set height $parms($w:height)

      set new_height 10000000
      for { set r 1 } { $r < $height } { incr r } {
	if { [winfo y $w.c${r}_0] > $H } {
	  set new_height $r
	  break
	}
      }

      if {$new_height < $height } {
	reduceSize $w $new_height
      } else {
	expandSize $w $H
      }

    }


    SpreadSheet::reqRepaint $w updateSize
  }

  #############################################################################
  #
  # Configure a SpreadSheet widget (arguments given in list)
  #
  proc configurev {w argv} {
    variable parms

#    puts "SpreadSheet::configurev $w $argv"

    set optlist {-height -headercolor -entrycolor -selectcolor -command -statecommand
      -bd -relief -yscrollcommand -grabcolor -entrycommand -expandcol -selectmode
      -dograb -dragcommand -resize}

    parseargs $argv $optlist

    foreach o $optlist {
      scan $o "-%s" var

      if {[info exists $var]} {
	set parms($w:$var) [set $var]
      }
    }
  }

  #############################################################################
  #
  # Configure a SpreadSheet widget (arguments on command line)
  #
  proc configure {w args} {
    configurev $w $args
  }


  #############################################################################
  #
  # SpreadSheet::init $w $argv
  #
  # Initialize the options of a spreadsheet
  #
  proc init {w} {
    variable parms
    variable cell


    set parms($w:mousedown) {}
    set parms($w:colwidth) {}
    set parms($w:position) 0
    set parms($w:numrows) 0
    set parms($w:grab) 0
    set parms($w:selection) ""
    set parms($w:entryselection) ""
    set parms($w:updatepending) 0
    set parms($w:headers) {}
    set parms($w:types) {}
    set parms($w:repaintpending) ""
    set parms($w:dograb) 1
    set parms($w:height) 1
    set parms($w:headercolor) [option get $w SpreadSheet.headerColor {}]
    set parms($w:entrycolor)  [option get $w SpreadSheet.entryColor {}]
    set parms($w:selectcolor) [option get $w SpreadSheet.selectColor {}]
    set parms($w:grabcolor)   [option get $w SpreadSheet.grabColor {}]
    set parms($w:entrycommand) ""
    set parms($w:command) ""
    set parms($w:statecommand) ""
    set parms($w:bd) 2
    set parms($w:relief) sunken
    set parms($w:yscrollcommand) ""
    set parms($w:expandcol) -1
    set parms($w:selectmode)  [option get $w SpreadSheet.selectmode {}]
    set parms($w:dragcommand) ""
    set parms($w:resize) 0

    set cell($w:0:0) ""
  }

  #############################################################################
  #
  # SpreadSheet::create $w [args...]
  #
  # Create a new spreadsheet.
  #
  proc create {w args} {
    variable parms

    set p_repaintPending($w) 0

    frame $w
    init $w
    configurev $w $args
    $w configure -bd $parms($w:bd) -relief $parms($w:relief) -takefocus 0 -width 10 -height 10

    bind $w <Delete> "SpreadSheet::requestDelete $w"
    bind $w <Configure> "SpreadSheet::updateSize $w"
  }

  proc tester {} {
    SpreadSheet::create .lb -bd 2 -relief sunken -yscrollcommand ".vb set" -height 15  -entrycommand entryManager
    scrollbar .vb -orient vertical -command "SpreadSheet::yview .lb"
    grid .lb -row 0 -column 0 -padx 20 -pady 20
    grid .vb -row 0 -column 1 -sticky ns
    SpreadSheet::addcolumn .lb -width 10 -header Fee
    SpreadSheet::addcolumn .lb -width 10 -header Fei
    SpreadSheet::addcolumn .lb -width 10 -header Foe
    SpreadSheet::addcolumn .lb -width 10 -header Fum

    SpreadSheet::insert .lb end {1 5 6 7}
    SpreadSheet::insert .lb end {2 12 18 99}
    SpreadSheet::insert .lb end {3 8 77 120}
    SpreadSheet::insert .lb end {4 12 18 35}
    SpreadSheet::insert .lb end {5 87 423 72}
    SpreadSheet::insert .lb end {6 786 72 281}
    SpreadSheet::insert .lb end {7 76 7823 76}
    SpreadSheet::insert .lb end {8 76 1289 89}
    SpreadSheet::insert .lb end {9 5 2013 1283}
    SpreadSheet::insert .lb end {10 12 12 123123}
    SpreadSheet::insert .lb end {11 123 87 28}
    SpreadSheet::insert .lb end {12 12783 765 123}
    SpreadSheet::insert .lb end {13 783 65 223}
    SpreadSheet::insert .lb end {14 183 76 129}
    SpreadSheet::insert .lb end {15 273 75 121}
    SpreadSheet::insert .lb end {16 1273 865 103}
    SpreadSheet::insert .lb end {17 1783 965 183}
  }
}

if { $spreadsheet_test } {
  SpreadSheet::tester
}


#
# entryManager canenter w r c data		see if we can enter data at (r,c)
# entryManager entrywidget w r c W width var	create special widget at (r,c) or return 0 for default
# entryManager close w r c data			close the entry widget on a cell and do a trandormation on the data
# entryManager canappend w r c data		can we append a row at (r,c)
# entryManager initentry w r c			if appending, get initial row values
#
proc entryManager {cmd args} {
  switch $cmd {
    canenter {
      set c [lindex $args 2]
      return [expr $c != 2 ]
    }
    entrywidget {
      set w [lindex $args 0]
      set r [lindex $args 1]
      set c [lindex $args 2]
      set W [lindex $args 3]
      set width [lindex $args 4]
      set variable [lindex $args 5]
      if { $c == 3 } {
	Dropbox::new $W -variable $variable -width [expr $width - 3] -bd 1 -highlightthickness 0
	Dropbox::itemadd $W "one"
	Dropbox::itemadd $W "two"
	Dropbox::itemadd $W "three"
	Dropbox::itemadd $W "four"

	return 1
      }
    }
    close {
      set data [lindex $args 3]
      return [string tolower $data]
    }
    canappend {
      set c [lindex $args 2]
      return [expr $c != 2 ]
    }
    initentry {
      return {0 0 0 0}
    }
  }
  return 0
}