]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xmlres.cpp
Committed forgotten files
[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 modif = file && file->GetModificationTime() > m_data[i].Time;
415 if (!file)
416 {
417 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
418 rt = false;
419 }
420 wxDELETE(file);
421 wxUnusedVar(file);
422 # else
423 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
424 # endif
425 }
426
427 if (modif)
428 {
429 wxLogTrace(_T("xrc"),
430 _T("opening file '%s'"), m_data[i].File.c_str());
431
432 wxInputStream *stream = NULL;
433
434 # if wxUSE_FILESYSTEM
435 file = fsys.OpenFile(m_data[i].File);
436 if (file)
437 stream = file->GetStream();
438 # else
439 stream = new wxFileInputStream(m_data[i].File);
440 # endif
441
442 if (stream)
443 {
444 delete m_data[i].Doc;
445 m_data[i].Doc = new wxXmlDocument;
446 }
447 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
448 {
449 wxLogError(_("Cannot load resources from file '%s'."),
450 m_data[i].File.c_str());
451 wxDELETE(m_data[i].Doc);
452 rt = false;
453 }
454 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
455 {
456 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
457 wxDELETE(m_data[i].Doc);
458 rt = false;
459 }
460 else
461 {
462 long version;
463 int v1, v2, v3, v4;
464 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
465 wxT("version"), wxT("0.0.0.0"));
466 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
467 &v1, &v2, &v3, &v4) == 4)
468 version = v1*256*256*256+v2*256*256+v3*256+v4;
469 else
470 version = 0;
471 if (m_version == -1)
472 m_version = version;
473 if (m_version != version)
474 {
475 wxLogError(_("Resource files must have same version number!"));
476 rt = false;
477 }
478
479 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
480 #if wxUSE_FILESYSTEM
481 m_data[i].Time = file->GetModificationTime();
482 #else
483 m_data[i].Time = wxDateTime(wxFileModificationTime(m_data[i].File));
484 #endif
485 }
486
487 # if wxUSE_FILESYSTEM
488 wxDELETE(file);
489 wxUnusedVar(file);
490 # else
491 wxDELETE(stream);
492 # endif
493 }
494 }
495
496 return rt;
497 }
498
499
500 wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
501 const wxString& name,
502 const wxString& classname,
503 bool recursive)
504 {
505 wxString dummy;
506 wxXmlNode *node;
507
508 // first search for match at the top-level nodes (as this is
509 // where the resource is most commonly looked for):
510 for (node = parent->GetChildren(); node; node = node->GetNext())
511 {
512 if ( node->GetType() == wxXML_ELEMENT_NODE &&
513 (node->GetName() == wxT("object") ||
514 node->GetName() == wxT("object_ref")) &&
515 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
516 {
517 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
518 if (!classname || cls == classname)
519 return node;
520 // object_ref may not have 'class' property:
521 if (cls.empty() && node->GetName() == wxT("object_ref"))
522 {
523 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
524 if (refName.empty())
525 continue;
526 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
527 if (refNode &&
528 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
529 {
530 return node;
531 }
532 }
533 }
534 }
535
536 if ( recursive )
537 for (node = parent->GetChildren(); node; node = node->GetNext())
538 {
539 if ( node->GetType() == wxXML_ELEMENT_NODE &&
540 (node->GetName() == wxT("object") ||
541 node->GetName() == wxT("object_ref")) )
542 {
543 wxXmlNode* found = DoFindResource(node, name, classname, true);
544 if ( found )
545 return found;
546 }
547 }
548
549 return NULL;
550 }
551
552 wxXmlNode *wxXmlResource::FindResource(const wxString& name,
553 const wxString& classname,
554 bool recursive)
555 {
556 UpdateResources(); //ensure everything is up-to-date
557
558 wxString dummy;
559 for (size_t f = 0; f < m_data.GetCount(); f++)
560 {
561 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
562 continue;
563
564 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
565 name, classname, recursive);
566 if ( found )
567 {
568 #if wxUSE_FILESYSTEM
569 m_curFileSystem.ChangePathTo(m_data[f].File);
570 #endif
571 return found;
572 }
573 }
574
575 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
576 name.c_str(), classname.c_str());
577 return NULL;
578 }
579
580 static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
581 {
582 // Merge properties:
583 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
584 {
585 wxXmlProperty *dprop;
586 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
587 {
588
589 if ( dprop->GetName() == prop->GetName() )
590 {
591 dprop->SetValue(prop->GetValue());
592 break;
593 }
594 }
595
596 if ( !dprop )
597 dest.AddProperty(prop->GetName(), prop->GetValue());
598 }
599
600 // Merge child nodes:
601 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
602 {
603 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
604 wxXmlNode *dnode;
605
606 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
607 {
608 if ( dnode->GetName() == node->GetName() &&
609 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
610 dnode->GetType() == node->GetType() )
611 {
612 MergeNodes(*dnode, *node);
613 break;
614 }
615 }
616
617 if ( !dnode )
618 dest.AddChild(new wxXmlNode(*node));
619 }
620
621 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
622 dest.SetContent(with.GetContent());
623 }
624
625 wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
626 wxObject *instance,
627 wxXmlResourceHandler *handlerToUse)
628 {
629 if (node == NULL) return NULL;
630
631 // handling of referenced resource
632 if ( node->GetName() == wxT("object_ref") )
633 {
634 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
635 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
636
637 if ( !refNode )
638 {
639 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
640 refName.c_str());
641 return NULL;
642 }
643
644 wxXmlNode copy(*refNode);
645 MergeNodes(copy, *node);
646
647 return CreateResFromNode(&copy, parent, instance);
648 }
649
650 wxXmlResourceHandler *handler;
651
652 if (handlerToUse)
653 {
654 if (handlerToUse->CanHandle(node))
655 {
656 return handlerToUse->CreateResource(node, parent, instance);
657 }
658 }
659 else if (node->GetName() == wxT("object"))
660 {
661 wxList::compatibility_iterator ND = m_handlers.GetFirst();
662 while (ND)
663 {
664 handler = (wxXmlResourceHandler*)ND->GetData();
665 if (handler->CanHandle(node))
666 {
667 return handler->CreateResource(node, parent, instance);
668 }
669 ND = ND->GetNext();
670 }
671 }
672
673 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
674 node->GetName().c_str(),
675 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
676 return NULL;
677 }
678
679
680 #include "wx/listimpl.cpp"
681 WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
682 WX_DEFINE_LIST(wxXmlSubclassFactoriesList)
683
684 wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
685
686 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
687 {
688 if (!ms_subclassFactories)
689 {
690 ms_subclassFactories = new wxXmlSubclassFactoriesList;
691 }
692 ms_subclassFactories->Append(factory);
693 }
694
695 class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
696 {
697 public:
698 ~wxXmlSubclassFactoryCXX() {}
699
700 wxObject *Create(const wxString& className)
701 {
702 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
703
704 if (classInfo)
705 return classInfo->CreateObject();
706 else
707 return NULL;
708 }
709 };
710
711
712
713
714 wxXmlResourceHandler::wxXmlResourceHandler()
715 : m_node(NULL), m_parent(NULL), m_instance(NULL),
716 m_parentAsWindow(NULL)
717 {}
718
719
720
721 wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
722 {
723 wxXmlNode *myNode = m_node;
724 wxString myClass = m_class;
725 wxObject *myParent = m_parent, *myInstance = m_instance;
726 wxWindow *myParentAW = m_parentAsWindow;
727
728 m_instance = instance;
729 if (!m_instance && node->HasProp(wxT("subclass")) &&
730 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
731 {
732 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
733 if (!subclass.empty())
734 {
735 for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst();
736 i; i = i->GetNext())
737 {
738 m_instance = i->GetData()->Create(subclass);
739 if (m_instance)
740 break;
741 }
742
743 if (!m_instance)
744 {
745 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
746 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
747 subclass.c_str(), name.c_str());
748 }
749 }
750 }
751
752 m_node = node;
753 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
754 m_parent = parent;
755 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
756
757 wxObject *returned = DoCreateResource();
758
759 m_node = myNode;
760 m_class = myClass;
761 m_parent = myParent; m_parentAsWindow = myParentAW;
762 m_instance = myInstance;
763
764 return returned;
765 }
766
767
768 void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
769 {
770 m_styleNames.Add(name);
771 m_styleValues.Add(value);
772 }
773
774
775
776 void wxXmlResourceHandler::AddWindowStyles()
777 {
778 XRC_ADD_STYLE(wxCLIP_CHILDREN);
779 XRC_ADD_STYLE(wxSIMPLE_BORDER);
780 XRC_ADD_STYLE(wxSUNKEN_BORDER);
781 XRC_ADD_STYLE(wxDOUBLE_BORDER);
782 XRC_ADD_STYLE(wxRAISED_BORDER);
783 XRC_ADD_STYLE(wxSTATIC_BORDER);
784 XRC_ADD_STYLE(wxNO_BORDER);
785 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
786 XRC_ADD_STYLE(wxWANTS_CHARS);
787 XRC_ADD_STYLE(wxTAB_TRAVERSAL);
788 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
789 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
790 XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
791 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
792 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
793 }
794
795
796
797 bool wxXmlResourceHandler::HasParam(const wxString& param)
798 {
799 return (GetParamNode(param) != NULL);
800 }
801
802
803 int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
804 {
805 wxString s = GetParamValue(param);
806
807 if (!s) return defaults;
808
809 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
810 int style = 0;
811 int index;
812 wxString fl;
813 while (tkn.HasMoreTokens())
814 {
815 fl = tkn.GetNextToken();
816 index = m_styleNames.Index(fl);
817 if (index != wxNOT_FOUND)
818 style |= m_styleValues[index];
819 else
820 wxLogError(_("Unknown style flag ") + fl);
821 }
822 return style;
823 }
824
825
826
827 wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
828 {
829 wxXmlNode *parNode = GetParamNode(param);
830 wxString str1(GetNodeContent(parNode));
831 wxString str2;
832 const wxChar *dt;
833 wxChar amp_char;
834
835 // VS: First version of XRC resources used $ instead of & (which is
836 // illegal in XML), but later I realized that '_' fits this purpose
837 // much better (because &File means "File with F underlined").
838 if (m_resource->CompareVersion(2,3,0,1) < 0)
839 amp_char = wxT('$');
840 else
841 amp_char = wxT('_');
842
843 for (dt = str1.c_str(); *dt; dt++)
844 {
845 // Remap amp_char to &, map double amp_char to amp_char (for things
846 // like "&File..." -- this is illegal in XML, so we use "_File..."):
847 if (*dt == amp_char)
848 {
849 if ( *(++dt) == amp_char )
850 str2 << amp_char;
851 else
852 str2 << wxT('&') << *dt;
853 }
854 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
855 else if (*dt == wxT('\\'))
856 switch (*(++dt))
857 {
858 case wxT('n'):
859 str2 << wxT('\n');
860 break;
861
862 case wxT('t'):
863 str2 << wxT('\t');
864 break;
865
866 case wxT('r'):
867 str2 << wxT('\r');
868 break;
869
870 case wxT('\\') :
871 // "\\" wasn't translated to "\" prior to 2.5.3.0:
872 if (m_resource->CompareVersion(2,5,3,0) >= 0)
873 {
874 str2 << wxT('\\');
875 break;
876 }
877 // else fall-through to default: branch below
878
879 default:
880 str2 << wxT('\\') << *dt;
881 break;
882 }
883 else str2 << *dt;
884 }
885
886 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
887 {
888 if (translate && parNode &&
889 parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0"))
890 {
891 return wxGetTranslation(str2);
892 }
893 else
894 {
895 #if wxUSE_UNICODE
896 return str2;
897 #else
898 // The string is internally stored as UTF-8, we have to convert
899 // it into system's default encoding so that it can be displayed:
900 return wxString(str2.mb_str(wxConvUTF8), wxConvLocal);
901 #endif
902 }
903 }
904
905 // If wxXRC_USE_LOCALE is not set, then the string is already in
906 // system's default encoding in ANSI build, so we don't have to
907 // do anything special here.
908 return str2;
909 }
910
911
912
913 long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
914 {
915 long value;
916 wxString str1 = GetParamValue(param);
917
918 if (!str1.ToLong(&value))
919 value = defaultv;
920
921 return value;
922 }
923
924 float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv)
925 {
926 double value;
927 wxString str1 = GetParamValue(param);
928
929 #ifndef __WXWINCE__
930 const char *prevlocale = setlocale(LC_NUMERIC, "C");
931 #endif
932
933 if (!str1.ToDouble(&value))
934 value = defaultv;
935
936 #ifndef __WXWINCE__
937 setlocale(LC_NUMERIC, prevlocale);
938 #endif
939
940 return wx_truncate_cast(float, value);
941 }
942
943
944 int wxXmlResourceHandler::GetID()
945 {
946 return wxXmlResource::GetXRCID(GetName());
947 }
948
949
950
951 wxString wxXmlResourceHandler::GetName()
952 {
953 return m_node->GetPropVal(wxT("name"), wxT("-1"));
954 }
955
956
957
958 bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
959 {
960 wxString v = GetParamValue(param);
961 v.MakeLower();
962 if (!v) return defaultv;
963
964 return (v == wxT("1"));
965 }
966
967
968 static wxColour GetSystemColour(const wxString& name)
969 {
970 if (!name.empty())
971 {
972 #define SYSCLR(clr) \
973 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
974 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
975 SYSCLR(wxSYS_COLOUR_BACKGROUND)
976 SYSCLR(wxSYS_COLOUR_DESKTOP)
977 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
978 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
979 SYSCLR(wxSYS_COLOUR_MENU)
980 SYSCLR(wxSYS_COLOUR_WINDOW)
981 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
982 SYSCLR(wxSYS_COLOUR_MENUTEXT)
983 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
984 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
985 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
986 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
987 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
988 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
989 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
990 SYSCLR(wxSYS_COLOUR_BTNFACE)
991 SYSCLR(wxSYS_COLOUR_3DFACE)
992 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
993 SYSCLR(wxSYS_COLOUR_3DSHADOW)
994 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
995 SYSCLR(wxSYS_COLOUR_BTNTEXT)
996 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
997 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
998 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
999 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1000 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1001 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1002 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1003 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1004 SYSCLR(wxSYS_COLOUR_INFOBK)
1005 SYSCLR(wxSYS_COLOUR_LISTBOX)
1006 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1007 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1008 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1009 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1010 SYSCLR(wxSYS_COLOUR_MENUBAR)
1011 #undef SYSCLR
1012 }
1013
1014 return wxNullColour;
1015 }
1016
1017 wxColour wxXmlResourceHandler::GetColour(const wxString& param)
1018 {
1019 wxString v = GetParamValue(param);
1020
1021 // find colour using HTML syntax (#RRGGBB)
1022 unsigned long tmp = 0;
1023
1024 if (v.Length() != 7 || v[0u] != wxT('#') ||
1025 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
1026 {
1027 // the colour doesn't use #RRGGBB format, check if it is symbolic
1028 // colour name:
1029 wxColour clr = GetSystemColour(v);
1030 if (clr.Ok())
1031 return clr;
1032
1033 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
1034 v.c_str(), param.c_str());
1035 return wxNullColour;
1036 }
1037
1038 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
1039 (unsigned char) ((tmp & 0x00FF00) >> 8),
1040 (unsigned char) ((tmp & 0x0000FF)));
1041 }
1042
1043
1044
1045 wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
1046 const wxArtClient& defaultArtClient,
1047 wxSize size)
1048 {
1049 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1050 wxXmlNode *bmpNode = GetParamNode(param);
1051 if ( bmpNode )
1052 {
1053 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
1054 if ( !sid.empty() )
1055 {
1056 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), wxEmptyString);
1057 if (scl.empty())
1058 scl = defaultArtClient;
1059 else
1060 scl = wxART_MAKE_CLIENT_ID_FROM_STR(scl);
1061
1062 wxBitmap stockArt =
1063 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
1064 scl, size);
1065 if ( stockArt.Ok() )
1066 return stockArt;
1067 }
1068 }
1069
1070 /* ...or load the bitmap from file: */
1071 wxString name = GetParamValue(param);
1072 if (name.empty()) return wxNullBitmap;
1073 #if wxUSE_FILESYSTEM
1074 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
1075 if (fsfile == NULL)
1076 {
1077 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1078 name.c_str());
1079 return wxNullBitmap;
1080 }
1081 wxImage img(*(fsfile->GetStream()));
1082 delete fsfile;
1083 #else
1084 wxImage img(GetParamValue(wxT("bitmap")));
1085 #endif
1086
1087 if (!img.Ok())
1088 {
1089 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1090 param.c_str());
1091 return wxNullBitmap;
1092 }
1093 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
1094 return wxBitmap(img);
1095
1096 }
1097
1098
1099
1100 wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
1101 const wxArtClient& defaultArtClient,
1102 wxSize size)
1103 {
1104 wxIcon icon;
1105 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
1106 return icon;
1107 }
1108
1109
1110
1111 wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
1112 {
1113 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
1114
1115 wxXmlNode *n = m_node->GetChildren();
1116
1117 while (n)
1118 {
1119 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
1120 return n;
1121 n = n->GetNext();
1122 }
1123 return NULL;
1124 }
1125
1126
1127 wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
1128 {
1129 wxXmlNode *n = node;
1130 if (n == NULL) return wxEmptyString;
1131 n = n->GetChildren();
1132
1133 while (n)
1134 {
1135 if (n->GetType() == wxXML_TEXT_NODE ||
1136 n->GetType() == wxXML_CDATA_SECTION_NODE)
1137 return n->GetContent();
1138 n = n->GetNext();
1139 }
1140 return wxEmptyString;
1141 }
1142
1143
1144
1145 wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
1146 {
1147 if (param.empty())
1148 return GetNodeContent(m_node);
1149 else
1150 return GetNodeContent(GetParamNode(param));
1151 }
1152
1153
1154
1155 wxSize wxXmlResourceHandler::GetSize(const wxString& param,
1156 wxWindow *windowToUse)
1157 {
1158 wxString s = GetParamValue(param);
1159 if (s.empty()) s = wxT("-1,-1");
1160 bool is_dlg;
1161 long sx, sy = 0;
1162
1163 is_dlg = s[s.Length()-1] == wxT('d');
1164 if (is_dlg) s.RemoveLast();
1165
1166 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
1167 !s.AfterLast(wxT(',')).ToLong(&sy))
1168 {
1169 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
1170 return wxDefaultSize;
1171 }
1172
1173 if (is_dlg)
1174 {
1175 if (windowToUse)
1176 {
1177 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
1178 }
1179 else if (m_parentAsWindow)
1180 {
1181 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
1182 }
1183 else
1184 {
1185 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1186 return wxDefaultSize;
1187 }
1188 }
1189
1190 return wxSize(sx, sy);
1191 }
1192
1193
1194
1195 wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
1196 {
1197 wxSize sz = GetSize(param);
1198 return wxPoint(sz.x, sz.y);
1199 }
1200
1201
1202
1203 wxCoord wxXmlResourceHandler::GetDimension(const wxString& param,
1204 wxCoord defaultv,
1205 wxWindow *windowToUse)
1206 {
1207 wxString s = GetParamValue(param);
1208 if (s.empty()) return defaultv;
1209 bool is_dlg;
1210 long sx;
1211
1212 is_dlg = s[s.Length()-1] == wxT('d');
1213 if (is_dlg) s.RemoveLast();
1214
1215 if (!s.ToLong(&sx))
1216 {
1217 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
1218 return defaultv;
1219 }
1220
1221 if (is_dlg)
1222 {
1223 if (windowToUse)
1224 {
1225 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
1226 }
1227 else if (m_parentAsWindow)
1228 {
1229 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
1230 }
1231 else
1232 {
1233 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1234 return defaultv;
1235 }
1236 }
1237
1238 return sx;
1239 }
1240
1241
1242 // Get system font index using indexname
1243 static wxFont GetSystemFont(const wxString& name)
1244 {
1245 if (!name.empty())
1246 {
1247 #define SYSFNT(fnt) \
1248 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1249 SYSFNT(wxSYS_OEM_FIXED_FONT)
1250 SYSFNT(wxSYS_ANSI_FIXED_FONT)
1251 SYSFNT(wxSYS_ANSI_VAR_FONT)
1252 SYSFNT(wxSYS_SYSTEM_FONT)
1253 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1254 SYSFNT(wxSYS_DEFAULT_PALETTE)
1255 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
1256 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
1257 #undef SYSFNT
1258 }
1259
1260 return wxNullFont;
1261 }
1262
1263 wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1264 {
1265 wxXmlNode *font_node = GetParamNode(param);
1266 if (font_node == NULL)
1267 {
1268 wxLogError(_("Cannot find font node '%s'."), param.c_str());
1269 return wxNullFont;
1270 }
1271
1272 wxXmlNode *oldnode = m_node;
1273 m_node = font_node;
1274
1275 // font attributes:
1276
1277 // size
1278 int isize = wxDEFAULT;
1279 bool hasSize = HasParam(wxT("size"));
1280 if (hasSize)
1281 isize = GetLong(wxT("size"), wxDEFAULT);
1282
1283 // style
1284 int istyle = wxNORMAL;
1285 bool hasStyle = HasParam(wxT("style"));
1286 if (hasStyle)
1287 {
1288 wxString style = GetParamValue(wxT("style"));
1289 if (style == wxT("italic"))
1290 istyle = wxITALIC;
1291 else if (style == wxT("slant"))
1292 istyle = wxSLANT;
1293 }
1294
1295 // weight
1296 int iweight = wxNORMAL;
1297 bool hasWeight = HasParam(wxT("weight"));
1298 if (hasWeight)
1299 {
1300 wxString weight = GetParamValue(wxT("weight"));
1301 if (weight == wxT("bold"))
1302 iweight = wxBOLD;
1303 else if (weight == wxT("light"))
1304 iweight = wxLIGHT;
1305 }
1306
1307 // underline
1308 bool hasUnderlined = HasParam(wxT("underlined"));
1309 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
1310
1311 // family and facename
1312 int ifamily = wxDEFAULT;
1313 bool hasFamily = HasParam(wxT("family"));
1314 if (hasFamily)
1315 {
1316 wxString family = GetParamValue(wxT("family"));
1317 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1318 else if (family == wxT("roman")) ifamily = wxROMAN;
1319 else if (family == wxT("script")) ifamily = wxSCRIPT;
1320 else if (family == wxT("swiss")) ifamily = wxSWISS;
1321 else if (family == wxT("modern")) ifamily = wxMODERN;
1322 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
1323 }
1324
1325
1326 wxString facename;
1327 bool hasFacename = HasParam(wxT("face"));
1328 if (hasFacename)
1329 {
1330 wxString faces = GetParamValue(wxT("face"));
1331 wxFontEnumerator enu;
1332 enu.EnumerateFacenames();
1333 wxStringTokenizer tk(faces, wxT(","));
1334 while (tk.HasMoreTokens())
1335 {
1336 int index = enu.GetFacenames()->Index(tk.GetNextToken(), false);
1337 if (index != wxNOT_FOUND)
1338 {
1339 facename = (*enu.GetFacenames())[index];
1340 break;
1341 }
1342 }
1343 }
1344
1345 // encoding
1346 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1347 bool hasEncoding = HasParam(wxT("encoding"));
1348 if (hasEncoding)
1349 {
1350 wxString encoding = GetParamValue(wxT("encoding"));
1351 wxFontMapper mapper;
1352 if (!encoding.empty())
1353 enc = mapper.CharsetToEncoding(encoding);
1354 if (enc == wxFONTENCODING_SYSTEM)
1355 enc = wxFONTENCODING_DEFAULT;
1356 }
1357
1358 // is this font based on a system font?
1359 wxFont sysfont = GetSystemFont(GetParamValue(wxT("sysfont")));
1360
1361 if (sysfont.Ok())
1362 {
1363 if (hasSize)
1364 sysfont.SetPointSize(isize);
1365 else if (HasParam(wxT("relativesize")))
1366 sysfont.SetPointSize(int(sysfont.GetPointSize() *
1367 GetFloat(wxT("relativesize"))));
1368
1369 if (hasStyle)
1370 sysfont.SetStyle(istyle);
1371 if (hasWeight)
1372 sysfont.SetWeight(iweight);
1373 if (hasUnderlined)
1374 sysfont.SetUnderlined(underlined);
1375 if (hasFamily)
1376 sysfont.SetFamily(ifamily);
1377 if (hasFacename)
1378 sysfont.SetFaceName(facename);
1379 if (hasEncoding)
1380 sysfont.SetDefaultEncoding(enc);
1381
1382 m_node = oldnode;
1383 return sysfont;
1384 }
1385
1386 m_node = oldnode;
1387 return wxFont(isize, ifamily, istyle, iweight,
1388 underlined, facename, enc);
1389 }
1390
1391
1392 void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1393 {
1394 //FIXME : add cursor
1395
1396 if (HasParam(wxT("exstyle")))
1397 // Have to OR it with existing style, since
1398 // some implementations (e.g. wxGTK) use the extra style
1399 // during creation
1400 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
1401 if (HasParam(wxT("bg")))
1402 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1403 if (HasParam(wxT("fg")))
1404 wnd->SetForegroundColour(GetColour(wxT("fg")));
1405 if (GetBool(wxT("enabled"), 1) == 0)
1406 wnd->Enable(false);
1407 if (GetBool(wxT("focused"), 0) == 1)
1408 wnd->SetFocus();
1409 if (GetBool(wxT("hidden"), 0) == 1)
1410 wnd->Show(false);
1411 #if wxUSE_TOOLTIPS
1412 if (HasParam(wxT("tooltip")))
1413 wnd->SetToolTip(GetText(wxT("tooltip")));
1414 #endif
1415 if (HasParam(wxT("font")))
1416 wnd->SetFont(GetFont());
1417 if (HasParam(wxT("help")))
1418 wnd->SetHelpText(GetText(wxT("help")));
1419 }
1420
1421
1422 void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1423 {
1424 wxXmlNode *n = m_node->GetChildren();
1425
1426 while (n)
1427 {
1428 if (n->GetType() == wxXML_ELEMENT_NODE &&
1429 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
1430 {
1431 m_resource->CreateResFromNode(n, parent, NULL,
1432 this_hnd_only ? this : NULL);
1433 }
1434 n = n->GetNext();
1435 }
1436 }
1437
1438
1439 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1440 {
1441 wxXmlNode *root;
1442 if (rootnode == NULL) root = m_node; else root = rootnode;
1443 wxXmlNode *n = root->GetChildren();
1444
1445 while (n)
1446 {
1447 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1448 {
1449 CreateResource(n, parent, NULL);
1450 }
1451 n = n->GetNext();
1452 }
1453 }
1454
1455
1456
1457
1458
1459
1460
1461 // --------------- XRCID implementation -----------------------------
1462
1463 #define XRCID_TABLE_SIZE 1024
1464
1465
1466 struct XRCID_record
1467 {
1468 int id;
1469 wxChar *key;
1470 XRCID_record *next;
1471 };
1472
1473 static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
1474
1475 static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2)
1476 {
1477 int index = 0;
1478
1479 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
1480 index %= XRCID_TABLE_SIZE;
1481
1482 XRCID_record *oldrec = NULL;
1483 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
1484 {
1485 if (wxStrcmp(rec->key, str_id) == 0)
1486 {
1487 return rec->id;
1488 }
1489 oldrec = rec;
1490 }
1491
1492 XRCID_record **rec_var = (oldrec == NULL) ?
1493 &XRCID_Records[index] : &oldrec->next;
1494 *rec_var = new XRCID_record;
1495 (*rec_var)->key = wxStrdup(str_id);
1496 (*rec_var)->next = NULL;
1497
1498 wxChar *end;
1499 if (value_if_not_found != -2)
1500 (*rec_var)->id = value_if_not_found;
1501 else
1502 {
1503 int asint = wxStrtol(str_id, &end, 10);
1504 if (*str_id && *end == 0)
1505 {
1506 // if str_id was integer, keep it verbosely:
1507 (*rec_var)->id = asint;
1508 }
1509 else
1510 {
1511 (*rec_var)->id = wxNewId();
1512 }
1513 }
1514
1515 return (*rec_var)->id;
1516 }
1517
1518 static void AddStdXRCID_Records();
1519
1520 /*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1521 {
1522 static bool s_stdIDsAdded = false;
1523
1524 if ( !s_stdIDsAdded )
1525 {
1526 s_stdIDsAdded = true;
1527 AddStdXRCID_Records();
1528 }
1529
1530 return XRCID_Lookup(str_id);
1531 }
1532
1533
1534 static void CleanXRCID_Record(XRCID_record *rec)
1535 {
1536 if (rec)
1537 {
1538 CleanXRCID_Record(rec->next);
1539 free(rec->key);
1540 delete rec;
1541 }
1542 }
1543
1544 static void CleanXRCID_Records()
1545 {
1546 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
1547 {
1548 CleanXRCID_Record(XRCID_Records[i]);
1549 XRCID_Records[i] = NULL;
1550 }
1551 }
1552
1553 static void AddStdXRCID_Records()
1554 {
1555 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1556 stdID(-1);
1557
1558 stdID(wxID_ANY);
1559 stdID(wxID_SEPARATOR);
1560
1561 stdID(wxID_OPEN);
1562 stdID(wxID_CLOSE);
1563 stdID(wxID_NEW);
1564 stdID(wxID_SAVE);
1565 stdID(wxID_SAVEAS);
1566 stdID(wxID_REVERT);
1567 stdID(wxID_EXIT);
1568 stdID(wxID_UNDO);
1569 stdID(wxID_REDO);
1570 stdID(wxID_HELP);
1571 stdID(wxID_PRINT);
1572 stdID(wxID_PRINT_SETUP);
1573 stdID(wxID_PREVIEW);
1574 stdID(wxID_ABOUT);
1575 stdID(wxID_HELP_CONTENTS);
1576 stdID(wxID_HELP_COMMANDS);
1577 stdID(wxID_HELP_PROCEDURES);
1578 stdID(wxID_HELP_CONTEXT);
1579 stdID(wxID_CLOSE_ALL);
1580 stdID(wxID_PREFERENCES);
1581 stdID(wxID_CUT);
1582 stdID(wxID_COPY);
1583 stdID(wxID_PASTE);
1584 stdID(wxID_CLEAR);
1585 stdID(wxID_FIND);
1586 stdID(wxID_DUPLICATE);
1587 stdID(wxID_SELECTALL);
1588 stdID(wxID_DELETE);
1589 stdID(wxID_REPLACE);
1590 stdID(wxID_REPLACE_ALL);
1591 stdID(wxID_PROPERTIES);
1592 stdID(wxID_VIEW_DETAILS);
1593 stdID(wxID_VIEW_LARGEICONS);
1594 stdID(wxID_VIEW_SMALLICONS);
1595 stdID(wxID_VIEW_LIST);
1596 stdID(wxID_VIEW_SORTDATE);
1597 stdID(wxID_VIEW_SORTNAME);
1598 stdID(wxID_VIEW_SORTSIZE);
1599 stdID(wxID_VIEW_SORTTYPE);
1600 stdID(wxID_FILE1);
1601 stdID(wxID_FILE2);
1602 stdID(wxID_FILE3);
1603 stdID(wxID_FILE4);
1604 stdID(wxID_FILE5);
1605 stdID(wxID_FILE6);
1606 stdID(wxID_FILE7);
1607 stdID(wxID_FILE8);
1608 stdID(wxID_FILE9);
1609 stdID(wxID_OK);
1610 stdID(wxID_CANCEL);
1611 stdID(wxID_APPLY);
1612 stdID(wxID_YES);
1613 stdID(wxID_NO);
1614 stdID(wxID_STATIC);
1615 stdID(wxID_FORWARD);
1616 stdID(wxID_BACKWARD);
1617 stdID(wxID_DEFAULT);
1618 stdID(wxID_MORE);
1619 stdID(wxID_SETUP);
1620 stdID(wxID_RESET);
1621 stdID(wxID_CONTEXT_HELP);
1622 stdID(wxID_YESTOALL);
1623 stdID(wxID_NOTOALL);
1624 stdID(wxID_ABORT);
1625 stdID(wxID_RETRY);
1626 stdID(wxID_IGNORE);
1627 stdID(wxID_ADD);
1628 stdID(wxID_REMOVE);
1629 stdID(wxID_UP);
1630 stdID(wxID_DOWN);
1631 stdID(wxID_HOME);
1632 stdID(wxID_REFRESH);
1633 stdID(wxID_STOP);
1634 stdID(wxID_INDEX);
1635 stdID(wxID_BOLD);
1636 stdID(wxID_ITALIC);
1637 stdID(wxID_JUSTIFY_CENTER);
1638 stdID(wxID_JUSTIFY_FILL);
1639 stdID(wxID_JUSTIFY_RIGHT);
1640 stdID(wxID_JUSTIFY_LEFT);
1641 stdID(wxID_UNDERLINE);
1642 stdID(wxID_INDENT);
1643 stdID(wxID_UNINDENT);
1644 stdID(wxID_ZOOM_100);
1645 stdID(wxID_ZOOM_FIT);
1646 stdID(wxID_ZOOM_IN);
1647 stdID(wxID_ZOOM_OUT);
1648 stdID(wxID_UNDELETE);
1649 stdID(wxID_REVERT_TO_SAVED);
1650 stdID(wxID_SYSTEM_MENU);
1651 stdID(wxID_CLOSE_FRAME);
1652 stdID(wxID_MOVE_FRAME);
1653 stdID(wxID_RESIZE_FRAME);
1654 stdID(wxID_MAXIMIZE_FRAME);
1655 stdID(wxID_ICONIZE_FRAME);
1656 stdID(wxID_RESTORE_FRAME);
1657
1658 #undef stdID
1659 }
1660
1661
1662
1663
1664
1665 // --------------- module and globals -----------------------------
1666
1667 class wxXmlResourceModule: public wxModule
1668 {
1669 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1670 public:
1671 wxXmlResourceModule() {}
1672 bool OnInit()
1673 {
1674 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
1675 return true;
1676 }
1677 void OnExit()
1678 {
1679 delete wxXmlResource::Set(NULL);
1680 if(wxXmlResource::ms_subclassFactories)
1681 WX_CLEAR_LIST(wxXmlSubclassFactoriesList, *wxXmlResource::ms_subclassFactories);
1682 wxDELETE(wxXmlResource::ms_subclassFactories);
1683 CleanXRCID_Records();
1684 }
1685 };
1686
1687 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1688
1689
1690 // When wxXml is loaded dynamically after the application is already running
1691 // then the built-in module system won't pick this one up. Add it manually.
1692 void wxXmlInitResourceModule()
1693 {
1694 wxModule* module = new wxXmlResourceModule;
1695 module->Init();
1696 wxModule::RegisterModule(module);
1697 }
1698
1699 #endif // wxUSE_XRC