xref: /third_party/python/Modules/tkappinit.c (revision 7db96d56)
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 */
23static int tk_load_failed;
24#endif
25
26int
27Tcl_AppInit(Tcl_Interp *interp)
28{
29    const char *_tkinter_skip_tk_init;
30#ifdef TKINTER_PROTECT_LOADTK
31    const char *_tkinter_tk_failed;
32#endif
33
34#ifdef TK_AQUA
35#ifndef MAX_PATH_LEN
36#define MAX_PATH_LEN 1024
37#endif
38    char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN];
39    Tcl_Obj*            pathPtr;
40
41    /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */
42    Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary",
43    tclLibPath, MAX_PATH_LEN, 0);
44
45    if (tclLibPath[0] != '\0') {
46    Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
47        Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
48        Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
49    }
50
51    if (tclLibPath[0] != '\0') {
52        Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY);
53        Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY);
54        Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY);
55    }
56#endif
57    if (Tcl_Init (interp) == TCL_ERROR)
58        return TCL_ERROR;
59
60#ifdef TK_AQUA
61    /* pre- Tk_Init code copied from tkMacOSXAppInit.c */
62    Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary",
63        tkLibPath, MAX_PATH_LEN, 1);
64
65    if (tclLibPath[0] != '\0') {
66        pathPtr = Tcl_NewStringObj(tclLibPath, -1);
67    } else {
68        Tcl_Obj *pathPtr = TclGetLibraryPath();
69    }
70
71    if (tkLibPath[0] != '\0') {
72        Tcl_Obj *objPtr;
73
74        Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY);
75        objPtr = Tcl_NewStringObj(tkLibPath, -1);
76        Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
77    }
78
79    TclSetLibraryPath(pathPtr);
80#endif
81
82#ifdef WITH_XXX
83        /* Initialize modules that don't require Tk */
84#endif
85
86    _tkinter_skip_tk_init =     Tcl_GetVar(interp,
87                    "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY);
88    if (_tkinter_skip_tk_init != NULL &&
89                    strcmp(_tkinter_skip_tk_init, "1") == 0) {
90        return TCL_OK;
91    }
92
93#ifdef TKINTER_PROTECT_LOADTK
94    _tkinter_tk_failed = Tcl_GetVar(interp,
95                    "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
96
97    if (tk_load_failed || (
98                            _tkinter_tk_failed != NULL &&
99                            strcmp(_tkinter_tk_failed, "1") == 0)) {
100        Tcl_SetResult(interp, TKINTER_LOADTK_ERRMSG, TCL_STATIC);
101        return TCL_ERROR;
102    }
103#endif
104
105    if (Tk_Init(interp) == TCL_ERROR) {
106#ifdef TKINTER_PROTECT_LOADTK
107        tk_load_failed = 1;
108        Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
109#endif
110        return TCL_ERROR;
111    }
112
113    Tk_MainWindow(interp);
114
115#ifdef TK_AQUA
116    TkMacOSXInitAppleEvents(interp);
117    TkMacOSXInitMenus(interp);
118#endif
119
120#ifdef WITH_PIL /* 0.2b5 and later -- not yet released as of May 14 */
121    {
122        extern void TkImaging_Init(Tcl_Interp *);
123        TkImaging_Init(interp);
124        /* XXX TkImaging_Init() doesn't have the right return type */
125        /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
126    }
127#endif
128
129#ifdef WITH_PIL_OLD /* 0.2b4 and earlier */
130    {
131        extern void TkImaging_Init(void);
132        /* XXX TkImaging_Init() doesn't have the right prototype */
133        /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/
134    }
135#endif
136
137#ifdef WITH_TIX
138    {
139        extern int Tix_Init(Tcl_Interp *interp);
140        extern int Tix_SafeInit(Tcl_Interp *interp);
141        Tcl_StaticPackage(NULL, "Tix", Tix_Init, Tix_SafeInit);
142    }
143#endif
144
145#ifdef WITH_BLT
146    {
147        extern int Blt_Init(Tcl_Interp *);
148        extern int Blt_SafeInit(Tcl_Interp *);
149        Tcl_StaticPackage(NULL, "Blt", Blt_Init, Blt_SafeInit);
150    }
151#endif
152
153#ifdef WITH_TOGL
154    {
155        /* XXX I've heard rumors that this doesn't work */
156        extern int Togl_Init(Tcl_Interp *);
157        /* XXX Is there no Togl_SafeInit? */
158        Tcl_StaticPackage(NULL, "Togl", Togl_Init, NULL);
159    }
160#endif
161
162#ifdef WITH_XXX
163
164#endif
165    return TCL_OK;
166}
167