File: dllwidget.cpp

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (123 lines) | stat: -rw-r--r-- 3,163 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
/////////////////////////////////////////////////////////////////////////////
// Name:        dllwidget.cpp
// 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
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#include "wx/defs.h"
#include "wx/dynlib.h"
#include "wx/sizer.h"

#include "dllwidget.h"


IMPLEMENT_ABSTRACT_CLASS(wxDllWidget, wxPanel)

wxDllWidget::wxDllWidget(wxWindow *parent,
                         wxWindowID id,
                         const wxString& dllName, const wxString& className,
                         const wxPoint& pos, const wxSize& size,
                         long style)
    : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL | wxNO_BORDER,
              className + wxT("_container")),
    m_widget(NULL), m_lib(NULL), m_controlAdded(false)
{
    SetBackgroundColour(wxColour(255, 0, 255));
    if ( !!className )
        LoadWidget(dllName, className, style);
}

wxDllWidget::~wxDllWidget()
{
    UnloadWidget();
}

void wxDllWidget::AddChild(wxWindowBase *child)
{
    wxASSERT_MSG( !m_controlAdded, wxT("Couldn't load two widgets into one container!") );

    wxPanel::AddChild(child);

    m_controlAdded = true;
    wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
    sizer->Add((wxWindow*)child, 1, wxEXPAND);
    SetSizer(sizer);
    Layout();
}


wxString wxDllWidget::GetDllExt()
{
    return wxDllLoader::GetDllExt();
}


typedef WXDLLEXPORT bool (*DLL_WidgetFactory_t)(const wxString& className,
                                                wxWindow *parent,
                                                long style,
                                                wxWindow **classInst,
                                                wxSendCommandFunc *cmdFunc);

bool wxDllWidget::LoadWidget(const wxString& dll, const wxString& className,
                             long style)
{
    UnloadWidget();

    // Load the dynamic library
    m_lib = new wxDynamicLibrary(dll);
    if ( !m_lib->IsLoaded() )
    {
        delete m_lib;
        m_lib = NULL;
        return false;
    }

    DLL_WidgetFactory_t factory;
    factory = (DLL_WidgetFactory_t) m_lib->GetSymbol(wxT("DLL_WidgetFactory"));
    if ( factory == NULL)
    {
        delete m_lib;
        m_lib = NULL;
        return false;
    }

    if ( !factory(className, this, style, &m_widget, &m_cmdFunc) )
    {
        delete m_widget;
        delete m_lib;
        m_lib = NULL;
        m_widget = NULL;
        return false;
    }

    return true;
}

void wxDllWidget::UnloadWidget()
{
    if ( m_widget )
    {
        DestroyChildren();
        m_widget = NULL;
        delete m_lib;
        m_lib = NULL;
    }
}

int wxDllWidget::SendCommand(int cmd, const wxString& param)
{
    wxASSERT_MSG( m_widget && m_cmdFunc, wxT("Sending command to not loaded widget!"));

    return m_cmdFunc(m_widget, cmd, param);
}