blob: 2ed85949cba034c985ba5bd5b9c31931041f59b6 [file] [log] [blame]
Guido van Rossum7a206c81997-11-22 17:34:41 +00001/* 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 Rossumf7132471994-06-27 08:00:16 +000014
Neal Norwitz0a8266a2004-06-13 20:29:55 +000015#include <string.h>
Guido van Rossumf7132471994-06-27 08:00:16 +000016#include <tcl.h>
17#include <tk.h>
18
Guilherme Polob681df42009-02-09 22:33:59 +000019#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
Guido van Rossumf7132471994-06-27 08:00:16 +000026int
Peter Schneider-Kampfaaad372000-07-10 09:26:41 +000027Tcl_AppInit(Tcl_Interp *interp)
Guido van Rossumf7132471994-06-27 08:00:16 +000028{
Gregory P. Smithdb66eba2012-03-13 23:21:53 -070029#ifdef WITH_MOREBUTTONS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000030 Tk_Window main_window;
Gregory P. Smithdb66eba2012-03-13 23:21:53 -070031#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000032 const char *_tkinter_skip_tk_init;
Guilherme Polob681df42009-02-09 22:33:59 +000033#ifdef TKINTER_PROTECT_LOADTK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 const char *_tkinter_tk_failed;
Guilherme Polob681df42009-02-09 22:33:59 +000035#endif
Guido van Rossumf7132471994-06-27 08:00:16 +000036
Jack Jansencb852442001-12-09 23:15:56 +000037#ifdef TK_AQUA
38#ifndef MAX_PATH_LEN
39#define MAX_PATH_LEN 1024
40#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN];
42 Tcl_Obj* pathPtr;
Jack Jansencb852442001-12-09 23:15:56 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */
45 Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary",
46 tclLibPath, MAX_PATH_LEN, 0);
Jack Jansencb852442001-12-09 23:15:56 +000047
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 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 }
Jack Jansencb852442001-12-09 23:15:56 +000059#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 if (Tcl_Init (interp) == TCL_ERROR)
61 return TCL_ERROR;
Jack Jansencb852442001-12-09 23:15:56 +000062
63#ifdef TK_AQUA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 /* pre- Tk_Init code copied from tkMacOSXAppInit.c */
65 Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary",
66 tkLibPath, MAX_PATH_LEN, 1);
Jack Jansencb852442001-12-09 23:15:56 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 if (tclLibPath[0] != '\0') {
69 pathPtr = Tcl_NewStringObj(tclLibPath, -1);
70 } else {
71 Tcl_Obj *pathPtr = TclGetLibraryPath();
72 }
Jack Jansencb852442001-12-09 23:15:56 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (tkLibPath[0] != '\0') {
75 Tcl_Obj *objPtr;
Jack Jansencb852442001-12-09 23:15:56 +000076
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY);
78 objPtr = Tcl_NewStringObj(tkLibPath, -1);
79 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
80 }
Jack Jansencb852442001-12-09 23:15:56 +000081
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 TclSetLibraryPath(pathPtr);
Jack Jansencb852442001-12-09 23:15:56 +000083#endif
84
David Aschere2b4b322004-02-18 05:59:53 +000085#ifdef WITH_XXX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 /* Initialize modules that don't require Tk */
David Aschere2b4b322004-02-18 05:59:53 +000087#endif
88
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 _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 }
Guilherme Polob681df42009-02-09 22:33:59 +000095
96#ifdef TKINTER_PROTECT_LOADTK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 _tkinter_tk_failed = Tcl_GetVar(interp,
98 "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
Guilherme Polob681df42009-02-09 22:33:59 +000099
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000100 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 }
Guilherme Polob681df42009-02-09 22:33:59 +0000106#endif
107
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000108 if (Tk_Init(interp) == TCL_ERROR) {
Guilherme Polob681df42009-02-09 22:33:59 +0000109#ifdef TKINTER_PROTECT_LOADTK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 tk_load_failed = 1;
111 Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
Guilherme Polob681df42009-02-09 22:33:59 +0000112#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 return TCL_ERROR;
114 }
Guido van Rossumf7132471994-06-27 08:00:16 +0000115
Gregory P. Smithdb66eba2012-03-13 23:21:53 -0700116#ifdef WITH_MOREBUTTONS
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 main_window = Tk_MainWindow(interp);
Gregory P. Smithdb66eba2012-03-13 23:21:53 -0700118#else
119 Tk_MainWindow(interp);
120#endif
Guido van Rossume168c651999-11-05 18:11:23 +0000121
Jack Jansencb852442001-12-09 23:15:56 +0000122#ifdef TK_AQUA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123 TkMacOSXInitAppleEvents(interp);
124 TkMacOSXInitMenus(interp);
Jack Jansencb852442001-12-09 23:15:56 +0000125#endif
Guido van Rossumf7132471994-06-27 08:00:16 +0000126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127#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 }
Guido van Rossumf7132471994-06-27 08:00:16 +0000137#endif
Guido van Rossum1cbdfb91997-05-14 19:22:11 +0000138
139#ifdef WITH_PIL /* 0.2b5 and later -- not yet released as of May 14 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000140 {
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 }
Guido van Rossum1cbdfb91997-05-14 19:22:11 +0000146#endif
147
148#ifdef WITH_PIL_OLD /* 0.2b4 and earlier */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 {
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 }
Guido van Rossum1cbdfb91997-05-14 19:22:11 +0000154#endif
155
Guido van Rossumaec74971997-11-19 18:56:17 +0000156#ifdef WITH_TIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 {
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 }
Guido van Rossumaec74971997-11-19 18:56:17 +0000162#endif
163
164#ifdef WITH_BLT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 {
166 extern int Blt_Init(Tcl_Interp *);
167 extern int Blt_SafeInit(Tcl_Interp *);
168 Tcl_StaticPackage(NULL, "Blt", Blt_Init, Blt_SafeInit);
169 }
Guido van Rossum7a206c81997-11-22 17:34:41 +0000170#endif
171
172#ifdef WITH_TOGL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173 {
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 }
Guido van Rossumaec74971997-11-19 18:56:17 +0000179#endif
180
Guido van Rossumf7132471994-06-27 08:00:16 +0000181#ifdef WITH_XXX
182
183#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 return TCL_OK;
Guido van Rossumf7132471994-06-27 08:00:16 +0000185}