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