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