Debian Codegraph @

pTk/mTk/tixGeneric/tixCmds.c

raw symbols SCIP
1
2 /* $Id: tixCmds.c,v 1.1.1.1 2000/05/17 11:08:38 idiscovery Exp $ */
3
4 /*
5 * tixCmds.c --
6 *
7 * Implements various TCL commands for Tix.
8 *
9 * Copyright (c) 1996, Expert Interface Technologies
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 */
15
16 #include "tixPort.h"
17 #include "tixInt.h"
18 #include <math.h>
19
20 TIX_DECLARE_CMD(Tix_ParentWindow);
21
22 /*
23 * Maximum intensity for a color:
24 */
25
26 #define MAX_INTENSITY 65535
27
28 /*
29 * Data structure used by the tixDoWhenIdle command.
30 */
31 typedef struct {
32 Tcl_Interp * interp;
33 char * command;
34 Tk_Window tkwin;
35 } IdleStruct;
36
37 /*
38 * Data structures used by the tixDoWhenMapped command.
39 */
40 typedef struct _MapCmdLink {
41 char * command;
42 struct _MapCmdLink * next;
43 } MapCmdLink;
44
45 typedef struct {
46 Tcl_Interp * interp;
47 Tk_Window tkwin;
48 MapCmdLink * cmds;
49 } MapEventStruct;
50
51 /*
52 * Global vars
53 */
54 static Tcl_HashTable idleTable; /* hash table for TixDoWhenIdle */
55 static Tcl_HashTable mapEventTable; /* hash table for TixDoWhenMapped */
56
57
58 /*
59 * Functions used only in this file.
60 */
61 static void IdleHandler _ANSI_ARGS_((ClientData clientData));
62 static void EventProc _ANSI_ARGS_((ClientData clientData,
63 XEvent *eventPtr));
64 static void MapEventProc _ANSI_ARGS_((ClientData clientData,
65 XEvent *eventPtr));
66 static int IsOption _ANSI_ARGS_((char *option,
67 int optArgc, char **optArgv));
68 static XColor * ScaleColor _ANSI_ARGS_((Tk_Window tkwin,
69 XColor * color, double scale));
70 static char * NameOfColor _ANSI_ARGS_((XColor * colorPtr));
71
72
73 /*----------------------------------------------------------------------
74 * Tix_DoWhenIdle --
75 *
76 * The difference between "tixDoWhenIdle" and "after" is: the
77 * "after" handler is called after all other TK Idel Event
78 * Handler are called. Sometimes this will cause some toplevel
79 * windows to be mapped before the Idle Event Handler is
80 * executed.
81 *
82 * This behavior of "after" is not suitable for implementing
83 * geometry managers. Therefore I wrote "tixDoWhenIdle" which is
84 * an exact TCL interface for Tk_DoWhenIdle()
85 *----------------------------------------------------------------------
86 */
87
88 TIX_DEFINE_CMD(Tix_DoWhenIdleCmd)
89 {
90 int isNew;
91 char * command;
92 static int inited = 0;
93 IdleStruct * iPtr;
94 Tk_Window tkwin;
95 Tcl_HashEntry * hashPtr;
96
97 if (!inited) {
98 Tcl_InitHashTable(&idleTable, TCL_STRING_KEYS);
99 inited = 1;
100 }
101
102 /*
103 * parse the arguments
104 */
105 if (strncmp(argv[0], "tixWidgetDoWhenIdle", strlen(argv[0]))== 0) {
106 if (argc<3) {
107 return Tix_ArgcError(interp, argc, argv, 1,
108 "command window ?arg arg ...?");
109 }
110 /* tixWidgetDoWhenIdle reqires that the second argument must
111 * be the name of a mega widget
112 */
113 tkwin=Tk_NameToWindow(interp, argv[2], Tk_MainWindow(interp));
114 if (tkwin == NULL) {
115 return TCL_ERROR;
116 }
117 } else {
118 if (argc<2) {
119 return Tix_ArgcError(interp, argc, argv, 1,
120 "command ?arg arg ...?");
121 }
122 tkwin = NULL;
123 }
124
125 command = Tcl_Merge(argc-1, argv+1);
126
127 hashPtr = Tcl_CreateHashEntry(&idleTable, command, &isNew);
128
129 if (!isNew) {
130 ckfree(command);
131 } else {
132 iPtr = (IdleStruct *) ckalloc(sizeof(IdleStruct));
133 iPtr->interp = interp;
134 iPtr->command = command;
135 iPtr->tkwin = tkwin;
136
137 Tcl_SetHashValue(hashPtr, (char*)iPtr);
138
139 if (tkwin) {
140 /* we just want one event handler for all idle events
141 * associated with a window. This is done by first calling
142 * Delete and then Create EventHandler.
143 */
144 Tk_DeleteEventHandler(tkwin, StructureNotifyMask, EventProc,
145 (ClientData)tkwin);
146 Tk_CreateEventHandler(tkwin, StructureNotifyMask, EventProc,
147 (ClientData)tkwin);
148 }
149
150 Tk_DoWhenIdle(IdleHandler, (ClientData) iPtr);
151 }
152
153 return TCL_OK;
154 }
155
156 /*----------------------------------------------------------------------
157 * Tix_DoWhenMapped
158 *
159 * Arranges a command to be called when the window received an
160 * <Map> event.
161 *
162 * argv[1..] = command argvs
163 *
164 *----------------------------------------------------------------------
165 */
166 TIX_DEFINE_CMD(Tix_DoWhenMappedCmd)
167 {
168 Tcl_HashEntry * hashPtr;
169 int isNew;
170 MapEventStruct * mPtr;
171 MapCmdLink * cmd;
172 Tk_Window tkwin;
173 static int inited = 0;
174
175 if (argc!=3) {
176 return Tix_ArgcError(interp, argc, argv, 1, " pathname command");
177 }
178
179 tkwin = Tk_NameToWindow(interp, argv[1], Tk_MainWindow(interp));
180 if (tkwin == NULL) {
181 return TCL_ERROR;
182 }
183
184 if (!inited) {
185 Tcl_InitHashTable(&mapEventTable, TCL_ONE_WORD_KEYS);
186 inited = 1;
187 }
188
189 hashPtr = Tcl_CreateHashEntry(&mapEventTable, (char*)tkwin, &isNew);
190
191 if (!isNew) {
192 mPtr = (MapEventStruct*) Tcl_GetHashValue(hashPtr);
193 } else {
194 mPtr = (MapEventStruct*) ckalloc(sizeof(MapEventStruct));
195 mPtr->interp = interp;
196 mPtr->tkwin = tkwin;
197 mPtr->cmds = 0;
198
199 Tcl_SetHashValue(hashPtr, (char*)mPtr);
200
201 Tk_CreateEventHandler(tkwin, StructureNotifyMask,
202 MapEventProc, (ClientData)mPtr);
203 }
204
205 /*
206 * Add this into a link list
207 */
208 cmd = (MapCmdLink*) ckalloc(sizeof(MapCmdLink));
209 cmd->command = tixStrDup(argv[2]);
210
211 cmd->next = mPtr->cmds;
212 mPtr->cmds = cmd;
213
214 return TCL_OK;
215 }
216
217 /*----------------------------------------------------------------------
218 * Tix_FalseCmd --
219 *
220 * Returns a false value regardless of the arguments. This is used to
221 * skip run-time debugging
222 *----------------------------------------------------------------------
223 */
224
225 TIX_DEFINE_CMD(Tix_FalseCmd)
226 {
227 Tcl_SetResult(interp, "0",TCL_STATIC);
228 return TCL_OK;
229 }
230
231 /*----------------------------------------------------------------------
232 * Tix_FileCmd --
233 *
234 * (obsolete)
235 *----------------------------------------------------------------------
236 */
237
238 TIX_DEFINE_CMD(Tix_FileCmd)
239 {
240 char *expandedFileName;
241 Tcl_DString buffer;
242 size_t len;
243
244 if (argc!=3) {
245 return Tix_ArgcError(interp, argc, argv, 1, "option filename");
246 }
247 len = strlen(argv[1]);
248 if (argv[1][0]=='t' && strncmp(argv[1], "tildesubst", len)==0) {
249
250 expandedFileName = Tcl_TildeSubst(interp, argv[2], &buffer);
251 Tcl_ResetResult(interp);
252 if (expandedFileName == NULL) {
253 Tcl_AppendResult(interp, argv[2], NULL);
254 } else {
255 Tcl_AppendResult(interp, expandedFileName, NULL);
256 Tcl_DStringFree(&buffer); /* Was initialized by Tcl_TildeSubst */
257 }
258
259 return TCL_OK;
260 }
261 else if (argv[1][0]=='t' && strncmp(argv[1], "trimslash", len)==0) {
262 /*
263 * Compress the extra "/"
264 */
265 char *src, *dst, *p;
266 int isSlash = 0;
267
268 p = tixStrDup(argv[2]);
269
270 for (src=dst=p; *src; src++) {
271 if (*src == '/') {
272 if (!isSlash) {
273 *dst++ = *src;
274 isSlash = 1;
275 }
276 } else {
277 *dst++ = *src;
278 isSlash = 0;
279 }
280 }
281 * dst = '\0';
282
283 if (dst > p) {
284 /*
285 * Trim the tariling "/", but only if this filename is not "/"
286 */
287 -- dst;
288 if (*dst == '/') {
289 if (dst != p) {
290 * dst = '\0';
291 }
292 }
293 }
294 Tcl_SetResult(interp, p, TCL_DYNAMIC);
295 return TCL_OK;
296 }
297
298 Tcl_AppendResult(interp, "unknown option \"", argv[1],
299 "\", must be tildesubst or trimslash", NULL);
300 return TCL_ERROR;
301 }
302
303 /*----------------------------------------------------------------------
304 * Tix_Get3DBorderCmd
305 *
306 * Returns the upper and lower border shades of a color. Returns then
307 * in a list of two X color names.
308 *
309 * The color is not very useful if the display is a mono display:
310 * it will just return black and white. So a clever program may
311 * want to check the [tk colormodel] and if it is mono, then
312 * dither using a bitmap.
313 *----------------------------------------------------------------------
314 */
315 TIX_DEFINE_CMD(Tix_Get3DBorderCmd)
316 {
317 XColor * color, * light, * dark;
318 Tk_Window tkwin;
319 Tk_Uid colorUID;
320
321 if (argc != 2) {
322 return Tix_ArgcError(interp, argc, argv, 0, "colorName");
323 }
324
325 tkwin = Tk_MainWindow(interp);
326
327 colorUID = Tk_GetUid(argv[1]);
328 color = Tk_GetColor(interp, tkwin, colorUID);
329 if (color == NULL) {
330 return TCL_ERROR;
331 }
332
333 if ((light = ScaleColor(tkwin, color, 1.4)) == NULL) {
334 return TCL_ERROR;
335 }
336 if ((dark = ScaleColor(tkwin, color, 0.6)) == NULL) {
337 return TCL_ERROR;
338 }
339
340 Tcl_ResetResult(interp);
341 Tcl_AppendElement(interp, NameOfColor(light));
342 Tcl_AppendElement(interp, NameOfColor(dark));
343
344 Tk_FreeColor(color);
345 Tk_FreeColor(light);
346 Tk_FreeColor(dark);
347
348 return TCL_OK;
349 }
350
351 /*----------------------------------------------------------------------
352 * Tix_GetBooleanCmd
353 *
354 * Return "1" if is a true boolean number. "0" otherwise
355 *
356 * argv[1] = string to test
357 *----------------------------------------------------------------------
358 */
359 TIX_DEFINE_CMD(Tix_GetBooleanCmd)
360 {
361 int value;
362 int nocomplain = 0;
363 char *string;
364 static char *results[2] = {"0", "1"};
365
366 if (argc == 3) {
367 if (strcmp(argv[1], "-nocomplain") != 0) {
368 goto error;
369 }
370 nocomplain = 1;
371 string = argv[2];
372 }
373 else if (argc != 2) {
374 goto error;
375 }
376 else {
377 string = argv[1];
378 }
379
380 if (Tcl_GetBooleanFromObj(interp, string, &value) != TCL_OK) {
381 if (nocomplain) {
382 value = 0;
383 }
384 else {
385 return TCL_ERROR;
386 }
387 }
388
389 Tcl_SetResult(interp, results[value], TCL_STATIC);
390 return TCL_OK;
391
392 error:
393 return Tix_ArgcError(interp, argc, argv, 1, "?-nocomplain? string");
394 }
395
396 /*----------------------------------------------------------------------
397 * Tix_GetIntCmd
398 *
399 * Return "1" if is a true boolean number. "0" otherwise
400 *
401 * argv[1] = string to test
402 *----------------------------------------------------------------------
403 */
404 TIX_DEFINE_CMD(Tix_GetIntCmd)
405 {
406 int i;
407 int opTrunc = 0;
408 int opNocomplain = 0;
409 int i_value;
410 double f_value;
411 char * string = 0;
412 char buff[20];
413
414 for (i=1; i<argc; i++) {
415 if (strcmp(argv[i], "-nocomplain") == 0) {
416 opNocomplain = 1;
417 }
418 else if (strcmp(argv[i], "-trunc") == 0) {
419 opTrunc = 1;
420 }
421 else {
422 string = argv[i];
423 break;
424 }
425 }
426 if (i != argc-1) {
427 return Tix_ArgcError(interp, argc, argv, 1,
428 "?-nocomplain? ?-trunc? string");
429 }
430
431 if (Tcl_GetIntFromObj(interp, string, &i_value) == TCL_OK) {
432 ;
433 }
434 else if (Tcl_GetDoubleFromObj(interp, string, &f_value) == TCL_OK) {
435 #if 0
436 /* Some machines don't have the "trunc" function */
437 if (opTrunc) {
438 i_value = (int) trunc(f_value);
439 }
440 else {
441 i_value = (int) f_value;
442 }
443 #else
444 i_value = (int) f_value;
445 #endif
446 }
447 else if (opNocomplain) {
448 i_value = 0;
449 }
450 else {
451 Tcl_ResetResult(interp);
452 Tcl_AppendResult(interp, "\"", string,
453 "\" is not a valid numerical value", NULL);
454 return TCL_ERROR;
455 }
456
457 sprintf(buff, "%d", i_value);
458 Tcl_SetResult(interp, buff, TCL_VOLATILE);
459 return TCL_OK;
460 }
461
462 /*----------------------------------------------------------------------
463 * Tix_HandleOptionsCmd
464 *
465 *
466 * argv[1] = recordName
467 * argv[2] = validOptions
468 * argv[3] = argList
469 * if (argv[3][0] == "-nounknown") then
470 * don't complain about unknown options
471 *----------------------------------------------------------------------
472 */
473 TIX_DEFINE_CMD(Tix_HandleOptionsCmd)
474 {
475 int listArgc;
476 int optArgc;
477 char ** listArgv = 0;
478 char ** optArgv = 0;
479 int i, code = TCL_OK;
480 int noUnknown = 0;
481
482 if (argc >= 2 && (strcmp(argv[1], "-nounknown") == 0)) {
483 noUnknown = 1;
484 argv[1] = argv[0];
485 argc --;
486 argv ++;
487 }
488
489 if (argc!=4) {
490 return Tix_ArgcError(interp, argc, argv, 2, "w validOptions argList");
491 }
492
493 if (Tcl_SplitList(interp, argv[2], &optArgc, &optArgv ) != TCL_OK) {
494 code = TCL_ERROR;
495 goto done;
496 }
497 if (Tcl_SplitList(interp, argv[3], &listArgc, &listArgv) != TCL_OK) {
498 code = TCL_ERROR;
499 goto done;
500 }
501
502 if ((listArgc %2) == 1) {
503 if (noUnknown || IsOption(listArgv[listArgc-1], optArgc, optArgv)) {
504 Tcl_AppendResult(interp, "value for \"", listArgv[listArgc-1],
505 "\" missing", (char*)NULL);
506 } else {
507 Tcl_AppendResult(interp, "unknown option \"", listArgv[listArgc-1],
508 "\"", (char*)NULL);
509 }
510 code = TCL_ERROR;
511 goto done;
512 }
513 for (i=0; i<listArgc; i+=2) {
514 if (IsOption(listArgv[i], optArgc, optArgv)) {
515 Tcl_SetVar2(interp, argv[1], listArgv[i], listArgv[i+1], 0);
516 }
517 else if (!noUnknown) {
518 Tcl_AppendResult(interp, "unknown option \"", listArgv[i],
519 "\"; must be one of \"", argv[2], "\".", NULL);
520 code = TCL_ERROR;
521 goto done;
522 }
523 }
524
525 done:
526
527 if (listArgv) {
528 ckfree((char *) listArgv);
529 }
530 if (optArgv) {
531 ckfree((char *) optArgv);
532 }
533
534 return code;
535 }
536
537 /*----------------------------------------------------------------------
538 * Tix_SetWindowParent --
539 *
540 * Sets the parent of a window. This is normally to change the
541 * state of toolbar and MDI windows between docking and free
542 * modes.
543 *
544 * Results:
545 * Standard Tcl results.
546 *
547 * Side effects:
548 * Windows may be re-parented. See user documentation.
549 *----------------------------------------------------------------------
550 */
551
552 TIX_DEFINE_CMD(Tix_ParentWindow)
553 {
554 Tk_Window mainWin, tkwin, newParent;
555 int parentId;
556
557 if (argc != 3) {
558 return Tix_ArgcError(interp, argc, argv, 1, "window parent");
559 }
560 mainWin = Tk_MainWindow(interp);
561 if (mainWin == NULL) {
562 Tcl_SetResult(interp, "interpreter does not have a main window",
563 TCL_STATIC);
564 return TCL_ERROR;
565 }
566
567 tkwin = Tk_NameToWindow(interp, argv[1], mainWin);
568 if (tkwin == NULL) {
569 return TCL_ERROR;
570 }
571
572 newParent = Tk_NameToWindow(interp, argv[2], mainWin);
573 if (newParent == NULL) {
574 if (Tcl_GetIntFromObj(interp, argv[2], &parentId) != TCL_OK) {
575 Tcl_ResetResult(interp);
576 Tcl_AppendResult(interp, "\"", argv[2],
577 "\" must be a window pathname or ID", NULL);
578 return TCL_ERROR;
579 }
580 }
581
582 return TixpSetWindowParent(interp, tkwin, newParent, parentId);
583 }
584
585 /*----------------------------------------------------------------------
586 * Tix_StrEqCmd --
587 *
588 * To test string equality. It is more readable to write
589 * if [tixStrEq $var1 $var2]
590 * than
591 * if ![string comp $var1 $var2]
592 *
593 *----------------------------------------------------------------------
594 */
595
596 TIX_DEFINE_CMD(Tix_StrEqCmd)
597 {
598 if (argc != 3) {
599 return Tix_ArgcError(interp, argc, argv, 1, "string1 string2");
600 }
601 if (strcmp(argv[1], argv[2]) == 0) {
602 Tcl_SetResult(interp, "1", TCL_STATIC);
603 } else {
604 Tcl_SetResult(interp, "0", TCL_STATIC);
605 }
606 return TCL_OK;
607 }
608
609 /*----------------------------------------------------------------------
610 * Tix_StringSubCmd --
611 *
612 * What does this do??
613 *----------------------------------------------------------------------
614 */
615
616 TIX_DEFINE_CMD(Tix_StringSubCmd)
617 {
618 Tcl_DString buffer;
619 char * str, *from, *to, *s, *e, *f;
620 int n, m, l, k;
621 int inited = 0;
622
623 if (argc!=4) {
624 return Tix_ArgcError(interp, argc, argv, 1, "strVar from to");
625 }
626 if ((str = Tcl_GetVar(interp, argv[1], 0)) == NULL) {
627 Tcl_AppendResult(interp, "variable ", argv[1]," does not exist", NULL);
628 return TCL_ERROR;
629 }
630 from = argv[2];
631 to = argv[3];
632
633 n = strlen(from);
634 l = strlen(to);
635
636 while (1) {
637 s = str;
638 k = 0;
639
640 while (*s && *s != *from) {
641 /* Find the beginning of token */
642 s++; k++;
643 }
644
645 if (*s && *s == *from) {
646 for (m=0,e=s,f=from; *e && *f && *e==*f && m<n; e++,f++,m++) {
647 ;
648 }
649 if (!inited) {
650 Tcl_DStringInit(&buffer);
651 inited = 1;
652 }
653 if (m == n) {
654 /* We found a match */
655 if (s > str) {
656 /* copy the unmatched prefix */
657 Tcl_DStringAppend(&buffer, str, k);
658 }
659 Tcl_DStringAppend(&buffer, to, l);
660 str = e;
661 } else {
662 Tcl_DStringAppend(&buffer, str, k+m);
663 str += k+m;
664 }
665 continue;
666 }
667
668 /* No match at all */
669 if (*str) {
670 if (inited) {
671 Tcl_DStringAppend(&buffer, str, k);
672 }
673 }
674 break;
675 }
676
677 if (inited) {
678 Tcl_SetVar(interp, argv[1], buffer.string, 0);
679 Tcl_DStringFree(&buffer);
680 }
681 return TCL_OK;
682 }
683
684 /*----------------------------------------------------------------------
685 * Tix_TmpLineCmd
686 *
687 * Draw a temporary line on the root window
688 *
689 * argv[1..] = x1 y1 x2 y2
690 *----------------------------------------------------------------------
691 */
692 TIX_DEFINE_CMD(Tix_TmpLineCmd)
693 {
694 Tk_Window mainWin = (Tk_Window)clientData;
695 Tk_Window tkwin;
696 int x1, y1, x2, y2;
697
698 if (argc != 5 && argc != 6) {
699 return Tix_ArgcError(interp, argc, argv, 0,
700 "tixTmpLine x1 y1 x2 y2 ?window?");
701 }
702 if (Tcl_GetIntFromObj(interp, argv[1], &x1) != TCL_OK) {
703 return TCL_ERROR;
704 }
705 if (Tcl_GetIntFromObj(interp, argv[2], &y1) != TCL_OK) {
706 return TCL_ERROR;
707 }
708 if (Tcl_GetIntFromObj(interp, argv[3], &x2) != TCL_OK) {
709 return TCL_ERROR;
710 }
711 if (Tcl_GetIntFromObj(interp, argv[4], &y2) != TCL_OK) {
712 return TCL_ERROR;
713 }
714 if (argc == 6) {
715 /*
716 * argv[5] tells which display to draw the tmp lines on, in
717 * case the application has opened more than one displays. If
718 * this argv[5] is omitted, draws to the display where the
719 * main window is on.
720 */
721 tkwin = Tk_NameToWindow(interp, argv[5], mainWin);
722 if (tkwin == NULL) {
723 return TCL_ERROR;
724 }
725 } else {
726 tkwin = Tk_MainWindow(interp);
727 }
728
729 TixpDrawTmpLine(x1, y1, x2, y2, tkwin);
730 return TCL_OK;
731 }
732
733 /*----------------------------------------------------------------------
734 * Tix_TrueCmd
735 *
736 * Returns a true value regardless of the arguments. This is used to
737 * skip run-time debugging
738 *----------------------------------------------------------------------
739 */
740
741 TIX_DEFINE_CMD(Tix_TrueCmd)
742 {
743 Tcl_SetResult(interp, "1",TCL_STATIC);
744 return TCL_OK;
745 }
746
747 /*----------------------------------------------------------------------
748 * EventProc --
749 *
750 * Monitors events sent to a window associated with a
751 * tixWidgetDoWhenIdle command. If this window is destroyed,
752 * remove the idle handlers associated with this window.
753 *----------------------------------------------------------------------
754 */
755
756 static void EventProc(clientData, eventPtr)
757 ClientData clientData;
758 XEvent *eventPtr;
759 {
760 Tk_Window tkwin = (Tk_Window)clientData;
761 Tcl_HashSearch hSearch;
762 Tcl_HashEntry * hashPtr;
763 IdleStruct * iPtr;
764
765 if (eventPtr->type != DestroyNotify) {
766 return;
767 }
768
769 /* Iterate over all the entries in the hash table */
770 for (hashPtr = Tcl_FirstHashEntry(&idleTable, &hSearch);
771 hashPtr;
772 hashPtr = Tcl_NextHashEntry(&hSearch)) {
773
774 iPtr = (IdleStruct *)Tcl_GetHashValue(hashPtr);
775
776 if (iPtr->tkwin == tkwin) {
777 Tcl_DeleteHashEntry(hashPtr);
778 Tk_CancelIdleCall(IdleHandler, (ClientData) iPtr);
779 ckfree((char*)iPtr->command);
780 ckfree((char*)iPtr);
781 }
782 }
783 }
784 /*----------------------------------------------------------------------
785 * IdleHandler --
786 *
787 * Called when Tk is idle. Dispatches all commands registered by
788 * tixDoWhenIdle and tixWidgetDoWhenIdle.
789 *----------------------------------------------------------------------
790 */
791
792 static void IdleHandler(clientData)
793 ClientData clientData; /* TCL command to evaluate */
794 {
795 Tcl_HashEntry * hashPtr;
796 IdleStruct * iPtr;
797
798 iPtr = (IdleStruct *) clientData;
799
800 /*
801 * Clean up the hash table. Note that we have to do this BEFORE
802 * calling the TCL command. Otherwise if the TCL command tries
803 * to register itself again, it will fail in Tix_DoWhenIdleCmd()
804 * because the command is still in the hashtable
805 */
806 hashPtr = Tcl_FindHashEntry(&idleTable, iPtr->command);
807 if (hashPtr) {
808 Tcl_DeleteHashEntry(hashPtr);
809 } else {
810 /* Probably some kind of error */
811 return;
812 }
813
814 if (Tcl_GlobalEval(iPtr->interp, iPtr->command) != TCL_OK) {
815 if (iPtr->tkwin != NULL) {
816 Tcl_AddErrorInfo(iPtr->interp,
817 "\n (idle event handler executed by tixWidgetDoWhenIdle)");
818 } else {
819 Tcl_AddErrorInfo(iPtr->interp,
820 "\n (idle event handler executed by tixDoWhenIdle)");
821 }
822 Tk_BackgroundError(iPtr->interp);
823 }
824
825 ckfree((char*)iPtr->command);
826 ckfree((char*)iPtr);
827 }
828
829 /*----------------------------------------------------------------------
830 * IsOption --
831 *
832 * Checks whether the string pointed by "option" is one of the
833 * options given by the "optArgv" array.
834 *----------------------------------------------------------------------
835 */
836 static int IsOption(option, optArgc, optArgv)
837 char *option; /* Number of arguments. */
838 int optArgc; /* Number of arguments. */
839 char **optArgv; /* Argument strings. */
840 {
841 int i;
842
843 for (i=0; i<optArgc; i++) {
844 if (strcmp(option, optArgv[i]) == 0) {
845 return 1;
846 }
847 }
848 return 0;
849 }
850
851
852 static void MapEventProc(clientData, eventPtr)
853 ClientData clientData; /* TCL command to evaluate */
854 XEvent *eventPtr; /* Information about event. */
855 {
856 Tcl_HashEntry * hashPtr;
857 MapEventStruct * mPtr;
858 MapCmdLink * cmd;
859
860 if (eventPtr->type != MapNotify) {
861 return;
862 }
863
864 mPtr = (MapEventStruct *) clientData;
865
866 Tk_DeleteEventHandler(mPtr->tkwin, StructureNotifyMask,
867 MapEventProc, (ClientData)mPtr);
868
869 /* Clean up the hash table.
870 */
871 if ((hashPtr = Tcl_FindHashEntry(&mapEventTable, (char*)mPtr->tkwin))) {
872 Tcl_DeleteHashEntry(hashPtr);
873 }
874
875 for (cmd = mPtr->cmds; cmd; ) {
876 MapCmdLink * old;
877
878 /* Execute the event handler */
879 if (Tcl_GlobalEval(mPtr->interp, cmd->command) != TCL_OK) {
880 Tcl_AddErrorInfo(mPtr->interp,
881 "\n (event handler executed by tixDoWhenMapped)");
882 Tk_BackgroundError(mPtr->interp);
883 }
884
885 /* Delete the link */
886 old = cmd;
887 cmd = cmd->next;
888
889 ckfree(old->command);
890 ckfree((char*)old);
891 }
892
893 /* deallocate the mapEventStruct */
894 ckfree((char*)mPtr);
895 }
896
897 static char *
898 NameOfColor(colorPtr)
899 XColor * colorPtr;
900 {
901 static char string[20];
902 char *ptr;
903
904 sprintf(string, "#%4x%4x%4x", colorPtr->red, colorPtr->green,
905 colorPtr->blue);
906
907 for (ptr = string; *ptr; ptr++) {
908 if (*ptr == ' ') {
909 *ptr = '0';
910 }
911 }
912 return string;
913 }
914
915
916 static XColor *
917 ScaleColor(tkwin, color, scale)
918 Tk_Window tkwin;
919 XColor * color;
920 double scale;
921 {
922 XColor test;
923
924 test.red = (int)((float)(color->red) * scale);
925 test.green = (int)((float)(color->green) * scale);
926 test.blue = (int)((float)(color->blue) * scale);
927 if (test.red > MAX_INTENSITY) {
928 test.red = MAX_INTENSITY;
929 }
930 if (test.green > MAX_INTENSITY) {
931 test.green = MAX_INTENSITY;
932 }
933 if (test.blue > MAX_INTENSITY) {
934 test.blue = MAX_INTENSITY;
935 }
936
937 return Tk_GetColorByValue(tkwin, &test);
938 }