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