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