1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamically loadable C++ widget for wxPython
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2001 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma interface "dllwidget.h"
15 #ifndef __DLLWIDGET_H__
16 #define __DLLWIDGET_H__
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.
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.
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
38 #define CMD_MAKEWHITE 1
40 class MyWindow : public wxWindow
43 MyWindow(wxWindow *parent, long style)
44 : wxWindow(parent, -1) {}
46 int HandleCommand(int cmd, const wxString& param)
48 if (cmd == CMD_MAKEWHITE)
49 SetBackgroundColour(*wxWHITE);
53 DECLARE_DLL_WIDGET(MyWindow)
55 class MyCanvasWindow : public wxScrolledWindow
59 DECLARE_DLL_WIDGET(MyCanvasWindow)
61 BEGIN_WIDGET_LIBRARY()
62 REGISTER_WIDGET(MyWindow)
63 REGISTER_WIDGET(MyCanvasWindow)
70 class WXDLLEXPORT wxDynamicLibrary
;
72 typedef int (*wxSendCommandFunc
)(wxWindow
*wnd
, int cmd
, const wxString
& param
);
74 class wxDllWidget
: public wxPanel
77 wxDllWidget(wxWindow
*parent
,
79 const wxString
& dllName
= wxEmptyString
,
80 const wxString
& className
= wxEmptyString
,
81 const wxPoint
& pos
= wxDefaultPosition
,
82 const wxSize
& size
= wxDefaultSize
,
84 virtual ~wxDllWidget();
86 bool Ok() { return m_widget
!= NULL
; }
88 virtual int SendCommand(int cmd
, const wxString
& param
= wxEmptyString
);
89 virtual wxWindow
* GetWidgetWindow() { return m_widget
; }
91 virtual void AddChild(wxWindowBase
*child
);
93 static wxString
GetDllExt();
97 bool LoadWidget(const wxString
& dll
, const wxString
& className
, long style
);
102 wxSendCommandFunc m_cmdFunc
;
103 wxDynamicLibrary
* m_lib
;
107 DECLARE_ABSTRACT_CLASS(wxDllWidget
)
111 #define DECLARE_DLL_WIDGET(widget) \
112 static int SendCommandTo##widget(wxWindow *wnd, int cmd, const wxString& param) \
114 return wxStaticCast(wnd, widget)->HandleCommand(cmd, param); \
118 #define BEGIN_WIDGET_LIBRARY() \
119 extern "C" WXEXPORT bool DLL_WidgetFactory( \
120 const wxString& className, \
123 wxWindow **classInst, \
124 wxSendCommandFunc *cmdFunc) \
126 wxClassInfo::CleanUpClasses(); \
127 wxClassInfo::InitializeClasses();
130 #define REGISTER_WIDGET(widget) \
131 if ( className == wxT(#widget) ) \
133 *classInst = new widget(parent, style); \
134 *cmdFunc = SendCommandTo##widget; \
139 #define END_WIDGET_LIBRARY() \
143 #endif // __DLLWIDGET_H__