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