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