]> git.saurik.com Git - wxWidgets.git/blob - contrib/src/xml/xmlres.cpp
fixed wxSpinButton handler (did refer to spinbutton nodename, not wxSpinButton class)
[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 bool 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
213 void wxXmlResource::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
261 void 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
344 wxXmlNode *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
375 wxObject *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
407 wxXmlResourceHandler::wxXmlResourceHandler()
408 : m_node(NULL), m_parent(NULL), m_instance(NULL),
409 m_parentAsWindow(NULL), m_instanceAsWindow(NULL)
410 {}
411
412
413
414 wxObject *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
439 void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
440 {
441 m_styleNames.Add(name);
442 m_styleValues.Add(value);
443 }
444
445
446
447 void 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(wxTRANSPARENT_WINDOW);
455 ADD_STYLE(wxWANTS_CHARS);
456 ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE);
457 }
458
459
460
461 bool wxXmlResourceHandler::HasParam(const wxString& param)
462 {
463 return (GetParamNode(param) != NULL);
464 }
465
466
467 int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults)
468 {
469 wxString s = GetParamValue(param);
470
471 if (!s) return defaults;
472
473 wxStringTokenizer tkn(s, wxT("| "), wxTOKEN_STRTOK);
474 int style = 0;
475 int index;
476 wxString fl;
477 while (tkn.HasMoreTokens())
478 {
479 fl = tkn.GetNextToken();
480 index = m_styleNames.Index(fl);
481 if (index != wxNOT_FOUND)
482 style |= m_styleValues[index];
483 else
484 wxLogError(_("Unknown style flag ") + fl);
485 }
486 return style;
487 }
488
489
490
491 wxString wxXmlResourceHandler::GetText(const wxString& param)
492 {
493 wxString str1 = GetParamValue(param);
494 wxString str2;
495 const wxChar *dt;
496 wxChar amp_char;
497
498 // VS: First version of XML resources used $ instead of & (which is illegal in XML),
499 // but later I realized that '_' fits this purpose much better (because
500 // &File means "File with F underlined").
501 if (m_resource->CompareVersion(2,3,0,1) < 0)
502 amp_char = wxT('$');
503 else
504 amp_char = wxT('_');
505
506 for (dt = str1.c_str(); *dt; dt++)
507 {
508 // Remap amp_char to &, map double amp_char to amp_char (for things
509 // like "&File..." -- this is illegal in XML, so we use "_File..."):
510 if (*dt == amp_char)
511 {
512 if ( *(++dt) == amp_char )
513 str2 << amp_char;
514 else
515 str2 << wxT('&') << *dt;
516 }
517 // Remap \n to CR, \r to LF, \t to TAB:
518 else if (*dt == wxT('\\'))
519 switch (*(++dt))
520 {
521 case wxT('n') : str2 << wxT('\n'); break;
522 case wxT('t') : str2 << wxT('\t'); break;
523 case wxT('r') : str2 << wxT('\r'); break;
524 default : str2 << wxT('\\') << *dt; break;
525 }
526 else str2 << *dt;
527 }
528
529 if (m_resource->GetUseLocale())
530 return wxGetTranslation(str2);
531 else
532 return str2;
533 }
534
535
536
537 long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv)
538 {
539 long value;
540 wxString str1 = GetParamValue(param);
541
542 if (!str1.ToLong(&value))
543 value = defaultv;
544
545 return value;
546 }
547
548
549 int wxXmlResourceHandler::GetID()
550 {
551 wxString sid = GetName();
552 long num;
553
554 if (sid == wxT("-1")) return -1;
555 else if (sid.IsNumber() && sid.ToLong(&num)) return num;
556 #define stdID(id) else if (sid == wxT(#id)) return id
557 stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
558 stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
559 stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
560 stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
561 stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
562 stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
563 stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
564 stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
565 stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
566 stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
567 stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
568 stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
569 stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
570 #undef stdID
571 else return XMLID(sid.c_str());
572 }
573
574
575 wxString wxXmlResourceHandler::GetName()
576 {
577 return m_node->GetPropVal(wxT("name"), wxT("-1"));
578 }
579
580
581
582 bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
583 {
584 wxString v = GetParamValue(param);
585 v.MakeLower();
586 if (!v) return defaultv;
587 else return (v == wxT("1"));
588 }
589
590
591
592 wxColour wxXmlResourceHandler::GetColour(const wxString& param)
593 {
594 wxString v = GetParamValue(param);
595 unsigned long tmp = 0;
596
597 if (v.Length() != 7 || v[0u] != wxT('#') ||
598 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
599 {
600 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
601 v.c_str(), param.c_str());
602 return wxNullColour;
603 }
604
605 return wxColour((tmp & 0xFF0000) >> 16 ,
606 (tmp & 0x00FF00) >> 8,
607 (tmp & 0x0000FF));
608 }
609
610
611
612 wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, wxSize size)
613 {
614 wxString name = GetParamValue(param);
615 if (name.IsEmpty()) return wxNullBitmap;
616 #if wxUSE_FILESYSTEM
617 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
618 if (fsfile == NULL)
619 {
620 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param.mb_str());
621 return wxNullBitmap;
622 }
623 wxImage img(*(fsfile->GetStream()));
624 delete fsfile;
625 #else
626 wxImage img(GetParamValue(wxT("bitmap")));
627 #endif
628 if (!img.Ok())
629 {
630 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param.mb_str());
631 return wxNullBitmap;
632 }
633 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
634 return img.ConvertToBitmap();
635 }
636
637
638
639 wxIcon wxXmlResourceHandler::GetIcon(const wxString& param, wxSize size)
640 {
641 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
642 wxIcon icon;
643 icon.CopyFromBitmap(GetBitmap(param, size));
644 #else
645 wxIcon *iconpt;
646 wxBitmap bmppt = GetBitmap(param, size);
647 iconpt = (wxIcon*)(&bmppt);
648 wxIcon icon(*iconpt);
649 #endif
650 return icon;
651 }
652
653
654
655 wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
656 {
657 wxXmlNode *n = m_node->GetChildren();
658
659 while (n)
660 {
661 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
662 return n;
663 n = n->GetNext();
664 }
665 return NULL;
666 }
667
668
669 wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
670 {
671 wxXmlNode *n = node;
672 if (n == NULL) return wxEmptyString;
673 n = n->GetChildren();
674
675 while (n)
676 {
677 if (n->GetType() == wxXML_TEXT_NODE ||
678 n->GetType() == wxXML_CDATA_SECTION_NODE)
679 return n->GetContent();
680 n = n->GetNext();
681 }
682 return wxEmptyString;
683 }
684
685
686
687 wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
688 {
689 if (param.IsEmpty())
690 return GetNodeContent(m_node);
691 else
692 return GetNodeContent(GetParamNode(param));
693 }
694
695
696
697 wxSize wxXmlResourceHandler::GetSize(const wxString& param)
698 {
699 wxString s = GetParamValue(param);
700 if (s.IsEmpty()) s = wxT("-1,-1");
701 bool is_dlg;
702 long sx, sy;
703
704 is_dlg = s[s.Length()-1] == wxT('d');
705 if (is_dlg) s.RemoveLast();
706
707 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
708 !s.AfterLast(wxT(',')).ToLong(&sy))
709 {
710 wxLogError(_("Cannot parse coordinates from '%s'."), s.mb_str());
711 return wxDefaultSize;
712 }
713
714 if (is_dlg)
715 {
716 if (m_instanceAsWindow)
717 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
718 else if (m_parentAsWindow)
719 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
720 else
721 {
722 wxLogError(_("Cannot convert dialog units: dialog unknown."));
723 return wxDefaultSize;
724 }
725 }
726 else return wxSize(sx, sy);
727 }
728
729
730
731 wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
732 {
733 wxSize sz = GetSize(param);
734 return wxPoint(sz.x, sz.y);
735 }
736
737
738
739 wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
740 {
741 wxString s = GetParamValue(param);
742 if (s.IsEmpty()) return defaultv;
743 bool is_dlg;
744 long sx;
745
746 is_dlg = s[s.Length()-1] == wxT('d');
747 if (is_dlg) s.RemoveLast();
748
749 if (!s.ToLong(&sx))
750 {
751 wxLogError(_("Cannot parse dimension from '%s'."), s.mb_str());
752 return defaultv;
753 }
754
755 if (is_dlg)
756 {
757 if (m_instanceAsWindow)
758 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
759 else if (m_parentAsWindow)
760 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
761 else
762 {
763 wxLogError(_("Cannot convert dialog units: dialog unknown."));
764 return defaultv;
765 }
766 }
767 else return sx;
768 }
769
770
771
772 wxFont wxXmlResourceHandler::GetFont(const wxString& param)
773 {
774 wxXmlNode *font_node = GetParamNode(param);
775 if (font_node == NULL)
776 {
777 wxLogError(_("Cannot find font node '%s'."), param.mb_str());
778 return wxNullFont;
779 }
780
781 wxXmlNode *oldnode = m_node;
782 m_node = font_node;
783
784 long size = GetLong(wxT("size"), 12);
785
786 wxString style = GetParamValue(wxT("style"));
787 wxString weight = GetParamValue(wxT("weight"));
788 int istyle = wxNORMAL, iweight = wxNORMAL;
789 if (style == wxT("italic")) istyle = wxITALIC;
790 else if (style == wxT("slant")) istyle = wxSLANT;
791 if (weight == wxT("bold")) iweight = wxBOLD;
792 else if (weight == wxT("light")) iweight = wxLIGHT;
793
794 wxString family = GetParamValue(wxT("family"));
795 int ifamily = wxDEFAULT;
796 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
797 else if (family == wxT("roman")) ifamily = wxROMAN;
798 else if (family == wxT("script")) ifamily = wxSCRIPT;
799 else if (family == wxT("swiss")) ifamily = wxSWISS;
800 else if (family == wxT("modern")) ifamily = wxMODERN;
801
802 bool underlined = GetBool(wxT("underlined"), FALSE);
803
804 wxString encoding = GetParamValue(wxT("encoding"));
805 wxFontMapper mapper;
806 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
807 if (!encoding.IsEmpty()) enc = mapper.CharsetToEncoding(encoding);
808 if (enc == wxFONTENCODING_SYSTEM) enc = wxFONTENCODING_SYSTEM;
809
810 wxString faces = GetParamValue(wxT("face"));
811 wxString facename = wxEmptyString;
812 wxFontEnumerator enu;
813 enu.EnumerateFacenames();
814 wxStringTokenizer tk(faces, wxT(","));
815 while (tk.HasMoreTokens())
816 {
817 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
818 if (index != wxNOT_FOUND)
819 {
820 facename = (*enu.GetFacenames())[index];
821 break;
822 }
823 }
824
825 m_node = oldnode;
826
827 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
828 return font;
829 }
830
831
832 void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
833 {
834 //FIXME : add cursor
835
836 if (HasParam(wxT("exstyle")))
837 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
838 if (HasParam(wxT("bg")))
839 wnd->SetBackgroundColour(GetColour(wxT("bg")));
840 if (HasParam(wxT("fg")))
841 wnd->SetForegroundColour(GetColour(wxT("fg")));
842 if (GetBool(wxT("enabled"), 1) == 0)
843 wnd->Enable(FALSE);
844 if (GetBool(wxT("focused"), 0) == 1)
845 wnd->SetFocus();
846 if (GetBool(wxT("hidden"), 0) == 1)
847 wnd->Show(FALSE);
848 #if wxUSE_TOOLTIPS
849 if (HasParam(wxT("tooltip")))
850 wnd->SetToolTip(GetText(wxT("tooltip")));
851 #endif
852 if (HasParam(wxT("font")))
853 wnd->SetFont(GetFont());
854 }
855
856
857 void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
858 {
859 wxXmlNode *n = m_node->GetChildren();
860
861 while (n)
862 {
863 if (n->GetType() == wxXML_ELEMENT_NODE &&
864 n->GetName() == wxT("object"))
865 {
866 if (this_hnd_only && CanHandle(n))
867 CreateResource(n, parent, NULL);
868 else
869 m_resource->CreateResFromNode(n, parent, NULL);
870 }
871 n = n->GetNext();
872 }
873 }
874
875
876 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
877 {
878 wxXmlNode *root;
879 if (rootnode == NULL) root = m_node; else root = rootnode;
880 wxXmlNode *n = root->GetChildren();
881
882 while (n)
883 {
884 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
885 {
886 CreateResource(n, parent, NULL);
887 }
888 n = n->GetNext();
889 }
890 }
891
892
893
894
895
896
897
898 // --------------- XMLID implementation -----------------------------
899
900 #define XMLID_TABLE_SIZE 1024
901
902
903 struct XMLID_record
904 {
905 int id;
906 char *key;
907 XMLID_record *next;
908 };
909
910 static XMLID_record *XMLID_Records[XMLID_TABLE_SIZE] = {NULL};
911
912 /*static*/ int wxXmlResource::GetXMLID(const char *str_id)
913 {
914 static int XMLID_LastID = wxID_HIGHEST;
915
916 int index = 0;
917
918 for (const char *c = str_id; *c != '\0'; c++) index += (int)*c;
919 index %= XMLID_TABLE_SIZE;
920
921 XMLID_record *oldrec = NULL;
922 int matchcnt = 0;
923 for (XMLID_record *rec = XMLID_Records[index]; rec; rec = rec->next)
924 {
925 if (strcmp(rec->key, str_id) == 0)
926 {
927 return rec->id;
928 }
929 matchcnt++;
930 oldrec = rec;
931 }
932
933 XMLID_record **rec_var = (oldrec == NULL) ?
934 &XMLID_Records[index] : &oldrec->next;
935 *rec_var = new XMLID_record;
936 (*rec_var)->id = ++XMLID_LastID;
937 (*rec_var)->key = strdup(str_id);
938 (*rec_var)->next = NULL;
939
940 return (*rec_var)->id;
941 }
942
943
944 static void CleanXMLID_Record(XMLID_record *rec)
945 {
946 if (rec)
947 {
948 CleanXMLID_Record(rec->next);
949 free (rec->key);
950 delete rec;
951 }
952 }
953
954 static void CleanXMLID_Records()
955 {
956 for (int i = 0; i < XMLID_TABLE_SIZE; i++)
957 CleanXMLID_Record(XMLID_Records[i]);
958 }
959
960
961
962
963
964
965
966
967 // --------------- module and globals -----------------------------
968
969
970 static wxXmlResource gs_XmlResource;
971
972 wxXmlResource *wxTheXmlResource = &gs_XmlResource;
973
974
975 class wxXmlResourceModule: public wxModule
976 {
977 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
978 public:
979 wxXmlResourceModule() {}
980 bool OnInit() {return TRUE;}
981 void OnExit()
982 {
983 wxTheXmlResource->ClearHandlers();
984 CleanXMLID_Records();
985 }
986 };
987
988 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)