]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
Added implementations for two cases of wxSystemSettingsNative::GetMetric to
[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
11#ifdef __GNUG__
12#pragma implementation "xmlres.h"
13#endif
14
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22#include "wx/dialog.h"
23#include "wx/panel.h"
24#include "wx/frame.h"
25#include "wx/wfstream.h"
26#include "wx/filesys.h"
317a0d73 27#include "wx/filename.h"
78d14f80
VS
28#include "wx/log.h"
29#include "wx/intl.h"
30#include "wx/tokenzr.h"
31#include "wx/fontenum.h"
32#include "wx/module.h"
33#include "wx/bitmap.h"
34#include "wx/image.h"
35#include "wx/fontmap.h"
af1337b0 36#include "wx/artprov.h"
78d14f80 37
34c6bbee 38#include "wx/xml/xml.h"
78d14f80
VS
39#include "wx/xrc/xmlres.h"
40
41#include "wx/arrimpl.cpp"
42WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
43
44
824e8eaa
VS
45wxXmlResource *wxXmlResource::ms_instance = NULL;
46
47/*static*/ wxXmlResource *wxXmlResource::Get()
48{
49 if ( !ms_instance )
50 ms_instance = new wxXmlResource;
51 return ms_instance;
52}
53
54/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res)
55{
56 wxXmlResource *old = ms_instance;
57 ms_instance = res;
58 return old;
59}
60
daa85ee3 61wxXmlResource::wxXmlResource(int flags)
78d14f80
VS
62{
63 m_handlers.DeleteContents(TRUE);
daa85ee3 64 m_flags = flags;
78d14f80
VS
65 m_version = -1;
66}
67
daa85ee3 68wxXmlResource::wxXmlResource(const wxString& filemask, int flags)
78d14f80 69{
daa85ee3 70 m_flags = flags;
78d14f80
VS
71 m_version = -1;
72 m_handlers.DeleteContents(TRUE);
73 Load(filemask);
74}
75
76wxXmlResource::~wxXmlResource()
77{
78 ClearHandlers();
79}
80
81
82bool wxXmlResource::Load(const wxString& filemask)
83{
84 wxString fnd;
85 wxXmlResourceDataRecord *drec;
86 bool iswild = wxIsWild(filemask);
87 bool rt = TRUE;
88
89#if wxUSE_FILESYSTEM
90 wxFileSystem fsys;
91# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
92# define wxXmlFindNext fsys.FindNext()
93#else
94# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
95# define wxXmlFindNext wxFindNextFile()
96#endif
97 if (iswild)
98 fnd = wxXmlFindFirst;
99 else
100 fnd = filemask;
101 while (!!fnd)
102 {
317a0d73
VS
103 // NB: Load() accepts both filenames and URLs (should probably be
104 // changed to filenames only, but embedded resources currently
105 // rely on its ability to handle URLs - FIXME). This check
106 // serves as a quick way to determine whether found name is
107 // filename and not URL:
108 if (wxFileName::FileExists(fnd))
109 {
110 // Make the name absolute filename, because the app may
111 // change working directory later:
112 wxFileName fn(fnd);
113 if (fn.IsRelative())
114 {
56acdfef 115 fn.MakeAbsolute();
317a0d73
VS
116 fnd = fn.GetFullPath();
117 }
75d38380
VS
118#if wxUSE_FILESYSTEM
119 fnd = wxFileSystem::FileNameToURL(fnd);
120#endif
317a0d73
VS
121 }
122
78d14f80 123#if wxUSE_FILESYSTEM
317a0d73
VS
124 if (fnd.Lower().Matches(wxT("*.zip")) ||
125 fnd.Lower().Matches(wxT("*.xrs")))
78d14f80 126 {
76806f59
VS
127 wxString url(wxFileSystem::FileNameToURL(fnd));
128 rt = rt && Load(url + wxT("#zip:*.xrc"));
78d14f80
VS
129 }
130 else
131#endif
132 {
133 drec = new wxXmlResourceDataRecord;
134 drec->File = fnd;
135 m_data.Add(drec);
136 }
137
138 if (iswild)
139 fnd = wxXmlFindNext;
140 else
141 fnd = wxEmptyString;
142 }
143# undef wxXmlFindFirst
144# undef wxXmlFindNext
145 return rt;
146}
147
148
854e189f 149IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject)
78d14f80
VS
150
151void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
152{
153 m_handlers.Append(handler);
154 handler->SetParentResource(this);
155}
156
92e898b0
RD
157void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler)
158{
159 m_handlers.Insert(handler);
160 handler->SetParentResource(this);
161}
162
78d14f80
VS
163
164
165void wxXmlResource::ClearHandlers()
166{
167 m_handlers.Clear();
168}
169
170
78d14f80
VS
171wxMenu *wxXmlResource::LoadMenu(const wxString& name)
172{
173 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
174}
175
176
177
4a1b9596 178wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 179{
4a1b9596 180 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
181}
182
183
184
4a1b9596 185#if wxUSE_TOOLBAR
78d14f80
VS
186wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
187{
188 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
189}
4a1b9596 190#endif
78d14f80
VS
191
192
193wxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name)
194{
4dd75a6a 195 return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);
78d14f80
VS
196}
197
198bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
199{
200 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
201}
202
203
204
205wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
206{
207 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
208}
209
210bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
211{
212 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
213}
214
92e898b0
RD
215wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name)
216{
217 return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);
218}
219
78d14f80
VS
220bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
221{
222 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
223}
224
225wxBitmap wxXmlResource::LoadBitmap(const wxString& name)
226{
227 wxBitmap *bmp = (wxBitmap*)CreateResFromNode(
228 FindResource(name, wxT("wxBitmap")), NULL, NULL);
229 wxBitmap rt;
230
231 if (bmp) { rt = *bmp; delete bmp; }
232 return rt;
233}
234
235wxIcon wxXmlResource::LoadIcon(const wxString& name)
236{
237 wxIcon *icon = (wxIcon*)CreateResFromNode(
238 FindResource(name, wxT("wxIcon")), NULL, NULL);
239 wxIcon rt;
240
241 if (icon) { rt = *icon; delete icon; }
242 return rt;
243}
244
92e898b0
RD
245
246wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname)
247{
248 return CreateResFromNode(FindResource(name, classname), parent, NULL);
249}
250
251bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname)
252{
253 return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;
254}
255
256
78d14f80
VS
257bool wxXmlResource::AttachUnknownControl(const wxString& name,
258 wxWindow *control, wxWindow *parent)
259{
260 if (parent == NULL)
261 parent = control->GetParent();
262 wxWindow *container = parent->FindWindow(name + wxT("_container"));
263 if (!container)
264 {
265 wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str());
266 return FALSE;
267 }
268 return control->Reparent(container);
269}
270
271
77b2f9b1 272static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
273{
274 wxString s;
275 bool isok;
276
277 wxXmlNode *c = node->GetChildren();
278 while (c)
279 {
280 isok = FALSE;
281 if (!c->GetPropVal(wxT("platform"), &s))
282 isok = TRUE;
283 else
284 {
2b5f62a0 285 wxStringTokenizer tkn(s, wxT(" |"));
78d14f80
VS
286
287 while (tkn.HasMoreTokens())
288 {
289 s = tkn.GetNextToken();
78d14f80 290#ifdef __WXMSW__
d003330c
VS
291 if (s == wxT("win")) isok = true;
292#endif
293#ifdef __UNIX__
294 if (s == wxT("unix")) isok = true;
295#endif
296#ifdef __MAC__
297 if (s == wxT("mac")) isok = true;
78d14f80 298#endif
d003330c
VS
299#ifdef __OS2__
300 if (s == wxT("os2")) isok = true;
301#endif
302
303 if (isok)
304 break;
78d14f80
VS
305 }
306 }
307
308 if (isok)
d7b1d73c 309 {
78d14f80 310 ProcessPlatformProperty(c);
d7b1d73c
VS
311 c = c->GetNext();
312 }
78d14f80
VS
313 else
314 {
d7b1d73c 315 wxXmlNode *c2 = c->GetNext();
db59a97c 316 node->RemoveChild(c);
78d14f80 317 delete c;
d7b1d73c 318 c = c2;
78d14f80 319 }
78d14f80
VS
320 }
321}
322
323
324
325void wxXmlResource::UpdateResources()
326{
327 bool modif;
328# if wxUSE_FILESYSTEM
329 wxFSFile *file = NULL;
330 wxFileSystem fsys;
331# endif
332
480505bc
VS
333 wxString encoding(wxT("UTF-8"));
334#if !wxUSE_UNICODE && wxUSE_INTL
335 if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 )
336 {
75d38380
VS
337 // In case we are not using wxLocale to translate strings, convert the
338 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
339 // is on, because it could break wxGetTranslation lookup.
480505bc
VS
340 encoding = wxLocale::GetSystemEncodingName();
341 }
342#endif
343
78d14f80
VS
344 for (size_t i = 0; i < m_data.GetCount(); i++)
345 {
346 modif = (m_data[i].Doc == NULL);
347
348 if (!modif)
349 {
350# if wxUSE_FILESYSTEM
351 file = fsys.OpenFile(m_data[i].File);
352 modif = file && file->GetModificationTime() > m_data[i].Time;
353 if (!file)
354 wxLogError(_("Cannot open file '%s'."), m_data[i].File.c_str());
355 wxDELETE(file);
356# else
357 modif = wxDateTime(wxFileModificationTime(m_data[i].File)) > m_data[i].Time;
358# endif
359 }
360
361 if (modif)
362 {
480505bc 363 wxInputStream *stream = NULL;
78d14f80
VS
364
365# if wxUSE_FILESYSTEM
366 file = fsys.OpenFile(m_data[i].File);
367 if (file)
368 stream = file->GetStream();
369# else
370 stream = new wxFileInputStream(m_data[i].File);
371# endif
372
373 if (stream)
374 {
375 delete m_data[i].Doc;
376 m_data[i].Doc = new wxXmlDocument;
377 }
4d876ee3 378 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
78d14f80 379 {
480505bc
VS
380 wxLogError(_("Cannot load resources from file '%s'."),
381 m_data[i].File.c_str());
78d14f80
VS
382 wxDELETE(m_data[i].Doc);
383 }
384 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
385 {
b5d6954b 386 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
78d14f80
VS
387 wxDELETE(m_data[i].Doc);
388 }
389 else
390 {
391 long version;
392 int v1, v2, v3, v4;
393 wxString verstr = m_data[i].Doc->GetRoot()->GetPropVal(
394 wxT("version"), wxT("0.0.0.0"));
395 if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"),
396 &v1, &v2, &v3, &v4) == 4)
397 version = v1*256*256*256+v2*256*256+v3*256+v4;
398 else
399 version = 0;
400 if (m_version == -1)
401 m_version = version;
402 if (m_version != version)
403 wxLogError(_("Resource files must have same version number!"));
404
405 ProcessPlatformProperty(m_data[i].Doc->GetRoot());
406 m_data[i].Time = file->GetModificationTime();
407 }
408
409# if wxUSE_FILESYSTEM
410 wxDELETE(file);
411# else
412 wxDELETE(stream);
413# endif
414 }
415 }
416}
417
418
b272b6dc
RD
419wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent,
420 const wxString& name,
421 const wxString& classname,
47793ab8
VS
422 bool recursive)
423{
424 wxString dummy;
425 wxXmlNode *node;
426
427 // first search for match at the top-level nodes (as this is
428 // where the resource is most commonly looked for):
429 for (node = parent->GetChildren(); node; node = node->GetNext())
430 {
b272b6dc
RD
431 if ( node->GetType() == wxXML_ELEMENT_NODE &&
432 (node->GetName() == wxT("object") ||
47793ab8 433 node->GetName() == wxT("object_ref")) &&
2b5f62a0
VZ
434 node->GetPropVal(wxT("name"), &dummy) && dummy == name )
435 {
436 wxString cls(node->GetPropVal(wxT("class"), wxEmptyString));
437 if (!classname || cls == classname)
438 return node;
439 // object_ref may not have 'class' property:
440 if (cls.empty() && node->GetName() == wxT("object_ref"))
441 {
442 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
443 if (refName.empty())
444 continue;
445 wxXmlNode* refNode = FindResource(refName, wxEmptyString, TRUE);
446 if (refNode &&
447 refNode->GetPropVal(wxT("class"), wxEmptyString) == classname)
448 {
449 return node;
450 }
451 }
452 }
47793ab8
VS
453 }
454
455 if ( recursive )
456 for (node = parent->GetChildren(); node; node = node->GetNext())
457 {
b272b6dc
RD
458 if ( node->GetType() == wxXML_ELEMENT_NODE &&
459 (node->GetName() == wxT("object") ||
47793ab8
VS
460 node->GetName() == wxT("object_ref")) )
461 {
462 wxXmlNode* found = DoFindResource(node, name, classname, TRUE);
463 if ( found )
464 return found;
465 }
466 }
467
468 return NULL;
469}
78d14f80 470
b272b6dc 471wxXmlNode *wxXmlResource::FindResource(const wxString& name,
47793ab8
VS
472 const wxString& classname,
473 bool recursive)
78d14f80
VS
474{
475 UpdateResources(); //ensure everything is up-to-date
476
477 wxString dummy;
478 for (size_t f = 0; f < m_data.GetCount(); f++)
479 {
47793ab8
VS
480 if ( m_data[f].Doc == NULL || m_data[f].Doc->GetRoot() == NULL )
481 continue;
482
b272b6dc 483 wxXmlNode* found = DoFindResource(m_data[f].Doc->GetRoot(),
47793ab8
VS
484 name, classname, recursive);
485 if ( found )
486 {
78d14f80 487#if wxUSE_FILESYSTEM
47793ab8 488 m_curFileSystem.ChangePathTo(m_data[f].File);
78d14f80 489#endif
47793ab8
VS
490 return found;
491 }
78d14f80
VS
492 }
493
b5d6954b 494 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
78d14f80
VS
495 name.c_str(), classname.c_str());
496 return NULL;
497}
498
47793ab8
VS
499static void MergeNodes(wxXmlNode& dest, wxXmlNode& with)
500{
501 // Merge properties:
502 for (wxXmlProperty *prop = with.GetProperties(); prop; prop = prop->GetNext())
503 {
504 wxXmlProperty *dprop;
505 for (dprop = dest.GetProperties(); dprop; dprop = dprop->GetNext())
506 {
b272b6dc 507
47793ab8
VS
508 if ( dprop->GetName() == prop->GetName() )
509 {
510 dprop->SetValue(prop->GetValue());
511 break;
512 }
513 }
78d14f80 514
47793ab8
VS
515 if ( !dprop )
516 dest.AddProperty(prop->GetName(), prop->GetValue());
517 }
518
519 // Merge child nodes:
520 for (wxXmlNode* node = with.GetChildren(); node; node = node->GetNext())
521 {
522 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
523 wxXmlNode *dnode;
524
525 for (dnode = dest.GetChildren(); dnode; dnode = dnode->GetNext() )
526 {
527 if ( dnode->GetName() == node->GetName() &&
2b5f62a0 528 dnode->GetPropVal(wxT("name"), wxEmptyString) == name &&
47793ab8
VS
529 dnode->GetType() == node->GetType() )
530 {
531 MergeNodes(*dnode, *node);
532 break;
533 }
534 }
535
536 if ( !dnode )
537 dest.AddChild(new wxXmlNode(*node));
538 }
539
540 if ( dest.GetType() == wxXML_TEXT_NODE && with.GetContent().Length() )
541 dest.SetContent(with.GetContent());
542}
78d14f80 543
317a0d73
VS
544wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent,
545 wxObject *instance,
546 wxXmlResourceHandler *handlerToUse)
78d14f80
VS
547{
548 if (node == NULL) return NULL;
549
47793ab8
VS
550 // handling of referenced resource
551 if ( node->GetName() == wxT("object_ref") )
552 {
553 wxString refName = node->GetPropVal(wxT("ref"), wxEmptyString);
554 wxXmlNode* refNode = FindResource(refName, wxEmptyString, TRUE);
555
556 if ( !refNode )
557 {
b272b6dc 558 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
47793ab8
VS
559 refName.c_str());
560 return NULL;
561 }
562
563 wxXmlNode copy(*refNode);
564 MergeNodes(copy, *node);
565
566 return CreateResFromNode(&copy, parent, instance);
567 }
568
78d14f80 569 wxXmlResourceHandler *handler;
317a0d73
VS
570
571 if (handlerToUse)
572 {
573 if (handlerToUse->CanHandle(node))
78d14f80 574 {
317a0d73
VS
575 return handlerToUse->CreateResource(node, parent, instance);
576 }
577 }
578 else if (node->GetName() == wxT("object"))
579 {
580 wxNode *ND = m_handlers.GetFirst();
581 while (ND)
582 {
583 handler = (wxXmlResourceHandler*)ND->GetData();
584 if (handler->CanHandle(node))
585 {
586 return handler->CreateResource(node, parent, instance);
587 }
588 ND = ND->GetNext();
78d14f80 589 }
78d14f80
VS
590 }
591
592 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
593 node->GetName().c_str(),
594 node->GetPropVal(wxT("class"), wxEmptyString).c_str());
595 return NULL;
596}
597
598
2b5f62a0
VZ
599#include "wx/listimpl.cpp"
600WX_DECLARE_LIST(wxXmlSubclassFactory, wxXmlSubclassFactoriesList);
601WX_DEFINE_LIST(wxXmlSubclassFactoriesList);
602
603wxXmlSubclassFactoriesList *wxXmlResource::ms_subclassFactories = NULL;
604
605/*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory *factory)
606{
607 if (!ms_subclassFactories)
608 {
609 ms_subclassFactories = new wxXmlSubclassFactoriesList;
610 ms_subclassFactories->DeleteContents(TRUE);
611 }
612 ms_subclassFactories->Append(factory);
613}
614
615class wxXmlSubclassFactoryCXX : public wxXmlSubclassFactory
616{
617public:
618 ~wxXmlSubclassFactoryCXX() {}
619
620 wxObject *Create(const wxString& className)
621 {
622 wxClassInfo* classInfo = wxClassInfo::FindClass(className);
623
624 if (classInfo)
625 return classInfo->CreateObject();
626 else
627 return NULL;
628 }
629};
630
631
632
78d14f80
VS
633
634
78d14f80
VS
635wxXmlResourceHandler::wxXmlResourceHandler()
636 : m_node(NULL), m_parent(NULL), m_instance(NULL),
637 m_parentAsWindow(NULL), m_instanceAsWindow(NULL)
638{}
639
640
641
642wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance)
643{
644 wxXmlNode *myNode = m_node;
645 wxString myClass = m_class;
646 wxObject *myParent = m_parent, *myInstance = m_instance;
647 wxWindow *myParentAW = m_parentAsWindow, *myInstanceAW = m_instanceAsWindow;
648
daa85ee3 649 m_instance = instance;
b272b6dc 650 if (!m_instance && node->HasProp(wxT("subclass")) &&
daa85ee3
VS
651 !(m_resource->GetFlags() & wxXRC_NO_SUBCLASSING))
652 {
653 wxString subclass = node->GetPropVal(wxT("subclass"), wxEmptyString);
2b5f62a0 654 if (!subclass.empty())
daa85ee3 655 {
2b5f62a0
VZ
656 for (wxXmlSubclassFactoriesList::Node *i = wxXmlResource::ms_subclassFactories->GetFirst();
657 i; i = i->GetNext())
658 {
659 m_instance = i->GetData()->Create(subclass);
660 if (m_instance)
661 break;
662 }
daa85ee3 663
2b5f62a0
VZ
664 if (!m_instance)
665 {
666 wxString name = node->GetPropVal(wxT("name"), wxEmptyString);
667 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
668 subclass.c_str(), name.c_str());
669 }
670 }
daa85ee3
VS
671 }
672
78d14f80
VS
673 m_node = node;
674 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
675 m_parent = parent;
78d14f80
VS
676 m_parentAsWindow = wxDynamicCast(m_parent, wxWindow);
677 m_instanceAsWindow = wxDynamicCast(m_instance, wxWindow);
678
679 wxObject *returned = DoCreateResource();
680
681 m_node = myNode;
682 m_class = myClass;
683 m_parent = myParent; m_parentAsWindow = myParentAW;
684 m_instance = myInstance; m_instanceAsWindow = myInstanceAW;
685
686 return returned;
687}
688
689
690void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
691{
692 m_styleNames.Add(name);
693 m_styleValues.Add(value);
694}
695
696
697
698void wxXmlResourceHandler::AddWindowStyles()
699{
daa85ee3
VS
700 XRC_ADD_STYLE(wxSIMPLE_BORDER);
701 XRC_ADD_STYLE(wxSUNKEN_BORDER);
702 XRC_ADD_STYLE(wxDOUBLE_BORDER);
703 XRC_ADD_STYLE(wxRAISED_BORDER);
704 XRC_ADD_STYLE(wxSTATIC_BORDER);
705 XRC_ADD_STYLE(wxNO_BORDER);
706 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW);
707 XRC_ADD_STYLE(wxWANTS_CHARS);
708 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
78d14f80
VS
709}
710
711
712
713bool wxXmlResourceHandler::HasParam(const wxString& param)
714{
715 return (GetParamNode(param) != NULL);
716}
717
718
719int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
720{
721 wxString s = GetParamValue(param);
722
723 if (!s) return defaults;
724
2b5f62a0 725 wxStringTokenizer tkn(s, wxT("| \t\n"), wxTOKEN_STRTOK);
78d14f80
VS
726 int style = 0;
727 int index;
728 wxString fl;
729 while (tkn.HasMoreTokens())
730 {
731 fl = tkn.GetNextToken();
732 index = m_styleNames.Index(fl);
733 if (index != wxNOT_FOUND)
734 style |= m_styleValues[index];
735 else
736 wxLogError(_("Unknown style flag ") + fl);
737 }
738 return style;
739}
740
741
742
ee1046d1 743wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate)
78d14f80 744{
718cf160 745 wxString str1(GetParamValue(param));
78d14f80
VS
746 wxString str2;
747 const wxChar *dt;
748 wxChar amp_char;
749
b272b6dc
RD
750 // VS: First version of XRC resources used $ instead of & (which is
751 // illegal in XML), but later I realized that '_' fits this purpose
718cf160 752 // much better (because &File means "File with F underlined").
78d14f80
VS
753 if (m_resource->CompareVersion(2,3,0,1) < 0)
754 amp_char = wxT('$');
755 else
756 amp_char = wxT('_');
757
758 for (dt = str1.c_str(); *dt; dt++)
759 {
760 // Remap amp_char to &, map double amp_char to amp_char (for things
761 // like "&File..." -- this is illegal in XML, so we use "_File..."):
762 if (*dt == amp_char)
763 {
764 if ( *(++dt) == amp_char )
765 str2 << amp_char;
766 else
767 str2 << wxT('&') << *dt;
768 }
769 // Remap \n to CR, \r to LF, \t to TAB:
770 else if (*dt == wxT('\\'))
771 switch (*(++dt))
772 {
773 case wxT('n') : str2 << wxT('\n'); break;
774 case wxT('t') : str2 << wxT('\t'); break;
775 case wxT('r') : str2 << wxT('\r'); break;
776 default : str2 << wxT('\\') << *dt; break;
777 }
778 else str2 << *dt;
779 }
b272b6dc 780
718cf160
VS
781 if (translate && m_resource->GetFlags() & wxXRC_USE_LOCALE)
782 return wxGetTranslation(str2);
783 else
784 return str2;
785
78d14f80
VS
786}
787
788
789
790long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
791{
792 long value;
793 wxString str1 = GetParamValue(param);
794
795 if (!str1.ToLong(&value))
796 value = defaultv;
797
798 return value;
799}
800
801
af1337b0 802
78d14f80
VS
803int wxXmlResourceHandler::GetID()
804{
13de23f6 805 return wxXmlResource::GetXRCID(GetName());
78d14f80
VS
806}
807
808
af1337b0 809
78d14f80
VS
810wxString wxXmlResourceHandler::GetName()
811{
812 return m_node->GetPropVal(wxT("name"), wxT("-1"));
813}
814
815
816
817bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
818{
819 wxString v = GetParamValue(param);
820 v.MakeLower();
821 if (!v) return defaultv;
822 else return (v == wxT("1"));
823}
824
825
826
827wxColour wxXmlResourceHandler::GetColour(const wxString& param)
828{
829 wxString v = GetParamValue(param);
830 unsigned long tmp = 0;
831
832 if (v.Length() != 7 || v[0u] != wxT('#') ||
833 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
834 {
b5d6954b 835 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
78d14f80
VS
836 v.c_str(), param.c_str());
837 return wxNullColour;
838 }
839
840 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
841 (unsigned char) ((tmp & 0x00FF00) >> 8),
842 (unsigned char) ((tmp & 0x0000FF)));
843}
844
845
846
92e898b0 847wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param,
db59a97c
VS
848 const wxArtClient& defaultArtClient,
849 wxSize size)
78d14f80 850{
db59a97c
VS
851 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
852 wxXmlNode *bmpNode = GetParamNode(param);
853 if ( bmpNode )
af1337b0 854 {
db59a97c
VS
855 wxString sid = bmpNode->GetPropVal(wxT("stock_id"), wxEmptyString);
856 if ( !sid.empty() )
857 {
858 wxString scl = bmpNode->GetPropVal(wxT("stock_client"), defaultArtClient);
92e898b0 859 wxBitmap stockArt =
db59a97c
VS
860 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid),
861 wxART_MAKE_CLIENT_ID_FROM_STR(scl),
862 size);
863 if ( stockArt.Ok() )
864 return stockArt;
865 }
af1337b0
JS
866 }
867
92e898b0 868 /* ...or load the bitmap from file: */
78d14f80 869 wxString name = GetParamValue(param);
92e898b0 870 if (name.IsEmpty()) return wxNullBitmap;
78d14f80
VS
871#if wxUSE_FILESYSTEM
872 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
873 if (fsfile == NULL)
874 {
d0b50ad9
VS
875 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
876 name.c_str());
78d14f80
VS
877 return wxNullBitmap;
878 }
879 wxImage img(*(fsfile->GetStream()));
880 delete fsfile;
881#else
882 wxImage img(GetParamValue(wxT("bitmap")));
883#endif
af1337b0 884
78d14f80
VS
885 if (!img.Ok())
886 {
b5d6954b 887 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
78d14f80
VS
888 return wxNullBitmap;
889 }
890 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
b272b6dc 891 return wxBitmap(img);
af1337b0 892
78d14f80
VS
893}
894
895
896
92e898b0 897wxIcon wxXmlResourceHandler::GetIcon(const wxString& param,
db59a97c
VS
898 const wxArtClient& defaultArtClient,
899 wxSize size)
78d14f80 900{
78d14f80 901 wxIcon icon;
db59a97c 902 icon.CopyFromBitmap(GetBitmap(param, defaultArtClient, size));
78d14f80
VS
903 return icon;
904}
905
906
907
908wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
909{
2b5f62a0
VZ
910 wxCHECK_MSG(m_node, NULL, wxT("You can't access handler data before it was initialized!"));
911
78d14f80
VS
912 wxXmlNode *n = m_node->GetChildren();
913
914 while (n)
915 {
916 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
917 return n;
918 n = n->GetNext();
919 }
920 return NULL;
921}
922
923
924wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
925{
926 wxXmlNode *n = node;
927 if (n == NULL) return wxEmptyString;
928 n = n->GetChildren();
929
930 while (n)
931 {
932 if (n->GetType() == wxXML_TEXT_NODE ||
933 n->GetType() == wxXML_CDATA_SECTION_NODE)
934 return n->GetContent();
935 n = n->GetNext();
936 }
937 return wxEmptyString;
938}
939
940
941
942wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
943{
944 if (param.IsEmpty())
945 return GetNodeContent(m_node);
946 else
947 return GetNodeContent(GetParamNode(param));
948}
949
950
951
952wxSize wxXmlResourceHandler::GetSize(const wxString& param)
953{
954 wxString s = GetParamValue(param);
955 if (s.IsEmpty()) s = wxT("-1,-1");
956 bool is_dlg;
957 long sx, sy;
958
959 is_dlg = s[s.Length()-1] == wxT('d');
960 if (is_dlg) s.RemoveLast();
961
962 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
963 !s.AfterLast(wxT(',')).ToLong(&sy))
964 {
00393283 965 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
78d14f80
VS
966 return wxDefaultSize;
967 }
968
969 if (is_dlg)
970 {
971 if (m_instanceAsWindow)
972 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
973 else if (m_parentAsWindow)
974 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
975 else
976 {
977 wxLogError(_("Cannot convert dialog units: dialog unknown."));
978 return wxDefaultSize;
979 }
980 }
981 else return wxSize(sx, sy);
982}
983
984
985
986wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
987{
988 wxSize sz = GetSize(param);
989 return wxPoint(sz.x, sz.y);
990}
991
992
993
994wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
995{
996 wxString s = GetParamValue(param);
997 if (s.IsEmpty()) return defaultv;
998 bool is_dlg;
999 long sx;
1000
1001 is_dlg = s[s.Length()-1] == wxT('d');
1002 if (is_dlg) s.RemoveLast();
1003
1004 if (!s.ToLong(&sx))
1005 {
00393283 1006 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
78d14f80
VS
1007 return defaultv;
1008 }
1009
1010 if (is_dlg)
1011 {
1012 if (m_instanceAsWindow)
1013 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
1014 else if (m_parentAsWindow)
1015 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
1016 else
1017 {
1018 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1019 return defaultv;
1020 }
1021 }
1022 else return sx;
1023}
1024
1025
1026
1027wxFont wxXmlResourceHandler::GetFont(const wxString& param)
1028{
1029 wxXmlNode *font_node = GetParamNode(param);
1030 if (font_node == NULL)
1031 {
00393283 1032 wxLogError(_("Cannot find font node '%s'."), param.c_str());
78d14f80
VS
1033 return wxNullFont;
1034 }
1035
1036 wxXmlNode *oldnode = m_node;
1037 m_node = font_node;
1038
1039 long size = GetLong(wxT("size"), 12);
1040
1041 wxString style = GetParamValue(wxT("style"));
1042 wxString weight = GetParamValue(wxT("weight"));
1043 int istyle = wxNORMAL, iweight = wxNORMAL;
1044 if (style == wxT("italic")) istyle = wxITALIC;
1045 else if (style == wxT("slant")) istyle = wxSLANT;
1046 if (weight == wxT("bold")) iweight = wxBOLD;
1047 else if (weight == wxT("light")) iweight = wxLIGHT;
1048
1049 wxString family = GetParamValue(wxT("family"));
1050 int ifamily = wxDEFAULT;
1051 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1052 else if (family == wxT("roman")) ifamily = wxROMAN;
1053 else if (family == wxT("script")) ifamily = wxSCRIPT;
1054 else if (family == wxT("swiss")) ifamily = wxSWISS;
1055 else if (family == wxT("modern")) ifamily = wxMODERN;
1056
1057 bool underlined = GetBool(wxT("underlined"), FALSE);
1058
1059 wxString encoding = GetParamValue(wxT("encoding"));
1060 wxFontMapper mapper;
1061 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
91cddacf
VS
1062 if (!encoding.IsEmpty())
1063 enc = mapper.CharsetToEncoding(encoding);
1064 if (enc == wxFONTENCODING_SYSTEM)
1065 enc = wxFONTENCODING_DEFAULT;
78d14f80
VS
1066
1067 wxString faces = GetParamValue(wxT("face"));
1068 wxString facename = wxEmptyString;
1069 wxFontEnumerator enu;
1070 enu.EnumerateFacenames();
1071 wxStringTokenizer tk(faces, wxT(","));
1072 while (tk.HasMoreTokens())
1073 {
1074 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
1075 if (index != wxNOT_FOUND)
1076 {
1077 facename = (*enu.GetFacenames())[index];
1078 break;
1079 }
1080 }
1081
1082 m_node = oldnode;
1083
1084 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
1085 return font;
1086}
1087
1088
1089void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1090{
1091 //FIXME : add cursor
1092
1093 if (HasParam(wxT("exstyle")))
1094 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
1095 if (HasParam(wxT("bg")))
1096 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1097 if (HasParam(wxT("fg")))
1098 wnd->SetForegroundColour(GetColour(wxT("fg")));
1099 if (GetBool(wxT("enabled"), 1) == 0)
1100 wnd->Enable(FALSE);
1101 if (GetBool(wxT("focused"), 0) == 1)
1102 wnd->SetFocus();
1103 if (GetBool(wxT("hidden"), 0) == 1)
1104 wnd->Show(FALSE);
1105#if wxUSE_TOOLTIPS
1106 if (HasParam(wxT("tooltip")))
1107 wnd->SetToolTip(GetText(wxT("tooltip")));
1108#endif
1109 if (HasParam(wxT("font")))
1110 wnd->SetFont(GetFont());
1111}
1112
1113
1114void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1115{
1116 wxXmlNode *n = m_node->GetChildren();
1117
1118 while (n)
1119 {
1120 if (n->GetType() == wxXML_ELEMENT_NODE &&
0fa2e104 1121 (n->GetName() == wxT("object") || n->GetName() == wxT("object_ref")))
78d14f80 1122 {
317a0d73
VS
1123 m_resource->CreateResFromNode(n, parent, NULL,
1124 this_hnd_only ? this : NULL);
78d14f80
VS
1125 }
1126 n = n->GetNext();
1127 }
1128}
1129
1130
1131void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1132{
1133 wxXmlNode *root;
1134 if (rootnode == NULL) root = m_node; else root = rootnode;
1135 wxXmlNode *n = root->GetChildren();
1136
1137 while (n)
1138 {
1139 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1140 {
1141 CreateResource(n, parent, NULL);
1142 }
1143 n = n->GetNext();
1144 }
1145}
1146
1147
1148
1149
1150
1151
1152
5ed345b7 1153// --------------- XRCID implementation -----------------------------
78d14f80 1154
5ed345b7 1155#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1156
1157
5ed345b7 1158struct XRCID_record
78d14f80
VS
1159{
1160 int id;
00393283 1161 wxChar *key;
5ed345b7 1162 XRCID_record *next;
78d14f80
VS
1163};
1164
5ed345b7 1165static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1166
13de23f6 1167static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2)
78d14f80 1168{
5ed345b7 1169 static int XRCID_LastID = wxID_HIGHEST;
78d14f80
VS
1170
1171 int index = 0;
1172
00393283 1173 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
5ed345b7 1174 index %= XRCID_TABLE_SIZE;
78d14f80 1175
5ed345b7 1176 XRCID_record *oldrec = NULL;
78d14f80 1177 int matchcnt = 0;
5ed345b7 1178 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 1179 {
00393283 1180 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
1181 {
1182 return rec->id;
1183 }
1184 matchcnt++;
1185 oldrec = rec;
1186 }
1187
5ed345b7
VS
1188 XRCID_record **rec_var = (oldrec == NULL) ?
1189 &XRCID_Records[index] : &oldrec->next;
1190 *rec_var = new XRCID_record;
00393283 1191 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
1192 (*rec_var)->next = NULL;
1193
85452d74 1194 wxChar *end;
13de23f6
VS
1195 if (value_if_not_found != -2)
1196 (*rec_var)->id = value_if_not_found;
85452d74
VS
1197 else
1198 {
13de23f6
VS
1199 int asint = wxStrtol(str_id, &end, 10);
1200 if (*str_id && *end == 0)
1201 {
1202 // if str_id was integer, keep it verbosely:
1203 (*rec_var)->id = asint;
1204 }
1205 else
1206 {
1207 (*rec_var)->id = ++XRCID_LastID;
1208 }
85452d74
VS
1209 }
1210
78d14f80
VS
1211 return (*rec_var)->id;
1212}
1213
13de23f6
VS
1214/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1215{
1216 return XRCID_Lookup(str_id);
1217}
1218
78d14f80 1219
5ed345b7 1220static void CleanXRCID_Record(XRCID_record *rec)
78d14f80
VS
1221{
1222 if (rec)
1223 {
5ed345b7 1224 CleanXRCID_Record(rec->next);
00393283 1225 free(rec->key);
78d14f80
VS
1226 delete rec;
1227 }
1228}
1229
5ed345b7 1230static void CleanXRCID_Records()
78d14f80 1231{
5ed345b7 1232 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
139c5871 1233 {
5ed345b7 1234 CleanXRCID_Record(XRCID_Records[i]);
139c5871
VS
1235 XRCID_Records[i] = NULL;
1236 }
78d14f80
VS
1237}
1238
13de23f6
VS
1239static void AddStdXRCID_Records()
1240{
1241#define stdID(id) XRCID_Lookup(wxT(#id), id)
1242 stdID(-1);
1243 stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
1244 stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
1245 stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
1246 stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
1247 stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
1248 stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
1249 stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
1250 stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
1251 stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
1252 stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
1253 stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
1254 stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
1255 stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
1256 stdID(wxID_CLOSE_ALL);
1257#undef stdID
1258}
78d14f80
VS
1259
1260
1261
1262
1263
1264// --------------- module and globals -----------------------------
1265
78d14f80
VS
1266class wxXmlResourceModule: public wxModule
1267{
1268DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1269public:
1270 wxXmlResourceModule() {}
824e8eaa
VS
1271 bool OnInit()
1272 {
13de23f6 1273 AddStdXRCID_Records();
2b5f62a0 1274 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX);
824e8eaa
VS
1275 return TRUE;
1276 }
78d14f80
VS
1277 void OnExit()
1278 {
1542c42e 1279 delete wxXmlResource::Set(NULL);
2b5f62a0 1280 wxDELETE(wxXmlResource::ms_subclassFactories);
5ed345b7 1281 CleanXRCID_Records();
78d14f80
VS
1282 }
1283};
1284
1285IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1286
1287
1288// When wxXml is loaded dynamically after the application is already running
1289// then the built-in module system won't pick this one up. Add it manually.
1290void wxXmlInitResourceModule()
1291{
1292 wxModule* module = new wxXmlResourceModule;
1293 module->Init();
1294 wxModule::RegisterModule(module);
1295}