File: dllwidget.h

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 482,632 kB
  • sloc: cpp: 2,127,216; python: 294,089; makefile: 51,946; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (139 lines) | stat: -rw-r--r-- 3,907 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/////////////////////////////////////////////////////////////////////////////
// Name:        dllwidget.h
// Purpose:     Dynamically loadable C++ widget for wxPython
// Author:      Vaclav Slavik
// Created:     2001/12/03
// RCS-ID:      $Id$
// Copyright:   (c) 2001 Vaclav Slavik
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

#ifndef __DLLWIDGET_H__
#define __DLLWIDGET_H__

#include "wx/panel.h"

/*

wxDllWidget can be used to embed a wxWindow implemented in C++ in your
wxPython application without the need to write a SWIG interface. Widget's code
is stored in shared library or DLL that exports DLL_WidgetFactory symbol
and loaded at runtime. All you have to do is to pass the name of DLL and the class
to create to wxDllWidget's ctor.

Runtime-loadable widget must have HandleCommand method (see the example) that is
used to communicate with Python app. You call wxDllWidget.SendCommand(cmd,param) from
Python and it in turn calls HandleCommand of the loaded widget.

You must use DECLARE_DLL_WIDGET, BEGIN_WIDGET_LIBRARY, END_WIDGET_LIBRARY and
REGISTER_WIDGET macros in your C++ module in order to provide all the meat
wxDllWidget needs.

Example of use:

    #define CMD_MAKEWHITE     1

    class MyWindow : public wxWindow
    {
    public:
        MyWindow(wxWindow *parent, long style)
            : wxWindow(parent, wxID_ANY) {}

        int HandleCommand(int cmd, const wxString& param)
        {
            if (cmd == CMD_MAKEWHITE)
                SetBackgroundColour(*wxWHITE);
            return 0;
        }
    };
    DECLARE_DLL_WIDGET(MyWindow)

    class MyCanvasWindow : public wxScrolledWindow
    {
        ...
    };
    DECLARE_DLL_WIDGET(MyCanvasWindow)

    BEGIN_WIDGET_LIBRARY()
        REGISTER_WIDGET(MyWindow)
        REGISTER_WIDGET(MyCanvasWindow)
    END_WIDGET_LIBRARY()

*/



class WXDLLEXPORT wxDynamicLibrary;

typedef int (*wxSendCommandFunc)(wxWindow *wnd, int cmd, const wxString& param);

class wxDllWidget : public wxPanel
{
public:
    wxDllWidget(wxWindow *parent,
                wxWindowID id = wxID_ANY,
                const wxString& dllName = wxEmptyString,
                const wxString& className = wxEmptyString,
                const wxPoint& pos = wxDefaultPosition,
                const wxSize& size = wxDefaultSize,
                long style = 0);
    virtual ~wxDllWidget();

    bool Ok() { return m_widget != NULL; }

    virtual int SendCommand(int cmd, const wxString& param = wxEmptyString);
    virtual wxWindow* GetWidgetWindow() { return m_widget; }

    virtual void AddChild(wxWindowBase *child);

    static wxString GetDllExt();


protected:
    bool LoadWidget(const wxString& dll, const wxString& className, long style);
    void UnloadWidget();

protected:
    wxWindow*           m_widget;
    wxSendCommandFunc   m_cmdFunc;
    wxDynamicLibrary*   m_lib;
    bool                m_controlAdded;

private:
    DECLARE_ABSTRACT_CLASS(wxDllWidget)
};


#define DECLARE_DLL_WIDGET(widget) \
    static int SendCommandTo##widget(wxWindow *wnd, int cmd, const wxString& param) \
    { \
        return wxStaticCast(wnd, widget)->HandleCommand(cmd, param); \
    }


#define BEGIN_WIDGET_LIBRARY() \
    extern "C" WXEXPORT bool DLL_WidgetFactory( \
                const wxString& className, \
                wxWindow *parent, \
                long style, \
                wxWindow **classInst, \
                wxSendCommandFunc *cmdFunc) \
    { \
        wxClassInfo::CleanUpClasses(); \
        wxClassInfo::InitializeClasses();


#define REGISTER_WIDGET(widget) \
        if ( className == wxT(#widget) ) \
        { \
            *classInst = new widget(parent, style); \
            *cmdFunc = SendCommandTo##widget; \
            return true; \
        }


#define END_WIDGET_LIBRARY() \
       return false; \
    }

#endif // __DLLWIDGET_H__