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