]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xmlres.cpp
XRC assigns ID to wxStaticBox created by including wxStaticBoxSizer (did what patch...
[wxWidgets.git] / src / xrc / xmlres.cpp
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 #include "wx/dialog.h"
23 #include "wx/panel.h"
24 #include "wx/frame.h"
25 #include "wx/wfstream.h"
26 #include "wx/filesys.h"
27 #include "wx/log.h"
28 #include "wx/intl.h"
29 #include "wx/tokenzr.h"
30 #include "wx/fontenum.h"
31 #include "wx/module.h"
32 #include "wx/bitmap.h"
33 #include "wx/image.h"
34 #include "wx/fontmap.h"
35 #include "wx/artprov.h"
36
37 #include "wx/xrc/xml.h"
38 #include "wx/xrc/xmlres.h"
39
40 #include "wx/arrimpl.cpp"
41 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
42
43
44 wxXmlResource *wxXmlResource::ms_instance = NULL;
45
46 /*static*/ wxXmlResource *wxXmlResource::Get()
47 {
48 if ( !ms_instance )
49 ms_instance = new wxXmlResource;
50 return ms_instance;
51 }
52
53 /*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
54 {
55 wxXmlResource *old = ms_instance;
56 ms_instance = res;
57 return old;
58 }
59
60 wxXmlResource::wxXmlResource(int flags)
61 {
62 m_handlers.DeleteContents(TRUE);
63 m_flags = flags;
64 m_version = -1;
65 }
66
67 wxXmlResource::wxXmlResource(const wxString& filemask, int flags)
68 {
69 m_flags = flags;
70 m_version = -1;
71 m_handlers.DeleteContents(TRUE);
72 Load(filemask);
73 }
74
75 wxXmlResource::~wxXmlResource()
76 {
77 ClearHandlers();
78 }
79
80
81 bool wxXmlResource::Load(const wxString& filemask)
82 {
83 wxString fnd;
84 wxXmlResourceDataRecord *drec;
85 bool iswild = wxIsWild(filemask);
86 bool rt = TRUE;
87
88 #if wxUSE_FILESYSTEM
89 wxFileSystem fsys;
90 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
91 # define wxXmlFindNext fsys.FindNext()
92 #else
93 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
94 # define wxXmlFindNext wxFindNextFile()
95 #endif
96 if (iswild)
97 fnd = wxXmlFindFirst;
98 else
99 fnd = filemask;
100 while (!!fnd)
101 {
102 #if wxUSE_FILESYSTEM
103 if (filemask.Lower().Matches(wxT("*.zip")) ||
104 filemask.Lower().Matches(wxT("*.xrs")))
105 {
106 rt = rt && Load(fnd + wxT("#zip:*.xmlbin"));
107 rt = rt && Load(fnd + wxT("#zip:*.xrc"));
108 }
109 else
110 #endif
111 {
112 drec = new wxXmlResourceDataRecord;
113 drec->File = fnd;
114 m_data.Add(drec);
115 }
116
117 if (iswild)
118 fnd = wxXmlFindNext;
119 else
120 fnd = wxEmptyString;
121 }
122 # undef wxXmlFindFirst
123 # undef wxXmlFindNext
124 return rt;
125 }
126
127
128
129 void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
130 {
131 m_handlers.Append(handler);
132 handler->SetParentResource(this);
133 }
134
135 void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
136 {
137 m_handlers.Insert(handler);
138 handler->SetParentResource(this);
139 }
140
141
142
143 void wxXmlResource::ClearHandlers()
144 {
145 m_handlers.Clear();
146 }
147
148
149 wxMenu *wxXmlResource::LoadMenu(const wxString& name)
150 {
151 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
152 }
153
154
155
156 wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
157 {
158 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
159 }
160
161
162
163 #if wxUSE_TOOLBAR
164 wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
165 {
166 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
167 }
168 #endif
169
170
171 wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
172 {
173 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
174 }
175
176 bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
177 {
178 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
179 }
180
181
182
183 wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
184 {
185 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
186 }
187
188 bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
189 {
190 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
191 }
192
193 wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
194 {
195 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
196 }
197
198 bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
199 {
200 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
201 }
202
203 wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
204 {
205 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
206 FindResource(name, wxT("wxBitmap")), NULL, NULL);
207 wxBitmap rt;
208
209 if (bmp) { rt = *bmp; delete bmp; }
210 return rt;
211 }
212
213 wxIcon wxXmlResource::LoadIcon(const wxString& name)
214 {
215 wxIcon *icon = (wxIcon*)CreateResFromNode(
216 FindResource(name, wxT("wxIcon")), NULL, NULL);
217 wxIcon rt;
218
219 if (icon) { rt = *icon; delete icon; }
220 return rt;
221 }
222
223
224 wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname)
225 {
226 return CreateResFromNode(FindResource(name, classname), parent, NULL);
227 }
228
229 bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname)
230 {
231 return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;
232 }
233
234
235 bool wxXmlResource::AttachUnknownControl(const wxString& name,
236 wxWindow *control, wxWindow *parent)
237 {
238 if (parent == NULL)
239 parent = control->GetParent();
240 wxWindow *container = parent->FindWindow(name + wxT("_container"));
241 if (!container)
242 {
243 wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str());
244 return FALSE;
245 }
246 return control->Reparent(container);
247 }
248
249
250 static void ProcessPlatformProperty(wxXmlNode *node)
251 {
252 wxString s;
253 bool isok;
254
255 wxXmlNode *c = node->GetChildren();
256 while (c)
257 {
258 isok = FALSE;
259 if (!c->GetPropVal(wxT("platform"), &s))
260 isok = TRUE;
261 else
262 {
263 wxStringTokenizer tkn(s, wxT(" |"));
264
265 while (tkn.HasMoreTokens())
266 {
267 s = tkn.GetNextToken();
268 if (
269 #ifdef __WXMSW__
270 s == wxString(wxT("win"))
271 #elif defined(__UNIX__)
272 s == wxString(wxT("unix"))
273 #elif defined(__MAC__)
274 s == wxString(wxT("mac"))
275 #elif defined(__OS2__)
276 s == wxString(wxT("os2"))
277 #else
278 FALSE
279 #endif
280 ) isok = TRUE;
281 }
282 }
283
284 if (isok)
285 {
286 ProcessPlatformProperty(c);
287 c = c->GetNext();
288 }
289 else
290 {
291 wxXmlNode *c2 = c->GetNext();
292 node->RemoveChild(c);
293 delete c;
294 c = c2;
295 }
296 }
297 }
298
299
300
301 void wxXmlResource::UpdateResources()
302 {
303 bool modif;
304 # if wxUSE_FILESYSTEM
305 wxFSFile *file = NULL;
306 wxFileSystem fsys;
307 # endif
308
309 wxString encoding(wxT("UTF-8"));
310 #if !wxUSE_UNICODE && wxUSE_INTL
311 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
312 {
313 // In case we are not using wxLocale to translate strings, convert the strings
314 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
315 // it could break wxGetTranslation lookup.
316 encoding = wxLocale::GetSystemEncodingName();
317 }
318 #endif
319
320 for (size_t i = 0; i < m_data.GetCount(); i++)
321 {
322 modif = (m_data[i].Doc == NULL);
323
324 if (!modif)
325 {
326 # if wxUSE_FILESYSTEM
327 file = fsys.OpenFile(m_data[i].File);
328 modif = file && file->GetModificationTime() > m_data[i].Time;
329 if (!file)
330 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
331 wxDELETE(file);
332 # else
333 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
334 # endif
335 }
336
337 if (modif)
338 {
339 wxInputStream *stream = NULL;
340
341 # if wxUSE_FILESYSTEM
342 file = fsys.OpenFile(m_data[i].File);
343 if (file)
344 stream = file->GetStream();
345 # else
346 stream = new wxFileInputStream(m_data[i].File);
347 # endif
348
349 if (stream)
350 {
351 delete m_data[i].Doc;
352 m_data[i].Doc = new wxXmlDocument;
353 }
354 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
355 {
356 wxLogError(_("Cannot load resources from file '%s'."),
357 m_data[i].File.c_str());
358 wxDELETE(m_data[i].Doc);
359 }
360 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
361 {
362 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
363 wxDELETE(m_data[i].Doc);
364 }
365 else
366 {
367 long version;
368 int v1, v2, v3, v4;
369 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
370 wxT("version"), wxT("0.0.0.0"));
371 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
372 &v1, &v2, &v3, &v4) == 4)
373 version = v1*256*256*256+v2*256*256+v3*256+v4;
374 else
375 version = 0;
376 if (m_version == -1)
377 m_version = version;
378 if (m_version != version)
379 wxLogError(_("Resource files must have same version number!"));
380
381 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
382 m_data[i].Time = file->GetModificationTime();
383 }
384
385 # if wxUSE_FILESYSTEM
386 wxDELETE(file);
387 # else
388 wxDELETE(stream);
389 # endif
390 }
391 }
392 }
393
394
395 wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
396 const wxString& name,
397 const wxString& classname,
398 bool recursive)
399 {
400 wxString dummy;
401 wxXmlNode *node;
402
403 // first search for match at the top-level nodes (as this is
404 // where the resource is most commonly looked for):
405 for (node = parent->GetChildren(); node; node = node->GetNext())
406 {
407 if ( node->GetType() == wxXML_ELEMENT_NODE &&
408 (node->GetName() == wxT("object") ||
409 node->GetName() == wxT("object_ref")) &&
410 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
411 {
412 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
413 if (!classname || cls == classname)
414 return node;
415 // object_ref may not have 'class' property:
416 if (cls.empty() && node->GetName() == wxT("object_ref"))
417 {
418 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
419 if (refName.empty())
420 continue;
421 wxXmlNode* refNode = FindResource(refName, wxEmptyString, TRUE);
422 if (refNode &&
423 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
424 {
425 return node;
426 }
427 }
428 }
429 }
430
431 if ( recursive )
432 for (node = parent->GetChildren(); node; node = node->GetNext())
433 {
434 if ( node->GetType() == wxXML_ELEMENT_NODE &&
435 (node->GetName() == wxT("object") ||
436 node->GetName() == wxT("object_ref")) )
437 {
438 wxXmlNode* found = DoFindResource(node, name, classname, TRUE);
439 if ( found )
440 return found;
441 }
442 }
443
444 return NULL;
445 }
446
447 wxXmlNode *wxXmlResource::FindResource(const wxString& name,
448 const wxString& classname,
449 bool recursive)
450 {
451 UpdateResources(); //ensure everything is up-to-date
452
453 wxString dummy;
454 for (size_t f = 0; f < m_data.GetCount(); f++)
455 {
456 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
457 continue;
458
459 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
460 name, classname, recursive);
461 if ( found )
462 {
463 #if wxUSE_FILESYSTEM
464 m_curFileSystem.ChangePathTo(m_data[f].File);
465 #endif
466 return found;
467 }
468 }
469
470 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
471 name.c_str(), classname.c_str());
472 return NULL;
473 }
474
475 static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
476 {
477 // Merge properties:
478 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
479 {
480 wxXmlProperty *dprop;
481 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
482 {
483
484 if ( dprop->GetName() == prop->GetName() )
485 {
486 dprop->SetValue(prop->GetValue());
487 break;
488 }
489 }
490
491 if ( !dprop )
492 dest.AddProperty(prop->GetName(), prop->GetValue());
493 }
494
495 // Merge child nodes:
496 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
497 {
498 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
499 wxXmlNode *dnode;
500
501 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
502 {
503 if ( dnode->GetName() == node->GetName() &&
504 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
505 dnode->GetType() == node->GetType() )
506 {
507 MergeNodes(*dnode, *node);
508 break;
509 }
510 }
511
512 if ( !dnode )
513 dest.AddChild(new wxXmlNode(*node));
514 }
515
516 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
517 dest.SetContent(with.GetContent());
518 }
519
520 wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance)
521 {
522 if (node == NULL) return NULL;
523
524 // handling of referenced resource
525 if ( node->GetName() == wxT("object_ref") )
526 {
527 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
528 wxXmlNode* refNode = FindResource(refName, wxEmptyString, TRUE);
529
530 if ( !refNode )
531 {
532 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
533 refName.c_str());
534 return NULL;
535 }
536
537 wxXmlNode copy(*refNode);
538 MergeNodes(copy, *node);
539
540 return CreateResFromNode(&copy, parent, instance);
541 }
542
543 wxXmlResourceHandler *handler;
544 wxObject *ret;
545 wxNode * ND = m_handlers.GetFirst();
546 while (ND)
547 {
548 handler = (wxXmlResourceHandler*)ND->GetData();
549 if (node->GetName() == wxT("object") && handler->CanHandle(node))
550 {
551 ret = handler->CreateResource(node, parent, instance);
552 if (ret) return ret;
553 }
554 ND = ND->GetNext();
555 }
556
557 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
558 node->GetName().c_str(),
559 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
560 return NULL;
561 }
562
563
564 #include "wx/listimpl.cpp"
565 WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
566 WX_DEFINE_LIST(wxXmlSubclassFactoriesList);
567
568 wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
569
570 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
571 {
572 if (!ms_subclassFactories)
573 {
574 ms_subclassFactories = new wxXmlSubclassFactoriesList;
575 ms_subclassFactories->DeleteContents(TRUE);
576 }
577 ms_subclassFactories->Append(factory);
578 }
579
580 class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
581 {
582 public:
583 ~wxXmlSubclassFactoryCXX() {}
584
585 wxObject *Create(const wxString& className)
586 {
587 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
588
589 if (classInfo)
590 return classInfo->CreateObject();
591 else
592 return NULL;
593 }
594 };
595
596
597
598
599
600 wxXmlResourceHandler::wxXmlResourceHandler()
601 : m_node(NULL), m_parent(NULL), m_instance(NULL),
602 m_parentAsWindow(NULL), m_instanceAsWindow(NULL)
603 {}
604
605
606
607 wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
608 {
609 wxXmlNode *myNode = m_node;
610 wxString myClass = m_class;
611 wxObject *myParent = m_parent, *myInstance = m_instance;
612 wxWindow *myParentAW = m_parentAsWindow, *myInstanceAW = m_instanceAsWindow;
613
614 m_instance = instance;
615 if (!m_instance && node->HasProp(wxT("subclass")) &&
616 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
617 {
618 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
619 if (!subclass.empty())
620 {
621 for (wxXmlSubclassFactoriesList::Node *i = wxXmlResource::ms_subclassFactories->GetFirst();
622 i; i = i->GetNext())
623 {
624 m_instance = i->GetData()->Create(subclass);
625 if (m_instance)
626 break;
627 }
628
629 if (!m_instance)
630 {
631 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
632 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
633 subclass.c_str(), name.c_str());
634 }
635 }
636 }
637
638 m_node = node;
639 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
640 m_parent = parent;
641 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
642 m_instanceAsWindow = wxDynamicCast(m_instance, wxWindow);
643
644 wxObject *returned = DoCreateResource();
645
646 m_node = myNode;
647 m_class = myClass;
648 m_parent = myParent; m_parentAsWindow = myParentAW;
649 m_instance = myInstance; m_instanceAsWindow = myInstanceAW;
650
651 return returned;
652 }
653
654
655 void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
656 {
657 m_styleNames.Add(name);
658 m_styleValues.Add(value);
659 }
660
661
662
663 void wxXmlResourceHandler::AddWindowStyles()
664 {
665 XRC_ADD_STYLE(wxSIMPLE_BORDER);
666 XRC_ADD_STYLE(wxSUNKEN_BORDER);
667 XRC_ADD_STYLE(wxDOUBLE_BORDER);
668 XRC_ADD_STYLE(wxRAISED_BORDER);
669 XRC_ADD_STYLE(wxSTATIC_BORDER);
670 XRC_ADD_STYLE(wxNO_BORDER);
671 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
672 XRC_ADD_STYLE(wxWANTS_CHARS);
673 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
674 }
675
676
677
678 bool wxXmlResourceHandler::HasParam(const wxString& param)
679 {
680 return (GetParamNode(param) != NULL);
681 }
682
683
684 int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
685 {
686 wxString s = GetParamValue(param);
687
688 if (!s) return defaults;
689
690 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
691 int style = 0;
692 int index;
693 wxString fl;
694 while (tkn.HasMoreTokens())
695 {
696 fl = tkn.GetNextToken();
697 index = m_styleNames.Index(fl);
698 if (index != wxNOT_FOUND)
699 style |= m_styleValues[index];
700 else
701 wxLogError(_("Unknown style flag ") + fl);
702 }
703 return style;
704 }
705
706
707
708 wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
709 {
710 wxString str1(GetParamValue(param));
711 wxString str2;
712 const wxChar *dt;
713 wxChar amp_char;
714
715 // VS: First version of XRC resources used $ instead of & (which is
716 // illegal in XML), but later I realized that '_' fits this purpose
717 // much better (because &File means "File with F underlined").
718 if (m_resource->CompareVersion(2,3,0,1) < 0)
719 amp_char = wxT('$');
720 else
721 amp_char = wxT('_');
722
723 for (dt = str1.c_str(); *dt; dt++)
724 {
725 // Remap amp_char to &, map double amp_char to amp_char (for things
726 // like "&File..." -- this is illegal in XML, so we use "_File..."):
727 if (*dt == amp_char)
728 {
729 if ( *(++dt) == amp_char )
730 str2 << amp_char;
731 else
732 str2 << wxT('&') << *dt;
733 }
734 // Remap \n to CR, \r to LF, \t to TAB:
735 else if (*dt == wxT('\\'))
736 switch (*(++dt))
737 {
738 case wxT('n') : str2 << wxT('\n'); break;
739 case wxT('t') : str2 << wxT('\t'); break;
740 case wxT('r') : str2 << wxT('\r'); break;
741 default : str2 << wxT('\\') << *dt; break;
742 }
743 else str2 << *dt;
744 }
745
746 if (translate && m_resource->GetFlags() & wxXRC_USE_LOCALE)
747 return wxGetTranslation(str2);
748 else
749 return str2;
750
751 }
752
753
754
755 long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
756 {
757 long value;
758 wxString str1 = GetParamValue(param);
759
760 if (!str1.ToLong(&value))
761 value = defaultv;
762
763 return value;
764 }
765
766
767
768 int wxXmlResourceHandler::GetID()
769 {
770 wxString sid = GetName();
771 long num;
772
773 if (sid == wxT("-1")) return -1;
774 else if (sid.IsNumber() && sid.ToLong(&num)) return num;
775 #define stdID(id) else if (sid == wxT(#id)) return id
776 stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
777 stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
778 stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
779 stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
780 stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
781 stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
782 stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
783 stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
784 stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
785 stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
786 stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
787 stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
788 stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
789 stdID(wxID_CLOSE_ALL);
790 #undef stdID
791 else return wxXmlResource::GetXRCID(sid);
792 }
793
794
795
796 wxString wxXmlResourceHandler::GetName()
797 {
798 return m_node->GetPropVal(wxT("name"), wxT("-1"));
799 }
800
801
802
803 bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
804 {
805 wxString v = GetParamValue(param);
806 v.MakeLower();
807 if (!v) return defaultv;
808 else return (v == wxT("1"));
809 }
810
811
812
813 wxColour wxXmlResourceHandler::GetColour(const wxString& param)
814 {
815 wxString v = GetParamValue(param);
816 unsigned long tmp = 0;
817
818 if (v.Length() != 7 || v[0u] != wxT('#') ||
819 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
820 {
821 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
822 v.c_str(), param.c_str());
823 return wxNullColour;
824 }
825
826 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
827 (unsigned char) ((tmp & 0x00FF00) >> 8),
828 (unsigned char) ((tmp & 0x0000FF)));
829 }
830
831
832
833 wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
834 const wxArtClient& defaultArtClient,
835 wxSize size)
836 {
837 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
838 wxXmlNode *bmpNode = GetParamNode(param);
839 if ( bmpNode )
840 {
841 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
842 if ( !sid.empty() )
843 {
844 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), defaultArtClient);
845 wxBitmap stockArt =
846 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
847 wxART_MAKE_CLIENT_ID_FROM_STR(scl),
848 size);
849 if ( stockArt.Ok() )
850 return stockArt;
851 }
852 }
853
854 /* ...or load the bitmap from file: */
855 wxString name = GetParamValue(param);
856 if (name.IsEmpty()) return wxNullBitmap;
857 #if wxUSE_FILESYSTEM
858 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
859 if (fsfile == NULL)
860 {
861 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
862 return wxNullBitmap;
863 }
864 wxImage img(*(fsfile->GetStream()));
865 delete fsfile;
866 #else
867 wxImage img(GetParamValue(wxT("bitmap")));
868 #endif
869
870 if (!img.Ok())
871 {
872 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
873 return wxNullBitmap;
874 }
875 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
876 return wxBitmap(img);
877
878 }
879
880
881
882 wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
883 const wxArtClient& defaultArtClient,
884 wxSize size)
885 {
886 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
887 wxIcon icon;
888 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
889 #else
890 wxIcon *iconpt;
891 wxBitmap bmppt = GetBitmap(param, size);
892 iconpt = (wxIcon*)(&bmppt);
893 wxIcon icon(*iconpt);
894 #endif
895 return icon;
896 }
897
898
899
900 wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
901 {
902 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
903
904 wxXmlNode *n = m_node->GetChildren();
905
906 while (n)
907 {
908 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
909 return n;
910 n = n->GetNext();
911 }
912 return NULL;
913 }
914
915
916 wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
917 {
918 wxXmlNode *n = node;
919 if (n == NULL) return wxEmptyString;
920 n = n->GetChildren();
921
922 while (n)
923 {
924 if (n->GetType() == wxXML_TEXT_NODE ||
925 n->GetType() == wxXML_CDATA_SECTION_NODE)
926 return n->GetContent();
927 n = n->GetNext();
928 }
929 return wxEmptyString;
930 }
931
932
933
934 wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
935 {
936 if (param.IsEmpty())
937 return GetNodeContent(m_node);
938 else
939 return GetNodeContent(GetParamNode(param));
940 }
941
942
943
944 wxSize wxXmlResourceHandler::GetSize(const wxString& param)
945 {
946 wxString s = GetParamValue(param);
947 if (s.IsEmpty()) s = wxT("-1,-1");
948 bool is_dlg;
949 long sx, sy;
950
951 is_dlg = s[s.Length()-1] == wxT('d');
952 if (is_dlg) s.RemoveLast();
953
954 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
955 !s.AfterLast(wxT(',')).ToLong(&sy))
956 {
957 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
958 return wxDefaultSize;
959 }
960
961 if (is_dlg)
962 {
963 if (m_instanceAsWindow)
964 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
965 else if (m_parentAsWindow)
966 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
967 else
968 {
969 wxLogError(_("Cannot convert dialog units: dialog unknown."));
970 return wxDefaultSize;
971 }
972 }
973 else return wxSize(sx, sy);
974 }
975
976
977
978 wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
979 {
980 wxSize sz = GetSize(param);
981 return wxPoint(sz.x, sz.y);
982 }
983
984
985
986 wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
987 {
988 wxString s = GetParamValue(param);
989 if (s.IsEmpty()) return defaultv;
990 bool is_dlg;
991 long sx;
992
993 is_dlg = s[s.Length()-1] == wxT('d');
994 if (is_dlg) s.RemoveLast();
995
996 if (!s.ToLong(&sx))
997 {
998 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
999 return defaultv;
1000 }
1001
1002 if (is_dlg)
1003 {
1004 if (m_instanceAsWindow)
1005 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
1006 else if (m_parentAsWindow)
1007 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
1008 else
1009 {
1010 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1011 return defaultv;
1012 }
1013 }
1014 else return sx;
1015 }
1016
1017
1018
1019 wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1020 {
1021 wxXmlNode *font_node = GetParamNode(param);
1022 if (font_node == NULL)
1023 {
1024 wxLogError(_("Cannot find font node '%s'."), param.c_str());
1025 return wxNullFont;
1026 }
1027
1028 wxXmlNode *oldnode = m_node;
1029 m_node = font_node;
1030
1031 long size = GetLong(wxT("size"), 12);
1032
1033 wxString style = GetParamValue(wxT("style"));
1034 wxString weight = GetParamValue(wxT("weight"));
1035 int istyle = wxNORMAL, iweight = wxNORMAL;
1036 if (style == wxT("italic")) istyle = wxITALIC;
1037 else if (style == wxT("slant")) istyle = wxSLANT;
1038 if (weight == wxT("bold")) iweight = wxBOLD;
1039 else if (weight == wxT("light")) iweight = wxLIGHT;
1040
1041 wxString family = GetParamValue(wxT("family"));
1042 int ifamily = wxDEFAULT;
1043 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1044 else if (family == wxT("roman")) ifamily = wxROMAN;
1045 else if (family == wxT("script")) ifamily = wxSCRIPT;
1046 else if (family == wxT("swiss")) ifamily = wxSWISS;
1047 else if (family == wxT("modern")) ifamily = wxMODERN;
1048
1049 bool underlined = GetBool(wxT("underlined"), FALSE);
1050
1051 wxString encoding = GetParamValue(wxT("encoding"));
1052 wxFontMapper mapper;
1053 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1054 if (!encoding.IsEmpty())
1055 enc = mapper.CharsetToEncoding(encoding);
1056 if (enc == wxFONTENCODING_SYSTEM)
1057 enc = wxFONTENCODING_DEFAULT;
1058
1059 wxString faces = GetParamValue(wxT("face"));
1060 wxString facename = wxEmptyString;
1061 wxFontEnumerator enu;
1062 enu.EnumerateFacenames();
1063 wxStringTokenizer tk(faces, wxT(","));
1064 while (tk.HasMoreTokens())
1065 {
1066 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
1067 if (index != wxNOT_FOUND)
1068 {
1069 facename = (*enu.GetFacenames())[index];
1070 break;
1071 }
1072 }
1073
1074 m_node = oldnode;
1075
1076 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
1077 return font;
1078 }
1079
1080
1081 void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1082 {
1083 //FIXME : add cursor
1084
1085 if (HasParam(wxT("exstyle")))
1086 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
1087 if (HasParam(wxT("bg")))
1088 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1089 if (HasParam(wxT("fg")))
1090 wnd->SetForegroundColour(GetColour(wxT("fg")));
1091 if (GetBool(wxT("enabled"), 1) == 0)
1092 wnd->Enable(FALSE);
1093 if (GetBool(wxT("focused"), 0) == 1)
1094 wnd->SetFocus();
1095 if (GetBool(wxT("hidden"), 0) == 1)
1096 wnd->Show(FALSE);
1097 #if wxUSE_TOOLTIPS
1098 if (HasParam(wxT("tooltip")))
1099 wnd->SetToolTip(GetText(wxT("tooltip")));
1100 #endif
1101 if (HasParam(wxT("font")))
1102 wnd->SetFont(GetFont());
1103 }
1104
1105
1106 void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1107 {
1108 wxXmlNode *n = m_node->GetChildren();
1109
1110 while (n)
1111 {
1112 if (n->GetType() == wxXML_ELEMENT_NODE &&
1113 n->GetName() == wxT("object"))
1114 {
1115 if (this_hnd_only && CanHandle(n))
1116 CreateResource(n, parent, NULL);
1117 else
1118 m_resource->CreateResFromNode(n, parent, NULL);
1119 }
1120 n = n->GetNext();
1121 }
1122 }
1123
1124
1125 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1126 {
1127 wxXmlNode *root;
1128 if (rootnode == NULL) root = m_node; else root = rootnode;
1129 wxXmlNode *n = root->GetChildren();
1130
1131 while (n)
1132 {
1133 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1134 {
1135 CreateResource(n, parent, NULL);
1136 }
1137 n = n->GetNext();
1138 }
1139 }
1140
1141
1142
1143
1144
1145
1146
1147 // --------------- XRCID implementation -----------------------------
1148
1149 #define XRCID_TABLE_SIZE 1024
1150
1151
1152 struct XRCID_record
1153 {
1154 int id;
1155 wxChar *key;
1156 XRCID_record *next;
1157 };
1158
1159 static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
1160
1161 /*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1162 {
1163 static int XRCID_LastID = wxID_HIGHEST;
1164
1165 int index = 0;
1166
1167 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
1168 index %= XRCID_TABLE_SIZE;
1169
1170 XRCID_record *oldrec = NULL;
1171 int matchcnt = 0;
1172 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
1173 {
1174 if (wxStrcmp(rec->key, str_id) == 0)
1175 {
1176 return rec->id;
1177 }
1178 matchcnt++;
1179 oldrec = rec;
1180 }
1181
1182 XRCID_record **rec_var = (oldrec == NULL) ?
1183 &XRCID_Records[index] : &oldrec->next;
1184 *rec_var = new XRCID_record;
1185 (*rec_var)->id = ++XRCID_LastID;
1186 (*rec_var)->key = wxStrdup(str_id);
1187 (*rec_var)->next = NULL;
1188
1189 return (*rec_var)->id;
1190 }
1191
1192
1193 static void CleanXRCID_Record(XRCID_record *rec)
1194 {
1195 if (rec)
1196 {
1197 CleanXRCID_Record(rec->next);
1198 free(rec->key);
1199 delete rec;
1200 }
1201 }
1202
1203 static void CleanXRCID_Records()
1204 {
1205 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
1206 CleanXRCID_Record(XRCID_Records[i]);
1207 }
1208
1209
1210
1211
1212
1213
1214
1215
1216 // --------------- module and globals -----------------------------
1217
1218 class wxXmlResourceModule: public wxModule
1219 {
1220 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1221 public:
1222 wxXmlResourceModule() {}
1223 bool OnInit()
1224 {
1225 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
1226 return TRUE;
1227 }
1228 void OnExit()
1229 {
1230 delete wxXmlResource::Set(NULL);
1231 wxDELETE(wxXmlResource::ms_subclassFactories);
1232 CleanXRCID_Records();
1233 }
1234 };
1235
1236 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1237
1238
1239 // When wxXml is loaded dynamically after the application is already running
1240 // then the built-in module system won't pick this one up. Add it manually.
1241 void wxXmlInitResourceModule()
1242 {
1243 wxModule* module = new wxXmlResourceModule;
1244 module->Init();
1245 wxModule::RegisterModule(module);
1246 }
1247
1248