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