]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
menu title is drawn in bold font and the commands from it are ignored
[wxWidgets.git] / src / common / dynlib.cpp
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
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
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
34 #if defined(__UNIX__)
35 #include <dlfcn.h>
36 #endif
37
38 #ifdef __WINDOWS__
39 #include <windows.h>
40 #endif
41
42 #ifdef LoadLibrary
43 #undef LoadLibrary
44 #endif
45
46 // ---------------------------------------------------------------------------
47 // Global variables
48 // ---------------------------------------------------------------------------
49
50 wxLibraries wxTheLibraries;
51
52 // ---------------------------------------------------------------------------
53 // wxLibrary (one instance per dynamic library
54 // ---------------------------------------------------------------------------
55
56 wxLibrary::wxLibrary(void *handle)
57 {
58 typedef wxClassInfo *(*t_get_first)(void);
59 t_get_first get_first;
60
61 m_handle = handle;
62 m_destroy = TRUE;
63
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());
69 }
70
71 wxLibrary::~wxLibrary()
72 {
73 if (m_handle && m_destroy) {
74 #if defined(__UNIX__)
75 dlclose(m_handle);
76 #endif
77 #ifdef __WINDOWS__
78 FreeLibrary((HMODULE)m_handle);
79 #endif
80 }
81 }
82
83 wxObject *wxLibrary::CreateObject(const wxString& name)
84 {
85 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
86
87 if (!info)
88 return NULL;
89
90 return info->CreateObject();
91 }
92
93 void wxLibrary::PrepareClasses(wxClassInfo *first)
94 {
95 // Index all class infos by their class name
96 wxClassInfo *info = first;
97 while (info)
98 {
99 if (info->m_className)
100 classTable.Put(info->m_className, (wxObject *)info);
101 info = info->GetNext();
102 }
103
104 // Set base pointers for each wxClassInfo
105 info = first;
106 while (info)
107 {
108 if (info->GetBaseClassName1())
109 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
110 if (info->GetBaseClassName2())
111 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
112 info = info->m_next;
113 }
114 }
115
116 void *wxLibrary::GetSymbol(const wxString& symbname)
117 {
118 #if defined(__UNIX__)
119 return dlsym(m_handle, WXSTRINGCAST symbname);
120 #elif defined( __WINDOWS__ )
121 return GetProcAddress((HINSTANCE) m_handle, WXSTRINGCAST symbname);
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 }
134 #endif
135 return NULL;
136 }
137
138 // ---------------------------------------------------------------------------
139 // wxLibraries (only one instance should normally exist)
140 // ---------------------------------------------------------------------------
141
142 wxLibraries::wxLibraries()
143 {
144 }
145
146 wxLibraries::~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
158 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
159 {
160 wxString lib_name = name;
161 wxNode *node;
162 wxLibrary *lib;
163 wxClassInfo *old_sm_first;
164
165 if ( (node = m_loaded.Find(name.GetData())) )
166 return ((wxLibrary *)node->Data());
167
168 // If DLL shares data, this is necessary.
169 old_sm_first = wxClassInfo::sm_first;
170 wxClassInfo::sm_first = NULL;
171
172 #if defined(__UNIX__)
173 lib_name.Prepend("./lib");
174 lib_name += ".so";
175
176 printf("lib_name = %s\n", WXSTRINGCAST lib_name);
177
178 void *handle = dlopen(WXSTRINGCAST lib_name, RTLD_LAZY);
179
180 printf("error = %s\n", dlerror());
181
182 if (!handle)
183 return NULL;
184 #elif defined(__WINDOWS__)
185 lib_name += ".dll";
186
187 #ifdef UNICODE
188 HMODULE handle = LoadLibraryW(lib_name);
189 #else
190 #ifdef __WIN16__
191 HMODULE handle = ::LoadLibrary(lib_name);
192 #else
193 HMODULE handle = LoadLibraryA(lib_name);
194 #endif
195 #endif
196 if (!handle)
197 return NULL;
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 }
212 #else
213 return NULL;
214 #endif
215
216 lib = new wxLibrary((void *)handle);
217
218 wxClassInfo::sm_first = old_sm_first;
219
220 m_loaded.Append(name.GetData(), lib);
221 return lib;
222 }
223
224 wxObject *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);
231 if (obj)
232 return obj;
233
234 node = node->Next();
235 }
236 return NULL;
237 }