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