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