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