]> git.saurik.com Git - wxWidgets.git/blob - wxPython/contrib/dllwidget/dllwidget.cpp
fixed wxVsnprintf() to write as much as it can if the output buffer is too short
[wxWidgets.git] / wxPython / contrib / dllwidget / dllwidget.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dllwidget.cpp
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 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #include "wx/defs.h"
19 #include "wx/dynlib.h"
20 #include "wx/sizer.h"
21
22 #include "dllwidget.h"
23
24
25 IMPLEMENT_ABSTRACT_CLASS(wxDllWidget, wxPanel)
26
27 wxDllWidget::wxDllWidget(wxWindow *parent,
28 wxWindowID id,
29 const wxString& dllName, const wxString& className,
30 const wxPoint& pos, const wxSize& size,
31 long style)
32 : wxPanel(parent, id, pos, size, wxTAB_TRAVERSAL | wxNO_BORDER,
33 className + wxT("_container")),
34 m_widget(NULL), m_lib(NULL), m_controlAdded(false)
35 {
36 SetBackgroundColour(wxColour(255, 0, 255));
37 if ( !!className )
38 LoadWidget(dllName, className, style);
39 }
40
41 wxDllWidget::~wxDllWidget()
42 {
43 UnloadWidget();
44 }
45
46 void wxDllWidget::AddChild(wxWindowBase *child)
47 {
48 wxASSERT_MSG( !m_controlAdded, wxT("Couldn't load two widgets into one container!") );
49
50 wxPanel::AddChild(child);
51
52 m_controlAdded = true;
53 wxSizer *sizer = new wxBoxSizer(wxHORIZONTAL);
54 sizer->Add((wxWindow*)child, 1, wxEXPAND);
55 SetSizer(sizer);
56 Layout();
57 }
58
59
60 wxString wxDllWidget::GetDllExt()
61 {
62 return wxDllLoader::GetDllExt();
63 }
64
65
66 typedef WXDLLEXPORT bool (*DLL_WidgetFactory_t)(const wxString& className,
67 wxWindow *parent,
68 long style,
69 wxWindow **classInst,
70 wxSendCommandFunc *cmdFunc);
71
72 bool wxDllWidget::LoadWidget(const wxString& dll, const wxString& className,
73 long style)
74 {
75 UnloadWidget();
76
77 // Load the dynamic library
78 m_lib = new wxDynamicLibrary(dll);
79 if ( !m_lib->IsLoaded() )
80 {
81 delete m_lib;
82 m_lib = NULL;
83 return false;
84 }
85
86 DLL_WidgetFactory_t factory;
87 factory = (DLL_WidgetFactory_t) m_lib->GetSymbol(wxT("DLL_WidgetFactory"));
88 if ( factory == NULL)
89 {
90 delete m_lib;
91 m_lib = NULL;
92 return false;
93 }
94
95 if ( !factory(className, this, style, &m_widget, &m_cmdFunc) )
96 {
97 delete m_widget;
98 delete m_lib;
99 m_lib = NULL;
100 m_widget = NULL;
101 return false;
102 }
103
104 return true;
105 }
106
107 void wxDllWidget::UnloadWidget()
108 {
109 if ( m_widget )
110 {
111 DestroyChildren();
112 m_widget = NULL;
113 delete m_lib;
114 m_lib = NULL;
115 }
116 }
117
118 int wxDllWidget::SendCommand(int cmd, const wxString& param)
119 {
120 wxASSERT_MSG( m_widget && m_cmdFunc, wxT("Sending command to not loaded widget!"));
121
122 return m_cmdFunc(m_widget, cmd, param);
123 }