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