1 ///////////////////////////////////////////////////////////////////////////// 
   3 // Purpose:     Dynamically loadable C++ widget for wxPython 
   4 // Author:      Vaclav Slavik 
   7 // Copyright:   (c) 2001 Vaclav Slavik 
   8 // Licence:     wxWindows licence 
   9 ///////////////////////////////////////////////////////////////////////////// 
  11 #ifndef __DLLWIDGET_H__ 
  12 #define __DLLWIDGET_H__ 
  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. 
  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. 
  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 
  34     #define CMD_MAKEWHITE     1 
  36     class MyWindow : public wxWindow 
  39         MyWindow(wxWindow *parent, long style) 
  40             : wxWindow(parent, wxID_ANY) {} 
  42         int HandleCommand(int cmd, const wxString& param) 
  44             if (cmd == CMD_MAKEWHITE) 
  45                 SetBackgroundColour(*wxWHITE); 
  49     DECLARE_DLL_WIDGET(MyWindow) 
  51     class MyCanvasWindow : public wxScrolledWindow 
  55     DECLARE_DLL_WIDGET(MyCanvasWindow) 
  57     BEGIN_WIDGET_LIBRARY() 
  58         REGISTER_WIDGET(MyWindow) 
  59         REGISTER_WIDGET(MyCanvasWindow) 
  66 class WXDLLEXPORT wxDynamicLibrary
; 
  68 typedef int (*wxSendCommandFunc
)(wxWindow 
*wnd
, int cmd
, const wxString
& param
); 
  70 class wxDllWidget 
: public wxPanel
 
  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
, 
  80     virtual ~wxDllWidget(); 
  82     bool Ok() { return m_widget 
!= NULL
; } 
  84     virtual int SendCommand(int cmd
, const wxString
& param 
= wxEmptyString
); 
  85     virtual wxWindow
* GetWidgetWindow() { return m_widget
; } 
  87     virtual void AddChild(wxWindowBase 
*child
); 
  89     static wxString 
GetDllExt(); 
  93     bool LoadWidget(const wxString
& dll
, const wxString
& className
, long style
); 
  98     wxSendCommandFunc   m_cmdFunc
; 
  99     wxDynamicLibrary
*   m_lib
; 
 103     DECLARE_ABSTRACT_CLASS(wxDllWidget
) 
 107 #define DECLARE_DLL_WIDGET(widget) \ 
 108     static int SendCommandTo##widget(wxWindow *wnd, int cmd, const wxString& param) \ 
 110         return wxStaticCast(wnd, widget)->HandleCommand(cmd, param); \ 
 114 #define BEGIN_WIDGET_LIBRARY() \ 
 115     extern "C" WXEXPORT bool DLL_WidgetFactory( \ 
 116                 const wxString& className, \ 
 119                 wxWindow **classInst, \ 
 120                 wxSendCommandFunc *cmdFunc) \ 
 122         wxClassInfo::CleanUpClasses(); \ 
 123         wxClassInfo::InitializeClasses(); 
 126 #define REGISTER_WIDGET(widget) \ 
 127         if ( className == wxT(#widget) ) \ 
 129             *classInst = new widget(parent, style); \ 
 130             *cmdFunc = SendCommandTo##widget; \ 
 135 #define END_WIDGET_LIBRARY() \ 
 139 #endif // __DLLWIDGET_H__