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