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
|
/*
* tkScale.c --
*
* This module implements a scale widgets for the Tk toolkit. A scale
* displays a slider that can be adjusted to change a value; it also
* displays numeric labels and a textual label, if desired.
*
* The modifications to use floating-point values are based on an
* implementation by Paul Mackerras. The -variable option is due to
* Henning Schulzrinne. All of these are used with permission.
*
* Copyright © 1990-1994 The Regents of the University of California.
* Copyright © 1994-1997 Sun Microsystems, Inc.
* Copyright © 1998-2000 Scriptics Corporation.
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkInt.h"
#include "tkScale.h"
#include "default.h"
#ifdef _WIN32
#include "tkWinInt.h"
#endif
/*
* The following table defines the legal values for the -orient option. It is
* used together with the "enum orient" declaration in tkScale.h.
*/
static const char *const orientStrings[] = {
"horizontal", "vertical", NULL
};
static const Tk_OptionSpec optionSpecs[] = {
{TK_OPTION_BORDER, "-activebackground", "activeBackground", "Foreground",
DEF_SCALE_ACTIVE_BG_COLOR, TCL_INDEX_NONE, offsetof(TkScale, activeBorder),
0, DEF_SCALE_ACTIVE_BG_MONO, 0},
{TK_OPTION_BORDER, "-background", "background", "Background",
DEF_SCALE_BG_COLOR, TCL_INDEX_NONE, offsetof(TkScale, bgBorder),
0, DEF_SCALE_BG_MONO, 0},
{TK_OPTION_DOUBLE, "-bigincrement", "bigIncrement", "BigIncrement",
DEF_SCALE_BIG_INCREMENT, TCL_INDEX_NONE, offsetof(TkScale, bigIncrement),
0, 0, 0},
{TK_OPTION_SYNONYM, "-bd", NULL, NULL,
NULL, 0, TCL_INDEX_NONE, 0, "-borderwidth", 0},
{TK_OPTION_SYNONYM, "-bg", NULL, NULL,
NULL, 0, TCL_INDEX_NONE, 0, "-background", 0},
{TK_OPTION_PIXELS, "-borderwidth", "borderWidth", "BorderWidth",
DEF_SCALE_BORDER_WIDTH, TCL_INDEX_NONE, offsetof(TkScale, borderWidth),
0, 0, 0},
{TK_OPTION_STRING, "-command", "command", "Command",
DEF_SCALE_COMMAND, TCL_INDEX_NONE, offsetof(TkScale, command),
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_CURSOR, "-cursor", "cursor", "Cursor",
DEF_SCALE_CURSOR, TCL_INDEX_NONE, offsetof(TkScale, cursor),
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_INT, "-digits", "digits", "Digits",
DEF_SCALE_DIGITS, TCL_INDEX_NONE, offsetof(TkScale, digits),
0, 0, 0},
{TK_OPTION_SYNONYM, "-fg", "foreground", NULL,
NULL, 0, TCL_INDEX_NONE, 0, "-foreground", 0},
{TK_OPTION_FONT, "-font", "font", "Font",
DEF_SCALE_FONT, TCL_INDEX_NONE, offsetof(TkScale, tkfont), 0, 0, 0},
{TK_OPTION_COLOR, "-foreground", "foreground", "Foreground",
DEF_SCALE_FG_COLOR, TCL_INDEX_NONE, offsetof(TkScale, textColorPtr), 0,
DEF_SCALE_FG_MONO, 0},
{TK_OPTION_DOUBLE, "-from", "from", "From", DEF_SCALE_FROM, TCL_INDEX_NONE,
offsetof(TkScale, fromValue), 0, 0, 0},
{TK_OPTION_BORDER, "-highlightbackground", "highlightBackground",
"HighlightBackground", DEF_SCALE_HIGHLIGHT_BG_COLOR,
TCL_INDEX_NONE, offsetof(TkScale, highlightBorder),
0, DEF_SCALE_HIGHLIGHT_BG_MONO, 0},
{TK_OPTION_COLOR, "-highlightcolor", "highlightColor", "HighlightColor",
DEF_SCALE_HIGHLIGHT, TCL_INDEX_NONE, offsetof(TkScale, highlightColorPtr),
0, 0, 0},
{TK_OPTION_PIXELS, "-highlightthickness", "highlightThickness",
"HighlightThickness", DEF_SCALE_HIGHLIGHT_WIDTH, TCL_INDEX_NONE,
offsetof(TkScale, highlightWidth), 0, 0, 0},
{TK_OPTION_STRING, "-label", "label", "Label",
DEF_SCALE_LABEL, TCL_INDEX_NONE, offsetof(TkScale, label),
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_PIXELS, "-length", "length", "Length",
DEF_SCALE_LENGTH, TCL_INDEX_NONE, offsetof(TkScale, length), 0, 0, 0},
{TK_OPTION_STRING_TABLE, "-orient", "orient", "Orient",
DEF_SCALE_ORIENT, TCL_INDEX_NONE, offsetof(TkScale, orient),
TK_OPTION_ENUM_VAR, orientStrings, 0},
{TK_OPTION_RELIEF, "-relief", "relief", "Relief",
DEF_SCALE_RELIEF, TCL_INDEX_NONE, offsetof(TkScale, relief), 0, 0, 0},
{TK_OPTION_INT, "-repeatdelay", "repeatDelay", "RepeatDelay",
DEF_SCALE_REPEAT_DELAY, TCL_INDEX_NONE, offsetof(TkScale, repeatDelay),
0, 0, 0},
{TK_OPTION_INT, "-repeatinterval", "repeatInterval", "RepeatInterval",
DEF_SCALE_REPEAT_INTERVAL, TCL_INDEX_NONE, offsetof(TkScale, repeatInterval),
0, 0, 0},
{TK_OPTION_DOUBLE, "-resolution", "resolution", "Resolution",
DEF_SCALE_RESOLUTION, TCL_INDEX_NONE, offsetof(TkScale, resolution),
0, 0, 0},
{TK_OPTION_BOOLEAN, "-showvalue", "showValue", "ShowValue",
DEF_SCALE_SHOW_VALUE, TCL_INDEX_NONE, offsetof(TkScale, showValue),
0, 0, 0},
{TK_OPTION_PIXELS, "-sliderlength", "sliderLength", "SliderLength",
DEF_SCALE_SLIDER_LENGTH, TCL_INDEX_NONE, offsetof(TkScale, sliderLength),
0, 0, 0},
{TK_OPTION_RELIEF, "-sliderrelief", "sliderRelief", "SliderRelief",
DEF_SCALE_SLIDER_RELIEF, TCL_INDEX_NONE, offsetof(TkScale, sliderRelief),
0, 0, 0},
{TK_OPTION_STRING_TABLE, "-state", "state", "State",
DEF_SCALE_STATE, TCL_INDEX_NONE, offsetof(TkScale, state),
TK_OPTION_ENUM_VAR, tkStateStrings, 0},
{TK_OPTION_STRING, "-takefocus", "takeFocus", "TakeFocus",
DEF_SCALE_TAKE_FOCUS, offsetof(TkScale, takeFocusPtr), TCL_INDEX_NONE,
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_DOUBLE, "-tickinterval", "tickInterval", "TickInterval",
DEF_SCALE_TICK_INTERVAL, TCL_INDEX_NONE, offsetof(TkScale, tickInterval),
0, 0, 0},
{TK_OPTION_DOUBLE, "-to", "to", "To",
DEF_SCALE_TO, TCL_INDEX_NONE, offsetof(TkScale, toValue), 0, 0, 0},
{TK_OPTION_COLOR, "-troughcolor", "troughColor", "Background",
DEF_SCALE_TROUGH_COLOR, TCL_INDEX_NONE, offsetof(TkScale, troughColorPtr),
0, DEF_SCALE_TROUGH_MONO, 0},
{TK_OPTION_STRING, "-variable", "variable", "Variable",
DEF_SCALE_VARIABLE, offsetof(TkScale, varNamePtr), TCL_INDEX_NONE,
TK_OPTION_NULL_OK, 0, 0},
{TK_OPTION_PIXELS, "-width", "width", "Width",
DEF_SCALE_WIDTH, TCL_INDEX_NONE, offsetof(TkScale, width), 0, 0, 0},
{TK_OPTION_END, NULL, NULL, NULL, NULL, 0, TCL_INDEX_NONE, 0, 0, 0}
};
/*
* The following tables define the scale widget commands and map the indexes
* into the string tables into a single enumerated type used to dispatch the
* scale widget command.
*/
static const char *const commandNames[] = {
"cget", "configure", "coords", "get", "identify", "set", NULL
};
enum command {
COMMAND_CGET, COMMAND_CONFIGURE, COMMAND_COORDS, COMMAND_GET,
COMMAND_IDENTIFY, COMMAND_SET
};
/*
* Forward declarations for procedures defined later in this file:
*/
static void ComputeFormat(TkScale *scalePtr, int forTicks);
static void ComputeScaleGeometry(TkScale *scalePtr);
static int ConfigureScale(Tcl_Interp *interp, TkScale *scalePtr,
int objc, Tcl_Obj *const objv[]);
static void DestroyScale(void *memPtr);
static double MaxTickRoundingError(TkScale *scalePtr,
double tickResolution);
static void ScaleCmdDeletedProc(void *clientData);
static void ScaleEventProc(void *clientData,
XEvent *eventPtr);
static char * ScaleVarProc(void *clientData,
Tcl_Interp *interp, const char *name1,
const char *name2, int flags);
static int ScaleWidgetObjCmd(void *clientData,
Tcl_Interp *interp, int objc,
Tcl_Obj *const objv[]);
static void ScaleWorldChanged(void *instanceData);
static void ScaleSetVariable(TkScale *scalePtr);
/*
* The structure below defines scale class behavior by means of procedures
* that can be invoked from generic window code.
*/
static const Tk_ClassProcs scaleClass = {
sizeof(Tk_ClassProcs), /* size */
ScaleWorldChanged, /* worldChangedProc */
NULL, /* createProc */
NULL /* modalProc */
};
/*
*--------------------------------------------------------------
*
* ScaleDigit, ScaleMax, ScaleMin, ScaleRound --
*
* Simple math helper functions, designed to be automatically inlined by
* the compiler most of the time.
*
*--------------------------------------------------------------
*/
static inline int
ScaleDigit(
double value)
{
return (int) floor(log10(fabs(value)));
}
static inline double
ScaleMax(
double a,
double b)
{
return (a > b) ? a : b;
}
static inline double
ScaleMin(
double a,
double b)
{
return (a < b) ? a : b;
}
static inline int
ScaleRound(
double value)
{
return (int) floor(value + 0.5);
}
/*
*--------------------------------------------------------------
*
* Tk_ScaleObjCmd --
*
* This procedure is invoked to process the "scale" Tcl command. See the
* user documentation for details on what it does.
*
* Results:
* A standard Tcl result.
*
* Side effects:
* See the user documentation.
*
*--------------------------------------------------------------
*/
int
Tk_ScaleObjCmd(
TCL_UNUSED(void *),
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument values. */
{
TkScale *scalePtr;
Tk_OptionTable optionTable;
Tk_Window tkwin;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "pathName ?-option value ...?");
return TCL_ERROR;
}
tkwin = Tk_CreateWindowFromPath(interp, Tk_MainWindow(interp),
Tcl_GetString(objv[1]), NULL);
if (tkwin == NULL) {
return TCL_ERROR;
}
/*
* Create the option table for this widget class. If it has already been
* created, the cached pointer will be returned.
*/
optionTable = Tk_CreateOptionTable(interp, optionSpecs);
Tk_SetClass(tkwin, "Scale");
scalePtr = TkpCreateScale(tkwin);
/*
* Initialize fields that won't be initialized by ConfigureScale, or which
* ConfigureScale expects to have reasonable values (e.g. resource
* pointers).
*/
scalePtr->tkwin = tkwin;
scalePtr->display = Tk_Display(tkwin);
scalePtr->interp = interp;
scalePtr->widgetCmd = Tcl_CreateObjCommand(interp,
Tk_PathName(scalePtr->tkwin), ScaleWidgetObjCmd,
scalePtr, ScaleCmdDeletedProc);
scalePtr->optionTable = optionTable;
scalePtr->orient = ORIENT_VERTICAL;
scalePtr->width = 0;
scalePtr->length = 0;
scalePtr->value = 0.0;
scalePtr->varNamePtr = NULL;
scalePtr->fromValue = 0.0;
scalePtr->toValue = 0.0;
scalePtr->tickInterval = 0.0;
scalePtr->resolution = 1.0;
scalePtr->digits = 0;
scalePtr->bigIncrement = 0.0;
scalePtr->command = NULL;
scalePtr->repeatDelay = 0;
scalePtr->repeatInterval = 0;
scalePtr->label = NULL;
scalePtr->labelLength = 0;
scalePtr->state = STATE_NORMAL;
scalePtr->borderWidth = 0;
scalePtr->bgBorder = NULL;
scalePtr->activeBorder = NULL;
scalePtr->sliderRelief = TK_RELIEF_RAISED;
scalePtr->troughColorPtr = NULL;
scalePtr->troughGC = NULL;
scalePtr->copyGC = NULL;
scalePtr->tkfont = NULL;
scalePtr->textColorPtr = NULL;
scalePtr->textGC = NULL;
scalePtr->relief = TK_RELIEF_FLAT;
scalePtr->highlightWidth = 0;
scalePtr->highlightBorder = NULL;
scalePtr->highlightColorPtr = NULL;
scalePtr->inset = 0;
scalePtr->sliderLength = 0;
scalePtr->showValue = 0;
scalePtr->horizLabelY = 0;
scalePtr->horizValueY = 0;
scalePtr->horizTroughY = 0;
scalePtr->horizTickY = 0;
scalePtr->vertTickRightX = 0;
scalePtr->vertValueRightX = 0;
scalePtr->vertTroughX = 0;
scalePtr->vertLabelX = 0;
scalePtr->fontHeight = 0;
scalePtr->cursor = NULL;
scalePtr->takeFocusPtr = NULL;
scalePtr->flags = NEVER_SET;
Tk_SetClassProcs(scalePtr->tkwin, &scaleClass, scalePtr);
Tk_CreateEventHandler(scalePtr->tkwin,
ExposureMask|StructureNotifyMask|FocusChangeMask,
ScaleEventProc, scalePtr);
if ((Tk_InitOptions(interp, scalePtr, optionTable, tkwin)
!= TCL_OK) ||
(ConfigureScale(interp, scalePtr, objc - 2, objv + 2) != TCL_OK)) {
Tk_DestroyWindow(scalePtr->tkwin);
return TCL_ERROR;
}
/*
* The widget was just created, no command callback must be invoked.
*/
scalePtr->flags &= ~INVOKE_COMMAND;
Tcl_SetObjResult(interp, Tk_NewWindowObj(scalePtr->tkwin));
return TCL_OK;
}
/*
*--------------------------------------------------------------
*
* ScaleWidgetObjCmd --
*
* 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
ScaleWidgetObjCmd(
void *clientData, /* Information about scale widget. */
Tcl_Interp *interp, /* Current interpreter. */
int objc, /* Number of arguments. */
Tcl_Obj *const objv[]) /* Argument strings. */
{
TkScale *scalePtr = (TkScale *)clientData;
Tcl_Obj *objPtr;
int index, result;
if (objc < 2) {
Tcl_WrongNumArgs(interp, 1, objv, "option ?arg ...?");
return TCL_ERROR;
}
result = Tcl_GetIndexFromObjStruct(interp, objv[1], commandNames,
sizeof(char *), "option", 0, &index);
if (result != TCL_OK) {
return result;
}
Tcl_Preserve(scalePtr);
switch (index) {
case COMMAND_CGET:
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "cget option");
goto error;
}
objPtr = Tk_GetOptionValue(interp, scalePtr,
scalePtr->optionTable, objv[2], scalePtr->tkwin);
if (objPtr == NULL) {
goto error;
}
Tcl_SetObjResult(interp, objPtr);
break;
case COMMAND_CONFIGURE:
if (objc <= 3) {
objPtr = Tk_GetOptionInfo(interp, scalePtr,
scalePtr->optionTable,
(objc == 3) ? objv[2] : NULL, scalePtr->tkwin);
if (objPtr == NULL) {
goto error;
}
Tcl_SetObjResult(interp, objPtr);
} else {
result = ConfigureScale(interp, scalePtr, objc-2, objv+2);
}
break;
case COMMAND_COORDS: {
int x, y;
double value;
Tcl_Obj *coords[2];
if ((objc != 2) && (objc != 3)) {
Tcl_WrongNumArgs(interp, 1, objv, "coords ?value?");
goto error;
}
if (objc == 3) {
if (Tcl_GetDoubleFromObj(interp, objv[2], &value) != TCL_OK) {
goto error;
}
} else {
value = scalePtr->value;
}
if (scalePtr->orient == ORIENT_VERTICAL) {
x = scalePtr->vertTroughX + scalePtr->width/2
+ scalePtr->borderWidth;
y = TkScaleValueToPixel(scalePtr, value);
} else {
x = TkScaleValueToPixel(scalePtr, value);
y = scalePtr->horizTroughY + scalePtr->width/2
+ scalePtr->borderWidth;
}
coords[0] = Tcl_NewWideIntObj(x);
coords[1] = Tcl_NewWideIntObj(y);
Tcl_SetObjResult(interp, Tcl_NewListObj(2, coords));
break;
}
case COMMAND_GET: {
double value;
int x, y;
if ((objc != 2) && (objc != 4)) {
Tcl_WrongNumArgs(interp, 1, objv, "get ?x y?");
goto error;
}
if (objc == 2) {
value = scalePtr->value;
} else {
if ((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK) ||
(Tcl_GetIntFromObj(interp, objv[3], &y) != TCL_OK)) {
goto error;
}
value = TkScalePixelToValue(scalePtr, x, y);
}
Tcl_SetObjResult(interp, Tcl_ObjPrintf(scalePtr->valueFormat, value));
break;
}
case COMMAND_IDENTIFY: {
int x, y;
const char *zone = "";
if (objc != 4) {
Tcl_WrongNumArgs(interp, 1, objv, "identify x y");
goto error;
}
if ((Tcl_GetIntFromObj(interp, objv[2], &x) != TCL_OK)
|| (Tcl_GetIntFromObj(interp, objv[3], &y) != TCL_OK)) {
goto error;
}
switch (TkpScaleElement(scalePtr, x, y)) {
case TROUGH1: zone = "trough1"; break;
case SLIDER: zone = "slider"; break;
case TROUGH2: zone = "trough2"; break;
}
Tcl_SetObjResult(interp, Tcl_NewStringObj(zone, TCL_INDEX_NONE));
break;
}
case COMMAND_SET: {
double value;
if (objc != 3) {
Tcl_WrongNumArgs(interp, 1, objv, "set value");
goto error;
}
if (Tcl_GetDoubleFromObj(interp, objv[2], &value) != TCL_OK) {
goto error;
}
if (scalePtr->state != STATE_DISABLED) {
TkScaleSetValue(scalePtr, value, 1, 1);
}
break;
}
}
Tcl_Release(scalePtr);
return result;
error:
Tcl_Release(scalePtr);
return TCL_ERROR;
}
/*
*----------------------------------------------------------------------
*
* DestroyScale --
*
* This procedure is invoked by Tcl_EventuallyFree or Tcl_Release to
* clean up the internal structure of a button at a safe time (when
* no-one is using it anymore).
*
* Results:
* None.
*
* Side effects:
* Everything associated with the scale is freed up.
*
*----------------------------------------------------------------------
*/
static void
DestroyScale(
void *memPtr) /* Info about scale widget. */
{
TkScale *scalePtr = (TkScale *) memPtr;
scalePtr->flags |= SCALE_DELETED;
Tcl_DeleteCommandFromToken(scalePtr->interp, scalePtr->widgetCmd);
if (scalePtr->flags & REDRAW_PENDING) {
Tcl_CancelIdleCall(TkpDisplayScale, scalePtr);
}
/*
* Free up all the stuff that requires special handling, then let
* Tk_FreeOptions handle all the standard option-related stuff.
*/
if (scalePtr->varNamePtr != NULL) {
Tcl_UntraceVar2(scalePtr->interp, Tcl_GetString(scalePtr->varNamePtr),
NULL, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
ScaleVarProc, scalePtr);
}
if (scalePtr->troughGC != NULL) {
Tk_FreeGC(scalePtr->display, scalePtr->troughGC);
}
if (scalePtr->copyGC != NULL) {
Tk_FreeGC(scalePtr->display, scalePtr->copyGC);
}
if (scalePtr->textGC != NULL) {
Tk_FreeGC(scalePtr->display, scalePtr->textGC);
}
Tk_FreeConfigOptions((char *) scalePtr, scalePtr->optionTable,
scalePtr->tkwin);
scalePtr->tkwin = NULL;
TkpDestroyScale(scalePtr);
}
/*
*----------------------------------------------------------------------
*
* ConfigureScale --
*
* This procedure is called to process an argv/argc list, plus the Tk
* option database, in order to configure (or reconfigure) a scale
* widget.
*
* Results:
* The return value is a standard Tcl result. If TCL_ERROR is returned,
* then the interp's result contains an error message.
*
* Side effects:
* Configuration information, such as colors, border width, etc. get set
* for scalePtr; old resources get freed, if there were any.
*
*----------------------------------------------------------------------
*/
static int
ConfigureScale(
Tcl_Interp *interp, /* Used for error reporting. */
TkScale *scalePtr, /* Information about widget; may or may not
* already have values for some fields. */
int objc, /* Number of valid entries in objv. */
Tcl_Obj *const objv[]) /* Argument values. */
{
Tk_SavedOptions savedOptions;
Tcl_Obj *errorResult = NULL;
int error;
double varValue;
/*
* Eliminate any existing trace on a variable monitored by the scale.
*/
if (scalePtr->varNamePtr != NULL) {
Tcl_UntraceVar2(interp, Tcl_GetString(scalePtr->varNamePtr),
NULL, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
ScaleVarProc, scalePtr);
}
for (error = 0; error <= 1; error++) {
if (!error) {
/*
* First pass: set options to new values.
*/
if (Tk_SetOptions(interp, scalePtr,
scalePtr->optionTable, objc, objv, scalePtr->tkwin,
&savedOptions, NULL) != TCL_OK) {
continue;
}
} else {
/*
* Second pass: restore options to old values.
*/
errorResult = Tcl_GetObjResult(interp);
Tcl_IncrRefCount(errorResult);
Tk_RestoreSavedOptions(&savedOptions);
}
/*
* If the scale is tied to the value of a variable, then set the
* scale's value from the value of the variable, if it exists and it
* holds a valid double value.
*/
if (scalePtr->varNamePtr != NULL) {
double value;
Tcl_Obj *valuePtr;
valuePtr = Tcl_ObjGetVar2(interp, scalePtr->varNamePtr, NULL,
TCL_GLOBAL_ONLY);
if ((valuePtr != NULL) &&
(Tcl_GetDoubleFromObj(NULL, valuePtr, &value) == TCL_OK)) {
scalePtr->value = TkRoundValueToResolution(scalePtr, value);
}
}
/*
* The fromValue shall not be rounded to the resolution, but the
* toValue and tickInterval do.
*/
scalePtr->toValue = TkRoundValueToResolution(scalePtr, scalePtr->toValue);
scalePtr->tickInterval = TkRoundIntervalToResolution(scalePtr,
scalePtr->tickInterval);
/*
* Make sure that the tick interval has the right sign so that
* addition moves from fromValue to toValue.
*/
if ((scalePtr->tickInterval < 0)
^ ((scalePtr->toValue - scalePtr->fromValue) < 0)) {
scalePtr->tickInterval = -scalePtr->tickInterval;
}
ComputeFormat(scalePtr, 0);
ComputeFormat(scalePtr, 1);
scalePtr->labelLength = scalePtr->label ? strlen(scalePtr->label) : 0;
Tk_SetBackgroundFromBorder(scalePtr->tkwin, scalePtr->bgBorder);
if (scalePtr->highlightWidth < 0) {
scalePtr->highlightWidth = 0;
}
scalePtr->inset = scalePtr->highlightWidth + scalePtr->borderWidth;
break;
}
if (!error) {
Tk_FreeSavedOptions(&savedOptions);
}
/*
* Set the scale value to itself; all this does is to make sure that the
* scale's value is within the new acceptable range for the scale. We
* don't set the var here because we need to make special checks for
* possibly changed varNamePtr.
*/
TkScaleSetValue(scalePtr, scalePtr->value, 0, 1);
/*
* Reestablish the variable trace, if it is needed.
*/
if (scalePtr->varNamePtr != NULL) {
Tcl_Obj *valuePtr;
/*
* Set the associated variable only when the new value differs from
* the current value, or the variable doesn't yet exist.
*/
valuePtr = Tcl_ObjGetVar2(interp, scalePtr->varNamePtr, NULL,
TCL_GLOBAL_ONLY);
if ((valuePtr == NULL) || (Tcl_GetDoubleFromObj(NULL,
valuePtr, &varValue) != TCL_OK)) {
ScaleSetVariable(scalePtr);
} else {
char varString[TCL_DOUBLE_SPACE], scaleString[TCL_DOUBLE_SPACE];
Tcl_PrintDouble(NULL, varValue, varString);
Tcl_PrintDouble(NULL, scalePtr->value, scaleString);
if (strcmp(varString, scaleString)) {
ScaleSetVariable(scalePtr);
}
}
Tcl_TraceVar2(interp, Tcl_GetString(scalePtr->varNamePtr),
NULL, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
ScaleVarProc, scalePtr);
}
ScaleWorldChanged(scalePtr);
if (error) {
Tcl_SetObjResult(interp, errorResult);
Tcl_DecrRefCount(errorResult);
return TCL_ERROR;
}
return TCL_OK;
}
/*
*---------------------------------------------------------------------------
*
* ScaleWorldChanged --
*
* This procedure is called when the world has changed in some way and
* the widget needs to recompute all its graphics contexts and determine
* its new geometry.
*
* Results:
* None.
*
* Side effects:
* Scale will be relayed out and redisplayed.
*
*---------------------------------------------------------------------------
*/
static void
ScaleWorldChanged(
void *instanceData) /* Information about widget. */
{
XGCValues gcValues;
GC gc;
TkScale *scalePtr = (TkScale *)instanceData;
gcValues.foreground = scalePtr->troughColorPtr->pixel;
gc = Tk_GetGC(scalePtr->tkwin, GCForeground, &gcValues);
if (scalePtr->troughGC != NULL) {
Tk_FreeGC(scalePtr->display, scalePtr->troughGC);
}
scalePtr->troughGC = gc;
gcValues.font = Tk_FontId(scalePtr->tkfont);
gcValues.foreground = scalePtr->textColorPtr->pixel;
gc = Tk_GetGC(scalePtr->tkwin, GCForeground | GCFont, &gcValues);
if (scalePtr->textGC != NULL) {
Tk_FreeGC(scalePtr->display, scalePtr->textGC);
}
scalePtr->textGC = gc;
if (scalePtr->copyGC == NULL) {
gcValues.graphics_exposures = False;
scalePtr->copyGC = Tk_GetGC(scalePtr->tkwin, GCGraphicsExposures,
&gcValues);
}
scalePtr->inset = scalePtr->highlightWidth + scalePtr->borderWidth;
/*
* Recompute display-related information, and let the geometry manager
* know how much space is needed now.
*/
ComputeScaleGeometry(scalePtr);
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL);
}
/*
*----------------------------------------------------------------------
*
* MaxTickRoundingError --
*
* Given the separation between values that can be displayed on ticks,
* this calculates the maximum magnitude of error for the displayed
* value. Tries to be clever by working out the increment in error
* between ticks rather than testing all of them, so may overestimate
* error if it is greater than 0.25 x the value separation.
*
* Results:
* Maximum error magnitude of tick numbers.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static double
MaxTickRoundingError(
TkScale *scalePtr, /* Information about scale widget. */
double tickResolution) /* Separation between displayable values. */
{
double tickPosn, firstTickError, lastTickError, intervalError;
int tickCount;
/*
* Compute the error for each tick-related measure.
*/
tickPosn = scalePtr->fromValue / tickResolution;
firstTickError = tickPosn - ScaleRound(tickPosn);
tickPosn = scalePtr->tickInterval / tickResolution;
intervalError = tickPosn - ScaleRound(tickPosn);
tickCount = (int) ((scalePtr->toValue - scalePtr->fromValue) /
scalePtr->tickInterval); /* not including first */
lastTickError = ScaleMin(0.5,
fabs(firstTickError + tickCount * intervalError));
/*
* Compute the maximum cumulative rounding error.
*/
return ScaleMax(fabs(firstTickError), lastTickError) * tickResolution;
}
/*
*----------------------------------------------------------------------
*
* ComputeFormat --
*
* This procedure is invoked to recompute the "valueFormat" or
* "tickFormat" field of a scale's widget record, which determines how
* the value of the scale or one of its ticks is converted to a string.
*
* Results:
* None.
*
* Side effects: The valueFormat or tickFormat field of scalePtr is modified.
*
*----------------------------------------------------------------------
*/
static void
ComputeFormat(
TkScale *scalePtr, /* Information about scale widget. */
int forTicks) /* Do for ticks rather than value */
{
double maxValue, x;
int mostSigDigit, numDigits, leastSigDigit, afterDecimal;
int eDigits, fDigits;
/*
* Compute the displacement from the decimal of the most significant digit
* required for any number in the scale's range.
*/
maxValue = fabs(scalePtr->fromValue);
x = fabs(scalePtr->toValue);
if (x > maxValue) {
maxValue = x;
}
if (maxValue == 0) {
maxValue = 1;
}
mostSigDigit = ScaleDigit(maxValue);
if (forTicks) {
/*
* Display only enough digits to ensure adjacent ticks have different
* values.
*/
if (scalePtr->tickInterval != 0) {
leastSigDigit = ScaleDigit(scalePtr->tickInterval);
/*
* Now add more digits until max error is less than
* TICK_VALUES_DISPLAY_ACCURACY intervals
*/
while (MaxTickRoundingError(scalePtr, pow(10, leastSigDigit))
> fabs(TICK_VALUES_DISPLAY_ACCURACY * scalePtr->tickInterval)) {
--leastSigDigit;
}
numDigits = 1 + mostSigDigit - leastSigDigit;
} else {
numDigits = 1;
}
} else {
/*
* If the number of significant digits wasn't specified explicitly,
* compute it. It's the difference between the most significant digit
* needed to represent any number on the scale and the most
* significant digit of the smallest difference between numbers on the
* scale. In other words, display enough digits so that at least one
* digit will be different between any two adjacent positions of the
* scale.
*/
numDigits = scalePtr->digits;
if (numDigits > TCL_MAX_PREC) {
numDigits = 0;
}
if (numDigits <= 0) {
if (scalePtr->resolution > 0) {
/*
* A resolution was specified for the scale, so just use it.
*/
leastSigDigit = ScaleDigit(scalePtr->resolution);
} else {
/*
* No resolution was specified, so compute the difference in
* value between adjacent pixels and use it for the least
* significant digit.
*/
x = fabs(scalePtr->fromValue - scalePtr->toValue);
if (scalePtr->length > 0) {
x /= scalePtr->length;
}
if (x > 0) {
leastSigDigit = ScaleDigit(x);
} else {
leastSigDigit = 0;
}
}
numDigits = mostSigDigit - leastSigDigit + 1;
if (numDigits < 1) {
numDigits = 1;
}
}
}
/*
* Compute the number of characters required using "e" format and "f"
* format, and then choose whichever one takes fewer characters.
*/
eDigits = numDigits + 4;
if (numDigits > 1) {
eDigits++; /* Decimal point. */
}
afterDecimal = numDigits - mostSigDigit - 1;
if (afterDecimal < 0) {
afterDecimal = 0;
}
fDigits = (mostSigDigit >= 0) ? mostSigDigit + afterDecimal : afterDecimal;
if (afterDecimal > 0) {
fDigits++; /* Decimal point. */
}
if (mostSigDigit < 0) {
fDigits++; /* Zero to left of decimal point. */
}
if (forTicks) {
if (fDigits <= eDigits) {
snprintf(scalePtr->tickFormat, sizeof(scalePtr->tickFormat), "%%.%df", afterDecimal);
} else {
snprintf(scalePtr->tickFormat, sizeof(scalePtr->tickFormat), "%%.%de", numDigits - 1);
}
} else {
if (fDigits <= eDigits) {
snprintf(scalePtr->valueFormat, sizeof(scalePtr->valueFormat), "%%.%df", afterDecimal);
} else {
snprintf(scalePtr->valueFormat, sizeof(scalePtr->valueFormat), "%%.%de", numDigits - 1);
}
}
}
/*
*----------------------------------------------------------------------
*
* ComputeScaleGeometry --
*
* This procedure is called to compute various geometrical information
* for a scale, such as where various things get displayed. It's called
* when the window is reconfigured.
*
* Results:
* None.
*
* Side effects:
* Display-related numbers get changed in *scalePtr. The geometry manager
* gets told about the window's preferred size.
*
*----------------------------------------------------------------------
*/
static void
ComputeScaleGeometry(
TkScale *scalePtr) /* Information about widget. */
{
char valueString[TCL_DOUBLE_SPACE];
int tmp, valuePixels, tickPixels, x, y, extraSpace;
Tk_FontMetrics fm;
Tk_GetFontMetrics(scalePtr->tkfont, &fm);
scalePtr->fontHeight = fm.linespace + SPACING;
/*
* Horizontal scales are simpler than vertical ones because all sizes are
* the same (the height of a line of text); handle them first and then
* quit.
*/
if (scalePtr->orient == ORIENT_HORIZONTAL) {
y = scalePtr->inset;
extraSpace = 0;
if (scalePtr->labelLength != 0) {
scalePtr->horizLabelY = y + SPACING;
y += scalePtr->fontHeight;
extraSpace = SPACING;
}
if (scalePtr->showValue) {
scalePtr->horizValueY = y + SPACING;
y += scalePtr->fontHeight;
extraSpace = SPACING;
} else {
scalePtr->horizValueY = y;
}
y += extraSpace;
scalePtr->horizTroughY = y;
y += scalePtr->width + 2*scalePtr->borderWidth;
if (scalePtr->tickInterval != 0) {
scalePtr->horizTickY = y + SPACING;
y += scalePtr->fontHeight + SPACING;
}
Tk_GeometryRequest(scalePtr->tkwin,
scalePtr->length + 2*scalePtr->inset, y + scalePtr->inset);
Tk_SetInternalBorder(scalePtr->tkwin, scalePtr->inset);
return;
}
/*
* Vertical scale: compute the amount of space needed to display the
* scales value by formatting strings for the two end points; use
* whichever length is longer.
*/
if (snprintf(valueString, TCL_DOUBLE_SPACE, scalePtr->valueFormat,
scalePtr->fromValue) < 0) {
valueString[TCL_DOUBLE_SPACE - 1] = '\0';
}
valuePixels = Tk_TextWidth(scalePtr->tkfont, valueString, -1);
if (snprintf(valueString, TCL_DOUBLE_SPACE, scalePtr->valueFormat,
scalePtr->toValue) < 0) {
valueString[TCL_DOUBLE_SPACE - 1] = '\0';
}
tmp = Tk_TextWidth(scalePtr->tkfont, valueString, -1);
if (valuePixels < tmp) {
valuePixels = tmp;
}
/*
* Now do the same thing for the tick values
*/
if (snprintf(valueString, TCL_DOUBLE_SPACE, scalePtr->tickFormat,
scalePtr->fromValue) < 0) {
valueString[TCL_DOUBLE_SPACE - 1] = '\0';
}
tickPixels = Tk_TextWidth(scalePtr->tkfont, valueString, -1);
if (snprintf(valueString, TCL_DOUBLE_SPACE, scalePtr->tickFormat,
scalePtr->toValue) < 0) {
valueString[TCL_DOUBLE_SPACE - 1] = '\0';
}
tmp = Tk_TextWidth(scalePtr->tkfont, valueString, -1);
if (tickPixels < tmp) {
tickPixels = tmp;
}
/*
* Assign x-locations to the elements of the scale, working from left to
* right.
*/
x = scalePtr->inset;
if ((scalePtr->tickInterval != 0) && (scalePtr->showValue)) {
scalePtr->vertTickRightX = x + SPACING + tickPixels;
scalePtr->vertValueRightX = scalePtr->vertTickRightX + valuePixels
+ fm.ascent/2;
x = scalePtr->vertValueRightX + SPACING;
} else if (scalePtr->tickInterval != 0) {
scalePtr->vertTickRightX = x + SPACING + tickPixels;
scalePtr->vertValueRightX = scalePtr->vertTickRightX;
x = scalePtr->vertTickRightX + SPACING;
} else if (scalePtr->showValue) {
scalePtr->vertTickRightX = x;
scalePtr->vertValueRightX = x + SPACING + valuePixels;
x = scalePtr->vertValueRightX + SPACING;
} else {
scalePtr->vertTickRightX = x;
scalePtr->vertValueRightX = x;
}
scalePtr->vertTroughX = x;
x += 2*scalePtr->borderWidth + scalePtr->width;
if (scalePtr->labelLength == 0) {
scalePtr->vertLabelX = 0;
} else {
scalePtr->vertLabelX = x + fm.ascent/2;
x = scalePtr->vertLabelX + fm.ascent/2
+ Tk_TextWidth(scalePtr->tkfont, scalePtr->label,
scalePtr->labelLength);
}
Tk_GeometryRequest(scalePtr->tkwin, x + scalePtr->inset,
scalePtr->length + 2*scalePtr->inset);
Tk_SetInternalBorder(scalePtr->tkwin, scalePtr->inset);
}
/*
*--------------------------------------------------------------
*
* ScaleEventProc --
*
* This procedure is invoked by the Tk dispatcher for various events on
* scales.
*
* Results:
* None.
*
* Side effects:
* When the window gets deleted, internal structures get cleaned up.
* When it gets exposed, it is redisplayed.
*
*--------------------------------------------------------------
*/
static void
ScaleEventProc(
void *clientData, /* Information about window. */
XEvent *eventPtr) /* Information about event. */
{
TkScale *scalePtr = (TkScale *)clientData;
if ((eventPtr->type == Expose) && (eventPtr->xexpose.count == 0)) {
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL);
} else if (eventPtr->type == DestroyNotify) {
DestroyScale(clientData);
} else if (eventPtr->type == ConfigureNotify) {
ComputeScaleGeometry(scalePtr);
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL);
} else if (eventPtr->type == FocusIn) {
if (eventPtr->xfocus.detail != NotifyInferior) {
scalePtr->flags |= GOT_FOCUS;
if (scalePtr->highlightWidth > 0) {
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL);
}
}
} else if (eventPtr->type == FocusOut) {
if (eventPtr->xfocus.detail != NotifyInferior) {
scalePtr->flags &= ~GOT_FOCUS;
if (scalePtr->highlightWidth > 0) {
TkEventuallyRedrawScale(scalePtr, REDRAW_ALL);
}
}
}
}
/*
*----------------------------------------------------------------------
*
* ScaleCmdDeletedProc --
*
* 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
ScaleCmdDeletedProc(
void *clientData) /* Pointer to widget record for widget. */
{
TkScale *scalePtr = (TkScale *)clientData;
Tk_Window tkwin = scalePtr->tkwin;
/*
* This procedure could be invoked either because the window was destroyed
* and the command was then deleted (in which case tkwin is NULL) or
* because the command was deleted, and then this procedure destroys the
* widget.
*/
if (!(scalePtr->flags & SCALE_DELETED)) {
scalePtr->flags |= SCALE_DELETED;
Tk_DestroyWindow(tkwin);
}
}
/*
*--------------------------------------------------------------
*
* TkEventuallyRedrawScale --
*
* Arrange for part or all of a scale widget to redrawn at the next
* convenient time in the future.
*
* Results:
* None.
*
* Side effects:
* If "what" is REDRAW_SLIDER then just the slider and the value readout
* will be redrawn; if "what" is REDRAW_ALL then the entire widget will
* be redrawn.
*
*--------------------------------------------------------------
*/
void
TkEventuallyRedrawScale(
TkScale *scalePtr, /* Information about widget. */
int what) /* What to redraw: REDRAW_SLIDER or
* REDRAW_ALL. */
{
if ((what == 0) || (scalePtr->tkwin == NULL)
|| !Tk_IsMapped(scalePtr->tkwin)) {
return;
}
if (!(scalePtr->flags & REDRAW_PENDING)) {
scalePtr->flags |= REDRAW_PENDING;
Tcl_DoWhenIdle(TkpDisplayScale, scalePtr);
}
scalePtr->flags |= what;
}
/*
*--------------------------------------------------------------
*
* TkRoundValueToResolution, TkRoundIntervalToResolution --
*
* Round a given floating-point value to the nearest multiple of the
* scale's resolution.
* TkRoundValueToResolution rounds an absolute value based on the from
* value as a reference.
* TkRoundIntervalToResolution rounds a relative value without
* reference, i.e. it rounds an interval.
*
* Results:
* The return value is the rounded result.
*
* Side effects:
* None.
*
*--------------------------------------------------------------
*/
double
TkRoundValueToResolution(
TkScale *scalePtr, /* Information about scale widget. */
double value) /* Value to round. */
{
return TkRoundIntervalToResolution(scalePtr, value - scalePtr->fromValue)
+ scalePtr->fromValue;
}
double
TkRoundIntervalToResolution(
TkScale *scalePtr, /* Information about scale widget. */
double value) /* Value to round. */
{
double rem, rounded, tick;
if (scalePtr->resolution <= 0) {
return value;
}
tick = floor(value/scalePtr->resolution);
rounded = scalePtr->resolution * tick;
rem = value - rounded;
if (rem < 0) {
if (rem <= -scalePtr->resolution/2) {
rounded = (tick - 1.0) * scalePtr->resolution;
}
} else {
if (rem >= scalePtr->resolution/2) {
rounded = (tick + 1.0) * scalePtr->resolution;
}
}
return rounded;
}
/*
*----------------------------------------------------------------------
*
* ScaleVarProc --
*
* This procedure is invoked by Tcl whenever someone modifies a variable
* associated with a scale widget.
*
* Results:
* NULL is always returned.
*
* Side effects:
* The value displayed in the scale will change to match the variable's
* new value. If the variable has a bogus value then it is reset to the
* value of the scale.
*
*----------------------------------------------------------------------
*/
static char *
ScaleVarProc(
void *clientData, /* Information about button. */
Tcl_Interp *interp, /* Interpreter containing variable. */
TCL_UNUSED(const char *), /* Name of variable. */
TCL_UNUSED(const char *), /* Second part of variable name. */
int flags) /* Information about what happened. */
{
TkScale *scalePtr = (TkScale *)clientData;
const char *resultStr;
double value;
Tcl_Obj *valuePtr;
int result;
/*
* If the variable is unset, then immediately recreate it unless the whole
* interpreter is going away.
*/
if (flags & TCL_TRACE_UNSETS) {
if (!Tcl_InterpDeleted(interp) && scalePtr->varNamePtr) {
void *probe = NULL;
do {
probe = Tcl_VarTraceInfo(interp,
Tcl_GetString(scalePtr->varNamePtr),
TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
ScaleVarProc, probe);
if (probe == (void *)scalePtr) {
break;
}
} while (probe);
if (probe) {
/*
* We were able to fetch the unset trace for our
* varNamePtr, which means it is not unset and not
* the cause of this unset trace. Instead some outdated
* former variable must be, and we should ignore it.
*/
return NULL;
}
Tcl_TraceVar2(interp, Tcl_GetString(scalePtr->varNamePtr),
NULL, TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
ScaleVarProc, clientData);
scalePtr->flags |= NEVER_SET;
TkScaleSetValue(scalePtr, scalePtr->value, 1, 0);
}
return NULL;
}
/*
* If we came here because we updated the variable (in TkScaleSetValue),
* then ignore the trace. Otherwise update the scale with the value of the
* variable.
*/
if (scalePtr->flags & SETTING_VAR) {
return NULL;
}
resultStr = NULL;
valuePtr = Tcl_ObjGetVar2(interp, scalePtr->varNamePtr, NULL,
TCL_GLOBAL_ONLY);
result = Tcl_GetDoubleFromObj(interp, valuePtr, &value);
if (result != TCL_OK) {
resultStr = "can't assign non-numeric value to scale variable";
ScaleSetVariable(scalePtr);
} else {
scalePtr->value = TkRoundValueToResolution(scalePtr, value);
/*
* This code is a bit tricky because it sets the scale's value before
* calling TkScaleSetValue. This way, TkScaleSetValue won't bother to
* set the variable again or to invoke the -command. However, it also
* won't redisplay the scale, so we have to ask for that explicitly.
*/
TkScaleSetValue(scalePtr, scalePtr->value, 1, 0);
}
TkEventuallyRedrawScale(scalePtr, REDRAW_SLIDER);
return (char *) resultStr;
}
/*
*--------------------------------------------------------------
*
* TkScaleSetValue --
*
* This procedure changes the value of a scale and invokes a Tcl command
* to reflect the current position of a scale
*
* Results:
* None.
*
* Side effects:
* A Tcl command is invoked, and an additional error-processing command
* may also be invoked. The scale's slider is redrawn.
*
*--------------------------------------------------------------
*/
void
TkScaleSetValue(
TkScale *scalePtr, /* Info about widget. */
double value, /* New value for scale. Gets adjusted if it's
* off the scale. */
int setVar, /* Non-zero means reflect new value through to
* associated variable, if any. */
int invokeCommand) /* Non-zero means invoked -command option to
* notify of new value, 0 means don't. */
{
value = TkRoundValueToResolution(scalePtr, value);
if ((value < scalePtr->fromValue)
^ (scalePtr->toValue < scalePtr->fromValue)) {
value = scalePtr->fromValue;
}
if ((value > scalePtr->toValue)
^ (scalePtr->toValue < scalePtr->fromValue)) {
value = scalePtr->toValue;
}
if (scalePtr->flags & NEVER_SET) {
scalePtr->flags &= ~NEVER_SET;
} else if (scalePtr->value == value) {
return;
}
scalePtr->value = value;
/*
* Schedule command callback invocation only if there is such a command
* already registered, otherwise the callback would trigger later when
* configuring the widget -command option even if the value did not change.
*/
if ((invokeCommand) && (scalePtr->command != NULL)) {
scalePtr->flags |= INVOKE_COMMAND;
}
TkEventuallyRedrawScale(scalePtr, REDRAW_SLIDER);
if (setVar && scalePtr->varNamePtr) {
ScaleSetVariable(scalePtr);
}
}
/*
*--------------------------------------------------------------
*
* ScaleSetVariable --
*
* This procedure sets the variable associated with a scale, if any.
*
* Results:
* None.
*
* Side effects:
* Other write traces on the variable will trigger.
*
*--------------------------------------------------------------
*/
static void
ScaleSetVariable(
TkScale *scalePtr) /* Info about widget. */
{
if (scalePtr->varNamePtr != NULL) {
char string[TCL_DOUBLE_SPACE];
if (snprintf(string, TCL_DOUBLE_SPACE, scalePtr->valueFormat,
scalePtr->value) < 0) {
string[TCL_DOUBLE_SPACE - 1] = '\0';
}
scalePtr->flags |= SETTING_VAR;
Tcl_ObjSetVar2(scalePtr->interp, scalePtr->varNamePtr, NULL,
Tcl_NewStringObj(string, TCL_INDEX_NONE), TCL_GLOBAL_ONLY);
scalePtr->flags &= ~SETTING_VAR;
}
}
/*
*----------------------------------------------------------------------
*
* TkScalePixelToValue --
*
* Given a pixel within a scale window, return the scale reading
* corresponding to that pixel.
*
* Results:
* A double-precision scale reading. If the value is outside the legal
* range for the scale then it's rounded to the nearest end of the scale.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
double
TkScalePixelToValue(
TkScale *scalePtr, /* Information about widget. */
int x, int y) /* Coordinates of point within window. */
{
double value, pixelRange;
if (scalePtr->orient == ORIENT_VERTICAL) {
pixelRange = Tk_Height(scalePtr->tkwin) - scalePtr->sliderLength
- 2*scalePtr->inset - 2*scalePtr->borderWidth;
value = y;
} else {
pixelRange = Tk_Width(scalePtr->tkwin) - scalePtr->sliderLength
- 2*scalePtr->inset - 2*scalePtr->borderWidth;
value = x;
}
if (pixelRange <= 0) {
/*
* Not enough room for the slider to actually slide: just return the
* scale's current value.
*/
return scalePtr->value;
}
value -= scalePtr->sliderLength/2 + scalePtr->inset
+ scalePtr->borderWidth;
value /= pixelRange;
if (value < 0) {
value = 0;
}
if (value > 1) {
value = 1;
}
value = scalePtr->fromValue +
value * (scalePtr->toValue - scalePtr->fromValue);
return TkRoundValueToResolution(scalePtr, value);
}
/*
*----------------------------------------------------------------------
*
* TkScaleValueToPixel --
*
* Given a reading of the scale, return the x-coordinate or y-coordinate
* corresponding to that reading, depending on whether the scale is
* vertical or horizontal, respectively.
*
* Results:
* An integer value giving the pixel location corresponding to reading.
* The value is restricted to lie within the defined range for the scale.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
TkScaleValueToPixel(
TkScale *scalePtr, /* Information about widget. */
double value) /* Reading of the widget. */
{
int y, pixelRange;
double valueRange;
valueRange = scalePtr->toValue - scalePtr->fromValue;
pixelRange = ((scalePtr->orient == ORIENT_VERTICAL)
? Tk_Height(scalePtr->tkwin) : Tk_Width(scalePtr->tkwin))
- scalePtr->sliderLength - 2*scalePtr->inset - 2*scalePtr->borderWidth;
if (valueRange == 0) {
y = 0;
} else {
y = ScaleRound((value - scalePtr->fromValue) * pixelRange
/ valueRange);
if (y < 0) {
y = 0;
} else if (y > pixelRange) {
y = pixelRange;
}
}
y += scalePtr->sliderLength/2 + scalePtr->inset + scalePtr->borderWidth;
return y;
}
/*
* Local Variables:
* mode: c
* c-basic-offset: 4
* fill-column: 78
* End:
*/
|