Guido van Rossum | 7a206c8 | 1997-11-22 17:34:41 +0000 | [diff] [blame] | 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 | */ |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | 0a8266a | 2004-06-13 20:29:55 +0000 | [diff] [blame] | 15 | #include <string.h> |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 16 | #include <tcl.h> |
| 17 | #include <tk.h> |
| 18 | |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 26 | int |
Peter Schneider-Kamp | faaad37 | 2000-07-10 09:26:41 +0000 | [diff] [blame] | 27 | Tcl_AppInit(Tcl_Interp *interp) |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 28 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 29 | const char *_tkinter_skip_tk_init; |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 30 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 31 | const char *_tkinter_tk_failed; |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 32 | #endif |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 33 | |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 34 | #ifdef TK_AQUA |
| 35 | #ifndef MAX_PATH_LEN |
| 36 | #define MAX_PATH_LEN 1024 |
| 37 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 38 | char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN]; |
| 39 | Tcl_Obj* pathPtr; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 40 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 41 | /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */ |
| 42 | Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary", |
| 43 | tclLibPath, MAX_PATH_LEN, 0); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 44 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 56 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | if (Tcl_Init (interp) == TCL_ERROR) |
| 58 | return TCL_ERROR; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 59 | |
| 60 | #ifdef TK_AQUA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | /* pre- Tk_Init code copied from tkMacOSXAppInit.c */ |
| 62 | Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary", |
| 63 | tkLibPath, MAX_PATH_LEN, 1); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 64 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 65 | if (tclLibPath[0] != '\0') { |
| 66 | pathPtr = Tcl_NewStringObj(tclLibPath, -1); |
| 67 | } else { |
| 68 | Tcl_Obj *pathPtr = TclGetLibraryPath(); |
| 69 | } |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 70 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 71 | if (tkLibPath[0] != '\0') { |
| 72 | Tcl_Obj *objPtr; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 73 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 74 | Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY); |
| 75 | objPtr = Tcl_NewStringObj(tkLibPath, -1); |
| 76 | Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 77 | } |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 78 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 79 | TclSetLibraryPath(pathPtr); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 82 | #ifdef WITH_XXX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | /* Initialize modules that don't require Tk */ |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 92 | |
| 93 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 94 | _tkinter_tk_failed = Tcl_GetVar(interp, |
| 95 | "_tkinter_tk_failed", TCL_GLOBAL_ONLY); |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 96 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 103 | #endif |
| 104 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | if (Tk_Init(interp) == TCL_ERROR) { |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 106 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | tk_load_failed = 1; |
| 108 | Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY); |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 109 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 110 | return TCL_ERROR; |
| 111 | } |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 112 | |
Gregory P. Smith | db66eba | 2012-03-13 23:21:53 -0700 | [diff] [blame] | 113 | Tk_MainWindow(interp); |
Guido van Rossum | e168c65 | 1999-11-05 18:11:23 +0000 | [diff] [blame] | 114 | |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 115 | #ifdef TK_AQUA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | TkMacOSXInitAppleEvents(interp); |
| 117 | TkMacOSXInitMenus(interp); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 118 | #endif |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | 1cbdfb9 | 1997-05-14 19:22:11 +0000 | [diff] [blame] | 120 | #ifdef WITH_PIL /* 0.2b5 and later -- not yet released as of May 14 */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | 1cbdfb9 | 1997-05-14 19:22:11 +0000 | [diff] [blame] | 127 | #endif |
| 128 | |
| 129 | #ifdef WITH_PIL_OLD /* 0.2b4 and earlier */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | 1cbdfb9 | 1997-05-14 19:22:11 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Guido van Rossum | aec7497 | 1997-11-19 18:56:17 +0000 | [diff] [blame] | 137 | #ifdef WITH_TIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | aec7497 | 1997-11-19 18:56:17 +0000 | [diff] [blame] | 143 | #endif |
| 144 | |
| 145 | #ifdef WITH_BLT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | 7a206c8 | 1997-11-22 17:34:41 +0000 | [diff] [blame] | 151 | #endif |
| 152 | |
| 153 | #ifdef WITH_TOGL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 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 | } |
Guido van Rossum | aec7497 | 1997-11-19 18:56:17 +0000 | [diff] [blame] | 160 | #endif |
| 161 | |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 162 | #ifdef WITH_XXX |
| 163 | |
| 164 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | return TCL_OK; |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 166 | } |