summaryrefslogtreecommitdiff
path: root/itcl/iwidgets3.0.0/generic/combobox.itk
blob: ab70ba98956f0fb21554656294899a27b80fd26e (plain)
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
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
# Combobox
# ----------------------------------------------------------------------
# Implements a Combobox widget. A Combobox has 2 basic styles: simple and
# dropdown. Dropdowns display an entry field with an arrow button to the 
# right of it. When the arrow button is pressed a selectable list of
# items is popped up. A simple Combobox displays an entry field and a listbox 
# just beneath it which is always displayed. In both types, if the user 
# selects an item in the listbox, the contents of the entry field are 
# replaced with the text from the selected item. If the Combobox is 
# editable, the user can type in the entry field and when <Return> is
# pressed the item will be inserted into the list.
#
# WISH LIST:
#	This section lists possible future enhancements.  
#
#	  Combobox 1.x:
#		  - convert bindings to bindtags.
#
# ----------------------------------------------------------------------
#  ORIGINAL AUTHOR: John S. Sigler		EMAIL: jsigler@spd.dsccc.com
#											   sigler@onramp.net
# ----------------------------------------------------------------------
#  CURRENT MAINTAINER: Mitch Gorman				EMAIL: logain@erols.com
#					Copyright (c) 1995	John S. Sigler
#					Copyright (c) 1997	Mitch Gorman
# ======================================================================
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
# 
# IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES 
# ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN 
# IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 
# DAMAGE.
#
# THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, 
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
# FITNESS FOR A PARTICULAR PURPOSE.	 THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
# ======================================================================

#
# Default resources.
#
option add *Combobox.borderWidth 2 widgetDefault
option add *Combobox.labelPos wn widgetDefault
option add *Combobox.listHeight 150 widgetDefault
option add *Combobox.hscrollMode dynamic widgetDefault
option add *Combobox.vscrollMode dynamic widgetDefault

#
# Usual options.
#
itk::usual Combobox {
    keep -background -borderwidth -cursor -foreground -highlightcolor \
	-highlightthickness -insertbackground -insertborderwidth \
	-insertofftime -insertontime -insertwidth -labelfont -popupcursor \
	-selectbackground -selectborderwidth -selectforeground \
	-textbackground -textfont
}

# ------------------------------------------------------------------
#							 COMBOBOX
# ------------------------------------------------------------------
class iwidgets::Combobox {
    inherit iwidgets::Entryfield
    
    constructor {args} {}
    destructor {}

    itk_option define -arrowrelief arrowRelief Relief raised
    itk_option define -completion completion Completion true
    itk_option define -dropdown dropdown Dropdown true
    itk_option define -editable editable Editable true
    itk_option define -grab grab Grab local
    itk_option define -listheight listHeight Height 150
    itk_option define -margin margin Margin 1
    itk_option define -popupcursor popupCursor Cursor arrow
    itk_option define -selectioncommand selectionCommand SelectionCommand {}
    itk_option define -state state State normal
    itk_option define -unique unique Unique true

    public method clear {{component all}}
    public method curselection {}
    public method delete {component first {last {}}}
    public method get {{index {}}}
    public method getcurselection {}
    public method insert {component index args}
    public method invoke {}
    public method justify {direction}
    public method see {index}
    public method selection {option first {last {}}}
    public method size {}
    public method sort {{mode ascending}}
    public method xview {args}
    public method yview {args}

    protected method _addToList {}
    protected method _createComponents {}
    protected method _deleteList {first {last {}}}
    protected method _deleteText {first {last {}}}
    protected method _doLayout {{when later}}
    protected method _drawArrow {}
    protected method _dropdownBtnRelease {{window {}} {x 1} {y 1}}
    protected method _ignoreNextBtnRelease {ignore}
    protected method _next {}
    protected method _packComponents {{when later}}
    protected method _positionList {}
    protected method _postList {}
    protected method _previous {}
    protected method _resizeArrow {}
    protected method _selectCmd {}
    protected method _toggleList {}
    protected method _unpostList {}
    protected method _commonBindings {}
    protected method _dropdownBindings {}
    protected method _simpleBindings {}
    protected method _listShowing {{val ""}}

    private method _bs {}
    private method _lookup {key}
    private method _slbListbox {}
    private method _stateSelect {}

    private variable _doit 0;
    private variable _inbs 0;
    private variable _inlookup 0;
    private variable _currItem {};			 ;# current selected item.
    private variable _ignoreRelease false	 ;# next button release ignored.
    private variable _isPosted false;		 ;# is the dropdown popped up.
    private variable _repacking {}	  ;# non-null => _packComponents pending.
    private common _listShowing
    private common count 0
}	 

#
# Provide a lowercase access method for the Combobox class.
# 
proc ::iwidgets::combobox {pathName args} {
    uplevel ::iwidgets::Combobox $pathName $args
}

