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