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