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