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