-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tcl_Linkvar85.C
executable file
·1353 lines (1119 loc) · 37.7 KB
/
Tcl_Linkvar85.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "Tcl_Linkvar85.h"
#include <tcl.h>
#include <tk.h>
static const int MAX_INTS = 1000; // Max number of int variables
static const int MAX_IB = 1000; // Max number of int/buttons
static const int MAX_FLOATS = 1000; // Max number of float variables
static const int MAX_FS = 1000; // Max number of floatscale vars
static const int MAX_SELS = 1000; // Max number of selectors
static int num_ints = 0; // Number of int variables
static Tclvar_int *int_list[MAX_INTS]; // Int variables
static int num_ib = 0; // Number of ints with buttons
static Tclvar_int_with_button *ib_list[MAX_IB];// IB variables
static int num_floats = 0; // Number of float variables
static Tclvar_float *float_list[MAX_FLOATS];// Float variables
static int num_fs = 0; // Number of floats with scales
static Tclvar_float_with_scale *fs_list[MAX_FS];// Floatscale variables
static int num_sels = 0; // Number of selectors
static Tclvar_selector *sel_list[MAX_SELS];
static Tcl_Interp *interpreter = NULL; // Tcl interpreter used
// DO NOT USE commands such as Tcl_GetVar2 (with the 2 at the end)
// if we use Tcl_GetVar instead, we wil be able to link with array elements
// in TCL.
// Update the integer variable in the handler that is pointed to
// when the variables changes.
static char *handle_int_value_change(ClientData clientData,
Tcl_Interp *interp, const char * /*name1*/, const char * /*name2*/, int /*flags*/)
{
const char *cvalue;
int value;
Tclvar_int *intvar = (Tclvar_int*)(clientData);
// Look up the new value of the variable
cvalue = Tcl_GetVar(interp, intvar->my_tcl_varname,
TCL_GLOBAL_ONLY);
if (cvalue == NULL) {
fprintf(stderr,"Warning! Can't read %s from Tcl\n",
intvar->my_tcl_varname);
} else if (Tcl_GetInt(interp, cvalue, &value) != TCL_OK) {
fprintf(stderr,"Warning! %s not int from Tcl\n",
intvar->my_tcl_varname);
}
intvar->myint = intvar->mylastint = value;
// Yank the callback if it has been set
if (intvar->callback) {
intvar->callback(value, intvar->userdata);
}
return NULL;
};
// Update the float variable in the handler that is pointed to
// when the variables changes.
static char *handle_float_value_change(ClientData clientData,
Tcl_Interp *interp, const char * /*name1*/, const char * /*name2*/, int /*flags*/)
{
const char *cvalue;
double value;
Tclvar_float *floatvar = (Tclvar_float*)(clientData);
// printf("floatvalchange: %s %s %s\n", name1, name2, floatvar->my_tcl_varname);
// Look up the new value of the variable
cvalue = Tcl_GetVar(interp, floatvar->my_tcl_varname,
TCL_GLOBAL_ONLY);
if (cvalue == NULL) {
fprintf(stderr,"Warning! Can't read %s from Tcl\n",
floatvar->my_tcl_varname);
} else if (Tcl_GetDouble(interp, cvalue, &value) != TCL_OK) {
fprintf(stderr,"Warning! %s not double from Tcl\n",
floatvar->my_tcl_varname);
}
floatvar->myfloat = floatvar->mylastfloat = (float)value;
// Yank the callback if it has been set
if (floatvar->callback) {
floatvar->callback((float)value, floatvar->userdata);
}
return NULL;
};
// Update the string variable in the handler that is pointed to
// when the variables changes.
static char *handle_string_value_change(ClientData clientData,
Tcl_Interp *interp, const char * /*name1*/, const char * /*name2*/, int /*flags*/)
{
const char *cvalue;
Tclvar_selector *selvar = (Tclvar_selector*)(clientData);
// Look up the new value of the variable
cvalue = Tcl_GetVar(interp, selvar->my_tcl_varname,
TCL_GLOBAL_ONLY);
if (cvalue == NULL) {
fprintf(stderr,"Warning! Can't read %s from Tcl\n",
selvar->my_tcl_varname);
}
strncpy(selvar->mystring, cvalue, TCLVAR_STRING_LENGTH);
strncpy(selvar->mylaststring, cvalue, TCLVAR_STRING_LENGTH);
// Yank the callback if it has been set
if (selvar->callback) {
selvar->callback(cvalue, selvar->userdata);
}
return NULL;
};
// Set the interpreter to be used by the integers and floats.
// Put together links for all of the variables that have been created
// before the interpreter was set.
// Return 0 on success, -1 on failure.
int Tclvar_init(Tcl_Interp *tcl_interp)
{
int i;
char cvalue[100];
//fprintf(stderr, "Tclvar_init() called\n");
// Set the interpreter
interpreter = tcl_interp;
// Insert callbacks for the integer variables
for (i = 0; i < num_ints; i++) {
// initialize the tcl variable with the value from the
// C variable
sprintf(cvalue, "%d", int_list[i]->myint);
Tcl_SetVar(interpreter, int_list[i]->my_tcl_varname,
cvalue, TCL_GLOBAL_ONLY);
if (Tcl_TraceVar(interpreter, int_list[i]->my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_int_value_change,
(ClientData)((void*)(int_list[i]))) != TCL_OK) {
fprintf(stderr, "Tcl_TraceVar(%s) failed: %s\n",
int_list[i]->my_tcl_varname,
interpreter->result);
return(-1);
}
}
// Insert callbacks for the float variables
for (i = 0; i < num_floats; i++) {
// initialize the tcl variable with the value from the
// C variable
sprintf(cvalue, "%g", float_list[i]->myfloat);
Tcl_SetVar(interpreter, float_list[i]->my_tcl_varname,
cvalue, TCL_GLOBAL_ONLY);
if (Tcl_TraceVar(interpreter, float_list[i]->my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_float_value_change,
(ClientData)((void*)(float_list[i]))) != TCL_OK) {
fprintf(stderr, "Tcl_TraceVar(%s) failed: %s\n",
float_list[i]->my_tcl_varname,
interpreter->result);
return(-1);
}
}
// Set up the integer with buttons.
for (i = 0; i < num_ib; i++) {
if (ib_list[i]->tcl_widget_name != NULL) {
if (ib_list[i]->initialize(interpreter)) return(-1);
}
}
// Set up the floatscales.
for (i = 0; i < num_fs; i++) {
if (fs_list[i]->tcl_widget_name != NULL) {
if (fs_list[i]->initialize(interpreter)) return(-1);
}
}
// Insert callbacks for the selector variables
// Set up the selectors.
for (i = 0; i < num_sels; i++) {
if (sel_list[i]->my_tcl_varname) // TCH 6 April 98
if (Tcl_TraceVar(interpreter, sel_list[i]->my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_string_value_change,
(ClientData)((void*)(sel_list[i]))) != TCL_OK) {
fprintf(stderr, "Tcl_TraceVar(%s) failed: %s\n",
sel_list[i]->my_tcl_varname,
interpreter->result);
return(-1);
}
if (sel_list[i]->tcl_widget_name != NULL) {
if (sel_list[i]->initialize(interpreter)) return(-1);
}
}
return 0;
}
int Tclvar_mainloop(void)
{
int i;
char cvalue[100];
// Make sure there is a valid interpeter
if (interpreter == NULL) {
return -1;
}
// Check for changes in the C integers and update the Tcl variables
// when they occur.
for (i = 0; i < num_ints; i++) {
if (int_list[i]->myint != int_list[i]->mylastint) {
int_list[i]->mylastint = int_list[i]->myint;
sprintf(cvalue,"%d", int_list[i]->myint);
Tcl_SetVar(interpreter, int_list[i]->my_tcl_varname,
cvalue, TCL_GLOBAL_ONLY);
}
}
// Check for changes in the C floats and update the Tcl variables
// when they occur.
for (i = 0; i < num_floats; i++) {
if (float_list[i]->myfloat != float_list[i]->mylastfloat) {
float_list[i]->mylastfloat = float_list[i]->myfloat;
sprintf(cvalue,"%g", float_list[i]->myfloat);
Tcl_SetVar(interpreter, float_list[i]->my_tcl_varname,
cvalue, TCL_GLOBAL_ONLY);
}
}
// Check for changes in the C selectors and update the Tcl variables
// when they occur.
for (i = 0; i < num_sels; i++) {
if (strcmp(sel_list[i]->mystring,sel_list[i]->mylaststring)) {
strcpy(sel_list[i]->mylaststring,sel_list[i]->mystring);
Tcl_SetVar(interpreter, sel_list[i]->my_tcl_varname,
sel_list[i]->mystring, TCL_GLOBAL_ONLY);
}
}
return 0;
}
// Add an entry into the list of active integer Tcl variables, and
// point it to this variable.
// Add a Tcl callback, if an interpreter has been declared.
Tclvar_int::Tclvar_int(char *tcl_varname, int default_value,
Linkvar_Intcall c, void *ud) :
my_tcl_varname(NULL)
{
callback = c;
userdata = ud;
//fprintf(stderr, "Tclvar_int constructor\n");
if (num_ints >= (MAX_INTS-1)) {
fprintf(stderr,"Tclvar_int::Tclvar_int(): Can't link to %s\n",
tcl_varname);
fprintf(stderr," (Out of storage)\n");
} else {
int_list[num_ints] = this;
my_tcl_varname = new char[strlen(tcl_varname)+1];
if (my_tcl_varname == NULL) {
fprintf(stderr,"Tclvar_int::Tclvar_int(): Out of memory\n");
} else {
strcpy(my_tcl_varname, tcl_varname);
myint = mylastint = default_value;
num_ints++;
}
}
// Add a callback for change if there is an interpreter
if (interpreter != NULL) {
if (Tcl_TraceVar(interpreter, my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_int_value_change,
(ClientData)((void*)(this))) != TCL_OK) {
fprintf(stderr, "Tclvar_int::Tclvar_int(): Tcl_TraceVar(%s) failed: %s\n",
my_tcl_varname, interpreter->result);
}
}
};
// Remove the entry from the list of active integer Tcl variables.
Tclvar_int::~Tclvar_int()
{
register int i = 0;
// Remove the trace callback, if there is an interpreter
if (interpreter != NULL) {
Tcl_UntraceVar(interpreter, my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_int_value_change,
(ClientData)((void*)(this)));
}
// Free the space for the name
if (my_tcl_varname != NULL) {delete [] my_tcl_varname; }
// Find this entry in the list of variables
while ( (i < num_ints) && (int_list[i] != this) ) { i++; };
if (i >= num_ints) {
fprintf(stderr,"Tclvar_int::~Tclvar_int(): Internal error -- not in list\n");
return;
}
// Move the last entry to this one
int_list[i] = int_list[num_ints-1];
// Reduce the number in the list
num_ints--;
};
// Add an entry into the list of active float Tcl variables, and
// point it to this variable.
// Add a trace callback if there is an interpreter.
Tclvar_float::Tclvar_float(char *tcl_varname, float default_value,
Linkvar_Floatcall c, void *ud) :
my_tcl_varname(NULL)
{
callback = c;
userdata = ud;
//fprintf(stderr, "Tclvar_float constructor\n");
if (num_floats >= (MAX_FLOATS-1)) {
fprintf(stderr,"Tclvar_float::Tclvar_float(): Can't link to %s\n",
tcl_varname);
fprintf(stderr," (Out of storage)\n");
} else {
float_list[num_floats] = this;
my_tcl_varname = new char[strlen(tcl_varname)+1];
if (my_tcl_varname == NULL) {
fprintf(stderr,"Tclvar_float::Tclvar_float(): Out of memory\n");
} else {
strcpy(my_tcl_varname, tcl_varname);
myfloat = mylastfloat = default_value;
num_floats++;
}
}
// Add a callback for change if there is an interpreter
if (interpreter != NULL) {
if (Tcl_TraceVar(interpreter, my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_float_value_change,
(ClientData)((void*)this)) != TCL_OK) {
fprintf(stderr, "Tclvar_float::Tclvar_float(): Tcl_TraceVar(%s) failed: %s\n",
my_tcl_varname, interpreter->result);
}
}
};
// Remove the entry from the list of active float Tcl variables.
Tclvar_float::~Tclvar_float()
{
register int i = 0;
// Remove the trace callback, if there is an interpreter
if (interpreter != NULL) {
Tcl_UntraceVar(interpreter, my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_float_value_change,
(ClientData)((void*)this));
}
// Free the space for the name
if (my_tcl_varname != NULL) { delete [] my_tcl_varname; }
// Find this entry in the list of variables
while ( (i < num_floats) && (float_list[i] != this) ) { i++; };
if (i >= num_floats) {
fprintf(stderr,"Tclvar_float::~Tclvar_float(): Internal error -- not in list\n");
return;
}
// Move the last entry to this one
float_list[i] = float_list[num_floats-1];
// Reduce the number in the list
num_floats--;
};
// Add a trace callback and handler widget if there is an interpreter.
Tclvar_int_with_button::Tclvar_int_with_button(char *tcl_varname,
char *parent_name, int default_value, Linkvar_Intcall c, void *ud) :
Tclvar_int(tcl_varname, default_value, c, ud),
tcl_widget_name(NULL)
{
//fprintf(stderr, "Tclvar_int_with_button constructor\n");
// Add this to the list of integers with buttons
if (num_ib >= (MAX_IB-1)) {
fprintf(stderr,"Tclvar_int_with_button::Tclvar_int_with_button(): Can't link to %s\n",
tcl_varname);
fprintf(stderr," (Out of storage)\n");
} else {
ib_list[num_ib] = this;
my_tcl_varname = new char[strlen(tcl_varname)+1];
if (my_tcl_varname == NULL) {
fprintf(stderr,"Tclvar_int_with_button::Tclvar_int_with_button(): Out of memory\n");
} else {
strcpy(my_tcl_varname, tcl_varname);
myint = mylastint = default_value;
num_ib++;
}
}
// Set the widget name, if there is a parent specified
// The widget name matches the tcl_varname, unless there are '.'s
// in the name. If there are '.'s, the name is just the part after
// the last '.' (this strips away the prepended widget part).
if (parent_name != NULL) {
char *last_part;
last_part = strrchr(tcl_varname, '.');
if (last_part == NULL) {
last_part = tcl_varname;
} else {
last_part++; // Skip the .
}
tcl_widget_name =
new char[strlen(parent_name) + strlen(last_part) + 2];
if (tcl_widget_name == NULL) {
fprintf(stderr,"Tclvar_int_with_button::Tclvar_int_with_button(): Out of memory\n");
}
sprintf(tcl_widget_name,"%s.%s",parent_name,last_part);
// Make sure widget name starts with a lower-case character.
tcl_widget_name[strlen(parent_name)+1] =
tolower(tcl_widget_name[strlen(parent_name)+1]);
} else {
tcl_widget_name = NULL;
}
// Add and pack a checkbutton if there is an interpreter and a widget
if ( (interpreter != NULL) && (tcl_widget_name != NULL) ) {
initialize(interpreter);
}
};
Tclvar_int_with_button::~Tclvar_int_with_button()
{
register int i = 0;
// Unpack and destroy the checkbutton, if there is an
// interpreter
if (interpreter != NULL) {
char command[1000];
sprintf(command,"destroy %s",tcl_widget_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
}
}
// Free the space for the widget name
if (tcl_widget_name != NULL) { delete [] tcl_widget_name; }
// Find this entry in the list of variables
while ( (i < num_ib) && (ib_list[i] != this) ) { i++; };
if (i >= num_ib) {
fprintf(stderr,"Tclvar_int_with_button::~Tclvar_int_with_button(): Internal error -- not in list\n");
return;
}
// Move the last entry to this one
ib_list[i] = ib_list[num_ib-1];
// Reduce the number in the list
num_ib--;
};
// Add and pack a checkbutton if there is an interpreter and a widget
// for each intwithbutton variable. Note that the change callback will
// be handled by the fact that it is derived from an int var.
// Set the value of the variable so it shows up on the display.
int Tclvar_int_with_button::initialize(Tcl_Interp *interpreter)
{
char command[1000];
char cvalue[100];
char *last_part;
// Set the variable to its default value
sprintf(cvalue,"%d", myint);
Tcl_SetVar(interpreter, my_tcl_varname, cvalue, TCL_GLOBAL_ONLY);
// Create the checkbutton. Only use the truncated name for the
// text in the widget (the part of the variable name after the
// last '.').
last_part = strrchr(my_tcl_varname, '.');
if (last_part == NULL) {
last_part = my_tcl_varname;
} else {
last_part++; // Skip the .
}
sprintf(command,"checkbutton {%s} -text {%s} -variable {%s} -anchor w",
tcl_widget_name, last_part, my_tcl_varname);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
return(-1);
}
// Pack the checkbutton
sprintf(command,"pack {%s} -fill x\n", tcl_widget_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
return(-1);
}
return 0;
}
// Add an entry into the list of active floatscale Tcl variables, and
// point it to this variable. Set up the min and max floats.
// Add a trace callback and handler widget if there is an interpreter.
Tclvar_float_with_scale::Tclvar_float_with_scale(char *tcl_varname,
char *parent_name, float minval, float maxval, float default_value,
Linkvar_Floatcall c, void *ud) :
Tclvar_float(tcl_varname, default_value, c, ud),
tcl_widget_name(NULL),
tcl_label_name(NULL)
{
//fprintf(stderr, "Tclvar_float_with_scale constructor\n");
minvalue = minval;
maxvalue = maxval;
if (num_fs >= (MAX_FS-1)) {
fprintf(stderr,"Tclvar_float_with_scale::Tclvar_float_with_scale(): Can't link to %s\n",
tcl_varname);
fprintf(stderr," (Out of storage)\n");
} else {
fs_list[num_fs] = this;
my_tcl_varname = new char[strlen(tcl_varname)+1];
if (my_tcl_varname == NULL) {
fprintf(stderr,"Tclvar_float_with_scale::Tclvar_float_with_scale(): Out of memory\n");
} else {
strcpy(my_tcl_varname, tcl_varname);
myfloat = mylastfloat = default_value;
num_fs++;
}
}
// Set the widget and label names, if there is a parent specified
if (parent_name != NULL) {
tcl_widget_name =
new char[strlen(parent_name) + strlen(tcl_varname) + 2];
if (tcl_widget_name == NULL) {
fprintf(stderr,"Tclvar_float_with_scale::Tclvar_float_with_scale(): Out of memory\n");
}
sprintf(tcl_widget_name,"%s.%s",parent_name,tcl_varname);
tcl_label_name =
new char[strlen(parent_name) + strlen(tcl_varname) +12];
if (tcl_label_name == NULL) {
fprintf(stderr,"Tclvar_float_with_scale::Tclvar_float_with_scale(): Out of memory\n");
}
sprintf(tcl_label_name,"%s.%slabel",parent_name,tcl_varname);
} else {
tcl_widget_name = NULL;
}
// Add and pack a floatscale if there is an interpreter and a widget
if ( (interpreter != NULL) && (tcl_widget_name != NULL) ) {
initialize(interpreter);
}
};
// Remove the entry from the list of active float Tcl variables.
// Base class destructor handles the rest.
Tclvar_float_with_scale::~Tclvar_float_with_scale()
{
register int i = 0;
// Unpack and destroy the floatscale and label, if there is an
// interpreter
if (interpreter != NULL) {
char command[1000];
sprintf(command,"destroy %s",tcl_widget_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
}
sprintf(command,"destroy %s",tcl_label_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
}
}
// Free the space for the widget name
if (tcl_widget_name != NULL) { delete [] tcl_widget_name; }
if (tcl_label_name != NULL) { delete [] tcl_label_name; }
// Find this entry in the list of variables
while ( (i < num_fs) && (fs_list[i] != this) ) { i++; };
if (i >= num_fs) {
fprintf(stderr,"Tclvar_float_with_scale::~Tclvar_float_with_scale(): Internal error -- not in list\n");
return;
}
// Move the last entry to this one
fs_list[i] = fs_list[num_fs-1];
// Reduce the number in the list
num_fs--;
};
// Add and pack a floatscale if there is an interpreter and a widget
// for each floatscale variable. Note that the change callback will
// be handled by the fact that it is derived from a float var.
// Set the value of the variable so it shows up on the display.
// Put in a label with the variable's name
int Tclvar_float_with_scale::initialize(Tcl_Interp *interpreter)
{
char command[1000];
char cvalue[100];
// Set the variable to its default value
sprintf(cvalue,"%g", myfloat);
Tcl_SetVar(interpreter, my_tcl_varname, cvalue, TCL_GLOBAL_ONLY);
// Create the floatscale
sprintf(command,"floatscale %s %g %g 300 0 1 %s",
tcl_widget_name, minvalue, maxvalue, my_tcl_varname);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
return(-1);
}
// Make the label for the floatscale
sprintf(tcl_label_name,"%slabel",tcl_widget_name);
sprintf(command,"label %s -text %s", tcl_label_name, my_tcl_varname);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
return(-1);
}
// Pack the label and floatscale
sprintf(command,"pack %s %s\n", tcl_label_name, tcl_widget_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
return(-1);
}
return 0;
}
Tclvar_list_of_strings::~Tclvar_list_of_strings()
{
// Delete all of the entries in the list.
// num_entries will be adjusted by each delete, which makes the
// loop control look strange.
while (num_entries)
Delete_entry(entries[0]);
// TCH 26 June 98
// better cleanup (for linux?)
while (num_selectors)
Delete_selector(selectors[0]);
}
// Returns 0 on success, 1 on list full, 2 on entry already in the list,
// -1 on other errors.
int Tclvar_list_of_strings::Add_entry(const char entry[])
{
int i;
// Make sure there is enough room for another entry
if (num_entries == 500) {
return(1);
}
// Make sure the entry is not already in the list
for (i = 0; i < num_entries; i++) {
if (strncmp(entries[i], entry, TCLVAR_STRING_LENGTH) == 0) {
return 2;
}
}
// Add the entry on the end of the list
strncpy(entries[num_entries], entry, TCLVAR_STRING_LENGTH);
num_entries++;
//fprintf(stderr, "Added \"%s\".\n", entry);
// If we have an interpreter and a widget, add the entry into the
// menu for each of the selectors that are interested in this list
if (interpreter) {
for (i = 0; i < num_selectors; i++) {
if (selectors[i]->tcl_widget_name) {
char command[1000];
Tclvar_selector *sel = selectors[i];
//fprintf(stderr, " Updating %s with the new entry.\n", sel->my_tcl_varname);
sprintf(command,"%s.menu add command -label {%s} -underline 0 -command \"set %s {%s}\"", sel->tcl_widget_name, entries[num_entries-1], sel->my_tcl_varname, entries[num_entries-1]);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
return(-1);
}
}
}
}
return 0;
}
// Returns 0 on success, 1 on string not found in list, -1 on other error
int Tclvar_list_of_strings::Delete_entry(const char entry[])
{
int i, s;
int retval = 0;
// Find the entry in the list, if we can
for (i = 0; i < num_entries; i++) {
if (strncmp(entries[i], entry, TCLVAR_STRING_LENGTH) == 0) {
break;
}
}
if (i == num_entries) { // Didn't find it!
return 1;
}
// If we have an interpreter, remove the entry from the menu for each
// of the selectors that are interested in this list.
if (interpreter) {
for (s = 0; s < num_selectors; s++) {
char command[1000];
Tclvar_selector *sel = selectors[s];
// TCH 30 Sep 97: added the "if (sel->tcl_widget_name)"
// since Add_entry has the same and the lack here was causing
// Tcl errors
if (sel->tcl_widget_name) {
sprintf(command, "%s.menu delete {%s}",
sel->tcl_widget_name, entries[i]);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
retval = -1;
}
}
}
}
// Remove the entry from the list by moving the last entry to its
// location and reducing the number of entries by one
strncpy(entries[i], entries[num_entries-1], TCLVAR_STRING_LENGTH);
num_entries--;
return retval;
}
// Returns 0 on success, 1 on list full, 2 on entry already in the list,
// -1 on other errors.
int Tclvar_list_of_strings::Add_selector(Tclvar_selector *sel)
{
int i;
// Make sure there is enough room for another entry
if (num_selectors == 500) {
return(1);
}
// Make sure the entry is not already in the list
for (i = 0; i < num_selectors; i++) {
if (selectors[i] == sel) {
return 2;
}
}
//if (sel->my_tcl_varname)
//fprintf(stderr, "Added selector \"%s\".\n", sel->my_tcl_varname);
//else
//fprintf(stderr, "Added anonymous selector.\n");
// Add the entry on the end of the list
selectors[num_selectors] = sel;
num_selectors++;
return 0;
}
// Returns 0 on success, 1 on selector not found in list, -1 on other error
int Tclvar_list_of_strings::Delete_selector(Tclvar_selector *sel)
{
int i;
if (!sel) return 1;
// Find the entry in the list, if we can
for (i = 0; i < num_selectors; i++) {
if (selectors[i] == sel) {
break;
}
}
if (i == num_selectors) { // Didn't find it!
return 1;
}
// Remove the entry from the list by moving the last entry to its
// location and reducing the number of entries by one
selectors[i] = selectors[num_selectors - 1];
num_selectors--;
// TCH 26 June 98
// better cleanup (for linux?)
sel->mylist = NULL;
return 0;
}
// Creates a selector, linking it to the list of strings that it
// uses to select from. Adds the selector to the list of selectors,
// so if we don't have an interpreter yet, it will be called when the
// interpreter is set.
// If we do have an interpreter, go ahead and build the menu for
// the variable and do all of the cross-linking to ensure that updates
// will happen.
Tclvar_selector::Tclvar_selector (const char * initial_value) :
my_tcl_varname (NULL),
tcl_widget_name (NULL),
tcl_label_name (NULL),
callback (NULL),
userdata (NULL),
mylist (NULL),
d_initialized (0)
{
if (num_sels >= (MAX_SELS-1)) {
fprintf(stderr, "Tclvar_selector: "
"Can't link to nameless variable\n");
fprintf(stderr," (Out of storage)\n");
return;
}
sel_list[num_sels] = this;
num_sels++;
Set(initial_value);
}
Tclvar_selector::Tclvar_selector (char * tcl_varname, char * parent_name,
Tclvar_list_of_strings * list, char * initial_value,
Linkvar_Selectcall c, void *ud) :
my_tcl_varname (NULL),
tcl_widget_name (NULL),
tcl_label_name (NULL),
mylist (NULL),
d_initialized (0)
{
callback = c;
userdata = ud;
//fprintf(stderr, "Tclvar_selector constructor\n");
tcl_widget_name = NULL;
// Put us on the list and set the Tcl variable name and parent name.
if (num_sels >= (MAX_SELS-1)) {
fprintf(stderr,"Tclvar_selector::Tclvar_selector(): Can't link to %s\n",
tcl_varname);
fprintf(stderr," (Out of storage)\n");
} else {
sel_list[num_sels] = this;
Set(initial_value);
num_sels++;
initializeTcl(tcl_varname, parent_name);
// Store which list of strings we are to use
// NULL list is legal and expected now,
// but we want to call this only if it's non-NULL.
if (list) bindList(list);
}
}
void Tclvar_selector::Set (const char *value)
{
strncpy(mystring, value, TCLVAR_STRING_LENGTH);
strncpy(mylaststring, value, TCLVAR_STRING_LENGTH);
}
Tclvar_selector::~Tclvar_selector (void)
{
int i = 0;
// Remove the trace callback, if there is an interpreter
if (interpreter) {
Tcl_UntraceVar(interpreter, my_tcl_varname,
TCL_TRACE_WRITES | TCL_GLOBAL_ONLY,
handle_string_value_change,
(ClientData)((void*)(this)));
}
// Free the space for the name
if (my_tcl_varname != NULL) { delete [] my_tcl_varname; }
// Unpack and destroy the menu and label, if there is an
// interpreter and a widget
if (interpreter && tcl_widget_name) {
char command[1000];
sprintf(command,"destroy %s",tcl_widget_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
}
sprintf(command,"destroy %s",tcl_label_name);
if (Tcl_Eval(interpreter, command) != TCL_OK) {
fprintf(stderr, "Tcl_Eval(%s) failed: %s\n", command,
interpreter->result);
}
}
// Free the space for the widget name
if (tcl_widget_name != NULL) { delete [] tcl_widget_name; tcl_widget_name = NULL; }
if (tcl_label_name != NULL) { delete [] tcl_label_name; tcl_label_name = NULL; }
// Remove this selector from its list of strings
if (mylist)
mylist->Delete_selector(this);
// Find this entry in the list of variables
while ( (i < num_sels) && (sel_list[i] != this) ) { i++; };
if (i >= num_sels) {
fprintf(stderr,"Tclvar_selector::~Tclvar_selector(): Internal error -- not in list\n");
return;
}
// Move the last entry to this one
sel_list[i] = sel_list[num_sels-1];
// Reduce the number in the list
num_sels--;
}
void Tclvar_selector::initializeTcl (const char * tcl_varname,
const char * parent_name) {
if (!tcl_varname) {
fprintf(stderr, "Tclvar_selector::initializeTcl: "
"NULL variable name!\n");
return;
}
my_tcl_varname = new char [strlen(tcl_varname) + 1];
if (my_tcl_varname == NULL) {
fprintf(stderr,"Tclvar_selector: Out of memory\n");
return;
}
strcpy(my_tcl_varname, tcl_varname);
// Set the widget and label names, if there is a parent specified
if (parent_name) {
tcl_widget_name =
new char [strlen(parent_name) + strlen(tcl_varname) + 2];
if (tcl_widget_name == NULL) {
fprintf(stderr,"Tclvar_selector: Out of memory\n");
return;
}
sprintf(tcl_widget_name,"%s.%s",parent_name,tcl_varname);
tcl_label_name =
new char[strlen(parent_name) + strlen(tcl_varname) +12];
if (tcl_label_name == NULL) {