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