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