]>
Commit | Line | Data |
---|---|---|
78d14f80 | 1 | ///////////////////////////////////////////////////////////////////////////// |
88a7a4e1 | 2 | // Name: src/xrc/xmlres.cpp |
b5d6954b | 3 | // Purpose: XRC resources |
78d14f80 VS |
4 | // Author: Vaclav Slavik |
5 | // Created: 2000/03/05 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2000 Vaclav Slavik | |
8 | // Licence: wxWindows licence | |
9 | ///////////////////////////////////////////////////////////////////////////// | |
10 | ||
78d14f80 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifdef __BORLANDC__ | |
15 | #pragma hdrstop | |
16 | #endif | |
17 | ||
621be1ec | 18 | #if wxUSE_XRC |
a1e4ec87 | 19 | |
88a7a4e1 WS |
20 | #include "wx/xrc/xmlres.h" |
21 | ||
22 | #ifndef WX_PRECOMP | |
23 | #include "wx/intl.h" | |
e4db172a | 24 | #include "wx/log.h" |
8e609c82 | 25 | #include "wx/panel.h" |
76b49cf4 | 26 | #include "wx/frame.h" |
fdf565fe | 27 | #include "wx/dialog.h" |
9eddec69 | 28 | #include "wx/settings.h" |
0bca0373 | 29 | #include "wx/bitmap.h" |
155ecd4c | 30 | #include "wx/image.h" |
02761f6c | 31 | #include "wx/module.h" |
88a7a4e1 WS |
32 | #endif |
33 | ||
e7a3a5a5 | 34 | #ifndef __WXWINCE__ |
88a7a4e1 | 35 | #include <locale.h> |
e7a3a5a5 | 36 | #endif |
1df61962 | 37 | |
78d14f80 VS |
38 | #include "wx/wfstream.h" |
39 | #include "wx/filesys.h" | |
317a0d73 | 40 | #include "wx/filename.h" |
78d14f80 VS |
41 | #include "wx/tokenzr.h" |
42 | #include "wx/fontenum.h" | |
78d14f80 | 43 | #include "wx/fontmap.h" |
af1337b0 | 44 | #include "wx/artprov.h" |
78d14f80 | 45 | |
34c6bbee | 46 | #include "wx/xml/xml.h" |
78d14f80 VS |
47 | |
48 | #include "wx/arrimpl.cpp" | |
17a1ebd1 | 49 | WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords) |
78d14f80 VS |
50 | |
51 | ||
824e8eaa VS |
52 | wxXmlResource *wxXmlResource::ms_instance = NULL; |
53 | ||
54 | /*static*/ wxXmlResource *wxXmlResource::Get() | |
55 | { | |
56 | if ( !ms_instance ) | |
57 | ms_instance = new wxXmlResource; | |
58 | return ms_instance; | |
59 | } | |
60 | ||
61 | /*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res) | |
62 | { | |
63 | wxXmlResource *old = ms_instance; | |
64 | ms_instance = res; | |
65 | return old; | |
66 | } | |
67 | ||
d4a724d4 | 68 | wxXmlResource::wxXmlResource(int flags, const wxString& domain) |
78d14f80 | 69 | { |
daa85ee3 | 70 | m_flags = flags; |
78d14f80 | 71 | m_version = -1; |
d4a724d4 RD |
72 | m_domain = NULL; |
73 | if (! domain.empty() ) | |
74 | SetDomain(domain); | |
78d14f80 VS |
75 | } |
76 | ||
d4a724d4 | 77 | wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain) |
78d14f80 | 78 | { |
daa85ee3 | 79 | m_flags = flags; |
78d14f80 | 80 | m_version = -1; |
d4a724d4 RD |
81 | m_domain = NULL; |
82 | if (! domain.empty() ) | |
83 | SetDomain(domain); | |
78d14f80 VS |
84 | Load(filemask); |
85 | } | |
86 | ||
87 | wxXmlResource::~wxXmlResource() | |
88 | { | |
d4a724d4 RD |
89 | if (m_domain) |
90 | free(m_domain); | |
78d14f80 VS |
91 | ClearHandlers(); |
92 | } | |
93 | ||
d4a724d4 RD |
94 | void wxXmlResource::SetDomain(const wxChar* domain) |
95 | { | |
96 | if (m_domain) | |
97 | free(m_domain); | |
98 | m_domain = NULL; | |
99 | if (domain && wxStrlen(domain)) | |
100 | m_domain = wxStrdup(domain); | |
101 | } | |
102 | ||
78d14f80 | 103 | |
60fd818a VZ |
104 | /* static */ |
105 | wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename) | |
106 | { | |
107 | wxString fnd(filename); | |
108 | ||
109 | // NB: as Load() and Unload() accept both filenames and URLs (should | |
110 | // probably be changed to filenames only, but embedded resources | |
111 | // currently rely on its ability to handle URLs - FIXME) we need to | |
112 | // determine whether found name is filename and not URL and this is the | |
113 | // fastest/simplest way to do it | |
114 | if (wxFileName::FileExists(fnd)) | |
115 | { | |
116 | // Make the name absolute filename, because the app may | |
117 | // change working directory later: | |
118 | wxFileName fn(fnd); | |
119 | if (fn.IsRelative()) | |
120 | { | |
121 | fn.MakeAbsolute(); | |
122 | fnd = fn.GetFullPath(); | |
123 | } | |
124 | #if wxUSE_FILESYSTEM | |
125 | fnd = wxFileSystem::FileNameToURL(fnd); | |
126 | #endif | |
127 | } | |
128 | ||
129 | return fnd; | |
130 | } | |
131 | ||
132 | #if wxUSE_FILESYSTEM | |
133 | ||
134 | /* static */ | |
135 | bool wxXmlResource::IsArchive(const wxString& filename) | |
136 | { | |
137 | const wxString fnd = filename.Lower(); | |
138 | ||
139 | return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs")); | |
140 | } | |
141 | ||
142 | #endif // wxUSE_FILESYSTEM | |
143 | ||
78d14f80 VS |
144 | bool wxXmlResource::Load(const wxString& filemask) |
145 | { | |
146 | wxString fnd; | |
147 | wxXmlResourceDataRecord *drec; | |
148 | bool iswild = wxIsWild(filemask); | |
f80ea77b | 149 | bool rt = true; |
78d14f80 VS |
150 | |
151 | #if wxUSE_FILESYSTEM | |
152 | wxFileSystem fsys; | |
153 | # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE) | |
154 | # define wxXmlFindNext fsys.FindNext() | |
155 | #else | |
156 | # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE) | |
157 | # define wxXmlFindNext wxFindNextFile() | |
158 | #endif | |
159 | if (iswild) | |
160 | fnd = wxXmlFindFirst; | |
161 | else | |
162 | fnd = filemask; | |
ec157c8f | 163 | while (!fnd.empty()) |
78d14f80 | 164 | { |
60fd818a | 165 | fnd = ConvertFileNameToURL(fnd); |
b380439d | 166 | |
78d14f80 | 167 | #if wxUSE_FILESYSTEM |
60fd818a | 168 | if ( IsArchive(fnd) ) |
78d14f80 | 169 | { |
f9d243a3 | 170 | rt = rt && Load(fnd + wxT("#zip:*.xrc")); |
78d14f80 | 171 | } |
60fd818a VZ |
172 | else // a single resource URL |
173 | #endif // wxUSE_FILESYSTEM | |
78d14f80 VS |
174 | { |
175 | drec = new wxXmlResourceDataRecord; | |
176 | drec->File = fnd; | |
177 | m_data.Add(drec); | |
178 | } | |
179 | ||
180 | if (iswild) | |
181 | fnd = wxXmlFindNext; | |
182 | else | |
183 | fnd = wxEmptyString; | |
184 | } | |
185 | # undef wxXmlFindFirst | |
186 | # undef wxXmlFindNext | |
d614f51b | 187 | return rt && UpdateResources(); |
78d14f80 VS |
188 | } |
189 | ||
60fd818a VZ |
190 | bool wxXmlResource::Unload(const wxString& filename) |
191 | { | |
192 | wxASSERT_MSG( !wxIsWild(filename), | |
193 | _T("wildcards not supported by wxXmlResource::Unload()") ); | |
194 | ||
195 | wxString fnd = ConvertFileNameToURL(filename); | |
196 | #if wxUSE_FILESYSTEM | |
197 | const bool isArchive = IsArchive(fnd); | |
198 | if ( isArchive ) | |
199 | fnd += _T("#zip:"); | |
200 | #endif // wxUSE_FILESYSTEM | |
201 | ||
202 | bool unloaded = false; | |
203 | const size_t count = m_data.GetCount(); | |
204 | for ( size_t i = 0; i < count; i++ ) | |
205 | { | |
206 | #if wxUSE_FILESYSTEM | |
207 | if ( isArchive ) | |
208 | { | |
209 | if ( m_data[i].File.StartsWith(fnd) ) | |
210 | unloaded = true; | |
211 | // don't break from the loop, we can have other matching files | |
212 | } | |
213 | else // a single resource URL | |
214 | #endif // wxUSE_FILESYSTEM | |
215 | { | |
216 | if ( m_data[i].File == fnd ) | |
217 | { | |
218 | m_data.RemoveAt(i); | |
219 | unloaded = true; | |
220 | ||
221 | // no sense in continuing, there is only one file with this URL | |
222 | break; | |
223 | } | |
224 | } | |
225 | } | |
226 | ||
227 | return unloaded; | |
228 | } | |
229 | ||
78d14f80 | 230 | |
854e189f | 231 | IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject) |
78d14f80 VS |
232 | |
233 | void wxXmlResource::AddHandler(wxXmlResourceHandler *handler) | |
234 | { | |
8e8a4e85 | 235 | m_handlers.Append(handler); |
78d14f80 VS |
236 | handler->SetParentResource(this); |
237 | } | |
238 | ||
92e898b0 RD |
239 | void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler) |
240 | { | |
8e8a4e85 | 241 | m_handlers.Insert(handler); |
92e898b0 RD |
242 | handler->SetParentResource(this); |
243 | } | |
244 | ||
78d14f80 VS |
245 | |
246 | ||
247 | void wxXmlResource::ClearHandlers() | |
248 | { | |
8e8a4e85 | 249 | WX_CLEAR_LIST(wxList, m_handlers); |
78d14f80 VS |
250 | } |
251 | ||
252 | ||
78d14f80 VS |
253 | wxMenu *wxXmlResource::LoadMenu(const wxString& name) |
254 | { | |
255 | return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL); | |
256 | } | |
257 | ||
258 | ||
259 | ||
4a1b9596 | 260 | wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name) |
78d14f80 | 261 | { |
4a1b9596 | 262 | return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL); |
78d14f80 VS |
263 | } |
264 | ||
265 | ||
266 | ||
4a1b9596 | 267 | #if wxUSE_TOOLBAR |
78d14f80 VS |
268 | wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name) |
269 | { | |
270 | return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL); | |
271 | } | |
4a1b9596 | 272 | #endif |
78d14f80 VS |
273 | |
274 | ||
275 | wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name) | |
276 | { | |
4dd75a6a | 277 | return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL); |
78d14f80 VS |
278 | } |
279 | ||
280 | bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name) | |
281 | { | |
282 | return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL; | |
283 | } | |
284 | ||
285 | ||
286 | ||
287 | wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name) | |
288 | { | |
289 | return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL); | |
290 | } | |
291 | ||
292 | bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name) | |
293 | { | |
294 | return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL; | |
295 | } | |
296 | ||
92e898b0 RD |
297 | wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name) |
298 | { | |
299 | return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL); | |
300 | } | |
301 | ||
78d14f80 VS |
302 | bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name) |
303 | { | |
304 | return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL; | |
305 | } | |
306 | ||
307 | wxBitmap wxXmlResource::LoadBitmap(const wxString& name) | |
308 | { | |
309 | wxBitmap *bmp = (wxBitmap*)CreateResFromNode( | |
310 | FindResource(name, wxT("wxBitmap")), NULL, NULL); | |
311 | wxBitmap rt; | |
312 | ||
313 | if (bmp) { rt = *bmp; delete bmp; } | |
314 | return rt; | |
315 | } | |
316 | ||
317 | wxIcon wxXmlResource::LoadIcon(const wxString& name) | |
318 | { | |
319 | wxIcon *icon = (wxIcon*)CreateResFromNode( | |
320 | FindResource(name, wxT("wxIcon")), NULL, NULL); | |
321 | wxIcon rt; | |
322 | ||
323 | if (icon) { rt = *icon; delete icon; } | |
324 | return rt; | |
325 | } | |
326 | ||
92e898b0 RD |
327 | |
328 | wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname) | |
329 | { | |
330 | return CreateResFromNode(FindResource(name, classname), parent, NULL); | |
331 | } | |
332 | ||
333 | bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname) | |
334 | { | |
335 | return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL; | |
336 | } | |
337 | ||
338 | ||
78d14f80 VS |
339 | bool wxXmlResource::AttachUnknownControl(const wxString& name, |
340 | wxWindow *control, wxWindow *parent) | |
341 | { | |
342 | if (parent == NULL) | |
343 | parent = control->GetParent(); | |
344 | wxWindow *container = parent->FindWindow(name + wxT("_container")); | |
345 | if (!container) | |
346 | { | |
347 | wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str()); | |
f80ea77b | 348 | return false; |
78d14f80 VS |
349 | } |
350 | return control->Reparent(container); | |
351 | } | |
352 | ||
353 | ||
77b2f9b1 | 354 | static void ProcessPlatformProperty(wxXmlNode *node) |
78d14f80 VS |
355 | { |
356 | wxString s; | |
357 | bool isok; | |
358 | ||
359 | wxXmlNode *c = node->GetChildren(); | |
360 | while (c) | |
361 | { | |
f80ea77b | 362 | isok = false; |
78d14f80 | 363 | if (!c->GetPropVal(wxT("platform"), &s)) |
f80ea77b | 364 | isok = true; |
78d14f80 VS |
365 | else |
366 | { | |
2b5f62a0 | 367 | wxStringTokenizer tkn(s, wxT(" |")); |
78d14f80 VS |
368 | |
369 | while (tkn.HasMoreTokens()) | |
370 | { | |
371 | s = tkn.GetNextToken(); | |
84389518 | 372 | #ifdef __WINDOWS__ |
d003330c VS |
373 | if (s == wxT("win")) isok = true; |
374 | #endif | |
b380439d | 375 | #if defined(__MAC__) || defined(__APPLE__) |
d003330c | 376 | if (s == wxT("mac")) isok = true; |
b380439d RD |
377 | #elif defined(__UNIX__) |
378 | if (s == wxT("unix")) isok = true; | |
78d14f80 | 379 | #endif |
d003330c VS |
380 | #ifdef __OS2__ |
381 | if (s == wxT("os2")) isok = true; | |
382 | #endif | |
383 | ||
384 | if (isok) | |
385 | break; | |
78d14f80 VS |
386 | } |
387 | } | |
388 | ||
389 | if (isok) | |
d7b1d73c | 390 | { |
78d14f80 | 391 | ProcessPlatformProperty(c); |
d7b1d73c VS |
392 | c = c->GetNext(); |
393 | } | |
78d14f80 VS |
394 | else |
395 | { | |
d7b1d73c | 396 | wxXmlNode *c2 = c->GetNext(); |
db59a97c | 397 | node->RemoveChild(c); |
78d14f80 | 398 | delete c; |
d7b1d73c | 399 | c = c2; |
78d14f80 | 400 | } |
78d14f80 VS |
401 | } |
402 | } | |
403 | ||
404 | ||
405 | ||
d614f51b | 406 | bool wxXmlResource::UpdateResources() |
78d14f80 | 407 | { |
d614f51b | 408 | bool rt = true; |
78d14f80 VS |
409 | bool modif; |
410 | # if wxUSE_FILESYSTEM | |
411 | wxFSFile *file = NULL; | |
19d0f58d | 412 | wxUnusedVar(file); |
78d14f80 VS |
413 | wxFileSystem fsys; |
414 | # endif | |
415 | ||
480505bc VS |
416 | wxString encoding(wxT("UTF-8")); |
417 | #if !wxUSE_UNICODE && wxUSE_INTL | |
418 | if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 ) | |
419 | { | |
75d38380 VS |
420 | // In case we are not using wxLocale to translate strings, convert the |
421 | // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE | |
422 | // is on, because it could break wxGetTranslation lookup. | |
480505bc VS |
423 | encoding = wxLocale::GetSystemEncodingName(); |
424 | } | |
425 | #endif | |
426 | ||
78d14f80 VS |
427 | for (size_t i = 0; i < m_data.GetCount(); i++) |
428 | { | |
429 | modif = (m_data[i].Doc == NULL); | |
430 | ||
648db587 | 431 | if (!modif && !(m_flags & wxXRC_NO_RELOADING)) |
78d14f80 VS |
432 | { |
433 | # if wxUSE_FILESYSTEM | |
434 | file = fsys.OpenFile(m_data[i].File); | |
34af0de4 | 435 | # if wxUSE_DATETIME |
78d14f80 | 436 | modif = file && file->GetModificationTime() > m_data[i].Time; |
34af0de4 VZ |
437 | # else // wxUSE_DATETIME |
438 | modif = true; | |
439 | # endif // wxUSE_DATETIME | |
78d14f80 | 440 | if (!file) |
d614f51b | 441 | { |
78d14f80 | 442 | wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str()); |
d614f51b VS |
443 | rt = false; |
444 | } | |
78d14f80 | 445 | wxDELETE(file); |
19d0f58d | 446 | wxUnusedVar(file); |
34af0de4 VZ |
447 | # else // wxUSE_FILESYSTEM |
448 | # if wxUSE_DATETIME | |
78d14f80 | 449 | modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time; |
34af0de4 VZ |
450 | # else // wxUSE_DATETIME |
451 | modif = true; | |
452 | # endif // wxUSE_DATETIME | |
453 | # endif // wxUSE_FILESYSTEM | |
78d14f80 VS |
454 | } |
455 | ||
456 | if (modif) | |
457 | { | |
648db587 VS |
458 | wxLogTrace(_T("xrc"), |
459 | _T("opening file '%s'"), m_data[i].File.c_str()); | |
460 | ||
480505bc | 461 | wxInputStream *stream = NULL; |
78d14f80 VS |
462 | |
463 | # if wxUSE_FILESYSTEM | |
464 | file = fsys.OpenFile(m_data[i].File); | |
f80ea77b WS |
465 | if (file) |
466 | stream = file->GetStream(); | |
78d14f80 VS |
467 | # else |
468 | stream = new wxFileInputStream(m_data[i].File); | |
469 | # endif | |
470 | ||
471 | if (stream) | |
472 | { | |
473 | delete m_data[i].Doc; | |
474 | m_data[i].Doc = new wxXmlDocument; | |
475 | } | |
4d876ee3 | 476 | if (!stream || !m_data[i].Doc->Load(*stream, encoding)) |
78d14f80 | 477 | { |
480505bc VS |
478 | wxLogError(_("Cannot load resources from file '%s'."), |
479 | m_data[i].File.c_str()); | |
78d14f80 | 480 | wxDELETE(m_data[i].Doc); |
d614f51b | 481 | rt = false; |
78d14f80 VS |
482 | } |
483 | else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource")) | |
484 | { | |
b5d6954b | 485 | wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str()); |
78d14f80 | 486 | wxDELETE(m_data[i].Doc); |
d614f51b | 487 | rt = false; |
78d14f80 VS |
488 | } |
489 | else | |
f80ea77b | 490 | { |
78d14f80 VS |
491 | long version; |
492 | int v1, v2, v3, v4; | |
493 | wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal( | |
494 | wxT("version"), wxT("0.0.0.0")); | |
495 | if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"), | |
496 | &v1, &v2, &v3, &v4) == 4) | |
497 | version = v1*256*256*256+v2*256*256+v3*256+v4; | |
498 | else | |
499 | version = 0; | |
500 | if (m_version == -1) | |
501 | m_version = version; | |
502 | if (m_version != version) | |
d614f51b | 503 | { |
78d14f80 | 504 | wxLogError(_("Resource files must have same version number!")); |
d614f51b VS |
505 | rt = false; |
506 | } | |
78d14f80 VS |
507 | |
508 | ProcessPlatformProperty(m_data[i].Doc->GetRoot()); | |
34af0de4 | 509 | #if wxUSE_DATETIME |
496f0a58 | 510 | #if wxUSE_FILESYSTEM |
f80ea77b | 511 | m_data[i].Time = file->GetModificationTime(); |
34af0de4 | 512 | #else // wxUSE_FILESYSTEM |
496f0a58 | 513 | m_data[i].Time = wxDateTime(wxFileModificationTime(m_data[i].File)); |
34af0de4 VZ |
514 | #endif // wxUSE_FILESYSTEM |
515 | #endif // wxUSE_DATETIME | |
f80ea77b | 516 | } |
78d14f80 VS |
517 | |
518 | # if wxUSE_FILESYSTEM | |
f80ea77b WS |
519 | wxDELETE(file); |
520 | wxUnusedVar(file); | |
78d14f80 | 521 | # else |
f80ea77b | 522 | wxDELETE(stream); |
78d14f80 VS |
523 | # endif |
524 | } | |
525 | } | |
d614f51b VS |
526 | |
527 | return rt; | |
78d14f80 VS |
528 | } |
529 | ||
530 | ||
b272b6dc RD |
531 | wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent, |
532 | const wxString& name, | |
533 | const wxString& classname, | |
47793ab8 VS |
534 | bool recursive) |
535 | { | |
536 | wxString dummy; | |
537 | wxXmlNode *node; | |
538 | ||
539 | // first search for match at the top-level nodes (as this is | |
540 | // where the resource is most commonly looked for): | |
541 | for (node = parent->GetChildren(); node; node = node->GetNext()) | |
542 | { | |
b272b6dc RD |
543 | if ( node->GetType() == wxXML_ELEMENT_NODE && |
544 | (node->GetName() == wxT("object") || | |
47793ab8 | 545 | node->GetName() == wxT("object_ref")) && |
2b5f62a0 VZ |
546 | node->GetPropVal(wxT("name"), &dummy) && dummy == name ) |
547 | { | |
548 | wxString cls(node->GetPropVal(wxT("class"), wxEmptyString)); | |
549 | if (!classname || cls == classname) | |
550 | return node; | |
551 | // object_ref may not have 'class' property: | |
552 | if (cls.empty() && node->GetName() == wxT("object_ref")) | |
553 | { | |
554 | wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString); | |
555 | if (refName.empty()) | |
556 | continue; | |
f80ea77b | 557 | wxXmlNode* refNode = FindResource(refName, wxEmptyString, true); |
2b5f62a0 VZ |
558 | if (refNode && |
559 | refNode->GetPropVal(wxT("class"), wxEmptyString) == classname) | |
560 | { | |
561 | return node; | |
562 | } | |
563 | } | |
564 | } | |
47793ab8 VS |
565 | } |
566 | ||
567 | if ( recursive ) | |
568 | for (node = parent->GetChildren(); node; node = node->GetNext()) | |
569 | { | |
b272b6dc RD |
570 | if ( node->GetType() == wxXML_ELEMENT_NODE && |
571 | (node->GetName() == wxT("object") || | |
47793ab8 VS |
572 | node->GetName() == wxT("object_ref")) ) |
573 | { | |
f80ea77b | 574 | wxXmlNode* found = DoFindResource(node, name, classname, true); |
47793ab8 VS |
575 | if ( found ) |
576 | return found; | |
577 | } | |
578 | } | |
579 | ||
580 | return NULL; | |
581 | } | |
78d14f80 | 582 | |
b272b6dc | 583 | wxXmlNode *wxXmlResource::FindResource(const wxString& name, |
47793ab8 VS |
584 | const wxString& classname, |
585 | bool recursive) | |
78d14f80 VS |
586 | { |
587 | UpdateResources(); //ensure everything is up-to-date | |
588 | ||
589 | wxString dummy; | |
590 | for (size_t f = 0; f < m_data.GetCount(); f++) | |
591 | { | |
47793ab8 VS |
592 | if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL ) |
593 | continue; | |
594 | ||
b272b6dc | 595 | wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(), |
47793ab8 VS |
596 | name, classname, recursive); |
597 | if ( found ) | |
598 | { | |
78d14f80 | 599 | #if wxUSE_FILESYSTEM |
47793ab8 | 600 | m_curFileSystem.ChangePathTo(m_data[f].File); |
78d14f80 | 601 | #endif |
47793ab8 VS |
602 | return found; |
603 | } | |
78d14f80 VS |
604 | } |
605 | ||
b5d6954b | 606 | wxLogError(_("XRC resource '%s' (class '%s') not found!"), |
78d14f80 VS |
607 | name.c_str(), classname.c_str()); |
608 | return NULL; | |
609 | } | |
610 | ||
47793ab8 VS |
611 | static void MergeNodes(wxXmlNode& dest, wxXmlNode& with) |
612 | { | |
613 | // Merge properties: | |
614 | for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext()) | |
615 | { | |
616 | wxXmlProperty *dprop; | |
617 | for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext()) | |
618 | { | |
b272b6dc | 619 | |
47793ab8 VS |
620 | if ( dprop->GetName() == prop->GetName() ) |
621 | { | |
622 | dprop->SetValue(prop->GetValue()); | |
623 | break; | |
624 | } | |
625 | } | |
78d14f80 | 626 | |
47793ab8 VS |
627 | if ( !dprop ) |
628 | dest.AddProperty(prop->GetName(), prop->GetValue()); | |
629 | } | |
630 | ||
631 | // Merge child nodes: | |
632 | for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext()) | |
633 | { | |
634 | wxString name = node->GetPropVal(wxT("name"), wxEmptyString); | |
635 | wxXmlNode *dnode; | |
636 | ||
637 | for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() ) | |
638 | { | |
639 | if ( dnode->GetName() == node->GetName() && | |
2b5f62a0 | 640 | dnode->GetPropVal(wxT("name"), wxEmptyString) == name && |
47793ab8 VS |
641 | dnode->GetType() == node->GetType() ) |
642 | { | |
643 | MergeNodes(*dnode, *node); | |
644 | break; | |
645 | } | |
646 | } | |
647 | ||
648 | if ( !dnode ) | |
b26a650c VZ |
649 | { |
650 | static const wxChar *AT_END = wxT("end"); | |
651 | wxString insert_pos = node->GetPropVal(wxT("insert_at"), AT_END); | |
652 | if ( insert_pos == AT_END ) | |
653 | { | |
654 | dest.AddChild(new wxXmlNode(*node)); | |
655 | } | |
656 | else if ( insert_pos == wxT("begin") ) | |
657 | { | |
658 | dest.InsertChild(new wxXmlNode(*node), dest.GetChildren()); | |
659 | } | |
660 | } | |
47793ab8 VS |
661 | } |
662 | ||
0bca0373 | 663 | if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().length() ) |
47793ab8 VS |
664 | dest.SetContent(with.GetContent()); |
665 | } | |
78d14f80 | 666 | |
317a0d73 VS |
667 | wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, |
668 | wxObject *instance, | |
669 | wxXmlResourceHandler *handlerToUse) | |
78d14f80 VS |
670 | { |
671 | if (node == NULL) return NULL; | |
672 | ||
47793ab8 VS |
673 | // handling of referenced resource |
674 | if ( node->GetName() == wxT("object_ref") ) | |
675 | { | |
676 | wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString); | |
f80ea77b | 677 | wxXmlNode* refNode = FindResource(refName, wxEmptyString, true); |
47793ab8 VS |
678 | |
679 | if ( !refNode ) | |
680 | { | |
b272b6dc | 681 | wxLogError(_("Referenced object node with ref=\"%s\" not found!"), |
47793ab8 VS |
682 | refName.c_str()); |
683 | return NULL; | |
684 | } | |
685 | ||
686 | wxXmlNode copy(*refNode); | |
687 | MergeNodes(copy, *node); | |
688 | ||
689 | return CreateResFromNode(©, parent, instance); | |
690 | } | |
691 | ||
8e8a4e85 VS |
692 | wxXmlResourceHandler *handler; |
693 | ||
317a0d73 | 694 | if (handlerToUse) |
b380439d | 695 | { |
317a0d73 | 696 | if (handlerToUse->CanHandle(node)) |
8e8a4e85 | 697 | { |
317a0d73 | 698 | return handlerToUse->CreateResource(node, parent, instance); |
8e8a4e85 | 699 | } |
317a0d73 VS |
700 | } |
701 | else if (node->GetName() == wxT("object")) | |
b380439d | 702 | { |
8e8a4e85 VS |
703 | wxList::compatibility_iterator ND = m_handlers.GetFirst(); |
704 | while (ND) | |
317a0d73 | 705 | { |
8e8a4e85 | 706 | handler = (wxXmlResourceHandler*)ND->GetData(); |
317a0d73 | 707 | if (handler->CanHandle(node)) |
8e8a4e85 | 708 | { |
317a0d73 | 709 | return handler->CreateResource(node, parent, instance); |
8e8a4e85 VS |
710 | } |
711 | ND = ND->GetNext(); | |
78d14f80 | 712 | } |
78d14f80 VS |
713 | } |
714 | ||
715 | wxLogError(_("No handler found for XML node '%s', class '%s'!"), | |
716 | node->GetName().c_str(), | |
717 | node->GetPropVal(wxT("class"), wxEmptyString).c_str()); | |
718 | return NULL; | |
719 | } | |
720 | ||
721 | ||
2b5f62a0 VZ |
722 | #include "wx/listimpl.cpp" |
723 | WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList); | |
17a1ebd1 | 724 | WX_DEFINE_LIST(wxXmlSubclassFactoriesList) |
2b5f62a0 VZ |
725 | |
726 | wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL; | |
727 | ||
728 | /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory) | |
729 | { | |
730 | if (!ms_subclassFactories) | |
731 | { | |
732 | ms_subclassFactories = new wxXmlSubclassFactoriesList; | |
2b5f62a0 VZ |
733 | } |
734 | ms_subclassFactories->Append(factory); | |
735 | } | |
736 | ||
737 | class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory | |
738 | { | |
739 | public: | |
740 | ~wxXmlSubclassFactoryCXX() {} | |
741 | ||
742 | wxObject *Create(const wxString& className) | |
743 | { | |
744 | wxClassInfo* classInfo = wxClassInfo::FindClass(className); | |
745 | ||
746 | if (classInfo) | |
747 | return classInfo->CreateObject(); | |
748 | else | |
749 | return NULL; | |
750 | } | |
751 | }; | |
752 | ||
753 | ||
754 | ||
78d14f80 | 755 | |
78d14f80 VS |
756 | wxXmlResourceHandler::wxXmlResourceHandler() |
757 | : m_node(NULL), m_parent(NULL), m_instance(NULL), | |
9a8d8c5a | 758 | m_parentAsWindow(NULL) |
78d14f80 VS |
759 | {} |
760 | ||
761 | ||
762 | ||
763 | wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance) | |
764 | { | |
765 | wxXmlNode *myNode = m_node; | |
766 | wxString myClass = m_class; | |
767 | wxObject *myParent = m_parent, *myInstance = m_instance; | |
9a8d8c5a | 768 | wxWindow *myParentAW = m_parentAsWindow; |
78d14f80 | 769 | |
daa85ee3 | 770 | m_instance = instance; |
b272b6dc | 771 | if (!m_instance && node->HasProp(wxT("subclass")) && |
daa85ee3 VS |
772 | !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING)) |
773 | { | |
774 | wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString); | |
2b5f62a0 | 775 | if (!subclass.empty()) |
daa85ee3 | 776 | { |
461932ae | 777 | for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst(); |
2b5f62a0 VZ |
778 | i; i = i->GetNext()) |
779 | { | |
780 | m_instance = i->GetData()->Create(subclass); | |
781 | if (m_instance) | |
782 | break; | |
783 | } | |
daa85ee3 | 784 | |
2b5f62a0 VZ |
785 | if (!m_instance) |
786 | { | |
787 | wxString name = node->GetPropVal(wxT("name"), wxEmptyString); | |
788 | wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"), | |
789 | subclass.c_str(), name.c_str()); | |
790 | } | |
791 | } | |
daa85ee3 VS |
792 | } |
793 | ||
78d14f80 VS |
794 | m_node = node; |
795 | m_class = node->GetPropVal(wxT("class"), wxEmptyString); | |
796 | m_parent = parent; | |
78d14f80 | 797 | m_parentAsWindow = wxDynamicCast(m_parent, wxWindow); |
78d14f80 VS |
798 | |
799 | wxObject *returned = DoCreateResource(); | |
800 | ||
801 | m_node = myNode; | |
802 | m_class = myClass; | |
803 | m_parent = myParent; m_parentAsWindow = myParentAW; | |
9a8d8c5a | 804 | m_instance = myInstance; |
78d14f80 VS |
805 | |
806 | return returned; | |
807 | } | |
808 | ||
809 | ||
810 | void wxXmlResourceHandler::AddStyle(const wxString& name, int value) | |
811 | { | |
812 | m_styleNames.Add(name); | |
813 | m_styleValues.Add(value); | |
814 | } | |
815 | ||
816 | ||
817 | ||
818 | void wxXmlResourceHandler::AddWindowStyles() | |
819 | { | |
9dc579b3 | 820 | XRC_ADD_STYLE(wxCLIP_CHILDREN); |
d54a3e73 VZ |
821 | |
822 | // the border styles all have the old and new names, recognize both for now | |
823 | XRC_ADD_STYLE(wxSIMPLE_BORDER); XRC_ADD_STYLE(wxBORDER_SIMPLE); | |
824 | XRC_ADD_STYLE(wxSUNKEN_BORDER); XRC_ADD_STYLE(wxBORDER_SUNKEN); | |
825 | XRC_ADD_STYLE(wxDOUBLE_BORDER); XRC_ADD_STYLE(wxBORDER_DOUBLE); | |
826 | XRC_ADD_STYLE(wxRAISED_BORDER); XRC_ADD_STYLE(wxBORDER_RAISED); | |
827 | XRC_ADD_STYLE(wxSTATIC_BORDER); XRC_ADD_STYLE(wxBORDER_STATIC); | |
828 | XRC_ADD_STYLE(wxNO_BORDER); XRC_ADD_STYLE(wxBORDER_NONE); | |
829 | ||
daa85ee3 VS |
830 | XRC_ADD_STYLE(wxTRANSPARENT_WINDOW); |
831 | XRC_ADD_STYLE(wxWANTS_CHARS); | |
43840d8b | 832 | XRC_ADD_STYLE(wxTAB_TRAVERSAL); |
daa85ee3 | 833 | XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE); |
7539ba56 | 834 | XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE); |
162a4f93 | 835 | XRC_ADD_STYLE(wxALWAYS_SHOW_SB); |
9f4ed861 | 836 | XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS); |
b0802e64 | 837 | XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY); |
78d14f80 VS |
838 | } |
839 | ||
840 | ||
841 | ||
842 | bool wxXmlResourceHandler::HasParam(const wxString& param) | |
843 | { | |
844 | return (GetParamNode(param) != NULL); | |
845 | } | |
846 | ||
847 | ||
848 | int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults) | |
849 | { | |
850 | wxString s = GetParamValue(param); | |
851 | ||
852 | if (!s) return defaults; | |
853 | ||
2b5f62a0 | 854 | wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK); |
78d14f80 VS |
855 | int style = 0; |
856 | int index; | |
857 | wxString fl; | |
858 | while (tkn.HasMoreTokens()) | |
859 | { | |
860 | fl = tkn.GetNextToken(); | |
861 | index = m_styleNames.Index(fl); | |
862 | if (index != wxNOT_FOUND) | |
863 | style |= m_styleValues[index]; | |
864 | else | |
865 | wxLogError(_("Unknown style flag ") + fl); | |
866 | } | |
867 | return style; | |
868 | } | |
869 | ||
870 | ||
871 | ||
ee1046d1 | 872 | wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate) |
78d14f80 | 873 | { |
7b56015f VS |
874 | wxXmlNode *parNode = GetParamNode(param); |
875 | wxString str1(GetNodeContent(parNode)); | |
78d14f80 VS |
876 | wxString str2; |
877 | const wxChar *dt; | |
878 | wxChar amp_char; | |
879 | ||
b272b6dc RD |
880 | // VS: First version of XRC resources used $ instead of & (which is |
881 | // illegal in XML), but later I realized that '_' fits this purpose | |
718cf160 | 882 | // much better (because &File means "File with F underlined"). |
78d14f80 VS |
883 | if (m_resource->CompareVersion(2,3,0,1) < 0) |
884 | amp_char = wxT('$'); | |
885 | else | |
886 | amp_char = wxT('_'); | |
887 | ||
888 | for (dt = str1.c_str(); *dt; dt++) | |
889 | { | |
890 | // Remap amp_char to &, map double amp_char to amp_char (for things | |
891 | // like "&File..." -- this is illegal in XML, so we use "_File..."): | |
892 | if (*dt == amp_char) | |
893 | { | |
894 | if ( *(++dt) == amp_char ) | |
895 | str2 << amp_char; | |
896 | else | |
897 | str2 << wxT('&') << *dt; | |
898 | } | |
984c33c9 | 899 | // Remap \n to CR, \r to LF, \t to TAB, \\ to \: |
78d14f80 VS |
900 | else if (*dt == wxT('\\')) |
901 | switch (*(++dt)) | |
902 | { | |
984c33c9 VS |
903 | case wxT('n'): |
904 | str2 << wxT('\n'); | |
905 | break; | |
e7a3a5a5 | 906 | |
984c33c9 VS |
907 | case wxT('t'): |
908 | str2 << wxT('\t'); | |
909 | break; | |
e7a3a5a5 | 910 | |
984c33c9 VS |
911 | case wxT('r'): |
912 | str2 << wxT('\r'); | |
913 | break; | |
914 | ||
915 | case wxT('\\') : | |
916 | // "\\" wasn't translated to "\" prior to 2.5.3.0: | |
917 | if (m_resource->CompareVersion(2,5,3,0) >= 0) | |
918 | { | |
919 | str2 << wxT('\\'); | |
920 | break; | |
921 | } | |
922 | // else fall-through to default: branch below | |
e7a3a5a5 | 923 | |
984c33c9 VS |
924 | default: |
925 | str2 << wxT('\\') << *dt; | |
926 | break; | |
78d14f80 VS |
927 | } |
928 | else str2 << *dt; | |
929 | } | |
b272b6dc | 930 | |
7b56015f VS |
931 | if (m_resource->GetFlags() & wxXRC_USE_LOCALE) |
932 | { | |
933 | if (translate && parNode && | |
934 | parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0")) | |
935 | { | |
d4a724d4 | 936 | return wxGetTranslation(str2, m_resource->GetDomain()); |
7b56015f VS |
937 | } |
938 | else | |
939 | { | |
940 | #if wxUSE_UNICODE | |
941 | return str2; | |
942 | #else | |
943 | // The string is internally stored as UTF-8, we have to convert | |
944 | // it into system's default encoding so that it can be displayed: | |
945 | return wxString(str2.mb_str(wxConvUTF8), wxConvLocal); | |
946 | #endif | |
947 | } | |
948 | } | |
8516a98b DS |
949 | |
950 | // If wxXRC_USE_LOCALE is not set, then the string is already in | |
951 | // system's default encoding in ANSI build, so we don't have to | |
952 | // do anything special here. | |
953 | return str2; | |
78d14f80 VS |
954 | } |
955 | ||
956 | ||
957 | ||
958 | long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv) | |
959 | { | |
960 | long value; | |
961 | wxString str1 = GetParamValue(param); | |
962 | ||
963 | if (!str1.ToLong(&value)) | |
964 | value = defaultv; | |
965 | ||
966 | return value; | |
967 | } | |
e7a3a5a5 | 968 | |
1df61962 VS |
969 | float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv) |
970 | { | |
971 | double value; | |
972 | wxString str1 = GetParamValue(param); | |
973 | ||
e7a3a5a5 | 974 | #ifndef __WXWINCE__ |
1df61962 | 975 | const char *prevlocale = setlocale(LC_NUMERIC, "C"); |
e7a3a5a5 WS |
976 | #endif |
977 | ||
1df61962 VS |
978 | if (!str1.ToDouble(&value)) |
979 | value = defaultv; | |
980 | ||
e7a3a5a5 | 981 | #ifndef __WXWINCE__ |
1df61962 | 982 | setlocale(LC_NUMERIC, prevlocale); |
e7a3a5a5 | 983 | #endif |
78d14f80 | 984 | |
17a1ebd1 | 985 | return wx_truncate_cast(float, value); |
1df61962 | 986 | } |
78d14f80 | 987 | |
af1337b0 | 988 | |
78d14f80 VS |
989 | int wxXmlResourceHandler::GetID() |
990 | { | |
13de23f6 | 991 | return wxXmlResource::GetXRCID(GetName()); |
78d14f80 VS |
992 | } |
993 | ||
994 | ||
af1337b0 | 995 | |
78d14f80 VS |
996 | wxString wxXmlResourceHandler::GetName() |
997 | { | |
998 | return m_node->GetPropVal(wxT("name"), wxT("-1")); | |
999 | } | |
1000 | ||
1001 | ||
1002 | ||
1003 | bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv) | |
1004 | { | |
1005 | wxString v = GetParamValue(param); | |
1006 | v.MakeLower(); | |
1007 | if (!v) return defaultv; | |
8516a98b DS |
1008 | |
1009 | return (v == wxT("1")); | |
78d14f80 VS |
1010 | } |
1011 | ||
1012 | ||
1df61962 VS |
1013 | static wxColour GetSystemColour(const wxString& name) |
1014 | { | |
1015 | if (!name.empty()) | |
1016 | { | |
1017 | #define SYSCLR(clr) \ | |
1018 | if (name == _T(#clr)) return wxSystemSettings::GetColour(clr); | |
1019 | SYSCLR(wxSYS_COLOUR_SCROLLBAR) | |
1020 | SYSCLR(wxSYS_COLOUR_BACKGROUND) | |
1021 | SYSCLR(wxSYS_COLOUR_DESKTOP) | |
1022 | SYSCLR(wxSYS_COLOUR_ACTIVECAPTION) | |
1023 | SYSCLR(wxSYS_COLOUR_INACTIVECAPTION) | |
1024 | SYSCLR(wxSYS_COLOUR_MENU) | |
1025 | SYSCLR(wxSYS_COLOUR_WINDOW) | |
1026 | SYSCLR(wxSYS_COLOUR_WINDOWFRAME) | |
1027 | SYSCLR(wxSYS_COLOUR_MENUTEXT) | |
1028 | SYSCLR(wxSYS_COLOUR_WINDOWTEXT) | |
1029 | SYSCLR(wxSYS_COLOUR_CAPTIONTEXT) | |
1030 | SYSCLR(wxSYS_COLOUR_ACTIVEBORDER) | |
1031 | SYSCLR(wxSYS_COLOUR_INACTIVEBORDER) | |
1032 | SYSCLR(wxSYS_COLOUR_APPWORKSPACE) | |
1033 | SYSCLR(wxSYS_COLOUR_HIGHLIGHT) | |
1034 | SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT) | |
1035 | SYSCLR(wxSYS_COLOUR_BTNFACE) | |
1036 | SYSCLR(wxSYS_COLOUR_3DFACE) | |
1037 | SYSCLR(wxSYS_COLOUR_BTNSHADOW) | |
1038 | SYSCLR(wxSYS_COLOUR_3DSHADOW) | |
1039 | SYSCLR(wxSYS_COLOUR_GRAYTEXT) | |
1040 | SYSCLR(wxSYS_COLOUR_BTNTEXT) | |
1041 | SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT) | |
1042 | SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT) | |
1043 | SYSCLR(wxSYS_COLOUR_BTNHILIGHT) | |
1044 | SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT) | |
1045 | SYSCLR(wxSYS_COLOUR_3DHILIGHT) | |
1046 | SYSCLR(wxSYS_COLOUR_3DDKSHADOW) | |
1047 | SYSCLR(wxSYS_COLOUR_3DLIGHT) | |
1048 | SYSCLR(wxSYS_COLOUR_INFOTEXT) | |
1049 | SYSCLR(wxSYS_COLOUR_INFOBK) | |
1050 | SYSCLR(wxSYS_COLOUR_LISTBOX) | |
1051 | SYSCLR(wxSYS_COLOUR_HOTLIGHT) | |
1052 | SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION) | |
1053 | SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION) | |
1054 | SYSCLR(wxSYS_COLOUR_MENUHILIGHT) | |
1055 | SYSCLR(wxSYS_COLOUR_MENUBAR) | |
1056 | #undef SYSCLR | |
1057 | } | |
1058 | ||
1059 | return wxNullColour; | |
1060 | } | |
78d14f80 | 1061 | |
984f1d84 | 1062 | wxColour wxXmlResourceHandler::GetColour(const wxString& param, const wxColour& defaultv) |
78d14f80 VS |
1063 | { |
1064 | wxString v = GetParamValue(param); | |
984f1d84 VS |
1065 | |
1066 | if ( v.empty() ) | |
1067 | return defaultv; | |
1068 | ||
68b4e4cf | 1069 | wxColour clr; |
1df61962 | 1070 | |
68b4e4cf WS |
1071 | // wxString -> wxColour conversion |
1072 | if (!clr.Set(v)) | |
78d14f80 | 1073 | { |
1df61962 VS |
1074 | // the colour doesn't use #RRGGBB format, check if it is symbolic |
1075 | // colour name: | |
68b4e4cf | 1076 | clr = GetSystemColour(v); |
1df61962 VS |
1077 | if (clr.Ok()) |
1078 | return clr; | |
e7a3a5a5 | 1079 | |
b5d6954b | 1080 | wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."), |
78d14f80 VS |
1081 | v.c_str(), param.c_str()); |
1082 | return wxNullColour; | |
1083 | } | |
1084 | ||
68b4e4cf | 1085 | return clr; |
78d14f80 VS |
1086 | } |
1087 | ||
1088 | ||
1089 | ||
92e898b0 | 1090 | wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, |
db59a97c VS |
1091 | const wxArtClient& defaultArtClient, |
1092 | wxSize size) | |
78d14f80 | 1093 | { |
db59a97c VS |
1094 | /* If the bitmap is specified as stock item, query wxArtProvider for it: */ |
1095 | wxXmlNode *bmpNode = GetParamNode(param); | |
1096 | if ( bmpNode ) | |
af1337b0 | 1097 | { |
db59a97c VS |
1098 | wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString); |
1099 | if ( !sid.empty() ) | |
1100 | { | |
b3e85292 VS |
1101 | wxString scl = bmpNode->GetPropVal(wxT("stock_client"), wxEmptyString); |
1102 | if (scl.empty()) | |
1103 | scl = defaultArtClient; | |
1104 | else | |
1105 | scl = wxART_MAKE_CLIENT_ID_FROM_STR(scl); | |
e7a3a5a5 | 1106 | |
92e898b0 | 1107 | wxBitmap stockArt = |
db59a97c | 1108 | wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid), |
b3e85292 | 1109 | scl, size); |
db59a97c VS |
1110 | if ( stockArt.Ok() ) |
1111 | return stockArt; | |
1112 | } | |
af1337b0 JS |
1113 | } |
1114 | ||
92e898b0 | 1115 | /* ...or load the bitmap from file: */ |
78d14f80 | 1116 | wxString name = GetParamValue(param); |
e7a3a5a5 | 1117 | if (name.empty()) return wxNullBitmap; |
78d14f80 VS |
1118 | #if wxUSE_FILESYSTEM |
1119 | wxFSFile *fsfile = GetCurFileSystem().OpenFile(name); | |
1120 | if (fsfile == NULL) | |
1121 | { | |
d0b50ad9 VS |
1122 | wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), |
1123 | name.c_str()); | |
78d14f80 VS |
1124 | return wxNullBitmap; |
1125 | } | |
1126 | wxImage img(*(fsfile->GetStream())); | |
1127 | delete fsfile; | |
1128 | #else | |
45f3249b | 1129 | wxImage img(name); |
78d14f80 | 1130 | #endif |
af1337b0 | 1131 | |
78d14f80 VS |
1132 | if (!img.Ok()) |
1133 | { | |
1df61962 | 1134 | wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), |
45f3249b | 1135 | name.c_str()); |
78d14f80 VS |
1136 | return wxNullBitmap; |
1137 | } | |
1138 | if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y); | |
b272b6dc | 1139 | return wxBitmap(img); |
78d14f80 VS |
1140 | } |
1141 | ||
1142 | ||
1143 | ||
92e898b0 | 1144 | wxIcon wxXmlResourceHandler::GetIcon(const wxString& param, |
db59a97c VS |
1145 | const wxArtClient& defaultArtClient, |
1146 | wxSize size) | |
78d14f80 | 1147 | { |
78d14f80 | 1148 | wxIcon icon; |
db59a97c | 1149 | icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size)); |
78d14f80 VS |
1150 | return icon; |
1151 | } | |
1152 | ||
1153 | ||
1154 | ||
1155 | wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param) | |
1156 | { | |
2b5f62a0 VZ |
1157 | wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!")); |
1158 | ||
78d14f80 VS |
1159 | wxXmlNode *n = m_node->GetChildren(); |
1160 | ||
1161 | while (n) | |
1162 | { | |
1163 | if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param) | |
1164 | return n; | |
1165 | n = n->GetNext(); | |
1166 | } | |
1167 | return NULL; | |
1168 | } | |
1169 | ||
1170 | ||
2d672c46 MW |
1171 | |
1172 | bool wxXmlResourceHandler::IsOfClass(wxXmlNode *node, const wxString& classname) | |
1173 | { | |
1174 | return node->GetPropVal(wxT("class"), wxEmptyString) == classname; | |
1175 | } | |
1176 | ||
1177 | ||
1178 | ||
78d14f80 VS |
1179 | wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node) |
1180 | { | |
1181 | wxXmlNode *n = node; | |
1182 | if (n == NULL) return wxEmptyString; | |
1183 | n = n->GetChildren(); | |
1184 | ||
1185 | while (n) | |
1186 | { | |
1187 | if (n->GetType() == wxXML_TEXT_NODE || | |
1188 | n->GetType() == wxXML_CDATA_SECTION_NODE) | |
1189 | return n->GetContent(); | |
1190 | n = n->GetNext(); | |
1191 | } | |
1192 | return wxEmptyString; | |
1193 | } | |
1194 | ||
1195 | ||
1196 | ||
1197 | wxString wxXmlResourceHandler::GetParamValue(const wxString& param) | |
1198 | { | |
e7a3a5a5 | 1199 | if (param.empty()) |
78d14f80 VS |
1200 | return GetNodeContent(m_node); |
1201 | else | |
1202 | return GetNodeContent(GetParamNode(param)); | |
1203 | } | |
1204 | ||
1205 | ||
1206 | ||
0c00c86f VS |
1207 | wxSize wxXmlResourceHandler::GetSize(const wxString& param, |
1208 | wxWindow *windowToUse) | |
78d14f80 VS |
1209 | { |
1210 | wxString s = GetParamValue(param); | |
e7a3a5a5 | 1211 | if (s.empty()) s = wxT("-1,-1"); |
78d14f80 | 1212 | bool is_dlg; |
d1f47235 | 1213 | long sx, sy = 0; |
78d14f80 | 1214 | |
88a7a4e1 | 1215 | is_dlg = s[s.length()-1] == wxT('d'); |
78d14f80 VS |
1216 | if (is_dlg) s.RemoveLast(); |
1217 | ||
1218 | if (!s.BeforeFirst(wxT(',')).ToLong(&sx) || | |
1219 | !s.AfterLast(wxT(',')).ToLong(&sy)) | |
1220 | { | |
00393283 | 1221 | wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str()); |
78d14f80 VS |
1222 | return wxDefaultSize; |
1223 | } | |
1224 | ||
1225 | if (is_dlg) | |
1226 | { | |
0c00c86f VS |
1227 | if (windowToUse) |
1228 | { | |
1229 | return wxDLG_UNIT(windowToUse, wxSize(sx, sy)); | |
1230 | } | |
1231 | else if (m_parentAsWindow) | |
1232 | { | |
78d14f80 | 1233 | return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy)); |
0c00c86f | 1234 | } |
78d14f80 VS |
1235 | else |
1236 | { | |
1237 | wxLogError(_("Cannot convert dialog units: dialog unknown.")); | |
1238 | return wxDefaultSize; | |
1239 | } | |
1240 | } | |
8516a98b DS |
1241 | |
1242 | return wxSize(sx, sy); | |
78d14f80 VS |
1243 | } |
1244 | ||
1245 | ||
1246 | ||
1247 | wxPoint wxXmlResourceHandler::GetPosition(const wxString& param) | |
1248 | { | |
1249 | wxSize sz = GetSize(param); | |
1250 | return wxPoint(sz.x, sz.y); | |
1251 | } | |
1252 | ||
1253 | ||
1254 | ||
0c00c86f VS |
1255 | wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, |
1256 | wxCoord defaultv, | |
1257 | wxWindow *windowToUse) | |
78d14f80 VS |
1258 | { |
1259 | wxString s = GetParamValue(param); | |
e7a3a5a5 | 1260 | if (s.empty()) return defaultv; |
78d14f80 VS |
1261 | bool is_dlg; |
1262 | long sx; | |
1263 | ||
88a7a4e1 | 1264 | is_dlg = s[s.length()-1] == wxT('d'); |
78d14f80 VS |
1265 | if (is_dlg) s.RemoveLast(); |
1266 | ||
1267 | if (!s.ToLong(&sx)) | |
1268 | { | |
00393283 | 1269 | wxLogError(_("Cannot parse dimension from '%s'."), s.c_str()); |
78d14f80 VS |
1270 | return defaultv; |
1271 | } | |
1272 | ||
1273 | if (is_dlg) | |
1274 | { | |
0c00c86f VS |
1275 | if (windowToUse) |
1276 | { | |
1277 | return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x; | |
1278 | } | |
1279 | else if (m_parentAsWindow) | |
1280 | { | |
78d14f80 | 1281 | return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x; |
0c00c86f | 1282 | } |
78d14f80 VS |
1283 | else |
1284 | { | |
1285 | wxLogError(_("Cannot convert dialog units: dialog unknown.")); | |
1286 | return defaultv; | |
1287 | } | |
1288 | } | |
8516a98b DS |
1289 | |
1290 | return sx; | |
78d14f80 VS |
1291 | } |
1292 | ||
1293 | ||
1df61962 VS |
1294 | // Get system font index using indexname |
1295 | static wxFont GetSystemFont(const wxString& name) | |
1296 | { | |
1297 | if (!name.empty()) | |
1298 | { | |
1299 | #define SYSFNT(fnt) \ | |
1300 | if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt); | |
1301 | SYSFNT(wxSYS_OEM_FIXED_FONT) | |
1302 | SYSFNT(wxSYS_ANSI_FIXED_FONT) | |
1303 | SYSFNT(wxSYS_ANSI_VAR_FONT) | |
1304 | SYSFNT(wxSYS_SYSTEM_FONT) | |
1305 | SYSFNT(wxSYS_DEVICE_DEFAULT_FONT) | |
1306 | SYSFNT(wxSYS_DEFAULT_PALETTE) | |
1307 | SYSFNT(wxSYS_SYSTEM_FIXED_FONT) | |
1308 | SYSFNT(wxSYS_DEFAULT_GUI_FONT) | |
1309 | #undef SYSFNT | |
1310 | } | |
1311 | ||
1312 | return wxNullFont; | |
1313 | } | |
78d14f80 VS |
1314 | |
1315 | wxFont wxXmlResourceHandler::GetFont(const wxString& param) | |
1316 | { | |
1317 | wxXmlNode *font_node = GetParamNode(param); | |
1318 | if (font_node == NULL) | |
1319 | { | |
00393283 | 1320 | wxLogError(_("Cannot find font node '%s'."), param.c_str()); |
78d14f80 VS |
1321 | return wxNullFont; |
1322 | } | |
1323 | ||
1324 | wxXmlNode *oldnode = m_node; | |
1325 | m_node = font_node; | |
1326 | ||
1df61962 | 1327 | // font attributes: |
78d14f80 | 1328 | |
1df61962 | 1329 | // size |
94245f6d | 1330 | int isize = -1; |
1df61962 | 1331 | bool hasSize = HasParam(wxT("size")); |
e7a3a5a5 | 1332 | if (hasSize) |
94245f6d | 1333 | isize = GetLong(wxT("size"), -1); |
78d14f80 | 1334 | |
1df61962 VS |
1335 | // style |
1336 | int istyle = wxNORMAL; | |
1337 | bool hasStyle = HasParam(wxT("style")); | |
1338 | if (hasStyle) | |
1339 | { | |
1340 | wxString style = GetParamValue(wxT("style")); | |
e7a3a5a5 | 1341 | if (style == wxT("italic")) |
1df61962 | 1342 | istyle = wxITALIC; |
e7a3a5a5 | 1343 | else if (style == wxT("slant")) |
1df61962 VS |
1344 | istyle = wxSLANT; |
1345 | } | |
78d14f80 | 1346 | |
1df61962 VS |
1347 | // weight |
1348 | int iweight = wxNORMAL; | |
1349 | bool hasWeight = HasParam(wxT("weight")); | |
1350 | if (hasWeight) | |
1351 | { | |
1352 | wxString weight = GetParamValue(wxT("weight")); | |
e7a3a5a5 | 1353 | if (weight == wxT("bold")) |
1df61962 | 1354 | iweight = wxBOLD; |
e7a3a5a5 | 1355 | else if (weight == wxT("light")) |
1df61962 VS |
1356 | iweight = wxLIGHT; |
1357 | } | |
e7a3a5a5 | 1358 | |
1df61962 VS |
1359 | // underline |
1360 | bool hasUnderlined = HasParam(wxT("underlined")); | |
1361 | bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false; | |
78d14f80 | 1362 | |
1df61962 VS |
1363 | // family and facename |
1364 | int ifamily = wxDEFAULT; | |
1365 | bool hasFamily = HasParam(wxT("family")); | |
1366 | if (hasFamily) | |
78d14f80 | 1367 | { |
1df61962 VS |
1368 | wxString family = GetParamValue(wxT("family")); |
1369 | if (family == wxT("decorative")) ifamily = wxDECORATIVE; | |
1370 | else if (family == wxT("roman")) ifamily = wxROMAN; | |
1371 | else if (family == wxT("script")) ifamily = wxSCRIPT; | |
1372 | else if (family == wxT("swiss")) ifamily = wxSWISS; | |
1373 | else if (family == wxT("modern")) ifamily = wxMODERN; | |
1374 | else if (family == wxT("teletype")) ifamily = wxTELETYPE; | |
1375 | } | |
e7a3a5a5 WS |
1376 | |
1377 | ||
1df61962 VS |
1378 | wxString facename; |
1379 | bool hasFacename = HasParam(wxT("face")); | |
1380 | if (hasFacename) | |
1381 | { | |
1382 | wxString faces = GetParamValue(wxT("face")); | |
6540132f | 1383 | wxArrayString facenames(wxFontEnumerator::GetFacenames()); |
1df61962 VS |
1384 | wxStringTokenizer tk(faces, wxT(",")); |
1385 | while (tk.HasMoreTokens()) | |
78d14f80 | 1386 | { |
6540132f | 1387 | int index = facenames.Index(tk.GetNextToken(), false); |
1df61962 VS |
1388 | if (index != wxNOT_FOUND) |
1389 | { | |
6540132f | 1390 | facename = facenames[index]; |
1df61962 VS |
1391 | break; |
1392 | } | |
78d14f80 VS |
1393 | } |
1394 | } | |
1395 | ||
1df61962 VS |
1396 | // encoding |
1397 | wxFontEncoding enc = wxFONTENCODING_DEFAULT; | |
1398 | bool hasEncoding = HasParam(wxT("encoding")); | |
1399 | if (hasEncoding) | |
1400 | { | |
1401 | wxString encoding = GetParamValue(wxT("encoding")); | |
1402 | wxFontMapper mapper; | |
e7a3a5a5 | 1403 | if (!encoding.empty()) |
1df61962 VS |
1404 | enc = mapper.CharsetToEncoding(encoding); |
1405 | if (enc == wxFONTENCODING_SYSTEM) | |
1406 | enc = wxFONTENCODING_DEFAULT; | |
1407 | } | |
78d14f80 | 1408 | |
1df61962 | 1409 | // is this font based on a system font? |
94245f6d | 1410 | wxFont font = GetSystemFont(GetParamValue(wxT("sysfont"))); |
e7a3a5a5 | 1411 | |
94245f6d | 1412 | if (font.Ok()) |
1df61962 | 1413 | { |
94245f6d VZ |
1414 | if (hasSize && isize != -1) |
1415 | font.SetPointSize(isize); | |
1df61962 | 1416 | else if (HasParam(wxT("relativesize"))) |
94245f6d | 1417 | font.SetPointSize(int(font.GetPointSize() * |
1df61962 | 1418 | GetFloat(wxT("relativesize")))); |
e7a3a5a5 | 1419 | |
1df61962 | 1420 | if (hasStyle) |
94245f6d | 1421 | font.SetStyle(istyle); |
1df61962 | 1422 | if (hasWeight) |
94245f6d | 1423 | font.SetWeight(iweight); |
1df61962 | 1424 | if (hasUnderlined) |
94245f6d | 1425 | font.SetUnderlined(underlined); |
1df61962 | 1426 | if (hasFamily) |
94245f6d | 1427 | font.SetFamily(ifamily); |
1df61962 | 1428 | if (hasFacename) |
94245f6d | 1429 | font.SetFaceName(facename); |
1df61962 | 1430 | if (hasEncoding) |
94245f6d VZ |
1431 | font.SetDefaultEncoding(enc); |
1432 | } | |
1433 | else // not based on system font | |
1434 | { | |
1435 | font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize, | |
1436 | ifamily, istyle, iweight, | |
1437 | underlined, facename, enc); | |
1df61962 | 1438 | } |
8516a98b DS |
1439 | |
1440 | m_node = oldnode; | |
94245f6d | 1441 | return font; |
78d14f80 VS |
1442 | } |
1443 | ||
1444 | ||
1445 | void wxXmlResourceHandler::SetupWindow(wxWindow *wnd) | |
1446 | { | |
1447 | //FIXME : add cursor | |
1448 | ||
1449 | if (HasParam(wxT("exstyle"))) | |
0099f343 JS |
1450 | // Have to OR it with existing style, since |
1451 | // some implementations (e.g. wxGTK) use the extra style | |
1452 | // during creation | |
1453 | wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle"))); | |
78d14f80 VS |
1454 | if (HasParam(wxT("bg"))) |
1455 | wnd->SetBackgroundColour(GetColour(wxT("bg"))); | |
1456 | if (HasParam(wxT("fg"))) | |
1457 | wnd->SetForegroundColour(GetColour(wxT("fg"))); | |
1458 | if (GetBool(wxT("enabled"), 1) == 0) | |
f80ea77b | 1459 | wnd->Enable(false); |
78d14f80 VS |
1460 | if (GetBool(wxT("focused"), 0) == 1) |
1461 | wnd->SetFocus(); | |
1462 | if (GetBool(wxT("hidden"), 0) == 1) | |
f80ea77b | 1463 | wnd->Show(false); |
78d14f80 VS |
1464 | #if wxUSE_TOOLTIPS |
1465 | if (HasParam(wxT("tooltip"))) | |
1466 | wnd->SetToolTip(GetText(wxT("tooltip"))); | |
1467 | #endif | |
1468 | if (HasParam(wxT("font"))) | |
1469 | wnd->SetFont(GetFont()); | |
b23030d6 JS |
1470 | if (HasParam(wxT("help"))) |
1471 | wnd->SetHelpText(GetText(wxT("help"))); | |
78d14f80 VS |
1472 | } |
1473 | ||
1474 | ||
1475 | void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only) | |
1476 | { | |
1477 | wxXmlNode *n = m_node->GetChildren(); | |
1478 | ||
1479 | while (n) | |
1480 | { | |
1481 | if (n->GetType() == wxXML_ELEMENT_NODE && | |
0fa2e104 | 1482 | (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref"))) |
78d14f80 | 1483 | { |
317a0d73 VS |
1484 | m_resource->CreateResFromNode(n, parent, NULL, |
1485 | this_hnd_only ? this : NULL); | |
78d14f80 VS |
1486 | } |
1487 | n = n->GetNext(); | |
1488 | } | |
1489 | } | |
1490 | ||
1491 | ||
1492 | void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode) | |
1493 | { | |
1494 | wxXmlNode *root; | |
1495 | if (rootnode == NULL) root = m_node; else root = rootnode; | |
1496 | wxXmlNode *n = root->GetChildren(); | |
1497 | ||
1498 | while (n) | |
1499 | { | |
1500 | if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n)) | |
1501 | { | |
1502 | CreateResource(n, parent, NULL); | |
1503 | } | |
1504 | n = n->GetNext(); | |
1505 | } | |
1506 | } | |
1507 | ||
1508 | ||
1509 | ||
1510 | ||
1511 | ||
1512 | ||
1513 | ||
5ed345b7 | 1514 | // --------------- XRCID implementation ----------------------------- |
78d14f80 | 1515 | |
5ed345b7 | 1516 | #define XRCID_TABLE_SIZE 1024 |
78d14f80 VS |
1517 | |
1518 | ||
5ed345b7 | 1519 | struct XRCID_record |
78d14f80 VS |
1520 | { |
1521 | int id; | |
00393283 | 1522 | wxChar *key; |
5ed345b7 | 1523 | XRCID_record *next; |
78d14f80 VS |
1524 | }; |
1525 | ||
5ed345b7 | 1526 | static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL}; |
78d14f80 | 1527 | |
9b2a7469 | 1528 | static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = wxID_NONE) |
78d14f80 | 1529 | { |
78d14f80 VS |
1530 | int index = 0; |
1531 | ||
00393283 | 1532 | for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c; |
5ed345b7 | 1533 | index %= XRCID_TABLE_SIZE; |
78d14f80 | 1534 | |
5ed345b7 | 1535 | XRCID_record *oldrec = NULL; |
5ed345b7 | 1536 | for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next) |
78d14f80 | 1537 | { |
00393283 | 1538 | if (wxStrcmp(rec->key, str_id) == 0) |
78d14f80 VS |
1539 | { |
1540 | return rec->id; | |
1541 | } | |
78d14f80 VS |
1542 | oldrec = rec; |
1543 | } | |
1544 | ||
5ed345b7 VS |
1545 | XRCID_record **rec_var = (oldrec == NULL) ? |
1546 | &XRCID_Records[index] : &oldrec->next; | |
1547 | *rec_var = new XRCID_record; | |
00393283 | 1548 | (*rec_var)->key = wxStrdup(str_id); |
78d14f80 VS |
1549 | (*rec_var)->next = NULL; |
1550 | ||
85452d74 | 1551 | wxChar *end; |
9b2a7469 | 1552 | if (value_if_not_found != wxID_NONE) |
13de23f6 | 1553 | (*rec_var)->id = value_if_not_found; |
85452d74 VS |
1554 | else |
1555 | { | |
13de23f6 VS |
1556 | int asint = wxStrtol(str_id, &end, 10); |
1557 | if (*str_id && *end == 0) | |
1558 | { | |
1559 | // if str_id was integer, keep it verbosely: | |
1560 | (*rec_var)->id = asint; | |
1561 | } | |
1562 | else | |
1563 | { | |
3a301301 | 1564 | (*rec_var)->id = wxNewId(); |
13de23f6 | 1565 | } |
85452d74 VS |
1566 | } |
1567 | ||
78d14f80 VS |
1568 | return (*rec_var)->id; |
1569 | } | |
1570 | ||
3b2a000e VS |
1571 | static void AddStdXRCID_Records(); |
1572 | ||
9b2a7469 VZ |
1573 | /*static*/ |
1574 | int wxXmlResource::GetXRCID(const wxChar *str_id, int value_if_not_found) | |
13de23f6 | 1575 | { |
3b2a000e VS |
1576 | static bool s_stdIDsAdded = false; |
1577 | ||
1578 | if ( !s_stdIDsAdded ) | |
1579 | { | |
1580 | s_stdIDsAdded = true; | |
1581 | AddStdXRCID_Records(); | |
1582 | } | |
1583 | ||
9b2a7469 | 1584 | return XRCID_Lookup(str_id, value_if_not_found); |
13de23f6 VS |
1585 | } |
1586 | ||
78d14f80 | 1587 | |
5ed345b7 | 1588 | static void CleanXRCID_Record(XRCID_record *rec) |
78d14f80 VS |
1589 | { |
1590 | if (rec) | |
1591 | { | |
5ed345b7 | 1592 | CleanXRCID_Record(rec->next); |
00393283 | 1593 | free(rec->key); |
78d14f80 VS |
1594 | delete rec; |
1595 | } | |
1596 | } | |
1597 | ||
5ed345b7 | 1598 | static void CleanXRCID_Records() |
78d14f80 | 1599 | { |
5ed345b7 | 1600 | for (int i = 0; i < XRCID_TABLE_SIZE; i++) |
139c5871 | 1601 | { |
5ed345b7 | 1602 | CleanXRCID_Record(XRCID_Records[i]); |
139c5871 VS |
1603 | XRCID_Records[i] = NULL; |
1604 | } | |
78d14f80 VS |
1605 | } |
1606 | ||
13de23f6 VS |
1607 | static void AddStdXRCID_Records() |
1608 | { | |
1609 | #define stdID(id) XRCID_Lookup(wxT(#id), id) | |
1610 | stdID(-1); | |
c369d4f1 VS |
1611 | |
1612 | stdID(wxID_ANY); | |
1613 | stdID(wxID_SEPARATOR); | |
e7a3a5a5 | 1614 | |
c369d4f1 VS |
1615 | stdID(wxID_OPEN); |
1616 | stdID(wxID_CLOSE); | |
1617 | stdID(wxID_NEW); | |
1618 | stdID(wxID_SAVE); | |
1619 | stdID(wxID_SAVEAS); | |
1620 | stdID(wxID_REVERT); | |
1621 | stdID(wxID_EXIT); | |
1622 | stdID(wxID_UNDO); | |
1623 | stdID(wxID_REDO); | |
1624 | stdID(wxID_HELP); | |
1625 | stdID(wxID_PRINT); | |
1626 | stdID(wxID_PRINT_SETUP); | |
e63f19ba | 1627 | stdID(wxID_PAGE_SETUP); |
c369d4f1 VS |
1628 | stdID(wxID_PREVIEW); |
1629 | stdID(wxID_ABOUT); | |
1630 | stdID(wxID_HELP_CONTENTS); | |
1631 | stdID(wxID_HELP_COMMANDS); | |
1632 | stdID(wxID_HELP_PROCEDURES); | |
1633 | stdID(wxID_HELP_CONTEXT); | |
13de23f6 | 1634 | stdID(wxID_CLOSE_ALL); |
c369d4f1 VS |
1635 | stdID(wxID_PREFERENCES); |
1636 | stdID(wxID_CUT); | |
1637 | stdID(wxID_COPY); | |
1638 | stdID(wxID_PASTE); | |
1639 | stdID(wxID_CLEAR); | |
1640 | stdID(wxID_FIND); | |
1641 | stdID(wxID_DUPLICATE); | |
1642 | stdID(wxID_SELECTALL); | |
1643 | stdID(wxID_DELETE); | |
1644 | stdID(wxID_REPLACE); | |
1645 | stdID(wxID_REPLACE_ALL); | |
1646 | stdID(wxID_PROPERTIES); | |
1647 | stdID(wxID_VIEW_DETAILS); | |
1648 | stdID(wxID_VIEW_LARGEICONS); | |
1649 | stdID(wxID_VIEW_SMALLICONS); | |
1650 | stdID(wxID_VIEW_LIST); | |
1651 | stdID(wxID_VIEW_SORTDATE); | |
1652 | stdID(wxID_VIEW_SORTNAME); | |
1653 | stdID(wxID_VIEW_SORTSIZE); | |
1654 | stdID(wxID_VIEW_SORTTYPE); | |
1655 | stdID(wxID_FILE1); | |
1656 | stdID(wxID_FILE2); | |
1657 | stdID(wxID_FILE3); | |
1658 | stdID(wxID_FILE4); | |
1659 | stdID(wxID_FILE5); | |
1660 | stdID(wxID_FILE6); | |
1661 | stdID(wxID_FILE7); | |
1662 | stdID(wxID_FILE8); | |
1663 | stdID(wxID_FILE9); | |
1664 | stdID(wxID_OK); | |
1665 | stdID(wxID_CANCEL); | |
1666 | stdID(wxID_APPLY); | |
1667 | stdID(wxID_YES); | |
1668 | stdID(wxID_NO); | |
1669 | stdID(wxID_STATIC); | |
1670 | stdID(wxID_FORWARD); | |
1671 | stdID(wxID_BACKWARD); | |
1672 | stdID(wxID_DEFAULT); | |
1673 | stdID(wxID_MORE); | |
1674 | stdID(wxID_SETUP); | |
1675 | stdID(wxID_RESET); | |
1676 | stdID(wxID_CONTEXT_HELP); | |
1677 | stdID(wxID_YESTOALL); | |
1678 | stdID(wxID_NOTOALL); | |
1679 | stdID(wxID_ABORT); | |
1680 | stdID(wxID_RETRY); | |
1681 | stdID(wxID_IGNORE); | |
1682 | stdID(wxID_ADD); | |
1683 | stdID(wxID_REMOVE); | |
1684 | stdID(wxID_UP); | |
1685 | stdID(wxID_DOWN); | |
1686 | stdID(wxID_HOME); | |
1687 | stdID(wxID_REFRESH); | |
1688 | stdID(wxID_STOP); | |
1689 | stdID(wxID_INDEX); | |
1690 | stdID(wxID_BOLD); | |
1691 | stdID(wxID_ITALIC); | |
1692 | stdID(wxID_JUSTIFY_CENTER); | |
1693 | stdID(wxID_JUSTIFY_FILL); | |
1694 | stdID(wxID_JUSTIFY_RIGHT); | |
1695 | stdID(wxID_JUSTIFY_LEFT); | |
1696 | stdID(wxID_UNDERLINE); | |
1697 | stdID(wxID_INDENT); | |
1698 | stdID(wxID_UNINDENT); | |
1699 | stdID(wxID_ZOOM_100); | |
1700 | stdID(wxID_ZOOM_FIT); | |
1701 | stdID(wxID_ZOOM_IN); | |
1702 | stdID(wxID_ZOOM_OUT); | |
1703 | stdID(wxID_UNDELETE); | |
1704 | stdID(wxID_REVERT_TO_SAVED); | |
1705 | stdID(wxID_SYSTEM_MENU); | |
1706 | stdID(wxID_CLOSE_FRAME); | |
1707 | stdID(wxID_MOVE_FRAME); | |
1708 | stdID(wxID_RESIZE_FRAME); | |
1709 | stdID(wxID_MAXIMIZE_FRAME); | |
1710 | stdID(wxID_ICONIZE_FRAME); | |
1711 | stdID(wxID_RESTORE_FRAME); | |
1712 | ||
13de23f6 VS |
1713 | #undef stdID |
1714 | } | |
78d14f80 VS |
1715 | |
1716 | ||
1717 | ||
1718 | ||
1719 | ||
1720 | // --------------- module and globals ----------------------------- | |
1721 | ||
78d14f80 VS |
1722 | class wxXmlResourceModule: public wxModule |
1723 | { | |
1724 | DECLARE_DYNAMIC_CLASS(wxXmlResourceModule) | |
1725 | public: | |
1726 | wxXmlResourceModule() {} | |
824e8eaa VS |
1727 | bool OnInit() |
1728 | { | |
2b5f62a0 | 1729 | wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX); |
f80ea77b | 1730 | return true; |
824e8eaa | 1731 | } |
78d14f80 VS |
1732 | void OnExit() |
1733 | { | |
1542c42e | 1734 | delete wxXmlResource::Set(NULL); |
461932ae MB |
1735 | if(wxXmlResource::ms_subclassFactories) |
1736 | WX_CLEAR_LIST(wxXmlSubclassFactoriesList, *wxXmlResource::ms_subclassFactories); | |
2b5f62a0 | 1737 | wxDELETE(wxXmlResource::ms_subclassFactories); |
5ed345b7 | 1738 | CleanXRCID_Records(); |
78d14f80 VS |
1739 | } |
1740 | }; | |
1741 | ||
1742 | IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule) | |
1743 | ||
1744 | ||
1745 | // When wxXml is loaded dynamically after the application is already running | |
1746 | // then the built-in module system won't pick this one up. Add it manually. | |
1747 | void wxXmlInitResourceModule() | |
1748 | { | |
1749 | wxModule* module = new wxXmlResourceModule; | |
1750 | module->Init(); | |
1751 | wxModule::RegisterModule(module); | |
1752 | } | |
a1e4ec87 VS |
1753 | |
1754 | #endif // wxUSE_XRC |