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