]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/src/helpers.h
Fixed wxImage GetData and SetData to properly use String objects for
[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);
8bf5d46e 48//# void AfterMainLoop(void);
7bf85405
RD
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
7bf85405 61void wxPyEventThunker(wxObject*, wxEvent& event);
8bf5d46e 62PyObject* wxPyConstructObject(void* ptr, char* className);
7bf85405
RD
63
64//----------------------------------------------------------------------
65
66
67#ifndef SWIGCODE
68extern "C" void SWIG_MakePtr(char *, void *, char *);
69extern "C" char *SWIG_GetPtr(char *, void **, char *);
70#endif
71
72
73#ifdef _MSC_VER
74# pragma warning(disable:4800)
75#endif
76
b639c3c5
RD
77typedef unsigned char byte;
78
7bf85405
RD
79
80// Non-const versions to keep SWIG happy.
81extern wxPoint wxPyDefaultPosition;
82extern wxSize wxPyDefaultSize;
7bf85405
RD
83extern wxString wxPyEmptyStr;
84
85//----------------------------------------------------------------------
86
87class wxPyCallback : public wxObject {
88public:
cf694132
RD
89 wxPyCallback(PyObject* func);
90 ~wxPyCallback();
7bf85405
RD
91
92 void EventThunker(wxEvent& event);
93
94 PyObject* m_func;
95};
96
97//---------------------------------------------------------------------------
98
8bf5d46e
RD
99// class wxPyMenu : public wxMenu {
100// public:
101// wxPyMenu(const wxString& title = "", PyObject* func=NULL);
102// ~wxPyMenu();
7bf85405 103
8bf5d46e
RD
104// private:
105// static void MenuCallback(wxMenu& menu, wxCommandEvent& evt);
106// PyObject* func;
107// };
714e6a9e 108
7bf85405
RD
109
110//---------------------------------------------------------------------------
111
112class wxPyTimer : public wxTimer {
113public:
114 wxPyTimer(PyObject* callback);
115 ~wxPyTimer();
116
117 void Notify();
118
119private:
120 PyObject* func;
121};
122
cf694132
RD
123//---------------------------------------------------------------------------
124
125class wxPyEvent : public wxCommandEvent {
126 DECLARE_DYNAMIC_CLASS(wxPyEvent)
127public:
128 wxPyEvent(wxEventType commandType = wxEVT_NULL, PyObject* userData = Py_None);
129 ~wxPyEvent();
130
131 void SetUserData(PyObject* userData);
132 PyObject* GetUserData();
133
134private:
135 PyObject* m_userData;
136};
137
bb0054cd
RD
138
139
140
141
142//---------------------------------------------------------------------------
143// This class holds an instance of a Python Shadow Class object and assists
144// with looking up and invoking Python callback methods from C++ virtual
145// method redirections. For all classes which have virtuals which should be
146// overridable in wxPython, a new subclass is created that contains a
a08cbc01 147// wxPyCallbackHelper.
bb0054cd
RD
148//---------------------------------------------------------------------------
149
150class wxPyCallbackHelper {
151public:
152 wxPyCallbackHelper();
153 ~wxPyCallbackHelper();
154
155 void setSelf(PyObject* self);
156
157 bool findCallback(const wxString& name);
158 int callCallback(PyObject* argTuple);
159 PyObject* callCallbackObj(PyObject* argTuple);
160
161private:
162 PyObject* m_self;
163 PyObject* m_lastFound;
164};
165
166
167
168//---------------------------------------------------------------------------
169// These macros are used to implement the virtual methods that should
170// redirect to a Python method if one exists. The names designate the
171// return type, if any as well as any parameter types.
172//---------------------------------------------------------------------------
173
174#define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \
175 bool CBNAME(int a, int b) { \
176 if (m_myInst.findCallback(#CBNAME)) \
177 return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \
178 else \
179 return PCLASS::CBNAME(a,b); \
180 } \
181 bool base_##CBNAME(int a, int b) { \
182 return PCLASS::CBNAME(a,b); \
183 }
184
185//---------------------------------------------------------------------------
186
187#define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \
188 bool CBNAME(int a) { \
189 if (m_myInst.findCallback(#CBNAME)) \
190 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
191 else \
192 return PCLASS::CBNAME(a); \
193 } \
194 bool base_##CBNAME(int a) { \
195 return PCLASS::CBNAME(a); \
196 }
197
198#define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \
199 bool CBNAME(int a) { \
200 if (m_myInst.findCallback(#CBNAME)) \
201 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
202 else return false; \
203 }
204
205
206//---------------------------------------------------------------------------
207
208#define PYCALLBACK__(PCLASS, CBNAME) \
209 void CBNAME() { \
210 if (m_myInst.findCallback(#CBNAME)) \
211 m_myInst.callCallback(Py_BuildValue("()")); \
212 else \
213 PCLASS::CBNAME(); \
214 } \
215 void base_##CBNAME() { \
216 PCLASS::CBNAME(); \
217 }
218
219//---------------------------------------------------------------------------
220
221#define PYPRIVATE \
222 void _setSelf(PyObject* self) { \
223 m_myInst.setSelf(self); \
224 } \
225 private: wxPyCallbackHelper m_myInst;
226
7bf85405 227//---------------------------------------------------------------------------
7bf85405
RD
228
229#endif
230