]> git.saurik.com Git - wxWidgets.git/blame - utils/wxPython/src/helpers.h
fixed font-matching bug in wxFontList::FindOrCreateFont for wxGTK when
[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
a08cbc01 149// wxPyCallbackHelper.
bb0054cd
RD
150//---------------------------------------------------------------------------
151
152class wxPyCallbackHelper {
153public:
154 wxPyCallbackHelper();
155 ~wxPyCallbackHelper();
156
157 void setSelf(PyObject* self);
158
159 bool findCallback(const wxString& name);
160 int callCallback(PyObject* argTuple);
161 PyObject* callCallbackObj(PyObject* argTuple);
162
163private:
164 PyObject* m_self;
165 PyObject* m_lastFound;
166};
167
168
169
170//---------------------------------------------------------------------------
171// These macros are used to implement the virtual methods that should
172// redirect to a Python method if one exists. The names designate the
173// return type, if any as well as any parameter types.
174//---------------------------------------------------------------------------
175
176#define PYCALLBACK_BOOL_INTINT(PCLASS, CBNAME) \
177 bool CBNAME(int a, int b) { \
178 if (m_myInst.findCallback(#CBNAME)) \
179 return m_myInst.callCallback(Py_BuildValue("(ii)",a,b)); \
180 else \
181 return PCLASS::CBNAME(a,b); \
182 } \
183 bool base_##CBNAME(int a, int b) { \
184 return PCLASS::CBNAME(a,b); \
185 }
186
187//---------------------------------------------------------------------------
188
189#define PYCALLBACK_BOOL_INT(PCLASS, CBNAME) \
190 bool CBNAME(int a) { \
191 if (m_myInst.findCallback(#CBNAME)) \
192 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
193 else \
194 return PCLASS::CBNAME(a); \
195 } \
196 bool base_##CBNAME(int a) { \
197 return PCLASS::CBNAME(a); \
198 }
199
200#define PYCALLBACK_BOOL_INT_pure(PCLASS, CBNAME) \
201 bool CBNAME(int a) { \
202 if (m_myInst.findCallback(#CBNAME)) \
203 return m_myInst.callCallback(Py_BuildValue("(i)",a)); \
204 else return false; \
205 }
206
207
208//---------------------------------------------------------------------------
209
210#define PYCALLBACK__(PCLASS, CBNAME) \
211 void CBNAME() { \
212 if (m_myInst.findCallback(#CBNAME)) \
213 m_myInst.callCallback(Py_BuildValue("()")); \
214 else \
215 PCLASS::CBNAME(); \
216 } \
217 void base_##CBNAME() { \
218 PCLASS::CBNAME(); \
219 }
220
221//---------------------------------------------------------------------------
222
223#define PYPRIVATE \
224 void _setSelf(PyObject* self) { \
225 m_myInst.setSelf(self); \
226 } \
227 private: wxPyCallbackHelper m_myInst;
228
7bf85405 229//---------------------------------------------------------------------------
7bf85405
RD
230
231#endif
232