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"
35 #include "wx/artprov.h"
37 #include "wx/xrc/xml.h"
38 #include "wx/xrc/xmlres.h"
40 #include "wx/arrimpl.cpp"
41 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
44 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
46 /*static*/ wxXmlResource
*wxXmlResource::Get()
49 ms_instance
= new wxXmlResource
;
53 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
55 wxXmlResource
*old
= ms_instance
;
60 wxXmlResource::wxXmlResource(int flags
)
62 m_handlers
.DeleteContents(TRUE
);
67 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
71 m_handlers
.DeleteContents(TRUE
);
75 wxXmlResource::~wxXmlResource()
81 bool wxXmlResource::Load(const wxString
& filemask
)
84 wxXmlResourceDataRecord
*drec
;
85 bool iswild
= wxIsWild(filemask
);
90 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
91 # define wxXmlFindNext fsys.FindNext()
93 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
94 # define wxXmlFindNext wxFindNextFile()
103 if (filemask
.Lower().Matches(wxT("*.zip")) ||
104 filemask
.Lower().Matches(wxT("*.xrs")))
106 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xmlbin"));
107 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
112 drec
= new wxXmlResourceDataRecord
;
122 # undef wxXmlFindFirst
123 # undef wxXmlFindNext
129 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
131 m_handlers
.Append(handler
);
132 handler
->SetParentResource(this);
137 void wxXmlResource::ClearHandlers()
144 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
146 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
151 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
153 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
159 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
161 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
166 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
168 wxDialog
*dialog
= new wxDialog
;
169 if (!LoadDialog(dialog
, parent
, name
))
170 { delete dialog
; return NULL
; }
174 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
176 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
181 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
183 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
186 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
188 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
191 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
193 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
196 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
198 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
199 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
202 if (bmp
) { rt
= *bmp
; delete bmp
; }
206 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
208 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
209 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
212 if (icon
) { rt
= *icon
; delete icon
; }
216 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
217 wxWindow
*control
, wxWindow
*parent
)
220 parent
= control
->GetParent();
221 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
224 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
227 return control
->Reparent(container
);
231 static void ProcessPlatformProperty(wxXmlNode
*node
)
236 wxXmlNode
*c
= node
->GetChildren();
240 if (!c
->GetPropVal(wxT("platform"), &s
))
244 wxStringTokenizer
tkn(s
, " |");
246 while (tkn
.HasMoreTokens())
248 s
= tkn
.GetNextToken();
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"))
267 ProcessPlatformProperty(c
);
272 wxXmlNode
*c2
= c
->GetNext();
273 node
->RemoveChild(c
);
282 void wxXmlResource::UpdateResources()
285 # if wxUSE_FILESYSTEM
286 wxFSFile
*file
= NULL
;
290 wxString
encoding(wxT("UTF-8"));
291 #if !wxUSE_UNICODE && wxUSE_INTL
292 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
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();
301 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
303 modif
= (m_data
[i
].Doc
== NULL
);
307 # if wxUSE_FILESYSTEM
308 file
= fsys
.OpenFile(m_data
[i
].File
);
309 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
311 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
314 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
320 wxInputStream
*stream
= NULL
;
322 # if wxUSE_FILESYSTEM
323 file
= fsys
.OpenFile(m_data
[i
].File
);
325 stream
= file
->GetStream();
327 stream
= new wxFileInputStream(m_data
[i
].File
);
332 delete m_data
[i
].Doc
;
333 m_data
[i
].Doc
= new wxXmlDocument
;
335 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
337 wxLogError(_("Cannot load resources from file '%s'."),
338 m_data
[i
].File
.c_str());
339 wxDELETE(m_data
[i
].Doc
);
341 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
343 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
344 wxDELETE(m_data
[i
].Doc
);
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
;
359 if (m_version
!= version
)
360 wxLogError(_("Resource files must have same version number!"));
362 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
363 m_data
[i
].Time
= file
->GetModificationTime();
366 # if wxUSE_FILESYSTEM
376 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
377 const wxString
& name
,
378 const wxString
& classname
,
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())
388 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
389 (node
->GetName() == wxT("object") ||
390 node
->GetName() == wxT("object_ref")) &&
392 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
393 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
398 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
400 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
401 (node
->GetName() == wxT("object") ||
402 node
->GetName() == wxT("object_ref")) )
404 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
413 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
414 const wxString
& classname
,
417 UpdateResources(); //ensure everything is up-to-date
420 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
422 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
425 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
426 name
, classname
, recursive
);
430 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
436 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
437 name
.c_str(), classname
.c_str());
441 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
444 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
446 wxXmlProperty
*dprop
;
447 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
450 if ( dprop
->GetName() == prop
->GetName() )
452 dprop
->SetValue(prop
->GetValue());
458 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
461 // Merge child nodes:
462 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
464 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
467 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
469 if ( dnode
->GetName() == node
->GetName() &&
470 dnode
->GetPropVal("name", wxEmptyString
) == name
&&
471 dnode
->GetType() == node
->GetType() )
473 MergeNodes(*dnode
, *node
);
479 dest
.AddChild(new wxXmlNode(*node
));
482 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
483 dest
.SetContent(with
.GetContent());
486 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
488 if (node
== NULL
) return NULL
;
490 // handling of referenced resource
491 if ( node
->GetName() == wxT("object_ref") )
493 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
494 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
498 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
503 wxXmlNode
copy(*refNode
);
504 MergeNodes(copy
, *node
);
506 return CreateResFromNode(©
, parent
, instance
);
509 wxXmlResourceHandler
*handler
;
511 wxNode
* ND
= m_handlers
.GetFirst();
514 handler
= (wxXmlResourceHandler
*)ND
->GetData();
515 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
517 ret
= handler
->CreateResource(node
, parent
, instance
);
523 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
524 node
->GetName().c_str(),
525 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
532 wxXmlResourceHandler::wxXmlResourceHandler()
533 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
534 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
539 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
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
;
546 m_instance
= instance
;
547 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
548 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
550 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
551 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
554 m_instance
= classInfo
->CreateObject();
558 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
559 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
562 m_instance
= classInfo
->CreateObject();
566 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
568 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
569 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
571 wxObject
*returned
= DoCreateResource();
575 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
576 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
582 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
584 m_styleNames
.Add(name
);
585 m_styleValues
.Add(value
);
590 void wxXmlResourceHandler::AddWindowStyles()
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
);
605 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
607 return (GetParamNode(param
) != NULL
);
611 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
613 wxString s
= GetParamValue(param
);
615 if (!s
) return defaults
;
617 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
621 while (tkn
.HasMoreTokens())
623 fl
= tkn
.GetNextToken();
624 index
= m_styleNames
.Index(fl
);
625 if (index
!= wxNOT_FOUND
)
626 style
|= m_styleValues
[index
];
628 wxLogError(_("Unknown style flag ") + fl
);
635 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
637 wxString
str1(GetParamValue(param
));
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)
650 for (dt
= str1
.c_str(); *dt
; dt
++)
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..."):
656 if ( *(++dt
) == amp_char
)
659 str2
<< wxT('&') << *dt
;
661 // Remap \n to CR, \r to LF, \t to TAB:
662 else if (*dt
== wxT('\\'))
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;
673 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
674 return wxGetTranslation(str2
);
682 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
685 wxString str1
= GetParamValue(param
);
687 if (!str1
.ToLong(&value
))
695 int wxXmlResourceHandler::GetID()
697 wxString sid
= GetName();
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
);
717 else return wxXmlResource::GetXRCID(sid
);
722 wxString
wxXmlResourceHandler::GetName()
724 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
729 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
731 wxString v
= GetParamValue(param
);
733 if (!v
) return defaultv
;
734 else return (v
== wxT("1"));
739 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
741 wxString v
= GetParamValue(param
);
742 unsigned long tmp
= 0;
744 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
745 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
747 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
748 v
.c_str(), param
.c_str());
752 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
753 (unsigned char) ((tmp
& 0x00FF00) >> 8),
754 (unsigned char) ((tmp
& 0x0000FF)));
759 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
760 const wxArtClient
& defaultArtClient
,
763 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
764 wxXmlNode
*bmpNode
= GetParamNode(param
);
767 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
770 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
772 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
773 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
780 /* ...or load the bitmap from file: */
781 wxString name
= GetParamValue(param
);
782 if (name
.IsEmpty()) return wxNullBitmap
;
784 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
787 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
790 wxImage
img(*(fsfile
->GetStream()));
793 wxImage
img(GetParamValue(wxT("bitmap")));
798 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
801 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
802 return wxBitmap(img
);
808 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
809 const wxArtClient
& defaultArtClient
,
812 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
814 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
817 wxBitmap bmppt
= GetBitmap(param
, size
);
818 iconpt
= (wxIcon
*)(&bmppt
);
819 wxIcon
icon(*iconpt
);
826 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
828 wxXmlNode
*n
= m_node
->GetChildren();
832 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
840 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
843 if (n
== NULL
) return wxEmptyString
;
844 n
= n
->GetChildren();
848 if (n
->GetType() == wxXML_TEXT_NODE
||
849 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
850 return n
->GetContent();
853 return wxEmptyString
;
858 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
861 return GetNodeContent(m_node
);
863 return GetNodeContent(GetParamNode(param
));
868 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
870 wxString s
= GetParamValue(param
);
871 if (s
.IsEmpty()) s
= wxT("-1,-1");
875 is_dlg
= s
[s
.Length()-1] == wxT('d');
876 if (is_dlg
) s
.RemoveLast();
878 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
879 !s
.AfterLast(wxT(',')).ToLong(&sy
))
881 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
882 return wxDefaultSize
;
887 if (m_instanceAsWindow
)
888 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
889 else if (m_parentAsWindow
)
890 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
893 wxLogError(_("Cannot convert dialog units: dialog unknown."));
894 return wxDefaultSize
;
897 else return wxSize(sx
, sy
);
902 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
904 wxSize sz
= GetSize(param
);
905 return wxPoint(sz
.x
, sz
.y
);
910 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
912 wxString s
= GetParamValue(param
);
913 if (s
.IsEmpty()) return defaultv
;
917 is_dlg
= s
[s
.Length()-1] == wxT('d');
918 if (is_dlg
) s
.RemoveLast();
922 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
928 if (m_instanceAsWindow
)
929 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
930 else if (m_parentAsWindow
)
931 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
934 wxLogError(_("Cannot convert dialog units: dialog unknown."));
943 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
945 wxXmlNode
*font_node
= GetParamNode(param
);
946 if (font_node
== NULL
)
948 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
952 wxXmlNode
*oldnode
= m_node
;
955 long size
= GetLong(wxT("size"), 12);
957 wxString style
= GetParamValue(wxT("style"));
958 wxString weight
= GetParamValue(wxT("weight"));
959 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
960 if (style
== wxT("italic")) istyle
= wxITALIC
;
961 else if (style
== wxT("slant")) istyle
= wxSLANT
;
962 if (weight
== wxT("bold")) iweight
= wxBOLD
;
963 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
965 wxString family
= GetParamValue(wxT("family"));
966 int ifamily
= wxDEFAULT
;
967 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
968 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
969 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
970 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
971 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
973 bool underlined
= GetBool(wxT("underlined"), FALSE
);
975 wxString encoding
= GetParamValue(wxT("encoding"));
977 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
978 if (!encoding
.IsEmpty())
979 enc
= mapper
.CharsetToEncoding(encoding
);
980 if (enc
== wxFONTENCODING_SYSTEM
)
981 enc
= wxFONTENCODING_DEFAULT
;
983 wxString faces
= GetParamValue(wxT("face"));
984 wxString facename
= wxEmptyString
;
985 wxFontEnumerator enu
;
986 enu
.EnumerateFacenames();
987 wxStringTokenizer
tk(faces
, wxT(","));
988 while (tk
.HasMoreTokens())
990 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
991 if (index
!= wxNOT_FOUND
)
993 facename
= (*enu
.GetFacenames())[index
];
1000 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1005 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1007 //FIXME : add cursor
1009 if (HasParam(wxT("exstyle")))
1010 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1011 if (HasParam(wxT("bg")))
1012 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1013 if (HasParam(wxT("fg")))
1014 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1015 if (GetBool(wxT("enabled"), 1) == 0)
1017 if (GetBool(wxT("focused"), 0) == 1)
1019 if (GetBool(wxT("hidden"), 0) == 1)
1022 if (HasParam(wxT("tooltip")))
1023 wnd
->SetToolTip(GetText(wxT("tooltip")));
1025 if (HasParam(wxT("font")))
1026 wnd
->SetFont(GetFont());
1030 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1032 wxXmlNode
*n
= m_node
->GetChildren();
1036 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1037 n
->GetName() == wxT("object"))
1039 if (this_hnd_only
&& CanHandle(n
))
1040 CreateResource(n
, parent
, NULL
);
1042 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1049 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1052 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1053 wxXmlNode
*n
= root
->GetChildren();
1057 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1059 CreateResource(n
, parent
, NULL
);
1071 // --------------- XRCID implementation -----------------------------
1073 #define XRCID_TABLE_SIZE 1024
1083 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1085 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1087 static int XRCID_LastID
= wxID_HIGHEST
;
1091 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1092 index
%= XRCID_TABLE_SIZE
;
1094 XRCID_record
*oldrec
= NULL
;
1096 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1098 if (wxStrcmp(rec
->key
, str_id
) == 0)
1106 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1107 &XRCID_Records
[index
] : &oldrec
->next
;
1108 *rec_var
= new XRCID_record
;
1109 (*rec_var
)->id
= ++XRCID_LastID
;
1110 (*rec_var
)->key
= wxStrdup(str_id
);
1111 (*rec_var
)->next
= NULL
;
1113 return (*rec_var
)->id
;
1117 static void CleanXRCID_Record(XRCID_record
*rec
)
1121 CleanXRCID_Record(rec
->next
);
1127 static void CleanXRCID_Records()
1129 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1130 CleanXRCID_Record(XRCID_Records
[i
]);
1140 // --------------- module and globals -----------------------------
1142 class wxXmlResourceModule
: public wxModule
1144 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1146 wxXmlResourceModule() {}
1153 delete wxXmlResource::Set(NULL
);
1154 CleanXRCID_Records();
1158 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1161 // When wxXml is loaded dynamically after the application is already running
1162 // then the built-in module system won't pick this one up. Add it manually.
1163 void wxXmlInitResourceModule()
1165 wxModule
* module = new wxXmlResourceModule
;
1167 wxModule::RegisterModule(module);