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
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
|
/*
* $Id: ttkEntry.c,v 1.21 2011/01/22 18:49:34 jenglish Exp $
*
* DERIVED FROM: tk/generic/tkEntry.c r1.35.
*
* Copyright (c) 1990-1994 The Regents of the University of California.
* Copyright (c) 1994-1997 Sun Microsystems, Inc.
* Copyright (c) 2000 Ajuba Solutions.
* Copyright (c) 2002 ActiveState Corporation.
* Copyright (c) 2004 Joe English
*/
#include <string.h>
#include <stdio.h>
#include <tk.h>
#include <X11/Xatom.h>
#include "ttkTheme.h"
#include "ttkWidget.h"
/*
* Extra bits for core.flags:
*/
#define GOT_SELECTION (WIDGET_USER_FLAG<<1)
#define SYNCING_VARIABLE (WIDGET_USER_FLAG<<2)
#define VALIDATING (WIDGET_USER_FLAG<<3)
#define VALIDATION_SET_VALUE (WIDGET_USER_FLAG<<4)
/*
* Definitions for -validate option values:
*/
typedef enum validateMode {
VMODE_ALL, VMODE_KEY, VMODE_FOCUS, VMODE_FOCUSIN, VMODE_FOCUSOUT, VMODE_NONE
} VMODE;
static const char *const validateStrings[] = {
"all", "key", "focus", "focusin", "focusout", "none", NULL
};
/*
* Validation reasons:
*/
typedef enum validateReason {
VALIDATE_INSERT, VALIDATE_DELETE,
VALIDATE_FOCUSIN, VALIDATE_FOCUSOUT,
VALIDATE_FORCED
} VREASON;
static const char *const validateReasonStrings[] = {
"key", "key", "focusin", "focusout", "forced", NULL
};
/*------------------------------------------------------------------------
* +++ Entry widget record.
*
* Dependencies:
*
* textVariableTrace : textVariableObj
*
* numBytes,numChars : string
* displayString : numChars, showChar
* layoutHeight,
* layoutWidth,
* textLayout : fontObj, displayString
* layoutX, layoutY : textLayout, justify, xscroll.first
*
* Invariants:
*
* 0 <= insertPos <= numChars
* 0 <= selectFirst < selectLast <= numChars || selectFirst == selectLast == -1
* displayString points to string if showChar == NULL,
* or to malloc'ed storage if showChar != NULL.
*/
/* Style parameters:
*/
typedef struct {
Tcl_Obj *foregroundObj; /* Foreground color for normal text */
Tcl_Obj *backgroundObj; /* Entry widget background color */
Tcl_Obj *selBorderObj; /* Border and background for selection */
Tcl_Obj *selBorderWidthObj; /* Width of selection border */
Tcl_Obj *selForegroundObj; /* Foreground color for selected text */
Tcl_Obj *insertColorObj; /* Color of insertion cursor */
Tcl_Obj *insertWidthObj; /* Insert cursor width */
} EntryStyleData;
typedef struct {
/*
* Internal state:
*/
char *string; /* Storage for string (malloced) */
int numBytes; /* Length of string in bytes. */
int numChars; /* Length of string in characters. */
int insertPos; /* Insert index */
int selectFirst; /* Index of start of selection, or -1 */
int selectLast; /* Index of end of selection, or -1 */
Scrollable xscroll; /* Current scroll position */
ScrollHandle xscrollHandle;
/*
* Options managed by Tk_SetOptions:
*/
Tcl_Obj *textVariableObj; /* Name of linked variable */
int exportSelection; /* Tie internal selection to X selection? */
VMODE validate; /* Validation mode */
char *validateCmd; /* Validation script template */
char *invalidCmd; /* Invalid callback script template */
char *showChar; /* Used to derive displayString */
Tcl_Obj *fontObj; /* Text font to use */
Tcl_Obj *widthObj; /* Desired width of window (in avgchars) */
Tk_Justify justify; /* Text justification */
EntryStyleData styleData; /* Display style data (widget options) */
EntryStyleData styleDefaults;/* Style defaults (fallback values) */
Tcl_Obj *stateObj; /* Compatibility option -- see CheckStateObj */
/*
* Derived resources:
*/
Ttk_TraceHandle *textVariableTrace;
char *displayString; /* String to use when displaying */
Tk_TextLayout textLayout; /* Cached text layout information. */
int layoutWidth; /* textLayout width */
int layoutHeight; /* textLayout height */
int layoutX, layoutY; /* Origin for text layout. */
} EntryPart;
typedef struct {
WidgetCore core;
EntryPart entry;
} Entry;
/*
* Extra mask bits for Tk_SetOptions()
*/
#define STATE_CHANGED (0x100) /* -state option changed */
#define TEXTVAR_CHANGED (0x200) /* -textvariable option changed */
#define SCROLLCMD_CHANGED (0x400) /* -xscrollcommand option changed */
/*
* Default option values:
*/
#define DEF_SELECT_BG "#000000"
#define DEF_SELECT_FG "#ffffff"
#define DEF_INSERT_BG "black"
#define DEF_ENTRY_WIDTH "20"
#define DEF_ENTRY_FONT "TkTextFont"
#define DEF_LIST_HEIGHT "10"
static Tk_OptionSpec EntryOptionSpecs[] = {
WIDGET_TAKES_FOCUS,
{TK_OPTION_BOOLEAN, "-exportselection", "exportSelection",
"ExportSelection", "1", -1, Tk_Offset(Entry, entry.exportSelection),
0,0,0 },
{TK_OPTION_FONT, "-font", "font", "Font",
DEF_ENTRY_FONT, Tk_Offset(Entry, entry.fontObj),-1,
0,0,GEOMETRY_CHANGED},
{TK_OPTION_STRING, "-invalidcommand", "invalidCommand", "InvalidCommand",
NULL, -1, Tk_Offset(Entry, entry.invalidCmd),
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_JUSTIFY, "-justify", "justify", "Justify",
"left", -1, Tk_Offset(Entry, entry.justify),
0, 0, GEOMETRY_CHANGED},
{TK_OPTION_STRING, "-show", "show", "Show",
NULL, -1, Tk_Offset(Entry, entry.showChar),
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_STRING, "-state", "state", "State",
"normal", Tk_Offset(Entry, entry.stateObj), -1,
0,0,STATE_CHANGED},
{TK_OPTION_STRING, "-textvariable", "textVariable", "Variable",
NULL, Tk_Offset(Entry, entry.textVariableObj), -1,
TK_OPTION_NULL_OK,0,TEXTVAR_CHANGED},
{TK_OPTION_STRING_TABLE, "-validate", "validate", "Validate",
"none", -1, Tk_Offset(Entry, entry.validate),
0, (ClientData) validateStrings, 0},
{TK_OPTION_STRING, "-validatecommand", "validateCommand", "ValidateCommand",
NULL, -1, Tk_Offset(Entry, entry.validateCmd),
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_INT, "-width", "width", "Width",
DEF_ENTRY_WIDTH, Tk_Offset(Entry, entry.widthObj), -1,
0,0,GEOMETRY_CHANGED},
{TK_OPTION_STRING, "-xscrollcommand", "xScrollCommand", "ScrollCommand",
NULL, -1, Tk_Offset(Entry, entry.xscroll.scrollCmd),
TK_OPTION_NULL_OK, 0, SCROLLCMD_CHANGED},
/* EntryStyleData options:
*/
{TK_OPTION_COLOR, "-foreground", "textColor", "TextColor",
NULL, Tk_Offset(Entry, entry.styleData.foregroundObj), -1,
TK_OPTION_NULL_OK,0,0},
{TK_OPTION_COLOR, "-background", "windowColor", "WindowColor",
NULL, Tk_Offset(Entry, entry.styleData.backgroundObj), -1,
TK_OPTION_NULL_OK,0,0},
WIDGET_INHERIT_OPTIONS(ttkCoreOptionSpecs)
};
/*------------------------------------------------------------------------
* +++ EntryStyleData management.
* This is still more awkward than it should be;
* it should be able to use the Element API instead.
*/
/* EntryInitStyleDefaults --
* Initialize EntryStyleData record to fallback values.
*/
static void EntryInitStyleDefaults(EntryStyleData *es)
{
#define INIT(member, value) \
es->member = Tcl_NewStringObj(value, -1); \
Tcl_IncrRefCount(es->member);
INIT(foregroundObj, DEFAULT_FOREGROUND)
INIT(selBorderObj, DEF_SELECT_BG)
INIT(selForegroundObj, DEF_SELECT_FG)
INIT(insertColorObj, DEFAULT_FOREGROUND)
INIT(selBorderWidthObj, "0")
INIT(insertWidthObj, "1")
#undef INIT
}
static void EntryFreeStyleDefaults(EntryStyleData *es)
{
Tcl_DecrRefCount(es->foregroundObj);
Tcl_DecrRefCount(es->selBorderObj);
Tcl_DecrRefCount(es->selForegroundObj);
Tcl_DecrRefCount(es->insertColorObj);
Tcl_DecrRefCount(es->selBorderWidthObj);
Tcl_DecrRefCount(es->insertWidthObj);
}
/*
* EntryInitStyleData --
* Look up style-specific data for an entry widget.
*/
static void EntryInitStyleData(Entry *entryPtr, EntryStyleData *es)
{
Ttk_State state = entryPtr->core.state;
Ttk_ResourceCache cache = Ttk_GetResourceCache(entryPtr->core.interp);
Tk_Window tkwin = entryPtr->core.tkwin;
Tcl_Obj *tmp;
/* Initialize to fallback values:
*/
*es = entryPtr->entry.styleDefaults;
# define INIT(member, name) \
if ((tmp=Ttk_QueryOption(entryPtr->core.layout,name,state))) \
es->member=tmp;
INIT(foregroundObj, "-foreground");
INIT(selBorderObj, "-selectbackground")
INIT(selBorderWidthObj, "-selectborderwidth")
INIT(selForegroundObj, "-selectforeground")
INIT(insertColorObj, "-insertcolor")
INIT(insertWidthObj, "-insertwidth")
#undef INIT
/* Reacquire color & border resources from resource cache.
*/
es->foregroundObj = Ttk_UseColor(cache, tkwin, es->foregroundObj);
es->selForegroundObj = Ttk_UseColor(cache, tkwin, es->selForegroundObj);
es->insertColorObj = Ttk_UseColor(cache, tkwin, es->insertColorObj);
es->selBorderObj = Ttk_UseBorder(cache, tkwin, es->selBorderObj);
}
/*------------------------------------------------------------------------
* +++ Resource management.
*/
/* EntryDisplayString --
* Return a malloc'ed string consisting of 'numChars' copies
* of (the first character in the string) 'showChar'.
* Used to compute the displayString if -show is non-NULL.
*/
static char *EntryDisplayString(const char *showChar, int numChars)
{
char *displayString, *p;
int size;
Tcl_UniChar ch;
char buf[TCL_UTF_MAX];
Tcl_UtfToUniChar(showChar, &ch);
size = Tcl_UniCharToUtf(ch, buf);
p = displayString = ckalloc(numChars * size + 1);
while (numChars--) {
p += Tcl_UniCharToUtf(ch, p);
}
*p = '\0';
return displayString;
}
/* EntryUpdateTextLayout --
* Recompute textLayout, layoutWidth, and layoutHeight
* from displayString and fontObj.
*/
static void EntryUpdateTextLayout(Entry *entryPtr)
{
Tk_FreeTextLayout(entryPtr->entry.textLayout);
entryPtr->entry.textLayout = Tk_ComputeTextLayout(
Tk_GetFontFromObj(entryPtr->core.tkwin, entryPtr->entry.fontObj),
entryPtr->entry.displayString, entryPtr->entry.numChars,
0/*wraplength*/, entryPtr->entry.justify, TK_IGNORE_NEWLINES,
&entryPtr->entry.layoutWidth, &entryPtr->entry.layoutHeight);
}
/* EntryEditable --
* Returns 1 if the entry widget accepts user changes, 0 otherwise
*/
static int
EntryEditable(Entry *entryPtr)
{
return !(entryPtr->core.state & (TTK_STATE_DISABLED|TTK_STATE_READONLY));
}
/*------------------------------------------------------------------------
* +++ Selection management.
*/
/* EntryFetchSelection --
* Selection handler for entry widgets.
*/
static int
EntryFetchSelection(
ClientData clientData, int offset, char *buffer, int maxBytes)
{
Entry *entryPtr = (Entry *) clientData;
size_t byteCount;
const char *string;
const char *selStart, *selEnd;
if (entryPtr->entry.selectFirst < 0 || !entryPtr->entry.exportSelection) {
return -1;
}
string = entryPtr->entry.displayString;
selStart = Tcl_UtfAtIndex(string, entryPtr->entry.selectFirst);
selEnd = Tcl_UtfAtIndex(selStart,
entryPtr->entry.selectLast - entryPtr->entry.selectFirst);
byteCount = selEnd - selStart - offset;
if (byteCount > (size_t)maxBytes) {
/* @@@POSSIBLE BUG: Can transfer partial UTF-8 sequences. Is this OK? */
byteCount = maxBytes;
}
if (byteCount <= 0) {
return 0;
}
memcpy(buffer, selStart + offset, byteCount);
buffer[byteCount] = '\0';
return byteCount;
}
/* EntryLostSelection --
* Tk_LostSelProc for Entry widgets; called when an entry
* loses ownership of the selection.
*/
static void EntryLostSelection(ClientData clientData)
{
Entry *entryPtr = (Entry *) clientData;
entryPtr->core.flags &= ~GOT_SELECTION;
entryPtr->entry.selectFirst = entryPtr->entry.selectLast = -1;
TtkRedisplayWidget(&entryPtr->core);
}
/* EntryOwnSelection --
* Assert ownership of the PRIMARY selection,
* if -exportselection set and selection is present.
*/
static void EntryOwnSelection(Entry *entryPtr)
{
if (entryPtr->entry.exportSelection
&& !(entryPtr->core.flags & GOT_SELECTION)) {
Tk_OwnSelection(entryPtr->core.tkwin, XA_PRIMARY, EntryLostSelection,
(ClientData) entryPtr);
entryPtr->core.flags |= GOT_SELECTION;
}
}
/*------------------------------------------------------------------------
* +++ Validation.
*/
/* ExpandPercents --
* Expand an entry validation script template (-validatecommand
* or -invalidcommand).
*/
static void
ExpandPercents(
Entry *entryPtr, /* Entry that needs validation. */
const char *template, /* Script template */
const char *new, /* Potential new value of entry string */
int index, /* index of insert/delete */
int count, /* #changed characters */
VREASON reason, /* Reason for change */
Tcl_DString *dsPtr) /* Result of %-substitutions */
{
int spaceNeeded, cvtFlags;
int number, length;
const char *string;
int stringLength;
Tcl_UniChar ch;
char numStorage[2*TCL_INTEGER_SPACE];
while (*template) {
/* Find everything up to the next % character and append it
* to the result string.
*/
string = Tcl_UtfFindFirst(template, '%');
if (string == NULL) {
/* No more %-sequences to expand.
* Copy the rest of the template.
*/
Tcl_DStringAppend(dsPtr, template, -1);
return;
}
if (string != template) {
Tcl_DStringAppend(dsPtr, template, string - template);
template = string;
}
/* There's a percent sequence here. Process it.
*/
++template; /* skip over % */
if (*template != '\0') {
template += Tcl_UtfToUniChar(template, &ch);
} else {
ch = '%';
}
stringLength = -1;
switch (ch) {
case 'd': /* Type of call that caused validation */
if (reason == VALIDATE_INSERT) {
number = 1;
} else if (reason == VALIDATE_DELETE) {
number = 0;
} else {
number = -1;
}
sprintf(numStorage, "%d", number);
string = numStorage;
break;
case 'i': /* index of insert/delete */
sprintf(numStorage, "%d", index);
string = numStorage;
break;
case 'P': /* 'Peeked' new value of the string */
string = new;
break;
case 's': /* Current string value */
string = entryPtr->entry.string;
break;
case 'S': /* string to be inserted/deleted, if any */
if (reason == VALIDATE_INSERT) {
string = Tcl_UtfAtIndex(new, index);
stringLength = Tcl_UtfAtIndex(string, count) - string;
} else if (reason == VALIDATE_DELETE) {
string = Tcl_UtfAtIndex(entryPtr->entry.string, index);
stringLength = Tcl_UtfAtIndex(string, count) - string;
} else {
string = "";
stringLength = 0;
}
break;
case 'v': /* type of validation currently set */
string = validateStrings[entryPtr->entry.validate];
break;
case 'V': /* type of validation in effect */
string = validateReasonStrings[reason];
break;
case 'W': /* widget name */
string = Tk_PathName(entryPtr->core.tkwin);
break;
default:
length = Tcl_UniCharToUtf(ch, numStorage);
numStorage[length] = '\0';
string = numStorage;
break;
}
spaceNeeded = Tcl_ScanCountedElement(string, stringLength, &cvtFlags);
length = Tcl_DStringLength(dsPtr);
Tcl_DStringSetLength(dsPtr, length + spaceNeeded);
spaceNeeded = Tcl_ConvertCountedElement(string, stringLength,
Tcl_DStringValue(dsPtr) + length,
cvtFlags | TCL_DONT_USE_BRACES);
Tcl_DStringSetLength(dsPtr, length + spaceNeeded);
}
}
/* RunValidationScript --
* Build and evaluate an entry validation script.
* If the script raises an error, disable validation
* by setting '-validate none'
*/
static int RunValidationScript(
Tcl_Interp *interp, /* Interpreter to use */
Entry *entryPtr, /* Entry being validated */
const char *template, /* Script template */
const char *optionName, /* "-validatecommand", "-invalidcommand" */
const char *new, /* Potential new value of entry string */
int index, /* index of insert/delete */
int count, /* #changed characters */
VREASON reason) /* Reason for change */
{
Tcl_DString script;
int code;
Tcl_DStringInit(&script);
ExpandPercents(entryPtr, template, new, index, count, reason, &script);
code = Tcl_EvalEx(interp,
Tcl_DStringValue(&script), Tcl_DStringLength(&script),
TCL_EVAL_GLOBAL);
Tcl_DStringFree(&script);
if (WidgetDestroyed(&entryPtr->core))
return TCL_ERROR;
if (code != TCL_OK && code != TCL_RETURN) {
Tcl_AddErrorInfo(interp, "\n\t(in ");
Tcl_AddErrorInfo(interp, optionName);
Tcl_AddErrorInfo(interp, " validation command executed by ");
Tcl_AddErrorInfo(interp, Tk_PathName(entryPtr->core.tkwin));
Tcl_AddErrorInfo(interp, ")");
entryPtr->entry.validate = VMODE_NONE;
return TCL_ERROR;
}
return TCL_OK;
}
/* EntryNeedsValidation --
* Determine whether the specified VREASON should trigger validation
* in the current VMODE.
*/
static int EntryNeedsValidation(VMODE vmode, VREASON reason)
{
return (reason == VALIDATE_FORCED)
|| (vmode == VMODE_ALL)
|| (reason == VALIDATE_FOCUSIN
&& (vmode == VMODE_FOCUSIN || vmode == VMODE_FOCUS))
|| (reason == VALIDATE_FOCUSOUT
&& (vmode == VMODE_FOCUSOUT || vmode == VMODE_FOCUS))
|| (reason == VALIDATE_INSERT && vmode == VMODE_KEY)
|| (reason == VALIDATE_DELETE && vmode == VMODE_KEY)
;
}
/* EntryValidateChange --
* Validate a proposed change to the entry widget's value if required.
* Call the -invalidcommand if validation fails.
*
* Returns:
* TCL_OK if the change is accepted
* TCL_BREAK if the change is rejected
* TCL_ERROR if any errors occured
*
* The change will be rejected if -validatecommand returns 0,
* or if -validatecommand or -invalidcommand modifies the value.
*/
static int
EntryValidateChange(
Entry *entryPtr, /* Entry that needs validation. */
const char *newValue, /* Potential new value of entry string */
int index, /* index of insert/delete, -1 otherwise */
int count, /* #changed characters */
VREASON reason) /* Reason for change */
{
Tcl_Interp *interp = entryPtr->core.interp;
VMODE vmode = entryPtr->entry.validate;
int code, change_ok;
if ( (entryPtr->entry.validateCmd == NULL)
|| (entryPtr->core.flags & VALIDATING)
|| !EntryNeedsValidation(vmode, reason) )
{
return TCL_OK;
}
entryPtr->core.flags |= VALIDATING;
/* Run -validatecommand and check return value:
*/
code = RunValidationScript(interp, entryPtr,
entryPtr->entry.validateCmd, "-validatecommand",
newValue, index, count, reason);
if (code != TCL_OK) {
goto done;
}
code = Tcl_GetBooleanFromObj(interp,Tcl_GetObjResult(interp), &change_ok);
if (code != TCL_OK) {
entryPtr->entry.validate = VMODE_NONE; /* Disable validation */
Tcl_AddErrorInfo(interp,
"\n(validation command did not return valid boolean)");
goto done;
}
/* Run the -invalidcommand if validation failed:
*/
if (!change_ok && entryPtr->entry.invalidCmd != NULL) {
code = RunValidationScript(interp, entryPtr,
entryPtr->entry.invalidCmd, "-invalidcommand",
newValue, index, count, reason);
if (code != TCL_OK) {
goto done;
}
}
/* Reject the pending change if validation failed
* or if a validation script changed the value.
*/
if (!change_ok || (entryPtr->core.flags & VALIDATION_SET_VALUE)) {
code = TCL_BREAK;
}
done:
entryPtr->core.flags &= ~(VALIDATING|VALIDATION_SET_VALUE);
return code;
}
/* EntryRevalidate --
* Revalidate the current value of an entry widget,
* update the TTK_STATE_INVALID bit.
*
* Returns:
* TCL_OK if valid, TCL_BREAK if invalid, TCL_ERROR on error.
*/
static int EntryRevalidate(Tcl_Interp *interp, Entry *entryPtr, VREASON reason)
{
int code = EntryValidateChange(
entryPtr, entryPtr->entry.string, -1,0, reason);
if (code == TCL_BREAK) {
TtkWidgetChangeState(&entryPtr->core, TTK_STATE_INVALID, 0);
} else if (code == TCL_OK) {
TtkWidgetChangeState(&entryPtr->core, 0, TTK_STATE_INVALID);
}
return code;
}
/* EntryRevalidateBG --
* Revalidate in the background (called from event handler).
*/
static void EntryRevalidateBG(Entry *entryPtr, VREASON reason)
{
Tcl_Interp *interp = entryPtr->core.interp;
if (EntryRevalidate(interp, entryPtr, reason) == TCL_ERROR) {
Tcl_BackgroundError(interp);
}
}
/*------------------------------------------------------------------------
* +++ Entry widget modification.
*/
/* AdjustIndex --
* Adjust index to account for insertion (nChars > 0)
* or deletion (nChars < 0) at specified index.
*/
static int AdjustIndex(int i0, int index, int nChars)
{
if (i0 >= index) {
i0 += nChars;
if (i0 < index) { /* index was inside deleted range */
i0 = index;
}
}
return i0;
}
/* AdjustIndices --
* Adjust all internal entry indexes to account for change.
* Note that insertPos, and selectFirst have "right gravity",
* while leftIndex (=xscroll.first) and selectLast have "left gravity".
*/
static void AdjustIndices(Entry *entryPtr, int index, int nChars)
{
EntryPart *e = &entryPtr->entry;
int g = nChars > 0; /* left gravity adjustment */
e->insertPos = AdjustIndex(e->insertPos, index, nChars);
e->selectFirst = AdjustIndex(e->selectFirst, index, nChars);
e->selectLast = AdjustIndex(e->selectLast, index+g, nChars);
e->xscroll.first= AdjustIndex(e->xscroll.first, index+g, nChars);
if (e->selectLast <= e->selectFirst)
e->selectFirst = e->selectLast = -1;
}
/* EntryStoreValue --
* Replace the contents of a text entry with a given value,
* recompute dependent resources, and schedule a redisplay.
*
* See also: EntrySetValue().
*/
static void
EntryStoreValue(Entry *entryPtr, const char *value)
{
size_t numBytes = strlen(value);
int numChars = Tcl_NumUtfChars(value, numBytes);
if (entryPtr->core.flags & VALIDATING)
entryPtr->core.flags |= VALIDATION_SET_VALUE;
/* Make sure all indices remain in bounds:
*/
if (numChars < entryPtr->entry.numChars)
AdjustIndices(entryPtr, numChars, numChars - entryPtr->entry.numChars);
/* Free old value:
*/
if (entryPtr->entry.displayString != entryPtr->entry.string)
ckfree(entryPtr->entry.displayString);
ckfree(entryPtr->entry.string);
/* Store new value:
*/
entryPtr->entry.string = ckalloc(numBytes + 1);
strcpy(entryPtr->entry.string, value);
entryPtr->entry.numBytes = numBytes;
entryPtr->entry.numChars = numChars;
entryPtr->entry.displayString
= entryPtr->entry.showChar
? EntryDisplayString(entryPtr->entry.showChar, numChars)
: entryPtr->entry.string
;
/* Update layout, schedule redisplay:
*/
EntryUpdateTextLayout(entryPtr);
TtkRedisplayWidget(&entryPtr->core);
}
/* EntrySetValue --
* Stores a new value in the entry widget and updates the
* linked -textvariable, if any. The write trace on the
* text variable is temporarily disabled; however, other
* write traces may change the value of the variable.
* If so, the widget is updated again with the new value.
*
* Returns:
* TCL_OK if successful, TCL_ERROR otherwise.
*/
static int EntrySetValue(Entry *entryPtr, const char *value)
{
EntryStoreValue(entryPtr, value);
if (entryPtr->entry.textVariableObj) {
const char *textVarName =
Tcl_GetString(entryPtr->entry.textVariableObj);
if (textVarName && *textVarName) {
entryPtr->core.flags |= SYNCING_VARIABLE;
value = Tcl_SetVar(entryPtr->core.interp, textVarName,
value, TCL_GLOBAL_ONLY|TCL_LEAVE_ERR_MSG);
entryPtr->core.flags &= ~SYNCING_VARIABLE;
if (!value || WidgetDestroyed(&entryPtr->core)) {
return TCL_ERROR;
} else if (strcmp(value, entryPtr->entry.string) != 0) {
/* Some write trace has changed the variable value.
*/
EntryStoreValue(entryPtr, value);
}
}
}
return TCL_OK;
}
/* EntryTextVariableTrace --
* Variable trace procedure for entry -textvariable
*/
static void EntryTextVariableTrace(void *recordPtr, const char *value)
{
Entry *entryPtr = recordPtr;
if (WidgetDestroyed(&entryPtr->core)) {
return;
}
if (entryPtr->core.flags & SYNCING_VARIABLE) {
/* Trace was fired due to Tcl_SetVar call in EntrySetValue.
* Don't do anything.
*/
return;
}
EntryStoreValue(entryPtr, value ? value : "");
}
/*------------------------------------------------------------------------
* +++ Insertion and deletion.
*/
/* InsertChars --
* Add new characters to an entry widget.
*/
static int
InsertChars(
Entry *entryPtr, /* Entry that is to get the new elements. */
int index, /* Insert before this index */
const char *value) /* New characters to add */
{
char *string = entryPtr->entry.string;
size_t byteIndex = Tcl_UtfAtIndex(string, index) - string;
size_t byteCount = strlen(value);
int charsAdded = Tcl_NumUtfChars(value, byteCount);
size_t newByteCount = entryPtr->entry.numBytes + byteCount + 1;
char *new;
int code;
if (byteCount == 0) {
return TCL_OK;
}
new = ckalloc(newByteCount);
memcpy(new, string, byteIndex);
strcpy(new + byteIndex, value);
strcpy(new + byteIndex + byteCount, string + byteIndex);
code = EntryValidateChange(
entryPtr, new, index, charsAdded, VALIDATE_INSERT);
if (code == TCL_OK) {
AdjustIndices(entryPtr, index, charsAdded);
code = EntrySetValue(entryPtr, new);
} else if (code == TCL_BREAK) {
code = TCL_OK;
}
ckfree(new);
return code;
}
/* DeleteChars --
* Remove one or more characters from an entry widget.
*/
static int
DeleteChars(
Entry *entryPtr, /* Entry widget to modify. */
int index, /* Index of first character to delete. */
int count) /* How many characters to delete. */
{
char *string = entryPtr->entry.string;
size_t byteIndex, byteCount, newByteCount;
char *new;
int code;
if (index < 0) {
index = 0;
}
if (count > entryPtr->entry.numChars - index) {
count = entryPtr->entry.numChars - index;
}
if (count <= 0) {
return TCL_OK;
}
byteIndex = Tcl_UtfAtIndex(string, index) - string;
byteCount = Tcl_UtfAtIndex(string+byteIndex, count) - (string+byteIndex);
newByteCount = entryPtr->entry.numBytes + 1 - byteCount;
new = ckalloc(newByteCount);
memcpy(new, string, byteIndex);
strcpy(new + byteIndex, string + byteIndex + byteCount);
code = EntryValidateChange(
entryPtr, new, index, count, VALIDATE_DELETE);
if (code == TCL_OK) {
AdjustIndices(entryPtr, index, -count);
code = EntrySetValue(entryPtr, new);
} else if (code == TCL_BREAK) {
code = TCL_OK;
}
ckfree(new);
return code;
}
/*------------------------------------------------------------------------
* +++ Event handler.
*/
/* EntryEventProc --
* Extra event handling for entry widgets:
* Triggers validation on FocusIn and FocusOut events.
*/
#define EntryEventMask (FocusChangeMask)
static void
EntryEventProc(ClientData clientData, XEvent *eventPtr)
{
Entry *entryPtr = (Entry *) clientData;
Tcl_Preserve(clientData);
switch (eventPtr->type) {
case DestroyNotify:
Tk_DeleteEventHandler(entryPtr->core.tkwin,
EntryEventMask, EntryEventProc, clientData);
break;
case FocusIn:
EntryRevalidateBG(entryPtr, VALIDATE_FOCUSIN);
break;
case FocusOut:
EntryRevalidateBG(entryPtr, VALIDATE_FOCUSOUT);
break;
}
Tcl_Release(clientData);
}
/*------------------------------------------------------------------------
* +++ Initialization and cleanup.
*/
static void
EntryInitialize(Tcl_Interp *interp, void *recordPtr)
{
Entry *entryPtr = recordPtr;
Tk_CreateEventHandler(
entryPtr->core.tkwin, EntryEventMask, EntryEventProc, entryPtr);
Tk_CreateSelHandler(entryPtr->core.tkwin, XA_PRIMARY, XA_STRING,
EntryFetchSelection, (ClientData) entryPtr, XA_STRING);
TtkBlinkCursor(&entryPtr->core);
entryPtr->entry.string = ckalloc(1);
*entryPtr->entry.string = '\0';
entryPtr->entry.displayString = entryPtr->entry.string;
entryPtr->entry.textVariableTrace = 0;
entryPtr->entry.numBytes = entryPtr->entry.numChars = 0;
EntryInitStyleDefaults(&entryPtr->entry.styleDefaults);
entryPtr->entry.xscrollHandle =
TtkCreateScrollHandle(&entryPtr->core, &entryPtr->entry.xscroll);
entryPtr->entry.insertPos = 0;
entryPtr->entry.selectFirst = -1;
entryPtr->entry.selectLast = -1;
}
static void
EntryCleanup(void *recordPtr)
{
Entry *entryPtr = recordPtr;
if (entryPtr->entry.textVariableTrace)
Ttk_UntraceVariable(entryPtr->entry.textVariableTrace);
TtkFreeScrollHandle(entryPtr->entry.xscrollHandle);
EntryFreeStyleDefaults(&entryPtr->entry.styleDefaults);
Tk_DeleteSelHandler(entryPtr->core.tkwin, XA_PRIMARY, XA_STRING);
Tk_FreeTextLayout(entryPtr->entry.textLayout);
if (entryPtr->entry.displayString != entryPtr->entry.string)
ckfree(entryPtr->entry.displayString);
ckfree(entryPtr->entry.string);
}
/* EntryConfigure --
* Configure hook for Entry widgets.
*/
static int EntryConfigure(Tcl_Interp *interp, void *recordPtr, int mask)
{
Entry *entryPtr = recordPtr;
Tcl_Obj *textVarName = entryPtr->entry.textVariableObj;
Ttk_TraceHandle *vt = 0;
if (mask & TEXTVAR_CHANGED) {
if (textVarName && *Tcl_GetString(textVarName)) {
vt = Ttk_TraceVariable(interp,
textVarName,EntryTextVariableTrace,entryPtr);
if (!vt) return TCL_ERROR;
}
}
if (TtkCoreConfigure(interp, recordPtr, mask) != TCL_OK) {
if (vt) Ttk_UntraceVariable(vt);
return TCL_ERROR;
}
/* Update derived resources:
*/
if (mask & TEXTVAR_CHANGED) {
if (entryPtr->entry.textVariableTrace)
Ttk_UntraceVariable(entryPtr->entry.textVariableTrace);
entryPtr->entry.textVariableTrace = vt;
}
/* Claim the selection, in case we've suddenly started exporting it.
*/
if (entryPtr->entry.exportSelection && entryPtr->entry.selectFirst != -1) {
EntryOwnSelection(entryPtr);
}
/* Handle -state compatibility option:
*/
if (mask & STATE_CHANGED) {
TtkCheckStateOption(&entryPtr->core, entryPtr->entry.stateObj);
}
/* Force scrollbar update if needed:
*/
if (mask & SCROLLCMD_CHANGED) {
TtkScrollbarUpdateRequired(entryPtr->entry.xscrollHandle);
}
/* Recompute the displayString, in case showChar changed:
*/
if (entryPtr->entry.displayString != entryPtr->entry.string)
ckfree(entryPtr->entry.displayString);
entryPtr->entry.displayString
= entryPtr->entry.showChar
? EntryDisplayString(entryPtr->entry.showChar, entryPtr->entry.numChars)
: entryPtr->entry.string
;
/* Update textLayout:
*/
EntryUpdateTextLayout(entryPtr);
return TCL_OK;
}
/* EntryPostConfigure --
* Post-configuration hook for entry widgets.
*/
static int EntryPostConfigure(Tcl_Interp *interp, void *recordPtr, int mask)
{
Entry *entryPtr = recordPtr;
int status = TCL_OK;
if ((mask & TEXTVAR_CHANGED) && entryPtr->entry.textVariableTrace != NULL) {
status = Ttk_FireTrace(entryPtr->entry.textVariableTrace);
}
return status;
}
/*------------------------------------------------------------------------
* +++ Layout and display.
*/
/* EntryCharPosition --
* Return the X coordinate of the specified character index.
* Precondition: textLayout and layoutX up-to-date.
*/
static int
EntryCharPosition(Entry *entryPtr, int index)
{
int xPos;
Tk_CharBbox(entryPtr->entry.textLayout, index, &xPos, NULL, NULL, NULL);
return xPos + entryPtr->entry.layoutX;
}
/* EntryDoLayout --
* Layout hook for entry widgets.
*
* Determine position of textLayout based on xscroll.first, justify,
* and display area.
*
* Recalculates layoutX, layoutY, and rightIndex,
* and updates xscroll accordingly.
* May adjust xscroll.first to ensure the maximum #characters are onscreen.
*/
static void
EntryDoLayout(void *recordPtr)
{
Entry *entryPtr = recordPtr;
WidgetCore *corePtr = &entryPtr->core;
Tk_TextLayout textLayout = entryPtr->entry.textLayout;
int leftIndex = entryPtr->entry.xscroll.first;
int rightIndex;
Ttk_Box textarea;
Ttk_PlaceLayout(corePtr->layout,corePtr->state,Ttk_WinBox(corePtr->tkwin));
textarea = Ttk_ClientRegion(corePtr->layout, "textarea");
/* Center the text vertically within the available parcel:
*/
entryPtr->entry.layoutY = textarea.y +
(textarea.height - entryPtr->entry.layoutHeight)/2;
/* Recompute where the leftmost character on the display will
* be drawn (layoutX) and adjust leftIndex if necessary.
*/
if (entryPtr->entry.layoutWidth <= textarea.width) {
/* Everything fits. Set leftIndex to zero (no need to scroll),
* and compute layoutX based on -justify.
*/
int extraSpace = textarea.width - entryPtr->entry.layoutWidth;
leftIndex = 0;
rightIndex = entryPtr->entry.numChars;
entryPtr->entry.layoutX = textarea.x;
if (entryPtr->entry.justify == TK_JUSTIFY_RIGHT) {
entryPtr->entry.layoutX += extraSpace;
} else if (entryPtr->entry.justify == TK_JUSTIFY_CENTER) {
entryPtr->entry.layoutX += extraSpace / 2;
}
} else {
/* The whole string doesn't fit in the window.
* Limit leftIndex to leave at most one character's worth
* of empty space on the right.
*/
int overflow = entryPtr->entry.layoutWidth - textarea.width;
int maxLeftIndex = 1 + Tk_PointToChar(textLayout, overflow, 0);
int leftX;
if (leftIndex > maxLeftIndex) {
leftIndex = maxLeftIndex;
}
/* Compute layoutX and rightIndex.
* rightIndex is set to one past the last fully-visible character.
*/
Tk_CharBbox(textLayout, leftIndex, &leftX, NULL, NULL, NULL);
rightIndex = Tk_PointToChar(textLayout, leftX + textarea.width, 0);
entryPtr->entry.layoutX = textarea.x - leftX;
}
TtkScrolled(entryPtr->entry.xscrollHandle,
leftIndex, rightIndex, entryPtr->entry.numChars);
}
/* EntryGetGC -- Helper routine.
* Get a GC using the specified foreground color and the entry's font.
* Result must be freed with Tk_FreeGC().
*/
static GC EntryGetGC(Entry *entryPtr, Tcl_Obj *colorObj)
{
Tk_Window tkwin = entryPtr->core.tkwin;
Tk_Font font = Tk_GetFontFromObj(tkwin, entryPtr->entry.fontObj);
XColor *colorPtr;
unsigned long mask = 0ul;
XGCValues gcValues;
gcValues.line_width = 1; mask |= GCLineWidth;
gcValues.font = Tk_FontId(font); mask |= GCFont;
if (colorObj != 0 && (colorPtr=Tk_GetColorFromObj(tkwin,colorObj)) != 0) {
gcValues.foreground = colorPtr->pixel;
mask |= GCForeground;
}
return Tk_GetGC(entryPtr->core.tkwin, mask, &gcValues);
}
/* EntryDisplay --
* Redraws the contents of an entry window.
*/
static void EntryDisplay(void *clientData, Drawable d)
{
Entry *entryPtr = clientData;
Tk_Window tkwin = entryPtr->core.tkwin;
int leftIndex = entryPtr->entry.xscroll.first,
rightIndex = entryPtr->entry.xscroll.last,
selFirst = entryPtr->entry.selectFirst,
selLast = entryPtr->entry.selectLast;
EntryStyleData es;
GC gc;
int showSelection, showCursor;
EntryInitStyleData(entryPtr, &es);
showCursor =
(entryPtr->core.flags & CURSOR_ON) != 0
&& EntryEditable(entryPtr)
&& entryPtr->entry.insertPos >= leftIndex
&& entryPtr->entry.insertPos <= rightIndex
;
showSelection =
(entryPtr->core.state & TTK_STATE_DISABLED) == 0
&& selFirst > -1
&& selLast > leftIndex
&& selFirst <= rightIndex
;
/* Adjust selection range to keep in display bounds.
*/
if (showSelection) {
if (selFirst < leftIndex)
selFirst = leftIndex;
if (selLast > rightIndex)
selLast = rightIndex;
}
/* Draw widget background & border
*/
Ttk_DrawLayout(entryPtr->core.layout, entryPtr->core.state, d);
/* Draw selection background
*/
if (showSelection && es.selBorderObj) {
Tk_3DBorder selBorder = Tk_Get3DBorderFromObj(tkwin, es.selBorderObj);
int selStartX = EntryCharPosition(entryPtr, selFirst);
int selEndX = EntryCharPosition(entryPtr, selLast);
int borderWidth = 1;
Tcl_GetIntFromObj(NULL, es.selBorderWidthObj, &borderWidth);
if (selBorder) {
Tk_Fill3DRectangle(tkwin, d, selBorder,
selStartX - borderWidth, entryPtr->entry.layoutY - borderWidth,
selEndX - selStartX + 2*borderWidth,
entryPtr->entry.layoutHeight + 2*borderWidth,
borderWidth, TK_RELIEF_RAISED);
}
}
/* Draw cursor:
*/
if (showCursor) {
int cursorX = EntryCharPosition(entryPtr, entryPtr->entry.insertPos),
cursorY = entryPtr->entry.layoutY,
cursorHeight = entryPtr->entry.layoutHeight,
cursorWidth = 1;
Tcl_GetIntFromObj(NULL,es.insertWidthObj,&cursorWidth);
if (cursorWidth <= 0) {
cursorWidth = 1;
}
/* @@@ should: maybe: SetCaretPos even when blinked off */
Tk_SetCaretPos(tkwin, cursorX, cursorY, cursorHeight);
gc = EntryGetGC(entryPtr, es.insertColorObj);
XFillRectangle(Tk_Display(tkwin), d, gc,
cursorX-cursorWidth/2, cursorY, cursorWidth, cursorHeight);
Tk_FreeGC(Tk_Display(tkwin), gc);
}
/* Draw the text:
*/
gc = EntryGetGC(entryPtr, es.foregroundObj);
Tk_DrawTextLayout(
Tk_Display(tkwin), d, gc, entryPtr->entry.textLayout,
entryPtr->entry.layoutX, entryPtr->entry.layoutY,
leftIndex, rightIndex);
Tk_FreeGC(Tk_Display(tkwin), gc);
/* Overwrite the selected portion (if any) in the -selectforeground color:
*/
if (showSelection) {
gc = EntryGetGC(entryPtr, es.selForegroundObj);
Tk_DrawTextLayout(
Tk_Display(tkwin), d, gc, entryPtr->entry.textLayout,
entryPtr->entry.layoutX, entryPtr->entry.layoutY,
selFirst, selLast);
Tk_FreeGC(Tk_Display(tkwin), gc);
}
}
/*------------------------------------------------------------------------
* +++ Widget commands.
*/
/* EntryIndex --
* Parse an index into an entry and return either its value
* or an error.
*
* Results:
* A standard Tcl result. If all went well, then *indexPtr is
* filled in with the character index (into entryPtr) corresponding to
* string. The index value is guaranteed to lie between 0 and
* the number of characters in the string, inclusive. If an
* error occurs then an error message is left in the interp's result.
*/
static int
EntryIndex(
Tcl_Interp *interp, /* For error messages. */
Entry *entryPtr, /* Entry widget to query */
Tcl_Obj *indexObj, /* Symbolic index name */
int *indexPtr) /* Return value */
{
# define EntryWidth(e) (Tk_Width(entryPtr->core.tkwin)) /* Not Right */
int length;
const char *string = Tcl_GetStringFromObj(indexObj, &length);
if (strncmp(string, "end", length) == 0) {
*indexPtr = entryPtr->entry.numChars;
} else if (strncmp(string, "insert", length) == 0) {
*indexPtr = entryPtr->entry.insertPos;
} else if (strncmp(string, "left", length) == 0) { /* for debugging */
*indexPtr = entryPtr->entry.xscroll.first;
} else if (strncmp(string, "right", length) == 0) { /* for debugging */
*indexPtr = entryPtr->entry.xscroll.last;
} else if (strncmp(string, "sel.", 4) == 0) {
if (entryPtr->entry.selectFirst < 0) {
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "selection isn't in widget ",
Tk_PathName(entryPtr->core.tkwin), NULL);
return TCL_ERROR;
}
if (strncmp(string, "sel.first", length) == 0) {
*indexPtr = entryPtr->entry.selectFirst;
} else if (strncmp(string, "sel.last", length) == 0) {
*indexPtr = entryPtr->entry.selectLast;
} else {
goto badIndex;
}
} else if (string[0] == '@') {
int roundUp = 0;
int maxWidth = EntryWidth(entryPtr);
int x;
if (Tcl_GetInt(interp, string + 1, &x) != TCL_OK) {
goto badIndex;
}
if (x > maxWidth) {
x = maxWidth;
roundUp = 1;
}
*indexPtr = Tk_PointToChar(entryPtr->entry.textLayout,
x - entryPtr->entry.layoutX, 0);
if (*indexPtr < entryPtr->entry.xscroll.first) {
*indexPtr = entryPtr->entry.xscroll.first;
}
/*
* Special trick: if the x-position was off-screen to the right,
* round the index up to refer to the character just after the
* last visible one on the screen. This is needed to enable the
* last character to be selected, for example.
*/
if (roundUp && (*indexPtr < entryPtr->entry.numChars)) {
*indexPtr += 1;
}
} else {
if (Tcl_GetInt(interp, string, indexPtr) != TCL_OK) {
goto badIndex;
}
if (*indexPtr < 0) {
*indexPtr = 0;
} else if (*indexPtr > entryPtr->entry.numChars) {
*indexPtr = entryPtr->entry.numChars;
}
}
return TCL_OK;
badIndex:
Tcl_ResetResult(interp);
Tcl_AppendResult(interp, "bad entry index \"", string, "\"", NULL);
return TCL_ERROR;
}
/* $entry bbox $index --
* Return the bounding box of the character at the specified index.
*/
static int
EntryBBoxCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
Ttk_Box b;
int index;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "index");
return TCL_ERROR;
}
if (EntryIndex(interp, entryPtr, objv[2], &index) != TCL_OK) {
return TCL_ERROR;
}
if ((index == entryPtr->entry.numChars) && (index > 0)) {
index--;
}
Tk_CharBbox(entryPtr->entry.textLayout, index,
&b.x, &b.y, &b.width, &b.height);
b.x += entryPtr->entry.layoutX;
b.y += entryPtr->entry.layoutY;
Tcl_SetObjResult(interp, Ttk_NewBoxObj(b));
return TCL_OK;
}
/* $entry delete $from ?$to? --
* Delete the characters in the range [$from,$to).
* $to defaults to $from+1 if not specified.
*/
static int
EntryDeleteCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
int first, last;
if ((objc < 3) || (objc > 4)) {
Tcl_WrongNumArgs(interp, 2, objv, "firstIndex ?lastIndex?");
return TCL_ERROR;
}
if (EntryIndex(interp, entryPtr, objv[2], &first) != TCL_OK) {
return TCL_ERROR;
}
if (objc == 3) {
last = first + 1;
} else if (EntryIndex(interp, entryPtr, objv[3], &last) != TCL_OK) {
return TCL_ERROR;
}
if (last >= first && EntryEditable(entryPtr)) {
return DeleteChars(entryPtr, first, last - first);
}
return TCL_OK;
}
/* $entry get --
* Return the current value of the entry widget.
*/
static int
EntryGetCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_SetResult(interp, entryPtr->entry.string, TCL_VOLATILE);
return TCL_OK;
}
/* $entry icursor $index --
* Set the insert cursor position.
*/
static int
EntryICursorCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "pos");
return TCL_ERROR;
}
if (EntryIndex(interp, entryPtr, objv[2],
&entryPtr->entry.insertPos) != TCL_OK) {
return TCL_ERROR;
}
TtkRedisplayWidget(&entryPtr->core);
return TCL_OK;
}
/* $entry index $index --
* Return numeric value (0..numChars) of the specified index.
*/
static int
EntryIndexCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
int index;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "string");
return TCL_ERROR;
}
if (EntryIndex(interp, entryPtr, objv[2], &index) != TCL_OK) {
return TCL_ERROR;
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(index));
return TCL_OK;
}
/* $entry insert $index $text --
* Insert $text after position $index.
* Silent no-op if the entry is disabled or read-only.
*/
static int
EntryInsertCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
int index;
if (objc != 4) {
Tcl_WrongNumArgs(interp, 2, objv, "index text");
return TCL_ERROR;
}
if (EntryIndex(interp, entryPtr, objv[2], &index) != TCL_OK) {
return TCL_ERROR;
}
if (EntryEditable(entryPtr)) {
return InsertChars(entryPtr, index, Tcl_GetString(objv[3]));
}
return TCL_OK;
}
/* $entry selection clear --
* Clear selection.
*/
static int EntrySelectionClearCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 3, objv, NULL);
return TCL_ERROR;
}
entryPtr->entry.selectFirst = entryPtr->entry.selectLast = -1;
TtkRedisplayWidget(&entryPtr->core);
return TCL_OK;
}
/* $entry selection present --
* Returns 1 if any characters are selected, 0 otherwise.
*/
static int EntrySelectionPresentCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 3, objv, NULL);
return TCL_ERROR;
}
Tcl_SetObjResult(interp,
Tcl_NewBooleanObj(entryPtr->entry.selectFirst >= 0));
return TCL_OK;
}
/* $entry selection range $start $end --
* Explicitly set the selection range.
*/
static int EntrySelectionRangeCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
int start, end;
if (objc != 5) {
Tcl_WrongNumArgs(interp, 3, objv, "start end");
return TCL_ERROR;
}
if ( EntryIndex(interp, entryPtr, objv[3], &start) != TCL_OK
|| EntryIndex(interp, entryPtr, objv[4], &end) != TCL_OK) {
return TCL_ERROR;
}
if (entryPtr->core.state & TTK_STATE_DISABLED) {
return TCL_OK;
}
if (start >= end) {
entryPtr->entry.selectFirst = entryPtr->entry.selectLast = -1;
} else {
entryPtr->entry.selectFirst = start;
entryPtr->entry.selectLast = end;
EntryOwnSelection(entryPtr);
}
TtkRedisplayWidget(&entryPtr->core);
return TCL_OK;
}
static const Ttk_Ensemble EntrySelectionCommands[] = {
{ "clear", EntrySelectionClearCommand,0 },
{ "present", EntrySelectionPresentCommand,0 },
{ "range", EntrySelectionRangeCommand,0 },
{ 0,0,0 }
};
/* $entry set $value
* Sets the value of an entry widget.
*/
static int EntrySetCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "value");
return TCL_ERROR;
}
EntrySetValue(entryPtr, Tcl_GetString(objv[2]));
return TCL_OK;
}
/* $entry validate --
* Trigger forced validation. Returns 1/0 if validation succeeds/fails
* or error status from -validatecommand / -invalidcommand.
*/
static int EntryValidateCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
int code;
if (objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return TCL_ERROR;
}
code = EntryRevalidate(interp, entryPtr, VALIDATE_FORCED);
if (code == TCL_ERROR)
return code;
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(code == TCL_OK));
return TCL_OK;
}
/* $entry xview -- horizontal scrolling interface
*/
static int EntryXViewCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Entry *entryPtr = recordPtr;
return TtkScrollviewCommand(interp, objc, objv, entryPtr->entry.xscrollHandle);
}
static const Ttk_Ensemble EntryCommands[] = {
{ "bbox", EntryBBoxCommand,0 },
{ "cget", TtkWidgetCgetCommand,0 },
{ "configure", TtkWidgetConfigureCommand,0 },
{ "delete", EntryDeleteCommand,0 },
{ "get", EntryGetCommand,0 },
{ "icursor", EntryICursorCommand,0 },
{ "identify", TtkWidgetIdentifyCommand,0 },
{ "index", EntryIndexCommand,0 },
{ "insert", EntryInsertCommand,0 },
{ "instate", TtkWidgetInstateCommand,0 },
{ "selection", 0,EntrySelectionCommands },
{ "state", TtkWidgetStateCommand,0 },
{ "validate", EntryValidateCommand,0 },
{ "xview", EntryXViewCommand,0 },
{ 0,0,0 }
};
/*------------------------------------------------------------------------
* +++ Entry widget definition.
*/
static WidgetSpec EntryWidgetSpec = {
"TEntry", /* className */
sizeof(Entry), /* recordSize */
EntryOptionSpecs, /* optionSpecs */
EntryCommands, /* subcommands */
EntryInitialize, /* initializeProc */
EntryCleanup, /* cleanupProc */
EntryConfigure, /* configureProc */
EntryPostConfigure, /* postConfigureProc */
TtkWidgetGetLayout, /* getLayoutProc */
TtkWidgetSize, /* sizeProc */
EntryDoLayout, /* layoutProc */
EntryDisplay /* displayProc */
};
/*------------------------------------------------------------------------
* +++ Combobox widget record.
*/
typedef struct {
Tcl_Obj *postCommandObj;
Tcl_Obj *valuesObj;
Tcl_Obj *heightObj;
int currentIndex;
} ComboboxPart;
typedef struct {
WidgetCore core;
EntryPart entry;
ComboboxPart combobox;
} Combobox;
static Tk_OptionSpec ComboboxOptionSpecs[] = {
{TK_OPTION_STRING, "-height", "height", "Height",
DEF_LIST_HEIGHT, Tk_Offset(Combobox, combobox.heightObj), -1,
0,0,0 },
{TK_OPTION_STRING, "-postcommand", "postCommand", "PostCommand",
"", Tk_Offset(Combobox, combobox.postCommandObj), -1,
0,0,0 },
{TK_OPTION_STRING, "-values", "values", "Values",
"", Tk_Offset(Combobox, combobox.valuesObj), -1,
0,0,0 },
WIDGET_INHERIT_OPTIONS(EntryOptionSpecs)
};
/* ComboboxInitialize --
* Initialization hook for combobox widgets.
*/
static void
ComboboxInitialize(Tcl_Interp *interp, void *recordPtr)
{
Combobox *cb = recordPtr;
cb->combobox.currentIndex = -1;
TtkTrackElementState(&cb->core);
EntryInitialize(interp, recordPtr);
}
/* ComboboxConfigure --
* Configuration hook for combobox widgets.
*/
static int
ComboboxConfigure(Tcl_Interp *interp, void *recordPtr, int mask)
{
Combobox *cbPtr = recordPtr;
int unused;
/* Make sure -values is a valid list:
*/
if (Tcl_ListObjLength(interp,cbPtr->combobox.valuesObj,&unused) != TCL_OK)
return TCL_ERROR;
return EntryConfigure(interp, recordPtr, mask);
}
/* $cb current ?newIndex? -- get or set current index.
* Setting the current index updates the combobox value,
* but the value and -values may be changed independently
* of the index. Instead of trying to keep currentIndex
* in sync at all times, [$cb current] double-checks
*/
static int ComboboxCurrentCommand(
void *recordPtr, Tcl_Interp *interp, int objc, Tcl_Obj *const objv[])
{
Combobox *cbPtr = recordPtr;
int currentIndex = cbPtr->combobox.currentIndex;
const char *currentValue = cbPtr->entry.string;
int nValues;
Tcl_Obj **values;
Tcl_ListObjGetElements(interp,cbPtr->combobox.valuesObj,&nValues,&values);
if (objc == 2) {
/* Check if currentIndex still valid:
*/
if ( currentIndex < 0
|| currentIndex >= nValues
|| strcmp(currentValue,Tcl_GetString(values[currentIndex]))
)
{
/* Not valid. Check current value against each element in -values:
*/
for (currentIndex = 0; currentIndex < nValues; ++currentIndex) {
if (!strcmp(currentValue,Tcl_GetString(values[currentIndex]))) {
break;
}
}
if (currentIndex >= nValues) {
/* Not found */
currentIndex = -1;
}
}
cbPtr->combobox.currentIndex = currentIndex;
Tcl_SetObjResult(interp, Tcl_NewIntObj(currentIndex));
return TCL_OK;
} else if (objc == 3) {
if (Tcl_GetIntFromObj(interp, objv[2], ¤tIndex) != TCL_OK) {
return TCL_ERROR;
}
if (currentIndex < 0 || currentIndex >= nValues) {
Tcl_AppendResult(interp,
"Index ", Tcl_GetString(objv[2]), " out of range",
NULL);
return TCL_ERROR;
}
cbPtr->combobox.currentIndex = currentIndex;
return EntrySetValue(recordPtr, Tcl_GetString(values[currentIndex]));
} else {
Tcl_WrongNumArgs(interp, 2, objv, "?newIndex?");
return TCL_ERROR;
}
return TCL_OK;
}
/*------------------------------------------------------------------------
* +++ Combobox widget definition.
*/
static const Ttk_Ensemble ComboboxCommands[] = {
{ "bbox", EntryBBoxCommand,0 },
{ "cget", TtkWidgetCgetCommand,0 },
{ "configure", TtkWidgetConfigureCommand,0 },
{ "current", ComboboxCurrentCommand,0 },
{ "delete", EntryDeleteCommand,0 },
{ "get", EntryGetCommand,0 },
{ "icursor", EntryICursorCommand,0 },
{ "identify", TtkWidgetIdentifyCommand,0 },
{ "index", EntryIndexCommand,0 },
{ "insert", EntryInsertCommand,0 },
{ "instate", TtkWidgetInstateCommand,0 },
{ "selection", 0,EntrySelectionCommands },
{ "state", TtkWidgetStateCommand,0 },
{ "set", EntrySetCommand,0 },
{ "validate", EntryValidateCommand,0 },
{ "xview", EntryXViewCommand,0 },
{ 0,0,0 }
};
static WidgetSpec ComboboxWidgetSpec = {
"TCombobox", /* className */
sizeof(Combobox), /* recordSize */
ComboboxOptionSpecs, /* optionSpecs */
ComboboxCommands, /* subcommands */
ComboboxInitialize, /* initializeProc */
EntryCleanup, /* cleanupProc */
ComboboxConfigure, /* configureProc */
EntryPostConfigure, /* postConfigureProc */
TtkWidgetGetLayout, /* getLayoutProc */
TtkWidgetSize, /* sizeProc */
EntryDoLayout, /* layoutProc */
EntryDisplay /* displayProc */
};
/*------------------------------------------------------------------------
* +++ Spinbox widget.
*/
typedef struct {
Tcl_Obj *valuesObj;
Tcl_Obj *fromObj;
Tcl_Obj *toObj;
Tcl_Obj *incrementObj;
Tcl_Obj *formatObj;
Tcl_Obj *wrapObj;
Tcl_Obj *commandObj;
} SpinboxPart;
typedef struct {
WidgetCore core;
EntryPart entry;
SpinboxPart spinbox;
} Spinbox;
static Tk_OptionSpec SpinboxOptionSpecs[] = {
{TK_OPTION_STRING, "-values", "values", "Values",
"", Tk_Offset(Spinbox, spinbox.valuesObj), -1,
0,0,0 },
{TK_OPTION_DOUBLE, "-from", "from", "From",
"0", Tk_Offset(Spinbox,spinbox.fromObj), -1,
0,0,0 },
{TK_OPTION_DOUBLE, "-to", "to", "To",
"0", Tk_Offset(Spinbox,spinbox.toObj), -1,
0,0,0 },
{TK_OPTION_DOUBLE, "-increment", "increment", "Increment",
"1", Tk_Offset(Spinbox,spinbox.incrementObj), -1,
0,0,0 },
{TK_OPTION_STRING, "-format", "format", "Format",
"", Tk_Offset(Spinbox, spinbox.formatObj), -1,
0,0,0 },
{TK_OPTION_STRING, "-command", "command", "Command",
"", Tk_Offset(Spinbox, spinbox.commandObj), -1,
0,0,0 },
{TK_OPTION_BOOLEAN, "-wrap", "wrap", "Wrap",
"0", Tk_Offset(Spinbox,spinbox.wrapObj), -1,
0,0,0 },
WIDGET_INHERIT_OPTIONS(EntryOptionSpecs)
};
/* SpinboxInitialize --
* Initialization hook for spinbox widgets.
*/
static void
SpinboxInitialize(Tcl_Interp *interp, void *recordPtr)
{
Spinbox *sb = recordPtr;
TtkTrackElementState(&sb->core);
EntryInitialize(interp, recordPtr);
}
/* SpinboxConfigure --
* Configuration hook for spinbox widgets.
*/
static int
SpinboxConfigure(Tcl_Interp *interp, void *recordPtr, int mask)
{
Spinbox *sb = recordPtr;
int unused;
/* Make sure -values is a valid list:
*/
if (Tcl_ListObjLength(interp,sb->spinbox.valuesObj,&unused) != TCL_OK)
return TCL_ERROR;
return EntryConfigure(interp, recordPtr, mask);
}
static const Ttk_Ensemble SpinboxCommands[] = {
{ "bbox", EntryBBoxCommand,0 },
{ "cget", TtkWidgetCgetCommand,0 },
{ "configure", TtkWidgetConfigureCommand,0 },
{ "delete", EntryDeleteCommand,0 },
{ "get", EntryGetCommand,0 },
{ "icursor", EntryICursorCommand,0 },
{ "identify", TtkWidgetIdentifyCommand,0 },
{ "index", EntryIndexCommand,0 },
{ "insert", EntryInsertCommand,0 },
{ "instate", TtkWidgetInstateCommand,0 },
{ "selection", 0,EntrySelectionCommands },
{ "state", TtkWidgetStateCommand,0 },
{ "set", EntrySetCommand,0 },
{ "validate", EntryValidateCommand,0 },
{ "xview", EntryXViewCommand,0 },
{ 0,0,0 }
};
static WidgetSpec SpinboxWidgetSpec = {
"TSpinbox", /* className */
sizeof(Spinbox), /* recordSize */
SpinboxOptionSpecs, /* optionSpecs */
SpinboxCommands, /* subcommands */
SpinboxInitialize, /* initializeProc */
EntryCleanup, /* cleanupProc */
SpinboxConfigure, /* configureProc */
EntryPostConfigure, /* postConfigureProc */
TtkWidgetGetLayout, /* getLayoutProc */
TtkWidgetSize, /* sizeProc */
EntryDoLayout, /* layoutProc */
EntryDisplay /* displayProc */
};
/*------------------------------------------------------------------------
* +++ Textarea element.
*
* Text display area for Entry widgets.
* Just computes requested size; display is handled by the widget itself.
*/
typedef struct {
Tcl_Obj *fontObj;
Tcl_Obj *widthObj;
} TextareaElement;
static Ttk_ElementOptionSpec TextareaElementOptions[] = {
{ "-font", TK_OPTION_FONT,
Tk_Offset(TextareaElement,fontObj), DEF_ENTRY_FONT },
{ "-width", TK_OPTION_INT,
Tk_Offset(TextareaElement,widthObj), "20" },
{ NULL, 0, 0, NULL }
};
static void TextareaElementSize(
void *clientData, void *elementRecord, Tk_Window tkwin,
int *widthPtr, int *heightPtr, Ttk_Padding *paddingPtr)
{
TextareaElement *textarea = elementRecord;
Tk_Font font = Tk_GetFontFromObj(tkwin, textarea->fontObj);
int avgWidth = Tk_TextWidth(font, "0", 1);
Tk_FontMetrics fm;
int prefWidth = 1;
Tk_GetFontMetrics(font, &fm);
Tcl_GetIntFromObj(NULL, textarea->widthObj, &prefWidth);
if (prefWidth <= 0)
prefWidth = 1;
*heightPtr = fm.linespace;
*widthPtr = prefWidth * avgWidth;
}
static Ttk_ElementSpec TextareaElementSpec = {
TK_STYLE_VERSION_2,
sizeof(TextareaElement),
TextareaElementOptions,
TextareaElementSize,
TtkNullElementDraw
};
/*------------------------------------------------------------------------
* +++ Widget layouts.
*/
TTK_BEGIN_LAYOUT(EntryLayout)
TTK_GROUP("Entry.field", TTK_FILL_BOTH|TTK_BORDER,
TTK_GROUP("Entry.padding", TTK_FILL_BOTH,
TTK_NODE("Entry.textarea", TTK_FILL_BOTH)))
TTK_END_LAYOUT
TTK_BEGIN_LAYOUT(ComboboxLayout)
TTK_GROUP("Combobox.field", TTK_FILL_BOTH,
TTK_NODE("Combobox.downarrow", TTK_PACK_RIGHT|TTK_FILL_Y)
TTK_GROUP("Combobox.padding", TTK_FILL_BOTH|TTK_PACK_LEFT|TTK_EXPAND,
TTK_NODE("Combobox.textarea", TTK_FILL_BOTH)))
TTK_END_LAYOUT
TTK_BEGIN_LAYOUT(SpinboxLayout)
TTK_GROUP("Spinbox.field", TTK_PACK_TOP|TTK_FILL_X,
TTK_GROUP("null", TTK_PACK_RIGHT,
TTK_NODE("Spinbox.uparrow", TTK_PACK_TOP|TTK_STICK_E)
TTK_NODE("Spinbox.downarrow", TTK_PACK_BOTTOM|TTK_STICK_E))
TTK_GROUP("Spinbox.padding", TTK_FILL_BOTH,
TTK_NODE("Spinbox.textarea", TTK_FILL_BOTH)))
TTK_END_LAYOUT
/*------------------------------------------------------------------------
* +++ Initialization.
*/
MODULE_SCOPE
void TtkEntry_Init(Tcl_Interp *interp)
{
Ttk_Theme themePtr = Ttk_GetDefaultTheme(interp);
Ttk_RegisterElement(interp, themePtr, "textarea", &TextareaElementSpec, 0);
Ttk_RegisterLayout(themePtr, "TEntry", EntryLayout);
Ttk_RegisterLayout(themePtr, "TCombobox", ComboboxLayout);
Ttk_RegisterLayout(themePtr, "TSpinbox", SpinboxLayout);
RegisterWidget(interp, "ttk::entry", &EntryWidgetSpec);
RegisterWidget(interp, "ttk::combobox", &ComboboxWidgetSpec);
RegisterWidget(interp, "ttk::spinbox", &SpinboxWidgetSpec);
}
/*EOF*/
|