]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
unknown controls container can't be subclassed, assert if somebody tries it (as does...
[wxWidgets.git] / src / xrc / xmlres.cpp
CommitLineData
78d14f80
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xmlres.cpp
b5d6954b 3// Purpose: XRC resources
78d14f80
VS
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#ifdef __GNUG__
12#pragma implementation "xmlres.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/dialog.h"
23#include "wx/panel.h"
24#include "wx/frame.h"
25#include "wx/wfstream.h"
26#include "wx/filesys.h"
317a0d73 27#include "wx/filename.h"
78d14f80
VS
28#include "wx/log.h"
29#include "wx/intl.h"
30#include "wx/tokenzr.h"
31#include "wx/fontenum.h"
32#include "wx/module.h"
33#include "wx/bitmap.h"
34#include "wx/image.h"
35#include "wx/fontmap.h"
af1337b0 36#include "wx/artprov.h"
78d14f80 37
34c6bbee 38#include "wx/xml/xml.h"
78d14f80
VS
39#include "wx/xrc/xmlres.h"
40
41#include "wx/arrimpl.cpp"
42WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
43
44
824e8eaa
VS
45wxXmlResource *wxXmlResource::ms_instance = NULL;
46
47/*static*/ wxXmlResource *wxXmlResource::Get()
48{
49 if ( !ms_instance )
50 ms_instance = new wxXmlResource;
51 return ms_instance;
52}
53
54/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
55{
56 wxXmlResource *old = ms_instance;
57 ms_instance = res;
58 return old;
59}
60
daa85ee3 61wxXmlResource::wxXmlResource(int flags)
78d14f80 62{
daa85ee3 63 m_flags = flags;
78d14f80
VS
64 m_version = -1;
65}
66
daa85ee3 67wxXmlResource::wxXmlResource(const wxString& filemask, int flags)
78d14f80 68{
daa85ee3 69 m_flags = flags;
78d14f80 70 m_version = -1;
78d14f80
VS
71 Load(filemask);
72}
73
74wxXmlResource::~wxXmlResource()
75{
76 ClearHandlers();
77}
78
79
80bool wxXmlResource::Load(const wxString& filemask)
81{
82 wxString fnd;
83 wxXmlResourceDataRecord *drec;
84 bool iswild = wxIsWild(filemask);
85 bool rt = TRUE;
86
87#if wxUSE_FILESYSTEM
88 wxFileSystem fsys;
89# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
90# define wxXmlFindNext fsys.FindNext()
91#else
92# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
93# define wxXmlFindNext wxFindNextFile()
94#endif
95 if (iswild)
96 fnd = wxXmlFindFirst;
97 else
98 fnd = filemask;
99 while (!!fnd)
100 {
317a0d73
VS
101 // NB: Load() accepts both filenames and URLs (should probably be
102 // changed to filenames only, but embedded resources currently
103 // rely on its ability to handle URLs - FIXME). This check
104 // serves as a quick way to determine whether found name is
105 // filename and not URL:
106 if (wxFileName::FileExists(fnd))
107 {
b380439d 108 // Make the name absolute filename, because the app may
317a0d73
VS
109 // change working directory later:
110 wxFileName fn(fnd);
111 if (fn.IsRelative())
112 {
56acdfef 113 fn.MakeAbsolute();
317a0d73
VS
114 fnd = fn.GetFullPath();
115 }
75d38380
VS
116#if wxUSE_FILESYSTEM
117 fnd = wxFileSystem::FileNameToURL(fnd);
118#endif
317a0d73 119 }
b380439d 120
78d14f80 121#if wxUSE_FILESYSTEM
317a0d73
VS
122 if (fnd.Lower().Matches(wxT("*.zip")) ||
123 fnd.Lower().Matches(wxT("*.xrs")))
78d14f80 124 {
f9d243a3 125 rt = rt && Load(fnd + wxT("#zip:*.xrc"));
78d14f80
VS
126 }
127 else
128#endif
129 {
130 drec = new wxXmlResourceDataRecord;
131 drec->File = fnd;
132 m_data.Add(drec);
133 }
134
135 if (iswild)
136 fnd = wxXmlFindNext;
137 else
138 fnd = wxEmptyString;
139 }
140# undef wxXmlFindFirst
141# undef wxXmlFindNext
d614f51b 142 return rt && UpdateResources();
78d14f80
VS
143}
144
145
854e189f 146IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)
78d14f80
VS
147
148void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
149{
150 m_handlers.Append(handler);
151 handler->SetParentResource(this);
152}
153
92e898b0
RD
154void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
155{
156 m_handlers.Insert(handler);
157 handler->SetParentResource(this);
158}
159
78d14f80
VS
160
161
162void wxXmlResource::ClearHandlers()
163{
461932ae 164 WX_CLEAR_LIST(wxList, m_handlers);
78d14f80
VS
165}
166
167
78d14f80
VS
168wxMenu *wxXmlResource::LoadMenu(const wxString& name)
169{
170 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
171}
172
173
174
4a1b9596 175wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 176{
4a1b9596 177 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
178}
179
180
181
4a1b9596 182#if wxUSE_TOOLBAR
78d14f80
VS
183wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
184{
185 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
186}
4a1b9596 187#endif
78d14f80
VS
188
189
190wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
191{
4dd75a6a 192 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
78d14f80
VS
193}
194
195bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
196{
197 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
198}
199
200
201
202wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
203{
204 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
205}
206
207bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
208{
209 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
210}
211
92e898b0
RD
212wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
213{
214 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
215}
216
78d14f80
VS
217bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
218{
219 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
220}
221
222wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
223{
224 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
225 FindResource(name, wxT("wxBitmap")), NULL, NULL);
226 wxBitmap rt;
227
228 if (bmp) { rt = *bmp; delete bmp; }
229 return rt;
230}
231
232wxIcon wxXmlResource::LoadIcon(const wxString& name)
233{
234 wxIcon *icon = (wxIcon*)CreateResFromNode(
235 FindResource(name, wxT("wxIcon")), NULL, NULL);
236 wxIcon rt;
237
238 if (icon) { rt = *icon; delete icon; }
239 return rt;
240}
241
92e898b0
RD
242
243wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname)
244{
245 return CreateResFromNode(FindResource(name, classname), parent, NULL);
246}
247
248bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname)
249{
250 return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;
251}
252
253
78d14f80
VS
254bool wxXmlResource::AttachUnknownControl(const wxString& name,
255 wxWindow *control, wxWindow *parent)
256{
257 if (parent == NULL)
258 parent = control->GetParent();
259 wxWindow *container = parent->FindWindow(name + wxT("_container"));
260 if (!container)
261 {
262 wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str());
263 return FALSE;
264 }
265 return control->Reparent(container);
266}
267
268
77b2f9b1 269static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
270{
271 wxString s;
272 bool isok;
273
274 wxXmlNode *c = node->GetChildren();
275 while (c)
276 {
277 isok = FALSE;
278 if (!c->GetPropVal(wxT("platform"), &s))
279 isok = TRUE;
280 else
281 {
2b5f62a0 282 wxStringTokenizer tkn(s, wxT(" |"));
78d14f80
VS
283
284 while (tkn.HasMoreTokens())
285 {
286 s = tkn.GetNextToken();
84389518 287#ifdef __WINDOWS__
d003330c
VS
288 if (s == wxT("win")) isok = true;
289#endif
b380439d 290#if defined(__MAC__) || defined(__APPLE__)
d003330c 291 if (s == wxT("mac")) isok = true;
b380439d
RD
292#elif defined(__UNIX__)
293 if (s == wxT("unix")) isok = true;
78d14f80 294#endif
d003330c
VS
295#ifdef __OS2__
296 if (s == wxT("os2")) isok = true;
297#endif
298
299 if (isok)
300 break;
78d14f80
VS
301 }
302 }
303
304 if (isok)
d7b1d73c 305 {
78d14f80 306 ProcessPlatformProperty(c);
d7b1d73c
VS
307 c = c->GetNext();
308 }
78d14f80
VS
309 else
310 {
d7b1d73c 311 wxXmlNode *c2 = c->GetNext();
db59a97c 312 node->RemoveChild(c);
78d14f80 313 delete c;
d7b1d73c 314 c = c2;
78d14f80 315 }
78d14f80
VS
316 }
317}
318
319
320
d614f51b 321bool wxXmlResource::UpdateResources()
78d14f80 322{
d614f51b 323 bool rt = true;
78d14f80
VS
324 bool modif;
325# if wxUSE_FILESYSTEM
326 wxFSFile *file = NULL;
327 wxFileSystem fsys;
328# endif
329
480505bc
VS
330 wxString encoding(wxT("UTF-8"));
331#if !wxUSE_UNICODE && wxUSE_INTL
332 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
333 {
75d38380
VS
334 // In case we are not using wxLocale to translate strings, convert the
335 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
336 // is on, because it could break wxGetTranslation lookup.
480505bc
VS
337 encoding = wxLocale::GetSystemEncodingName();
338 }
339#endif
340
78d14f80
VS
341 for (size_t i = 0; i < m_data.GetCount(); i++)
342 {
343 modif = (m_data[i].Doc == NULL);
344
345 if (!modif)
346 {
347# if wxUSE_FILESYSTEM
348 file = fsys.OpenFile(m_data[i].File);
349 modif = file && file->GetModificationTime() > m_data[i].Time;
350 if (!file)
d614f51b 351 {
78d14f80 352 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
d614f51b
VS
353 rt = false;
354 }
78d14f80
VS
355 wxDELETE(file);
356# else
357 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
358# endif
359 }
360
361 if (modif)
362 {
480505bc 363 wxInputStream *stream = NULL;
78d14f80
VS
364
365# if wxUSE_FILESYSTEM
366 file = fsys.OpenFile(m_data[i].File);
367 if (file)
368 stream = file->GetStream();
369# else
370 stream = new wxFileInputStream(m_data[i].File);
371# endif
372
373 if (stream)
374 {
375 delete m_data[i].Doc;
376 m_data[i].Doc = new wxXmlDocument;
377 }
4d876ee3 378 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
78d14f80 379 {
480505bc
VS
380 wxLogError(_("Cannot load resources from file '%s'."),
381 m_data[i].File.c_str());
78d14f80 382 wxDELETE(m_data[i].Doc);
d614f51b 383 rt = false;
78d14f80
VS
384 }
385 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
386 {
b5d6954b 387 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
78d14f80 388 wxDELETE(m_data[i].Doc);
d614f51b 389 rt = false;
78d14f80
VS
390 }
391 else
392 {
393 long version;
394 int v1, v2, v3, v4;
395 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
396 wxT("version"), wxT("0.0.0.0"));
397 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
398 &v1, &v2, &v3, &v4) == 4)
399 version = v1*256*256*256+v2*256*256+v3*256+v4;
400 else
401 version = 0;
402 if (m_version == -1)
403 m_version = version;
404 if (m_version != version)
d614f51b 405 {
78d14f80 406 wxLogError(_("Resource files must have same version number!"));
d614f51b
VS
407 rt = false;
408 }
78d14f80
VS
409
410 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
411 m_data[i].Time = file->GetModificationTime();
412 }
413
414# if wxUSE_FILESYSTEM
415 wxDELETE(file);
416# else
417 wxDELETE(stream);
418# endif
419 }
420 }
d614f51b
VS
421
422 return rt;
78d14f80
VS
423}
424
425
b272b6dc
RD
426wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
427 const wxString& name,
428 const wxString& classname,
47793ab8
VS
429 bool recursive)
430{
431 wxString dummy;
432 wxXmlNode *node;
433
434 // first search for match at the top-level nodes (as this is
435 // where the resource is most commonly looked for):
436 for (node = parent->GetChildren(); node; node = node->GetNext())
437 {
b272b6dc
RD
438 if ( node->GetType() == wxXML_ELEMENT_NODE &&
439 (node->GetName() == wxT("object") ||
47793ab8 440 node->GetName() == wxT("object_ref")) &&
2b5f62a0
VZ
441 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
442 {
443 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
444 if (!classname || cls == classname)
445 return node;
446 // object_ref may not have 'class' property:
447 if (cls.empty() && node->GetName() == wxT("object_ref"))
448 {
449 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
450 if (refName.empty())
451 continue;
452 wxXmlNode* refNode = FindResource(refName, wxEmptyString, TRUE);
453 if (refNode &&
454 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
455 {
456 return node;
457 }
458 }
459 }
47793ab8
VS
460 }
461
462 if ( recursive )
463 for (node = parent->GetChildren(); node; node = node->GetNext())
464 {
b272b6dc
RD
465 if ( node->GetType() == wxXML_ELEMENT_NODE &&
466 (node->GetName() == wxT("object") ||
47793ab8
VS
467 node->GetName() == wxT("object_ref")) )
468 {
469 wxXmlNode* found = DoFindResource(node, name, classname, TRUE);
470 if ( found )
471 return found;
472 }
473 }
474
475 return NULL;
476}
78d14f80 477
b272b6dc 478wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
479 const wxString& classname,
480 bool recursive)
78d14f80
VS
481{
482 UpdateResources(); //ensure everything is up-to-date
483
484 wxString dummy;
485 for (size_t f = 0; f < m_data.GetCount(); f++)
486 {
47793ab8
VS
487 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
488 continue;
489
b272b6dc 490 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
47793ab8
VS
491 name, classname, recursive);
492 if ( found )
493 {
78d14f80 494#if wxUSE_FILESYSTEM
47793ab8 495 m_curFileSystem.ChangePathTo(m_data[f].File);
78d14f80 496#endif
47793ab8
VS
497 return found;
498 }
78d14f80
VS
499 }
500
b5d6954b 501 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
78d14f80
VS
502 name.c_str(), classname.c_str());
503 return NULL;
504}
505
47793ab8
VS
506static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
507{
508 // Merge properties:
509 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
510 {
511 wxXmlProperty *dprop;
512 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
513 {
b272b6dc 514
47793ab8
VS
515 if ( dprop->GetName() == prop->GetName() )
516 {
517 dprop->SetValue(prop->GetValue());
518 break;
519 }
520 }
78d14f80 521
47793ab8
VS
522 if ( !dprop )
523 dest.AddProperty(prop->GetName(), prop->GetValue());
524 }
525
526 // Merge child nodes:
527 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
528 {
529 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
530 wxXmlNode *dnode;
531
532 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
533 {
534 if ( dnode->GetName() == node->GetName() &&
2b5f62a0 535 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
536 dnode->GetType() == node->GetType() )
537 {
538 MergeNodes(*dnode, *node);
539 break;
540 }
541 }
542
543 if ( !dnode )
544 dest.AddChild(new wxXmlNode(*node));
545 }
546
547 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
548 dest.SetContent(with.GetContent());
549}
78d14f80 550
317a0d73
VS
551wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
552 wxObject *instance,
553 wxXmlResourceHandler *handlerToUse)
78d14f80
VS
554{
555 if (node == NULL) return NULL;
556
47793ab8
VS
557 // handling of referenced resource
558 if ( node->GetName() == wxT("object_ref") )
559 {
560 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
561 wxXmlNode* refNode = FindResource(refName, wxEmptyString, TRUE);
562
563 if ( !refNode )
564 {
b272b6dc 565 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
47793ab8
VS
566 refName.c_str());
567 return NULL;
568 }
569
570 wxXmlNode copy(*refNode);
571 MergeNodes(copy, *node);
572
573 return CreateResFromNode(&copy, parent, instance);
574 }
575
78d14f80 576 wxXmlResourceHandler *handler;
317a0d73
VS
577
578 if (handlerToUse)
b380439d 579 {
317a0d73 580 if (handlerToUse->CanHandle(node))
78d14f80 581 {
317a0d73
VS
582 return handlerToUse->CreateResource(node, parent, instance);
583 }
584 }
585 else if (node->GetName() == wxT("object"))
b380439d 586 {
461932ae 587 wxList::compatibility_iterator ND = m_handlers.GetFirst();
317a0d73
VS
588 while (ND)
589 {
590 handler = (wxXmlResourceHandler*)ND->GetData();
591 if (handler->CanHandle(node))
592 {
593 return handler->CreateResource(node, parent, instance);
594 }
595 ND = ND->GetNext();
78d14f80 596 }
78d14f80
VS
597 }
598
599 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
600 node->GetName().c_str(),
601 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
602 return NULL;
603}
604
605
2b5f62a0
VZ
606#include "wx/listimpl.cpp"
607WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
608WX_DEFINE_LIST(wxXmlSubclassFactoriesList);
609
610wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
611
612/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
613{
614 if (!ms_subclassFactories)
615 {
616 ms_subclassFactories = new wxXmlSubclassFactoriesList;
2b5f62a0
VZ
617 }
618 ms_subclassFactories->Append(factory);
619}
620
621class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
622{
623public:
624 ~wxXmlSubclassFactoryCXX() {}
625
626 wxObject *Create(const wxString& className)
627 {
628 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
629
630 if (classInfo)
631 return classInfo->CreateObject();
632 else
633 return NULL;
634 }
635};
636
637
638
78d14f80
VS
639
640
78d14f80
VS
641wxXmlResourceHandler::wxXmlResourceHandler()
642 : m_node(NULL), m_parent(NULL), m_instance(NULL),
643 m_parentAsWindow(NULL), m_instanceAsWindow(NULL)
644{}
645
646
647
648wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
649{
650 wxXmlNode *myNode = m_node;
651 wxString myClass = m_class;
652 wxObject *myParent = m_parent, *myInstance = m_instance;
653 wxWindow *myParentAW = m_parentAsWindow, *myInstanceAW = m_instanceAsWindow;
654
daa85ee3 655 m_instance = instance;
b272b6dc 656 if (!m_instance && node->HasProp(wxT("subclass")) &&
daa85ee3
VS
657 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
658 {
659 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
2b5f62a0 660 if (!subclass.empty())
daa85ee3 661 {
461932ae 662 for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst();
2b5f62a0
VZ
663 i; i = i->GetNext())
664 {
665 m_instance = i->GetData()->Create(subclass);
666 if (m_instance)
667 break;
668 }
daa85ee3 669
2b5f62a0
VZ
670 if (!m_instance)
671 {
672 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
673 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
674 subclass.c_str(), name.c_str());
675 }
676 }
daa85ee3
VS
677 }
678
78d14f80
VS
679 m_node = node;
680 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
681 m_parent = parent;
78d14f80
VS
682 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
683 m_instanceAsWindow = wxDynamicCast(m_instance, wxWindow);
684
685 wxObject *returned = DoCreateResource();
686
687 m_node = myNode;
688 m_class = myClass;
689 m_parent = myParent; m_parentAsWindow = myParentAW;
690 m_instance = myInstance; m_instanceAsWindow = myInstanceAW;
691
692 return returned;
693}
694
695
696void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
697{
698 m_styleNames.Add(name);
699 m_styleValues.Add(value);
700}
701
702
703
704void wxXmlResourceHandler::AddWindowStyles()
705{
daa85ee3
VS
706 XRC_ADD_STYLE(wxSIMPLE_BORDER);
707 XRC_ADD_STYLE(wxSUNKEN_BORDER);
708 XRC_ADD_STYLE(wxDOUBLE_BORDER);
709 XRC_ADD_STYLE(wxRAISED_BORDER);
710 XRC_ADD_STYLE(wxSTATIC_BORDER);
711 XRC_ADD_STYLE(wxNO_BORDER);
712 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
713 XRC_ADD_STYLE(wxWANTS_CHARS);
714 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
78d14f80
VS
715}
716
717
718
719bool wxXmlResourceHandler::HasParam(const wxString& param)
720{
721 return (GetParamNode(param) != NULL);
722}
723
724
725int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
726{
727 wxString s = GetParamValue(param);
728
729 if (!s) return defaults;
730
2b5f62a0 731 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
732 int style = 0;
733 int index;
734 wxString fl;
735 while (tkn.HasMoreTokens())
736 {
737 fl = tkn.GetNextToken();
738 index = m_styleNames.Index(fl);
739 if (index != wxNOT_FOUND)
740 style |= m_styleValues[index];
741 else
742 wxLogError(_("Unknown style flag ") + fl);
743 }
744 return style;
745}
746
747
748
ee1046d1 749wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
78d14f80 750{
718cf160 751 wxString str1(GetParamValue(param));
78d14f80
VS
752 wxString str2;
753 const wxChar *dt;
754 wxChar amp_char;
755
b272b6dc
RD
756 // VS: First version of XRC resources used $ instead of & (which is
757 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 758 // much better (because &File means "File with F underlined").
78d14f80
VS
759 if (m_resource->CompareVersion(2,3,0,1) < 0)
760 amp_char = wxT('$');
761 else
762 amp_char = wxT('_');
763
764 for (dt = str1.c_str(); *dt; dt++)
765 {
766 // Remap amp_char to &, map double amp_char to amp_char (for things
767 // like "&File..." -- this is illegal in XML, so we use "_File..."):
768 if (*dt == amp_char)
769 {
770 if ( *(++dt) == amp_char )
771 str2 << amp_char;
772 else
773 str2 << wxT('&') << *dt;
774 }
775 // Remap \n to CR, \r to LF, \t to TAB:
776 else if (*dt == wxT('\\'))
777 switch (*(++dt))
778 {
779 case wxT('n') : str2 << wxT('\n'); break;
780 case wxT('t') : str2 << wxT('\t'); break;
781 case wxT('r') : str2 << wxT('\r'); break;
782 default : str2 << wxT('\\') << *dt; break;
783 }
784 else str2 << *dt;
785 }
b272b6dc 786
718cf160
VS
787 if (translate && m_resource->GetFlags() & wxXRC_USE_LOCALE)
788 return wxGetTranslation(str2);
789 else
790 return str2;
791
78d14f80
VS
792}
793
794
795
796long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
797{
798 long value;
799 wxString str1 = GetParamValue(param);
800
801 if (!str1.ToLong(&value))
802 value = defaultv;
803
804 return value;
805}
806
807
af1337b0 808
78d14f80
VS
809int wxXmlResourceHandler::GetID()
810{
13de23f6 811 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
812}
813
814
af1337b0 815
78d14f80
VS
816wxString wxXmlResourceHandler::GetName()
817{
818 return m_node->GetPropVal(wxT("name"), wxT("-1"));
819}
820
821
822
823bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
824{
825 wxString v = GetParamValue(param);
826 v.MakeLower();
827 if (!v) return defaultv;
828 else return (v == wxT("1"));
829}
830
831
832
833wxColour wxXmlResourceHandler::GetColour(const wxString& param)
834{
835 wxString v = GetParamValue(param);
836 unsigned long tmp = 0;
837
838 if (v.Length() != 7 || v[0u] != wxT('#') ||
839 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
840 {
b5d6954b 841 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
78d14f80
VS
842 v.c_str(), param.c_str());
843 return wxNullColour;
844 }
845
846 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
847 (unsigned char) ((tmp & 0x00FF00) >> 8),
848 (unsigned char) ((tmp & 0x0000FF)));
849}
850
851
852
92e898b0 853wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
db59a97c
VS
854 const wxArtClient& defaultArtClient,
855 wxSize size)
78d14f80 856{
db59a97c
VS
857 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
858 wxXmlNode *bmpNode = GetParamNode(param);
859 if ( bmpNode )
af1337b0 860 {
db59a97c
VS
861 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
862 if ( !sid.empty() )
863 {
864 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), defaultArtClient);
92e898b0 865 wxBitmap stockArt =
db59a97c
VS
866 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
867 wxART_MAKE_CLIENT_ID_FROM_STR(scl),
868 size);
869 if ( stockArt.Ok() )
870 return stockArt;
871 }
af1337b0
JS
872 }
873
92e898b0 874 /* ...or load the bitmap from file: */
78d14f80 875 wxString name = GetParamValue(param);
92e898b0 876 if (name.IsEmpty()) return wxNullBitmap;
78d14f80
VS
877#if wxUSE_FILESYSTEM
878 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
879 if (fsfile == NULL)
880 {
d0b50ad9
VS
881 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
882 name.c_str());
78d14f80
VS
883 return wxNullBitmap;
884 }
885 wxImage img(*(fsfile->GetStream()));
886 delete fsfile;
887#else
888 wxImage img(GetParamValue(wxT("bitmap")));
889#endif
af1337b0 890
78d14f80
VS
891 if (!img.Ok())
892 {
b5d6954b 893 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
78d14f80
VS
894 return wxNullBitmap;
895 }
896 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 897 return wxBitmap(img);
af1337b0 898
78d14f80
VS
899}
900
901
902
92e898b0 903wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
db59a97c
VS
904 const wxArtClient& defaultArtClient,
905 wxSize size)
78d14f80 906{
78d14f80 907 wxIcon icon;
db59a97c 908 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
78d14f80
VS
909 return icon;
910}
911
912
913
914wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
915{
2b5f62a0
VZ
916 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
917
78d14f80
VS
918 wxXmlNode *n = m_node->GetChildren();
919
920 while (n)
921 {
922 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
923 return n;
924 n = n->GetNext();
925 }
926 return NULL;
927}
928
929
930wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
931{
932 wxXmlNode *n = node;
933 if (n == NULL) return wxEmptyString;
934 n = n->GetChildren();
935
936 while (n)
937 {
938 if (n->GetType() == wxXML_TEXT_NODE ||
939 n->GetType() == wxXML_CDATA_SECTION_NODE)
940 return n->GetContent();
941 n = n->GetNext();
942 }
943 return wxEmptyString;
944}
945
946
947
948wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
949{
950 if (param.IsEmpty())
951 return GetNodeContent(m_node);
952 else
953 return GetNodeContent(GetParamNode(param));
954}
955
956
957
958wxSize wxXmlResourceHandler::GetSize(const wxString& param)
959{
960 wxString s = GetParamValue(param);
961 if (s.IsEmpty()) s = wxT("-1,-1");
962 bool is_dlg;
963 long sx, sy;
964
965 is_dlg = s[s.Length()-1] == wxT('d');
966 if (is_dlg) s.RemoveLast();
967
968 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
969 !s.AfterLast(wxT(',')).ToLong(&sy))
970 {
00393283 971 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
78d14f80
VS
972 return wxDefaultSize;
973 }
974
975 if (is_dlg)
976 {
977 if (m_instanceAsWindow)
978 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
979 else if (m_parentAsWindow)
980 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
981 else
982 {
983 wxLogError(_("Cannot convert dialog units: dialog unknown."));
984 return wxDefaultSize;
985 }
986 }
987 else return wxSize(sx, sy);
988}
989
990
991
992wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
993{
994 wxSize sz = GetSize(param);
995 return wxPoint(sz.x, sz.y);
996}
997
998
999
1000wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
1001{
1002 wxString s = GetParamValue(param);
1003 if (s.IsEmpty()) return defaultv;
1004 bool is_dlg;
1005 long sx;
1006
1007 is_dlg = s[s.Length()-1] == wxT('d');
1008 if (is_dlg) s.RemoveLast();
1009
1010 if (!s.ToLong(&sx))
1011 {
00393283 1012 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
78d14f80
VS
1013 return defaultv;
1014 }
1015
1016 if (is_dlg)
1017 {
1018 if (m_instanceAsWindow)
1019 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
1020 else if (m_parentAsWindow)
1021 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
1022 else
1023 {
1024 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1025 return defaultv;
1026 }
1027 }
1028 else return sx;
1029}
1030
1031
1032
1033wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1034{
1035 wxXmlNode *font_node = GetParamNode(param);
1036 if (font_node == NULL)
1037 {
00393283 1038 wxLogError(_("Cannot find font node '%s'."), param.c_str());
78d14f80
VS
1039 return wxNullFont;
1040 }
1041
1042 wxXmlNode *oldnode = m_node;
1043 m_node = font_node;
1044
1045 long size = GetLong(wxT("size"), 12);
1046
1047 wxString style = GetParamValue(wxT("style"));
1048 wxString weight = GetParamValue(wxT("weight"));
1049 int istyle = wxNORMAL, iweight = wxNORMAL;
1050 if (style == wxT("italic")) istyle = wxITALIC;
1051 else if (style == wxT("slant")) istyle = wxSLANT;
1052 if (weight == wxT("bold")) iweight = wxBOLD;
1053 else if (weight == wxT("light")) iweight = wxLIGHT;
1054
1055 wxString family = GetParamValue(wxT("family"));
1056 int ifamily = wxDEFAULT;
1057 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1058 else if (family == wxT("roman")) ifamily = wxROMAN;
1059 else if (family == wxT("script")) ifamily = wxSCRIPT;
1060 else if (family == wxT("swiss")) ifamily = wxSWISS;
1061 else if (family == wxT("modern")) ifamily = wxMODERN;
1062
1063 bool underlined = GetBool(wxT("underlined"), FALSE);
1064
1065 wxString encoding = GetParamValue(wxT("encoding"));
1066 wxFontMapper mapper;
1067 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
91cddacf
VS
1068 if (!encoding.IsEmpty())
1069 enc = mapper.CharsetToEncoding(encoding);
1070 if (enc == wxFONTENCODING_SYSTEM)
1071 enc = wxFONTENCODING_DEFAULT;
78d14f80
VS
1072
1073 wxString faces = GetParamValue(wxT("face"));
1074 wxString facename = wxEmptyString;
1075 wxFontEnumerator enu;
1076 enu.EnumerateFacenames();
1077 wxStringTokenizer tk(faces, wxT(","));
1078 while (tk.HasMoreTokens())
1079 {
1080 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
1081 if (index != wxNOT_FOUND)
1082 {
1083 facename = (*enu.GetFacenames())[index];
1084 break;
1085 }
1086 }
1087
1088 m_node = oldnode;
1089
1090 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
1091 return font;
1092}
1093
1094
1095void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1096{
1097 //FIXME : add cursor
1098
1099 if (HasParam(wxT("exstyle")))
1100 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
1101 if (HasParam(wxT("bg")))
1102 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1103 if (HasParam(wxT("fg")))
1104 wnd->SetForegroundColour(GetColour(wxT("fg")));
1105 if (GetBool(wxT("enabled"), 1) == 0)
1106 wnd->Enable(FALSE);
1107 if (GetBool(wxT("focused"), 0) == 1)
1108 wnd->SetFocus();
1109 if (GetBool(wxT("hidden"), 0) == 1)
1110 wnd->Show(FALSE);
1111#if wxUSE_TOOLTIPS
1112 if (HasParam(wxT("tooltip")))
1113 wnd->SetToolTip(GetText(wxT("tooltip")));
1114#endif
1115 if (HasParam(wxT("font")))
1116 wnd->SetFont(GetFont());
1117}
1118
1119
1120void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1121{
1122 wxXmlNode *n = m_node->GetChildren();
1123
1124 while (n)
1125 {
1126 if (n->GetType() == wxXML_ELEMENT_NODE &&
0fa2e104 1127 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
78d14f80 1128 {
317a0d73
VS
1129 m_resource->CreateResFromNode(n, parent, NULL,
1130 this_hnd_only ? this : NULL);
78d14f80
VS
1131 }
1132 n = n->GetNext();
1133 }
1134}
1135
1136
1137void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1138{
1139 wxXmlNode *root;
1140 if (rootnode == NULL) root = m_node; else root = rootnode;
1141 wxXmlNode *n = root->GetChildren();
1142
1143 while (n)
1144 {
1145 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1146 {
1147 CreateResource(n, parent, NULL);
1148 }
1149 n = n->GetNext();
1150 }
1151}
1152
1153
1154
1155
1156
1157
1158
5ed345b7 1159// --------------- XRCID implementation -----------------------------
78d14f80 1160
5ed345b7 1161#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1162
1163
5ed345b7 1164struct XRCID_record
78d14f80
VS
1165{
1166 int id;
00393283 1167 wxChar *key;
5ed345b7 1168 XRCID_record *next;
78d14f80
VS
1169};
1170
5ed345b7 1171static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1172
13de23f6 1173static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2)
78d14f80 1174{
5ed345b7 1175 static int XRCID_LastID = wxID_HIGHEST;
78d14f80
VS
1176
1177 int index = 0;
1178
00393283 1179 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
5ed345b7 1180 index %= XRCID_TABLE_SIZE;
78d14f80 1181
5ed345b7 1182 XRCID_record *oldrec = NULL;
78d14f80 1183 int matchcnt = 0;
5ed345b7 1184 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 1185 {
00393283 1186 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
1187 {
1188 return rec->id;
1189 }
1190 matchcnt++;
1191 oldrec = rec;
1192 }
1193
5ed345b7
VS
1194 XRCID_record **rec_var = (oldrec == NULL) ?
1195 &XRCID_Records[index] : &oldrec->next;
1196 *rec_var = new XRCID_record;
00393283 1197 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
1198 (*rec_var)->next = NULL;
1199
85452d74 1200 wxChar *end;
13de23f6
VS
1201 if (value_if_not_found != -2)
1202 (*rec_var)->id = value_if_not_found;
85452d74
VS
1203 else
1204 {
13de23f6
VS
1205 int asint = wxStrtol(str_id, &end, 10);
1206 if (*str_id && *end == 0)
1207 {
1208 // if str_id was integer, keep it verbosely:
1209 (*rec_var)->id = asint;
1210 }
1211 else
1212 {
1213 (*rec_var)->id = ++XRCID_LastID;
1214 }
85452d74
VS
1215 }
1216
78d14f80
VS
1217 return (*rec_var)->id;
1218}
1219
13de23f6
VS
1220/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1221{
1222 return XRCID_Lookup(str_id);
1223}
1224
78d14f80 1225
5ed345b7 1226static void CleanXRCID_Record(XRCID_record *rec)
78d14f80
VS
1227{
1228 if (rec)
1229 {
5ed345b7 1230 CleanXRCID_Record(rec->next);
00393283 1231 free(rec->key);
78d14f80
VS
1232 delete rec;
1233 }
1234}
1235
5ed345b7 1236static void CleanXRCID_Records()
78d14f80 1237{
5ed345b7 1238 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
139c5871 1239 {
5ed345b7 1240 CleanXRCID_Record(XRCID_Records[i]);
139c5871
VS
1241 XRCID_Records[i] = NULL;
1242 }
78d14f80
VS
1243}
1244
13de23f6
VS
1245static void AddStdXRCID_Records()
1246{
1247#define stdID(id) XRCID_Lookup(wxT(#id), id)
1248 stdID(-1);
1249 stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
1250 stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
1251 stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
1252 stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
1253 stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
1254 stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
1255 stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
1256 stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
1257 stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
1258 stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
1259 stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
1260 stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
1261 stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
1262 stdID(wxID_CLOSE_ALL);
1263#undef stdID
1264}
78d14f80
VS
1265
1266
1267
1268
1269
1270// --------------- module and globals -----------------------------
1271
78d14f80
VS
1272class wxXmlResourceModule: public wxModule
1273{
1274DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1275public:
1276 wxXmlResourceModule() {}
824e8eaa
VS
1277 bool OnInit()
1278 {
13de23f6 1279 AddStdXRCID_Records();
2b5f62a0 1280 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
824e8eaa
VS
1281 return TRUE;
1282 }
78d14f80
VS
1283 void OnExit()
1284 {
1542c42e 1285 delete wxXmlResource::Set(NULL);
461932ae
MB
1286 if(wxXmlResource::ms_subclassFactories)
1287 WX_CLEAR_LIST(wxXmlSubclassFactoriesList, *wxXmlResource::ms_subclassFactories);
2b5f62a0 1288 wxDELETE(wxXmlResource::ms_subclassFactories);
5ed345b7 1289 CleanXRCID_Records();
78d14f80
VS
1290 }
1291};
1292
1293IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1294
1295
1296// When wxXml is loaded dynamically after the application is already running
1297// then the built-in module system won't pick this one up. Add it manually.
1298void wxXmlInitResourceModule()
1299{
1300 wxModule* module = new wxXmlResourceModule;
1301 module->Init();
1302 wxModule::RegisterModule(module);
1303}