]> git.saurik.com Git - wxWidgets.git/blame - wxPython/contrib/dllwidget/dllwidget.h
Just to be sure...
[wxWidgets.git] / wxPython / contrib / dllwidget / dllwidget.h
CommitLineData
4a61305d
RD
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
22wxDllWidget can be used to embed a wxWindow implemented in C++ in your
23wxPython application without the need to write a SWIG interface. Widget's code
24is stored in shared library or DLL that exports DLL_WidgetFactory symbol
25and loaded at runtime. All you have to do is to pass the name of DLL and the class
26to create to wxDllWidget's ctor.
27
28Runtime-loadable widget must have HandleCommand method (see the example) that is
29used to communicate with Python app. You call wxDllWidget.SendCommand(cmd,param) from
30Python and it in turn calls HandleCommand of the loaded widget.
31
32You must use DECLARE_DLL_WIDGET, BEGIN_WIDGET_LIBRARY, END_WIDGET_LIBRARY and
33REGISTER_WIDGET macros in your C++ module in order to provide all the meat
34wxDllWidget needs.
35
36Example of use:
37
38 #define CMD_MAKEWHITE 1
39
40 class MyWindow : public wxWindow
41 {
42 public:
43 MyWindow(wxWindow *parent, long style)
dabbc6a5 44 : wxWindow(parent, wxID_ANY) {}
4a61305d
RD
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
70class WXDLLEXPORT wxDynamicLibrary;
71
72typedef int (*wxSendCommandFunc)(wxWindow *wnd, int cmd, const wxString& param);
73
74class wxDllWidget : public wxPanel
75{
76public:
77 wxDllWidget(wxWindow *parent,
dabbc6a5 78 wxWindowID id = wxID_ANY,
4a61305d
RD
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);
ff65119e 89 virtual wxWindow* GetWidgetWindow() { return m_widget; }
4a61305d
RD
90
91 virtual void AddChild(wxWindowBase *child);
92
93 static wxString GetDllExt();
94
95
96protected:
97 bool LoadWidget(const wxString& dll, const wxString& className, long style);
98 void UnloadWidget();
99
100protected:
101 wxWindow* m_widget;
102 wxSendCommandFunc m_cmdFunc;
103 wxDynamicLibrary* m_lib;
104 bool m_controlAdded;
105
106private:
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; \
dabbc6a5 135 return true; \
4a61305d
RD
136 }
137
138
139#define END_WIDGET_LIBRARY() \
dabbc6a5 140 return false; \
4a61305d
RD
141 }
142
143#endif // __DLLWIDGET_H__