OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tix/] [generic/] [tixCmds.c] - Blame information for rev 1778

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.