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