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