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