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