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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tix/] [generic/] [tixWidget.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tixWidget.c --
3
 *
4
 *      Constructs Tix-based compound widgets
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 <tclInt.h>
14
#include <tixInt.h>
15
#include <tixItcl.h>
16
 
17
static int                      ParseOptions _ANSI_ARGS_((
18
                                    Tcl_Interp * interp,TixClassRecord * cPtr,
19
                                    char *widRec, int argc, char** argv));
20
 
21
TIX_DECLARE_CMD(Tix_InstanceCmd);
22
 
23
/*----------------------------------------------------------------------
24
 * Tix_CreateWidgetCmd
25
 *
26
 *      Create an instance object of a Tix widget class.
27
 *
28
 * argv[0]  = object name.
29
 * argv[1+] = args
30
 *----------------------------------------------------------------------
31
 */
32
TIX_DEFINE_CMD(Tix_CreateWidgetCmd)
33
{
34
    TixClassRecord * cPtr =(TixClassRecord *)clientData;
35
    TixConfigSpec * spec;
36
    char * widRec = NULL;
37
    char * rootCmd = NULL;
38
    char * tmpArgv[3];
39
    char * value;
40
    int i;
41
    int code = TCL_OK;
42
    Tk_Window mainWin = Tk_MainWindow(interp);
43
    Tcl_DString ds;
44
 
45
    DECLARE_ITCL_NAMESP(nameSp, interp);
46
 
47
    if (argc <= 1) {
48
        return Tix_ArgcError(interp, argc, argv, 1, "pathname ?arg? ...");
49
    } else {
50
        widRec = argv[1];
51
    }
52
 
53
    if (Tk_NameToWindow(interp, widRec, mainWin) != NULL) {
54
        Tcl_ResetResult(interp);
55
        Tcl_AppendResult(interp, "window name \"", widRec,
56
            "\" already exists", NULL);
57
        return TCL_ERROR;
58
    } else {
59
        Tcl_ResetResult(interp);
60
    }
61
 
62
    if (!TixItclSetGlobalNameSp(&nameSp, interp)) {
63
        code = TCL_ERROR;
64
        goto done;
65
    }
66
 
67
    /*
68
     * Before doing anything, let's reset the TCL result, errorInfo,
69
     * errorCode, etc.
70
     */
71
    Tcl_SetVar2(interp, "errorInfo", NULL, "", TCL_GLOBAL_ONLY);
72
    Tcl_SetVar2(interp, "errorCode", NULL, "", TCL_GLOBAL_ONLY);
73
    Tcl_ResetResult(interp);
74
 
75
    /* Set up the widget record */
76
    rootCmd = ckalloc(strlen(widRec)+10);
77
    sprintf(rootCmd, "%s:root", widRec);
78
    Tcl_SetVar2(interp, widRec, "className", cPtr->className, TCL_GLOBAL_ONLY);
79
    Tcl_SetVar2(interp, widRec, "ClassName", cPtr->ClassName, TCL_GLOBAL_ONLY);
80
    Tcl_SetVar2(interp, widRec, "context",   cPtr->className, TCL_GLOBAL_ONLY);
81
    Tcl_SetVar2(interp, widRec, "w:root",    widRec,          TCL_GLOBAL_ONLY);
82
    Tcl_SetVar2(interp, widRec, "rootCmd",   rootCmd,         TCL_GLOBAL_ONLY);
83
 
84
    /* We need to create the root widget in order to parse the options
85
     * database
86
     */
87
    if (Tix_CallMethod(interp, cPtr->className, widRec, "CreateRootWidget",
88
            argc-2, argv+2) != TCL_OK) {
89
        code = TCL_ERROR;
90
        goto done;
91
    }
92
 
93
    /* Parse the options specified in the option database and supplied
94
     * in the command line.
95
     */
96
    Tcl_ResetResult(interp);
97
    if (ParseOptions(interp, cPtr, widRec, argc-2, argv+2) != TCL_OK) {
98
        code = TCL_ERROR;
99
        goto done;
100
    }
101
 
102
    /* Rename the root widget command and create a new TCL command for
103
     * this widget
104
     */
105
 
106
#ifndef TK_8_0_OR_LATER
107
    tmpArgv[0] = "rename";
108
    tmpArgv[1] = widRec;
109
    tmpArgv[2] = rootCmd;
110
 
111
    if (Tcl_RenameCmd((ClientData)0, interp, 3, tmpArgv) != TCL_OK) {
112
        code = TCL_ERROR;
113
        goto done;
114
    }
115
#else
116
    Tcl_DStringInit(&ds);
117
    Tcl_DStringAppendElement(&ds, "rename");
118
    Tcl_DStringAppendElement(&ds, widRec);
119
    Tcl_DStringAppendElement(&ds, rootCmd);
120
 
121
    if (Tcl_Eval(interp, ds.string) != TCL_OK) {
122
        Tcl_DStringFree(&ds);
123
        code = TCL_ERROR;
124
        goto done;
125
    } else {
126
        Tcl_DStringFree(&ds);
127
    }
128
#endif
129
 
130
    Tcl_CreateCommand(interp, widRec, Tix_InstanceCmd,
131
        (ClientData)cPtr, NULL);
132
 
133
    /* Now call the initialization methods defined by the Tix Intrinsics
134
     */
135
    if (Tix_CallMethod(interp, cPtr->className, widRec, "InitWidgetRec",
136
            0, 0) != TCL_OK) {
137
        code = TCL_ERROR;
138
        goto done;
139
    }
140
 
141
    if (Tix_CallMethod(interp, cPtr->className, widRec, "ConstructWidget",
142
            0, 0) != TCL_OK) {
143
        code = TCL_ERROR;
144
        goto done;
145
    }
146
 
147
    if (Tix_CallMethod(interp, cPtr->className, widRec, "SetBindings",
148
                0, 0) != TCL_OK) {
149
        code = TCL_ERROR;
150
        goto done;
151
    }
152
 
153
    /* The widget has been successfully initialized. Now call the config
154
     * method for all -forceCall options
155
     */
156
    for (i=0; i<cPtr->nSpecs; i++) {
157
        spec = cPtr->specs[i];
158
        if (spec->forceCall) {
159
            value = Tcl_GetVar2(interp, widRec, spec->argvName,
160
                TCL_GLOBAL_ONLY);
161
            if (Tix_CallConfigMethod(interp, cPtr, widRec, spec,
162
                    value)!=TCL_OK){
163
                code = TCL_ERROR;
164
                goto done;
165
            }
166
        }
167
    }
168
 
169
    Tcl_SetResult(interp, widRec, TCL_VOLATILE);
170
 
171
  done:
172
 
173
    if (code != TCL_OK) {
174
        /* %% TCL CORE USED !! %% */
175
        Interp *iPtr = (Interp *) interp;
176
        char * oldResult, * oldErrorInfo, * oldErrorCode;
177
        Tk_Window topLevel, tkwin;
178
 
179
        /* We need to save the old error message because
180
         * interp->result may be changed by some of the following function
181
         * calls.
182
         */
183
        if (interp->result) {
184
            oldResult = (char*)tixStrDup(interp->result);
185
#if 0
186
            printf("%s -->\n%s\n", widRec, oldResult);
187
#endif
188
        } else {
189
            oldResult = NULL;
190
        }
191
        oldErrorInfo = Tcl_GetVar2(interp, "errorInfo", NULL, TCL_GLOBAL_ONLY);
192
        oldErrorCode = Tcl_GetVar2(interp, "errorCode", NULL, TCL_GLOBAL_ONLY);
193
 
194
        Tcl_ResetResult(interp);
195
 
196
        /* (1) window */
197
        topLevel = cPtr->mainWindow;
198
 
199
        if (widRec != NULL) {
200
            Display *display = NULL;
201
 
202
            tkwin = Tk_NameToWindow(interp, widRec, topLevel);
203
            if (tkwin != NULL) {
204
                display = Tk_Display(tkwin);
205
                Tk_DestroyWindow(tkwin);
206
            }
207
 
208
            /* (2) widget command + root command */
209
            Tcl_DeleteCommand(interp, widRec);
210
            Tcl_DeleteCommand(interp, rootCmd);
211
 
212
            /* (3) widget record */
213
            Tcl_UnsetVar(interp, widRec, TCL_GLOBAL_ONLY);
214
 
215
            if (display) {
216
#ifndef _WINDOWS
217
                XSync(display, False);
218
#endif
219
                while (1) {
220
                    if (Tk_DoOneEvent(TK_X_EVENTS|TK_DONT_WAIT) == 0) {
221
                        break;
222
                    }
223
                }
224
            }
225
        }
226
        if (oldResult) {
227
            Tcl_SetResult(interp, oldResult, TCL_DYNAMIC);
228
        }
229
        if (oldErrorInfo && *oldErrorInfo) {
230
            Tcl_SetVar2(interp, "errorInfo", NULL, oldErrorInfo,
231
                TCL_GLOBAL_ONLY);
232
        } else {
233
            Tcl_SetVar2(interp, "errorInfo", NULL, oldResult,
234
                TCL_GLOBAL_ONLY);
235
        }
236
        if (oldErrorCode) {
237
            Tcl_SetVar2(interp, "errorCode", NULL, oldErrorCode,
238
                TCL_GLOBAL_ONLY);
239
        }
240
        iPtr->flags |= ERR_IN_PROGRESS;
241
    }
242
    if (rootCmd) {
243
        ckfree(rootCmd);
244
    }
245
 
246
    TixItclRestoreGlobalNameSp(&nameSp, interp);
247
 
248
    return code;
249
}
250
 
251
/*----------------------------------------------------------------------
252
 * Subroutines for object instantiation.
253
 *
254
 *
255
 *----------------------------------------------------------------------
256
 */
257
static int ParseOptions(interp, cPtr, widRec, argc, argv)
258
    Tcl_Interp * interp;
259
    TixClassRecord * cPtr;
260
    char *widRec;
261
    int argc;
262
    char** argv;
263
{
264
    int i;
265
    TixConfigSpec *spec;
266
    Tk_Window tkwin;
267
    char * value;
268
 
269
    if ((argc %2) != 0) {
270
        Tcl_AppendResult(interp, "missing argument for \"", argv[argc-1],
271
            "\"", NULL);
272
        return TCL_ERROR;
273
    }
274
 
275
    if ((tkwin = Tk_NameToWindow(interp, widRec, cPtr->mainWindow)) == NULL) {
276
        return TCL_ERROR;
277
    }
278
 
279
    /* Set all specs by their default values */
280
    for (i=0; i<cPtr->nSpecs; i++) {
281
        spec = cPtr->specs[i];
282
 
283
        if (!spec->isAlias) {
284
            if ((value=Tk_GetOption(tkwin,spec->dbName,spec->dbClass))==NULL) {
285
                value = spec->defValue;
286
            }
287
            if (Tix_ChangeOneOption(interp, cPtr, widRec, spec,
288
                value, 1, 0)!=TCL_OK) {
289
                return TCL_ERROR;
290
            }
291
        }
292
    }
293
 
294
    /* Set specs according to argument line values */
295
    for (i=0; i<argc; i+=2) {
296
        spec = Tix_FindConfigSpecByName(interp, cPtr, argv[i]);
297
 
298
        if (spec == NULL) {     /* this is an invalid flag */
299
            return TCL_ERROR;
300
        }
301
 
302
        if (Tix_ChangeOneOption(interp, cPtr, widRec, spec,
303
                argv[i+1], 0, 1)!=TCL_OK) {
304
            return TCL_ERROR;
305
        }
306
    }
307
 
308
    return TCL_OK;
309
}

powered by: WebSVN 2.1.0

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