1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "xmlres.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/dialog.h"
25 #include "wx/wfstream.h"
26 #include "wx/filesys.h"
29 #include "wx/tokenzr.h"
30 #include "wx/fontenum.h"
31 #include "wx/module.h"
32 #include "wx/bitmap.h"
34 #include "wx/fontmap.h"
36 #include "wx/xrc/xml.h"
37 #include "wx/xrc/xmlres.h"
39 #include "wx/arrimpl.cpp"
40 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
43 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
45 /*static*/ wxXmlResource
*wxXmlResource::Get()
48 ms_instance
= new wxXmlResource
;
52 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
54 wxXmlResource
*old
= ms_instance
;
59 wxXmlResource::wxXmlResource(int flags
)
61 m_handlers
.DeleteContents(TRUE
);
66 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
70 m_handlers
.DeleteContents(TRUE
);
74 wxXmlResource::~wxXmlResource()
80 bool wxXmlResource::Load(const wxString
& filemask
)
83 wxXmlResourceDataRecord
*drec
;
84 bool iswild
= wxIsWild(filemask
);
89 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
90 # define wxXmlFindNext fsys.FindNext()
92 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
93 # define wxXmlFindNext wxFindNextFile()
102 if (filemask
.Lower().Matches(wxT("*.zip")) ||
103 filemask
.Lower().Matches(wxT("*.xrs")))
105 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xmlbin"));
106 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
111 drec
= new wxXmlResourceDataRecord
;
121 # undef wxXmlFindFirst
122 # undef wxXmlFindNext
128 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
130 m_handlers
.Append(handler
);
131 handler
->SetParentResource(this);
136 void wxXmlResource::ClearHandlers()
143 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
145 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
150 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
152 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
158 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
160 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
165 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
167 wxDialog
*dialog
= new wxDialog
;
168 if (!LoadDialog(dialog
, parent
, name
))
169 { delete dialog
; return NULL
; }
173 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
175 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
180 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
182 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
185 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
187 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
190 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
192 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
195 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
197 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
198 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
201 if (bmp
) { rt
= *bmp
; delete bmp
; }
205 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
207 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
208 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
211 if (icon
) { rt
= *icon
; delete icon
; }
215 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
216 wxWindow
*control
, wxWindow
*parent
)
219 parent
= control
->GetParent();
220 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
223 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
226 return control
->Reparent(container
);
230 static void ProcessPlatformProperty(wxXmlNode
*node
)
235 wxXmlNode
*c
= node
->GetChildren();
239 if (!c
->GetPropVal(wxT("platform"), &s
))
243 wxStringTokenizer
tkn(s
, " |");
245 while (tkn
.HasMoreTokens())
247 s
= tkn
.GetNextToken();
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"))
266 ProcessPlatformProperty(c
);
271 node
->RemoveChild(c
);
272 wxXmlNode
*c2
= c
->GetNext();
281 void wxXmlResource::UpdateResources()
284 # if wxUSE_FILESYSTEM
285 wxFSFile
*file
= NULL
;
289 wxString
encoding(wxT("UTF-8"));
290 #if !wxUSE_UNICODE && wxUSE_INTL
291 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
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();
300 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
302 modif
= (m_data
[i
].Doc
== NULL
);
306 # if wxUSE_FILESYSTEM
307 file
= fsys
.OpenFile(m_data
[i
].File
);
308 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
310 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
313 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
319 wxInputStream
*stream
= NULL
;
321 # if wxUSE_FILESYSTEM
322 file
= fsys
.OpenFile(m_data
[i
].File
);
324 stream
= file
->GetStream();
326 stream
= new wxFileInputStream(m_data
[i
].File
);
331 delete m_data
[i
].Doc
;
332 m_data
[i
].Doc
= new wxXmlDocument
;
334 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
336 wxLogError(_("Cannot load resources from file '%s'."),
337 m_data
[i
].File
.c_str());
338 wxDELETE(m_data
[i
].Doc
);
340 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
342 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
343 wxDELETE(m_data
[i
].Doc
);
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
;
358 if (m_version
!= version
)
359 wxLogError(_("Resource files must have same version number!"));
361 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
362 m_data
[i
].Time
= file
->GetModificationTime();
365 # if wxUSE_FILESYSTEM
375 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
376 const wxString
& name
,
377 const wxString
& classname
,
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())
387 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
388 (node
->GetName() == wxT("object") ||
389 node
->GetName() == wxT("object_ref")) &&
391 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
392 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
397 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
399 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
400 (node
->GetName() == wxT("object") ||
401 node
->GetName() == wxT("object_ref")) )
403 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
412 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
413 const wxString
& classname
,
416 UpdateResources(); //ensure everything is up-to-date
419 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
421 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
424 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
425 name
, classname
, recursive
);
429 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
435 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
436 name
.c_str(), classname
.c_str());
440 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
443 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
445 wxXmlProperty
*dprop
;
446 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
449 if ( dprop
->GetName() == prop
->GetName() )
451 dprop
->SetValue(prop
->GetValue());
457 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
460 // Merge child nodes:
461 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
463 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
466 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
468 if ( dnode
->GetName() == node
->GetName() &&
469 dnode
->GetPropVal("name", wxEmptyString
) == name
&&
470 dnode
->GetType() == node
->GetType() )
472 MergeNodes(*dnode
, *node
);
478 dest
.AddChild(new wxXmlNode(*node
));
481 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
482 dest
.SetContent(with
.GetContent());
485 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
487 if (node
== NULL
) return NULL
;
489 // handling of referenced resource
490 if ( node
->GetName() == wxT("object_ref") )
492 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
493 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
497 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
502 wxXmlNode
copy(*refNode
);
503 MergeNodes(copy
, *node
);
505 return CreateResFromNode(©
, parent
, instance
);
508 wxXmlResourceHandler
*handler
;
510 wxNode
* ND
= m_handlers
.GetFirst();
513 handler
= (wxXmlResourceHandler
*)ND
->GetData();
514 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
516 ret
= handler
->CreateResource(node
, parent
, instance
);
522 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
523 node
->GetName().c_str(),
524 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
531 wxXmlResourceHandler::wxXmlResourceHandler()
532 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
533 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
538 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
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
;
545 m_instance
= instance
;
546 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
547 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
549 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
550 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
553 m_instance
= classInfo
->CreateObject();
557 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
558 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
561 m_instance
= classInfo
->CreateObject();
565 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
567 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
568 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
570 wxObject
*returned
= DoCreateResource();
574 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
575 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
581 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
583 m_styleNames
.Add(name
);
584 m_styleValues
.Add(value
);
589 void wxXmlResourceHandler::AddWindowStyles()
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
);
604 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
606 return (GetParamNode(param
) != NULL
);
610 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
612 wxString s
= GetParamValue(param
);
614 if (!s
) return defaults
;
616 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
620 while (tkn
.HasMoreTokens())
622 fl
= tkn
.GetNextToken();
623 index
= m_styleNames
.Index(fl
);
624 if (index
!= wxNOT_FOUND
)
625 style
|= m_styleValues
[index
];
627 wxLogError(_("Unknown style flag ") + fl
);
634 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
641 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
642 str1
= wxGetTranslation(GetParamValue(param
));
644 str1
= GetParamValue(param
);
646 // VS: First version of XRC resources used $ instead of & (which is illegal in XML),
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)
654 for (dt
= str1
.c_str(); *dt
; dt
++)
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..."):
660 if ( *(++dt
) == amp_char
)
663 str2
<< wxT('&') << *dt
;
665 // Remap \n to CR, \r to LF, \t to TAB:
666 else if (*dt
== wxT('\\'))
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;
682 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
685 wxString str1
= GetParamValue(param
);
687 if (!str1
.ToLong(&value
))
694 int wxXmlResourceHandler::GetID()
696 wxString sid
= GetName();
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
);
716 else return wxXmlResource::GetXRCID(sid
);
720 wxString
wxXmlResourceHandler::GetName()
722 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
727 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
729 wxString v
= GetParamValue(param
);
731 if (!v
) return defaultv
;
732 else return (v
== wxT("1"));
737 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
739 wxString v
= GetParamValue(param
);
740 unsigned long tmp
= 0;
742 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
743 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
745 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
746 v
.c_str(), param
.c_str());
750 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
751 (unsigned char) ((tmp
& 0x00FF00) >> 8),
752 (unsigned char) ((tmp
& 0x0000FF)));
757 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
759 wxString name
= GetParamValue(param
);
760 if (name
.IsEmpty()) return wxNullBitmap
;
762 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
765 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
768 wxImage
img(*(fsfile
->GetStream()));
771 wxImage
img(GetParamValue(wxT("bitmap")));
775 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
778 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
779 return img
.ConvertToBitmap();
784 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
786 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
788 icon
.CopyFromBitmap(GetBitmap(param
, size
));
791 wxBitmap bmppt
= GetBitmap(param
, size
);
792 iconpt
= (wxIcon
*)(&bmppt
);
793 wxIcon
icon(*iconpt
);
800 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
802 wxXmlNode
*n
= m_node
->GetChildren();
806 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
814 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
817 if (n
== NULL
) return wxEmptyString
;
818 n
= n
->GetChildren();
822 if (n
->GetType() == wxXML_TEXT_NODE
||
823 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
824 return n
->GetContent();
827 return wxEmptyString
;
832 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
835 return GetNodeContent(m_node
);
837 return GetNodeContent(GetParamNode(param
));
842 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
844 wxString s
= GetParamValue(param
);
845 if (s
.IsEmpty()) s
= wxT("-1,-1");
849 is_dlg
= s
[s
.Length()-1] == wxT('d');
850 if (is_dlg
) s
.RemoveLast();
852 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
853 !s
.AfterLast(wxT(',')).ToLong(&sy
))
855 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
856 return wxDefaultSize
;
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
));
867 wxLogError(_("Cannot convert dialog units: dialog unknown."));
868 return wxDefaultSize
;
871 else return wxSize(sx
, sy
);
876 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
878 wxSize sz
= GetSize(param
);
879 return wxPoint(sz
.x
, sz
.y
);
884 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
886 wxString s
= GetParamValue(param
);
887 if (s
.IsEmpty()) return defaultv
;
891 is_dlg
= s
[s
.Length()-1] == wxT('d');
892 if (is_dlg
) s
.RemoveLast();
896 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
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
;
908 wxLogError(_("Cannot convert dialog units: dialog unknown."));
917 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
919 wxXmlNode
*font_node
= GetParamNode(param
);
920 if (font_node
== NULL
)
922 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
926 wxXmlNode
*oldnode
= m_node
;
929 long size
= GetLong(wxT("size"), 12);
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
;
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
;
947 bool underlined
= GetBool(wxT("underlined"), FALSE
);
949 wxString encoding
= GetParamValue(wxT("encoding"));
951 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
952 if (!encoding
.IsEmpty())
953 enc
= mapper
.CharsetToEncoding(encoding
);
954 if (enc
== wxFONTENCODING_SYSTEM
)
955 enc
= wxFONTENCODING_DEFAULT
;
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())
964 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
965 if (index
!= wxNOT_FOUND
)
967 facename
= (*enu
.GetFacenames())[index
];
974 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
979 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
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)
991 if (GetBool(wxT("focused"), 0) == 1)
993 if (GetBool(wxT("hidden"), 0) == 1)
996 if (HasParam(wxT("tooltip")))
997 wnd
->SetToolTip(GetText(wxT("tooltip")));
999 if (HasParam(wxT("font")))
1000 wnd
->SetFont(GetFont());
1004 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1006 wxXmlNode
*n
= m_node
->GetChildren();
1010 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1011 n
->GetName() == wxT("object"))
1013 if (this_hnd_only
&& CanHandle(n
))
1014 CreateResource(n
, parent
, NULL
);
1016 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1023 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1026 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1027 wxXmlNode
*n
= root
->GetChildren();
1031 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1033 CreateResource(n
, parent
, NULL
);
1045 // --------------- XRCID implementation -----------------------------
1047 #define XRCID_TABLE_SIZE 1024
1057 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1059 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1061 static int XRCID_LastID
= wxID_HIGHEST
;
1065 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1066 index
%= XRCID_TABLE_SIZE
;
1068 XRCID_record
*oldrec
= NULL
;
1070 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1072 if (wxStrcmp(rec
->key
, str_id
) == 0)
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
;
1084 (*rec_var
)->key
= wxStrdup(str_id
);
1085 (*rec_var
)->next
= NULL
;
1087 return (*rec_var
)->id
;
1091 static void CleanXRCID_Record(XRCID_record
*rec
)
1095 CleanXRCID_Record(rec
->next
);
1101 static void CleanXRCID_Records()
1103 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1104 CleanXRCID_Record(XRCID_Records
[i
]);
1114 // --------------- module and globals -----------------------------
1116 class wxXmlResourceModule
: public wxModule
1118 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1120 wxXmlResourceModule() {}
1127 delete wxXmlResource::Set(NULL
);
1128 CleanXRCID_Records();
1132 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
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.
1137 void wxXmlInitResourceModule()
1139 wxModule
* module = new wxXmlResourceModule
;
1141 wxModule::RegisterModule(module);