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