This repository has been archived by the owner on Jan 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 169
/
helpers.cpp
3630 lines (3055 loc) · 103 KB
/
helpers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/////////////////////////////////////////////////////////////////////////////
// Name: helpers.cpp
// Purpose: Helper functions/classes for the wxPython extension module
//
// Author: Robin Dunn
//
// Created: 1-July-1997
// RCS-ID: $Id$
// Copyright: (c) 1998 by Total Control Software
// Licence: wxWindows license
/////////////////////////////////////////////////////////////////////////////
#undef DEBUG
#include <Python.h>
#include "wx/wxPython/wxPython_int.h"
#include "wx/wxPython/pyistream.h"
#include "wx/wxPython/swigver.h"
#include "wx/wxPython/twoitem.h"
#ifdef __WXMSW__
#include <wx/msw/private.h>
#include <wx/msw/winundef.h>
#include <wx/msw/msvcrt.h>
#endif
#ifdef __WXGTK__
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#ifdef __WXGTK3__
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
GDK_WINDOW_XID(gtk_widget_get_window((wxwin)->m_wxwindow)) : \
GDK_WINDOW_XID(gtk_widget_get_window((wxwin)->m_widget))
#else
#define GetXWindow(wxwin) (wxwin)->m_wxwindow ? \
GDK_WINDOW_XWINDOW((wxwin)->m_wxwindow->window) : \
GDK_WINDOW_XWINDOW((wxwin)->m_widget->window)
#endif
#include <locale.h>
#endif
#ifdef __WXX11__
#include "wx/x11/privx.h"
#define GetXWindow(wxwin) ((Window)(wxwin)->GetHandle())
#endif
#ifdef __WXMAC__
#include <wx/osx/private.h>
#endif
#include <wx/clipbrd.h>
#include <wx/mimetype.h>
#include <wx/image.h>
//----------------------------------------------------------------------
#if PYTHON_API_VERSION < 1009 && wxUSE_UNICODE
#error Python must support Unicode to use wxWindows Unicode
#endif
//----------------------------------------------------------------------
wxPyApp* wxPythonApp = NULL; // Global instance of application object
bool wxPyDoCleanup = true;
bool wxPyDoingCleanup = false;
#ifdef WXP_WITH_THREAD
#if !wxPyUSE_GIL_STATE
struct wxPyThreadState {
unsigned long tid;
PyThreadState* tstate;
wxPyThreadState(unsigned long _tid=0, PyThreadState* _tstate=NULL)
: tid(_tid), tstate(_tstate) {}
};
#include <wx/dynarray.h>
WX_DECLARE_OBJARRAY(wxPyThreadState, wxPyThreadStateArray);
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(wxPyThreadStateArray);
wxPyThreadStateArray* wxPyTStates = NULL;
wxMutex* wxPyTMutex = NULL;
#endif
#endif
#define DEFAULTENCODING_SIZE 64
static char wxPyDefaultEncoding[DEFAULTENCODING_SIZE] = "ascii";
static bool wxPyDefaultEncodingIsUTF8 = false;
static PyObject* wxPython_dict = NULL;
static PyObject* wxPyAssertionError = NULL;
static PyObject* wxPyNoAppError = NULL;
PyObject* wxPyPtrTypeMap = NULL;
#ifdef __WXMSW__ // If building for Windows...
//----------------------------------------------------------------------
// Use an ActivationContext to ensure that the new (themed) version of
// the comctl32 DLL is loaded.
//----------------------------------------------------------------------
// Note that the use of the ISOLATION_AWARE_ENABLED define replaces the
// activation context APIs with wrappers that dynamically load the API
// pointers from the kernel32 DLL so we don't have to do that ourselves.
// Using ISOLATION_AWARE_ENABLED also causes the manifest resource to be put
// in slot #2 as expected for DLLs. (See wx/msw/wx.rc)
#ifdef ISOLATION_AWARE_ENABLED
static ULONG_PTR wxPySetActivationContext()
{
OSVERSIONINFO info;
wxZeroMemory(info);
info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&info);
if (info.dwMajorVersion < 5)
return 0;
ULONG_PTR cookie = 0;
HANDLE h;
ACTCTX actctx;
TCHAR modulename[MAX_PATH];
GetModuleFileName(wxGetInstance(), modulename, MAX_PATH);
wxZeroMemory(actctx);
actctx.cbSize = sizeof(actctx);
actctx.lpSource = modulename;
actctx.lpResourceName = MAKEINTRESOURCE(2);
actctx.hModule = wxGetInstance();
actctx.dwFlags = ACTCTX_FLAG_HMODULE_VALID | ACTCTX_FLAG_RESOURCE_NAME_VALID;
h = CreateActCtx(&actctx);
if (h == INVALID_HANDLE_VALUE) {
wxLogLastError(wxT("CreateActCtx"));
return 0;
}
if (! ActivateActCtx(h, &cookie))
wxLogLastError(wxT("ActivateActCtx"));
return cookie;
}
static void wxPyClearActivationContext(ULONG_PTR cookie)
{
if (! DeactivateActCtx(0, cookie))
wxLogLastError(wxT("DeactivateActCtx"));
}
#endif
//----------------------------------------------------------------------
// This gets run when the DLL is loaded. We just need to save a handle.
//----------------------------------------------------------------------
extern "C"
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
)
{
// If wxPython is embedded in another wxWidgets app then
// the instance has already been set.
if (! wxGetInstance())
wxSetInstance(hinstDLL);
return TRUE;
}
#endif
//----------------------------------------------------------------------
// Classes for implementing the wxp main application shell.
//----------------------------------------------------------------------
IMPLEMENT_ABSTRACT_CLASS(wxPyApp, wxApp);
wxPyApp::wxPyApp() {
m_assertMode = wxPYAPP_ASSERT_EXCEPTION;
m_startupComplete = false;
m_callFilterEvent = false;
}
wxPyApp::~wxPyApp() {
wxPyDoingCleanup = true;
wxPythonApp = NULL;
wxApp::SetInstance(NULL);
}
// This one isn't acutally called... We fake it with _BootstrapApp
bool wxPyApp::OnInit() {
return true;
}
int wxPyApp::MainLoop() {
int retval = 0;
{
#ifdef __WXOSX__
wxMacAutoreleasePool autoreleasePool;
#endif
DeletePendingObjects();
}
bool initialized = wxTopLevelWindows.GetCount() != 0;
if (initialized) {
if ( m_exitOnFrameDelete == Later ) {
m_exitOnFrameDelete = Yes;
}
retval = wxApp::MainLoop();
OnExit();
}
return retval;
}
bool wxPyApp::OnInitGui() {
bool rval=true;
wxApp::OnInitGui(); // in this case always call the base class version
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "OnInitGui"))
rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
wxPyEndBlockThreads(blocked);
return rval;
}
void wxPyApp::OnEventLoopEnter(wxEventLoopBase* loop)
{
bool found;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if ((found = wxPyCBH_findCallback(m_myInst, "OnEventLoopEnter"))) {
PyObject* obj = wxPyConstructObject((void*)loop, wxT("wxEventLoopBase"));
wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
Py_DECREF(obj);
}
wxPyEndBlockThreads(blocked);
if (! found)
wxApp::OnEventLoopEnter(loop);
}
void wxPyApp::OnEventLoopExit(wxEventLoopBase* loop)
{
bool found;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if ((found = wxPyCBH_findCallback(m_myInst, "OnEventLoopExit"))) {
PyObject* obj = wxPyConstructObject((void*)loop, wxT("wxEventLoopBase"));
wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
Py_DECREF(obj);
}
wxPyEndBlockThreads(blocked);
if (! found)
wxApp::OnEventLoopExit(loop);
}
int wxPyApp::OnExit() {
int rval=0;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "OnExit"))
rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
wxPyEndBlockThreads(blocked);
wxApp::OnExit(); // in this case always call the base class version
return rval;
}
#if wxUSE_EXCEPTIONS
bool wxPyApp::OnExceptionInMainLoop() {
bool rval=false;
bool found;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if ((found = wxPyCBH_findCallback(m_myInst, "OnExceptionInMainLoop")))
rval = wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
wxPyEndBlockThreads(blocked);
if (! found)
rval = wxApp::OnExceptionInMainLoop();
return rval;
}
#endif
void wxPyApp::ExitMainLoop() {
bool found;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if ((found = wxPyCBH_findCallback(m_myInst, "ExitMainLoop")))
wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
wxPyEndBlockThreads(blocked);
if (! found)
wxApp::ExitMainLoop();
}
int wxPyApp::FilterEvent(wxEvent& event) {
int result = -1;
if (m_callFilterEvent) {
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "FilterEvent")) {
wxString className = event.GetClassInfo()->GetClassName();
PyObject* eventObject = wxPyConstructObject((void*)&event, className, 0);
result = wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", eventObject));
Py_DECREF(eventObject);
}
wxPyEndBlockThreads(blocked);
}
return result;
}
void wxPyApp::OnAssertFailure(const wxChar *file,
int line,
const wxChar *func,
const wxChar *cond,
const wxChar *msg)
{
// if we're not fully initialized then just log the error
if (! m_startupComplete) {
wxString buf;
buf.Alloc(4096);
buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
file, line, cond);
if ( func && *func )
buf << wxT(" in ") << func << wxT("()");
if (msg != NULL)
buf << wxT(": ") << msg;
wxLogDebug(buf);
return;
}
// If the OnAssert is overloaded in the Python class then call it...
bool found;
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if ((found = wxPyCBH_findCallback(m_myInst, "OnAssert"))) {
PyObject* fso = wx2PyString(file);
PyObject* cso = wx2PyString(file);
PyObject* mso;
if (msg != NULL)
mso = wx2PyString(file);
else {
mso = Py_None; Py_INCREF(Py_None);
}
wxPyCBH_callCallback(m_myInst, Py_BuildValue("(OiOO)", fso, line, cso, mso));
Py_DECREF(fso);
Py_DECREF(cso);
Py_DECREF(mso);
}
wxPyEndBlockThreads(blocked);
// ...otherwise do our own thing with it
if (! found) {
// ignore it?
if (m_assertMode & wxPYAPP_ASSERT_SUPPRESS)
return;
// turn it into a Python exception?
if (m_assertMode & wxPYAPP_ASSERT_EXCEPTION) {
wxString buf;
buf.Alloc(4096);
buf.Printf(wxT("C++ assertion \"%s\" failed at %s(%d)"), cond, file, line);
if ( func && *func )
buf << wxT(" in ") << func << wxT("()");
if (msg != NULL)
buf << wxT(": ") << msg;
// set the exception
wxPyBlock_t blocked = wxPyBeginBlockThreads();
PyObject* s = wx2PyString(buf);
PyErr_SetObject(wxPyAssertionError, s);
Py_DECREF(s);
wxPyEndBlockThreads(blocked);
// Now when control returns to whatever API wrapper was called from
// Python it should detect that an exception is set and will return
// NULL, signalling the exception to Python.
}
// Send it to the normal log destination, but only if
// not _DIALOG because it will call this too
if ( (m_assertMode & wxPYAPP_ASSERT_LOG) && !(m_assertMode & wxPYAPP_ASSERT_DIALOG)) {
wxString buf;
buf.Alloc(4096);
buf.Printf(wxT("%s(%d): assert \"%s\" failed"),
file, line, cond);
if ( func && *func )
buf << wxT(" in ") << func << wxT("()");
if (msg != NULL)
buf << wxT(": ") << msg;
wxLogDebug(buf);
}
// do the normal wx assert dialog?
if (m_assertMode & wxPYAPP_ASSERT_DIALOG)
wxApp::OnAssertFailure(file, line, func, cond, msg);
}
}
// For catching Apple Events
void wxPyApp::MacOpenFile(const wxString &fileName)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "MacOpenFile")) {
PyObject* s = wx2PyString(fileName);
wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s));
Py_DECREF(s);
}
wxPyEndBlockThreads(blocked);
}
void wxPyApp::MacOpenFiles(const wxArrayString& fileNames)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "MacOpenFiles")) {
PyObject* s = wxArrayString2PyList_helper(fileNames);
wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s));
Py_DECREF(s);
}
wxPyEndBlockThreads(blocked);
}
void wxPyApp::MacOpenURL(const wxString &url)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "MacOpenURL")) {
PyObject* s = wx2PyString(url);
wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s));
Py_DECREF(s);
}
wxPyEndBlockThreads(blocked);
}
void wxPyApp::MacPrintFile(const wxString &fileName)
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "MacPrintFile")) {
PyObject* s = wx2PyString(fileName);
wxPyCBH_callCallback(m_myInst, Py_BuildValue("(O)", s));
Py_DECREF(s);
}
wxPyEndBlockThreads(blocked);
}
void wxPyApp::MacNewFile()
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "MacNewFile"))
wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
wxPyEndBlockThreads(blocked);
}
void wxPyApp::MacReopenApp()
{
wxPyBlock_t blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "MacReopenApp"))
wxPyCBH_callCallback(m_myInst, Py_BuildValue("()"));
wxPyEndBlockThreads(blocked);
}
/*static*/
bool wxPyApp::GetMacSupportPCMenuShortcuts() {
return 0;
}
/*static*/
long wxPyApp::GetMacAboutMenuItemId() {
#ifdef __WXMAC__
return s_macAboutMenuItemId;
#else
return 0;
#endif
}
/*static*/
long wxPyApp::GetMacPreferencesMenuItemId() {
#ifdef __WXMAC__
return s_macPreferencesMenuItemId;
#else
return 0;
#endif
}
/*static*/
long wxPyApp::GetMacExitMenuItemId() {
#ifdef __WXMAC__
return s_macExitMenuItemId;
#else
return 0;
#endif
}
/*static*/
wxString wxPyApp::GetMacHelpMenuTitleName() {
#ifdef __WXMAC__
return s_macHelpMenuTitleName;
#else
return wxEmptyString;
#endif
}
/*static*/
void wxPyApp::SetMacSupportPCMenuShortcuts(bool) {
}
/*static*/
void wxPyApp::SetMacAboutMenuItemId(long val) {
#ifdef __WXMAC__
s_macAboutMenuItemId = val;
#endif
}
/*static*/
void wxPyApp::SetMacPreferencesMenuItemId(long val) {
#ifdef __WXMAC__
s_macPreferencesMenuItemId = val;
#endif
}
/*static*/
void wxPyApp::SetMacExitMenuItemId(long val) {
#ifdef __WXMAC__
s_macExitMenuItemId = val;
#endif
}
/*static*/
void wxPyApp::SetMacHelpMenuTitleName(const wxString& val) {
#ifdef __WXMAC__
s_macHelpMenuTitleName = val;
#endif
}
// This finishes the initialization of wxWindows and then calls the OnInit
// that should be present in the derived (Python) class.
void wxPyApp::_BootstrapApp()
{
static bool haveInitialized = false;
bool result;
wxPyBlock_t blocked;
PyObject* retval = NULL;
PyObject* pyint = NULL;
// Only initialize wxWidgets once
if (! haveInitialized) {
// Copy the values in Python's sys.argv list to a C array of char* to
// be passed to the wxEntryStart function below.
int argc = 0;
char** argv = NULL;
blocked = wxPyBeginBlockThreads();
PyObject* sysargv = PySys_GetObject("argv");
if (sysargv != NULL) {
argc = PyList_Size(sysargv);
argv = new char*[argc+1];
int x;
for(x=0; x<argc; x++) {
PyObject *pyArg = PyList_GetItem(sysargv, x); // borrowed reference
// if there isn't anything in sys.argv[0] then set it to the python executable
if (x == 0 && PyObject_Length(pyArg) < 1)
pyArg = PySys_GetObject("executable");
argv[x] = strdup(PyString_AsString(pyArg));
}
argv[argc] = NULL;
}
wxPyEndBlockThreads(blocked);
// Initialize wxWidgets
#ifdef __WXOSX__
wxMacAutoreleasePool autoreleasePool;
#endif
result = wxEntryStart(argc, argv);
// wxApp takes ownership of the argv array, don't delete it here
blocked = wxPyBeginBlockThreads();
if (! result) {
PyErr_SetString(PyExc_SystemError,
"wxEntryStart failed, unable to initialize wxWidgets!"
#ifdef __WXGTK__
" (Is DISPLAY set properly?)"
#endif
);
goto error;
}
// On wxGTK the locale will be changed to match the system settings,
// but Python before 2.4 needs to have LC_NUMERIC set to "C" in order
// for the floating point conversions and such to work right.
#if defined(__WXGTK__) && PY_VERSION_HEX < 0x02040000
setlocale(LC_NUMERIC, "C");
#endif
wxPyEndBlockThreads(blocked);
}
else {
this->argc = 0;
}
// It's now ok to generate exceptions for assertion errors.
wxPythonApp->SetStartupComplete(true);
// Call the Python wxApp's OnPreInit and OnInit functions
blocked = wxPyBeginBlockThreads();
if (wxPyCBH_findCallback(m_myInst, "OnPreInit")) {
#ifdef __WXOSX__
wxMacAutoreleasePool autoreleasePool;
#endif
PyObject* method = m_myInst.GetLastFound();
PyObject* argTuple = PyTuple_New(0);
retval = PyEval_CallObject(method, argTuple);
m_myInst.clearRecursionGuard(method);
Py_DECREF(argTuple);
Py_DECREF(method);
if (retval == NULL)
goto error;
}
if (wxPyCBH_findCallback(m_myInst, "OnInit")) {
#ifdef __WXOSX__
wxMacAutoreleasePool autoreleasePool;
#endif
PyObject* method = m_myInst.GetLastFound();
PyObject* argTuple = PyTuple_New(0);
retval = PyEval_CallObject(method, argTuple);
m_myInst.clearRecursionGuard(method);
Py_DECREF(argTuple);
Py_DECREF(method);
if (retval == NULL)
// Don't PyErr_Print here, let the exception in this case go back
// up to the wx.PyApp.__init__ scope.
goto error;
pyint = PyNumber_Int(retval);
if (! pyint) {
PyErr_SetString(PyExc_TypeError, "OnInit should return a boolean value");
goto error;
}
result = PyInt_AS_LONG(pyint);
}
else {
// Is it okay if there is no OnInit? Probably so...
result = true;
}
if (! result) {
PyErr_SetString(PyExc_SystemExit, "OnInit returned false, exiting...");
}
else {
// On wxOSX_Cocoa a private m_inited flag is set in CallOnInit, (and
// pending events processed), and that flag controls things like
// whether the various apple-event virtuals in the App object are
// called. So although we have already bootstrapped a call to OnInit,
// we still need to call this method to ensure that flag is set.
if (! haveInitialized)
CallOnInit();
}
haveInitialized = true;
error:
Py_XDECREF(retval);
Py_XDECREF(pyint);
wxPyEndBlockThreads(blocked);
};
//---------------------------------------------------------------------
//----------------------------------------------------------------------
#if 0
static char* wxPyCopyCString(const wxChar* src)
{
wxWX2MBbuf buff = (wxWX2MBbuf)wxConvCurrent->cWX2MB(src);
size_t len = strlen(buff);
char* dest = new char[len+1];
strcpy(dest, buff);
return dest;
}
#if wxUSE_UNICODE
static char* wxPyCopyCString(const char* src) // we need a char version too
{
size_t len = strlen(src);
char* dest = new char[len+1];
strcpy(dest, src);
return dest;
}
#endif
static wxChar* wxPyCopyWString(const char *src)
{
//wxMB2WXbuf buff = wxConvCurrent->cMB2WX(src);
wxString str(src, *wxConvCurrent);
return copystring(str);
}
#if wxUSE_UNICODE
static wxChar* wxPyCopyWString(const wxChar *src)
{
return copystring(src);
}
#endif
#endif
inline const char* dropwx(const char* name) {
if (name[0] == 'w' && name[1] == 'x')
return name+2;
else
return name;
}
//----------------------------------------------------------------------
// This function is called when the wx._core_ module is imported to do some
// initial setup. (Before there is a wxApp object.) The rest happens in
// wxPyApp::_BootstrapApp
void __wxPyPreStart(PyObject* moduleDict)
{
#ifdef ISOLATION_AWARE_ENABLED
wxPySetActivationContext();
#endif
#ifdef __WXMSW__
// wxCrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF
// | _CRTDBG_CHECK_ALWAYS_DF
// | _CRTDBG_DELAY_FREE_MEM_DF
// );
#endif
#ifdef WXP_WITH_THREAD
#if wxPyUSE_GIL_STATE
PyEval_InitThreads();
#else
PyEval_InitThreads();
wxPyTStates = new wxPyThreadStateArray;
wxPyTMutex = new wxMutex;
// Save the current (main) thread state in our array
PyThreadState* tstate = wxPyBeginAllowThreads();
wxPyEndAllowThreads(tstate);
#endif
#endif
// Ensure that the build options in the DLL (or whatever) match this build
wxApp::CheckBuildOptions(WX_BUILD_OPTIONS_SIGNATURE, "wxPython");
wxInitAllImageHandlers();
}
void __wxPyCleanup() {
wxPyDoingCleanup = true;
if (wxPyDoCleanup) {
wxPyDoCleanup = false;
wxEntryCleanup();
}
#ifdef WXP_WITH_THREAD
#if !wxPyUSE_GIL_STATE
delete wxPyTMutex;
wxPyTMutex = NULL;
wxPyTStates->Empty();
delete wxPyTStates;
wxPyTStates = NULL;
#endif
#endif
}
// Save a reference to the dictionary of the wx._core module, and inject
// a few more things into it.
PyObject* __wxPySetDictionary(PyObject* /* self */, PyObject* args)
{
if (!PyArg_ParseTuple(args, "O", &wxPython_dict))
return NULL;
if (!PyDict_Check(wxPython_dict)) {
PyErr_SetString(PyExc_TypeError,
"_wxPySetDictionary must have dictionary object!");
return NULL;
}
if (! wxPyPtrTypeMap)
wxPyPtrTypeMap = PyDict_New();
PyDict_SetItemString(wxPython_dict, "__wxPyPtrTypeMap", wxPyPtrTypeMap);
// Create an exception object to use for wxASSERTions
wxPyAssertionError = PyErr_NewException("wx._core.PyAssertionError",
PyExc_AssertionError, NULL);
PyDict_SetItemString(wxPython_dict, "PyAssertionError", wxPyAssertionError);
// Create an exception object to use when the app object hasn't been created yet
wxPyNoAppError = PyErr_NewException("wx._core.PyNoAppError",
PyExc_RuntimeError, NULL);
PyDict_SetItemString(wxPython_dict, "PyNoAppError", wxPyNoAppError);
#ifdef __WXMOTIF__
#define wxPlatform "__WXMOTIF__"
#define wxPlatName "wxMotif"
#endif
#ifdef __WXX11__
#define wxPlatform "__WXX11__"
#define wxPlatName "wxX11"
#endif
#ifdef __WXGTK__
#define wxPlatform "__WXGTK__"
#define wxPlatName "wxGTK"
#endif
#ifdef __WXMSW__
#define wxPlatform "__WXMSW__"
#define wxPlatName "wxMSW"
#endif
#ifdef __WXMAC__
#define wxPlatform "__WXMAC__"
#define wxPlatName "wxMac"
#endif
#ifdef __WXDEBUG__
int wxdebug = 1;
#else
int wxdebug = 0;
#endif
// These should be deprecated in favor of the PlatformInfo tuple built below...
PyDict_SetItemString(wxPython_dict, "Platform", PyString_FromString(wxPlatform));
PyDict_SetItemString(wxPython_dict, "USE_UNICODE", PyInt_FromLong(wxUSE_UNICODE));
PyDict_SetItemString(wxPython_dict, "__WXDEBUG__", PyInt_FromLong(wxdebug));
// Make a tuple of strings that gives more info about the platform.
PyObject* PlatInfo = PyList_New(0);
PyObject* obj;
#define _AddInfoString(st) \
obj = PyString_FromString(st); \
PyList_Append(PlatInfo, obj); \
Py_DECREF(obj)
_AddInfoString(wxPlatform);
_AddInfoString(wxPlatName);
#if wxUSE_UNICODE
_AddInfoString("unicode");
#else
_AddInfoString("ansi");
#endif
#ifdef __WXOSX__
_AddInfoString("wxOSX");
#endif
#ifdef __WXOSX_CARBON__
_AddInfoString("wxOSX-carbon");
#endif
#ifdef __WXOSX_COCOA__
_AddInfoString("wxOSX-cocoa");
#endif
#ifdef __WXGTK__
#ifdef __WXGTK3__
_AddInfoString("gtk3");
#elif __WXGTK20__
_AddInfoString("gtk2");
#else
_AddInfoString("gtk1");
#endif
#endif
#ifdef __WXDEBUG__
_AddInfoString("wx-assertions-on");
#else
_AddInfoString("wx-assertions-off");
#endif
_AddInfoString(wxPy_SWIG_VERSION);
#undef _AddInfoString
PyObject* PlatInfoTuple = PyList_AsTuple(PlatInfo);
Py_DECREF(PlatInfo);
PyDict_SetItemString(wxPython_dict, "PlatformInfo", PlatInfoTuple);
RETURN_NONE();
}
//---------------------------------------------------------------------------
// Check for existence of a wxApp, setting an exception if there isn't one.
// This doesn't need to aquire the GIL because it should only be called from
// an %exception before the lock is released.
bool wxPyCheckForApp() {
if (wxTheApp != NULL)
return true;
else {
PyErr_SetString(wxPyNoAppError, "The wx.App object must be created first!");
return false;
}
}
//---------------------------------------------------------------------------
void wxPyUserData_dtor(wxPyUserData* self) {
if (wxPyDoingCleanup) {
self->ReleaseDataDuringCleanup();
} // otherwise let the wxPyUserDataHelper dtor cleanup
}
void wxPyClientData_dtor(wxPyClientData* self) {
if (wxPyDoingCleanup) { // Don't do it during cleanup as Python
// may have already garbage collected the object...
self->ReleaseDataDuringCleanup();
} // otherwise let the wxPyUserDataHelper dtor cleanup
}
// This is called when an OOR controled object is being destroyed. Although
// the C++ object is going away there is no way to force the Python object
// (and all references to it) to die too. This causes problems (crashes) in
// wxPython when a python shadow object attempts to call a C++ method using
// the now bogus pointer... So to try and prevent this we'll do a little black
// magic and change the class of the python instance to a class that will
// raise an exception for any attempt to call methods with it. See
// _wxPyDeadObject in _core_ex.py for the implementation of this class.
void wxPyOORClientData_dtor(wxPyOORClientData* self) {
static PyObject* deadObjectClass = NULL;
if (!wxPyDoingCleanup) {
wxPyThreadBlocker blocker;
if (deadObjectClass == NULL) {
deadObjectClass = PyDict_GetItemString(wxPython_dict, "_wxPyDeadObject");
// TODO: Can not wxASSERT here because inside a wxPyBeginBlock Threads,
// will lead to a deadlock when it tries to aquire the GIL again.
//wxASSERT_MSG(deadObjectClass != NULL, wxT("Can't get _wxPyDeadObject class!"));
Py_INCREF(deadObjectClass);
}
// Only if there is more than one reference to the object and we are
// holding the OOR reference:
PyObject* obj = self->BorrowData();
if (obj->ob_refcnt > 1 && self->m_incRef) {
// bool isInstance = wxPyInstance_Check(obj);
// TODO same here
//wxASSERT_MSG(isInstance, wxT("m_obj not an instance!?!?!"));
// Call __del__, if there is one.
PyObject* func = PyObject_GetAttrString(obj, "__del__");
if (func) {
PyObject* rv = PyObject_CallMethod(obj, "__del__", NULL);
Py_XDECREF(rv);
Py_DECREF(func);
}
if (PyErr_Occurred())
PyErr_Clear(); // just ignore it for now
PyObject* dict = PyObject_GetAttrString(obj, "__dict__");
if (dict) {
// Clear the instance's dictionary
PyDict_Clear(dict);
// put the name of the old class into the instance, and then reset the
// class to be the dead class.
PyObject* klass = PyObject_GetAttrString(obj, "__class__");
PyObject* name = PyObject_GetAttrString(klass, "__name__");
PyDict_SetItemString(dict, "_name", name);
PyObject_SetAttrString(obj, "__class__", deadObjectClass);
//Py_INCREF(deadObjectClass);
Py_DECREF(klass);
Py_DECREF(name);
Py_DECREF(dict);
}
}
if (self->m_incRef) {
Py_DECREF(obj);
}
}
self->ReleaseDataDuringCleanup();
}
//---------------------------------------------------------------------------
// Stuff used by OOR to find the right wxPython class type to return and to
// build it.