1 
2 #ifdef __WXMSW__             // If building for Windows...
3 
4 #include <wx/msw/private.h>
5 #include <wx/msw/winundef.h>
6 #include <wx/msw/msvcrt.h>
7 
8 //----------------------------------------------------------------------
9 // Use an ActivationContext to ensure that the new (themed) version of
10 // the comctl32 DLL is loaded.
11 //----------------------------------------------------------------------
12 
13 // Note that the use of the ISOLATION_AWARE_ENABLED define replaces the
14 // activation context APIs with wrappers that dynamically load the API
15 // pointers from the kernel32 DLL so we don't have to do that ourselves.
16 // Using ISOLATION_AWARE_ENABLED also causes the manifest resource to be put
17 // in slot #2 as expected for DLLs. (See wx/msw/wx.rc)
18 
19 #ifdef ISOLATION_AWARE_ENABLED
20 
wxPySetActivationContext()21 static ULONG_PTR wxPySetActivationContext()
22 {
23 
24     OSVERSIONINFO info;
25     wxZeroMemory(info);
26     info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
27     GetVersionEx(&info);
28     if (info.dwMajorVersion < 5)
29         return 0;
30 
31     ULONG_PTR cookie = 0;
32     HANDLE h;
33     ACTCTX actctx;
34     TCHAR modulename[MAX_PATH];
35 
36     GetModuleFileName(wxGetInstance(), modulename, MAX_PATH);
37     wxZeroMemory(actctx);
38     actctx.cbSize = sizeof(actctx);
39     actctx.lpSource = modulename;
40     actctx.lpResourceName = MAKEINTRESOURCE(2);
41     actctx.hModule = wxGetInstance();
42     actctx.dwFlags = ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID;
43 
44     h = CreateActCtx(&actctx);
45     if (h == INVALID_HANDLE_VALUE) {
46         wxLogLastError(wxT("CreateActCtx"));
47         return 0;
48     }
49 
50     if (! ActivateActCtx(h, &cookie))
51         wxLogLastError(wxT("ActivateActCtx"));
52 
53     return cookie;
54 }
55 
wxPyClearActivationContext(ULONG_PTR cookie)56 static void wxPyClearActivationContext(ULONG_PTR cookie)
57 {
58     if (! DeactivateActCtx(0, cookie))
59         wxLogLastError(wxT("DeactivateActCtx"));
60 }
61 
62 #endif  // ISOLATION_AWARE_ENABLED
63 
64 #endif // __WXMSW__
65 
wxPyPreInit(PyObject * moduleDict)66 void wxPyPreInit(PyObject* moduleDict)
67 {
68 #ifdef ISOLATION_AWARE_ENABLED
69     wxPySetActivationContext();
70 #endif
71 //#ifdef __WXMSW__
72 ////     wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
73 ////                     | _CRTDBG_CHECK_ALWAYS_DF
74 ////                     | _CRTDBG_DELAY_FREE_MEM_DF
75 ////         );
76 //#endif
77 //
78 //#ifdef WXP_WITH_THREAD
79 //#if wxPyUSE_GIL_STATE
80 //    PyEval_InitThreads();
81 //#else
82 //    PyEval_InitThreads();
83 //    wxPyTStates = new wxPyThreadStateArray;
84 //    wxPyTMutex = new wxMutex;
85 //
86 //    // Save the current (main) thread state in our array
87 //    PyThreadState* tstate = wxPyBeginAllowThreads();
88 //    wxPyEndAllowThreads(tstate);
89 //#endif
90 //#endif
91 
92     // Ensure that the build options in the DLL (or whatever) match this build
93     wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "wxPython");
94 }
95 
_wxPyCleanup()96 void _wxPyCleanup()
97 {
98     wxEntryCleanup();
99 }
100 
101 PyObject* wxAssertionError = NULL;      // Exception object raised for wxASSERT failures
102 
wxPyCoreModuleInject(PyObject * moduleDict)103 void wxPyCoreModuleInject(PyObject* moduleDict)
104 {
105     // Create an exception object to use for wxASSERTions
106     wxAssertionError = PyErr_NewException("wx._core.wxAssertionError",
107                                             PyExc_AssertionError, NULL);
108     PyDict_SetItemString(moduleDict, "wxAssertionError", wxAssertionError);
109 
110     // An alias that should be deprecated sometime
111     PyDict_SetItemString(moduleDict, "PyAssertionError", wxAssertionError);
112 
113 
114     // Create an exception object to use when the app object hasn't been created yet
115     wxPyNoAppError = PyErr_NewException("wx._core.PyNoAppError",
116                                         PyExc_RuntimeError, NULL);
117     PyDict_SetItemString(moduleDict, "PyNoAppError", wxPyNoAppError);
118 
119 #ifdef __WXGTK__
120 #define wxPort "__WXGTK__"
121 #define wxPortName "wxGTK"
122 #endif
123 #ifdef __WXMSW__
124 #define wxPort "__WXMSW__"
125 #define wxPortName "wxMSW"
126 #endif
127 #ifdef __WXMAC__
128 #define wxPort "__WXMAC__"
129 #define wxPortName "wxMac"
130 #endif
131 
132     wxInitAllImageHandlers();
133 
134     // TODO: Find some magic way to deprecate wx.Platform such that it raises
135     // a warning when used...  Maybe a class that returns wx.Port for any __getattr__?
136     PyDict_SetItemString(moduleDict, "Port", PyUnicode_FromString(wxPort));
137     PyDict_SetItemString(moduleDict, "Platform", PyUnicode_FromString(wxPort));
138 
139     PyDict_SetItemString(moduleDict, "wxWidgets_version", wx2PyString(wxVERSION_STRING));
140 
141     PyDict_SetItemString(moduleDict, "_sizeof_int", PyLong_FromLong(sizeof(int)));
142     PyDict_SetItemString(moduleDict, "_sizeof_long", PyLong_FromLong(sizeof(long)));
143     PyDict_SetItemString(moduleDict, "_sizeof_longlong", PyLong_FromLong(sizeof(long long)));
144     PyDict_SetItemString(moduleDict, "_sizeof_double", PyLong_FromLong(sizeof(double)));
145     PyDict_SetItemString(moduleDict, "_sizeof_size_t", PyLong_FromLong(sizeof(size_t)));
146     PyDict_SetItemString(moduleDict, "_LONG_MIN", PyLong_FromLong(LONG_MIN));
147     PyDict_SetItemString(moduleDict, "_LONG_MAX", PyLong_FromLong(LONG_MAX));
148     PyDict_SetItemString(moduleDict, "_LLONG_MIN", PyLong_FromLongLong(PY_LLONG_MIN));
149     PyDict_SetItemString(moduleDict, "_LLONG_MAX", PyLong_FromLongLong(PY_LLONG_MAX));
150 
151     // Make a tuple of strings that gives more info about the platform and build.
152     PyObject* PlatformInfo = PyList_New(0);
153     PyObject* obj;
154 
155 #define _AddInfoString(st) \
156     obj = PyUnicode_FromString(st); \
157     PyList_Append(PlatformInfo, obj); \
158     Py_DECREF(obj)
159 
160     _AddInfoString(wxPort);
161     _AddInfoString(wxPortName);
162 #if wxUSE_UNICODE
163     _AddInfoString("unicode");
164 #if wxUSE_UNICODE_WCHAR
165     _AddInfoString("unicode-wchar");
166 #else
167     _AddInfoString("unicode-utf8");
168 #endif
169 #else
170     _AddInfoString("ansi");
171 #endif
172 
173 #ifdef __WXOSX__
174     _AddInfoString("wxOSX");
175 #endif
176 #ifdef __WXOSX_CARBON__
177     _AddInfoString("wxOSX-carbon");
178 #endif
179 #ifdef __WXOSX_COCOA__
180     _AddInfoString("wxOSX-cocoa");
181 #endif
182 #ifdef __WXGTK__
183 #ifdef __WXGTK3__
184     _AddInfoString("gtk3");
185 #elif __WXGTK20__
186     _AddInfoString("gtk2");
187 #else
188     _AddInfoString("gtk1");
189 #endif
190 #endif
191 #ifdef __WXDEBUG__
192     _AddInfoString("wx-assertions-on");
193 #else
194     _AddInfoString("wx-assertions-off");
195 #endif
196     _AddInfoString("phoenix");
197 
198     obj = wx2PyString(wxVERSION_STRING);
199     PyList_Append(PlatformInfo, obj);
200     Py_DECREF(obj);
201 
202 #if wxUSE_AUTOID_MANAGEMENT
203     _AddInfoString("autoidman");
204 #endif
205 
206     wxString sip_version = wxString("sip-") + wxString(SIP_VERSION_STR);
207     obj = wx2PyString(sip_version);
208     PyList_Append(PlatformInfo, obj);
209     Py_DECREF(obj);
210 
211 #undef _AddInfoString
212 
213     PyObject* PlatformInfoTuple = PyList_AsTuple(PlatformInfo);
214     Py_DECREF(PlatformInfo);
215     PyDict_SetItemString(moduleDict, "PlatformInfo", PlatformInfoTuple);
216 
217 }
218