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
, bool translate
)
636 wxString
str1(GetParamValue(param
));
641 // VS: First version of XRC resources used $ instead of & (which is
642 // illegal in XML), but later I realized that '_' fits this purpose
643 // much better (because &File means "File with F underlined").
644 if (m_resource
->CompareVersion(2,3,0,1) < 0)
649 for (dt
= str1
.c_str(); *dt
; dt
++)
651 // Remap amp_char to &, map double amp_char to amp_char (for things
652 // like "&File..." -- this is illegal in XML, so we use "_File..."):
655 if ( *(++dt
) == amp_char
)
658 str2
<< wxT('&') << *dt
;
660 // Remap \n to CR, \r to LF, \t to TAB:
661 else if (*dt
== wxT('\\'))
664 case wxT('n') : str2
<< wxT('\n'); break;
665 case wxT('t') : str2
<< wxT('\t'); break;
666 case wxT('r') : str2
<< wxT('\r'); break;
667 default : str2
<< wxT('\\') << *dt
; break;
672 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
673 return wxGetTranslation(str2
);
681 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
684 wxString str1
= GetParamValue(param
);
686 if (!str1
.ToLong(&value
))
693 int wxXmlResourceHandler::GetID()
695 wxString sid
= GetName();
698 if (sid
== wxT("-1")) return -1;
699 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
700 #define stdID(id) else if (sid == wxT(#id)) return id
701 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
702 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
703 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
704 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
705 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
706 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
707 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
708 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
709 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
710 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
711 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
712 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
713 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
715 else return wxXmlResource::GetXRCID(sid
);
719 wxString
wxXmlResourceHandler::GetName()
721 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
726 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
728 wxString v
= GetParamValue(param
);
730 if (!v
) return defaultv
;
731 else return (v
== wxT("1"));
736 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
738 wxString v
= GetParamValue(param
);
739 unsigned long tmp
= 0;
741 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
742 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
744 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
745 v
.c_str(), param
.c_str());
749 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
750 (unsigned char) ((tmp
& 0x00FF00) >> 8),
751 (unsigned char) ((tmp
& 0x0000FF)));
756 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
758 wxString name
= GetParamValue(param
);
759 if (name
.IsEmpty()) return wxNullBitmap
;
761 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
764 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
767 wxImage
img(*(fsfile
->GetStream()));
770 wxImage
img(GetParamValue(wxT("bitmap")));
774 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
777 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
778 return wxBitmap(img
);
783 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
785 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
787 icon
.CopyFromBitmap(GetBitmap(param
, size
));
790 wxBitmap bmppt
= GetBitmap(param
, size
);
791 iconpt
= (wxIcon
*)(&bmppt
);
792 wxIcon
icon(*iconpt
);
799 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
801 wxXmlNode
*n
= m_node
->GetChildren();
805 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
813 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
816 if (n
== NULL
) return wxEmptyString
;
817 n
= n
->GetChildren();
821 if (n
->GetType() == wxXML_TEXT_NODE
||
822 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
823 return n
->GetContent();
826 return wxEmptyString
;
831 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
834 return GetNodeContent(m_node
);
836 return GetNodeContent(GetParamNode(param
));
841 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
843 wxString s
= GetParamValue(param
);
844 if (s
.IsEmpty()) s
= wxT("-1,-1");
848 is_dlg
= s
[s
.Length()-1] == wxT('d');
849 if (is_dlg
) s
.RemoveLast();
851 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
852 !s
.AfterLast(wxT(',')).ToLong(&sy
))
854 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
855 return wxDefaultSize
;
860 if (m_instanceAsWindow
)
861 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
862 else if (m_parentAsWindow
)
863 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
866 wxLogError(_("Cannot convert dialog units: dialog unknown."));
867 return wxDefaultSize
;
870 else return wxSize(sx
, sy
);
875 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
877 wxSize sz
= GetSize(param
);
878 return wxPoint(sz
.x
, sz
.y
);
883 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
885 wxString s
= GetParamValue(param
);
886 if (s
.IsEmpty()) return defaultv
;
890 is_dlg
= s
[s
.Length()-1] == wxT('d');
891 if (is_dlg
) s
.RemoveLast();
895 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
901 if (m_instanceAsWindow
)
902 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
903 else if (m_parentAsWindow
)
904 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
907 wxLogError(_("Cannot convert dialog units: dialog unknown."));
916 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
918 wxXmlNode
*font_node
= GetParamNode(param
);
919 if (font_node
== NULL
)
921 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
925 wxXmlNode
*oldnode
= m_node
;
928 long size
= GetLong(wxT("size"), 12);
930 wxString style
= GetParamValue(wxT("style"));
931 wxString weight
= GetParamValue(wxT("weight"));
932 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
933 if (style
== wxT("italic")) istyle
= wxITALIC
;
934 else if (style
== wxT("slant")) istyle
= wxSLANT
;
935 if (weight
== wxT("bold")) iweight
= wxBOLD
;
936 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
938 wxString family
= GetParamValue(wxT("family"));
939 int ifamily
= wxDEFAULT
;
940 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
941 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
942 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
943 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
944 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
946 bool underlined
= GetBool(wxT("underlined"), FALSE
);
948 wxString encoding
= GetParamValue(wxT("encoding"));
950 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
951 if (!encoding
.IsEmpty())
952 enc
= mapper
.CharsetToEncoding(encoding
);
953 if (enc
== wxFONTENCODING_SYSTEM
)
954 enc
= wxFONTENCODING_DEFAULT
;
956 wxString faces
= GetParamValue(wxT("face"));
957 wxString facename
= wxEmptyString
;
958 wxFontEnumerator enu
;
959 enu
.EnumerateFacenames();
960 wxStringTokenizer
tk(faces
, wxT(","));
961 while (tk
.HasMoreTokens())
963 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
964 if (index
!= wxNOT_FOUND
)
966 facename
= (*enu
.GetFacenames())[index
];
973 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
978 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
982 if (HasParam(wxT("exstyle")))
983 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
984 if (HasParam(wxT("bg")))
985 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
986 if (HasParam(wxT("fg")))
987 wnd
->SetForegroundColour(GetColour(wxT("fg")));
988 if (GetBool(wxT("enabled"), 1) == 0)
990 if (GetBool(wxT("focused"), 0) == 1)
992 if (GetBool(wxT("hidden"), 0) == 1)
995 if (HasParam(wxT("tooltip")))
996 wnd
->SetToolTip(GetText(wxT("tooltip")));
998 if (HasParam(wxT("font")))
999 wnd
->SetFont(GetFont());
1003 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1005 wxXmlNode
*n
= m_node
->GetChildren();
1009 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1010 n
->GetName() == wxT("object"))
1012 if (this_hnd_only
&& CanHandle(n
))
1013 CreateResource(n
, parent
, NULL
);
1015 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1022 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1025 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1026 wxXmlNode
*n
= root
->GetChildren();
1030 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1032 CreateResource(n
, parent
, NULL
);
1044 // --------------- XRCID implementation -----------------------------
1046 #define XRCID_TABLE_SIZE 1024
1056 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1058 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1060 static int XRCID_LastID
= wxID_HIGHEST
;
1064 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1065 index
%= XRCID_TABLE_SIZE
;
1067 XRCID_record
*oldrec
= NULL
;
1069 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1071 if (wxStrcmp(rec
->key
, str_id
) == 0)
1079 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1080 &XRCID_Records
[index
] : &oldrec
->next
;
1081 *rec_var
= new XRCID_record
;
1082 (*rec_var
)->id
= ++XRCID_LastID
;
1083 (*rec_var
)->key
= wxStrdup(str_id
);
1084 (*rec_var
)->next
= NULL
;
1086 return (*rec_var
)->id
;
1090 static void CleanXRCID_Record(XRCID_record
*rec
)
1094 CleanXRCID_Record(rec
->next
);
1100 static void CleanXRCID_Records()
1102 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1103 CleanXRCID_Record(XRCID_Records
[i
]);
1113 // --------------- module and globals -----------------------------
1115 class wxXmlResourceModule
: public wxModule
1117 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1119 wxXmlResourceModule() {}
1126 delete wxXmlResource::Set(NULL
);
1127 CleanXRCID_Records();
1131 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1134 // When wxXml is loaded dynamically after the application is already running
1135 // then the built-in module system won't pick this one up. Add it manually.
1136 void wxXmlInitResourceModule()
1138 wxModule
* module = new wxXmlResourceModule
;
1140 wxModule::RegisterModule(module);