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 | Tk_Window main_window; |
| 30 | const char *_tkinter_skip_tk_init; |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 31 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 32 | const char *_tkinter_tk_failed; |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 33 | #endif |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 34 | |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 35 | #ifdef TK_AQUA |
| 36 | #ifndef MAX_PATH_LEN |
| 37 | #define MAX_PATH_LEN 1024 |
| 38 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 39 | char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN]; |
| 40 | Tcl_Obj* pathPtr; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 41 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 42 | /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */ |
| 43 | Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary", |
| 44 | tclLibPath, MAX_PATH_LEN, 0); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 45 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 46 | if (tclLibPath[0] != '\0') { |
| 47 | Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY); |
| 48 | Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY); |
| 49 | Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY); |
| 50 | } |
| 51 | |
| 52 | if (tclLibPath[0] != '\0') { |
| 53 | Tcl_SetVar(interp, "tcl_library", tclLibPath, TCL_GLOBAL_ONLY); |
| 54 | Tcl_SetVar(interp, "tclDefaultLibrary", tclLibPath, TCL_GLOBAL_ONLY); |
| 55 | Tcl_SetVar(interp, "tcl_pkgPath", tclLibPath, TCL_GLOBAL_ONLY); |
| 56 | } |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 57 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 58 | if (Tcl_Init (interp) == TCL_ERROR) |
| 59 | return TCL_ERROR; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 60 | |
| 61 | #ifdef TK_AQUA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | /* pre- Tk_Init code copied from tkMacOSXAppInit.c */ |
| 63 | Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary", |
| 64 | tkLibPath, MAX_PATH_LEN, 1); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 65 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 66 | if (tclLibPath[0] != '\0') { |
| 67 | pathPtr = Tcl_NewStringObj(tclLibPath, -1); |
| 68 | } else { |
| 69 | Tcl_Obj *pathPtr = TclGetLibraryPath(); |
| 70 | } |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 71 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 72 | if (tkLibPath[0] != '\0') { |
| 73 | Tcl_Obj *objPtr; |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 74 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 75 | Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY); |
| 76 | objPtr = Tcl_NewStringObj(tkLibPath, -1); |
| 77 | Tcl_ListObjAppendElement(NULL, pathPtr, objPtr); |
| 78 | } |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 79 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 80 | TclSetLibraryPath(pathPtr); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 83 | #ifdef WITH_XXX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | /* Initialize modules that don't require Tk */ |
David Ascher | e2b4b32 | 2004-02-18 05:59:53 +0000 | [diff] [blame] | 85 | #endif |
| 86 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | _tkinter_skip_tk_init = Tcl_GetVar(interp, |
| 88 | "_tkinter_skip_tk_init", TCL_GLOBAL_ONLY); |
| 89 | if (_tkinter_skip_tk_init != NULL && |
| 90 | strcmp(_tkinter_skip_tk_init, "1") == 0) { |
| 91 | return TCL_OK; |
| 92 | } |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 93 | |
| 94 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 95 | _tkinter_tk_failed = Tcl_GetVar(interp, |
| 96 | "_tkinter_tk_failed", TCL_GLOBAL_ONLY); |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 97 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 98 | if (tk_load_failed || ( |
| 99 | _tkinter_tk_failed != NULL && |
| 100 | strcmp(_tkinter_tk_failed, "1") == 0)) { |
| 101 | Tcl_SetResult(interp, TKINTER_LOADTK_ERRMSG, TCL_STATIC); |
| 102 | return TCL_ERROR; |
| 103 | } |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | if (Tk_Init(interp) == TCL_ERROR) { |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 107 | #ifdef TKINTER_PROTECT_LOADTK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 108 | tk_load_failed = 1; |
| 109 | Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY); |
Guilherme Polo | b681df4 | 2009-02-09 22:33:59 +0000 | [diff] [blame] | 110 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 111 | return TCL_ERROR; |
| 112 | } |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | main_window = Tk_MainWindow(interp); |
Guido van Rossum | e168c65 | 1999-11-05 18:11:23 +0000 | [diff] [blame] | 115 | |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 116 | #ifdef TK_AQUA |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 117 | TkMacOSXInitAppleEvents(interp); |
| 118 | TkMacOSXInitMenus(interp); |
Jack Jansen | cb85244 | 2001-12-09 23:15:56 +0000 | [diff] [blame] | 119 | #endif |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 120 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | #ifdef WITH_MOREBUTTONS |
| 122 | { |
| 123 | extern Tcl_CmdProc studButtonCmd; |
| 124 | extern Tcl_CmdProc triButtonCmd; |
| 125 | |
| 126 | Tcl_CreateCommand(interp, "studbutton", studButtonCmd, |
| 127 | (ClientData) main_window, NULL); |
| 128 | Tcl_CreateCommand(interp, "tributton", triButtonCmd, |
| 129 | (ClientData) main_window, NULL); |
| 130 | } |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 131 | #endif |
Guido van Rossum | 1cbdfb9 | 1997-05-14 19:22:11 +0000 | [diff] [blame] | 132 | |
| 133 | #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] | 134 | { |
| 135 | extern void TkImaging_Init(Tcl_Interp *); |
| 136 | TkImaging_Init(interp); |
| 137 | /* XXX TkImaging_Init() doesn't have the right return type */ |
| 138 | /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/ |
| 139 | } |
Guido van Rossum | 1cbdfb9 | 1997-05-14 19:22:11 +0000 | [diff] [blame] | 140 | #endif |
| 141 | |
| 142 | #ifdef WITH_PIL_OLD /* 0.2b4 and earlier */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | { |
| 144 | extern void TkImaging_Init(void); |
| 145 | /* XXX TkImaging_Init() doesn't have the right prototype */ |
| 146 | /*Tcl_StaticPackage(interp, "Imaging", TkImaging_Init, NULL);*/ |
| 147 | } |
Guido van Rossum | 1cbdfb9 | 1997-05-14 19:22:11 +0000 | [diff] [blame] | 148 | #endif |
| 149 | |
Guido van Rossum | aec7497 | 1997-11-19 18:56:17 +0000 | [diff] [blame] | 150 | #ifdef WITH_TIX |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 151 | { |
| 152 | extern int Tix_Init(Tcl_Interp *interp); |
| 153 | extern int Tix_SafeInit(Tcl_Interp *interp); |
| 154 | Tcl_StaticPackage(NULL, "Tix", Tix_Init, Tix_SafeInit); |
| 155 | } |
Guido van Rossum | aec7497 | 1997-11-19 18:56:17 +0000 | [diff] [blame] | 156 | #endif |
| 157 | |
| 158 | #ifdef WITH_BLT |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 159 | { |
| 160 | extern int Blt_Init(Tcl_Interp *); |
| 161 | extern int Blt_SafeInit(Tcl_Interp *); |
| 162 | Tcl_StaticPackage(NULL, "Blt", Blt_Init, Blt_SafeInit); |
| 163 | } |
Guido van Rossum | 7a206c8 | 1997-11-22 17:34:41 +0000 | [diff] [blame] | 164 | #endif |
| 165 | |
| 166 | #ifdef WITH_TOGL |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 167 | { |
| 168 | /* XXX I've heard rumors that this doesn't work */ |
| 169 | extern int Togl_Init(Tcl_Interp *); |
| 170 | /* XXX Is there no Togl_SafeInit? */ |
| 171 | Tcl_StaticPackage(NULL, "Togl", Togl_Init, NULL); |
| 172 | } |
Guido van Rossum | aec7497 | 1997-11-19 18:56:17 +0000 | [diff] [blame] | 173 | #endif |
| 174 | |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 175 | #ifdef WITH_XXX |
| 176 | |
| 177 | #endif |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 178 | return TCL_OK; |
Guido van Rossum | f713247 | 1994-06-27 08:00:16 +0000 | [diff] [blame] | 179 | } |