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