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