1 /* appinit.c -- Tcl and Tk application initialization.
2 
3    The function Tcl_AppInit() below initializes various Tcl packages.
4    It is called for each Tcl interpreter created by _tkinter.create().
5    It needs to be compiled with -DWITH_<package> flags for each package
6    that you are statically linking with.  You may have to add sections
7    for packages not yet listed below.
8 
9    Note that those packages for which Tcl_StaticPackage() is called with
10    a NULL first argument are known as "static loadable" packages to
11    Tcl but not actually initialized.  To use these, you have to load
12    it explicitly, e.g. tkapp.eval("load {} Blt").
13  */
14 
15 #include <string.h>
16 #include <tcl.h>
17 #include <tk.h>
18 
19 #include "tkinter.h"
20 
21 #ifdef TKINTER_PROTECT_LOADTK
22 /* See Tkapp_TkInit in _tkinter.c for the usage of tk_load_faile */
23 static int tk_load_failed;
24 #endif
25 
26 int
Tcl_AppInit(Tcl_Interp * interp)27 Tcl_AppInit(Tcl_Interp *interp)
28 {
29 #ifdef WITH_MOREBUTTONS
30     Tk_Window main_window;
31 #endif
32     const char *_tkinter_skip_tk_init;
33 #ifdef TKINTER_PROTECT_LOADTK
34     const char *_tkinter_tk_failed;
35 #endif
36 
37 #ifdef TK_AQUA
38 #ifndef MAX_PATH_LEN
39 #define MAX_PATH_LEN 1024
40 #endif
41     char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN];
42     Tcl_Obj*            pathPtr;
43 
44     /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */
45     Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary",
46     tclLibPath, MAX_PATH_LEN, 0);
47 
48     if (tclLibPath[0] != '\0') {
49     Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
50         Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
51         Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
52     }
53 
54     if (tclLibPath[0] != '\0') {
55         Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
56         Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
57         Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
58     }
59 #endif
60     if (Tcl_Init (interp) == TCL_ERROR)
61         return TCL_ERROR;
62 
63 #ifdef TK_AQUA
64     /* pre- Tk_Init code copied from tkMacOSXAppInit.c */
65     Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary",
66         tkLibPath, MAX_PATH_LEN, 1);
67 
68     if (tclLibPath[0] != '\0') {
69         pathPtr = Tcl_NewStringObj(tclLibPath, -1);
70     } else {
71         Tcl_Obj *pathPtr = TclGetLibraryPath();
72     }
73 
74     if (tkLibPath[0] != '\0') {
75         Tcl_Obj *objPtr;
76 
77         Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY);
78         objPtr = Tcl_NewStringObj(tkLibPath, -1);
79         Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
80     }
81 
82     TclSetLibraryPath(pathPtr);
83 #endif
84 
85 #ifdef WITH_XXX
86         /* Initialize modules that don't require Tk */
87 #endif
88 
89     _tkinter_skip_tk_init =     Tcl_GetVar(interp,
90                     "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
91     if (_tkinter_skip_tk_init != NULL &&
92                     strcmp(_tkinter_skip_tk_init, "1") == 0) {
93         return TCL_OK;
94     }
95 
96 #ifdef TKINTER_PROTECT_LOADTK
97     _tkinter_tk_failed = Tcl_GetVar(interp,
98                     "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
99 
100     if (tk_load_failed || (
101                             _tkinter_tk_failed != NULL &&
102                             strcmp(_tkinter_tk_failed, "1") == 0)) {
103         Tcl_SetResult(interp, TKINTER_LOADTK_ERRMSG, TCL_STATIC);
104         return TCL_ERROR;
105     }
106 #endif
107 
108     if (Tk_Init(interp) == TCL_ERROR) {
109 #ifdef TKINTER_PROTECT_LOADTK
110         tk_load_failed = 1;
111         Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
112 #endif
113         return TCL_ERROR;
114     }
115 
116 #ifdef WITH_MOREBUTTONS
117     main_window = Tk_MainWindow(interp);
118 #else
119     Tk_MainWindow(interp);
120 #endif
121 
122 #ifdef TK_AQUA
123     TkMacOSXInitAppleEvents(interp);
124     TkMacOSXInitMenus(interp);
125 #endif
126 
127 #ifdef WITH_MOREBUTTONS
128     {
129         extern Tcl_CmdProc studButtonCmd;
130         extern Tcl_CmdProc triButtonCmd;
131 
132         Tcl_CreateCommand(interp, "studbutton", studButtonCmd,
133                           (ClientData) main_window, NULL);
134         Tcl_CreateCommand(interp, "tributton", triButtonCmd,
135                           (ClientData) main_window, NULL);
136     }
137 #endif
138 
139 #ifdef WITH_PIL /* 0.2b5 and later -- not yet released as of May 14 */
140     {
141         extern void TkImaging_Init(Tcl_Interp *);
142         TkImaging_Init(interp);
143         /* XXX TkImaging_Init() doesn't have the right return type */
144         /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
145     }
146 #endif
147 
148 #ifdef WITH_PIL_OLD /* 0.2b4 and earlier */
149     {
150         extern void TkImaging_Init(void);
151         /* XXX TkImaging_Init() doesn't have the right prototype */
152         /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
153     }
154 #endif
155 
156 #ifdef WITH_TIX
157     {
158         extern int Tix_Init(Tcl_Interp *interp);
159         extern int Tix_SafeInit(Tcl_Interp *interp);
160         Tcl_StaticPackage(NULL, "Tix", Tix_Init, Tix_SafeInit);
161     }
162 #endif
163 
164 #ifdef WITH_BLT
165     {
166         extern int Blt_Init(Tcl_Interp *);
167         extern int Blt_SafeInit(Tcl_Interp *);
168         Tcl_StaticPackage(NULL, "Blt", Blt_Init, Blt_SafeInit);
169     }
170 #endif
171 
172 #ifdef WITH_TOGL
173     {
174         /* XXX I've heard rumors that this doesn't work */
175         extern int Togl_Init(Tcl_Interp *);
176         /* XXX Is there no Togl_SafeInit? */
177         Tcl_StaticPackage(NULL, "Togl", Togl_Init, NULL);
178     }
179 #endif
180 
181 #ifdef WITH_XXX
182 
183 #endif
184     return TCL_OK;
185 }
186