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