-
Notifications
You must be signed in to change notification settings - Fork 0
/
ckEntry.c
1726 lines (1591 loc) · 50.5 KB
/
ckEntry.c
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
/*
* ckEntry.c --
*
* This module implements entry widgets for the
* toolkit. An entry displays a string and allows
* the string to be edited.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1995 Sun Microsystems, Inc.
* Copyright (c) 1995 Christian Werner
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "ckPort.h"
#include "ck.h"
#include "default.h"
/*
* A data structure of the following type is kept for each entry
* widget managed by this file:
*/
typedef struct {
CkWindow *winPtr; /* Window that embodies the entry. NULL
* means that the window has been destroyed
* but the data structures haven't yet been
* cleaned up.*/
Tcl_Interp *interp; /* Interpreter associated with entry. */
Tcl_Command widgetCmd; /* Token for entry's widget command. */
#if CK_USE_UTF
int numBytes; /* Number of bytes in string. */
#endif
int numChars; /* Number of non-NULL characters in
* string (may be 0). */
char *string; /* Pointer to storage for string;
* NULL-terminated; malloc-ed. */
char *textVarName; /* Name of variable (malloc'ed) or NULL.
* If non-NULL, entry's string tracks the
* contents of this variable and vice versa. */
Ck_Uid state; /* Normal or disabled. Entry is read-only
* when disabled. */
/*
* Information used when displaying widget:
*/
int normalBg; /* Normal background color. */
int normalFg; /* Normal foreground color. */
int normalAttr; /* Normal video attributes. */
int selBg; /* Select background color. */
int selFg; /* Select foreground color. */
int selAttr; /* Select video attributes. */
Ck_Justify justify; /* Justification to use for text within
* window. */
int leftX; /* X position at which leftIndex is drawn
* (varies depending on justify). */
int leftIndex; /* Index of left-most character visible in
* window. */
int tabOrigin; /* Origin for tabs (left edge of string[0]). */
int insertPos; /* Index of character before which next
* typed character will be inserted. */
char *showChar; /* Value of -show option. If non-NULL, first
* character is used for displaying all
* characters in entry. Malloc'ed. */
char *displayString; /* If non-NULL, points to string with same
* length as string but whose characters
* are all equal to showChar. Malloc'ed. */
int prefWidth; /* Preferred width for window. */
/*
* Information about what's selected, if any.
*/
int selectFirst; /* Index of first selected character (-1 means
* nothing selected. */
int selectLast; /* Index of last selected character (-1 means
* nothing selected. */
int selectAnchor; /* Fixed end of selection (i.e. "select to"
* operation will use this as one end of the
* selection). */
/*
* Miscellaneous information:
*/
char *takeFocus; /* Value of -takefocus option; not used in
* the C code, but used by keyboard traversal
* scripts. Malloc'ed, but may be NULL. */
char *scrollCmd; /* Command prefix for communicating with
* scrollbar(s). Malloc'ed. NULL means
* no command to issue. */
int flags; /* Miscellaneous flags; see below for
* definitions. */
} Entry;
/*
* Assigned bits of "flags" fields of Entry structures, and what those
* bits mean:
*
* REDRAW_PENDING: Non-zero means a DoWhenIdle handler has
* already been queued to redisplay the entry.
* GOT_FOCUS: Non-zero means this window has the input
* focus.
* UPDATE_SCROLLBAR: Non-zero means scrollbar should be updated
* during next redisplay operation.
*/
#define REDRAW_PENDING 1
#define GOT_FOCUS 2
#define UPDATE_SCROLLBAR 4
/*
* Information used for argv parsing.
*/
static Ck_ConfigSpec configSpecs[] = {
{CK_CONFIG_ATTR, "-attributes", "attributes", "Attributes",
DEF_ENTRY_ATTR, Ck_Offset(Entry, normalAttr), 0},
{CK_CONFIG_COLOR, "-background", "background", "Background",
DEF_ENTRY_BG_COLOR, Ck_Offset(Entry, normalBg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-background", "background", "Background",
DEF_ENTRY_BG_MONO, Ck_Offset(Entry, normalBg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_SYNONYM, "-bg", "background", (char *) NULL,
(char *) NULL, 0, 0},
{CK_CONFIG_SYNONYM, "-fg", "foreground", (char *) NULL,
(char *) NULL, 0, 0},
{CK_CONFIG_COLOR, "-foreground", "foreground", "Foreground",
DEF_ENTRY_FG, Ck_Offset(Entry, normalFg), 0},
{CK_CONFIG_JUSTIFY, "-justify", "justify", "Justify",
DEF_ENTRY_JUSTIFY, Ck_Offset(Entry, justify), 0},
{CK_CONFIG_ATTR, "-selectattributes", "selectAttributes",
"SelectAttributes", DEF_ENTRY_SELECT_ATTR_COLOR,
Ck_Offset(Entry, selAttr), CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_ATTR, "-selectattributes", "selectAttributes",
"SelectAttributes", DEF_ENTRY_SELECT_ATTR_MONO,
Ck_Offset(Entry, selAttr), CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-selectbackground", "selectBackground", "Foreground",
DEF_ENTRY_SELECT_BG_COLOR, Ck_Offset(Entry, selBg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-selectbackground", "selectBackground", "Foreground",
DEF_ENTRY_SELECT_BG_MONO, Ck_Offset(Entry, selBg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_ENTRY_SELECT_FG_COLOR, Ck_Offset(Entry, selFg),
CK_CONFIG_COLOR_ONLY},
{CK_CONFIG_COLOR, "-selectforeground", "selectForeground", "Background",
DEF_ENTRY_SELECT_FG_MONO, Ck_Offset(Entry, selFg),
CK_CONFIG_MONO_ONLY},
{CK_CONFIG_STRING, "-show", "show", "Show",
DEF_ENTRY_SHOW, Ck_Offset(Entry, showChar), CK_CONFIG_NULL_OK},
{CK_CONFIG_UID, "-state", "state", "State",
DEF_ENTRY_STATE, Ck_Offset(Entry, state), 0},
{CK_CONFIG_STRING, "-takefocus", "takeFocus", "TakeFocus",
DEF_ENTRY_TAKE_FOCUS, Ck_Offset(Entry, takeFocus), CK_CONFIG_NULL_OK},
{CK_CONFIG_STRING, "-textvariable", "textVariable", "Variable",
DEF_ENTRY_TEXT_VARIABLE, Ck_Offset(Entry, textVarName),
CK_CONFIG_NULL_OK},
{CK_CONFIG_INT, "-width", "width", "Width",
DEF_ENTRY_WIDTH, Ck_Offset(Entry, prefWidth), 0},
{CK_CONFIG_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
DEF_ENTRY_SCROLL_COMMAND, Ck_Offset(Entry, scrollCmd),
CK_CONFIG_NULL_OK},
{CK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
(char *) NULL, 0, 0}
};
/*
* Flags for GetEntryIndex procedure:
*/
#define ZERO_OK 1
#define LAST_PLUS_ONE_OK 2
/*
* Forward declarations for procedures defined later in this file:
*/
static int ConfigureEntry _ANSI_ARGS_((Tcl_Interp *interp,
Entry *entryPtr, int argc, char **argv,
int flags));
static void DeleteChars _ANSI_ARGS_((Entry *entryPtr, int index,
int count));
static void DestroyEntry _ANSI_ARGS_((ClientData clientData));
static void DisplayEntry _ANSI_ARGS_((ClientData clientData));
static void EntryComputeGeometry _ANSI_ARGS_((Entry *entryPtr));
static void EntryEventProc _ANSI_ARGS_((ClientData clientData,
CkEvent *eventPtr));
static void EntryFocusProc _ANSI_ARGS_ ((Entry *entryPtr,
int gotFocus));
static void EventuallyRedraw _ANSI_ARGS_((Entry *entryPtr));
static void EntryCmdDeletedProc _ANSI_ARGS_((
ClientData clientData));
static void EntrySetValue _ANSI_ARGS_((Entry *entryPtr,
char *value));
static void EntrySelectTo _ANSI_ARGS_((
Entry *entryPtr, int index));
static char * EntryTextVarProc _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, char *name1, char *name2,
int flags));
static void EntryUpdateScrollbar _ANSI_ARGS_((Entry *entryPtr));
static void EntryVisibleRange _ANSI_ARGS_((Entry *entryPtr,
double *firstPtr, double *lastPtr));
static int EntryWidgetCmd _ANSI_ARGS_((ClientData clientData,
Tcl_Interp *interp, int argc, char **argv));
static int GetEntryIndex _ANSI_ARGS_((Tcl_Interp *interp,
Entry *entryPtr, char *string, int *indexPtr));
static void InsertChars _ANSI_ARGS_((Entry *entryPtr, int index,
char *string));
/*
*--------------------------------------------------------------
*
* Ck_EntryCmd --
*
* This procedure is invoked to process the "entry" Tcl
* command. See the user documentation for details on what
* it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Ck_EntryCmd(clientData, interp, argc, argv)
ClientData clientData; /* Main window associated with
* interpreter. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
CkWindow *mainPtr = (CkWindow *) clientData;
register Entry *entryPtr;
CkWindow *new;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " pathName ?options?\"", (char *) NULL);
return TCL_ERROR;
}
new = Ck_CreateWindowFromPath(interp, mainPtr, argv[1], 0);
if (new == NULL) {
return TCL_ERROR;
}
/*
* Initialize the fields of the structure that won't be initialized
* by ConfigureEntry, or that ConfigureEntry requires to be
* initialized already (e.g. resource pointers).
*/
entryPtr = (Entry *) ckalloc(sizeof (Entry));
entryPtr->winPtr = new;
entryPtr->interp = interp;
entryPtr->widgetCmd = Tcl_CreateCommand(interp,
entryPtr->winPtr->pathName, EntryWidgetCmd,
(ClientData) entryPtr, EntryCmdDeletedProc);
#if CK_USE_UTF
entryPtr->numBytes = 0;
#endif
entryPtr->numChars = 0;
entryPtr->string = (char *) ckalloc(1);
entryPtr->string[0] = '\0';
entryPtr->textVarName = NULL;
entryPtr->state = ckNormalUid;
entryPtr->normalBg = 0;
entryPtr->normalFg = 0;
entryPtr->normalAttr = 0;
entryPtr->selBg = 0;
entryPtr->selFg = 0;
entryPtr->selAttr = 0;
entryPtr->justify = CK_JUSTIFY_LEFT;
entryPtr->prefWidth = 0;
entryPtr->leftIndex = 0;
entryPtr->tabOrigin = 0;
entryPtr->insertPos = 0;
entryPtr->showChar = NULL;
entryPtr->displayString = NULL;
entryPtr->prefWidth = 1;
entryPtr->selectFirst = -1;
entryPtr->selectLast = -1;
entryPtr->selectAnchor = 0;
entryPtr->takeFocus = NULL;
entryPtr->scrollCmd = NULL;
entryPtr->flags = 0;
Ck_SetClass(entryPtr->winPtr, "Entry");
Ck_CreateEventHandler(entryPtr->winPtr,
CK_EV_EXPOSE | CK_EV_MAP | CK_EV_DESTROY,
EntryEventProc, (ClientData) entryPtr);
if (ConfigureEntry(interp, entryPtr, argc-2, argv+2, 0) != TCL_OK) {
goto error;
}
Tcl_SetObjResult(interp, Tcl_NewStringObj(entryPtr->winPtr->pathName,-1));
return TCL_OK;
error:
Ck_DestroyWindow(entryPtr->winPtr);
return TCL_ERROR;
}
/*
*--------------------------------------------------------------
*
* EntryWidgetCmd --
*
* This procedure is invoked to process the Tcl command
* that corresponds to a widget managed by this module.
* See the user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
static int
EntryWidgetCmd(clientData, interp, argc, argv)
ClientData clientData; /* Information about entry widget. */
Tcl_Interp *interp; /* Current interpreter. */
int argc; /* Number of arguments. */
char **argv; /* Argument strings. */
{
register Entry *entryPtr = (Entry *) clientData;
int result = TCL_OK;
size_t length;
char buf[256];
int c;
if (argc < 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " option ?arg arg ...?\"", (char *) NULL);
return TCL_ERROR;
}
Ck_Preserve((ClientData) entryPtr);
c = argv[1][0];
length = strlen(argv[1]);
if ((c == 'c') && (strncmp(argv[1], "cget", length) == 0)
&& (length >= 2)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " cget option\"",
(char *) NULL);
goto error;
}
result = Ck_ConfigureValue(interp, entryPtr->winPtr, configSpecs,
(char *) entryPtr, argv[2], 0);
} else if ((c == 'c') && (strncmp(argv[1], "configure", length) == 0)
&& (length >= 2)) {
if (argc == 2) {
result = Ck_ConfigureInfo(interp, entryPtr->winPtr, configSpecs,
(char *) entryPtr, (char *) NULL, 0);
} else if (argc == 3) {
result = Ck_ConfigureInfo(interp, entryPtr->winPtr, configSpecs,
(char *) entryPtr, argv[2], 0);
} else {
result = ConfigureEntry(interp, entryPtr, argc-2, argv+2,
CK_CONFIG_ARGV_ONLY);
}
} else if ((c == 'd') && (strncmp(argv[1], "delete", length) == 0)) {
int first, last;
if ((argc < 3) || (argc > 4)) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " delete firstIndex ?lastIndex?\"",
(char *) NULL);
goto error;
}
if (GetEntryIndex(interp, entryPtr, argv[2], &first) != TCL_OK) {
goto error;
}
if (argc == 3) {
last = first+1;
} else {
if (GetEntryIndex(interp, entryPtr, argv[3], &last) != TCL_OK) {
goto error;
}
}
if ((last >= first) && (entryPtr->state == ckNormalUid)) {
DeleteChars(entryPtr, first, last-first);
}
} else if ((c == 'g') && (strncmp(argv[1], "get", length) == 0)) {
if (argc != 2) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " get\"", (char *) NULL);
goto error;
}
Tcl_SetObjResult(interp, Tcl_NewStringObj(entryPtr->string,-1));
} else if ((c == 'i') && (strncmp(argv[1], "icursor", length) == 0)
&& (length >= 2)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " icursor pos\"",
(char *) NULL);
goto error;
}
if (GetEntryIndex(interp, entryPtr, argv[2], &entryPtr->insertPos)
!= TCL_OK) {
goto error;
}
EventuallyRedraw(entryPtr);
} else if ((c == 'i') && (strncmp(argv[1], "index", length) == 0)
&& (length >= 3)) {
int index;
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " index string\"", (char *) NULL);
goto error;
}
if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
goto error;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(index));
} else if ((c == 'i') && (strncmp(argv[1], "insert", length) == 0)
&& (length >= 3)) {
int index;
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " insert index text\"",
(char *) NULL);
goto error;
}
if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
goto error;
}
if (entryPtr->state == ckNormalUid) {
InsertChars(entryPtr, index, argv[3]);
}
} else if ((c == 's') && (length >= 2)
&& (strncmp(argv[1], "selection", length) == 0)) {
int index, index2;
if (argc < 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " select option ?index?\"", (char *) NULL);
goto error;
}
length = strlen(argv[2]);
c = argv[2][0];
if ((c == 'c') && (strncmp(argv[2], "clear", length) == 0)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " selection clear\"", (char *) NULL);
goto error;
}
if (entryPtr->selectFirst != -1) {
entryPtr->selectFirst = entryPtr->selectLast = -1;
EventuallyRedraw(entryPtr);
}
goto done;
} else if ((c == 'p') && (strncmp(argv[2], "present", length) == 0)) {
if (argc != 3) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " selection present\"", (char *) NULL);
goto error;
}
if (entryPtr->selectFirst == -1) {
Tcl_SetObjResult(interp, Tcl_NewStringObj("0",-1));
} else {
Tcl_SetObjResult(interp, Tcl_NewStringObj("1",-1));
}
goto done;
}
if (argc >= 4) {
if (GetEntryIndex(interp, entryPtr, argv[3], &index) != TCL_OK) {
goto error;
}
}
if ((c == 'a') && (strncmp(argv[2], "adjust", length) == 0)) {
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " selection adjust index\"",
(char *) NULL);
goto error;
}
if (entryPtr->selectFirst >= 0) {
int half1, half2;
half1 = (entryPtr->selectFirst + entryPtr->selectLast)/2;
half2 = (entryPtr->selectFirst + entryPtr->selectLast + 1)/2;
if (index < half1) {
entryPtr->selectAnchor = entryPtr->selectLast;
} else if (index > half2) {
entryPtr->selectAnchor = entryPtr->selectFirst;
} else {
/*
* We're at about the halfway point in the selection;
* just keep the existing anchor.
*/
}
}
EntrySelectTo(entryPtr, index);
} else if ((c == 'f') && (strncmp(argv[2], "from", length) == 0)) {
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " selection from index\"",
(char *) NULL);
goto error;
}
entryPtr->selectAnchor = index;
} else if ((c == 'r') && (strncmp(argv[2], "range", length) == 0)) {
if (argc != 5) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " selection range start end\"",
(char *) NULL);
goto error;
}
if (GetEntryIndex(interp, entryPtr, argv[4], &index2) != TCL_OK) {
goto error;
}
if (index >= index2) {
entryPtr->selectFirst = entryPtr->selectLast = -1;
} else {
entryPtr->selectFirst = index;
entryPtr->selectLast = index2;
}
EventuallyRedraw(entryPtr);
} else if ((c == 't') && (strncmp(argv[2], "to", length) == 0)) {
if (argc != 4) {
Tcl_AppendResult(interp, "wrong # args: should be \"",
argv[0], " selection to index\"",
(char *) NULL);
goto error;
}
EntrySelectTo(entryPtr, index);
} else {
Tcl_AppendResult(interp, "bad selection option \"", argv[2],
"\": must be adjust, clear, from, present, range, or to",
(char *) NULL);
goto error;
}
} else if ((c == 'x') && (strncmp(argv[1], "xview", length) == 0)) {
int index, type, count, charsPerPage;
double fraction, first, last;
if (argc == 2) {
EntryVisibleRange(entryPtr, &first, &last);
sprintf(buf, "%g %g", first, last);
Tcl_SetObjResult(interp, Tcl_NewStringObj(buf,-1));
goto done;
} else if (argc == 3) {
if (GetEntryIndex(interp, entryPtr, argv[2], &index) != TCL_OK) {
goto error;
}
} else {
type = Ck_GetScrollInfo(interp, argc, argv, &fraction, &count);
index = entryPtr->leftIndex;
switch (type) {
case CK_SCROLL_ERROR:
goto error;
case CK_SCROLL_MOVETO:
index = (int) (fraction * entryPtr->numChars);
break;
case CK_SCROLL_PAGES:
charsPerPage = entryPtr->winPtr->width - 2;
if (charsPerPage < 1)
charsPerPage = 1;
index += charsPerPage*count;
break;
case CK_SCROLL_UNITS:
index += count;
break;
}
}
if (index >= entryPtr->numChars) {
index = entryPtr->numChars-1;
}
if (index < 0) {
index = 0;
}
entryPtr->leftIndex = index;
entryPtr->flags |= UPDATE_SCROLLBAR;
EntryComputeGeometry(entryPtr);
EventuallyRedraw(entryPtr);
} else {
Tcl_AppendResult(interp, "bad option \"", argv[1],
"\": must be cget, configure, delete, get, ",
"icursor, index, insert, selection, or xview",
(char *) NULL);
goto error;
}
done:
Ck_Release((ClientData) entryPtr);
return result;
error:
Ck_Release((ClientData) entryPtr);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DestroyEntry --
*
* This procedure is invoked by Ck_EventuallyFree or Ck_Release
* to clean up the internal structure of an entry at a safe time
* (when no-one is using it anymore).
*
* Results:
* None.
*
* Side effects:
* Everything associated with the entry is freed up.
*
*----------------------------------------------------------------------
*/
static void
DestroyEntry(clientData)
ClientData clientData; /* Info about entry widget. */
{
Entry *entryPtr = (Entry *) clientData;
/*
* Free up all the stuff that requires special handling, then
* let Ck_FreeOptions handle all the standard option-related
* stuff.
*/
ckfree(entryPtr->string);
if (entryPtr->textVarName != NULL) {
Tcl_UntraceVar(entryPtr->interp, entryPtr->textVarName,
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
EntryTextVarProc, (ClientData) entryPtr);
}
if (entryPtr->displayString != NULL) {
ckfree(entryPtr->displayString);
}
Ck_FreeOptions(configSpecs, (char *) entryPtr, 0);
ckfree((char *) entryPtr);
}
/*
*----------------------------------------------------------------------
*
* EntryCmdDeletedProc --
*
* This procedure is invoked when a widget command is deleted. If
* the widget isn't already in the process of being destroyed,
* this command destroys it.
*
* Results:
* None.
*
* Side effects:
* The widget is destroyed.
*
*----------------------------------------------------------------------
*/
static void
EntryCmdDeletedProc(clientData)
ClientData clientData; /* Pointer to widget record for widget. */
{
Entry *entryPtr = (Entry *) clientData;
CkWindow *winPtr = entryPtr->winPtr;
/*
* This procedure could be invoked either because the window was
* destroyed and the command was then deleted (in which case winPtr
* is NULL) or because the command was deleted, and then this procedure
* destroys the widget.
*/
if (winPtr != NULL) {
entryPtr->winPtr = NULL;
Ck_DestroyWindow(winPtr);
}
}
/*
*----------------------------------------------------------------------
*
* ConfigureEntry --
*
* This procedure is called to process an argv/argc list, plus
* the Tk option database, in order to configure (or reconfigure)
* an entry widget.
*
* Results:
* The return value is a standard Tcl result. If TCL_ERROR is
* returned, then interp->result contains an error message.
*
* Side effects:
* Configuration information, such as colors, border width,
* etc. get set for entryPtr; old resources get freed,
* if there were any.
*
*----------------------------------------------------------------------
*/
static int
ConfigureEntry(interp, entryPtr, argc, argv, flags)
Tcl_Interp *interp; /* Used for error reporting. */
register Entry *entryPtr; /* Information about widget; may or may
* not already have values for some fields. */
int argc; /* Number of valid entries in argv. */
char **argv; /* Arguments. */
int flags; /* Flags to pass to Tk_ConfigureWidget. */
{
/*
* Eliminate any existing trace on a variable monitored by the entry.
*/
if (entryPtr->textVarName != NULL) {
Tcl_UntraceVar(interp, entryPtr->textVarName,
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
EntryTextVarProc, (ClientData) entryPtr);
}
if (Ck_ConfigureWidget(interp, entryPtr->winPtr, configSpecs,
argc, argv, (char *) entryPtr, flags) != TCL_OK) {
return TCL_ERROR;
}
/*
* If the entry is tied to the value of a variable, then set up
* a trace on the variable's value, create the variable if it doesn't
* exist, and set the entry's value from the variable's value.
*/
if (entryPtr->textVarName != NULL) {
char *value;
value = Tcl_GetVar(interp, entryPtr->textVarName, TCL_GLOBAL_ONLY);
if (value == NULL) {
Tcl_SetVar(interp, entryPtr->textVarName, entryPtr->string,
TCL_GLOBAL_ONLY);
} else {
EntrySetValue(entryPtr, value);
}
Tcl_TraceVar(interp, entryPtr->textVarName,
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
EntryTextVarProc, (ClientData) entryPtr);
}
/*
* A few other options also need special processing, such as parsing
* the geometry and setting the colors.
*/
if ((entryPtr->state != ckNormalUid)
&& (entryPtr->state != ckDisabledUid)) {
Tcl_AppendResult(interp, "bad state value \"", entryPtr->state,
"\": must be normal or disabled", (char *) NULL);
entryPtr->state = ckNormalUid;
return TCL_ERROR;
}
/*
* Recompute the window's geometry and arrange for it to be
* redisplayed.
*/
EntryComputeGeometry(entryPtr);
entryPtr->flags |= UPDATE_SCROLLBAR;
EventuallyRedraw(entryPtr);
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* DisplayEntry --
*
* This procedure redraws the contents of an entry window.
*
* Results:
* None.
*
* Side effects:
* Information appears on the screen.
*
*--------------------------------------------------------------
*/
static void
DisplayEntry(clientData)
ClientData clientData; /* Information about window. */
{
register Entry *entryPtr = (Entry *) clientData;
CkWindow *winPtr = entryPtr->winPtr;
int y, startX, leftIndex, selectFirst, selectLast, insertPos, dummy;
char *displayString;
entryPtr->flags &= ~REDRAW_PENDING;
if ((entryPtr->winPtr == NULL) || !(winPtr->flags & CK_MAPPED))
return;
/*
* Update the scrollbar if that's needed.
*/
if (entryPtr->flags & UPDATE_SCROLLBAR) {
EntryUpdateScrollbar(entryPtr);
}
/*
* Compute x-coordinate of the pixel just after last visible
* one, plus vertical position of baseline of text.
*/
y = winPtr->height / 2;
if (entryPtr->displayString == NULL) {
displayString = entryPtr->string;
} else {
displayString = entryPtr->displayString;
}
Ck_SetWindowAttr(winPtr, entryPtr->normalFg, entryPtr->normalBg,
entryPtr->normalAttr);
Ck_ClearToBot(winPtr, 0, 0);
#if CK_USE_UTF
leftIndex = Tcl_UtfAtIndex(displayString, entryPtr->leftIndex) -
displayString;
selectFirst = Tcl_UtfAtIndex(displayString, entryPtr->selectFirst) -
displayString;
selectLast = Tcl_UtfAtIndex(displayString, entryPtr->selectLast) -
displayString;
insertPos = Tcl_UtfAtIndex(displayString, entryPtr->insertPos) -
displayString;
#else
leftIndex = entryPtr->leftIndex;
selectFirst = entryPtr->selectFirst;
selectLast = entryPtr->selectLast;
insertPos = entryPtr->insertPos;
#endif
CkDisplayChars(winPtr->mainPtr, winPtr->window,
displayString + leftIndex,
strlen(displayString) - leftIndex,
entryPtr->leftX, y, entryPtr->tabOrigin,
CK_NEWLINES_NOT_SPECIAL);
if (entryPtr->selectLast >= entryPtr->leftIndex) {
if (entryPtr->selectFirst < entryPtr->leftIndex) {
startX = 0;
} else {
CkMeasureChars(winPtr->mainPtr,
displayString + leftIndex,
selectFirst - leftIndex, entryPtr->leftX,
winPtr->width, entryPtr->tabOrigin, CK_NEWLINES_NOT_SPECIAL,
&startX, &dummy);
}
if (startX < winPtr->width) {
Ck_SetWindowAttr(winPtr, entryPtr->selFg, entryPtr->selBg,
entryPtr->selAttr);
wmove(winPtr->window, y, startX + entryPtr->leftX);
CkDisplayChars(winPtr->mainPtr, winPtr->window,
displayString + selectFirst,
selectLast - selectFirst,
entryPtr->leftX + startX, y, entryPtr->tabOrigin,
CK_NEWLINES_NOT_SPECIAL);
Ck_SetWindowAttr(winPtr, entryPtr->normalFg, entryPtr->normalBg,
entryPtr->normalAttr);
}
}
CkMeasureChars(winPtr->mainPtr, displayString + leftIndex,
insertPos - leftIndex, entryPtr->leftX,
winPtr->width, entryPtr->tabOrigin, CK_NEWLINES_NOT_SPECIAL,
&startX, &dummy);
if (startX >= 0 && startX < winPtr->width) {
wmove(winPtr->window, y, startX);
if (entryPtr->state == ckNormalUid)
Ck_SetHWCursor(winPtr, 1);
else
Ck_SetHWCursor(winPtr, 0);
} else {
wmove(winPtr->window, y, 0);
Ck_SetHWCursor(winPtr, 0);
}
Ck_EventuallyRefresh(winPtr);
}
/*
*----------------------------------------------------------------------
*
* EntryComputeGeometry --
*
* This procedure is invoked to recompute information about where
* in its window an entry's string will be displayed. It also
* computes the requested size for the window.
*
* Results:
* None.
*
* Side effects:
* The tabOrigin fields are recomputed for entryPtr,
* and leftIndex may be adjusted. Ck_GeometryRequest is called
* to register the desired dimensions for the window.
*
*----------------------------------------------------------------------
*/
static void
EntryComputeGeometry(entryPtr)
Entry *entryPtr; /* Widget record for entry. */
{
int totalLength, overflow, maxOffScreen;
int width, i, rightX, dummy;
char *p, *displayString;
CkWindow *winPtr = entryPtr->winPtr;
/*
* If we're displaying a special character instead of the value of
* the entry, recompute the displayString.
*/
if (entryPtr->displayString != NULL) {
ckfree(entryPtr->displayString);
entryPtr->displayString = NULL;
}
if (entryPtr->showChar != NULL) {
#if CK_USE_UTF
int ulen;
entryPtr->displayString = (char *) ckalloc(entryPtr->numChars * 3 + 1);
ulen = Tcl_UtfNext(entryPtr->showChar) - entryPtr->showChar;
for (p = entryPtr->displayString, i = entryPtr->numChars; i > 0;
i--) {
memcpy(p, entryPtr->showChar, ulen);
p += ulen;
}
#else
entryPtr->displayString = (char *) ckalloc(entryPtr->numChars + 1);
for (p = entryPtr->displayString, i = entryPtr->numChars; i > 0;
i--, p++) {
*p = entryPtr->showChar[0];
}
#endif
*p = 0;
displayString = entryPtr->displayString;
} else {
displayString = entryPtr->string;
}
/*
* Recompute where the leftmost character on the display will
* be drawn (entryPtr->leftX) and adjust leftIndex if necessary
* so that we don't let characters hang off the edge of the
* window unless the entire window is full.
*/
CkMeasureChars(winPtr->mainPtr, displayString, strlen(displayString),
0, INT_MAX, 0,
CK_NEWLINES_NOT_SPECIAL, &totalLength, &dummy);
if (entryPtr->insertPos == entryPtr->numChars)
totalLength += 1;
overflow = totalLength - entryPtr->winPtr->width;
if (overflow < 0) {
entryPtr->leftIndex = 0;
if (entryPtr->justify == CK_JUSTIFY_LEFT) {
entryPtr->leftX = 0;
} else if (entryPtr->justify == CK_JUSTIFY_RIGHT) {
entryPtr->leftX = entryPtr->winPtr->width - totalLength;
} else {
entryPtr->leftX = (entryPtr->winPtr->width - totalLength) / 2;
}
entryPtr->tabOrigin = entryPtr->leftX;
} else {
int leftIndex;
/*
* The whole string can't fit in the window. Compute the
* maximum number of characters that may be off-screen to
* the left without leaving empty space on the right of the
* window, then don't let leftIndex be any greater than that.
*/
maxOffScreen = CkMeasureChars(winPtr->mainPtr,
displayString, strlen(displayString),
0, overflow, 0, CK_NEWLINES_NOT_SPECIAL|CK_PARTIAL_OK, &rightX,
&dummy);
if (rightX < overflow) {
maxOffScreen += 1;
}
if (entryPtr->leftIndex > maxOffScreen) {
entryPtr->leftIndex = maxOffScreen;
}