Added some column width contrl code.
[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"
17a1ebd1 45WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords)
78d14f80
VS
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);
34af0de4 414# if wxUSE_DATETIME
78d14f80 415 modif = file && file->GetModificationTime() > m_data[i].Time;
34af0de4
VZ
416# else // wxUSE_DATETIME
417 modif = true;
418# endif // wxUSE_DATETIME
78d14f80 419 if (!file)
d614f51b 420 {
78d14f80 421 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
d614f51b
VS
422 rt = false;
423 }
78d14f80 424 wxDELETE(file);
19d0f58d 425 wxUnusedVar(file);
34af0de4
VZ
426# else // wxUSE_FILESYSTEM
427# if wxUSE_DATETIME
78d14f80 428 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
34af0de4
VZ
429# else // wxUSE_DATETIME
430 modif = true;
431# endif // wxUSE_DATETIME
432# endif // wxUSE_FILESYSTEM
78d14f80
VS
433 }
434
435 if (modif)
436 {
648db587
VS
437 wxLogTrace(_T("xrc"),
438 _T("opening file '%s'"), m_data[i].File.c_str());
439
480505bc 440 wxInputStream *stream = NULL;
78d14f80
VS
441
442# if wxUSE_FILESYSTEM
443 file = fsys.OpenFile(m_data[i].File);
f80ea77b
WS
444 if (file)
445 stream = file->GetStream();
78d14f80
VS
446# else
447 stream = new wxFileInputStream(m_data[i].File);
448# endif
449
450 if (stream)
451 {
452 delete m_data[i].Doc;
453 m_data[i].Doc = new wxXmlDocument;
454 }
4d876ee3 455 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
78d14f80 456 {
480505bc
VS
457 wxLogError(_("Cannot load resources from file '%s'."),
458 m_data[i].File.c_str());
78d14f80 459 wxDELETE(m_data[i].Doc);
d614f51b 460 rt = false;
78d14f80
VS
461 }
462 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
463 {
b5d6954b 464 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
78d14f80 465 wxDELETE(m_data[i].Doc);
d614f51b 466 rt = false;
78d14f80
VS
467 }
468 else
f80ea77b 469 {
78d14f80
VS
470 long version;
471 int v1, v2, v3, v4;
472 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
473 wxT("version"), wxT("0.0.0.0"));
474 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
475 &v1, &v2, &v3, &v4) == 4)
476 version = v1*256*256*256+v2*256*256+v3*256+v4;
477 else
478 version = 0;
479 if (m_version == -1)
480 m_version = version;
481 if (m_version != version)
d614f51b 482 {
78d14f80 483 wxLogError(_("Resource files must have same version number!"));
d614f51b
VS
484 rt = false;
485 }
78d14f80
VS
486
487 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
34af0de4 488#if wxUSE_DATETIME
496f0a58 489#if wxUSE_FILESYSTEM
f80ea77b 490 m_data[i].Time = file->GetModificationTime();
34af0de4 491#else // wxUSE_FILESYSTEM
496f0a58 492 m_data[i].Time = wxDateTime(wxFileModificationTime(m_data[i].File));
34af0de4
VZ
493#endif // wxUSE_FILESYSTEM
494#endif // wxUSE_DATETIME
f80ea77b 495 }
78d14f80
VS
496
497# if wxUSE_FILESYSTEM
f80ea77b
WS
498 wxDELETE(file);
499 wxUnusedVar(file);
78d14f80 500# else
f80ea77b 501 wxDELETE(stream);
78d14f80
VS
502# endif
503 }
504 }
d614f51b
VS
505
506 return rt;
78d14f80
VS
507}
508
509
b272b6dc
RD
510wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
511 const wxString& name,
512 const wxString& classname,
47793ab8
VS
513 bool recursive)
514{
515 wxString dummy;
516 wxXmlNode *node;
517
518 // first search for match at the top-level nodes (as this is
519 // where the resource is most commonly looked for):
520 for (node = parent->GetChildren(); node; node = node->GetNext())
521 {
b272b6dc
RD
522 if ( node->GetType() == wxXML_ELEMENT_NODE &&
523 (node->GetName() == wxT("object") ||
47793ab8 524 node->GetName() == wxT("object_ref")) &&
2b5f62a0
VZ
525 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
526 {
527 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
528 if (!classname || cls == classname)
529 return node;
530 // object_ref may not have 'class' property:
531 if (cls.empty() && node->GetName() == wxT("object_ref"))
532 {
533 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
534 if (refName.empty())
535 continue;
f80ea77b 536 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
2b5f62a0
VZ
537 if (refNode &&
538 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
539 {
540 return node;
541 }
542 }
543 }
47793ab8
VS
544 }
545
546 if ( recursive )
547 for (node = parent->GetChildren(); node; node = node->GetNext())
548 {
b272b6dc
RD
549 if ( node->GetType() == wxXML_ELEMENT_NODE &&
550 (node->GetName() == wxT("object") ||
47793ab8
VS
551 node->GetName() == wxT("object_ref")) )
552 {
f80ea77b 553 wxXmlNode* found = DoFindResource(node, name, classname, true);
47793ab8
VS
554 if ( found )
555 return found;
556 }
557 }
558
559 return NULL;
560}
78d14f80 561
b272b6dc 562wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
563 const wxString& classname,
564 bool recursive)
78d14f80
VS
565{
566 UpdateResources(); //ensure everything is up-to-date
567
568 wxString dummy;
569 for (size_t f = 0; f < m_data.GetCount(); f++)
570 {
47793ab8
VS
571 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
572 continue;
573
b272b6dc 574 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
47793ab8
VS
575 name, classname, recursive);
576 if ( found )
577 {
78d14f80 578#if wxUSE_FILESYSTEM
47793ab8 579 m_curFileSystem.ChangePathTo(m_data[f].File);
78d14f80 580#endif
47793ab8
VS
581 return found;
582 }
78d14f80
VS
583 }
584
b5d6954b 585 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
78d14f80
VS
586 name.c_str(), classname.c_str());
587 return NULL;
588}
589
47793ab8
VS
590static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
591{
592 // Merge properties:
593 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
594 {
595 wxXmlProperty *dprop;
596 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
597 {
b272b6dc 598
47793ab8
VS
599 if ( dprop->GetName() == prop->GetName() )
600 {
601 dprop->SetValue(prop->GetValue());
602 break;
603 }
604 }
78d14f80 605
47793ab8
VS
606 if ( !dprop )
607 dest.AddProperty(prop->GetName(), prop->GetValue());
608 }
609
610 // Merge child nodes:
611 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
612 {
613 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
614 wxXmlNode *dnode;
615
616 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
617 {
618 if ( dnode->GetName() == node->GetName() &&
2b5f62a0 619 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
620 dnode->GetType() == node->GetType() )
621 {
622 MergeNodes(*dnode, *node);
623 break;
624 }
625 }
626
627 if ( !dnode )
b26a650c
VZ
628 {
629 static const wxChar *AT_END = wxT("end");
630 wxString insert_pos = node->GetPropVal(wxT("insert_at"), AT_END);
631 if ( insert_pos == AT_END )
632 {
633 dest.AddChild(new wxXmlNode(*node));
634 }
635 else if ( insert_pos == wxT("begin") )
636 {
637 dest.InsertChild(new wxXmlNode(*node), dest.GetChildren());
638 }
639 }
47793ab8
VS
640 }
641
642 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
643 dest.SetContent(with.GetContent());
644}
78d14f80 645
317a0d73
VS
646wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
647 wxObject *instance,
648 wxXmlResourceHandler *handlerToUse)
78d14f80
VS
649{
650 if (node == NULL) return NULL;
651
47793ab8
VS
652 // handling of referenced resource
653 if ( node->GetName() == wxT("object_ref") )
654 {
655 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
f80ea77b 656 wxXmlNode* refNode = FindResource(refName, wxEmptyString, true);
47793ab8
VS
657
658 if ( !refNode )
659 {
b272b6dc 660 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
47793ab8
VS
661 refName.c_str());
662 return NULL;
663 }
664
665 wxXmlNode copy(*refNode);
666 MergeNodes(copy, *node);
667
668 return CreateResFromNode(&copy, parent, instance);
669 }
670
78d14f80 671 wxXmlResourceHandler *handler;
317a0d73
VS
672
673 if (handlerToUse)
b380439d 674 {
317a0d73 675 if (handlerToUse->CanHandle(node))
78d14f80 676 {
317a0d73
VS
677 return handlerToUse->CreateResource(node, parent, instance);
678 }
679 }
680 else if (node->GetName() == wxT("object"))
b380439d 681 {
461932ae 682 wxList::compatibility_iterator ND = m_handlers.GetFirst();
317a0d73
VS
683 while (ND)
684 {
685 handler = (wxXmlResourceHandler*)ND->GetData();
686 if (handler->CanHandle(node))
687 {
688 return handler->CreateResource(node, parent, instance);
689 }
690 ND = ND->GetNext();
78d14f80 691 }
78d14f80
VS
692 }
693
694 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
695 node->GetName().c_str(),
696 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
697 return NULL;
698}
699
700
2b5f62a0
VZ
701#include "wx/listimpl.cpp"
702WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
17a1ebd1 703WX_DEFINE_LIST(wxXmlSubclassFactoriesList)
2b5f62a0
VZ
704
705wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
706
707/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
708{
709 if (!ms_subclassFactories)
710 {
711 ms_subclassFactories = new wxXmlSubclassFactoriesList;
2b5f62a0
VZ
712 }
713 ms_subclassFactories->Append(factory);
714}
715
716class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
717{
718public:
719 ~wxXmlSubclassFactoryCXX() {}
720
721 wxObject *Create(const wxString& className)
722 {
723 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
724
725 if (classInfo)
726 return classInfo->CreateObject();
727 else
728 return NULL;
729 }
730};
731
732
733
78d14f80 734
78d14f80
VS
735wxXmlResourceHandler::wxXmlResourceHandler()
736 : m_node(NULL), m_parent(NULL), m_instance(NULL),
9a8d8c5a 737 m_parentAsWindow(NULL)
78d14f80
VS
738{}
739
740
741
742wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
743{
744 wxXmlNode *myNode = m_node;
745 wxString myClass = m_class;
746 wxObject *myParent = m_parent, *myInstance = m_instance;
9a8d8c5a 747 wxWindow *myParentAW = m_parentAsWindow;
78d14f80 748
daa85ee3 749 m_instance = instance;
b272b6dc 750 if (!m_instance && node->HasProp(wxT("subclass")) &&
daa85ee3
VS
751 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
752 {
753 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
2b5f62a0 754 if (!subclass.empty())
daa85ee3 755 {
461932ae 756 for (wxXmlSubclassFactoriesList::compatibility_iterator i = wxXmlResource::ms_subclassFactories->GetFirst();
2b5f62a0
VZ
757 i; i = i->GetNext())
758 {
759 m_instance = i->GetData()->Create(subclass);
760 if (m_instance)
761 break;
762 }
daa85ee3 763
2b5f62a0
VZ
764 if (!m_instance)
765 {
766 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
767 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
768 subclass.c_str(), name.c_str());
769 }
770 }
daa85ee3
VS
771 }
772
78d14f80
VS
773 m_node = node;
774 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
775 m_parent = parent;
78d14f80 776 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
78d14f80
VS
777
778 wxObject *returned = DoCreateResource();
779
780 m_node = myNode;
781 m_class = myClass;
782 m_parent = myParent; m_parentAsWindow = myParentAW;
9a8d8c5a 783 m_instance = myInstance;
78d14f80
VS
784
785 return returned;
786}
787
788
789void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
790{
791 m_styleNames.Add(name);
792 m_styleValues.Add(value);
793}
794
795
796
797void wxXmlResourceHandler::AddWindowStyles()
798{
9dc579b3 799 XRC_ADD_STYLE(wxCLIP_CHILDREN);
d54a3e73
VZ
800
801 // the border styles all have the old and new names, recognize both for now
802 XRC_ADD_STYLE(wxSIMPLE_BORDER); XRC_ADD_STYLE(wxBORDER_SIMPLE);
803 XRC_ADD_STYLE(wxSUNKEN_BORDER); XRC_ADD_STYLE(wxBORDER_SUNKEN);
804 XRC_ADD_STYLE(wxDOUBLE_BORDER); XRC_ADD_STYLE(wxBORDER_DOUBLE);
805 XRC_ADD_STYLE(wxRAISED_BORDER); XRC_ADD_STYLE(wxBORDER_RAISED);
806 XRC_ADD_STYLE(wxSTATIC_BORDER); XRC_ADD_STYLE(wxBORDER_STATIC);
807 XRC_ADD_STYLE(wxNO_BORDER); XRC_ADD_STYLE(wxBORDER_NONE);
808
daa85ee3
VS
809 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
810 XRC_ADD_STYLE(wxWANTS_CHARS);
43840d8b 811 XRC_ADD_STYLE(wxTAB_TRAVERSAL);
daa85ee3 812 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
7539ba56 813 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE);
162a4f93 814 XRC_ADD_STYLE(wxALWAYS_SHOW_SB);
9f4ed861 815 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS);
b0802e64 816 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY);
78d14f80
VS
817}
818
819
820
821bool wxXmlResourceHandler::HasParam(const wxString& param)
822{
823 return (GetParamNode(param) != NULL);
824}
825
826
827int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
828{
829 wxString s = GetParamValue(param);
830
831 if (!s) return defaults;
832
2b5f62a0 833 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
834 int style = 0;
835 int index;
836 wxString fl;
837 while (tkn.HasMoreTokens())
838 {
839 fl = tkn.GetNextToken();
840 index = m_styleNames.Index(fl);
841 if (index != wxNOT_FOUND)
842 style |= m_styleValues[index];
843 else
844 wxLogError(_("Unknown style flag ") + fl);
845 }
846 return style;
847}
848
849
850
ee1046d1 851wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
78d14f80 852{
7b56015f
VS
853 wxXmlNode *parNode = GetParamNode(param);
854 wxString str1(GetNodeContent(parNode));
78d14f80
VS
855 wxString str2;
856 const wxChar *dt;
857 wxChar amp_char;
858
b272b6dc
RD
859 // VS: First version of XRC resources used $ instead of & (which is
860 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 861 // much better (because &File means "File with F underlined").
78d14f80
VS
862 if (m_resource->CompareVersion(2,3,0,1) < 0)
863 amp_char = wxT('$');
864 else
865 amp_char = wxT('_');
866
867 for (dt = str1.c_str(); *dt; dt++)
868 {
869 // Remap amp_char to &, map double amp_char to amp_char (for things
870 // like "&File..." -- this is illegal in XML, so we use "_File..."):
871 if (*dt == amp_char)
872 {
873 if ( *(++dt) == amp_char )
874 str2 << amp_char;
875 else
876 str2 << wxT('&') << *dt;
877 }
984c33c9 878 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
78d14f80
VS
879 else if (*dt == wxT('\\'))
880 switch (*(++dt))
881 {
984c33c9
VS
882 case wxT('n'):
883 str2 << wxT('\n');
884 break;
e7a3a5a5 885
984c33c9
VS
886 case wxT('t'):
887 str2 << wxT('\t');
888 break;
e7a3a5a5 889
984c33c9
VS
890 case wxT('r'):
891 str2 << wxT('\r');
892 break;
893
894 case wxT('\\') :
895 // "\\" wasn't translated to "\" prior to 2.5.3.0:
896 if (m_resource->CompareVersion(2,5,3,0) >= 0)
897 {
898 str2 << wxT('\\');
899 break;
900 }
901 // else fall-through to default: branch below
e7a3a5a5 902
984c33c9
VS
903 default:
904 str2 << wxT('\\') << *dt;
905 break;
78d14f80
VS
906 }
907 else str2 << *dt;
908 }
b272b6dc 909
7b56015f
VS
910 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
911 {
912 if (translate && parNode &&
913 parNode->GetPropVal(wxT("translate"), wxEmptyString) != wxT("0"))
914 {
915 return wxGetTranslation(str2);
916 }
917 else
918 {
919#if wxUSE_UNICODE
920 return str2;
921#else
922 // The string is internally stored as UTF-8, we have to convert
923 // it into system's default encoding so that it can be displayed:
924 return wxString(str2.mb_str(wxConvUTF8), wxConvLocal);
925#endif
926 }
927 }
8516a98b
DS
928
929 // If wxXRC_USE_LOCALE is not set, then the string is already in
930 // system's default encoding in ANSI build, so we don't have to
931 // do anything special here.
932 return str2;
78d14f80
VS
933}
934
935
936
937long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
938{
939 long value;
940 wxString str1 = GetParamValue(param);
941
942 if (!str1.ToLong(&value))
943 value = defaultv;
944
945 return value;
946}
e7a3a5a5 947
1df61962
VS
948float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv)
949{
950 double value;
951 wxString str1 = GetParamValue(param);
952
e7a3a5a5 953#ifndef __WXWINCE__
1df61962 954 const char *prevlocale = setlocale(LC_NUMERIC, "C");
e7a3a5a5
WS
955#endif
956
1df61962
VS
957 if (!str1.ToDouble(&value))
958 value = defaultv;
959
e7a3a5a5 960#ifndef __WXWINCE__
1df61962 961 setlocale(LC_NUMERIC, prevlocale);
e7a3a5a5 962#endif
78d14f80 963
17a1ebd1 964 return wx_truncate_cast(float, value);
1df61962 965}
78d14f80 966
af1337b0 967
78d14f80
VS
968int wxXmlResourceHandler::GetID()
969{
13de23f6 970 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
971}
972
973
af1337b0 974
78d14f80
VS
975wxString wxXmlResourceHandler::GetName()
976{
977 return m_node->GetPropVal(wxT("name"), wxT("-1"));
978}
979
980
981
982bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
983{
984 wxString v = GetParamValue(param);
985 v.MakeLower();
986 if (!v) return defaultv;
8516a98b
DS
987
988 return (v == wxT("1"));
78d14f80
VS
989}
990
991
1df61962
VS
992static wxColour GetSystemColour(const wxString& name)
993{
994 if (!name.empty())
995 {
996 #define SYSCLR(clr) \
997 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
998 SYSCLR(wxSYS_COLOUR_SCROLLBAR)
999 SYSCLR(wxSYS_COLOUR_BACKGROUND)
1000 SYSCLR(wxSYS_COLOUR_DESKTOP)
1001 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION)
1002 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION)
1003 SYSCLR(wxSYS_COLOUR_MENU)
1004 SYSCLR(wxSYS_COLOUR_WINDOW)
1005 SYSCLR(wxSYS_COLOUR_WINDOWFRAME)
1006 SYSCLR(wxSYS_COLOUR_MENUTEXT)
1007 SYSCLR(wxSYS_COLOUR_WINDOWTEXT)
1008 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT)
1009 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER)
1010 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER)
1011 SYSCLR(wxSYS_COLOUR_APPWORKSPACE)
1012 SYSCLR(wxSYS_COLOUR_HIGHLIGHT)
1013 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT)
1014 SYSCLR(wxSYS_COLOUR_BTNFACE)
1015 SYSCLR(wxSYS_COLOUR_3DFACE)
1016 SYSCLR(wxSYS_COLOUR_BTNSHADOW)
1017 SYSCLR(wxSYS_COLOUR_3DSHADOW)
1018 SYSCLR(wxSYS_COLOUR_GRAYTEXT)
1019 SYSCLR(wxSYS_COLOUR_BTNTEXT)
1020 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT)
1021 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT)
1022 SYSCLR(wxSYS_COLOUR_BTNHILIGHT)
1023 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT)
1024 SYSCLR(wxSYS_COLOUR_3DHILIGHT)
1025 SYSCLR(wxSYS_COLOUR_3DDKSHADOW)
1026 SYSCLR(wxSYS_COLOUR_3DLIGHT)
1027 SYSCLR(wxSYS_COLOUR_INFOTEXT)
1028 SYSCLR(wxSYS_COLOUR_INFOBK)
1029 SYSCLR(wxSYS_COLOUR_LISTBOX)
1030 SYSCLR(wxSYS_COLOUR_HOTLIGHT)
1031 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION)
1032 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION)
1033 SYSCLR(wxSYS_COLOUR_MENUHILIGHT)
1034 SYSCLR(wxSYS_COLOUR_MENUBAR)
1035 #undef SYSCLR
1036 }
1037
1038 return wxNullColour;
1039}
78d14f80
VS
1040
1041wxColour wxXmlResourceHandler::GetColour(const wxString& param)
1042{
1043 wxString v = GetParamValue(param);
1df61962
VS
1044
1045 // find colour using HTML syntax (#RRGGBB)
78d14f80
VS
1046 unsigned long tmp = 0;
1047
1048 if (v.Length() != 7 || v[0u] != wxT('#') ||
1049 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
1050 {
1df61962
VS
1051 // the colour doesn't use #RRGGBB format, check if it is symbolic
1052 // colour name:
1053 wxColour clr = GetSystemColour(v);
1054 if (clr.Ok())
1055 return clr;
e7a3a5a5 1056
b5d6954b 1057 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
78d14f80
VS
1058 v.c_str(), param.c_str());
1059 return wxNullColour;
1060 }
1061
1062 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
1063 (unsigned char) ((tmp & 0x00FF00) >> 8),
1064 (unsigned char) ((tmp & 0x0000FF)));
1065}
1066
1067
1068
92e898b0 1069wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
db59a97c
VS
1070 const wxArtClient& defaultArtClient,
1071 wxSize size)
78d14f80 1072{
db59a97c
VS
1073 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1074 wxXmlNode *bmpNode = GetParamNode(param);
1075 if ( bmpNode )
af1337b0 1076 {
db59a97c
VS
1077 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
1078 if ( !sid.empty() )
1079 {
b3e85292
VS
1080 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), wxEmptyString);
1081 if (scl.empty())
1082 scl = defaultArtClient;
1083 else
1084 scl = wxART_MAKE_CLIENT_ID_FROM_STR(scl);
e7a3a5a5 1085
92e898b0 1086 wxBitmap stockArt =
db59a97c 1087 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
b3e85292 1088 scl, size);
db59a97c
VS
1089 if ( stockArt.Ok() )
1090 return stockArt;
1091 }
af1337b0
JS
1092 }
1093
92e898b0 1094 /* ...or load the bitmap from file: */
78d14f80 1095 wxString name = GetParamValue(param);
e7a3a5a5 1096 if (name.empty()) return wxNullBitmap;
78d14f80
VS
1097#if wxUSE_FILESYSTEM
1098 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
1099 if (fsfile == NULL)
1100 {
d0b50ad9
VS
1101 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1102 name.c_str());
78d14f80
VS
1103 return wxNullBitmap;
1104 }
1105 wxImage img(*(fsfile->GetStream()));
1106 delete fsfile;
1107#else
45f3249b 1108 wxImage img(name);
78d14f80 1109#endif
af1337b0 1110
78d14f80
VS
1111 if (!img.Ok())
1112 {
1df61962 1113 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
45f3249b 1114 name.c_str());
78d14f80
VS
1115 return wxNullBitmap;
1116 }
1117 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 1118 return wxBitmap(img);
78d14f80
VS
1119}
1120
1121
1122
92e898b0 1123wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
db59a97c
VS
1124 const wxArtClient& defaultArtClient,
1125 wxSize size)
78d14f80 1126{
78d14f80 1127 wxIcon icon;
db59a97c 1128 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
78d14f80
VS
1129 return icon;
1130}
1131
1132
1133
1134wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
1135{
2b5f62a0
VZ
1136 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
1137
78d14f80
VS
1138 wxXmlNode *n = m_node->GetChildren();
1139
1140 while (n)
1141 {
1142 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
1143 return n;
1144 n = n->GetNext();
1145 }
1146 return NULL;
1147}
1148
1149
1150wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
1151{
1152 wxXmlNode *n = node;
1153 if (n == NULL) return wxEmptyString;
1154 n = n->GetChildren();
1155
1156 while (n)
1157 {
1158 if (n->GetType() == wxXML_TEXT_NODE ||
1159 n->GetType() == wxXML_CDATA_SECTION_NODE)
1160 return n->GetContent();
1161 n = n->GetNext();
1162 }
1163 return wxEmptyString;
1164}
1165
1166
1167
1168wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
1169{
e7a3a5a5 1170 if (param.empty())
78d14f80
VS
1171 return GetNodeContent(m_node);
1172 else
1173 return GetNodeContent(GetParamNode(param));
1174}
1175
1176
1177
0c00c86f
VS
1178wxSize wxXmlResourceHandler::GetSize(const wxString& param,
1179 wxWindow *windowToUse)
78d14f80
VS
1180{
1181 wxString s = GetParamValue(param);
e7a3a5a5 1182 if (s.empty()) s = wxT("-1,-1");
78d14f80 1183 bool is_dlg;
d1f47235 1184 long sx, sy = 0;
78d14f80
VS
1185
1186 is_dlg = s[s.Length()-1] == wxT('d');
1187 if (is_dlg) s.RemoveLast();
1188
1189 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
1190 !s.AfterLast(wxT(',')).ToLong(&sy))
1191 {
00393283 1192 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
78d14f80
VS
1193 return wxDefaultSize;
1194 }
1195
1196 if (is_dlg)
1197 {
0c00c86f
VS
1198 if (windowToUse)
1199 {
1200 return wxDLG_UNIT(windowToUse, wxSize(sx, sy));
1201 }
1202 else if (m_parentAsWindow)
1203 {
78d14f80 1204 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
0c00c86f 1205 }
78d14f80
VS
1206 else
1207 {
1208 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1209 return wxDefaultSize;
1210 }
1211 }
8516a98b
DS
1212
1213 return wxSize(sx, sy);
78d14f80
VS
1214}
1215
1216
1217
1218wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
1219{
1220 wxSize sz = GetSize(param);
1221 return wxPoint(sz.x, sz.y);
1222}
1223
1224
1225
0c00c86f
VS
1226wxCoord wxXmlResourceHandler::GetDimension(const wxString& param,
1227 wxCoord defaultv,
1228 wxWindow *windowToUse)
78d14f80
VS
1229{
1230 wxString s = GetParamValue(param);
e7a3a5a5 1231 if (s.empty()) return defaultv;
78d14f80
VS
1232 bool is_dlg;
1233 long sx;
1234
1235 is_dlg = s[s.Length()-1] == wxT('d');
1236 if (is_dlg) s.RemoveLast();
1237
1238 if (!s.ToLong(&sx))
1239 {
00393283 1240 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
78d14f80
VS
1241 return defaultv;
1242 }
1243
1244 if (is_dlg)
1245 {
0c00c86f
VS
1246 if (windowToUse)
1247 {
1248 return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x;
1249 }
1250 else if (m_parentAsWindow)
1251 {
78d14f80 1252 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
0c00c86f 1253 }
78d14f80
VS
1254 else
1255 {
1256 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1257 return defaultv;
1258 }
1259 }
8516a98b
DS
1260
1261 return sx;
78d14f80
VS
1262}
1263
1264
1df61962
VS
1265// Get system font index using indexname
1266static wxFont GetSystemFont(const wxString& name)
1267{
1268 if (!name.empty())
1269 {
1270 #define SYSFNT(fnt) \
1271 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1272 SYSFNT(wxSYS_OEM_FIXED_FONT)
1273 SYSFNT(wxSYS_ANSI_FIXED_FONT)
1274 SYSFNT(wxSYS_ANSI_VAR_FONT)
1275 SYSFNT(wxSYS_SYSTEM_FONT)
1276 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT)
1277 SYSFNT(wxSYS_DEFAULT_PALETTE)
1278 SYSFNT(wxSYS_SYSTEM_FIXED_FONT)
1279 SYSFNT(wxSYS_DEFAULT_GUI_FONT)
1280 #undef SYSFNT
1281 }
1282
1283 return wxNullFont;
1284}
78d14f80
VS
1285
1286wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1287{
1288 wxXmlNode *font_node = GetParamNode(param);
1289 if (font_node == NULL)
1290 {
00393283 1291 wxLogError(_("Cannot find font node '%s'."), param.c_str());
78d14f80
VS
1292 return wxNullFont;
1293 }
1294
1295 wxXmlNode *oldnode = m_node;
1296 m_node = font_node;
1297
1df61962 1298 // font attributes:
78d14f80 1299
1df61962 1300 // size
94245f6d 1301 int isize = -1;
1df61962 1302 bool hasSize = HasParam(wxT("size"));
e7a3a5a5 1303 if (hasSize)
94245f6d 1304 isize = GetLong(wxT("size"), -1);
78d14f80 1305
1df61962
VS
1306 // style
1307 int istyle = wxNORMAL;
1308 bool hasStyle = HasParam(wxT("style"));
1309 if (hasStyle)
1310 {
1311 wxString style = GetParamValue(wxT("style"));
e7a3a5a5 1312 if (style == wxT("italic"))
1df61962 1313 istyle = wxITALIC;
e7a3a5a5 1314 else if (style == wxT("slant"))
1df61962
VS
1315 istyle = wxSLANT;
1316 }
78d14f80 1317
1df61962
VS
1318 // weight
1319 int iweight = wxNORMAL;
1320 bool hasWeight = HasParam(wxT("weight"));
1321 if (hasWeight)
1322 {
1323 wxString weight = GetParamValue(wxT("weight"));
e7a3a5a5 1324 if (weight == wxT("bold"))
1df61962 1325 iweight = wxBOLD;
e7a3a5a5 1326 else if (weight == wxT("light"))
1df61962
VS
1327 iweight = wxLIGHT;
1328 }
e7a3a5a5 1329
1df61962
VS
1330 // underline
1331 bool hasUnderlined = HasParam(wxT("underlined"));
1332 bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false;
78d14f80 1333
1df61962
VS
1334 // family and facename
1335 int ifamily = wxDEFAULT;
1336 bool hasFamily = HasParam(wxT("family"));
1337 if (hasFamily)
78d14f80 1338 {
1df61962
VS
1339 wxString family = GetParamValue(wxT("family"));
1340 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1341 else if (family == wxT("roman")) ifamily = wxROMAN;
1342 else if (family == wxT("script")) ifamily = wxSCRIPT;
1343 else if (family == wxT("swiss")) ifamily = wxSWISS;
1344 else if (family == wxT("modern")) ifamily = wxMODERN;
1345 else if (family == wxT("teletype")) ifamily = wxTELETYPE;
1346 }
e7a3a5a5
WS
1347
1348
1df61962
VS
1349 wxString facename;
1350 bool hasFacename = HasParam(wxT("face"));
1351 if (hasFacename)
1352 {
1353 wxString faces = GetParamValue(wxT("face"));
1354 wxFontEnumerator enu;
1355 enu.EnumerateFacenames();
1356 wxStringTokenizer tk(faces, wxT(","));
1357 while (tk.HasMoreTokens())
78d14f80 1358 {
1df61962
VS
1359 int index = enu.GetFacenames()->Index(tk.GetNextToken(), false);
1360 if (index != wxNOT_FOUND)
1361 {
1362 facename = (*enu.GetFacenames())[index];
1363 break;
1364 }
78d14f80
VS
1365 }
1366 }
1367
1df61962
VS
1368 // encoding
1369 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1370 bool hasEncoding = HasParam(wxT("encoding"));
1371 if (hasEncoding)
1372 {
1373 wxString encoding = GetParamValue(wxT("encoding"));
1374 wxFontMapper mapper;
e7a3a5a5 1375 if (!encoding.empty())
1df61962
VS
1376 enc = mapper.CharsetToEncoding(encoding);
1377 if (enc == wxFONTENCODING_SYSTEM)
1378 enc = wxFONTENCODING_DEFAULT;
1379 }
78d14f80 1380
1df61962 1381 // is this font based on a system font?
94245f6d 1382 wxFont font = GetSystemFont(GetParamValue(wxT("sysfont")));
e7a3a5a5 1383
94245f6d 1384 if (font.Ok())
1df61962 1385 {
94245f6d
VZ
1386 if (hasSize && isize != -1)
1387 font.SetPointSize(isize);
1df61962 1388 else if (HasParam(wxT("relativesize")))
94245f6d 1389 font.SetPointSize(int(font.GetPointSize() *
1df61962 1390 GetFloat(wxT("relativesize"))));
e7a3a5a5 1391
1df61962 1392 if (hasStyle)
94245f6d 1393 font.SetStyle(istyle);
1df61962 1394 if (hasWeight)
94245f6d 1395 font.SetWeight(iweight);
1df61962 1396 if (hasUnderlined)
94245f6d 1397 font.SetUnderlined(underlined);
1df61962 1398 if (hasFamily)
94245f6d 1399 font.SetFamily(ifamily);
1df61962 1400 if (hasFacename)
94245f6d 1401 font.SetFaceName(facename);
1df61962 1402 if (hasEncoding)
94245f6d
VZ
1403 font.SetDefaultEncoding(enc);
1404 }
1405 else // not based on system font
1406 {
1407 font = wxFont(isize == -1 ? wxNORMAL_FONT->GetPointSize() : isize,
1408 ifamily, istyle, iweight,
1409 underlined, facename, enc);
1df61962 1410 }
8516a98b
DS
1411
1412 m_node = oldnode;
94245f6d 1413 return font;
78d14f80
VS
1414}
1415
1416
1417void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1418{
1419 //FIXME : add cursor
1420
1421 if (HasParam(wxT("exstyle")))
0099f343
JS
1422 // Have to OR it with existing style, since
1423 // some implementations (e.g. wxGTK) use the extra style
1424 // during creation
1425 wnd->SetExtraStyle(wnd->GetExtraStyle() | GetStyle(wxT("exstyle")));
78d14f80
VS
1426 if (HasParam(wxT("bg")))
1427 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1428 if (HasParam(wxT("fg")))
1429 wnd->SetForegroundColour(GetColour(wxT("fg")));
1430 if (GetBool(wxT("enabled"), 1) == 0)
f80ea77b 1431 wnd->Enable(false);
78d14f80
VS
1432 if (GetBool(wxT("focused"), 0) == 1)
1433 wnd->SetFocus();
1434 if (GetBool(wxT("hidden"), 0) == 1)
f80ea77b 1435 wnd->Show(false);
78d14f80
VS
1436#if wxUSE_TOOLTIPS
1437 if (HasParam(wxT("tooltip")))
1438 wnd->SetToolTip(GetText(wxT("tooltip")));
1439#endif
1440 if (HasParam(wxT("font")))
1441 wnd->SetFont(GetFont());
b23030d6
JS
1442 if (HasParam(wxT("help")))
1443 wnd->SetHelpText(GetText(wxT("help")));
78d14f80
VS
1444}
1445
1446
1447void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1448{
1449 wxXmlNode *n = m_node->GetChildren();
1450
1451 while (n)
1452 {
1453 if (n->GetType() == wxXML_ELEMENT_NODE &&
0fa2e104 1454 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
78d14f80 1455 {
317a0d73
VS
1456 m_resource->CreateResFromNode(n, parent, NULL,
1457 this_hnd_only ? this : NULL);
78d14f80
VS
1458 }
1459 n = n->GetNext();
1460 }
1461}
1462
1463
1464void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1465{
1466 wxXmlNode *root;
1467 if (rootnode == NULL) root = m_node; else root = rootnode;
1468 wxXmlNode *n = root->GetChildren();
1469
1470 while (n)
1471 {
1472 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1473 {
1474 CreateResource(n, parent, NULL);
1475 }
1476 n = n->GetNext();
1477 }
1478}
1479
1480
1481
1482
1483
1484
1485
5ed345b7 1486// --------------- XRCID implementation -----------------------------
78d14f80 1487
5ed345b7 1488#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1489
1490
5ed345b7 1491struct XRCID_record
78d14f80
VS
1492{
1493 int id;
00393283 1494 wxChar *key;
5ed345b7 1495 XRCID_record *next;
78d14f80
VS
1496};
1497
5ed345b7 1498static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1499
13de23f6 1500static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2)
78d14f80 1501{
78d14f80
VS
1502 int index = 0;
1503
00393283 1504 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
5ed345b7 1505 index %= XRCID_TABLE_SIZE;
78d14f80 1506
5ed345b7 1507 XRCID_record *oldrec = NULL;
5ed345b7 1508 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 1509 {
00393283 1510 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
1511 {
1512 return rec->id;
1513 }
78d14f80
VS
1514 oldrec = rec;
1515 }
1516
5ed345b7
VS
1517 XRCID_record **rec_var = (oldrec == NULL) ?
1518 &XRCID_Records[index] : &oldrec->next;
1519 *rec_var = new XRCID_record;
00393283 1520 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
1521 (*rec_var)->next = NULL;
1522
85452d74 1523 wxChar *end;
13de23f6
VS
1524 if (value_if_not_found != -2)
1525 (*rec_var)->id = value_if_not_found;
85452d74
VS
1526 else
1527 {
13de23f6
VS
1528 int asint = wxStrtol(str_id, &end, 10);
1529 if (*str_id && *end == 0)
1530 {
1531 // if str_id was integer, keep it verbosely:
1532 (*rec_var)->id = asint;
1533 }
1534 else
1535 {
3a301301 1536 (*rec_var)->id = wxNewId();
13de23f6 1537 }
85452d74
VS
1538 }
1539
78d14f80
VS
1540 return (*rec_var)->id;
1541}
1542
3b2a000e
VS
1543static void AddStdXRCID_Records();
1544
13de23f6
VS
1545/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1546{
3b2a000e
VS
1547 static bool s_stdIDsAdded = false;
1548
1549 if ( !s_stdIDsAdded )
1550 {
1551 s_stdIDsAdded = true;
1552 AddStdXRCID_Records();
1553 }
1554
13de23f6
VS
1555 return XRCID_Lookup(str_id);
1556}
1557
78d14f80 1558
5ed345b7 1559static void CleanXRCID_Record(XRCID_record *rec)
78d14f80
VS
1560{
1561 if (rec)
1562 {
5ed345b7 1563 CleanXRCID_Record(rec->next);
00393283 1564 free(rec->key);
78d14f80
VS
1565 delete rec;
1566 }
1567}
1568
5ed345b7 1569static void CleanXRCID_Records()
78d14f80 1570{
5ed345b7 1571 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
139c5871 1572 {
5ed345b7 1573 CleanXRCID_Record(XRCID_Records[i]);
139c5871
VS
1574 XRCID_Records[i] = NULL;
1575 }
78d14f80
VS
1576}
1577
13de23f6
VS
1578static void AddStdXRCID_Records()
1579{
1580#define stdID(id) XRCID_Lookup(wxT(#id), id)
1581 stdID(-1);
c369d4f1
VS
1582
1583 stdID(wxID_ANY);
1584 stdID(wxID_SEPARATOR);
e7a3a5a5 1585
c369d4f1
VS
1586 stdID(wxID_OPEN);
1587 stdID(wxID_CLOSE);
1588 stdID(wxID_NEW);
1589 stdID(wxID_SAVE);
1590 stdID(wxID_SAVEAS);
1591 stdID(wxID_REVERT);
1592 stdID(wxID_EXIT);
1593 stdID(wxID_UNDO);
1594 stdID(wxID_REDO);
1595 stdID(wxID_HELP);
1596 stdID(wxID_PRINT);
1597 stdID(wxID_PRINT_SETUP);
1598 stdID(wxID_PREVIEW);
1599 stdID(wxID_ABOUT);
1600 stdID(wxID_HELP_CONTENTS);
1601 stdID(wxID_HELP_COMMANDS);
1602 stdID(wxID_HELP_PROCEDURES);
1603 stdID(wxID_HELP_CONTEXT);
13de23f6 1604 stdID(wxID_CLOSE_ALL);
c369d4f1
VS
1605 stdID(wxID_PREFERENCES);
1606 stdID(wxID_CUT);
1607 stdID(wxID_COPY);
1608 stdID(wxID_PASTE);
1609 stdID(wxID_CLEAR);
1610 stdID(wxID_FIND);
1611 stdID(wxID_DUPLICATE);
1612 stdID(wxID_SELECTALL);
1613 stdID(wxID_DELETE);
1614 stdID(wxID_REPLACE);
1615 stdID(wxID_REPLACE_ALL);
1616 stdID(wxID_PROPERTIES);
1617 stdID(wxID_VIEW_DETAILS);
1618 stdID(wxID_VIEW_LARGEICONS);
1619 stdID(wxID_VIEW_SMALLICONS);
1620 stdID(wxID_VIEW_LIST);
1621 stdID(wxID_VIEW_SORTDATE);
1622 stdID(wxID_VIEW_SORTNAME);
1623 stdID(wxID_VIEW_SORTSIZE);
1624 stdID(wxID_VIEW_SORTTYPE);
1625 stdID(wxID_FILE1);
1626 stdID(wxID_FILE2);
1627 stdID(wxID_FILE3);
1628 stdID(wxID_FILE4);
1629 stdID(wxID_FILE5);
1630 stdID(wxID_FILE6);
1631 stdID(wxID_FILE7);
1632 stdID(wxID_FILE8);
1633 stdID(wxID_FILE9);
1634 stdID(wxID_OK);
1635 stdID(wxID_CANCEL);
1636 stdID(wxID_APPLY);
1637 stdID(wxID_YES);
1638 stdID(wxID_NO);
1639 stdID(wxID_STATIC);
1640 stdID(wxID_FORWARD);
1641 stdID(wxID_BACKWARD);
1642 stdID(wxID_DEFAULT);
1643 stdID(wxID_MORE);
1644 stdID(wxID_SETUP);
1645 stdID(wxID_RESET);
1646 stdID(wxID_CONTEXT_HELP);
1647 stdID(wxID_YESTOALL);
1648 stdID(wxID_NOTOALL);
1649 stdID(wxID_ABORT);
1650 stdID(wxID_RETRY);
1651 stdID(wxID_IGNORE);
1652 stdID(wxID_ADD);
1653 stdID(wxID_REMOVE);
1654 stdID(wxID_UP);
1655 stdID(wxID_DOWN);
1656 stdID(wxID_HOME);
1657 stdID(wxID_REFRESH);
1658 stdID(wxID_STOP);
1659 stdID(wxID_INDEX);
1660 stdID(wxID_BOLD);
1661 stdID(wxID_ITALIC);
1662 stdID(wxID_JUSTIFY_CENTER);
1663 stdID(wxID_JUSTIFY_FILL);
1664 stdID(wxID_JUSTIFY_RIGHT);
1665 stdID(wxID_JUSTIFY_LEFT);
1666 stdID(wxID_UNDERLINE);
1667 stdID(wxID_INDENT);
1668 stdID(wxID_UNINDENT);
1669 stdID(wxID_ZOOM_100);
1670 stdID(wxID_ZOOM_FIT);
1671 stdID(wxID_ZOOM_IN);
1672 stdID(wxID_ZOOM_OUT);
1673 stdID(wxID_UNDELETE);
1674 stdID(wxID_REVERT_TO_SAVED);
1675 stdID(wxID_SYSTEM_MENU);
1676 stdID(wxID_CLOSE_FRAME);
1677 stdID(wxID_MOVE_FRAME);
1678 stdID(wxID_RESIZE_FRAME);
1679 stdID(wxID_MAXIMIZE_FRAME);
1680 stdID(wxID_ICONIZE_FRAME);
1681 stdID(wxID_RESTORE_FRAME);
1682
13de23f6
VS
1683#undef stdID
1684}
78d14f80
VS
1685
1686
1687
1688
1689
1690// --------------- module and globals -----------------------------
1691
78d14f80
VS
1692class wxXmlResourceModule: public wxModule
1693{
1694DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1695public:
1696 wxXmlResourceModule() {}
824e8eaa
VS
1697 bool OnInit()
1698 {
2b5f62a0 1699 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
f80ea77b 1700 return true;
824e8eaa 1701 }
78d14f80
VS
1702 void OnExit()
1703 {
1542c42e 1704 delete wxXmlResource::Set(NULL);
461932ae
MB
1705 if(wxXmlResource::ms_subclassFactories)
1706 WX_CLEAR_LIST(wxXmlSubclassFactoriesList, *wxXmlResource::ms_subclassFactories);
2b5f62a0 1707 wxDELETE(wxXmlResource::ms_subclassFactories);
5ed345b7 1708 CleanXRCID_Records();
78d14f80
VS
1709 }
1710};
1711
1712IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1713
1714
1715// When wxXml is loaded dynamically after the application is already running
1716// then the built-in module system won't pick this one up. Add it manually.
1717void wxXmlInitResourceModule()
1718{
1719 wxModule* module = new wxXmlResourceModule;
1720 module->Init();
1721 wxModule::RegisterModule(module);
1722}
a1e4ec87
VS
1723
1724#endif // wxUSE_XRC