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