]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
EVT_TEXT_UPDATED bug fixed, text ctrl callbacks simplified
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynlib.cpp
3// Purpose: Dynamic library management
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 20/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
7a4b9130 20#ifdef __GNUG__
7b0bfbb2 21 #pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b
JS
24#include "wx/wxprec.h"
25
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif //__BORLANDC__
29
8a0d4cf6
VZ
30#if wxUSE_DYNLIB_CLASS
31
7b0bfbb2
VZ
32#include "wx/dynlib.h"
33#include "wx/filefn.h"
34#include "wx/intl.h"
35#include "wx/log.h"
8a0d4cf6 36#include "wx/tokenzr.h"
7b0bfbb2
VZ
37
38// ----------------------------------------------------------------------------
39// conditional compilation
40// ----------------------------------------------------------------------------
41
42#if defined(HAVE_DLOPEN)
43 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
44 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
45 #define wxDllClose dlclose
8a0d4cf6
VZ
46#elif defined(HAVE_SHL_LOAD)
47 #define wxDllOpen(lib) shl_load(lib, BIND_DEFERRED, 0)
7b0bfbb2
VZ
48 #define wxDllClose shl_unload
49
8a0d4cf6 50 static inline void *wxDllGetSymbol(shl_t handle, const char *name)
7b0bfbb2
VZ
51 {
52 void *sym;
8a0d4cf6 53 if ( shl_findsym(&handle, name, TYPE_UNDEFINED, &sym) == 0 )
7b0bfbb2
VZ
54 return sym;
55 else
56 return (void *)0;
57 }
58#elif defined(__WINDOWS__)
90022ddc
VZ
59 #include <windows.h>
60
61 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
62 #ifdef __WIN32__
63 #define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
64 #else // Win16
65 #define wxDllOpen(lib) ::LoadLibrary(lib)
66 #endif // Win32/16
7b0bfbb2
VZ
67 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
68 #define wxDllClose ::FreeLibrary
69#else
70 #error "Don't know how to load shared libraries on this platform."
71#endif // OS
7a4b9130
GL
72
73// ---------------------------------------------------------------------------
7b0bfbb2 74// Global variables
7a4b9130
GL
75// ---------------------------------------------------------------------------
76
7b0bfbb2 77wxLibraries wxTheLibraries;
7a4b9130 78
7b0bfbb2
VZ
79// ----------------------------------------------------------------------------
80// private functions
81// ----------------------------------------------------------------------------
123a7fdd 82
7b0bfbb2
VZ
83// construct the full name from the base shared object name: adds a .dll
84// suffix under Windows or .so under Unix
85static wxString ConstructLibraryName(const wxString& basename)
86{
87 wxString fullname(basename);
88
89#if defined(__UNIX__)
8a0d4cf6
VZ
90 #if defined(__HPUX__)
91 fullname << ".sl";
92 #else //__HPUX__
93 fullname << ".so";
94 #endif //__HPUX__
7b0bfbb2
VZ
95#elif defined(__WINDOWS__)
96 fullname << ".dll";
27529614
JS
97#endif
98
7b0bfbb2
VZ
99 return fullname;
100}
7a4b9130 101
7b0bfbb2
VZ
102// ============================================================================
103// implementation
104// ============================================================================
7a4b9130
GL
105
106// ---------------------------------------------------------------------------
7b0bfbb2 107// wxLibrary (one instance per dynamic library)
7a4b9130
GL
108// ---------------------------------------------------------------------------
109
7b0bfbb2 110wxLibrary::wxLibrary(wxDllType handle)
7a4b9130 111{
7b0bfbb2
VZ
112 typedef wxClassInfo *(*t_get_first)(void);
113 t_get_first get_first;
7a4b9130 114
7b0bfbb2 115 m_handle = handle;
7a4b9130 116
7b0bfbb2
VZ
117 // Some system may use a local heap for library.
118 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
119 // It is a wxWindows DLL.
120 if (get_first)
121 PrepareClasses(get_first());
7a4b9130
GL
122}
123
124wxLibrary::~wxLibrary()
125{
7b0bfbb2
VZ
126 if ( m_handle )
127 {
128 wxDllClose(m_handle);
129 }
7a4b9130
GL
130}
131
132wxObject *wxLibrary::CreateObject(const wxString& name)
133{
7b0bfbb2 134 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
f4a8c29f 135
7b0bfbb2
VZ
136 if (!info)
137 return NULL;
f4a8c29f 138
7b0bfbb2 139 return info->CreateObject();
f4a8c29f
GL
140}
141
856d2e52 142void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f 143{
7b0bfbb2
VZ
144 // Index all class infos by their class name
145 wxClassInfo *info = first;
146 while (info)
147 {
148 if (info->m_className)
149 classTable.Put(info->m_className, (wxObject *)info);
150 info = info->GetNext();
151 }
152
153 // Set base pointers for each wxClassInfo
154 info = first;
155 while (info)
156 {
157 if (info->GetBaseClassName1())
158 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
159 if (info->GetBaseClassName2())
160 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
161 info = info->m_next;
162 }
7a4b9130
GL
163}
164
165void *wxLibrary::GetSymbol(const wxString& symbname)
166{
7b0bfbb2
VZ
167 void *symbol = NULL; // return value
168
169#if defined( __WXMAC__ )
170 Ptr symAddress ;
171 CFragSymbolClass symClass ;
172 Str255 symName ;
173
174 strcpy( (char*) symName , symbname ) ;
175 c2pstr( (char*) symName ) ;
176
177 if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr )
178 {
179 symbol = (void *)symAddress ;
180 }
181#else
182 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
183 // should make a copy of it
184 symbol = wxDllGetSymbol(m_handle, WXSTRINGCAST symbname);
123a7fdd 185#endif
7b0bfbb2
VZ
186
187 if ( !symbol )
188 {
189 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
190 symbname.c_str());
191 }
192
193 return symbol;
7a4b9130
GL
194}
195
196// ---------------------------------------------------------------------------
197// wxLibraries (only one instance should normally exist)
198// ---------------------------------------------------------------------------
199
200wxLibraries::wxLibraries()
201{
202}
203
204wxLibraries::~wxLibraries()
205{
7b0bfbb2 206 wxNode *node = m_loaded.First();
7a4b9130 207
7b0bfbb2
VZ
208 while (node) {
209 wxLibrary *lib = (wxLibrary *)node->Data();
210 delete lib;
7a4b9130 211
7b0bfbb2
VZ
212 node = node->Next();
213 }
7a4b9130
GL
214}
215
216wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
217{
7b0bfbb2
VZ
218 wxNode *node;
219 wxLibrary *lib;
220 wxClassInfo *old_sm_first;
7a4b9130 221
7b0bfbb2
VZ
222 if ( (node = m_loaded.Find(name.GetData())) )
223 return ((wxLibrary *)node->Data());
7a4b9130 224
7b0bfbb2
VZ
225 // If DLL shares data, this is necessary.
226 old_sm_first = wxClassInfo::sm_first;
227 wxClassInfo::sm_first = NULL;
228
229 wxString lib_name = ConstructLibraryName(name);
856d2e52 230
9e4b2f1c 231#if defined(__UNIX__)
8a0d4cf6
VZ
232 // found the first file in LD_LIBRARY_PATH with this name
233 wxString libPath("/lib:/usr/lib"); // system path first
234 const char *envLibPath = getenv("LD_LIBRARY_PATH");
235 if ( envLibPath )
236 libPath << ':' << envLibPath;
237 wxStringTokenizer tokenizer(libPath, ':');
238 while ( tokenizer.HasMoreToken() )
239 {
240 wxString fullname(tokenizer.NextToken());
241
242 fullname << '/' << lib_name;
243 if ( wxFileExists(fullname) )
244 {
245 lib_name = fullname;
246
247 // found the library
248 break;
249 }
250 }
251 //else: not found in the path, leave the name as is (secutiry risk?)
252
7b0bfbb2 253#endif // __UNIX__
7a4b9130 254
8a0d4cf6 255 wxDllType handle;
7a4b9130 256
7b0bfbb2
VZ
257#if defined(__WXMAC__)
258 FSSpec myFSSpec ;
259 Ptr myMainAddr ;
260 Str255 myErrName ;
7a4b9130 261
7b0bfbb2
VZ
262 wxMacPathToFSSpec( lib_name , &myFSSpec ) ;
263 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
264 myErrName ) != noErr )
265 {
266 p2cstr( myErrName ) ;
267 wxASSERT_MSG( 1 , (char*)myErrName ) ;
268 return NULL ;
269 }
270#else // !Mac
271 handle = wxDllOpen(lib_name);
272#endif // OS
856d2e52 273
7b0bfbb2
VZ
274 if ( !handle )
275 {
276 wxLogSysError(_("Failed to load shared library '%s'"),
277 lib_name.c_str());
7a4b9130 278
7b0bfbb2
VZ
279 return NULL;
280 }
f4a8c29f 281
7b0bfbb2 282 lib = new wxLibrary(handle);
7a4b9130 283
7b0bfbb2 284 wxClassInfo::sm_first = old_sm_first;
856d2e52 285
7b0bfbb2
VZ
286 m_loaded.Append(name.GetData(), lib);
287
288 return lib;
7a4b9130
GL
289}
290
291wxObject *wxLibraries::CreateObject(const wxString& path)
292{
7b0bfbb2
VZ
293 wxNode *node = m_loaded.First();
294 wxObject *obj;
7a4b9130 295
7b0bfbb2
VZ
296 while (node) {
297 obj = ((wxLibrary *)node->Data())->CreateObject(path);
298 if (obj)
299 return obj;
7a4b9130 300
7b0bfbb2
VZ
301 node = node->Next();
302 }
303 return NULL;
7a4b9130 304}
8a0d4cf6
VZ
305
306#endif // wxUSE_DYNLIB_CLASS