# ------------------------------------------------------------------
#						CONSTRUCTOR
# ------------------------------------------------------------------
body iwidgets::Combobox::constructor {args} {
    set _listShowing($this) 0

    # combobox is different as all components are created 
    # after determining what the dropdown style is...

    # configure args
    eval itk_initialize $args
    
    # create components that are dependent on options 
    # (Scrolledlistbox, arrow button) and pack them.
    if {$count == 0} {
	image create bitmap downarrow -data {
	    #define down_width 16
	    #define down_height 16
	    static unsigned char down_bits[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
		0x00, 0x00, 0x00, 0x00, 0xfc, 0x7f, 0xf8, 0x3f, 
		0xf0, 0x1f, 0xe0, 0x0f, 0xc0, 0x07, 0x80, 0x03, 
		0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	    };
	}
	image create bitmap uparrow -data {
	    #define up_width 16
	    #define up_height 16
	    static unsigned char up_bits[] = {
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 
		0xc0, 0x01, 0xe0, 0x03, 0xf0, 0x07, 0xf8, 0x0f, 
		0xfc, 0x1f, 0xfe, 0x3f, 0x00, 0x00, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
	    };
	}
    }
    incr count
    _doLayout
}

# ------------------------------------------------------------------
#						   DESTRUCTOR
# ------------------------------------------------------------------
body iwidgets::Combobox::destructor {} {
    # catch any repacking that may be waiting for idle time
    if {$_repacking != ""} {
	after cancel $_repacking
    }
    incr count -1
    if {$count == 0} {
	image delete uparrow
	image delete downarrow
    }
}

# ================================================================
#							OPTIONS
# ================================================================

# --------------------------------------------------------------------
# OPTION:  -arrowrelief
#
# Relief style used on the arrow button.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::arrowrelief {}

# --------------------------------------------------------------------
# OPTION:  -completion
#
# Relief style used on the arrow button.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::completion {
    switch -- $itk_option(-completion) {
	0 - no - false - off { }
	1 - yes - true - on { }
	default {
	    error "bad completion option \"$itk_option(-completion)\":\
					   should be boolean"
	}
    }
}

# --------------------------------------------------------------------
# OPTION:  -dropdown  
#
# Boolean which determines the Combobox style: dropdown or simple.
# Because the two style's lists reside in different toplevel widgets
# this is more complicated than it should be.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::dropdown {
    switch -- $itk_option(-dropdown) {
	1 - yes - true - on {
	    if {[winfo exists $itk_interior.list]} {
		set vals [$itk_component(list) get 0 end]
		destroy $itk_component(list)
		_doLayout
		if [llength $vals] {
		    eval insert list end $vals
		}
	    }
	}
	0 - no - false - off {
	    if {[winfo exists $itk_interior.popup.list]} {
		set vals [$itk_component(list) get 0 end]
		catch {destroy $itk_component(arrowBtn)}
		destroy $itk_component(popup)  ;# this deletes the list too
		_doLayout
		if [llength $vals] {
		    eval insert list end $vals
		}
	    }
	}
	default {
	    error "bad dropdown option \"$itk_option(-dropdown)\":\
					   should be boolean"
	}
    }
}

# --------------------------------------------------------------------
# OPTION: -editable	 
#
# Boolean which allows/disallows user input to the entry field area.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::editable {
    switch -- $itk_option(-editable) {
	1 - true - yes - on {
	    switch -- $itk_option(-state) {
		normal {
		    $itk_component(entry) configure -state normal
		}
	    }
	}
	0 - false - no - off {
	    $itk_component(entry) configure -state disabled
	}
	default {
	    error "bad editable option \"$itk_option(-editable)\":\
				   should be boolean"
	}
    }
}

# --------------------------------------------------------------------
# OPTION:  -grab
#
# grab-state of megawidget
# --------------------------------------------------------------------
configbody iwidgets::Combobox::grab {
    switch -- $itk_option(-grab) {
	local { }
	global { }
	default {
	    error "bad grab value \"$itk_option(-grab)\":\
				   must be global or local"
	}
    }
}

# --------------------------------------------------------------------
# OPTION: -listheight  
#
# Listbox height in pixels. (Need to integrate the scrolledlistbox
# -visibleitems option here - at least for simple listbox.)
# --------------------------------------------------------------------
configbody iwidgets::Combobox::listheight {}

# --------------------------------------------------------------------
# OPTION:  -margin
#
# Spacer between the entry field and arrow button of dropdown style
# Comboboxes.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::margin {
    grid columnconfigure $itk_interior 0 -minsize $itk_option(-margin)
}

# --------------------------------------------------------------------
# OPTION:  -popupcursor
#
# Set the cursor for the popup list.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::popupcursor {}

# --------------------------------------------------------------------
# OPTION:  -selectioncommand
#
# Defines the proc to be called when an item is selected in the list.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::selectioncommand {}

