blob: 7616d9d319d228a3b9d7824576b78774834cb133 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 const char *_tkinter_skip_tk_init;
Guilherme Polob681df42009-02-09 22:33:59 +000030#ifdef TKINTER_PROTECT_LOADTK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 const char *_tkinter_tk_failed;
Guilherme Polob681df42009-02-09 22:33:59 +000032#endif
Guido van Rossumf7132471994-06-27 08:00:16 +000033
Jack Jansencb852442001-12-09 23:15:56 +000034#ifdef TK_AQUA
35#ifndef MAX_PATH_LEN
36#define MAX_PATH_LEN 1024
37#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 char tclLibPath[MAX_PATH_LEN], tkLibPath[MAX_PATH_LEN];
39 Tcl_Obj* pathPtr;
Jack Jansencb852442001-12-09 23:15:56 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 /* pre- Tcl_Init code copied from tkMacOSXAppInit.c */
42 Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tcllibrary",
43 tclLibPath, MAX_PATH_LEN, 0);
Jack Jansencb852442001-12-09 23:15:56 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 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 Jansencb852442001-12-09 23:15:56 +000056#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000057 if (Tcl_Init (interp) == TCL_ERROR)
58 return TCL_ERROR;
Jack Jansencb852442001-12-09 23:15:56 +000059
60#ifdef TK_AQUA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 /* pre- Tk_Init code copied from tkMacOSXAppInit.c */
62 Tk_MacOSXOpenBundleResources (interp, "com.tcltk.tklibrary",
63 tkLibPath, MAX_PATH_LEN, 1);
Jack Jansencb852442001-12-09 23:15:56 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 if (tclLibPath[0] != '\0') {
66 pathPtr = Tcl_NewStringObj(tclLibPath, -1);
67 } else {
68 Tcl_Obj *pathPtr = TclGetLibraryPath();
69 }
Jack Jansencb852442001-12-09 23:15:56 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (tkLibPath[0] != '\0') {
72 Tcl_Obj *objPtr;
Jack Jansencb852442001-12-09 23:15:56 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 Tcl_SetVar(interp, "tk_library", tkLibPath, TCL_GLOBAL_ONLY);
75 objPtr = Tcl_NewStringObj(tkLibPath, -1);
76 Tcl_ListObjAppendElement(NULL, pathPtr, objPtr);
77 }
Jack Jansencb852442001-12-09 23:15:56 +000078
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000079 TclSetLibraryPath(pathPtr);
Jack Jansencb852442001-12-09 23:15:56 +000080#endif
81
David Aschere2b4b322004-02-18 05:59:53 +000082#ifdef WITH_XXX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 /* Initialize modules that don't require Tk */
David Aschere2b4b322004-02-18 05:59:53 +000084#endif
85
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000086 _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 Polob681df42009-02-09 22:33:59 +000092
93#ifdef TKINTER_PROTECT_LOADTK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 _tkinter_tk_failed = Tcl_GetVar(interp,
95 "_tkinter_tk_failed", TCL_GLOBAL_ONLY);
Guilherme Polob681df42009-02-09 22:33:59 +000096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000097 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 Polob681df42009-02-09 22:33:59 +0000103#endif
104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (Tk_Init(interp) == TCL_ERROR) {
Guilherme Polob681df42009-02-09 22:33:59 +0000106#ifdef TKINTER_PROTECT_LOADTK
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 tk_load_failed = 1;
108 Tcl_SetVar(interp, "_tkinter_tk_failed", "1", TCL_GLOBAL_ONLY);
Guilherme Polob681df42009-02-09 22:33:59 +0000109#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 return TCL_ERROR;
111 }
Guido van Rossumf7132471994-06-27 08:00:16 +0000112
Gregory P. Smithdb66eba2012-03-13 23:21:53 -0700113 Tk_MainWindow(interp);
Guido van Rossume168c651999-11-05 18:11:23 +0000114
Jack Jansencb852442001-12-09 23:15:56 +0000115#ifdef TK_AQUA
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 TkMacOSXInitAppleEvents(interp);
117 TkMacOSXInitMenus(interp);
Jack Jansencb852442001-12-09 23:15:56 +0000118#endif
Guido van Rossumf7132471994-06-27 08:00:16 +0000119
Guido van Rossum1cbdfb91997-05-14 19:22:11 +0000120#ifdef WITH_PIL /* 0.2b5 and later -- not yet released as of May 14 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 {
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 Rossum1cbdfb91997-05-14 19:22:11 +0000127#endif
128
129#ifdef WITH_PIL_OLD /* 0.2b4 and earlier */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000130 {
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 Rossum1cbdfb91997-05-14 19:22:11 +0000135#endif
136
Guido van Rossumaec74971997-11-19 18:56:17 +0000137#ifdef WITH_TIX
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 {
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 Rossumaec74971997-11-19 18:56:17 +0000143#endif
144
145#ifdef WITH_BLT
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 {
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 Rossum7a206c81997-11-22 17:34:41 +0000151#endif
152
153#ifdef WITH_TOGL
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 {
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 Rossumaec74971997-11-19 18:56:17 +0000160#endif
161
Guido van Rossumf7132471994-06-27 08:00:16 +0000162#ifdef WITH_XXX
163
164#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 return TCL_OK;
Guido van Rossumf7132471994-06-27 08:00:16 +0000166}