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