]> git.saurik.com Git - wxWidgets.git/blame - src/xrc/xmlres.cpp
wxX11:
[wxWidgets.git] / src / xrc / xmlres.cpp
CommitLineData
78d14f80
VS
1/////////////////////////////////////////////////////////////////////////////
2// Name: xmlres.cpp
b5d6954b 3// Purpose: XRC resources
78d14f80
VS
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"
40WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords);
41
42
824e8eaa
VS
43wxXmlResource *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
daa85ee3 59wxXmlResource::wxXmlResource(int flags)
78d14f80
VS
60{
61 m_handlers.DeleteContents(TRUE);
daa85ee3 62 m_flags = flags;
78d14f80
VS
63 m_version = -1;
64}
65
daa85ee3 66wxXmlResource::wxXmlResource(const wxString& filemask, int flags)
78d14f80 67{
daa85ee3 68 m_flags = flags;
78d14f80
VS
69 m_version = -1;
70 m_handlers.DeleteContents(TRUE);
71 Load(filemask);
72}
73
74wxXmlResource::~wxXmlResource()
75{
76 ClearHandlers();
77}
78
79
80bool 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
00393283 102 if (filemask.Lower().Matches(wxT("*.zip")) ||
480505bc 103 filemask.Lower().Matches(wxT("*.xrs")))
78d14f80 104 {
480505bc 105 rt = rt && Load(fnd + wxT("#zip:*.xmlbin"));
78d14f80
VS
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
128void wxXmlResource::AddHandler(wxXmlResourceHandler *handler)
129{
130 m_handlers.Append(handler);
131 handler->SetParentResource(this);
132}
133
134
135
136void wxXmlResource::ClearHandlers()
137{
138 m_handlers.Clear();
139}
140
141
142
143wxMenu *wxXmlResource::LoadMenu(const wxString& name)
144{
145 return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);
146}
147
148
149
4a1b9596 150wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name)
78d14f80 151{
4a1b9596 152 return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);
78d14f80
VS
153}
154
155
156
4a1b9596 157#if wxUSE_TOOLBAR
78d14f80
VS
158wxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name)
159{
160 return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);
161}
4a1b9596 162#endif
78d14f80
VS
163
164
165wxDialog *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
173bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name)
174{
175 return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;
176}
177
178
179
180wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name)
181{
182 return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);
183}
184
185bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name)
186{
187 return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;
188}
189
190bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name)
191{
192 return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;
193}
194
195wxBitmap 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
205wxIcon 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
215bool 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
77b2f9b1 230static void ProcessPlatformProperty(wxXmlNode *node)
78d14f80
VS
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)
d7b1d73c 265 {
78d14f80 266 ProcessPlatformProperty(c);
d7b1d73c
VS
267 c = c->GetNext();
268 }
78d14f80
VS
269 else
270 {
271 node->RemoveChild(c);
d7b1d73c 272 wxXmlNode *c2 = c->GetNext();
78d14f80 273 delete c;
d7b1d73c 274 c = c2;
78d14f80 275 }
78d14f80
VS
276 }
277}
278
279
280
281void wxXmlResource::UpdateResources()
282{
283 bool modif;
284# if wxUSE_FILESYSTEM
285 wxFSFile *file = NULL;
286 wxFileSystem fsys;
287# endif
288
480505bc
VS
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
78d14f80
VS
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 {
480505bc 319 wxInputStream *stream = NULL;
78d14f80
VS
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 }
4d876ee3 334 if (!stream || !m_data[i].Doc->Load(*stream, encoding))
78d14f80 335 {
480505bc
VS
336 wxLogError(_("Cannot load resources from file '%s'."),
337 m_data[i].File.c_str());
78d14f80
VS
338 wxDELETE(m_data[i].Doc);
339 }
340 else if (m_data[i].Doc->GetRoot()->GetName() != wxT("resource"))
341 {
b5d6954b 342 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i].File.c_str());
78d14f80
VS
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
47793ab8
VS
375wxXmlNode *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}
78d14f80 411
47793ab8
VS
412wxXmlNode *wxXmlResource::FindResource(const wxString& name,
413 const wxString& classname,
414 bool recursive)
78d14f80
VS
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 {
47793ab8
VS
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 {
78d14f80 428#if wxUSE_FILESYSTEM
47793ab8 429 m_curFileSystem.ChangePathTo(m_data[f].File);
78d14f80 430#endif
47793ab8
VS
431 return found;
432 }
78d14f80
VS
433 }
434
b5d6954b 435 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
78d14f80
VS
436 name.c_str(), classname.c_str());
437 return NULL;
438}
439
47793ab8
VS
440static 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 }
78d14f80 455
47793ab8
VS
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}
78d14f80
VS
484
485wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance)
486{
487 if (node == NULL) return NULL;
488
47793ab8
VS
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
78d14f80
VS
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
78d14f80
VS
531wxXmlResourceHandler::wxXmlResourceHandler()
532 : m_node(NULL), m_parent(NULL), m_instance(NULL),
533 m_parentAsWindow(NULL), m_instanceAsWindow(NULL)
534{}
535
536
537
538wxObject *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
daa85ee3
VS
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
78d14f80
VS
564 m_node = node;
565 m_class = node->GetPropVal(wxT("class"), wxEmptyString);
566 m_parent = parent;
78d14f80
VS
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
581void wxXmlResourceHandler::AddStyle(const wxString& name, int value)
582{
583 m_styleNames.Add(name);
584 m_styleValues.Add(value);
585}
586
587
588
589void wxXmlResourceHandler::AddWindowStyles()
590{
daa85ee3
VS
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);
78d14f80
VS
600}
601
602
603
604bool wxXmlResourceHandler::HasParam(const wxString& param)
605{
606 return (GetParamNode(param) != NULL);
607}
608
609
610int 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
634wxString wxXmlResourceHandler::GetText(const wxString& param)
635{
3f2eabd5 636 wxString str1;
78d14f80
VS
637 wxString str2;
638 const wxChar *dt;
639 wxChar amp_char;
640
daa85ee3 641 if (m_resource->GetFlags() & wxXRC_USE_LOCALE)
3f2eabd5
VS
642 str1 = wxGetTranslation(GetParamValue(param));
643 else
644 str1 = GetParamValue(param);
645
b5d6954b 646 // VS: First version of XRC resources used $ instead of & (which is illegal in XML),
78d14f80
VS
647 // but later I realized that '_' fits this purpose much better (because
648 // &File means "File with F underlined").
649 if (m_resource->CompareVersion(2,3,0,1) < 0)
650 amp_char = wxT('$');
651 else
652 amp_char = wxT('_');
653
654 for (dt = str1.c_str(); *dt; dt++)
655 {
656 // Remap amp_char to &, map double amp_char to amp_char (for things
657 // like "&File..." -- this is illegal in XML, so we use "_File..."):
658 if (*dt == amp_char)
659 {
660 if ( *(++dt) == amp_char )
661 str2 << amp_char;
662 else
663 str2 << wxT('&') << *dt;
664 }
665 // Remap \n to CR, \r to LF, \t to TAB:
666 else if (*dt == wxT('\\'))
667 switch (*(++dt))
668 {
669 case wxT('n') : str2 << wxT('\n'); break;
670 case wxT('t') : str2 << wxT('\t'); break;
671 case wxT('r') : str2 << wxT('\r'); break;
672 default : str2 << wxT('\\') << *dt; break;
673 }
674 else str2 << *dt;
675 }
3f2eabd5
VS
676
677 return str2;
78d14f80
VS
678}
679
680
681
682long 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
694int wxXmlResourceHandler::GetID()
695{
696 wxString sid = GetName();
697 long num;
698
699 if (sid == wxT("-1")) return -1;
700 else if (sid.IsNumber() && sid.ToLong(&num)) return num;
701#define stdID(id) else if (sid == wxT(#id)) return id
702 stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW);
703 stdID(wxID_SAVE); stdID(wxID_SAVEAS); stdID(wxID_REVERT);
704 stdID(wxID_EXIT); stdID(wxID_UNDO); stdID(wxID_REDO);
705 stdID(wxID_HELP); stdID(wxID_PRINT); stdID(wxID_PRINT_SETUP);
706 stdID(wxID_PREVIEW); stdID(wxID_ABOUT); stdID(wxID_HELP_CONTENTS);
707 stdID(wxID_HELP_COMMANDS); stdID(wxID_HELP_PROCEDURES);
708 stdID(wxID_CUT); stdID(wxID_COPY); stdID(wxID_PASTE);
709 stdID(wxID_CLEAR); stdID(wxID_FIND); stdID(wxID_DUPLICATE);
710 stdID(wxID_SELECTALL); stdID(wxID_OK); stdID(wxID_CANCEL);
711 stdID(wxID_APPLY); stdID(wxID_YES); stdID(wxID_NO);
712 stdID(wxID_STATIC); stdID(wxID_FORWARD); stdID(wxID_BACKWARD);
713 stdID(wxID_DEFAULT); stdID(wxID_MORE); stdID(wxID_SETUP);
714 stdID(wxID_RESET); stdID(wxID_HELP_CONTEXT);
715#undef stdID
5ed345b7 716 else return wxXmlResource::GetXRCID(sid);
78d14f80
VS
717}
718
719
720wxString wxXmlResourceHandler::GetName()
721{
722 return m_node->GetPropVal(wxT("name"), wxT("-1"));
723}
724
725
726
727bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv)
728{
729 wxString v = GetParamValue(param);
730 v.MakeLower();
731 if (!v) return defaultv;
732 else return (v == wxT("1"));
733}
734
735
736
737wxColour wxXmlResourceHandler::GetColour(const wxString& param)
738{
739 wxString v = GetParamValue(param);
740 unsigned long tmp = 0;
741
742 if (v.Length() != 7 || v[0u] != wxT('#') ||
743 wxSscanf(v.c_str(), wxT("#%lX"), &tmp) != 1)
744 {
b5d6954b 745 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
78d14f80
VS
746 v.c_str(), param.c_str());
747 return wxNullColour;
748 }
749
750 return wxColour((unsigned char) ((tmp & 0xFF0000) >> 16) ,
751 (unsigned char) ((tmp & 0x00FF00) >> 8),
752 (unsigned char) ((tmp & 0x0000FF)));
753}
754
755
756
757wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, wxSize size)
758{
759 wxString name = GetParamValue(param);
760 if (name.IsEmpty()) return wxNullBitmap;
761#if wxUSE_FILESYSTEM
762 wxFSFile *fsfile = GetCurFileSystem().OpenFile(name);
763 if (fsfile == NULL)
764 {
b5d6954b 765 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
78d14f80
VS
766 return wxNullBitmap;
767 }
768 wxImage img(*(fsfile->GetStream()));
769 delete fsfile;
770#else
771 wxImage img(GetParamValue(wxT("bitmap")));
772#endif
773 if (!img.Ok())
774 {
b5d6954b 775 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param.c_str());
78d14f80
VS
776 return wxNullBitmap;
777 }
778 if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y);
779 return img.ConvertToBitmap();
780}
781
782
783
784wxIcon wxXmlResourceHandler::GetIcon(const wxString& param, wxSize size)
785{
786#if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
787 wxIcon icon;
788 icon.CopyFromBitmap(GetBitmap(param, size));
789#else
790 wxIcon *iconpt;
791 wxBitmap bmppt = GetBitmap(param, size);
792 iconpt = (wxIcon*)(&bmppt);
793 wxIcon icon(*iconpt);
794#endif
795 return icon;
796}
797
798
799
800wxXmlNode *wxXmlResourceHandler::GetParamNode(const wxString& param)
801{
802 wxXmlNode *n = m_node->GetChildren();
803
804 while (n)
805 {
806 if (n->GetType() == wxXML_ELEMENT_NODE && n->GetName() == param)
807 return n;
808 n = n->GetNext();
809 }
810 return NULL;
811}
812
813
814wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node)
815{
816 wxXmlNode *n = node;
817 if (n == NULL) return wxEmptyString;
818 n = n->GetChildren();
819
820 while (n)
821 {
822 if (n->GetType() == wxXML_TEXT_NODE ||
823 n->GetType() == wxXML_CDATA_SECTION_NODE)
824 return n->GetContent();
825 n = n->GetNext();
826 }
827 return wxEmptyString;
828}
829
830
831
832wxString wxXmlResourceHandler::GetParamValue(const wxString& param)
833{
834 if (param.IsEmpty())
835 return GetNodeContent(m_node);
836 else
837 return GetNodeContent(GetParamNode(param));
838}
839
840
841
842wxSize wxXmlResourceHandler::GetSize(const wxString& param)
843{
844 wxString s = GetParamValue(param);
845 if (s.IsEmpty()) s = wxT("-1,-1");
846 bool is_dlg;
847 long sx, sy;
848
849 is_dlg = s[s.Length()-1] == wxT('d');
850 if (is_dlg) s.RemoveLast();
851
852 if (!s.BeforeFirst(wxT(',')).ToLong(&sx) ||
853 !s.AfterLast(wxT(',')).ToLong(&sy))
854 {
00393283 855 wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str());
78d14f80
VS
856 return wxDefaultSize;
857 }
858
859 if (is_dlg)
860 {
861 if (m_instanceAsWindow)
862 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, sy));
863 else if (m_parentAsWindow)
864 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy));
865 else
866 {
867 wxLogError(_("Cannot convert dialog units: dialog unknown."));
868 return wxDefaultSize;
869 }
870 }
871 else return wxSize(sx, sy);
872}
873
874
875
876wxPoint wxXmlResourceHandler::GetPosition(const wxString& param)
877{
878 wxSize sz = GetSize(param);
879 return wxPoint(sz.x, sz.y);
880}
881
882
883
884wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv)
885{
886 wxString s = GetParamValue(param);
887 if (s.IsEmpty()) return defaultv;
888 bool is_dlg;
889 long sx;
890
891 is_dlg = s[s.Length()-1] == wxT('d');
892 if (is_dlg) s.RemoveLast();
893
894 if (!s.ToLong(&sx))
895 {
00393283 896 wxLogError(_("Cannot parse dimension from '%s'."), s.c_str());
78d14f80
VS
897 return defaultv;
898 }
899
900 if (is_dlg)
901 {
902 if (m_instanceAsWindow)
903 return wxDLG_UNIT(m_instanceAsWindow, wxSize(sx, 0)).x;
904 else if (m_parentAsWindow)
905 return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x;
906 else
907 {
908 wxLogError(_("Cannot convert dialog units: dialog unknown."));
909 return defaultv;
910 }
911 }
912 else return sx;
913}
914
915
916
917wxFont wxXmlResourceHandler::GetFont(const wxString& param)
918{
919 wxXmlNode *font_node = GetParamNode(param);
920 if (font_node == NULL)
921 {
00393283 922 wxLogError(_("Cannot find font node '%s'."), param.c_str());
78d14f80
VS
923 return wxNullFont;
924 }
925
926 wxXmlNode *oldnode = m_node;
927 m_node = font_node;
928
929 long size = GetLong(wxT("size"), 12);
930
931 wxString style = GetParamValue(wxT("style"));
932 wxString weight = GetParamValue(wxT("weight"));
933 int istyle = wxNORMAL, iweight = wxNORMAL;
934 if (style == wxT("italic")) istyle = wxITALIC;
935 else if (style == wxT("slant")) istyle = wxSLANT;
936 if (weight == wxT("bold")) iweight = wxBOLD;
937 else if (weight == wxT("light")) iweight = wxLIGHT;
938
939 wxString family = GetParamValue(wxT("family"));
940 int ifamily = wxDEFAULT;
941 if (family == wxT("decorative")) ifamily = wxDECORATIVE;
942 else if (family == wxT("roman")) ifamily = wxROMAN;
943 else if (family == wxT("script")) ifamily = wxSCRIPT;
944 else if (family == wxT("swiss")) ifamily = wxSWISS;
945 else if (family == wxT("modern")) ifamily = wxMODERN;
946
947 bool underlined = GetBool(wxT("underlined"), FALSE);
948
949 wxString encoding = GetParamValue(wxT("encoding"));
950 wxFontMapper mapper;
951 wxFontEncoding enc = wxFONTENCODING_DEFAULT;
91cddacf
VS
952 if (!encoding.IsEmpty())
953 enc = mapper.CharsetToEncoding(encoding);
954 if (enc == wxFONTENCODING_SYSTEM)
955 enc = wxFONTENCODING_DEFAULT;
78d14f80
VS
956
957 wxString faces = GetParamValue(wxT("face"));
958 wxString facename = wxEmptyString;
959 wxFontEnumerator enu;
960 enu.EnumerateFacenames();
961 wxStringTokenizer tk(faces, wxT(","));
962 while (tk.HasMoreTokens())
963 {
964 int index = enu.GetFacenames()->Index(tk.GetNextToken(), FALSE);
965 if (index != wxNOT_FOUND)
966 {
967 facename = (*enu.GetFacenames())[index];
968 break;
969 }
970 }
971
972 m_node = oldnode;
973
974 wxFont font(size, ifamily, istyle, iweight, underlined, facename, enc);
975 return font;
976}
977
978
979void wxXmlResourceHandler::SetupWindow(wxWindow *wnd)
980{
981 //FIXME : add cursor
982
983 if (HasParam(wxT("exstyle")))
984 wnd->SetExtraStyle(GetStyle(wxT("exstyle")));
985 if (HasParam(wxT("bg")))
986 wnd->SetBackgroundColour(GetColour(wxT("bg")));
987 if (HasParam(wxT("fg")))
988 wnd->SetForegroundColour(GetColour(wxT("fg")));
989 if (GetBool(wxT("enabled"), 1) == 0)
990 wnd->Enable(FALSE);
991 if (GetBool(wxT("focused"), 0) == 1)
992 wnd->SetFocus();
993 if (GetBool(wxT("hidden"), 0) == 1)
994 wnd->Show(FALSE);
995#if wxUSE_TOOLTIPS
996 if (HasParam(wxT("tooltip")))
997 wnd->SetToolTip(GetText(wxT("tooltip")));
998#endif
999 if (HasParam(wxT("font")))
1000 wnd->SetFont(GetFont());
1001}
1002
1003
1004void wxXmlResourceHandler::CreateChildren(wxObject *parent, bool this_hnd_only)
1005{
1006 wxXmlNode *n = m_node->GetChildren();
1007
1008 while (n)
1009 {
1010 if (n->GetType() == wxXML_ELEMENT_NODE &&
1011 n->GetName() == wxT("object"))
1012 {
1013 if (this_hnd_only && CanHandle(n))
1014 CreateResource(n, parent, NULL);
1015 else
1016 m_resource->CreateResFromNode(n, parent, NULL);
1017 }
1018 n = n->GetNext();
1019 }
1020}
1021
1022
1023void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode)
1024{
1025 wxXmlNode *root;
1026 if (rootnode == NULL) root = m_node; else root = rootnode;
1027 wxXmlNode *n = root->GetChildren();
1028
1029 while (n)
1030 {
1031 if (n->GetType() == wxXML_ELEMENT_NODE && CanHandle(n))
1032 {
1033 CreateResource(n, parent, NULL);
1034 }
1035 n = n->GetNext();
1036 }
1037}
1038
1039
1040
1041
1042
1043
1044
5ed345b7 1045// --------------- XRCID implementation -----------------------------
78d14f80 1046
5ed345b7 1047#define XRCID_TABLE_SIZE 1024
78d14f80
VS
1048
1049
5ed345b7 1050struct XRCID_record
78d14f80
VS
1051{
1052 int id;
00393283 1053 wxChar *key;
5ed345b7 1054 XRCID_record *next;
78d14f80
VS
1055};
1056
5ed345b7 1057static XRCID_record *XRCID_Records[XRCID_TABLE_SIZE] = {NULL};
78d14f80 1058
5ed345b7 1059/*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id)
78d14f80 1060{
5ed345b7 1061 static int XRCID_LastID = wxID_HIGHEST;
78d14f80
VS
1062
1063 int index = 0;
1064
00393283 1065 for (const wxChar *c = str_id; *c != wxT('\0'); c++) index += (int)*c;
5ed345b7 1066 index %= XRCID_TABLE_SIZE;
78d14f80 1067
5ed345b7 1068 XRCID_record *oldrec = NULL;
78d14f80 1069 int matchcnt = 0;
5ed345b7 1070 for (XRCID_record *rec = XRCID_Records[index]; rec; rec = rec->next)
78d14f80 1071 {
00393283 1072 if (wxStrcmp(rec->key, str_id) == 0)
78d14f80
VS
1073 {
1074 return rec->id;
1075 }
1076 matchcnt++;
1077 oldrec = rec;
1078 }
1079
5ed345b7
VS
1080 XRCID_record **rec_var = (oldrec == NULL) ?
1081 &XRCID_Records[index] : &oldrec->next;
1082 *rec_var = new XRCID_record;
1083 (*rec_var)->id = ++XRCID_LastID;
00393283 1084 (*rec_var)->key = wxStrdup(str_id);
78d14f80
VS
1085 (*rec_var)->next = NULL;
1086
1087 return (*rec_var)->id;
1088}
1089
1090
5ed345b7 1091static void CleanXRCID_Record(XRCID_record *rec)
78d14f80
VS
1092{
1093 if (rec)
1094 {
5ed345b7 1095 CleanXRCID_Record(rec->next);
00393283 1096 free(rec->key);
78d14f80
VS
1097 delete rec;
1098 }
1099}
1100
5ed345b7 1101static void CleanXRCID_Records()
78d14f80 1102{
5ed345b7
VS
1103 for (int i = 0; i < XRCID_TABLE_SIZE; i++)
1104 CleanXRCID_Record(XRCID_Records[i]);
78d14f80
VS
1105}
1106
1107
1108
1109
1110
1111
1112
1113
1114// --------------- module and globals -----------------------------
1115
78d14f80
VS
1116class wxXmlResourceModule: public wxModule
1117{
1118DECLARE_DYNAMIC_CLASS(wxXmlResourceModule)
1119public:
1120 wxXmlResourceModule() {}
824e8eaa
VS
1121 bool OnInit()
1122 {
1123 return TRUE;
1124 }
78d14f80
VS
1125 void OnExit()
1126 {
1542c42e 1127 delete wxXmlResource::Set(NULL);
5ed345b7 1128 CleanXRCID_Records();
78d14f80
VS
1129 }
1130};
1131
1132IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule, wxModule)
1133
1134
1135// When wxXml is loaded dynamically after the application is already running
1136// then the built-in module system won't pick this one up. Add it manually.
1137void wxXmlInitResourceModule()
1138{
1139 wxModule* module = new wxXmlResourceModule;
1140 module->Init();
1141 wxModule::RegisterModule(module);
1142}
1143
1144