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