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