]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
remove unused variables, minor cleanup
[wxWidgets.git] / src / xrc / xmlres.cpp
CommitLineData
78d14f80 1/////////////////////////////////////////////////////////////////////////////
88a7a4e1 2// Name: src/xrc/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
78d14f80
VS
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifdef __BORLANDC__
15 #pragma hdrstop
16#endif
17
621be1ec 18#if wxUSE_XRC
a1e4ec87 19
88a7a4e1
WS
20#include "wx/xrc/xmlres.h"
21
22#ifndef WX_PRECOMP
23 #include "wx/intl.h"
e4db172a 24 #include "wx/log.h"
8e609c82 25 #include "wx/panel.h"
76b49cf4 26 #include "wx/frame.h"
fdf565fe 27 #include "wx/dialog.h"
9eddec69 28 #include "wx/settings.h"
0bca0373 29 #include "wx/bitmap.h"
155ecd4c 30 #include "wx/image.h"
02761f6c 31 #include "wx/module.h"
193d0c93 32 #include "wx/wxcrtvararg.h"
88a7a4e1
WS
33#endif
34
e7a3a5a5 35#ifndef __WXWINCE__
88a7a4e1 36 #include <locale.h>
e7a3a5a5 37#endif
1df61962 38
eb2d0d23 39#include "wx/vector.h"
78d14f80
VS
40#include "wx/wfstream.h"
41#include "wx/filesys.h"
317a0d73 42#include "wx/filename.h"
78d14f80
VS
43#include "wx/tokenzr.h"
44#include "wx/fontenum.h"
78d14f80 45#include "wx/fontmap.h"
af1337b0 46#include "wx/artprov.h"
326462ae 47#include "wx/imaglist.h"
2bb9a404 48#include "wx/dir.h"
34c6bbee 49#include "wx/xml/xml.h"
78d14f80 50
f35fdf7e 51
eb2d0d23
VS
52class wxXmlResourceDataRecord
53{
54public:
55 wxXmlResourceDataRecord() : Doc(NULL) {
56#if wxUSE_DATETIME
57 Time = wxDateTime::Now();
58#endif
59 }
60 ~wxXmlResourceDataRecord() {delete Doc;}
61
62 wxString File;
63 wxXmlDocument *Doc;
64#if wxUSE_DATETIME
65 wxDateTime Time;
66#endif
67};
68
f396e5a7 69class wxXmlResourceDataRecords : public wxVector<wxXmlResourceDataRecord*>
eb2d0d23
VS
70{
71 // this is a class so that it can be forward-declared
72};
78d14f80 73
23239d94
VZ
74namespace
75{
76
77// helper used by DoFindResource() and elsewhere: returns true if this is an
78// object or object_ref node
79//
80// node must be non-NULL
81inline bool IsObjectNode(wxXmlNode *node)
82{
83 return node->GetType() == wxXML_ELEMENT_NODE &&
84 (node->GetName() == wxS("object") ||
85 node->GetName() == wxS("object_ref"));
86}
87
a1eeda0b
VS
88// special XML attribute with name of input file, see GetFileNameFromNode()
89const char *ATTR_INPUT_FILENAME = "__wx:filename";
90
91// helper to get filename corresponding to an XML node
92wxString
93GetFileNameFromNode(wxXmlNode *node, const wxXmlResourceDataRecords& files)
94{
95 // this loop does two things: it looks for ATTR_INPUT_FILENAME among
96 // parents and if it isn't used, it finds the root of the XML tree 'node'
97 // is in
98 for ( ;; )
99 {
100 // in some rare cases (specifically, when an <object_ref> is used, see
101 // wxXmlResource::CreateResFromNode() and MergeNodesOver()), we work
102 // with XML nodes that are not rooted in any document from 'files'
103 // (because a new node was created by CreateResFromNode() to merge the
104 // content of <object_ref> and the referenced <object>); in that case,
105 // we hack around the problem by putting the information about input
106 // file into a custom attribute
107 if ( node->HasAttribute(ATTR_INPUT_FILENAME) )
108 return node->GetAttribute(ATTR_INPUT_FILENAME);
109
110 if ( !node->GetParent() )
111 break; // we found the root of this XML tree
112
113 node = node->GetParent();
114 }
115
116 // NB: 'node' now points to the root of XML document
117
118 for ( wxXmlResourceDataRecords::const_iterator i = files.begin();
119 i != files.end(); ++i )
120 {
121 if ( (*i)->Doc->GetRoot() == node )
122 {
123 return (*i)->File;
124 }
125 }
126
127 return wxEmptyString; // not found
128}
129
23239d94
VZ
130} // anonymous namespace
131
78d14f80 132
824e8eaa
VS
133wxXmlResource *wxXmlResource::ms_instance = NULL;
134
135/*static*/ wxXmlResource *wxXmlResource::Get()
136{
137 if ( !ms_instance )
138 ms_instance = new wxXmlResource;
139 return ms_instance;
140}
141
142/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
143{
144 wxXmlResource *old = ms_instance;
145 ms_instance = res;
146 return old;
147}
148
d4a724d4 149wxXmlResource::wxXmlResource(int flags, const wxString& domain)
78d14f80 150{
daa85ee3 151 m_flags = flags;
78d14f80 152 m_version = -1;
eb2d0d23 153 m_data = new wxXmlResourceDataRecords;
d7a80cf5 154 SetDomain(domain);
78d14f80
VS
155}
156
d4a724d4 157wxXmlResource::wxXmlResource(const wxString& filemask, int flags, const wxString& domain)
78d14f80 158{
daa85ee3 159 m_flags = flags;
78d14f80 160 m_version = -1;
eb2d0d23 161 m_data = new wxXmlResourceDataRecords;
d7a80cf5 162 SetDomain(domain);
78d14f80
VS
163 Load(filemask);
164}
165
166wxXmlResource::~wxXmlResource()
167{
168 ClearHandlers();
eb2d0d23 169
f396e5a7
VS
170 for ( wxXmlResourceDataRecords::iterator i = m_data->begin();
171 i != m_data->end(); ++i )
172 {
173 delete *i;
174 }
eb2d0d23 175 delete m_data;
78d14f80
VS
176}
177
d7a80cf5 178void wxXmlResource::SetDomain(const wxString& domain)
d4a724d4 179{
d7a80cf5 180 m_domain = domain;
d4a724d4
RD
181}
182
78d14f80 183
60fd818a
VZ
184/* static */
185wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename)
186{
187 wxString fnd(filename);
188
189 // NB: as Load() and Unload() accept both filenames and URLs (should
190 // probably be changed to filenames only, but embedded resources
191 // currently rely on its ability to handle URLs - FIXME) we need to
192 // determine whether found name is filename and not URL and this is the
193 // fastest/simplest way to do it
194 if (wxFileName::FileExists(fnd))
195 {
196 // Make the name absolute filename, because the app may
197 // change working directory later:
198 wxFileName fn(fnd);
199 if (fn.IsRelative())
200 {
201 fn.MakeAbsolute();
202 fnd = fn.GetFullPath();
203 }
204#if wxUSE_FILESYSTEM
205 fnd = wxFileSystem::FileNameToURL(fnd);
206#endif
207 }
208
209 return fnd;
210}
211
212#if wxUSE_FILESYSTEM
213
214/* static */
215bool wxXmlResource::IsArchive(const wxString& filename)
216{
217 const wxString fnd = filename.Lower();
218
219 return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs"));
220}
221
222#endif // wxUSE_FILESYSTEM
223
04ae32cd
VS
224bool wxXmlResource::LoadFile(const wxFileName& file)
225{
159852ae 226#if wxUSE_FILESYSTEM
04ae32cd 227 return Load(wxFileSystem::FileNameToURL(file));
159852ae
VS
228#else
229 return Load(file.GetFullPath());
230#endif
04ae32cd
VS
231}
232
2bb9a404
VS
233bool wxXmlResource::LoadAllFiles(const wxString& dirname)
234{
235 bool ok = true;
236 wxArrayString files;
237
238 wxDir::GetAllFiles(dirname, &files, "*.xrc");
239
240 for ( wxArrayString::const_iterator i = files.begin(); i != files.end(); ++i )
241 {
242 if ( !LoadFile(*i) )
243 ok = false;
244 }
245
246 return ok;
247}
248
acd32ffc 249bool wxXmlResource::Load(const wxString& filemask_)
78d14f80 250{
acd32ffc
VS
251 wxString filemask = ConvertFileNameToURL(filemask_);
252
78d14f80
VS
253#if wxUSE_FILESYSTEM
254 wxFileSystem fsys;
255# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
256# define wxXmlFindNext fsys.FindNext()
257#else
258# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
259# define wxXmlFindNext wxFindNextFile()
260#endif
e5bb501b
VZ
261 wxString fnd = wxXmlFindFirst;
262 if ( fnd.empty() )
263 {
264 wxLogError(_("Cannot load resources from '%s'."), filemask);
265 return false;
266 }
267
ec157c8f 268 while (!fnd.empty())
78d14f80
VS
269 {
270#if wxUSE_FILESYSTEM
60fd818a 271 if ( IsArchive(fnd) )
78d14f80 272 {
e5bb501b
VZ
273 if ( !Load(fnd + wxT("#zip:*.xrc")) )
274 return false;
78d14f80 275 }
60fd818a
VZ
276 else // a single resource URL
277#endif // wxUSE_FILESYSTEM
78d14f80 278 {
f396e5a7
VS
279 wxXmlResourceDataRecord *drec = new wxXmlResourceDataRecord;
280 drec->File = fnd;
eb2d0d23 281 Data().push_back(drec);
78d14f80
VS
282 }
283
e5bb501b 284 fnd = wxXmlFindNext;
78d14f80
VS
285 }
286# undef wxXmlFindFirst
287# undef wxXmlFindNext
e5bb501b
VZ
288
289 return UpdateResources();
78d14f80
VS
290}
291
60fd818a
VZ
292bool wxXmlResource::Unload(const wxString& filename)
293{
294 wxASSERT_MSG( !wxIsWild(filename),
9a83f860 295 wxT("wildcards not supported by wxXmlResource::Unload()") );
60fd818a
VZ
296
297 wxString fnd = ConvertFileNameToURL(filename);
298#if wxUSE_FILESYSTEM
299 const bool isArchive = IsArchive(fnd);
300 if ( isArchive )
9a83f860 301 fnd += wxT("#zip:");
60fd818a
VZ
302#endif // wxUSE_FILESYSTEM
303
304 bool unloaded = false;
eb2d0d23
VS
305 for ( wxXmlResourceDataRecords::iterator i = Data().begin();
306 i != Data().end(); ++i )
60fd818a
VZ
307 {
308#if wxUSE_FILESYSTEM
309 if ( isArchive )
310 {
f396e5a7 311 if ( (*i)->File.StartsWith(fnd) )
60fd818a
VZ
312 unloaded = true;
313 // don't break from the loop, we can have other matching files
314 }
315 else // a single resource URL
316#endif // wxUSE_FILESYSTEM
317 {
f396e5a7 318 if ( (*i)->File == fnd )
60fd818a 319 {
f8b1df0b 320 delete *i;
eb2d0d23 321 Data().erase(i);
60fd818a
VZ
322 unloaded = true;
323
324 // no sense in continuing, there is only one file with this URL
325 break;
326 }
327 }
328 }
329
330 return unloaded;
331}
332
78d14f80 333
854e189f 334IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)
78d14f80
VS
335
336void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
337{
eb2d0d23 338 m_handlers.push_back(handler);
78d14f80
VS
339 handler->SetParentResource(this);
340}
341
92e898b0
RD
342void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
343{
eb2d0d23 344 m_handlers.insert(m_handlers.begin(), handler);
92e898b0
RD
345 handler->SetParentResource(this);
346}
347
78d14f80
VS
348
349
350void wxXmlResource::ClearHandlers()
351{
eb2d0d23
VS
352 for ( wxVector<wxXmlResourceHandler*>::iterator i = m_handlers.begin();
353 i != m_handlers.end(); ++i )
354 delete *i;
355 m_handlers.clear();
78d14f80
VS
356}
357
358
78d14f80
VS
359wxMenu *wxXmlResource::LoadMenu(const wxString& name)
360{
361 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
362}
363
364
365
4a1b9596 366wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 367{
4a1b9596 368 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
369}
370
371
372
4a1b9596 373#if wxUSE_TOOLBAR
78d14f80
VS
374wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
375{
376 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
377}
4a1b9596 378#endif
78d14f80
VS
379
380
381wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
382{
4dd75a6a 383 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
78d14f80
VS
384}
385
386bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
387{
388 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
389}
390
391
392
393wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
394{
395 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
396}
397
398bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
399{
400 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
401}
402
92e898b0
RD
403wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
404{
405 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
406}
407
78d14f80
VS
408bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
409{
410 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
411}
412
413wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
414{
415 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
416 FindResource(name, wxT("wxBitmap")), NULL, NULL);
417 wxBitmap rt;
418
419 if (bmp) { rt = *bmp; delete bmp; }
420 return rt;
421}
422
423wxIcon wxXmlResource::LoadIcon(const wxString& name)
424{
425 wxIcon *icon = (wxIcon*)CreateResFromNode(
426 FindResource(name, wxT("wxIcon")), NULL, NULL);
427 wxIcon rt;
428
429 if (icon) { rt = *icon; delete icon; }
430 return rt;
431}
432
92e898b0 433
af0ac990
VZ
434wxObject *
435wxXmlResource::DoLoadObject(wxWindow *parent,
436 const wxString& name,
437 const wxString& classname,
438 bool recursive)
92e898b0 439{
af0ac990
VZ
440 wxXmlNode * const node = FindResource(name, classname, recursive);
441
442 return node ? DoCreateResFromNode(*node, parent, NULL) : NULL;
92e898b0
RD
443}
444
af0ac990
VZ
445bool
446wxXmlResource::DoLoadObject(wxObject *instance,
447 wxWindow *parent,
448 const wxString& name,
449 const wxString& classname,
450 bool recursive)
92e898b0 451{
af0ac990
VZ
452 wxXmlNode * const node = FindResource(name, classname, recursive);
453
454 return node && DoCreateResFromNode(*node, parent, instance) != NULL;
92e898b0
RD
455}
456
457
78d14f80
VS
458bool wxXmlResource::AttachUnknownControl(const wxString& name,
459 wxWindow *control, wxWindow *parent)
460{
461 if (parent == NULL)
462 parent = control->GetParent();
463 wxWindow *container = parent->FindWindow(name + wxT("_container"));
464 if (!container)
465 {
819559b2 466 wxLogError("Cannot find container for unknown control '%s'.", name);
f80ea77b 467 return false;
78d14f80
VS
468 }
469 return control->Reparent(container);
470}
471
472
77b2f9b1 473static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
474{
475 wxString s;
476 bool isok;
477
478 wxXmlNode *c = node->GetChildren();
479 while (c)
480 {
f80ea77b 481 isok = false;
288b6107 482 if (!c->GetAttribute(wxT("platform"), &s))
f80ea77b 483 isok = true;
78d14f80
VS
484 else
485 {
2b5f62a0 486 wxStringTokenizer tkn(s, wxT(" |"));
78d14f80
VS
487
488 while (tkn.HasMoreTokens())
489 {
490 s = tkn.GetNextToken();
84389518 491#ifdef __WINDOWS__
d003330c
VS
492 if (s == wxT("win")) isok = true;
493#endif
b380439d 494#if defined(__MAC__) || defined(__APPLE__)
d003330c 495 if (s == wxT("mac")) isok = true;
b380439d
RD
496#elif defined(__UNIX__)
497 if (s == wxT("unix")) isok = true;
78d14f80 498#endif
d003330c
VS
499#ifdef __OS2__
500 if (s == wxT("os2")) isok = true;
501#endif
502
503 if (isok)
504 break;
78d14f80
VS
505 }
506 }
507
508 if (isok)
d7b1d73c 509 {
78d14f80 510 ProcessPlatformProperty(c);
d7b1d73c
VS
511 c = c->GetNext();
512 }
78d14f80
VS
513 else
514 {
d7b1d73c 515 wxXmlNode *c2 = c->GetNext();
db59a97c 516 node->RemoveChild(c);
78d14f80 517 delete c;
d7b1d73c 518 c = c2;
78d14f80 519 }
78d14f80
VS
520 }
521}
522
523
524
d614f51b 525bool wxXmlResource::UpdateResources()
78d14f80 526{
d614f51b 527 bool rt = true;
78d14f80
VS
528 bool modif;
529# if wxUSE_FILESYSTEM
530 wxFSFile *file = NULL;
19d0f58d 531 wxUnusedVar(file);
78d14f80
VS
532 wxFileSystem fsys;
533# endif
534
480505bc
VS
535 wxString encoding(wxT("UTF-8"));
536#if !wxUSE_UNICODE && wxUSE_INTL
537 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
538 {
75d38380
VS
539 // In case we are not using wxLocale to translate strings, convert the
540 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
541 // is on, because it could break wxGetTranslation lookup.
480505bc
VS
542 encoding = wxLocale::GetSystemEncodingName();
543 }
544#endif
545
eb2d0d23
VS
546 for ( wxXmlResourceDataRecords::iterator i = Data().begin();
547 i != Data().end(); ++i )
78d14f80 548 {
f396e5a7
VS
549 wxXmlResourceDataRecord* const rec = *i;
550
551 modif = (rec->Doc == NULL);
78d14f80 552
648db587 553 if (!modif && !(m_flags & wxXRC_NO_RELOADING))
78d14f80
VS
554 {
555# if wxUSE_FILESYSTEM
f396e5a7 556 file = fsys.OpenFile(rec->File);
34af0de4 557# if wxUSE_DATETIME
f396e5a7 558 modif = file && file->GetModificationTime() > rec->Time;
34af0de4
VZ
559# else // wxUSE_DATETIME
560 modif = true;
561# endif // wxUSE_DATETIME
78d14f80 562 if (!file)
d614f51b 563 {
f396e5a7 564 wxLogError(_("Cannot open file '%s'."), rec->File);
d614f51b
VS
565 rt = false;
566 }
78d14f80 567 wxDELETE(file);
19d0f58d 568 wxUnusedVar(file);
34af0de4
VZ
569# else // wxUSE_FILESYSTEM
570# if wxUSE_DATETIME
f396e5a7 571 modif = wxDateTime(wxFileModificationTime(rec->File)) > rec->Time;
34af0de4
VZ
572# else // wxUSE_DATETIME
573 modif = true;
574# endif // wxUSE_DATETIME
575# endif // wxUSE_FILESYSTEM
78d14f80
VS
576 }
577
578 if (modif)
579 {
9a83f860 580 wxLogTrace(wxT("xrc"), wxT("opening file '%s'"), rec->File);
648db587 581
480505bc 582 wxInputStream *stream = NULL;
78d14f80
VS
583
584# if wxUSE_FILESYSTEM
f396e5a7 585 file = fsys.OpenFile(rec->File);
f80ea77b
WS
586 if (file)
587 stream = file->GetStream();
78d14f80 588# else
f396e5a7 589 stream = new wxFileInputStream(rec->File);
78d14f80
VS
590# endif
591
592 if (stream)
593 {
f396e5a7
VS
594 delete rec->Doc;
595 rec->Doc = new wxXmlDocument;
78d14f80 596 }
9690b006 597 if (!stream || !stream->IsOk() || !rec->Doc->Load(*stream, encoding))
78d14f80 598 {
480505bc 599 wxLogError(_("Cannot load resources from file '%s'."),
f396e5a7
VS
600 rec->File);
601 wxDELETE(rec->Doc);
d614f51b 602 rt = false;
78d14f80 603 }
f396e5a7 604 else if (rec->Doc->GetRoot()->GetName() != wxT("resource"))
78d14f80 605 {
819559b2
VS
606 ReportError
607 (
608 rec->Doc->GetRoot(),
609 "invalid XRC resource, doesn't have root node <resource>"
610 );
f396e5a7 611 wxDELETE(rec->Doc);
d614f51b 612 rt = false;
78d14f80
VS
613 }
614 else
f80ea77b 615 {
78d14f80
VS
616 long version;
617 int v1, v2, v3, v4;
f396e5a7 618 wxString verstr = rec->Doc->GetRoot()->GetAttribute(
78d14f80
VS
619 wxT("version"), wxT("0.0.0.0"));
620 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
621 &v1, &v2, &v3, &v4) == 4)
622 version = v1*256*256*256+v2*256*256+v3*256+v4;
623 else
624 version = 0;
625 if (m_version == -1)
626 m_version = version;
627 if (m_version != version)
d614f51b 628 {
819559b2 629 wxLogError("Resource files must have same version number.");
d614f51b
VS
630 rt = false;
631 }
78d14f80 632
f396e5a7 633 ProcessPlatformProperty(rec->Doc->GetRoot());
34af0de4 634#if wxUSE_DATETIME
496f0a58 635#if wxUSE_FILESYSTEM
f396e5a7 636 rec->Time = file->GetModificationTime();
34af0de4 637#else // wxUSE_FILESYSTEM
f396e5a7 638 rec->Time = wxDateTime(wxFileModificationTime(rec->File));
34af0de4
VZ
639#endif // wxUSE_FILESYSTEM
640#endif // wxUSE_DATETIME
f80ea77b 641 }
78d14f80
VS
642
643# if wxUSE_FILESYSTEM
f80ea77b
WS
644 wxDELETE(file);
645 wxUnusedVar(file);
78d14f80 646# else
f80ea77b 647 wxDELETE(stream);
78d14f80
VS
648# endif
649 }
650 }
d614f51b
VS
651
652 return rt;
78d14f80
VS
653}
654
b272b6dc
RD
655wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
656 const wxString& name,
657 const wxString& classname,
23239d94 658 bool recursive) const
47793ab8 659{
47793ab8
VS
660 wxXmlNode *node;
661
662 // first search for match at the top-level nodes (as this is
663 // where the resource is most commonly looked for):
664 for (node = parent->GetChildren(); node; node = node->GetNext())
665 {
23239d94 666 if ( IsObjectNode(node) && node->GetAttribute(wxS("name")) == name )
2b5f62a0 667 {
23239d94
VZ
668 // empty class name matches everything
669 if ( classname.empty() )
2b5f62a0 670 return node;
23239d94
VZ
671
672 wxString cls(node->GetAttribute(wxS("class")));
673
288b6107 674 // object_ref may not have 'class' attribute:
23239d94 675 if (cls.empty() && node->GetName() == wxS("object_ref"))
2b5f62a0 676 {
23239d94 677 wxString refName = node->GetAttribute(wxS("ref"));
2b5f62a0
VZ
678 if (refName.empty())
679 continue;
23239d94
VZ
680
681 const wxXmlNode * const refNode = GetResourceNode(refName);
682 if ( refNode )
683 cls = refNode->GetAttribute(wxS("class"));
2b5f62a0 684 }
23239d94
VZ
685
686 if ( cls == classname )
687 return node;
2b5f62a0 688 }
47793ab8
VS
689 }
690
23239d94 691 // then recurse in child nodes
47793ab8 692 if ( recursive )
23239d94 693 {
47793ab8
VS
694 for (node = parent->GetChildren(); node; node = node->GetNext())
695 {
23239d94 696 if ( IsObjectNode(node) )
47793ab8 697 {
f80ea77b 698 wxXmlNode* found = DoFindResource(node, name, classname, true);
47793ab8
VS
699 if ( found )
700 return found;
701 }
702 }
23239d94 703 }
47793ab8 704
23239d94 705 return NULL;
47793ab8 706}
78d14f80 707
b272b6dc 708wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
709 const wxString& classname,
710 bool recursive)
78d14f80 711{
23239d94
VZ
712 wxString path;
713 wxXmlNode * const
714 node = GetResourceNodeAndLocation(name, classname, recursive, &path);
715
716 if ( !node )
717 {
819559b2
VS
718 ReportError
719 (
720 NULL,
721 wxString::Format
722 (
723 "XRC resource \"%s\" (class \"%s\") not found",
724 name, classname
725 )
726 );
23239d94
VZ
727 }
728#if wxUSE_FILESYSTEM
729 else // node was found
730 {
731 // ensure that relative paths work correctly when loading this node
732 // (which should happen as soon as we return as FindResource() result
733 // is always passed to CreateResFromNode())
734 m_curFileSystem.ChangePathTo(path);
735 }
736#endif // wxUSE_FILESYSTEM
737
738 return node;
739}
740
741wxXmlNode *
742wxXmlResource::GetResourceNodeAndLocation(const wxString& name,
743 const wxString& classname,
744 bool recursive,
745 wxString *path) const
746{
747 // ensure everything is up-to-date: this is needed to support on-remand
748 // reloading of XRC files
749 const_cast<wxXmlResource *>(this)->UpdateResources();
78d14f80 750
eb2d0d23
VS
751 for ( wxXmlResourceDataRecords::const_iterator f = Data().begin();
752 f != Data().end(); ++f )
78d14f80 753 {
23239d94
VZ
754 wxXmlResourceDataRecord *const rec = *f;
755 wxXmlDocument * const doc = rec->Doc;
756 if ( !doc || !doc->GetRoot() )
47793ab8
VS
757 continue;
758
23239d94
VZ
759 wxXmlNode * const
760 found = DoFindResource(doc->GetRoot(), name, classname, recursive);
47793ab8
VS
761 if ( found )
762 {
23239d94
VZ
763 if ( path )
764 *path = rec->File;
765
47793ab8
VS
766 return found;
767 }
78d14f80
VS
768 }
769
78d14f80
VS
770 return NULL;
771}
772
a1eeda0b
VS
773static void MergeNodesOver(wxXmlNode& dest, wxXmlNode& overwriteWith,
774 const wxString& overwriteFilename)
47793ab8 775{
288b6107 776 // Merge attributes:
a1eeda0b 777 for ( wxXmlAttribute *attr = overwriteWith.GetAttributes();
288b6107 778 attr; attr = attr->GetNext() )
47793ab8 779 {
288b6107
VS
780 wxXmlAttribute *dattr;
781 for (dattr = dest.GetAttributes(); dattr; dattr = dattr->GetNext())
47793ab8 782 {
b272b6dc 783
288b6107 784 if ( dattr->GetName() == attr->GetName() )
47793ab8 785 {
288b6107 786 dattr->SetValue(attr->GetValue());
47793ab8
VS
787 break;
788 }
789 }
78d14f80 790
288b6107
VS
791 if ( !dattr )
792 dest.AddAttribute(attr->GetName(), attr->GetValue());
47793ab8
VS
793 }
794
795 // Merge child nodes:
a1eeda0b 796 for (wxXmlNode* node = overwriteWith.GetChildren(); node; node = node->GetNext())
47793ab8 797 {
288b6107 798 wxString name = node->GetAttribute(wxT("name"), wxEmptyString);
47793ab8
VS
799 wxXmlNode *dnode;
800
801 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
802 {
803 if ( dnode->GetName() == node->GetName() &&
288b6107 804 dnode->GetAttribute(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
805 dnode->GetType() == node->GetType() )
806 {
a1eeda0b 807 MergeNodesOver(*dnode, *node, overwriteFilename);
47793ab8
VS
808 break;
809 }
810 }
811
812 if ( !dnode )
b26a650c 813 {
a1eeda0b
VS
814 wxXmlNode *copyOfNode = new wxXmlNode(*node);
815 // remember referenced object's file, see GetFileNameFromNode()
816 copyOfNode->AddAttribute(ATTR_INPUT_FILENAME, overwriteFilename);
817
b26a650c 818 static const wxChar *AT_END = wxT("end");
288b6107 819 wxString insert_pos = node->GetAttribute(wxT("insert_at"), AT_END);
b26a650c
VZ
820 if ( insert_pos == AT_END )
821 {
a1eeda0b 822 dest.AddChild(copyOfNode);
b26a650c
VZ
823 }
824 else if ( insert_pos == wxT("begin") )
825 {
a1eeda0b 826 dest.InsertChild(copyOfNode, dest.GetChildren());
b26a650c
VZ
827 }
828 }
47793ab8
VS
829 }
830
a1eeda0b
VS
831 if ( dest.GetType() == wxXML_TEXT_NODE && overwriteWith.GetContent().length() )
832 dest.SetContent(overwriteWith.GetContent());
47793ab8 833}
78d14f80 834
af0ac990
VZ
835wxObject *
836wxXmlResource::DoCreateResFromNode(wxXmlNode& node,
837 wxObject *parent,
838 wxObject *instance,
839 wxXmlResourceHandler *handlerToUse)
78d14f80 840{
47793ab8 841 // handling of referenced resource
af0ac990 842 if ( node.GetName() == wxT("object_ref") )
47793ab8 843 {
af0ac990 844 wxString refName = node.GetAttribute(wxT("ref"), wxEmptyString);
f80ea77b 845 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
47793ab8
VS
846
847 if ( !refNode )
848 {
819559b2
VS
849 ReportError
850 (
af0ac990 851 &node,
819559b2
VS
852 wxString::Format
853 (
854 "referenced object node with ref=\"%s\" not found",
855 refName
856 )
857 );
47793ab8
VS
858 return NULL;
859 }
860
af0ac990 861 if ( !node.GetChildren() )
44108a07
VS
862 {
863 // In the typical, simple case, <object_ref> is used to link
864 // to another node and doesn't have any content of its own that
865 // would overwrite linked object's properties. In this case,
866 // we can simply create the resource from linked node.
867
af0ac990 868 return DoCreateResFromNode(*refNode, parent, instance);
44108a07
VS
869 }
870 else
871 {
872 // In the more complicated (but rare) case, <object_ref> has
873 // subnodes that partially overwrite content of the referenced
874 // object. In this case, we need to merge both XML trees and
875 // load the resource from result of the merge.
876
877 wxXmlNode copy(*refNode);
af0ac990 878 MergeNodesOver(copy, node, GetFileNameFromNode(&node, Data()));
a1eeda0b 879
44108a07
VS
880 // remember referenced object's file, see GetFileNameFromNode()
881 copy.AddAttribute(ATTR_INPUT_FILENAME,
882 GetFileNameFromNode(refNode, Data()));
47793ab8 883
af0ac990 884 return DoCreateResFromNode(copy, parent, instance);
44108a07 885 }
47793ab8
VS
886 }
887
317a0d73 888 if (handlerToUse)
b380439d 889 {
af0ac990 890 if (handlerToUse->CanHandle(&node))
8e8a4e85 891 {
af0ac990 892 return handlerToUse->CreateResource(&node, parent, instance);
8e8a4e85 893 }
317a0d73 894 }
af0ac990 895 else if (node.GetName() == wxT("object"))
b380439d 896 {
eb2d0d23
VS
897 for ( wxVector<wxXmlResourceHandler*>::iterator h = m_handlers.begin();
898 h != m_handlers.end(); ++h )
317a0d73 899 {
eb2d0d23 900 wxXmlResourceHandler *handler = *h;
af0ac990
VZ
901 if (handler->CanHandle(&node))
902 return handler->CreateResource(&node, parent, instance);
78d14f80 903 }
78d14f80
VS
904 }
905
819559b2
VS
906 ReportError
907 (
af0ac990 908 &node,
819559b2
VS
909 wxString::Format
910 (
911 "no handler found for XML node \"%s\" (class \"%s\")",
af0ac990
VZ
912 node.GetName(),
913 node.GetAttribute("class", wxEmptyString)
819559b2
VS
914 )
915 );
78d14f80
VS
916 return NULL;
917}
918
919
eb2d0d23
VS
920class wxXmlSubclassFactories : public wxVector<wxXmlSubclassFactory*>
921{
922 // this is a class so that it can be forward-declared
923};
2b5f62a0 924
eb2d0d23 925wxXmlSubclassFactories *wxXmlResource::ms_subclassFactories = NULL;
2b5f62a0
VZ
926
927/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
928{
929 if (!ms_subclassFactories)
930 {
eb2d0d23 931 ms_subclassFactories = new wxXmlSubclassFactories;
2b5f62a0 932 }
eb2d0d23 933 ms_subclassFactories->push_back(factory);
2b5f62a0
VZ
934}
935
936class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
937{
938public:
939 ~wxXmlSubclassFactoryCXX() {}
940
941 wxObject *Create(const wxString& className)
942 {
943 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
944
945 if (classInfo)
946 return classInfo->CreateObject();
947 else
948 return NULL;
949 }
950};
951
952
953
78d14f80 954
78d14f80
VS
955wxXmlResourceHandler::wxXmlResourceHandler()
956 : m_node(NULL), m_parent(NULL), m_instance(NULL),
9a8d8c5a 957 m_parentAsWindow(NULL)
78d14f80
VS
958{}
959
960
961
962wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
963{
964 wxXmlNode *myNode = m_node;
965 wxString myClass = m_class;
966 wxObject *myParent = m_parent, *myInstance = m_instance;
9a8d8c5a 967 wxWindow *myParentAW = m_parentAsWindow;
78d14f80 968
daa85ee3 969 m_instance = instance;
288b6107 970 if (!m_instance && node->HasAttribute(wxT("subclass")) &&
daa85ee3
VS
971 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
972 {
288b6107 973 wxString subclass = node->GetAttribute(wxT("subclass"), wxEmptyString);
2b5f62a0 974 if (!subclass.empty())
daa85ee3 975 {
eb2d0d23
VS
976 for (wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin();
977 i != wxXmlResource::ms_subclassFactories->end(); ++i)
2b5f62a0 978 {
eb2d0d23 979 m_instance = (*i)->Create(subclass);
2b5f62a0
VZ
980 if (m_instance)
981 break;
982 }
daa85ee3 983
2b5f62a0
VZ
984 if (!m_instance)
985 {
288b6107 986 wxString name = node->GetAttribute(wxT("name"), wxEmptyString);
819559b2
VS
987 ReportError
988 (
989 node,
990 wxString::Format
991 (
992 "subclass \"%s\" not found for resource \"%s\", not subclassing",
993 subclass, name
994 )
995 );
2b5f62a0
VZ
996 }
997 }
daa85ee3
VS
998 }
999
78d14f80 1000 m_node = node;
288b6107 1001 m_class = node->GetAttribute(wxT("class"), wxEmptyString);
78d14f80 1002 m_parent = parent;
78d14f80 1003 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
78d14f80
VS
1004
1005 wxObject *returned = DoCreateResource();
1006
1007 m_node = myNode;
1008 m_class = myClass;
1009 m_parent = myParent; m_parentAsWindow = myParentAW;
9a8d8c5a 1010 m_instance = myInstance;
78d14f80
VS
1011
1012 return returned;
1013}
1014
1015
1016void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
1017{
1018 m_styleNames.Add(name);
1019 m_styleValues.Add(value);
1020}
1021
1022
1023
1024void wxXmlResourceHandler::AddWindowStyles()
1025{
9dc579b3 1026 XRC_ADD_STYLE(wxCLIP_CHILDREN);
d54a3e73
VZ
1027
1028 // the border styles all have the old and new names, recognize both for now
1029 XRC_ADD_STYLE(wxSIMPLE_BORDER); XRC_ADD_STYLE(wxBORDER_SIMPLE);
1030 XRC_ADD_STYLE(wxSUNKEN_BORDER); XRC_ADD_STYLE(wxBORDER_SUNKEN);
b37e592b
JS
1031 XRC_ADD_STYLE(wxDOUBLE_BORDER); XRC_ADD_STYLE(wxBORDER_DOUBLE); // deprecated
1032 XRC_ADD_STYLE(wxBORDER_THEME);
d54a3e73
VZ
1033 XRC_ADD_STYLE(wxRAISED_BORDER); XRC_ADD_STYLE(wxBORDER_RAISED);
1034 XRC_ADD_STYLE(wxSTATIC_BORDER); XRC_ADD_STYLE(wxBORDER_STATIC);
1035 XRC_ADD_STYLE(wxNO_BORDER); XRC_ADD_STYLE(wxBORDER_NONE);
1036
daa85ee3
VS
1037 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
1038 XRC_ADD_STYLE(wxWANTS_CHARS);
43840d8b 1039 XRC_ADD_STYLE(wxTAB_TRAVERSAL);
daa85ee3 1040 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
7539ba56 1041 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
162a4f93 1042 XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
9f4ed861 1043 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
b0802e64 1044 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
78d14f80
VS
1045}
1046
1047
1048
1049bool wxXmlResourceHandler::HasParam(const wxString& param)
1050{
1051 return (GetParamNode(param) != NULL);
1052}
1053
1054
1055int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
1056{
1057 wxString s = GetParamValue(param);
1058
1059 if (!s) return defaults;
1060
2b5f62a0 1061 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
1062 int style = 0;
1063 int index;
1064 wxString fl;
1065 while (tkn.HasMoreTokens())
1066 {
1067 fl = tkn.GetNextToken();
1068 index = m_styleNames.Index(fl);
1069 if (index != wxNOT_FOUND)
819559b2 1070 {
78d14f80 1071 style |= m_styleValues[index];
819559b2 1072 }
78d14f80 1073 else
819559b2
VS
1074 {
1075 ReportParamError
1076 (
1077 param,
1078 wxString::Format("unknown style flag \"%s\"", fl)
1079 );
1080 }
78d14f80
VS
1081 }
1082 return style;
1083}
1084
1085
1086
ee1046d1 1087wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
78d14f80 1088{
7b56015f
VS
1089 wxXmlNode *parNode = GetParamNode(param);
1090 wxString str1(GetNodeContent(parNode));
78d14f80 1091 wxString str2;
424af7aa
VS
1092
1093 // "\\" wasn't translated to "\" prior to 2.5.3.0:
1094 const bool escapeBackslash = (m_resource->CompareVersion(2,5,3,0) >= 0);
78d14f80 1095
b272b6dc
RD
1096 // VS: First version of XRC resources used $ instead of & (which is
1097 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 1098 // much better (because &File means "File with F underlined").
424af7aa
VS
1099 const wxChar amp_char = (m_resource->CompareVersion(2,3,0,1) < 0)
1100 ? '$' : '_';
78d14f80 1101
424af7aa 1102 for ( wxString::const_iterator dt = str1.begin(); dt != str1.end(); ++dt )
78d14f80
VS
1103 {
1104 // Remap amp_char to &, map double amp_char to amp_char (for things
1105 // like "&File..." -- this is illegal in XML, so we use "_File..."):
424af7aa 1106 if ( *dt == amp_char )
78d14f80
VS
1107 {
1108 if ( *(++dt) == amp_char )
1109 str2 << amp_char;
1110 else
1111 str2 << wxT('&') << *dt;
1112 }
984c33c9 1113 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
424af7aa
VS
1114 else if ( *dt == wxT('\\') )
1115 {
1116 switch ( (*(++dt)).GetValue() )
78d14f80 1117 {
984c33c9
VS
1118 case wxT('n'):
1119 str2 << wxT('\n');
1120 break;
e7a3a5a5 1121
984c33c9
VS
1122 case wxT('t'):
1123 str2 << wxT('\t');
1124 break;
e7a3a5a5 1125
984c33c9
VS
1126 case wxT('r'):
1127 str2 << wxT('\r');
1128 break;
1129
1130 case wxT('\\') :
1131 // "\\" wasn't translated to "\" prior to 2.5.3.0:
424af7aa 1132 if ( escapeBackslash )
984c33c9
VS
1133 {
1134 str2 << wxT('\\');
1135 break;
1136 }
1137 // else fall-through to default: branch below
e7a3a5a5 1138
984c33c9
VS
1139 default:
1140 str2 << wxT('\\') << *dt;
1141 break;
78d14f80 1142 }
424af7aa
VS
1143 }
1144 else
1145 {
1146 str2 << *dt;
1147 }
78d14f80 1148 }
b272b6dc 1149
7b56015f
VS
1150 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
1151 {
1152 if (translate && parNode &&
288b6107 1153 parNode->GetAttribute(wxT("translate"), wxEmptyString) != wxT("0"))
7b56015f 1154 {
d4a724d4 1155 return wxGetTranslation(str2, m_resource->GetDomain());
7b56015f
VS
1156 }
1157 else
1158 {
1159#if wxUSE_UNICODE
1160 return str2;
1161#else
1162 // The string is internally stored as UTF-8, we have to convert
1163 // it into system's default encoding so that it can be displayed:
6251e0ea 1164 return wxString(str2.wc_str(wxConvUTF8), wxConvLocal);
7b56015f
VS
1165#endif
1166 }
1167 }
8516a98b
DS
1168
1169 // If wxXRC_USE_LOCALE is not set, then the string is already in
1170 // system's default encoding in ANSI build, so we don't have to
1171 // do anything special here.
1172 return str2;
78d14f80
VS
1173}
1174
1175
1176
1177long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
1178{
1179 long value;
1180 wxString str1 = GetParamValue(param);
1181
1182 if (!str1.ToLong(&value))
1183 value = defaultv;
1184
1185 return value;
1186}
e7a3a5a5 1187
1df61962
VS
1188float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv)
1189{
1d9473d3 1190 wxString str = GetParamValue(param);
1df61962 1191
cfd91598 1192#if wxUSE_INTL
1d9473d3
VZ
1193 // strings in XRC always use C locale but wxString::ToDouble() uses the
1194 // current one, so transform the string to it supposing that the only
1195 // difference between them is the decimal separator
1196 //
1197 // TODO: use wxString::ToCDouble() when we have it
1198 str.Replace(wxT("."), wxLocale::GetInfo(wxLOCALE_DECIMAL_POINT,
1199 wxLOCALE_CAT_NUMBER));
cfd91598 1200#endif // wxUSE_INTL
e7a3a5a5 1201
1d9473d3
VZ
1202 double value;
1203 if (!str.ToDouble(&value))
1df61962
VS
1204 value = defaultv;
1205
17a1ebd1 1206 return wx_truncate_cast(float, value);
1df61962 1207}
78d14f80 1208
af1337b0 1209
78d14f80
VS
1210int wxXmlResourceHandler::GetID()
1211{
13de23f6 1212 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
1213}
1214
1215
af1337b0 1216
78d14f80
VS
1217wxString wxXmlResourceHandler::GetName()
1218{
288b6107 1219 return m_node->GetAttribute(wxT("name"), wxT("-1"));
78d14f80
VS
1220}
1221
1222
1223
8758875e
VZ
1224bool wxXmlResourceHandler::GetBoolAttr(const wxString& attr, bool defaultv)
1225{
1226 wxString v;
1227 return m_node->GetAttribute(attr, &v) ? v == '1' : defaultv;
1228}
1229
78d14f80
VS
1230bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
1231{
8758875e 1232 const wxString v = GetParamValue(param);
8516a98b 1233
8758875e 1234 return v.empty() ? defaultv : (v == '1');
78d14f80
VS
1235}
1236
1237
1df61962
VS
1238static wxColour GetSystemColour(const wxString& name)
1239{
1240 if (!name.empty())
1241 {
1242 #define SYSCLR(clr) \
9a83f860 1243 if (name == wxT(#clr)) return wxSystemSettings::GetColour(clr);
1df61962
VS
1244 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
1245 SYSCLR(wxSYS_COLOUR_BACKGROUND)
1246 SYSCLR(wxSYS_COLOUR_DESKTOP)
1247 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
1248 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
1249 SYSCLR(wxSYS_COLOUR_MENU)
1250 SYSCLR(wxSYS_COLOUR_WINDOW)
1251 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
1252 SYSCLR(wxSYS_COLOUR_MENUTEXT)
1253 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
1254 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
1255 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
1256 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
1257 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
1258 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
1259 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
1260 SYSCLR(wxSYS_COLOUR_BTNFACE)
1261 SYSCLR(wxSYS_COLOUR_3DFACE)
1262 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
1263 SYSCLR(wxSYS_COLOUR_3DSHADOW)
1264 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
1265 SYSCLR(wxSYS_COLOUR_BTNTEXT)
1266 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
1267 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
1268 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
1269 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1270 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1271 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1272 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1273 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1274 SYSCLR(wxSYS_COLOUR_INFOBK)
1275 SYSCLR(wxSYS_COLOUR_LISTBOX)
1276 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1277 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1278 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1279 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1280 SYSCLR(wxSYS_COLOUR_MENUBAR)
1281 #undef SYSCLR
1282 }
1283
1284 return wxNullColour;
1285}
78d14f80 1286
984f1d84 1287wxColour wxXmlResourceHandler::GetColour(const wxString& param, const wxColour& defaultv)
78d14f80
VS
1288{
1289 wxString v = GetParamValue(param);
984f1d84
VS
1290
1291 if ( v.empty() )
1292 return defaultv;
1293
68b4e4cf 1294 wxColour clr;
1df61962 1295
68b4e4cf
WS
1296 // wxString -> wxColour conversion
1297 if (!clr.Set(v))
78d14f80 1298 {
1df61962
VS
1299 // the colour doesn't use #RRGGBB format, check if it is symbolic
1300 // colour name:
68b4e4cf 1301 clr = GetSystemColour(v);
1df61962
VS
1302 if (clr.Ok())
1303 return clr;
e7a3a5a5 1304
819559b2
VS
1305 ReportParamError
1306 (
1307 param,
1308 wxString::Format("incorrect colour specification \"%s\"", v)
1309 );
78d14f80
VS
1310 return wxNullColour;
1311 }
1312
68b4e4cf 1313 return clr;
78d14f80
VS
1314}
1315
1c60f644
VS
1316namespace
1317{
1318
1319// if 'param' has stock_id/stock_client, extracts them and returns true
1320bool GetStockArtAttrs(const wxXmlNode *paramNode,
1321 const wxString& defaultArtClient,
1322 wxString& art_id, wxString& art_client)
1323{
1324 if ( paramNode )
1325 {
1326 art_id = paramNode->GetAttribute("stock_id", "");
1327
1328 if ( !art_id.empty() )
1329 {
1330 art_id = wxART_MAKE_ART_ID_FROM_STR(art_id);
1331
1332 art_client = paramNode->GetAttribute("stock_client", "");
1333 if ( art_client.empty() )
1334 art_client = defaultArtClient;
1335 else
1336 art_client = wxART_MAKE_CLIENT_ID_FROM_STR(art_client);
78d14f80 1337
1c60f644
VS
1338 return true;
1339 }
1340 }
1341
1342 return false;
1343}
1344
1345} // anonymous namespace
78d14f80 1346
92e898b0 1347wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
db59a97c
VS
1348 const wxArtClient& defaultArtClient,
1349 wxSize size)
326462ae 1350{
9c1d2aa2
VZ
1351 // it used to be possible to pass an empty string here to indicate that the
1352 // bitmap name should be read from this node itself but this is not
1353 // supported any more because GetBitmap(m_node) can be used directly
1354 // instead
1355 wxASSERT_MSG( !param.empty(), "bitmap parameter name can't be empty" );
1356
1357 const wxXmlNode* const node = GetParamNode(param);
1358
1359 if ( !node )
1360 {
1361 // this is not an error as bitmap parameter could be optional
1362 return wxNullBitmap;
1363 }
1364
1365 return GetBitmap(node, defaultArtClient, size);
326462ae
VZ
1366}
1367
1368wxBitmap wxXmlResourceHandler::GetBitmap(const wxXmlNode* node,
1369 const wxArtClient& defaultArtClient,
1370 wxSize size)
78d14f80 1371{
9c1d2aa2
VZ
1372 wxCHECK_MSG( node, wxNullBitmap, "bitmap node can't be NULL" );
1373
db59a97c 1374 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1c60f644 1375 wxString art_id, art_client;
326462ae 1376 if ( GetStockArtAttrs(node, defaultArtClient,
1c60f644 1377 art_id, art_client) )
af1337b0 1378 {
1c60f644
VS
1379 wxBitmap stockArt(wxArtProvider::GetBitmap(art_id, art_client, size));
1380 if ( stockArt.Ok() )
1381 return stockArt;
af1337b0
JS
1382 }
1383
92e898b0 1384 /* ...or load the bitmap from file: */
326462ae 1385 wxString name = GetParamValue(node);
e7a3a5a5 1386 if (name.empty()) return wxNullBitmap;
78d14f80 1387#if wxUSE_FILESYSTEM
4532786e 1388 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
78d14f80
VS
1389 if (fsfile == NULL)
1390 {
819559b2
VS
1391 ReportParamError
1392 (
326462ae 1393 node->GetName(),
819559b2
VS
1394 wxString::Format("cannot open bitmap resource \"%s\"", name)
1395 );
78d14f80
VS
1396 return wxNullBitmap;
1397 }
1398 wxImage img(*(fsfile->GetStream()));
1399 delete fsfile;
1400#else
45f3249b 1401 wxImage img(name);
78d14f80 1402#endif
af1337b0 1403
78d14f80
VS
1404 if (!img.Ok())
1405 {
819559b2
VS
1406 ReportParamError
1407 (
326462ae 1408 node->GetName(),
819559b2
VS
1409 wxString::Format("cannot create bitmap from \"%s\"", name)
1410 );
78d14f80
VS
1411 return wxNullBitmap;
1412 }
1413 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 1414 return wxBitmap(img);
78d14f80
VS
1415}
1416
78d14f80 1417
92e898b0 1418wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
db59a97c
VS
1419 const wxArtClient& defaultArtClient,
1420 wxSize size)
326462ae 1421{
9c1d2aa2
VZ
1422 // see comment in GetBitmap(wxString) overload
1423 wxASSERT_MSG( !param.empty(), "icon parameter name can't be empty" );
1424
1425 const wxXmlNode* const node = GetParamNode(param);
1426
1427 if ( !node )
1428 {
1429 // this is not an error as icon parameter could be optional
1430 return wxIcon();
1431 }
1432
1433 return GetIcon(node, defaultArtClient, size);
326462ae
VZ
1434}
1435
1436wxIcon wxXmlResourceHandler::GetIcon(const wxXmlNode* node,
1437 const wxArtClient& defaultArtClient,
1438 wxSize size)
78d14f80 1439{
78d14f80 1440 wxIcon icon;
326462ae 1441 icon.CopyFromBitmap(GetBitmap(node, defaultArtClient, size));
78d14f80
VS
1442 return icon;
1443}
1444
326462ae 1445
1c60f644
VS
1446wxIconBundle wxXmlResourceHandler::GetIconBundle(const wxString& param,
1447 const wxArtClient& defaultArtClient)
1448{
1449 wxString art_id, art_client;
1450 if ( GetStockArtAttrs(GetParamNode(param), defaultArtClient,
1451 art_id, art_client) )
1452 {
1453 wxIconBundle stockArt(wxArtProvider::GetIconBundle(art_id, art_client));
1454 if ( stockArt.IsOk() )
1455 return stockArt;
1456 }
1457
1458 const wxString name = GetParamValue(param);
1459 if ( name.empty() )
1460 return wxNullIconBundle;
1461
1462#if wxUSE_FILESYSTEM
1463 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE);
1464 if ( fsfile == NULL )
1465 {
1466 ReportParamError
1467 (
1468 param,
1469 wxString::Format("cannot open icon resource \"%s\"", name)
1470 );
1471 return wxNullIconBundle;
1472 }
1473
1474 wxIconBundle bundle(*(fsfile->GetStream()));
1475 delete fsfile;
1476#else
1477 wxIconBundle bundle(name);
1478#endif
1479
1480 if ( !bundle.IsOk() )
1481 {
1482 ReportParamError
1483 (
1484 param,
1485 wxString::Format("cannot create icon from \"%s\"", name)
1486 );
1487 return wxNullIconBundle;
1488 }
1489
1490 return bundle;
1491}
78d14f80
VS
1492
1493
326462ae
VZ
1494wxImageList *wxXmlResourceHandler::GetImageList(const wxString& param)
1495{
1496 wxXmlNode * const imagelist_node = GetParamNode(param);
1497 if ( !imagelist_node )
1498 return NULL;
1499
1500 wxXmlNode * const oldnode = m_node;
1501 m_node = imagelist_node;
1502
1503 // size
1504 wxSize size = GetSize();
1505 size.SetDefaults(wxSize(wxSystemSettings::GetMetric(wxSYS_ICON_X),
1506 wxSystemSettings::GetMetric(wxSYS_ICON_Y)));
1507
1508 // mask: true by default
1509 bool mask = HasParam(wxT("mask")) ? GetBool(wxT("mask"), true) : true;
1510
1511 // now we have everything we need to create the image list
1512 wxImageList *imagelist = new wxImageList(size.x, size.y, mask);
1513
1514 // add images
1515 wxString parambitmap = wxT("bitmap");
1516 if ( HasParam(parambitmap) )
1517 {
1518 wxXmlNode *n = m_node->GetChildren();
1519 while (n)
1520 {
1521 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == parambitmap)
1522 {
1523 // add icon instead of bitmap to keep the bitmap mask
1524 imagelist->Add(GetIcon(n));
1525 }
1526 n = n->GetNext();
1527 }
1528 }
1529
1530 m_node = oldnode;
1531 return imagelist;
1532}
1533
78d14f80
VS
1534wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
1535{
2b5f62a0
VZ
1536 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
1537
78d14f80
VS
1538 wxXmlNode *n = m_node->GetChildren();
1539
1540 while (n)
1541 {
1542 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
cffff062
VZ
1543 {
1544 // TODO: check that there are no other properties/parameters with
1545 // the same name and log an error if there are (can't do this
1546 // right now as I'm not sure if it's not going to break code
1547 // using this function in unintentional way (i.e. for
1548 // accessing other things than properties), for example
1549 // wxBitmapComboBoxXmlHandler almost surely does
78d14f80 1550 return n;
cffff062 1551 }
78d14f80
VS
1552 n = n->GetNext();
1553 }
1554 return NULL;
1555}
1556
2d672c46
MW
1557bool wxXmlResourceHandler::IsOfClass(wxXmlNode *node, const wxString& classname)
1558{
288b6107 1559 return node->GetAttribute(wxT("class"), wxEmptyString) == classname;
2d672c46
MW
1560}
1561
1562
1563
326462ae 1564wxString wxXmlResourceHandler::GetNodeContent(const wxXmlNode *node)
78d14f80 1565{
326462ae 1566 const wxXmlNode *n = node;
78d14f80
VS
1567 if (n == NULL) return wxEmptyString;
1568 n = n->GetChildren();
1569
1570 while (n)
1571 {
1572 if (n->GetType() == wxXML_TEXT_NODE ||
1573 n->GetType() == wxXML_CDATA_SECTION_NODE)
1574 return n->GetContent();
1575 n = n->GetNext();
1576 }
1577 return wxEmptyString;
1578}
1579
1580
1581
1582wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
1583{
e7a3a5a5 1584 if (param.empty())
78d14f80
VS
1585 return GetNodeContent(m_node);
1586 else
1587 return GetNodeContent(GetParamNode(param));
1588}
1589
326462ae
VZ
1590wxString wxXmlResourceHandler::GetParamValue(const wxXmlNode* node)
1591{
1592 return GetNodeContent(node);
1593}
78d14f80
VS
1594
1595
0c00c86f
VS
1596wxSize wxXmlResourceHandler::GetSize(const wxString& param,
1597 wxWindow *windowToUse)
78d14f80
VS
1598{
1599 wxString s = GetParamValue(param);
e7a3a5a5 1600 if (s.empty()) s = wxT("-1,-1");
78d14f80 1601 bool is_dlg;
d1f47235 1602 long sx, sy = 0;
78d14f80 1603
88a7a4e1 1604 is_dlg = s[s.length()-1] == wxT('d');
78d14f80
VS
1605 if (is_dlg) s.RemoveLast();
1606
1607 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
1608 !s.AfterLast(wxT(',')).ToLong(&sy))
1609 {
819559b2
VS
1610 ReportParamError
1611 (
1612 param,
1613 wxString::Format("cannot parse coordinates value \"%s\"", s)
1614 );
78d14f80
VS
1615 return wxDefaultSize;
1616 }
1617
1618 if (is_dlg)
1619 {
0c00c86f
VS
1620 if (windowToUse)
1621 {
1622 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
1623 }
1624 else if (m_parentAsWindow)
1625 {
78d14f80 1626 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
0c00c86f 1627 }
78d14f80
VS
1628 else
1629 {
819559b2
VS
1630 ReportParamError
1631 (
1632 param,
1633 "cannot convert dialog units: dialog unknown"
1634 );
78d14f80
VS
1635 return wxDefaultSize;
1636 }
1637 }
8516a98b
DS
1638
1639 return wxSize(sx, sy);
78d14f80
VS
1640}
1641
1642
1643
1644wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
1645{
1646 wxSize sz = GetSize(param);
1647 return wxPoint(sz.x, sz.y);
1648}
1649
1650
1651
0c00c86f
VS
1652wxCoord wxXmlResourceHandler::GetDimension(const wxString& param,
1653 wxCoord defaultv,
1654 wxWindow *windowToUse)
78d14f80
VS
1655{
1656 wxString s = GetParamValue(param);
e7a3a5a5 1657 if (s.empty()) return defaultv;
78d14f80
VS
1658 bool is_dlg;
1659 long sx;
1660
88a7a4e1 1661 is_dlg = s[s.length()-1] == wxT('d');
78d14f80
VS
1662 if (is_dlg) s.RemoveLast();
1663
1664 if (!s.ToLong(&sx))
1665 {
819559b2
VS
1666 ReportParamError
1667 (
1668 param,
1669 wxString::Format("cannot parse dimension value \"%s\"", s)
1670 );
78d14f80
VS
1671 return defaultv;
1672 }
1673
1674 if (is_dlg)
1675 {
0c00c86f
VS
1676 if (windowToUse)
1677 {
1678 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
1679 }
1680 else if (m_parentAsWindow)
1681 {
78d14f80 1682 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
0c00c86f 1683 }
78d14f80
VS
1684 else
1685 {
819559b2
VS
1686 ReportParamError
1687 (
1688 param,
1689 "cannot convert dialog units: dialog unknown"
1690 );
78d14f80
VS
1691 return defaultv;
1692 }
1693 }
8516a98b
DS
1694
1695 return sx;
78d14f80
VS
1696}
1697
1698
1df61962
VS
1699// Get system font index using indexname
1700static wxFont GetSystemFont(const wxString& name)
1701{
1702 if (!name.empty())
1703 {
1704 #define SYSFNT(fnt) \
9a83f860 1705 if (name == wxT(#fnt)) return wxSystemSettings::GetFont(fnt);
1df61962
VS
1706 SYSFNT(wxSYS_OEM_FIXED_FONT)
1707 SYSFNT(wxSYS_ANSI_FIXED_FONT)
1708 SYSFNT(wxSYS_ANSI_VAR_FONT)
1709 SYSFNT(wxSYS_SYSTEM_FONT)
1710 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1df61962
VS
1711 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
1712 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
1713 #undef SYSFNT
1714 }
1715
1716 return wxNullFont;
1717}
78d14f80
VS
1718
1719wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1720{
1721 wxXmlNode *font_node = GetParamNode(param);
1722 if (font_node == NULL)
1723 {
819559b2
VS
1724 ReportError(
1725 wxString::Format("cannot find font node \"%s\"", param));
78d14f80
VS
1726 return wxNullFont;
1727 }
1728
1729 wxXmlNode *oldnode = m_node;
1730 m_node = font_node;
1731
1df61962 1732 // font attributes:
78d14f80 1733
1df61962 1734 // size
94245f6d 1735 int isize = -1;
1df61962 1736 bool hasSize = HasParam(wxT("size"));
e7a3a5a5 1737 if (hasSize)
94245f6d 1738 isize = GetLong(wxT("size"), -1);
78d14f80 1739
1df61962
VS
1740 // style
1741 int istyle = wxNORMAL;
1742 bool hasStyle = HasParam(wxT("style"));
1743 if (hasStyle)
1744 {
1745 wxString style = GetParamValue(wxT("style"));
e7a3a5a5 1746 if (style == wxT("italic"))
1df61962 1747 istyle = wxITALIC;
e7a3a5a5 1748 else if (style == wxT("slant"))
1df61962
VS
1749 istyle = wxSLANT;
1750 }
78d14f80 1751
1df61962
VS
1752 // weight
1753 int iweight = wxNORMAL;
1754 bool hasWeight = HasParam(wxT("weight"));
1755 if (hasWeight)
1756 {
1757 wxString weight = GetParamValue(wxT("weight"));
e7a3a5a5 1758 if (weight == wxT("bold"))
1df61962 1759 iweight = wxBOLD;
e7a3a5a5 1760 else if (weight == wxT("light"))
1df61962
VS
1761 iweight = wxLIGHT;
1762 }
e7a3a5a5 1763
1df61962
VS
1764 // underline
1765 bool hasUnderlined = HasParam(wxT("underlined"));
1766 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
78d14f80 1767
1df61962
VS
1768 // family and facename
1769 int ifamily = wxDEFAULT;
1770 bool hasFamily = HasParam(wxT("family"));
1771 if (hasFamily)
78d14f80 1772 {
1df61962
VS
1773 wxString family = GetParamValue(wxT("family"));
1774 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1775 else if (family == wxT("roman")) ifamily = wxROMAN;
1776 else if (family == wxT("script")) ifamily = wxSCRIPT;
1777 else if (family == wxT("swiss")) ifamily = wxSWISS;
1778 else if (family == wxT("modern")) ifamily = wxMODERN;
1779 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
1780 }
e7a3a5a5
WS
1781
1782
1df61962
VS
1783 wxString facename;
1784 bool hasFacename = HasParam(wxT("face"));
1785 if (hasFacename)
1786 {
1787 wxString faces = GetParamValue(wxT("face"));
1df61962 1788 wxStringTokenizer tk(faces, wxT(","));
63feebce
VS
1789#if wxUSE_FONTENUM
1790 wxArrayString facenames(wxFontEnumerator::GetFacenames());
1df61962 1791 while (tk.HasMoreTokens())
78d14f80 1792 {
6540132f 1793 int index = facenames.Index(tk.GetNextToken(), false);
1df61962
VS
1794 if (index != wxNOT_FOUND)
1795 {
6540132f 1796 facename = facenames[index];
1df61962
VS
1797 break;
1798 }
78d14f80 1799 }
63feebce
VS
1800#else // !wxUSE_FONTENUM
1801 // just use the first face name if we can't check its availability:
1802 if (tk.HasMoreTokens())
1803 facename = tk.GetNextToken();
1804#endif // wxUSE_FONTENUM/!wxUSE_FONTENUM
78d14f80
VS
1805 }
1806
1df61962
VS
1807 // encoding
1808 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1809 bool hasEncoding = HasParam(wxT("encoding"));
dc2575ba 1810#if wxUSE_FONTMAP
1df61962
VS
1811 if (hasEncoding)
1812 {
1813 wxString encoding = GetParamValue(wxT("encoding"));
1814 wxFontMapper mapper;
e7a3a5a5 1815 if (!encoding.empty())
1df61962
VS
1816 enc = mapper.CharsetToEncoding(encoding);
1817 if (enc == wxFONTENCODING_SYSTEM)
1818 enc = wxFONTENCODING_DEFAULT;
1819 }
dc2575ba 1820#endif // wxUSE_FONTMAP
78d14f80 1821
1df61962 1822 // is this font based on a system font?
94245f6d 1823 wxFont font = GetSystemFont(GetParamValue(wxT("sysfont")));
e7a3a5a5 1824
94245f6d 1825 if (font.Ok())
1df61962 1826 {
94245f6d
VZ
1827 if (hasSize && isize != -1)
1828 font.SetPointSize(isize);
1df61962 1829 else if (HasParam(wxT("relativesize")))
94245f6d 1830 font.SetPointSize(int(font.GetPointSize() *
1df61962 1831 GetFloat(wxT("relativesize"))));
e7a3a5a5 1832
1df61962 1833 if (hasStyle)
94245f6d 1834 font.SetStyle(istyle);
1df61962 1835 if (hasWeight)
94245f6d 1836 font.SetWeight(iweight);
1df61962 1837 if (hasUnderlined)
94245f6d 1838 font.SetUnderlined(underlined);
1df61962 1839 if (hasFamily)
94245f6d 1840 font.SetFamily(ifamily);
1df61962 1841 if (hasFacename)
94245f6d 1842 font.SetFaceName(facename);
1df61962 1843 if (hasEncoding)
94245f6d
VZ
1844 font.SetDefaultEncoding(enc);
1845 }
1846 else // not based on system font
1847 {
1848 font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize,
1849 ifamily, istyle, iweight,
1850 underlined, facename, enc);
1df61962 1851 }
8516a98b
DS
1852
1853 m_node = oldnode;
94245f6d 1854 return font;
78d14f80
VS
1855}
1856
1857
1858void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1859{
1860 //FIXME : add cursor
1861
1862 if (HasParam(wxT("exstyle")))
0099f343
JS
1863 // Have to OR it with existing style, since
1864 // some implementations (e.g. wxGTK) use the extra style
1865 // during creation
1866 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
78d14f80
VS
1867 if (HasParam(wxT("bg")))
1868 wnd->SetBackgroundColour(GetColour(wxT("bg")));
a7435c3e
VZ
1869 if (HasParam(wxT("ownbg")))
1870 wnd->SetOwnBackgroundColour(GetColour(wxT("ownbg")));
78d14f80
VS
1871 if (HasParam(wxT("fg")))
1872 wnd->SetForegroundColour(GetColour(wxT("fg")));
a7435c3e
VZ
1873 if (HasParam(wxT("ownfg")))
1874 wnd->SetOwnForegroundColour(GetColour(wxT("ownfg")));
78d14f80 1875 if (GetBool(wxT("enabled"), 1) == 0)
f80ea77b 1876 wnd->Enable(false);
78d14f80
VS
1877 if (GetBool(wxT("focused"), 0) == 1)
1878 wnd->SetFocus();
1879 if (GetBool(wxT("hidden"), 0) == 1)
f80ea77b 1880 wnd->Show(false);
78d14f80
VS
1881#if wxUSE_TOOLTIPS
1882 if (HasParam(wxT("tooltip")))
1883 wnd->SetToolTip(GetText(wxT("tooltip")));
1884#endif
1885 if (HasParam(wxT("font")))
a7435c3e
VZ
1886 wnd->SetFont(GetFont(wxT("font")));
1887 if (HasParam(wxT("ownfont")))
1888 wnd->SetOwnFont(GetFont(wxT("ownfont")));
b23030d6
JS
1889 if (HasParam(wxT("help")))
1890 wnd->SetHelpText(GetText(wxT("help")));
78d14f80
VS
1891}
1892
1893
1894void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1895{
23239d94 1896 for ( wxXmlNode *n = m_node->GetChildren(); n; n = n->GetNext() )
78d14f80 1897 {
23239d94 1898 if ( IsObjectNode(n) )
78d14f80 1899 {
af0ac990
VZ
1900 m_resource->DoCreateResFromNode(*n, parent, NULL,
1901 this_hnd_only ? this : NULL);
78d14f80 1902 }
78d14f80
VS
1903 }
1904}
1905
1906
1907void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1908{
1909 wxXmlNode *root;
1910 if (rootnode == NULL) root = m_node; else root = rootnode;
1911 wxXmlNode *n = root->GetChildren();
1912
1913 while (n)
1914 {
1915 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1916 {
1917 CreateResource(n, parent, NULL);
1918 }
1919 n = n->GetNext();
1920 }
1921}
1922
1923
819559b2
VS
1924//-----------------------------------------------------------------------------
1925// errors reporting
1926//-----------------------------------------------------------------------------
1927
1928void wxXmlResourceHandler::ReportError(const wxString& message)
1929{
1930 m_resource->ReportError(m_node, message);
1931}
1932
1933void wxXmlResourceHandler::ReportError(wxXmlNode *context,
1934 const wxString& message)
1935{
1936 m_resource->ReportError(context ? context : m_node, message);
1937}
1938
1939void wxXmlResourceHandler::ReportParamError(const wxString& param,
1940 const wxString& message)
1941{
1942 m_resource->ReportError(GetParamNode(param), message);
1943}
1944
819559b2
VS
1945void wxXmlResource::ReportError(wxXmlNode *context, const wxString& message)
1946{
1947 if ( !context )
1948 {
1949 DoReportError("", NULL, message);
1950 return;
1951 }
78d14f80 1952
819559b2
VS
1953 // We need to find out the file that 'context' is part of. Performance of
1954 // this code is not critical, so we simply find the root XML node and
1955 // compare it with all loaded XRC files.
1956 const wxString filename = GetFileNameFromNode(context, Data());
78d14f80 1957
819559b2
VS
1958 DoReportError(filename, context, message);
1959}
78d14f80 1960
819559b2
VS
1961void wxXmlResource::DoReportError(const wxString& xrcFile, wxXmlNode *position,
1962 const wxString& message)
1963{
1964 const int line = position ? position->GetLineNumber() : -1;
1965
1966 wxString loc;
1967 if ( !xrcFile.empty() )
1968 loc = xrcFile + ':';
1969 if ( line != -1 )
1970 loc += wxString::Format("%d:", line);
1971 if ( !loc.empty() )
1972 loc += ' ';
1973
1974 wxLogError("XRC error: %s%s", loc, message);
1975}
78d14f80
VS
1976
1977
819559b2
VS
1978//-----------------------------------------------------------------------------
1979// XRCID implementation
1980//-----------------------------------------------------------------------------
78d14f80 1981
5ed345b7 1982#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1983
1984
5ed345b7 1985struct XRCID_record
78d14f80 1986{
cf2810aa
VZ
1987 /* Hold the id so that once an id is allocated for a name, it
1988 does not get created again by NewControlId at least
1989 until we are done with it */
1990 wxWindowIDRef id;
c560da98 1991 char *key;
5ed345b7 1992 XRCID_record *next;
78d14f80
VS
1993};
1994
5ed345b7 1995static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1996
c560da98 1997static int XRCID_Lookup(const char *str_id, int value_if_not_found = wxID_NONE)
78d14f80 1998{
78d14f80
VS
1999 int index = 0;
2000
c560da98 2001 for (const char *c = str_id; *c != '\0'; c++) index += (int)*c;
5ed345b7 2002 index %= XRCID_TABLE_SIZE;
78d14f80 2003
5ed345b7 2004 XRCID_record *oldrec = NULL;
5ed345b7 2005 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 2006 {
00393283 2007 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
2008 {
2009 return rec->id;
2010 }
78d14f80
VS
2011 oldrec = rec;
2012 }
2013
5ed345b7
VS
2014 XRCID_record **rec_var = (oldrec == NULL) ?
2015 &XRCID_Records[index] : &oldrec->next;
2016 *rec_var = new XRCID_record;
00393283 2017 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
2018 (*rec_var)->next = NULL;
2019
c560da98 2020 char *end;
9b2a7469 2021 if (value_if_not_found != wxID_NONE)
13de23f6 2022 (*rec_var)->id = value_if_not_found;
85452d74
VS
2023 else
2024 {
13de23f6
VS
2025 int asint = wxStrtol(str_id, &end, 10);
2026 if (*str_id && *end == 0)
2027 {
2028 // if str_id was integer, keep it verbosely:
2029 (*rec_var)->id = asint;
2030 }
2031 else
2032 {
f35fdf7e 2033 (*rec_var)->id = wxWindowBase::NewControlId();
13de23f6 2034 }
85452d74
VS
2035 }
2036
78d14f80
VS
2037 return (*rec_var)->id;
2038}
2039
a58804ea 2040namespace
78d14f80 2041{
f35fdf7e 2042
a58804ea
VZ
2043// flag indicating whether standard XRC ids were already initialized
2044static bool gs_stdIDsAdded = false;
78d14f80 2045
a58804ea 2046void AddStdXRCID_Records()
13de23f6 2047{
c560da98 2048#define stdID(id) XRCID_Lookup(#id, id)
13de23f6 2049 stdID(-1);
c369d4f1
VS
2050
2051 stdID(wxID_ANY);
2052 stdID(wxID_SEPARATOR);
e7a3a5a5 2053
c369d4f1
VS
2054 stdID(wxID_OPEN);
2055 stdID(wxID_CLOSE);
2056 stdID(wxID_NEW);
2057 stdID(wxID_SAVE);
2058 stdID(wxID_SAVEAS);
2059 stdID(wxID_REVERT);
2060 stdID(wxID_EXIT);
2061 stdID(wxID_UNDO);
2062 stdID(wxID_REDO);
2063 stdID(wxID_HELP);
2064 stdID(wxID_PRINT);
2065 stdID(wxID_PRINT_SETUP);
e63f19ba 2066 stdID(wxID_PAGE_SETUP);
c369d4f1
VS
2067 stdID(wxID_PREVIEW);
2068 stdID(wxID_ABOUT);
2069 stdID(wxID_HELP_CONTENTS);
2070 stdID(wxID_HELP_COMMANDS);
2071 stdID(wxID_HELP_PROCEDURES);
2072 stdID(wxID_HELP_CONTEXT);
13de23f6 2073 stdID(wxID_CLOSE_ALL);
c369d4f1 2074 stdID(wxID_PREFERENCES);
d73195fd 2075 stdID(wxID_EDIT);
c369d4f1
VS
2076 stdID(wxID_CUT);
2077 stdID(wxID_COPY);
2078 stdID(wxID_PASTE);
2079 stdID(wxID_CLEAR);
2080 stdID(wxID_FIND);
2081 stdID(wxID_DUPLICATE);
2082 stdID(wxID_SELECTALL);
2083 stdID(wxID_DELETE);
2084 stdID(wxID_REPLACE);
2085 stdID(wxID_REPLACE_ALL);
2086 stdID(wxID_PROPERTIES);
2087 stdID(wxID_VIEW_DETAILS);
2088 stdID(wxID_VIEW_LARGEICONS);
2089 stdID(wxID_VIEW_SMALLICONS);
2090 stdID(wxID_VIEW_LIST);
2091 stdID(wxID_VIEW_SORTDATE);
2092 stdID(wxID_VIEW_SORTNAME);
2093 stdID(wxID_VIEW_SORTSIZE);
2094 stdID(wxID_VIEW_SORTTYPE);
2095 stdID(wxID_FILE1);
2096 stdID(wxID_FILE2);
2097 stdID(wxID_FILE3);
2098 stdID(wxID_FILE4);
2099 stdID(wxID_FILE5);
2100 stdID(wxID_FILE6);
2101 stdID(wxID_FILE7);
2102 stdID(wxID_FILE8);
2103 stdID(wxID_FILE9);
2104 stdID(wxID_OK);
2105 stdID(wxID_CANCEL);
2106 stdID(wxID_APPLY);
2107 stdID(wxID_YES);
2108 stdID(wxID_NO);
2109 stdID(wxID_STATIC);
2110 stdID(wxID_FORWARD);
2111 stdID(wxID_BACKWARD);
2112 stdID(wxID_DEFAULT);
2113 stdID(wxID_MORE);
2114 stdID(wxID_SETUP);
2115 stdID(wxID_RESET);
2116 stdID(wxID_CONTEXT_HELP);
2117 stdID(wxID_YESTOALL);
2118 stdID(wxID_NOTOALL);
2119 stdID(wxID_ABORT);
2120 stdID(wxID_RETRY);
2121 stdID(wxID_IGNORE);
2122 stdID(wxID_ADD);
2123 stdID(wxID_REMOVE);
2124 stdID(wxID_UP);
2125 stdID(wxID_DOWN);
2126 stdID(wxID_HOME);
2127 stdID(wxID_REFRESH);
2128 stdID(wxID_STOP);
2129 stdID(wxID_INDEX);
2130 stdID(wxID_BOLD);
2131 stdID(wxID_ITALIC);
2132 stdID(wxID_JUSTIFY_CENTER);
2133 stdID(wxID_JUSTIFY_FILL);
2134 stdID(wxID_JUSTIFY_RIGHT);
2135 stdID(wxID_JUSTIFY_LEFT);
2136 stdID(wxID_UNDERLINE);
2137 stdID(wxID_INDENT);
2138 stdID(wxID_UNINDENT);
2139 stdID(wxID_ZOOM_100);
2140 stdID(wxID_ZOOM_FIT);
2141 stdID(wxID_ZOOM_IN);
2142 stdID(wxID_ZOOM_OUT);
2143 stdID(wxID_UNDELETE);
2144 stdID(wxID_REVERT_TO_SAVED);
2145 stdID(wxID_SYSTEM_MENU);
2146 stdID(wxID_CLOSE_FRAME);
2147 stdID(wxID_MOVE_FRAME);
2148 stdID(wxID_RESIZE_FRAME);
2149 stdID(wxID_MAXIMIZE_FRAME);
2150 stdID(wxID_ICONIZE_FRAME);
2151 stdID(wxID_RESTORE_FRAME);
6b1eedc1
VZ
2152 stdID(wxID_CDROM);
2153 stdID(wxID_CONVERT);
2154 stdID(wxID_EXECUTE);
2155 stdID(wxID_FLOPPY);
2156 stdID(wxID_HARDDISK);
2157 stdID(wxID_BOTTOM);
2158 stdID(wxID_FIRST);
2159 stdID(wxID_LAST);
2160 stdID(wxID_TOP);
2161 stdID(wxID_INFO);
2162 stdID(wxID_JUMP_TO);
2163 stdID(wxID_NETWORK);
2164 stdID(wxID_SELECT_COLOR);
2165 stdID(wxID_SELECT_FONT);
2166 stdID(wxID_SORT_ASCENDING);
2167 stdID(wxID_SORT_DESCENDING);
2168 stdID(wxID_SPELL_CHECK);
2169 stdID(wxID_STRIKETHROUGH);
c369d4f1 2170
13de23f6
VS
2171#undef stdID
2172}
78d14f80 2173
a58804ea 2174} // anonymous namespace
78d14f80
VS
2175
2176
a58804ea
VZ
2177/*static*/
2178int wxXmlResource::DoGetXRCID(const char *str_id, int value_if_not_found)
2179{
2180 if ( !gs_stdIDsAdded )
2181 {
2182 gs_stdIDsAdded = true;
2183 AddStdXRCID_Records();
2184 }
2185
2186 return XRCID_Lookup(str_id, value_if_not_found);
2187}
2188
2189/* static */
2190wxString wxXmlResource::FindXRCIDById(int numId)
2191{
2192 for ( int i = 0; i < XRCID_TABLE_SIZE; i++ )
2193 {
2194 for ( XRCID_record *rec = XRCID_Records[i]; rec; rec = rec->next )
2195 {
2196 if ( rec->id == numId )
2197 return wxString(rec->key);
2198 }
2199 }
2200
2201 return wxString();
2202}
2203
2204static void CleanXRCID_Record(XRCID_record *rec)
2205{
2206 if (rec)
2207 {
2208 CleanXRCID_Record(rec->next);
2209
2210 free(rec->key);
2211 delete rec;
2212 }
2213}
2214
2215static void CleanXRCID_Records()
2216{
2217 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
2218 {
2219 CleanXRCID_Record(XRCID_Records[i]);
2220 XRCID_Records[i] = NULL;
2221 }
2222
2223 gs_stdIDsAdded = false;
2224}
78d14f80
VS
2225
2226
819559b2
VS
2227//-----------------------------------------------------------------------------
2228// module and globals
2229//-----------------------------------------------------------------------------
78d14f80 2230
fd230129
VZ
2231// normally we would do the cleanup from wxXmlResourceModule::OnExit() but it
2232// can happen that some XRC records have been created because of the use of
2233// XRCID() in event tables, which happens during static objects initialization,
2234// but then the application initialization failed and so the wx modules were
2235// neither initialized nor cleaned up -- this static object does the cleanup in
2236// this case
2237static struct wxXRCStaticCleanup
2238{
2239 ~wxXRCStaticCleanup() { CleanXRCID_Records(); }
2240} s_staticCleanup;
2241
78d14f80
VS
2242class wxXmlResourceModule: public wxModule
2243{
2244DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
2245public:
2246 wxXmlResourceModule() {}
824e8eaa
VS
2247 bool OnInit()
2248 {
2b5f62a0 2249 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
f80ea77b 2250 return true;
824e8eaa 2251 }
78d14f80
VS
2252 void OnExit()
2253 {
1542c42e 2254 delete wxXmlResource::Set(NULL);
461932ae 2255 if(wxXmlResource::ms_subclassFactories)
eb2d0d23
VS
2256 {
2257 for ( wxXmlSubclassFactories::iterator i = wxXmlResource::ms_subclassFactories->begin();
2258 i != wxXmlResource::ms_subclassFactories->end(); ++i )
2259 {
2260 delete *i;
2261 }
2262 wxDELETE(wxXmlResource::ms_subclassFactories);
2263 }
5ed345b7 2264 CleanXRCID_Records();
78d14f80
VS
2265 }
2266};
2267
2268IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
2269
2270
2271// When wxXml is loaded dynamically after the application is already running
2272// then the built-in module system won't pick this one up. Add it manually.
2273void wxXmlInitResourceModule()
2274{
2275 wxModule* module = new wxXmlResourceModule;
2276 module->Init();
2277 wxModule::RegisterModule(module);
2278}
a1e4ec87
VS
2279
2280#endif // wxUSE_XRC