# --------------------------------------------------------------------
# OPTION:  -state
#
# overall state of megawidget
# --------------------------------------------------------------------
configbody iwidgets::Combobox::state {
    switch -- $itk_option(-state) {
	disabled {
	    $itk_component(entry) configure -state disabled
	}
	normal {
	    switch -- $itk_option(-editable) {
		1 - true - yes - on {
		    $itk_component(entry) configure -state normal
		}
		0 - false - no - off {
		    $itk_component(entry) configure -state disabled
		}
	    }
	}
	default {
	    error "bad state value \"$itk_option(-state)\":\
				   must be normal  or disabled"
	}
    }
    if {[winfo exists itk_component(arrowBtn)]} {
	$itk_component(arrowBtn) configure -state $itk_option(-state)
    }
}

# --------------------------------------------------------------------
# OPTION: -unique  
#
# Boolean which disallows/allows adding duplicate items to the listbox.
# --------------------------------------------------------------------
configbody iwidgets::Combobox::unique {
    # boolean error check
    switch -- $itk_option(-unique) {
	1 - true - yes - on { }
	0 - false - no - off { }
	default {
	    error "bad unique value \"$itk_option(-unique)\":\
				   should be boolean"
	}
    }
}

# =================================================================
#							 METHODS
# =================================================================

# ------------------------------------------------------
#  PUBLIC METHOD: clear ?component?
#
#  Remove all elements from the listbox, all contents
#  from the entry component, or both (if all).
#
# ------------------------------------------------------
body iwidgets::Combobox::clear {{component all}} {
    switch -- $component {
	entry {
	    iwidgets::Entryfield::clear
	}
	list {
	    delete list 0 end
	}
	all {
	    delete list 0 end
	    iwidgets::Entryfield::clear
	}
	default {
	    error "bad Combobox component \"$component\":\
				   must be entry, list, or all."
	}
    }
    return
}

# ------------------------------------------------------
# PUBLIC METHOD: curselection
#
# Return the current selection index.
#
# ------------------------------------------------------
body iwidgets::Combobox::curselection {} {
    return [$itk_component(list) curselection]
}

# ------------------------------------------------------
# PUBLIC METHOD: delete component first ?last?
#
# Delete an item or items from the listbox OR delete
# text from the entry field. First argument determines
# which component deletion occurs in - valid values are
# entry or list.
#
# ------------------------------------------------------
body iwidgets::Combobox::delete {component first {last {}}} {
    switch -- $component {
	entry {
	    if {$last == {}} {
	      set last [expr $first + 1]
	    }
	    iwidgets::Entryfield::delete $first $last
	}
	list {
	    _deleteList $first $last
	}
	default {
	    error "bad Combobox component \"$component\":\
				   must be entry or list."
	}
    }
}

# ------------------------------------------------------
# PUBLIC METHOD: get ?index?
#
#
# Retrieve entry contents if no args OR use args as list 
# index and retrieve list item at index .
#
# ------------------------------------------------------
body iwidgets::Combobox::get {{index {}}} {
    # no args means to get the current text in the entry field area
    if {$index == {}} {
	iwidgets::Entryfield::get
    } else {
	eval $itk_component(list) get $index
    }
}

# ------------------------------------------------------
# PUBLIC METHOD: getcurselection
#
# Return currently selected item in the listbox. Shortcut
# version of get curselection command combination.
#
# ------------------------------------------------------
body iwidgets::Combobox::getcurselection {} {
    return [$itk_component(list) getcurselection]
}

# ------------------------------------------------------------------
# PUBLIC METHOD: ivoke
#
# Pops up or down a dropdown combobox.
# 
# ------------------------------------------------------------------
body iwidgets::Combobox::invoke {} {
    if {$itk_option(-dropdown)} {
	return [_toggleList]
    }
    return 
}

# ------------------------------------------------------------
# PUBLIC METHOD: insert comonent index string ?string ...?
#
# Insert an item into the listbox OR text into the entry area.
# Valid component names are entry or list.
#
# ------------------------------------------------------------
body iwidgets::Combobox::insert {component index args} {
    set nargs [llength $args]

    if {$nargs == 0} {
	error "no value given for parameter \"string\" in function\
			   \"Combobox::insert\""
    } 

    switch -- $component {
	entry {
	    if { $nargs > 1} {
		error "called function \"Combobox::insert entry\"\
					   with too many arguments"
	    } else {
		if {$itk_option(-state) == "normal"} {
		    eval iwidgets::Entryfield::insert $index $args
		    [code $this _lookup ""]
		}
	    }
	}
	list {
	    if {$itk_option(-state) == "normal"} {
		eval $itk_component(list) insert $index $args
	    }
	}
	default {
	    error "bad Combobox component \"$component\": must\
				   be entry or list."
	}
    }
}

# ------------------------------------------------------
# PUBLIC METHOD: justify direction
#
# Wrapper for justifying the listbox items in one of
# 4 directions:	 top, bottom, left, or right.
#
# ------------------------------------------------------
body iwidgets::Combobox::justify {direction} {
    return [$itk_component(list) justify $direction]
}

