]> git.saurik.com Git - wxWidgets.git/blob - src/xrc/xmlres.cpp
Script mods
[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 node->RemoveChild(c);
273 wxXmlNode *c2 = c->GetNext();
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 wxArtID wxXmlResourceHandler::GetStockID(const wxString& param)
723 {
724 wxXmlNode *stockIDNode = GetParamNode(param);
725 if (stockIDNode == NULL)
726 return wxEmptyString;
727
728 wxXmlNode *oldnode = m_node;
729 m_node = stockIDNode;
730
731 wxArtID sid = m_node->GetPropVal(wxT("stock_id"), wxT(""));
732
733 m_node = oldnode;
734
735 if (sid.IsEmpty()) return wxEmptyString;
736 #define stdID(id) else if (sid == wxT(#id)) return id
737 stdID(wxART_ADD_BOOKMARK); stdID(wxART_DEL_BOOKMARK);
738 stdID(wxART_HELP_SIDE_PANEL); stdID(wxART_HELP_SETTINGS);
739 stdID(wxART_HELP_BOOK); stdID(wxART_HELP_FOLDER);
740 stdID(wxART_HELP_PAGE); stdID(wxART_GO_BACK);
741 stdID(wxART_GO_FORWARD); stdID(wxART_GO_UP);
742 stdID(wxART_GO_DOWN); stdID(wxART_GO_TO_PARENT);
743 stdID(wxART_GO_HOME); stdID(wxART_FILE_OPEN);
744 stdID(wxART_PRINT); stdID(wxART_HELP); stdID(wxART_TIP);
745 stdID(wxART_REPORT_VIEW); stdID(wxART_LIST_VIEW);
746 stdID(wxART_NEW_DIR); stdID(wxART_FOLDER);
747 stdID(wxART_GO_DIR_UP); stdID(wxART_EXECUTABLE_FILE);
748 stdID(wxART_NORMAL_FILE); stdID(wxART_TICK_MARK);
749 stdID(wxART_CROSS_MARK); stdID(wxART_ERROR);
750 stdID(wxART_QUESTION); stdID(wxART_WARNING);
751 stdID(wxART_INFORMATION);
752 #undef stdID
753 else return sid;
754 }
755
756
757
758 wxArtClient wxXmlResourceHandler::GetStockClient(const wxString& param)
759 {
760 wxXmlNode *stockClientNode = GetParamNode(param);
761 if (stockClientNode == NULL)
762 return wxEmptyString;
763
764 wxXmlNode *oldnode = m_node;
765 m_node = stockClientNode;
766
767 wxArtClient sid = m_node->GetPropVal(wxT("stock_client"), wxT("wxART_OTHER"));
768
769 m_node = oldnode;
770
771 if (sid.IsEmpty()) return wxEmptyString;
772 #define stdID(id) else if (sid == wxT(#id)) return id
773 stdID(wxART_TOOLBAR); stdID(wxART_MENU);
774 stdID(wxART_FRAME_ICON); stdID(wxART_CMN_DIALOG);
775 stdID(wxART_HELP_BROWSER); stdID(wxART_MESSAGE_BOX);
776 stdID(wxART_OTHER);
777 #undef stdID
778 else return sid;
779 }
780
781
782
783 wxString wxXmlResourceHandler::GetName()
784 {
785 return m_node->GetPropVal(wxT("name"), wxT("-1"));
786 }
787
788
789
790 bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
791 {
792 wxString v = GetParamValue(param);
793 v.MakeLower();
794 if (!v) return defaultv;
795 else return (v == wxT("1"));
796 }
797
798
799
800 wxColour wxXmlResourceHandler::GetColour(const wxString& param)
801 {
802 wxString v = GetParamValue(param);
803 unsigned long tmp = 0;
804
805 if (v.Length() != 7 || v[0u] != wxT('#') ||
806 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
807 {
808 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
809 v.c_str(), param.c_str());
810 return wxNullColour;
811 }
812
813 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
814 (unsigned char) ((tmp & 0x00FF00) >> 8),
815 (unsigned char) ((tmp & 0x0000FF)));
816 }
817
818
819
820 wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, wxSize size)
821 {
822 wxString stk = param;
823 if (!GetStockID(stk).IsEmpty())
824 {
825 wxBitmap stockArt = wxArtProvider::GetBitmap(GetStockID(stk),
826 GetStockClient(stk), size);
827 if (stockArt != wxNullBitmap)
828 return stockArt;
829 }
830
831 wxString name = GetParamValue(param);
832 if (name.IsEmpty()) return wxNullBitmap;
833 #if wxUSE_FILESYSTEM
834 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
835 if (fsfile == NULL)
836 {
837 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
838 return wxNullBitmap;
839 }
840 wxImage img(*(fsfile->GetStream()));
841 delete fsfile;
842 #else
843 wxImage img(GetParamValue(wxT("bitmap")));
844 #endif
845
846 if (!img.Ok())
847 {
848 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
849 return wxNullBitmap;
850 }
851 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
852 return wxBitmap(img);
853
854 }
855
856
857
858 wxIcon wxXmlResourceHandler::GetIcon(const wxString& param, wxSize size)
859 {
860 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
861 wxIcon icon;
862 icon.CopyFromBitmap(GetBitmap(param, size));
863 #else
864 wxIcon *iconpt;
865 wxBitmap bmppt = GetBitmap(param, size);
866 iconpt = (wxIcon*)(&bmppt);
867 wxIcon icon(*iconpt);
868 #endif
869 return icon;
870 }
871
872
873
874 wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
875 {
876 wxXmlNode *n = m_node->GetChildren();
877
878 while (n)
879 {
880 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
881 return n;
882 n = n->GetNext();
883 }
884 return NULL;
885 }
886
887
888 wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
889 {
890 wxXmlNode *n = node;
891 if (n == NULL) return wxEmptyString;
892 n = n->GetChildren();
893
894 while (n)
895 {
896 if (n->GetType() == wxXML_TEXT_NODE ||
897 n->GetType() == wxXML_CDATA_SECTION_NODE)
898 return n->GetContent();
899 n = n->GetNext();
900 }
901 return wxEmptyString;
902 }
903
904
905
906 wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
907 {
908 if (param.IsEmpty())
909 return GetNodeContent(m_node);
910 else
911 return GetNodeContent(GetParamNode(param));
912 }
913
914
915
916 wxSize wxXmlResourceHandler::GetSize(const wxString& param)
917 {
918 wxString s = GetParamValue(param);
919 if (s.IsEmpty()) s = wxT("-1,-1");
920 bool is_dlg;
921 long sx, sy;
922
923 is_dlg = s[s.Length()-1] == wxT('d');
924 if (is_dlg) s.RemoveLast();
925
926 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
927 !s.AfterLast(wxT(',')).ToLong(&sy))
928 {
929 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
930 return wxDefaultSize;
931 }
932
933 if (is_dlg)
934 {
935 if (m_instanceAsWindow)
936 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
937 else if (m_parentAsWindow)
938 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
939 else
940 {
941 wxLogError(_("Cannot convert dialog units: dialog unknown."));
942 return wxDefaultSize;
943 }
944 }
945 else return wxSize(sx, sy);
946 }
947
948
949
950 wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
951 {
952 wxSize sz = GetSize(param);
953 return wxPoint(sz.x, sz.y);
954 }
955
956
957
958 wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
959 {
960 wxString s = GetParamValue(param);
961 if (s.IsEmpty()) return defaultv;
962 bool is_dlg;
963 long sx;
964
965 is_dlg = s[s.Length()-1] == wxT('d');
966 if (is_dlg) s.RemoveLast();
967
968 if (!s.ToLong(&sx))
969 {
970 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
971 return defaultv;
972 }
973
974 if (is_dlg)
975 {
976 if (m_instanceAsWindow)
977 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
978 else if (m_parentAsWindow)
979 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
980 else
981 {
982 wxLogError(_("Cannot convert dialog units: dialog unknown."));
983 return defaultv;
984 }
985 }
986 else return sx;
987 }
988
989
990
991 wxFont wxXmlResourceHandler::GetFont(const wxString& param)
992 {
993 wxXmlNode *font_node = GetParamNode(param);
994 if (font_node == NULL)
995 {
996 wxLogError(_("Cannot find font node '%s'."), param.c_str());
997 return wxNullFont;
998 }
999
1000 wxXmlNode *oldnode = m_node;
1001 m_node = font_node;
1002
1003 long size = GetLong(wxT("size"), 12);
1004
1005 wxString style = GetParamValue(wxT("style"));
1006 wxString weight = GetParamValue(wxT("weight"));
1007 int istyle = wxNORMAL, iweight = wxNORMAL;
1008 if (style == wxT("italic")) istyle = wxITALIC;
1009 else if (style == wxT("slant")) istyle = wxSLANT;
1010 if (weight == wxT("bold")) iweight = wxBOLD;
1011 else if (weight == wxT("light")) iweight = wxLIGHT;
1012
1013 wxString family = GetParamValue(wxT("family"));
1014 int ifamily = wxDEFAULT;
1015 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
1016 else if (family == wxT("roman")) ifamily = wxROMAN;
1017 else if (family == wxT("script")) ifamily = wxSCRIPT;
1018 else if (family == wxT("swiss")) ifamily = wxSWISS;
1019 else if (family == wxT("modern")) ifamily = wxMODERN;
1020
1021 bool underlined = GetBool(wxT("underlined"), FALSE);
1022
1023 wxString encoding = GetParamValue(wxT("encoding"));
1024 wxFontMapper mapper;
1025 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
1026 if (!encoding.IsEmpty())
1027 enc = mapper.CharsetToEncoding(encoding);
1028 if (enc == wxFONTENCODING_SYSTEM)
1029 enc = wxFONTENCODING_DEFAULT;
1030
1031 wxString faces = GetParamValue(wxT("face"));
1032 wxString facename = wxEmptyString;
1033 wxFontEnumerator enu;
1034 enu.EnumerateFacenames();
1035 wxStringTokenizer tk(faces, wxT(","));
1036 while (tk.HasMoreTokens())
1037 {
1038 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
1039 if (index != wxNOT_FOUND)
1040 {
1041 facename = (*enu.GetFacenames())[index];
1042 break;
1043 }
1044 }
1045
1046 m_node = oldnode;
1047
1048 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
1049 return font;
1050 }
1051
1052
1053 void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
1054 {
1055 //FIXME : add cursor
1056
1057 if (HasParam(wxT("exstyle")))
1058 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
1059 if (HasParam(wxT("bg")))
1060 wnd->SetBackgroundColour(GetColour(wxT("bg")));
1061 if (HasParam(wxT("fg")))
1062 wnd->SetForegroundColour(GetColour(wxT("fg")));
1063 if (GetBool(wxT("enabled"), 1) == 0)
1064 wnd->Enable(FALSE);
1065 if (GetBool(wxT("focused"), 0) == 1)
1066 wnd->SetFocus();
1067 if (GetBool(wxT("hidden"), 0) == 1)
1068 wnd->Show(FALSE);
1069 #if wxUSE_TOOLTIPS
1070 if (HasParam(wxT("tooltip")))
1071 wnd->SetToolTip(GetText(wxT("tooltip")));
1072 #endif
1073 if (HasParam(wxT("font")))
1074 wnd->SetFont(GetFont());
1075 }
1076
1077
1078 void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1079 {
1080 wxXmlNode *n = m_node->GetChildren();
1081
1082 while (n)
1083 {
1084 if (n->GetType() == wxXML_ELEMENT_NODE &&
1085 n->GetName() == wxT("object"))
1086 {
1087 if (this_hnd_only && CanHandle(n))
1088 CreateResource(n, parent, NULL);
1089 else
1090 m_resource->CreateResFromNode(n, parent, NULL);
1091 }
1092 n = n->GetNext();
1093 }
1094 }
1095
1096
1097 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1098 {
1099 wxXmlNode *root;
1100 if (rootnode == NULL) root = m_node; else root = rootnode;
1101 wxXmlNode *n = root->GetChildren();
1102
1103 while (n)
1104 {
1105 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1106 {
1107 CreateResource(n, parent, NULL);
1108 }
1109 n = n->GetNext();
1110 }
1111 }
1112
1113
1114
1115
1116
1117
1118
1119 // --------------- XRCID implementation -----------------------------
1120
1121 #define XRCID_TABLE_SIZE 1024
1122
1123
1124 struct XRCID_record
1125 {
1126 int id;
1127 wxChar *key;
1128 XRCID_record *next;
1129 };
1130
1131 static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
1132
1133 /*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
1134 {
1135 static int XRCID_LastID = wxID_HIGHEST;
1136
1137 int index = 0;
1138
1139 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
1140 index %= XRCID_TABLE_SIZE;
1141
1142 XRCID_record *oldrec = NULL;
1143 int matchcnt = 0;
1144 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
1145 {
1146 if (wxStrcmp(rec->key, str_id) == 0)
1147 {
1148 return rec->id;
1149 }
1150 matchcnt++;
1151 oldrec = rec;
1152 }
1153
1154 XRCID_record **rec_var = (oldrec == NULL) ?
1155 &XRCID_Records[index] : &oldrec->next;
1156 *rec_var = new XRCID_record;
1157 (*rec_var)->id = ++XRCID_LastID;
1158 (*rec_var)->key = wxStrdup(str_id);
1159 (*rec_var)->next = NULL;
1160
1161 return (*rec_var)->id;
1162 }
1163
1164
1165 static void CleanXRCID_Record(XRCID_record *rec)
1166 {
1167 if (rec)
1168 {
1169 CleanXRCID_Record(rec->next);
1170 free(rec->key);
1171 delete rec;
1172 }
1173 }
1174
1175 static void CleanXRCID_Records()
1176 {
1177 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
1178 CleanXRCID_Record(XRCID_Records[i]);
1179 }
1180
1181
1182
1183
1184
1185
1186
1187
1188 // --------------- module and globals -----------------------------
1189
1190 class wxXmlResourceModule: public wxModule
1191 {
1192 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1193 public:
1194 wxXmlResourceModule() {}
1195 bool OnInit()
1196 {
1197 return TRUE;
1198 }
1199 void OnExit()
1200 {
1201 delete wxXmlResource::Set(NULL);
1202 CleanXRCID_Records();
1203 }
1204 };
1205
1206 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1207
1208
1209 // When wxXml is loaded dynamically after the application is already running
1210 // then the built-in module system won't pick this one up. Add it manually.
1211 void wxXmlInitResourceModule()
1212 {
1213 wxModule* module = new wxXmlResourceModule;
1214 module->Init();
1215 wxModule::RegisterModule(module);
1216 }
1217
1218