]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynload.cpp
Fixed notebook redraw problems when using XP theme with Classic appearance.
[wxWidgets.git] / src / common / dynload.cpp
CommitLineData
0b9ab0bd
RL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynload.cpp
3// Purpose: Dynamic loading framework
4// Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
5// (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
6// Modified by:
7// Created: 03/12/01
8// RCS-ID: $Id$
9// Copyright: (c) 2001 Ron Lee <ron@debian.org>
55d99c7a 10// Licence: wxWindows licence
0b9ab0bd
RL
11/////////////////////////////////////////////////////////////////////////////
12
14f355c2 13#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
0b9ab0bd
RL
14#pragma implementation "dynload.h"
15#endif
16
17// ----------------------------------------------------------------------------
18// headers
19// ----------------------------------------------------------------------------
20
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
409bd320 24 #pragma hdrstop
0b9ab0bd
RL
25#endif
26
27#if wxUSE_DYNAMIC_LOADER
28
29#ifdef __WINDOWS__
409bd320 30 #include "wx/msw/private.h"
0b9ab0bd
RL
31#endif
32
33#ifndef WX_PRECOMP
409bd320
VZ
34 #include "wx/log.h"
35 #include "wx/intl.h"
2b5f62a0 36 #include "wx/hash.h"
076fdb21 37 #include "wx/utils.h"
0b9ab0bd
RL
38#endif
39
401eb3de 40#include "wx/strconv.h"
0b9ab0bd 41
409bd320 42#include "wx/dynload.h"
dabd1377 43#include "wx/module.h"
0b9ab0bd 44
0b9ab0bd
RL
45
46// ---------------------------------------------------------------------------
4f89dbc4 47// wxPluginLibrary
0b9ab0bd
RL
48// ---------------------------------------------------------------------------
49
50
dabd1377
JS
51wxDLImports* wxPluginLibrary::ms_classes = NULL;
52
53class wxPluginLibraryModule : public wxModule
54{
55public:
d0fcccc4
VZ
56 wxPluginLibraryModule() { }
57
58 // TODO: create ms_classes on demand, why always preallocate it?
59 virtual bool OnInit()
60 {
b2bb920f 61 wxPluginLibrary::ms_classes = new wxDLImports;
d0fcccc4
VZ
62 wxPluginManager::CreateManifest();
63 return TRUE;
64 }
65
66 virtual void OnExit()
67 {
68 delete wxPluginLibrary::ms_classes;
69 wxPluginLibrary::ms_classes = NULL;
70 wxPluginManager::ClearManifest();
71 }
dabd1377
JS
72
73private:
74 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
75};
76
77IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
78
0b9ab0bd 79
23213f18 80wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
4f89dbc4 81 : m_linkcount(1)
7c1e2b44 82 , m_objcount(0)
0b9ab0bd 83{
4f89dbc4
RL
84 m_before = wxClassInfo::sm_first;
85 Load( libname, flags );
86 m_after = wxClassInfo::sm_first;
87
0b9ab0bd
RL
88 if( m_handle != 0 )
89 {
90 UpdateClassInfo();
91 RegisterModules();
92 }
93 else
d0fcccc4
VZ
94 {
95 // Flag us for deletion
96 --m_linkcount;
97 }
0b9ab0bd
RL
98}
99
4f89dbc4 100wxPluginLibrary::~wxPluginLibrary()
0b9ab0bd 101{
01610529
RL
102 if( m_handle != 0 )
103 {
104 UnregisterModules();
105 RestoreClassInfo();
01610529 106 }
0b9ab0bd
RL
107}
108
d0fcccc4
VZ
109wxPluginLibrary *wxPluginLibrary::RefLib()
110{
111 wxCHECK_MSG( m_linkcount > 0, NULL,
112 _T("Library had been already deleted!") );
113
114 ++m_linkcount;
115 return this;
116}
117
4f89dbc4 118bool wxPluginLibrary::UnrefLib()
7c1e2b44 119{
d0fcccc4
VZ
120 wxASSERT_MSG( m_objcount == 0,
121 _T("Library unloaded before all objects were destroyed") );
122
123 if ( --m_linkcount == 0 )
7c1e2b44
RL
124 {
125 delete this;
126 return TRUE;
127 }
d0fcccc4 128
7c1e2b44
RL
129 return FALSE;
130}
0b9ab0bd
RL
131
132// ------------------------
133// Private methods
134// ------------------------
135
4f89dbc4 136void wxPluginLibrary::UpdateClassInfo()
0b9ab0bd
RL
137{
138 wxClassInfo *info;
139 wxHashTable *t = wxClassInfo::sm_classTable;
140
141 // FIXME: Below is simply a cut and paste specialisation of
142 // wxClassInfo::InitializeClasses. Once this stabilises,
143 // the two should probably be merged.
144 //
145 // Actually it's becoming questionable whether we should merge
146 // this info with the main ClassInfo tables since we can nearly
147 // handle this completely internally now and it does expose
148 // certain (minimal % user_stupidy) risks.
149
150 for(info = m_after; info != m_before; info = info->m_next)
151 {
152 if( info->m_className )
153 {
154 if( t->Get(info->m_className) == 0 )
155 t->Put(info->m_className, (wxObject *)info);
156
157 // Hash all the class names into a local table too so
158 // we can quickly find the entry they correspond to.
2b5f62a0 159 (*ms_classes)[info->m_className] = this;
0b9ab0bd
RL
160 }
161 }
0b9ab0bd
RL
162}
163
4f89dbc4 164void wxPluginLibrary::RestoreClassInfo()
0b9ab0bd
RL
165{
166 wxClassInfo *info;
167
168 for(info = m_after; info != m_before; info = info->m_next)
169 {
170 wxClassInfo::sm_classTable->Delete(info->m_className);
2b5f62a0 171 ms_classes->erase(ms_classes->find(info->m_className));
0b9ab0bd
RL
172 }
173
174 if( wxClassInfo::sm_first == m_after )
175 wxClassInfo::sm_first = m_before;
176 else
177 {
178 info = wxClassInfo::sm_first;
179 while( info->m_next && info->m_next != m_after ) info = info->m_next;
180
63fd5f0b 181 wxASSERT_MSG( info, _T("ClassInfo from wxPluginLibrary not found on purge"));
0b9ab0bd
RL
182
183 info->m_next = m_before;
184 }
185}
186
4f89dbc4 187void wxPluginLibrary::RegisterModules()
0b9ab0bd
RL
188{
189 // Plugin libraries might have wxModules, Register and initialise them if
190 // they do.
191 //
192 // Note that these classes are NOT included in the reference counting since
193 // it's implicit that they will be unloaded if and when the last handle to
194 // the library is. We do have to keep a copy of the module's pointer
195 // though, as there is currently no way to Unregister it without it.
196
7c1e2b44 197 wxASSERT_MSG( m_linkcount == 1,
0b9ab0bd
RL
198 _T("RegisterModules should only be called for the first load") );
199
6eccc0c4 200 for ( wxClassInfo *info = m_after; info != m_before; info = info->m_next)
0b9ab0bd
RL
201 {
202 if( info->IsKindOf(CLASSINFO(wxModule)) )
203 {
204 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
205
206 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
207
df5168c4 208 m_wxmodules.push_back(m);
0b9ab0bd
RL
209 wxModule::RegisterModule(m);
210 }
211 }
212
213 // FIXME: Likewise this is (well was) very similar to InitializeModules()
214
df5168c4
MB
215 for ( wxModuleList::iterator it = m_wxmodules.begin();
216 it != m_wxmodules.end();
217 ++it)
0b9ab0bd 218 {
df5168c4 219 if( !(*it)->Init() )
0b9ab0bd 220 {
4f89dbc4 221 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
0b9ab0bd
RL
222
223 // XXX: Watch this, a different hash implementation might break it,
224 // a good hash implementation would let us fix it though.
225
226 // The name of the game is to remove any uninitialised modules and
227 // let the dtor Exit the rest on shutdown, (which we'll initiate
228 // shortly).
229
df5168c4 230 wxModuleList::iterator oldNode = m_wxmodules.end();
0b9ab0bd 231 do {
df5168c4
MB
232 ++it;
233 if( oldNode != m_wxmodules.end() )
234 m_wxmodules.erase(oldNode);
235 wxModule::UnregisterModule( *it );
236 oldNode = it;
237 } while( it != m_wxmodules.end() );
0b9ab0bd 238
7c1e2b44 239 --m_linkcount; // Flag us for deletion
0b9ab0bd
RL
240 break;
241 }
242 }
243}
244
4f89dbc4 245void wxPluginLibrary::UnregisterModules()
0b9ab0bd 246{
df5168c4 247 wxModuleList::iterator it;
0b9ab0bd 248
df5168c4
MB
249 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
250 (*it)->Exit();
0b9ab0bd 251
df5168c4
MB
252 for ( it = m_wxmodules.begin(); it != m_wxmodules.end(); ++it )
253 wxModule::UnregisterModule( *it );
0b9ab0bd 254
df5168c4 255 WX_CLEAR_LIST(wxModuleList, m_wxmodules);
0b9ab0bd
RL
256}
257
258
259// ---------------------------------------------------------------------------
d0fcccc4 260// wxPluginManager
0b9ab0bd
RL
261// ---------------------------------------------------------------------------
262
dabd1377 263wxDLManifest* wxPluginManager::ms_manifest = NULL;
0b9ab0bd
RL
264
265// ------------------------
266// Static accessors
267// ------------------------
268
d0fcccc4
VZ
269wxPluginLibrary *
270wxPluginManager::LoadLibrary(const wxString &libname, int flags)
0b9ab0bd 271{
4f89dbc4 272 wxString realname(libname);
0b9ab0bd 273
4f89dbc4
RL
274 if( !(flags & wxDL_VERBATIM) )
275 realname += wxDynamicLibrary::GetDllExt();
276
d0fcccc4
VZ
277 wxPluginLibrary *entry;
278
279 if ( flags & wxDL_NOSHARE )
280 {
281 entry = NULL;
282 }
283 else
284 {
2b5f62a0 285 entry = FindByName(realname);
d0fcccc4 286 }
4f89dbc4 287
d0fcccc4 288 if ( entry )
0b9ab0bd 289 {
d0fcccc4
VZ
290 wxLogTrace(_T("dll"),
291 _T("LoadLibrary(%s): already loaded."), realname.c_str());
292
7c1e2b44 293 entry->RefLib();
0b9ab0bd
RL
294 }
295 else
296 {
4f89dbc4 297 entry = new wxPluginLibrary( libname, flags );
0b9ab0bd 298
d0fcccc4 299 if ( entry->IsLoaded() )
0b9ab0bd 300 {
2b5f62a0 301 (*ms_manifest)[realname] = entry;
d0fcccc4
VZ
302
303 wxLogTrace(_T("dll"),
304 _T("LoadLibrary(%s): loaded ok."), realname.c_str());
305
0b9ab0bd
RL
306 }
307 else
308 {
d0fcccc4
VZ
309 wxLogTrace(_T("dll"),
310 _T("LoadLibrary(%s): failed to load."), realname.c_str());
311
312 // we have created entry just above
313 if ( !entry->UnrefLib() )
314 {
315 // ... so UnrefLib() is supposed to delete it
316 wxFAIL_MSG( _T("Currently linked library is not loaded?") );
317 }
318
319 entry = NULL;
0b9ab0bd
RL
320 }
321 }
d0fcccc4 322
0b9ab0bd
RL
323 return entry;
324}
325
d0fcccc4 326bool wxPluginManager::UnloadLibrary(const wxString& libname)
0b9ab0bd 327{
d0fcccc4 328 wxString realname = libname;
4f89dbc4 329
2b5f62a0 330 wxPluginLibrary *entry = FindByName(realname);
0b9ab0bd 331
d0fcccc4
VZ
332 if ( !entry )
333 {
334 realname += wxDynamicLibrary::GetDllExt();
0b9ab0bd 335
2b5f62a0 336 entry = FindByName(realname);
d0fcccc4
VZ
337 }
338
339 if ( !entry )
340 {
341 wxLogDebug(_T("Attempt to unload library '%s' which is not loaded."),
342 libname.c_str());
343
344 return FALSE;
345 }
346
347 wxLogTrace(_T("dll"), _T("UnloadLibrary(%s)"), realname.c_str());
348
349 if ( !entry->UnrefLib() )
350 {
351 // not really unloaded yet
352 return FALSE;
353 }
354
2b5f62a0 355 ms_manifest->erase(ms_manifest->find(realname));
d0fcccc4
VZ
356
357 return TRUE;
4f89dbc4
RL
358}
359
0b9ab0bd
RL
360// ------------------------
361// Class implementation
362// ------------------------
363
23213f18 364bool wxPluginManager::Load(const wxString &libname, int flags)
0b9ab0bd 365{
4f89dbc4 366 m_entry = wxPluginManager::LoadLibrary(libname, flags);
2b5f62a0 367
4f89dbc4 368 return IsLoaded();
0b9ab0bd
RL
369}
370
4f89dbc4 371void wxPluginManager::Unload()
0b9ab0bd 372{
2b5f62a0 373 wxCHECK_RET( m_entry, _T("unloading an invalid wxPluginManager?") );
0b9ab0bd 374
2b5f62a0
VZ
375 for ( wxDLManifest::iterator i = ms_manifest->begin();
376 i != ms_manifest->end();
377 ++i )
4f89dbc4 378 {
2b5f62a0
VZ
379 if ( i->second == m_entry )
380 {
381 ms_manifest->erase(i);
46113053 382 break;
2b5f62a0 383 }
4f89dbc4 384 }
2b5f62a0
VZ
385
386 m_entry->UnrefLib();
387
388 m_entry = NULL;
0b9ab0bd
RL
389}
390
1948bb32
VS
391
392
393#if WXWIN_COMPATIBILITY_2_2
394
395wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
396{
397 for ( wxDLManifest::iterator i = ms_manifest->begin();
398 i != ms_manifest->end();
399 ++i )
400 {
401 wxPluginLibrary * const lib = i->second;
402
403 if ( lib->GetLibHandle() == handle )
404 return lib;
405 }
406
407 return NULL;
408}
409
4f89dbc4
RL
410// ---------------------------------------------------------------------------
411// wxDllLoader (all these methods are static)
412// ---------------------------------------------------------------------------
413
4f89dbc4 414
3d7fa4e6 415wxDllType wxDllLoader::LoadLibrary(const wxString &name, bool *success)
4f89dbc4 416{
d0fcccc4
VZ
417 wxPluginLibrary *p = wxPluginManager::LoadLibrary
418 (
419 name,
420 wxDL_DEFAULT | wxDL_VERBATIM | wxDL_NOSHARE
421 );
422
3d7fa4e6
VZ
423 if ( success )
424 *success = p != NULL;
425
d0fcccc4 426 return p ? p->GetLibHandle() : 0;
0b9ab0bd
RL
427}
428
4f89dbc4 429void wxDllLoader::UnloadLibrary(wxDllType handle)
0b9ab0bd 430{
4f89dbc4 431 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
d0fcccc4
VZ
432
433 wxCHECK_RET( p, _T("Unloading a library not loaded with wxDllLoader?") );
434
4f89dbc4 435 p->UnrefLib();
0b9ab0bd
RL
436}
437
d0fcccc4
VZ
438void *
439wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
0b9ab0bd 440{
4f89dbc4 441 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
d0fcccc4
VZ
442
443 if ( !p )
444 {
445 wxFAIL_MSG( _T("Using a library not loaded with wxDllLoader?") );
446
447 if ( success )
448 *success = FALSE;
449
450 return NULL;
451 }
452
4f89dbc4 453 return p->GetSymbol(name, success);
0b9ab0bd
RL
454}
455
1948bb32
VS
456
457// ---------------------------------------------------------------------------
458// Global variables
459// ---------------------------------------------------------------------------
460
461wxLibraries wxTheLibraries;
462
463// ============================================================================
464// implementation
465// ============================================================================
466
467// construct the full name from the base shared object name: adds a .dll
468// suffix under Windows or .so under Unix
469static wxString ConstructLibraryName(const wxString& basename)
470{
471 wxString fullname;
472 fullname << basename << wxDllLoader::GetDllExt();
473
474 return fullname;
475}
476
477// ---------------------------------------------------------------------------
478// wxLibrary (one instance per dynamic library)
479// ---------------------------------------------------------------------------
480
481wxLibrary::wxLibrary(wxDllType handle)
482{
483 typedef wxClassInfo *(*t_get_first)(void);
484 t_get_first get_first;
485
486 m_handle = handle;
487
488 // Some system may use a local heap for library.
489 get_first = (t_get_first)GetSymbol(_T("wxGetClassFirst"));
490 // It is a wxWindows DLL.
491 if (get_first)
492 PrepareClasses(get_first());
493}
494
495wxLibrary::~wxLibrary()
496{
497 if ( m_handle )
498 {
499 wxDllLoader::UnloadLibrary(m_handle);
500 }
501}
502
503wxObject *wxLibrary::CreateObject(const wxString& name)
504{
505 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
506
507 if (!info)
508 return NULL;
509
510 return info->CreateObject();
511}
512
513void wxLibrary::PrepareClasses(wxClassInfo *first)
514{
515 // Index all class infos by their class name
516 wxClassInfo *info = first;
517 while (info)
518 {
519 if (info->m_className)
520 classTable.Put(info->m_className, (wxObject *)info);
521 info = info->m_next;
522 }
523
d1d738f1 524#if !wxUSE_EXTENDED_RTTI
1948bb32
VS
525 // Set base pointers for each wxClassInfo
526 info = first;
527 while (info)
528 {
529 if (info->GetBaseClassName1())
530 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
531 if (info->GetBaseClassName2())
532 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
533 info = info->m_next;
534 }
1bc7510a 535#endif
1948bb32
VS
536}
537
538void *wxLibrary::GetSymbol(const wxString& symbname)
539{
540 return wxDllLoader::GetSymbol(m_handle, symbname);
541}
542
543
544// ---------------------------------------------------------------------------
545// wxLibraries (only one instance should normally exist)
546// ---------------------------------------------------------------------------
547
548wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
549{
550}
551
552wxLibraries::~wxLibraries()
553{
554 wxNode *node = m_loaded.First();
555
556 while (node) {
557 wxLibrary *lib = (wxLibrary *)node->Data();
558 delete lib;
559
560 node = node->Next();
561 }
562}
563
564wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
565{
566 wxLibrary *lib;
567 wxClassInfo *old_sm_first;
568 wxNode *node = m_loaded.Find(name.GetData());
569
570 if (node != NULL)
571 return ((wxLibrary *)node->Data());
572
573 // If DLL shares data, this is necessary.
574 old_sm_first = wxClassInfo::sm_first;
575 wxClassInfo::sm_first = NULL;
576
577 wxString libname = ConstructLibraryName(name);
578
579 bool success = FALSE;
580 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
581 if(success)
582 {
583 lib = new wxLibrary(handle);
584 wxClassInfo::sm_first = old_sm_first;
585
586 m_loaded.Append(name.GetData(), lib);
587 }
588 else
589 lib = NULL;
590 return lib;
591}
592
593wxObject *wxLibraries::CreateObject(const wxString& path)
594{
595 wxNode *node = m_loaded.First();
596 wxObject *obj;
597
598 while (node) {
599 obj = ((wxLibrary *)node->Data())->CreateObject(path);
600 if (obj)
601 return obj;
602
603 node = node->Next();
604 }
605 return NULL;
606}
607
4f89dbc4
RL
608#endif // WXWIN_COMPATIBILITY_2_2
609
1948bb32 610
0b9ab0bd
RL
611#endif // wxUSE_DYNAMIC_LOADER
612