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