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