]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Faster imagescale code
[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
12#ifdef __GNUG__
13#pragma implementation "dynlib.h"
14#endif
15
0c32066b
JS
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif //__BORLANDC__
21
22#ifndef WX_PRECOMP
23#endif //WX_PRECOMP
24
7a4b9130
GL
25#include <wx/dynlib.h>
26#include <wx/filefn.h>
27#include <wx/list.h>
28#include <wx/string.h>
29
30// ---------------------------------------------------------------------------
31// System dependent include
32// ---------------------------------------------------------------------------
33
9e4b2f1c 34#if defined(__UNIX__)
7a4b9130
GL
35#include <dlfcn.h>
36#endif
37
123a7fdd
GL
38#ifdef __WINDOWS__
39#include <windows.h>
40#endif
41
27529614
JS
42#ifdef LoadLibrary
43#undef LoadLibrary
44#endif
45
7a4b9130
GL
46// ---------------------------------------------------------------------------
47// Global variables
48// ---------------------------------------------------------------------------
49
50wxLibraries wxTheLibraries;
51
52// ---------------------------------------------------------------------------
53// wxLibrary (one instance per dynamic library
54// ---------------------------------------------------------------------------
55
56wxLibrary::wxLibrary(void *handle)
57{
856d2e52 58 typedef wxClassInfo *(*t_get_first)(void);
f4a8c29f 59 t_get_first get_first;
7a4b9130
GL
60
61 m_handle = handle;
f4a8c29f 62 m_destroy = TRUE;
7a4b9130 63
f4a8c29f
GL
64 // Some system may use a local heap for library.
65 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
66 // It is a wxWindows DLL.
67 if (get_first)
68 PrepareClasses(get_first());
7a4b9130
GL
69}
70
71wxLibrary::~wxLibrary()
72{
f4a8c29f 73 if (m_handle && m_destroy) {
9e4b2f1c 74#if defined(__UNIX__)
7a4b9130 75 dlclose(m_handle);
123a7fdd
GL
76#endif
77#ifdef __WINDOWS__
78 FreeLibrary((HMODULE)m_handle);
79#endif
7a4b9130
GL
80 }
81}
82
83wxObject *wxLibrary::CreateObject(const wxString& name)
84{
f4a8c29f
GL
85 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
86
87 if (!info)
88 return NULL;
89
90 return info->CreateObject();
91}
92
856d2e52 93void wxLibrary::PrepareClasses(wxClassInfo *first)
f4a8c29f
GL
94{
95 // Index all class infos by their class name
856d2e52 96 wxClassInfo *info = first;
f4a8c29f
GL
97 while (info)
98 {
0c32066b
JS
99 if (info->m_className)
100 classTable.Put(info->m_className, (wxObject *)info);
856d2e52 101 info = info->GetNext();
f4a8c29f
GL
102 }
103
104 // Set base pointers for each wxClassInfo
856d2e52 105 info = first;
f4a8c29f
GL
106 while (info)
107 {
108 if (info->GetBaseClassName1())
0c32066b 109 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
f4a8c29f 110 if (info->GetBaseClassName2())
0c32066b
JS
111 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
112 info = info->m_next;
f4a8c29f 113 }
7a4b9130
GL
114}
115
116void *wxLibrary::GetSymbol(const wxString& symbname)
117{
9e4b2f1c 118#if defined(__UNIX__)
123a7fdd 119 return dlsym(m_handle, WXSTRINGCAST symbname);
17dff81c 120#elif defined( __WINDOWS__ )
0c32066b 121 return GetProcAddress((HINSTANCE) m_handle, WXSTRINGCAST symbname);
17dff81c
SC
122#elif defined( __WXMAC__ )
123 Ptr symAddress ;
124 CFragSymbolClass symClass ;
125 Str255 symName ;
126
127 strcpy( (char*) symName , symbname ) ;
128 c2pstr( (char*) symName ) ;
129
130 if ( FindSymbol( (CFragConnectionID) m_handle , symName , &symAddress , &symClass ) == noErr )
131 {
132 return symAddress ;
133 }
123a7fdd
GL
134#endif
135 return NULL;
7a4b9130
GL
136}
137
138// ---------------------------------------------------------------------------
139// wxLibraries (only one instance should normally exist)
140// ---------------------------------------------------------------------------
141
142wxLibraries::wxLibraries()
143{
144}
145
146wxLibraries::~wxLibraries()
147{
148 wxNode *node = m_loaded.First();
149
150 while (node) {
151 wxLibrary *lib = (wxLibrary *)node->Data();
152 delete lib;
153
154 node = node->Next();
155 }
156}
157
158wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
159{
160 wxString lib_name = name;
161 wxNode *node;
162 wxLibrary *lib;
856d2e52 163 wxClassInfo *old_sm_first;
7a4b9130
GL
164
165 if ( (node = m_loaded.Find(name.GetData())) )
166 return ((wxLibrary *)node->Data());
167
856d2e52
GL
168 // If DLL shares data, this is necessary.
169 old_sm_first = wxClassInfo::sm_first;
170 wxClassInfo::sm_first = NULL;
171
9e4b2f1c 172#if defined(__UNIX__)
1d44aaf8 173 lib_name.Prepend("./lib");
7a4b9130
GL
174 lib_name += ".so";
175
176 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
177
123a7fdd 178 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
7a4b9130 179
856d2e52
GL
180 printf("error = %s\n", dlerror());
181
123a7fdd
GL
182 if (!handle)
183 return NULL;
f4a8c29f 184#elif defined(__WINDOWS__)
7a4b9130
GL
185 lib_name += ".dll";
186
27529614
JS
187#ifdef UNICODE
188 HMODULE handle = LoadLibraryW(lib_name);
62448488
JS
189#else
190#ifdef __WIN16__
191 HMODULE handle = ::LoadLibrary(lib_name);
27529614
JS
192#else
193 HMODULE handle = LoadLibraryA(lib_name);
62448488 194#endif
27529614 195#endif
123a7fdd
GL
196 if (!handle)
197 return NULL;
17dff81c
SC
198#elif defined(__WXMAC__)
199 FSSpec myFSSpec ;
200 CFragConnectionID handle ;
201 Ptr myMainAddr ;
202 Str255 myErrName ;
203
204 wxMacPathToFSSpec( lib_name , &myFSSpec ) ;
205 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
206 myErrName ) != noErr )
207 {
208 p2cstr( myErrName ) ;
209 wxASSERT_MSG( 1 , (char*)myErrName ) ;
210 return NULL ;
211 }
f4a8c29f
GL
212#else
213 return NULL;
7a4b9130 214#endif
f4a8c29f 215
123a7fdd 216 lib = new wxLibrary((void *)handle);
7a4b9130 217
856d2e52
GL
218 wxClassInfo::sm_first = old_sm_first;
219
7a4b9130
GL
220 m_loaded.Append(name.GetData(), lib);
221 return lib;
222}
223
224wxObject *wxLibraries::CreateObject(const wxString& path)
225{
226 wxNode *node = m_loaded.First();
227 wxObject *obj;
228
229 while (node) {
230 obj = ((wxLibrary *)node->Data())->CreateObject(path);
297a3fec 231 if (obj)
7a4b9130
GL
232 return obj;
233
234 node = node->Next();
235 }
236 return NULL;
237}