]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/src/helpers.h
Replaced ostream with FILE* in wxExpr.
[wxWidgets.git] / utils / wxPython / src / helpers.h
CommitLineData
7bf85405
RD
1/////////////////////////////////////////////////////////////////////////////
2// Name: helpers.h
3// Purpose: Helper functions/classes for the wxPython extenaion module
4//
5// Author: Robin Dunn
6//
7// Created: 7/1/97
8// RCS-ID: $Id$
9// Copyright: (c) 1998 by Total Control Software
10// Licence: wxWindows license
11/////////////////////////////////////////////////////////////////////////////
12
13#ifndef __wxp_helpers__
14#define __wxp_helpers__
15
16#include <wx/wx.h>
17
18
cf694132
RD
19//----------------------------------------------------------------------
20
21// if we want to handle threads and Python threads are available...
22#if defined(WXP_USE_THREAD) && defined(WITH_THREAD)
23
24#define WXP_WITH_THREAD
25#define wxPy_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS
26#define wxPy_END_ALLOW_THREADS Py_END_ALLOW_THREADS
27
28#else // no Python threads...
29#undef WXP_WITH_THREAD
30#define wxPy_BEGIN_ALLOW_THREADS
31#define wxPy_END_ALLOW_THREADS
32#endif
33
bb0054cd
RD
34#ifdef WXP_WITH_THREAD
35extern PyThreadState* wxPyEventThreadState;
36extern bool wxPyInEvent;
37#endif
cf694132 38
7bf85405
RD
39//----------------------------------------------------------------------
40
41class wxPyApp: public wxApp
42{
43public:
cf694132
RD
44 wxPyApp();
45 ~wxPyApp();
7bf85405
RD
46 int MainLoop(void);
47 bool OnInit(void);
48 void AfterMainLoop(void);
49};
50
51extern wxPyApp *wxPythonApp;
52
53//----------------------------------------------------------------------
54
0d6f9504 55void __wxPreStart();
7bf85405
RD
56PyObject* __wxStart(PyObject*, PyObject* args);
57
58extern PyObject* wxPython_dict;
59PyObject* __wxSetDictionary(PyObject*, PyObject* args);
60
61extern wxHashTable* wxPyWindows; // keep track of all windows so we
62 // don't accidentally delete them twice.
63
64void wxPyEventThunker(wxObject*, wxEvent& event);
65
66//----------------------------------------------------------------------
67
68
69#ifndef SWIGCODE
70extern "C" void SWIG_MakePtr(char *, void *, char *);
71extern "C" char *SWIG_GetPtr(char *, void **, char *);
72#endif
73
74
75#ifdef _MSC_VER
76# pragma warning(disable:4800)
77#endif
78
b639c3c5
RD
79typedef unsigned char byte;
80
7bf85405
RD
81
82// Non-const versions to keep SWIG happy.
83extern wxPoint wxPyDefaultPosition;
84extern wxSize wxPyDefaultSize;
7bf85405
RD
85extern wxString wxPyEmptyStr;
86
87//----------------------------------------------------------------------
88
89class wxPyCallback : public wxObject {
90public:
cf694132
RD
91 wxPyCallback(PyObject* func);
92 ~wxPyCallback();
7bf85405
RD
93
94 void EventThunker(wxEvent& event);
95
96 PyObject* m_func;
97};
98
99//---------------------------------------------------------------------------
100
101class wxPyMenu : public wxMenu {
102public:
103 wxPyMenu(const wxString& title = "", PyObject* func=NULL);
104 ~wxPyMenu();
105
106private:
107 static void MenuCallback(wxMenu& menu, wxCommandEvent& evt);
108 PyObject* func;
109};
714e6a9e 110
7bf85405
RD
111
112//---------------------------------------------------------------------------
113
114class wxPyTimer : public wxTimer {
115public:
116 wxPyTimer(PyObject* callback);
117 ~wxPyTimer();
118
119 void Notify();
120
121private:
122 PyObject* func;
123};
124
cf694132
RD
125//---------------------------------------------------------------------------
126
127class wxPyEvent : public wxCommandEvent {
128 DECLARE_DYNAMIC_CLASS(wxPyEvent)
129public:
130 wxPyEvent(wxEventType commandType = wxEVT_NULL, PyObject* userData = Py_None);
131 ~wxPyEvent();
132
133 void SetUserData(PyObject* userData);
134 PyObject* GetUserData();
135
136private:
137 PyObject* m_userData;
138};
139
bb0054cd
RD
140
141
142
143
144//---------------------------------------------------------------------------
145// This class holds an instance of a Python Shadow Class object and assists
146// with looking up and invoking Python callback methods from C++ virtual
147// method redirections. For all classes which have virtuals which should be
148// overridable in wxPython, a new subclass is created that contains a
149// wxPyCallbackList. This list is used to hold references to the Python
150// methods.
151//---------------------------------------------------------------------------
152
153class wxPyCallbackHelper {
154public:
155 wxPyCallbackHelper();
156 ~wxPyCallbackHelper();
157
158 void setSelf(PyObject* self);
159
160 bool findCallback(const wxString& name);
161 int callCallback(PyObject* argTuple);
162 PyObject* callCallbackObj(PyObject* argTuple);
163
164private:
165 PyObject* m_self;
166 PyObject* m_lastFound;
167};
168
169
170
171//---------------------------------------------------------------------------
172// These macros are used to implement the virtual methods that should
173// redirect to a Python method if one exists. The names designate the
174// return type, if any as well as any parameter types.
175//---------------------------------------------------------------------------
176
177#define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \
178 bool CBNAME(int a, int b) { \
179 if (m_myInst.findCallback(#CBNAME)) \
180 return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \
181 else \
182 return PCLASS::CBNAME(a,b); \
183 } \
184 bool base_##CBNAME(int a, int b) { \
185 return PCLASS::CBNAME(a,b); \
186 }
187
188//---------------------------------------------------------------------------
189
190#define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \
191 bool CBNAME(int a) { \
192 if (m_myInst.findCallback(#CBNAME)) \
193 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
194 else \
195 return PCLASS::CBNAME(a); \
196 } \
197 bool base_##CBNAME(int a) { \
198 return PCLASS::CBNAME(a); \
199 }
200
201#define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \
202 bool CBNAME(int a) { \
203 if (m_myInst.findCallback(#CBNAME)) \
204 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
205 else return false; \
206 }
207
208
209//---------------------------------------------------------------------------
210
211#define PYCALLBACK__(PCLASS, CBNAME) \
212 void CBNAME() { \
213 if (m_myInst.findCallback(#CBNAME)) \
214 m_myInst.callCallback(Py_BuildValue("()")); \
215 else \
216 PCLASS::CBNAME(); \
217 } \
218 void base_##CBNAME() { \
219 PCLASS::CBNAME(); \
220 }
221
222//---------------------------------------------------------------------------
223
224#define PYPRIVATE \
225 void _setSelf(PyObject* self) { \
226 m_myInst.setSelf(self); \
227 } \
228 private: wxPyCallbackHelper m_myInst;
229
7bf85405 230//---------------------------------------------------------------------------
7bf85405
RD
231
232#endif
233