# ------------------------------------------------------------------
# PUBLIC METHOD: see index
#
# Adjusts the view such that the element given by index is visible.
# ------------------------------------------------------------------
body iwidgets::Combobox::see {index} {
    return [$itk_component(list) see $index]
}

# ------------------------------------------------------------------
# PUBLIC METHOD: selection option first ?last?
#
# Adjusts the selection within the listbox and changes the contents
# of the entry component to be the value of the selected list item.
# ------------------------------------------------------------------
body iwidgets::Combobox::selection {option first {last {}}} {
    # thin wrap
    if {$option == "set"} {
	$itk_component(list) selection clear 0 end
	$itk_component(list) selection set $first
	set rtn ""
    } else {
	set rtn [eval $itk_component(list) selection $option $first $last]
    }
    set _currItem $first

    # combobox additions
    set theText [getcurselection]
    if {$theText != [$itk_component(entry) get]} {
	clear entry
	if {$theText != ""} {
	    insert entry 0 $theText
	}
    }
    return $rtn
}

# ------------------------------------------------------------------
# PUBLIC METHOD: size 
#
# Returns a decimal string indicating the total number of elements 
# in the listbox.
# ------------------------------------------------------------------
body iwidgets::Combobox::size {} {
    return [$itk_component(list) size]
}

# ------------------------------------------------------
# PUBLIC METHOD: sort ?mode?
#
# Sort the current list in either "ascending" or "descending" order.
#
#	jss: how should i handle selected items?
#
# ------------------------------------------------------
body iwidgets::Combobox::sort {{mode ascending}} {
    $itk_component(list) sort $mode
    #	 return [$itk_component(list) sort $mode]
}


# ------------------------------------------------------------------
# PUBLIC METHOD: xview ?arg arg ...?
#
# Change or query the vertical position of the text in the list box.
# ------------------------------------------------------------------
body iwidgets::Combobox::xview {args} {
    return [eval $itk_component(list) xview $args]
}

# ------------------------------------------------------------------
# PUBLIC METHOD: yview ?arg arg ...?
#
# Change or query the horizontal position of the text in the list box.
# ------------------------------------------------------------------
body iwidgets::Combobox::yview {args} {
    return [eval $itk_component(list) yview $args]
}

