One more check on valid display which is known to be in the startup
[xcircuit.git] / xcwrap.c
blobe7c2c5075dde61934c27f55176fc3a6085434b6a
1 /*--------------------------------------------------------------*/
2 /* xcwrap.c: */
3 /* Tcl module generator for xcircuit */
4 /*--------------------------------------------------------------*/
6 #ifdef TCL_WRAPPER
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
12 #include <tk.h>
14 Tcl_Interp *xcinterp;
15 Tcl_Interp *consoleinterp;
17 /*------------------------------------------------------*/
18 /* Redefine all of the callback functions for Tcl/Tk */
19 /*------------------------------------------------------*/
21 extern int Tk_SimpleObjCmd();
22 extern int xctcl_standardaction();
23 extern int xctcl_action();
24 extern int xctcl_refresh();
25 extern int xctcl_library();
26 extern int xctcl_tech();
27 extern int xctcl_font();
28 extern int xctcl_cursor();
29 extern int xctcl_bind();
30 extern int xctcl_filerecover();
31 extern int xctcl_color();
32 extern int xctcl_dofill();
33 extern int xctcl_doborder();
34 extern int xctcl_promptquit();
35 extern int xctcl_quit();
36 extern int xctcl_promptsavepage();
37 extern int xctcl_eventmode();
38 extern int xctcl_delete();
39 extern int xctcl_undo();
40 extern int xctcl_redo();
41 extern int xctcl_copy();
42 extern int xctcl_move();
43 extern int xctcl_edit();
44 extern int xctcl_param();
45 extern int xctcl_select();
46 extern int xctcl_deselect();
47 extern int xctcl_push();
48 extern int xctcl_pop();
49 extern int xctcl_rotate();
50 extern int xctcl_flip();
51 extern int xctcl_config();
52 extern int xctcl_page();
53 extern int xctcl_spice();
54 extern int xctcl_svg();
55 extern int xctcl_zoom();
56 extern int xctcl_pan();
57 extern int xctcl_element();
58 extern int xctcl_label();
59 extern int xctcl_polygon();
60 extern int xctcl_instance();
61 extern int xctcl_spline();
62 extern int xctcl_path();
63 extern int xctcl_arc();
64 extern int xctcl_graphic();
65 extern int xctcl_object();
66 extern int xctcl_here();
67 extern int xctcl_start();
68 extern int xctcl_netlist();
69 extern int xctcl_symschem();
70 extern int xctcl_tag();
72 typedef struct {
73 const char *cmdstr;
74 void (*func)();
75 } cmdstruct;
77 static cmdstruct xc_commands[] =
79 {"action", (void *)xctcl_action},
80 {"standardaction", (void *)xctcl_standardaction},
81 {"start", (void *)xctcl_start},
82 {"tag", (void *)xctcl_tag},
83 {"refresh", (void *)xctcl_refresh},
84 {"library", (void *)xctcl_library},
85 {"technology", (void *)xctcl_tech},
86 {"loadfont", (void *)xctcl_font},
87 {"cursor", (void *)xctcl_cursor},
88 {"bindkey", (void *)xctcl_bind},
89 {"filerecover", (void *)xctcl_filerecover},
90 {"color", (void *)xctcl_color},
91 {"fill", (void *)xctcl_dofill},
92 {"border", (void *)xctcl_doborder},
93 {"quit", (void *)xctcl_promptquit},
94 {"quitnocheck", (void *)xctcl_quit},
95 {"promptsavepage", (void *)xctcl_promptsavepage},
96 {"eventmode", (void *)xctcl_eventmode},
97 {"delete", (void *)xctcl_delete},
98 {"undo", (void *)xctcl_undo},
99 {"redo", (void *)xctcl_redo},
100 {"select", (void *)xctcl_select},
101 {"deselect", (void *)xctcl_deselect},
102 {"copy", (void *)xctcl_copy},
103 {"move", (void *)xctcl_move},
104 {"edit", (void *)xctcl_edit},
105 {"parameter", (void *)xctcl_param},
106 {"push", (void *)xctcl_push},
107 {"pop", (void *)xctcl_pop},
108 {"rotate", (void *)xctcl_rotate},
109 {"flip", (void *)xctcl_flip},
110 {"config", (void *)xctcl_config},
111 {"page", (void *)xctcl_page},
112 {"zoom", (void *)xctcl_zoom},
113 {"pan", (void *)xctcl_pan},
114 {"element", (void *)xctcl_element},
115 {"label", (void *)xctcl_label},
116 {"polygon", (void *)xctcl_polygon},
117 {"instance", (void *)xctcl_instance},
118 {"spline", (void *)xctcl_spline},
119 {"path", (void *)xctcl_path},
120 {"arc", (void *)xctcl_arc},
121 {"graphic", (void *)xctcl_graphic},
122 {"object", (void *)xctcl_object},
123 {"here", (void *)xctcl_here},
124 {"netlist", (void *)xctcl_netlist},
125 {"symbol", (void *)xctcl_symschem},
126 {"schematic", (void *)xctcl_symschem},
127 {"spice", (void *)xctcl_spice},
128 {"svg", (void *)xctcl_svg},
129 {"", NULL} /* sentinel */
132 extern Tcl_HashTable XcTagTable;
134 /*--------------------------------------------------------------*/
135 /* Initialization procedure for Tcl/Tk */
136 /*--------------------------------------------------------------*/
138 #ifdef _MSC_VER
139 __declspec(dllexport)
140 #endif
142 Xcircuit_Init(Tcl_Interp *interp)
144 char command[256];
145 int cmdidx;
146 Tk_Window tktop;
147 char *tmp_s, *tmp_l;
148 char *cadhome;
149 char version_string[20];
151 /* Interpreter sanity checks */
152 if (interp == NULL) return TCL_ERROR;
154 /* Remember the interpreter */
155 xcinterp = interp;
157 if (Tcl_InitStubs(interp, "8.1", 0) == NULL) return TCL_ERROR;
159 tmp_s = getenv("XCIRCUIT_SRC_DIR");
160 if (tmp_s == NULL) tmp_s = SCRIPTS_DIR;
162 tmp_l = getenv("XCIRCUIT_LIB_DIR");
163 if (tmp_l == NULL) tmp_l = BUILTINS_DIR;
165 strcpy(command, "xcircuit::");
167 /* Create the start command */
169 tktop = Tk_MainWindow(interp);
171 /* Create all of the commands (except "simple") */
173 for (cmdidx = 0; xc_commands[cmdidx].func != NULL; cmdidx++) {
174 sprintf(command + 10, "%s", xc_commands[cmdidx].cmdstr);
175 Tcl_CreateObjCommand(interp, command,
176 (Tcl_ObjCmdProc *)xc_commands[cmdidx].func,
177 (ClientData)tktop, (Tcl_CmdDeleteProc *) NULL);
180 /* Command which creates a "simple" window (this is a top- */
181 /* level command, not one in the xcircuit namespace which */
182 /* is why I treat it separately from the other commands). */
184 Tcl_CreateObjCommand(interp, "simple",
185 (Tcl_ObjCmdProc *)Tk_SimpleObjCmd,
186 (ClientData)tktop, (Tcl_CmdDeleteProc *) NULL);
188 sprintf(command, "lappend auto_path %s", tmp_s);
189 Tcl_Eval(interp, command);
190 if (!strstr(tmp_s, "tcl")) {
191 sprintf(command, "lappend auto_path %s/tcl", tmp_s);
192 Tcl_Eval(interp, command);
195 if (strcmp(tmp_s, SCRIPTS_DIR))
196 Tcl_Eval(interp, "lappend auto_path " SCRIPTS_DIR );
198 /* Set $XCIRCUIT_SRC_DIR and $XCIRCUIT_LIB_DIR as Tcl variables */
200 Tcl_SetVar(interp, "XCIRCUIT_SRC_DIR", tmp_s, TCL_GLOBAL_ONLY);
201 Tcl_SetVar(interp, "XCIRCUIT_LIB_DIR", tmp_l, TCL_GLOBAL_ONLY);
203 /* Set $CAD_ROOT as a Tcl variable */
205 cadhome = getenv("CAD_ROOT");
206 if (cadhome == NULL) cadhome = CAD_DIR;
207 Tcl_SetVar(interp, "CAD_ROOT", cadhome, TCL_GLOBAL_ONLY);
209 /* Set $XCIRCUIT_VERSION and $XCIRCUIT_REVISION as Tcl variables */
211 sprintf(version_string, "%s", PROG_REVISION);
212 Tcl_SetVar(interp, "XCIRCUIT_REVISION", version_string, TCL_GLOBAL_ONLY);
214 sprintf(version_string, "%s", PROG_VERSION);
215 Tcl_SetVar(interp, "XCIRCUIT_VERSION", version_string, TCL_GLOBAL_ONLY);
217 #ifdef ASG
218 /* Set a variable in Tcl indicating that the ASG module is compiled in */
219 Tcl_SetVar(interp, "XCIRCUIT_ASG", "1", TCL_GLOBAL_ONLY);
220 #endif
222 /* Export the namespace commands */
224 Tcl_Eval(interp, "namespace eval xcircuit namespace export *");
225 Tcl_PkgProvide(interp, "Xcircuit", version_string);
227 /* Initialize the console interpreter, if there is one. */
229 if ((consoleinterp = Tcl_GetMaster(interp)) == NULL)
230 consoleinterp = interp;
232 /* Initialize the command tag table */
234 Tcl_InitHashTable(&XcTagTable, TCL_STRING_KEYS);
236 return TCL_OK;
239 #endif /* TCL_WRAPPER */