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