]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/module.cpp
remove wxTextAttr::CreateFont(); return wxNullFont from GetFont() if we have no font...
[wxWidgets.git] / src / common / module.cpp
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/common/module.cpp
3// Purpose: Modules initialization/destruction
4// Author: Wolfram Gloger/adapted by Guilhem Lavaux
5// Modified by:
6// Created: 04/11/98
7// RCS-ID: $Id$
8// Copyright: (c) Wolfram Gloger and Guilhem Lavaux
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16 #pragma hdrstop
17#endif
18
19#include "wx/module.h"
20
21#ifndef WX_PRECOMP
22 #include "wx/hash.h"
23 #include "wx/intl.h"
24 #include "wx/log.h"
25#endif
26
27#include "wx/listimpl.cpp"
28
29#define TRACE_MODULE _T("module")
30
31WX_DEFINE_LIST(wxModuleList)
32
33IMPLEMENT_CLASS(wxModule, wxObject)
34
35wxModuleList wxModule::m_modules;
36
37void wxModule::RegisterModule(wxModule* module)
38{
39 module->m_state = State_Registered;
40 m_modules.Append(module);
41}
42
43void wxModule::UnregisterModule(wxModule* module)
44{
45 m_modules.DeleteObject(module);
46 delete module;
47}
48
49// Collect up all module-derived classes, create an instance of each,
50// and register them.
51void wxModule::RegisterModules()
52{
53 for (wxClassInfo::const_iterator it = wxClassInfo::begin_classinfo(),
54 end = wxClassInfo::end_classinfo();
55 it != end; ++it)
56 {
57 const wxClassInfo* classInfo = *it;
58
59 if ( classInfo->IsKindOf(CLASSINFO(wxModule)) &&
60 (classInfo != (& (wxModule::ms_classInfo))) )
61 {
62 wxLogTrace(TRACE_MODULE, wxT("Registering module %s"),
63 classInfo->GetClassName());
64 wxModule* module = (wxModule *)classInfo->CreateObject();
65 wxModule::RegisterModule(module);
66 }
67 }
68}
69
70bool wxModule::DoInitializeModule(wxModule *module,
71 wxModuleList &initializedModules)
72{
73 if ( module->m_state == State_Initializing )
74 {
75 wxLogError(_("Circular dependency involving module \"%s\" detected."),
76 module->GetClassInfo()->GetClassName());
77 return false;
78 }
79
80 module->m_state = State_Initializing;
81
82 // translate named dependencies to the normal ones first
83 if ( !module->ResolveNamedDependencies() )
84 return false;
85
86 const wxArrayClassInfo& dependencies = module->m_dependencies;
87
88 // satisfy module dependencies by loading them before the current module
89 for ( unsigned int i = 0; i < dependencies.size(); ++i )
90 {
91 wxClassInfo * cinfo = dependencies[i];
92
93 // Check if the module is already initialized
94 wxModuleList::compatibility_iterator node;
95 for ( node = initializedModules.GetFirst(); node; node = node->GetNext() )
96 {
97 if ( node->GetData()->GetClassInfo() == cinfo )
98 break;
99 }
100
101 if ( node )
102 {
103 // this dependency is already initialized, nothing to do
104 continue;
105 }
106
107 // find the module in the registered modules list
108 for ( node = m_modules.GetFirst(); node; node = node->GetNext() )
109 {
110 wxModule *moduleDep = node->GetData();
111 if ( moduleDep->GetClassInfo() == cinfo )
112 {
113 if ( !DoInitializeModule(moduleDep, initializedModules ) )
114 {
115 // failed to initialize a dependency, so fail this one too
116 return false;
117 }
118
119 break;
120 }
121 }
122
123 if ( !node )
124 {
125 wxLogError(_("Dependency \"%s\" of module \"%s\" doesn't exist."),
126 cinfo->GetClassName(),
127 module->GetClassInfo()->GetClassName());
128 return false;
129 }
130 }
131
132 if ( !module->Init() )
133 {
134 wxLogError(_("Module \"%s\" initialization failed"),
135 module->GetClassInfo()->GetClassName());
136 return false;
137 }
138
139 wxLogTrace(TRACE_MODULE, wxT("Module \"%s\" initialized"),
140 module->GetClassInfo()->GetClassName());
141
142 module->m_state = State_Initialized;
143 initializedModules.Append(module);
144
145 return true;
146}
147
148// Initialize user-defined modules
149bool wxModule::InitializeModules()
150{
151 wxModuleList initializedModules;
152
153 for ( wxModuleList::compatibility_iterator node = m_modules.GetFirst();
154 node;
155 node = node->GetNext() )
156 {
157 wxModule *module = node->GetData();
158
159 // the module could have been already initialized as dependency of
160 // another one
161 if ( module->m_state == State_Registered )
162 {
163 if ( !DoInitializeModule( module, initializedModules ) )
164 {
165 // failed to initialize all modules, so clean up the already
166 // initialized ones
167 DoCleanUpModules(initializedModules);
168
169 return false;
170 }
171 }
172 }
173
174 // remember the real initialisation order
175 m_modules = initializedModules;
176
177 return true;
178}
179
180// Clean up all currently initialized modules
181void wxModule::DoCleanUpModules(const wxModuleList& modules)
182{
183 // cleanup user-defined modules in the reverse order compared to their
184 // initialization -- this ensures that dependencies are respected
185 for ( wxModuleList::compatibility_iterator node = modules.GetLast();
186 node;
187 node = node->GetPrevious() )
188 {
189 wxLogTrace(TRACE_MODULE, wxT("Cleanup module %s"),
190 node->GetData()->GetClassInfo()->GetClassName());
191
192 wxModule * module = node->GetData();
193
194 wxASSERT_MSG( module->m_state == State_Initialized,
195 _T("not initialized module being cleaned up") );
196
197 module->Exit();
198 module->m_state = State_Registered;
199 }
200
201 // clear all modules, even the non-initialized ones
202 WX_CLEAR_LIST(wxModuleList, m_modules);
203}
204
205bool wxModule::ResolveNamedDependencies()
206{
207 // first resolve required dependencies
208 for ( size_t i = 0; i < m_namedDependencies.size(); ++i )
209 {
210 wxClassInfo *info = wxClassInfo::FindClass(m_namedDependencies[i]);
211
212 if ( !info )
213 {
214 // required dependency not found
215 return false;
216 }
217
218 // add it even if it is not derived from wxModule because
219 // DoInitializeModule() will make sure a module with the same class
220 // info exists and fail if it doesn't
221 m_dependencies.Add(info);
222 }
223
224 return true;
225}