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