# ------------------------------------------------------
# PROTECTED METHOD: _addToList
#
# Add the current item in the entry to the listbox.
#
# ------------------------------------------------------
body iwidgets::Combobox::_addToList {} {
    set input [get]
    if {$input != ""} {
	if {$itk_option(-unique)} {
	    # if item is already in list, select it and exit
	    set item [lsearch -exact [$itk_component(list) get 0 end] $input]
	    if {$item != -1} {
		selection clear 0 end
		if {$item != {}} {
		    selection set $item $item
		    set _currItem $item
		}
		return
	    }
	}
	# add the item to end of list
	selection clear 0 end
	insert list end $input
	selection set end end
    }
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _createComponents
#
# Create deferred combobox components and add bindings.
#
# ------------------------------------------------------
body iwidgets::Combobox::_createComponents {} {
    if {$itk_option(-dropdown)} {
	# --- build a dropdown combobox ---

	# make the arrow childsite be on the right hand side

  	#-------------------------------------------------------------
	# BUG FIX: csmith (Chad Smith: csmith@adc.com), 3/4/99
  	#-------------------------------------------------------------
	# The following commented line of code overwrites the -command
	# option when passed into the constructor.  The order of calls
	# in the constructor is:
	# 	1) eval itk_initalize $args (initializes -command)
	#       2) _doLayout
	#       3) _createComponents (overwrites -command)
	# The solution is to only set the -command option if it hasn't
	# already been set.  The following 4 lines of code do this.
  	#-------------------------------------------------------------
	# ** configure -childsitepos e -command [code $this _addToList]
  	#-------------------------------------------------------------
	configure -childsitepos e
	if ![llength [cget -command]] {
	  configure -command [code $this _addToList]
	}
	
	# arrow button to popup the list
	itk_component add arrowBtn {
	    button $itk_interior.arrowBtn -borderwidth 2 \
		-width 15 -height 15 -image downarrow \
		-command [code $this _toggleList] -state $itk_option(-state)
	} {
	    keep -background -borderwidth -cursor  -state \
		-highlightcolor -highlightthickness
	    rename -relief -arrowrelief arrowRelief Relief
	    rename -highlightbackground -background background Background
	}
	
	# popup list container
	itk_component add popup {
	    toplevel $itk_interior.popup
	} {
	    keep -background -cursor
	}
	wm withdraw $itk_interior.popup
	
	# the listbox
	itk_component add list {
	    iwidgets::Scrolledlistbox $itk_interior.popup.list -exportselection no \
		-vscrollmode dynamic -hscrollmode dynamic -selectmode browse
	} {
	    keep -background -borderwidth -cursor -foreground \
		-highlightcolor -highlightthickness \
		-hscrollmode -selectbackground \
		-selectborderwidth -selectforeground -textbackground \
		-textfont -vscrollmode
	    rename -height -listheight listHeight Height
	    rename -cursor -popupcursor popupCursor Cursor
	}
	# mode specific bindings
	_dropdownBindings

	# Ugly hack to avoid tk buglet revealed in _dropdownBtnRelease where 
	# relief is used but not set in scrollbar.tcl. 
	global tkPriv
	set tkPriv(relief) raise

    } else {
	# --- build a simple combobox ---
	configure -childsitepos s
	itk_component add list {
	    iwidgets::Scrolledlistbox $itk_interior.list -exportselection no \
		-vscrollmode dynamic -hscrollmode dynamic 
	} {
	    keep -background -borderwidth -cursor -foreground \
		-highlightcolor -highlightthickness \
		-hscrollmode -selectbackground \
		-selectborderwidth -selectforeground -textbackground \
		-textfont -visibleitems -vscrollmode 
	    rename -height -listheight listHeight Height
	}
	# add mode specific bindings
	_simpleBindings
    }

    # popup cursor applies only to the list within the combobox
    configure -popupcursor $itk_option(-popupcursor)

    # add mode independent bindings
    _commonBindings
}

# ------------------------------------------------------
# PROTECTED METHOD: _deleteList first ?last?
#
# Delete an item or items from the listbox. Called via 
# "delete list args".
#
# ------------------------------------------------------
body iwidgets::Combobox::_deleteList {first {last {}}} {

    if {$last == {}} {
	set last $first
    }
    $itk_component(list) delete $first $last

    # remove the item if it is no longer in the list
    set text [$this get]
    if {$text != ""} {
	set index [lsearch -exact [$itk_component(list) get 0 end] $text ]
	if {$index == -1} {
	    clear entry
	}
    }
    return
}

# ------------------------------------------------------
# PROTECTED METHOD: _deleteText first ?last?
#
# Renamed Entryfield delete method. Called via "delete entry args".
#
# ------------------------------------------------------
body iwidgets::Combobox::_deleteText {first {last {}}} {
    $itk_component(entry) configure -state normal 
    set rtrn [delete $first $last]
    switch -- $itk_option(-editable) {
	0 - false - no - off {
	    $itk_component(entry) configure -state disabled
	}
    }
    return $rtrn
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _doLayout ?when?
#
# Call methods to create and pack the Combobox components.
#
# ------------------------------------------------------
body iwidgets::Combobox::_doLayout {{when later}} {
    _createComponents
    _packComponents $when
}


# ------------------------------------------------------
# PROTECTED METHOD:	  _drawArrow 
#
# Draw the arrow button. Determines packing according to
# -labelpos.
#
# ------------------------------------------------------
body iwidgets::Combobox::_drawArrow {} {
    set flip false
    set relief ""
    set fg [cget -foreground]
    if {$_isPosted} {
	set flip true
	set relief "-relief sunken"
    } else {
	set relief "-relief $itk_option(-arrowrelief)"
    }

    if {$flip} {
	#	 
	#				draw up arrow
	#
	eval $itk_component(arrowBtn) configure -image uparrow $relief
    } else {
	#	 
	#				draw down arrow
	#
	eval $itk_component(arrowBtn) configure -image downarrow $relief
    }
}

# ------------------------------------------------------
# PROTECTED METHOD: _dropdownBtnRelease window x y
#
# Event handler for button releases while a dropdown list
# is posted.
#
# ------------------------------------------------------
body iwidgets::Combobox::_dropdownBtnRelease {{window {}} {x 1} {y 1}} {

    # if it's a scrollbar then ignore the release
    if {($window == [$itk_component(list) component vertsb]) ||
	($window == [$itk_component(list) component horizsb])} {
	return
    }

    # 1st release allows list to stay up unless we are in listbox
    if {$_ignoreRelease} {
	_ignoreNextBtnRelease false
	return
    }
    
    # should I use just the listbox or also include the scrollbars
    if { ($x >= 0) && ($x < [winfo width [_slbListbox]])
	 && ($y >= 0) && ($y < [winfo height [_slbListbox]])} {
	_stateSelect
    }
    
    _unpostList
}

# ------------------------------------------------------
# PROTECTED METHOD: _ignoreNextBtnRelease ignore
#
# Set private variable _ignoreRelease. If this variable
# is true then the next button release will not remove
# a dropdown list.
#
# ------------------------------------------------------
body iwidgets::Combobox::_ignoreNextBtnRelease {ignore} {
    set _ignoreRelease $ignore
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _next
#
# Select the next item in the list.
#
# ------------------------------------------------------
body iwidgets::Combobox::_next {} {
    if {[size] <= 1} {
	return
    }
    set i [curselection]
    if {($i == {}) || ($i == [expr [size]-1]) } {
	set i 0
    } else {
	incr i
    }
    selection clear 0 end
    selection set $i $i
    see $i
    set _currItem $i
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _packComponents ?when?
#
# Pack the components of the combobox and add bindings.
#
# ------------------------------------------------------
body iwidgets::Combobox::_packComponents {{when later}} {
    if {$when == "later"} {
	if {$_repacking == ""} {
	    set _repacking [after idle [code $this _packComponents now]]
	    return
	}
    } elseif {$when != "now"} {
	error "bad option \"$when\": should be now or later"
    }

    if {$itk_option(-dropdown)} {
	grid configure $itk_component(list) -row 1 -column 0 -sticky news
	_resizeArrow
        grid config $itk_component(arrowBtn) -row 0 -column 1 -sticky nsew
    } else {
	# size and pack list hack
	grid configure $itk_component(entry) -row 0 -column 0 -sticky ew
	grid configure $itk_component(efchildsite) -row 1 -column 0 -sticky nsew
	grid configure $itk_component(list) -row 0 -column 0 -sticky nsew

	grid rowconfigure $itk_component(efchildsite) 1 -weight 1
	grid columnconfigure $itk_component(efchildsite) 0 -weight 1
    }
    set _repacking ""
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _positionList
#
# Determine the position (geometry) for the popped up list
# and map it to the screen.
#
# ------------------------------------------------------
body iwidgets::Combobox::_positionList {} {

    set x [winfo rootx $itk_component(entry) ]
    set y [expr [winfo rooty $itk_component(entry) ] + \
	       [winfo height $itk_component(entry) ]]
    set w [winfo width $itk_component(entry) ]
    set h [winfo height [_slbListbox] ]
    set sh [winfo screenheight .]

    if {([expr $y+$h] > $sh) && ($y > [expr $sh/2])} {
	set y [expr [winfo rooty $itk_component(entry) ] - $h]
    }
    
    $itk_component(list) configure -width $w
    wm overrideredirect $itk_component(popup) 0
    wm geometry $itk_component(popup) +$x+$y
    wm overrideredirect $itk_component(popup) 1
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _postList
#
# Pop up the list in a dropdown style Combobox.
#
# ------------------------------------------------------
body iwidgets::Combobox::_postList {} {
    if {[$itk_component(list) size] == ""} {
	return
    }

    set _isPosted true
    _positionList

    # map window and do a grab
    wm deiconify $itk_component(popup)
    _listShowing -wait
    if {$itk_option(-grab) == "global"} {
	grab -global $itk_component(popup) 
    } else {
	grab $itk_component(popup) 
    }
    raise $itk_component(popup)
    focus $itk_component(popup)
    _drawArrow
}

# ------------------------------------------------------
# PROTECTED METHOD:	   _previous
#
# Select the previous item in the list. Wraps at front
# and end of list. 
#
# ------------------------------------------------------
body iwidgets::Combobox::_previous {} {
    if {[size] <= 1} {
	return
    }
    set i [curselection]
    if {$i == "" || $i == 0} {
	set i [expr [size] - 1]
    } else {
	incr i -1
    }
    selection clear 0 end
    selection set $i $i
    see $i
    set _currItem $i
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _resizeArrow
#
# Recalculate the arrow button size and then redraw it.
#
# ------------------------------------------------------
body iwidgets::Combobox::_resizeArrow {} {
    set bw [expr [$itk_component(arrowBtn) cget -borderwidth]+ \
		[$itk_component(arrowBtn) cget -highlightthickness]]
    set newHeight [expr [winfo reqheight $itk_component(entry) ]-(2*$bw) - 2]
    $itk_component(arrowBtn) configure -width $newHeight -height $newHeight
    _drawArrow
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _selectCmd
#
# Called when list item is selected to insert new text 
# in entry, and call user -command callback if defined.
#
# ------------------------------------------------------
body iwidgets::Combobox::_selectCmd {} {
    $itk_component(entry) configure -state normal
    
    set _currItem [$itk_component(list) curselection]
    set item [$itk_component(list) getcurselection]
    clear entry
    $itk_component(entry) insert 0 $item
    switch -- $itk_option(-editable) {
	0 - false - no - off {
	    $itk_component(entry) configure -state disabled
	}
    }

    # execute user command
    if {$itk_option(-selectioncommand) != ""} {
	uplevel #0 $itk_option(-selectioncommand)
    }
}

# ------------------------------------------------------
# PROTECTED METHOD:	 _toggleList
#
# Post or unpost the dropdown listbox (toggle).
#
# ------------------------------------------------------
body iwidgets::Combobox::_toggleList {} {
    if {[winfo ismapped $itk_component(popup)] } {
	_unpostList
    } else {
	_postList
    }
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _unpostList
#
# Unmap the listbox (pop it down).
#
# ------------------------------------------------------
body iwidgets::Combobox::_unpostList {} {
    # Determine if event occured in the scrolledlistbox and, if it did, 
    # don't unpost it. (A selection in the list unposts it correctly and 
    # in the scrollbar we don't want to unpost it.)
    set x [winfo x $itk_component(list)]
    set y [winfo y $itk_component(list)]
    set w [winfo width $itk_component(list)]
    set h [winfo height $itk_component(list)]

    wm withdraw $itk_component(popup)
    grab release $itk_component(popup)	
    
    set _isPosted false
    
    $itk_component(list) selection clear 0 end
    if {$_currItem != {}} {
	$itk_component(list) selection set $_currItem $_currItem
	$itk_component(list) activate $_currItem
    }

    switch -- $itk_option(-editable) {
	1 - true - yes - on {
	    $itk_component(entry) configure -state normal
	}
	0 - false - no - off {
	    $itk_component(entry) configure -state disabled
	}
    }

    _drawArrow
}

# ------------------------------------------------------
# PROTECTED METHOD:	  _commonBindings
#
# Bindings that are used by both simple and dropdown
# style Comboboxes.
#
# ------------------------------------------------------
body iwidgets::Combobox::_commonBindings {} {
    bind $itk_component(entry) <KeyPress-BackSpace> [code $this _bs]
    bind $itk_component(entry) <KeyRelease> [code $this _lookup %K]
    bind $itk_component(entry) <Down>       [code $this _next]
    bind $itk_component(entry) <Up>         [code $this _previous]
    bind $itk_component(entry) <Control-n>  [code $this _next]
    bind $itk_component(entry) <Control-p>  [code $this _previous]
    bind [_slbListbox]         <Control-n>  [code $this _next]
    bind [_slbListbox]         <Control-p>  [code $this _previous]
}


# ------------------------------------------------------
# PROTECTED METHOD: _dropdownBindings
#
# Bindings used only by the dropdown type Combobox.
#
# ------------------------------------------------------
body iwidgets::Combobox::_dropdownBindings {} {
    bind $itk_component(popup)  <Escape> [code $this _unpostList]
    bind $itk_component(popup)  <space>  \
	"[code $this _stateSelect]; [code $this _unpostList]"
    bind $itk_component(popup)  <Return> \
	"[code $this _stateSelect]; [code $this _unpostList]"
    bind $itk_component(popup)  <ButtonRelease-1> \
        [code $this _dropdownBtnRelease %W %x %y]

    bind $itk_component(list)  <Map> \
	[code $this _listShowing 1]
    bind $itk_component(list)  <Unmap> \
        [code $this _listShowing 0]

    # once in the listbox, we drop on the next release (unless in scrollbar)
    bind [_slbListbox]   <Enter>   \
	[code $this _ignoreNextBtnRelease false]

    bind $itk_component(arrowBtn) <3>          [code $this _next]
    bind $itk_component(arrowBtn) <Shift-3>    [code $this _previous]
    bind $itk_component(arrowBtn) <Down>       [code $this _next]
    bind $itk_component(arrowBtn) <Up>         [code $this _previous]
    bind $itk_component(arrowBtn) <Control-n>  [code $this _next]
    bind $itk_component(arrowBtn) <Control-p>  [code $this _previous]
    bind $itk_component(arrowBtn) <Shift-Down> [code $this _toggleList]
    bind $itk_component(arrowBtn) <Shift-Up>   [code $this _toggleList]
    bind $itk_component(arrowBtn) <Return>     [code $this _toggleList]
    bind $itk_component(arrowBtn) <space>      [code $this _toggleList]

    bind $itk_component(entry)    <Configure>  [code $this _resizeArrow]
    bind $itk_component(entry)    <Shift-Down> [code $this _toggleList]
    bind $itk_component(entry)    <Shift-Up>   [code $this _toggleList]
}

# ------------------------------------------------------
# PROTECTED METHOD: _simpleBindings
#
# Bindings used only by the simple type Comboboxes.
#
# ------------------------------------------------------
body iwidgets::Combobox::_simpleBindings {} {
    bind [_slbListbox]         <ButtonRelease-1> [code $this _stateSelect]
    #               "[code $this _stateselect]; [code $this _selectCmd]"


    bind [_slbListbox]         <space>     [code $this _stateSelect]
    bind [_slbListbox]         <Return>    [code $this _stateSelect]
    bind $itk_component(entry) <Escape>     ""
    bind $itk_component(entry) <Shift-Down> ""
    bind $itk_component(entry) <Shift-Up>   ""
    bind $itk_component(entry) <Configure>  ""
}

# ------------------------------------------------------
# PROTECTED METHOD: _listShowing ?val?
#
# Used instead of "tkwait visibility" to make sure that
# the dropdown list is visible.	 Whenever the list gets
# mapped or unmapped, this method is called to keep
# track of it.	When it is called with the value "-wait",
# it waits for the list to be mapped.
# ------------------------------------------------------
body iwidgets::Combobox::_listShowing {{val ""}} {
    if {$val == ""} {
	return $_listShowing($this)
    } elseif {$val == "-wait"} {
	while {!$_listShowing($this)} {
	    tkwait variable [scope _listShowing($this)]
	}
	return
    }
    set _listShowing($this) $val
}

# ------------------------------------------------------
# PRIVATE METHOD:	 _slbListbox
#
# Access the tk listbox window out of the scrolledlistbox.
#
# ------------------------------------------------------
body iwidgets::Combobox::_slbListbox {} {
    return [$itk_component(list) component listbox]
}

# ------------------------------------------------------
# PRIVATE METHOD:	 _stateSelect
#
# only allows a B1 release in the listbox to have an effect if -state is
#	normal.
#
# ------------------------------------------------------
body iwidgets::Combobox::_stateSelect {} {
    switch --  $itk_option(-state) {
	normal {
	    [code $this _selectCmd]
	}
    }
}

# ------------------------------------------------------
# PRIVATE METHOD:	 _bs
#
# A part of the auto-completion code, this function sets a flag when the
#	Backspace key is hit and there is a selection in the entry field.
# Note that it's probably buggy to assume that a selection being present
#	means that that selection came from auto-completion.
#
# ------------------------------------------------------
body iwidgets::Combobox::_bs {} {
    #
    #		exit if completion is turned off
    #
    switch -- $itk_option(-completion) {
	0 - no - false - off {
	    return
	}
    }
    #
    #		critical section flag.  it ain't perfect, but for most usage it'll
    #		keep us from being in this code "twice" at the same time
    #		(auto-repeated keystrokes are a pain!)
    #
    if {$_inbs} {
	return
    } else {
	set _inbs 1
    }

    #
    #		set the _doit flag if there is a selection set in the entry field
    #
    set _doit 0
    if [$itk_component(entry) selection present] {
	set _doit 1
    }

    #
    #		clear the semaphore and return
    #
    set _inbs 0
}

# ------------------------------------------------------
# PRIVATE METHOD:	 _lookup
#
# handles auto-completion of text typed (or insert'd) into the entry field.
#
# ------------------------------------------------------
body iwidgets::Combobox::_lookup {key} {
    #
    #		exit if completion is turned off
    #
    switch -- $itk_option(-completion) {
	0 - no - false - off {
	    return
	}
    }

    #
    #		critical section flag.  it ain't perfect, but for most usage it'll
    #		keep us from being in this code "twice" at the same time
    #		(auto-repeated keystrokes are a pain!)
    #
    if {$_inlookup} {
	return
    } else {
	set _inlookup 1
    }

    #
    #		if state of megawidget is disabled, or the entry is not editable,
    #		clear the semaphore and exit
    #
    if {$itk_option(-state) == "disabled" \
	    || [lsearch {on 1 true yes} $itk_option(-editable)] == -1} {
	set _inlookup 0
	return
    }

    #
    #		okay, *now* we can get to work
    #		the _bs function is called on keyPRESS of BackSpace, and will set
    #		the _doit flag if there's a selection set in the entryfield.  If
    #		there is, we're assuming that it's generated by completion itself
    #		(this is probably a Bad Assumption), so we'll want to whack the
    #		selected text, as well as the character immediately preceding the
    #		insertion cursor.
    #
    if {$key == "BackSpace"} {
	if {$_doit} {
	    set first [expr [$itk_component(entry) index insert] -1]
	    $itk_component(entry) delete $first end
	    $itk_component(entry) icursor $first
	}
    }

    #
    #		get the text left in the entry field, and its length.  if
    #		zero-length, clear the selection in the listbox, clear the
    #		semaphore, and boogie.
    #
    set text [get]
    set len [string length $text]
    if {$len == 0} {
	$itk_component(list) selection clear 0 end
	set _inlookup 0
	return
    }

    #
    #		okay, so we have to do a lookup.  find the first match in the
    #		listbox to the text we've got in the entry field (glob).
    #		if one exists, clear the current listbox selection, and set it to
    #		the one we just found, making that one visible in the listbox.
    #		then, pick off the text from the listbox entry that hadn't yet been
    #		entered into the entry field.  we need to tack that text onto the
    #		end of the entry field, select it, and then set the insertion cursor
    #		back to just before the point where we just added that text.
    #		if one didn't exist, then just clear the listbox selection
    #
    set item [lsearch [$itk_component(list) get 0 end] "$text*" ]
    if {$item != -1} {
	$itk_component(list) selection clear 0 end
	$itk_component(list) selection set $item $item
	see $item
	set remainder [string range [$itk_component(list) get $item] \
			   $len end]
	$itk_component(entry) insert end $remainder
	$itk_component(entry) selection range $len end
	$itk_component(entry) icursor $len
    } else {
	$itk_component(list) selection clear 0 end
    }
    #
    #		clear the semaphore and return
    #
    set _inlookup 0
    return
}