]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: dllwidget.h | |
3 | // Purpose: Dynamically loadable C++ widget for wxPython | |
4 | // Author: Vaclav Slavik | |
5 | // Created: 2001/12/03 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2001 Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma interface "dllwidget.h" | |
13 | #endif | |
14 | ||
15 | #ifndef __DLLWIDGET_H__ | |
16 | #define __DLLWIDGET_H__ | |
17 | ||
18 | #include "wx/panel.h" | |
19 | ||
20 | /* | |
21 | ||
22 | wxDllWidget can be used to embed a wxWindow implemented in C++ in your | |
23 | wxPython application without the need to write a SWIG interface. Widget's code | |
24 | is stored in shared library or DLL that exports DLL_WidgetFactory symbol | |
25 | and loaded at runtime. All you have to do is to pass the name of DLL and the class | |
26 | to create to wxDllWidget's ctor. | |
27 | ||
28 | Runtime-loadable widget must have HandleCommand method (see the example) that is | |
29 | used to communicate with Python app. You call wxDllWidget.SendCommand(cmd,param) from | |
30 | Python and it in turn calls HandleCommand of the loaded widget. | |
31 | ||
32 | You must use DECLARE_DLL_WIDGET, BEGIN_WIDGET_LIBRARY, END_WIDGET_LIBRARY and | |
33 | REGISTER_WIDGET macros in your C++ module in order to provide all the meat | |
34 | wxDllWidget needs. | |
35 | ||
36 | Example of use: | |
37 | ||
38 | #define CMD_MAKEWHITE 1 | |
39 | ||
40 | class MyWindow : public wxWindow | |
41 | { | |
42 | public: | |
43 | MyWindow(wxWindow *parent, long style) | |
44 | : wxWindow(parent, -1) {} | |
45 | ||
46 | int HandleCommand(int cmd, const wxString& param) | |
47 | { | |
48 | if (cmd == CMD_MAKEWHITE) | |
49 | SetBackgroundColour(*wxWHITE); | |
50 | return 0; | |
51 | } | |
52 | }; | |
53 | DECLARE_DLL_WIDGET(MyWindow) | |
54 | ||
55 | class MyCanvasWindow : public wxScrolledWindow | |
56 | { | |
57 | ... | |
58 | }; | |
59 | DECLARE_DLL_WIDGET(MyCanvasWindow) | |
60 | ||
61 | BEGIN_WIDGET_LIBRARY() | |
62 | REGISTER_WIDGET(MyWindow) | |
63 | REGISTER_WIDGET(MyCanvasWindow) | |
64 | END_WIDGET_LIBRARY() | |
65 | ||
66 | */ | |
67 | ||
68 | ||
69 | ||
70 | class WXDLLEXPORT wxDynamicLibrary; | |
71 | ||
72 | typedef int (*wxSendCommandFunc)(wxWindow *wnd, int cmd, const wxString& param); | |
73 | ||
74 | class wxDllWidget : public wxPanel | |
75 | { | |
76 | public: | |
77 | wxDllWidget(wxWindow *parent, | |
78 | wxWindowID id = -1, | |
79 | const wxString& dllName = wxEmptyString, | |
80 | const wxString& className = wxEmptyString, | |
81 | const wxPoint& pos = wxDefaultPosition, | |
82 | const wxSize& size = wxDefaultSize, | |
83 | long style = 0); | |
84 | virtual ~wxDllWidget(); | |
85 | ||
86 | bool Ok() { return m_widget != NULL; } | |
87 | ||
88 | virtual int SendCommand(int cmd, const wxString& param = wxEmptyString); | |
89 | virtual wxWindow* GetWidgetWindow() { return m_widget; } | |
90 | ||
91 | virtual void AddChild(wxWindowBase *child); | |
92 | ||
93 | static wxString GetDllExt(); | |
94 | ||
95 | ||
96 | protected: | |
97 | bool LoadWidget(const wxString& dll, const wxString& className, long style); | |
98 | void UnloadWidget(); | |
99 | ||
100 | protected: | |
101 | wxWindow* m_widget; | |
102 | wxSendCommandFunc m_cmdFunc; | |
103 | wxDynamicLibrary* m_lib; | |
104 | bool m_controlAdded; | |
105 | ||
106 | private: | |
107 | DECLARE_ABSTRACT_CLASS(wxDllWidget) | |
108 | }; | |
109 | ||
110 | ||
111 | #define DECLARE_DLL_WIDGET(widget) \ | |
112 | static int SendCommandTo##widget(wxWindow *wnd, int cmd, const wxString& param) \ | |
113 | { \ | |
114 | return wxStaticCast(wnd, widget)->HandleCommand(cmd, param); \ | |
115 | } | |
116 | ||
117 | ||
118 | #define BEGIN_WIDGET_LIBRARY() \ | |
119 | extern "C" WXEXPORT bool DLL_WidgetFactory( \ | |
120 | const wxString& className, \ | |
121 | wxWindow *parent, \ | |
122 | long style, \ | |
123 | wxWindow **classInst, \ | |
124 | wxSendCommandFunc *cmdFunc) \ | |
125 | { \ | |
126 | wxClassInfo::CleanUpClasses(); \ | |
127 | wxClassInfo::InitializeClasses(); | |
128 | ||
129 | ||
130 | #define REGISTER_WIDGET(widget) \ | |
131 | if ( className == wxT(#widget) ) \ | |
132 | { \ | |
133 | *classInst = new widget(parent, style); \ | |
134 | *cmdFunc = SendCommandTo##widget; \ | |
135 | return TRUE; \ | |
136 | } | |
137 | ||
138 | ||
139 | #define END_WIDGET_LIBRARY() \ | |
140 | return FALSE; \ | |
141 | } | |
142 | ||
143 | #endif // __DLLWIDGET_H__ |