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