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"
27 #include "wx/filename.h"
30 #include "wx/tokenzr.h"
31 #include "wx/fontenum.h"
32 #include "wx/module.h"
33 #include "wx/bitmap.h"
35 #include "wx/fontmap.h"
36 #include "wx/artprov.h"
38 #include "wx/xml/xml.h"
39 #include "wx/xrc/xmlres.h"
41 #include "wx/arrimpl.cpp"
42 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
45 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
47 /*static*/ wxXmlResource
*wxXmlResource::Get()
50 ms_instance
= new wxXmlResource
;
54 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
56 wxXmlResource
*old
= ms_instance
;
61 wxXmlResource::wxXmlResource(int flags
)
67 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
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()
101 // NB: Load() accepts both filenames and URLs (should probably be
102 // changed to filenames only, but embedded resources currently
103 // rely on its ability to handle URLs - FIXME). This check
104 // serves as a quick way to determine whether found name is
105 // filename and not URL:
106 if (wxFileName::FileExists(fnd
))
108 // Make the name absolute filename, because the app may
109 // change working directory later:
114 fnd
= fn
.GetFullPath();
117 fnd
= wxFileSystem::FileNameToURL(fnd
);
122 if (fnd
.Lower().Matches(wxT("*.zip")) ||
123 fnd
.Lower().Matches(wxT("*.xrs")))
125 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
130 drec
= new wxXmlResourceDataRecord
;
140 # undef wxXmlFindFirst
141 # undef wxXmlFindNext
142 return rt
&& UpdateResources();
146 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
148 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
150 m_handlers
.Append(handler
);
151 handler
->SetParentResource(this);
154 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
156 m_handlers
.Insert(handler
);
157 handler
->SetParentResource(this);
162 void wxXmlResource::ClearHandlers()
164 WX_CLEAR_LIST(wxList
, m_handlers
);
168 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
170 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
175 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
177 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
183 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
185 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
190 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
192 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
195 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
197 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
202 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
204 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
207 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
209 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
212 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
214 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
217 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
219 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
222 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
224 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
225 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
228 if (bmp
) { rt
= *bmp
; delete bmp
; }
232 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
234 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
235 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
238 if (icon
) { rt
= *icon
; delete icon
; }
243 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
245 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
248 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
250 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
254 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
255 wxWindow
*control
, wxWindow
*parent
)
258 parent
= control
->GetParent();
259 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
262 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
265 return control
->Reparent(container
);
269 static void ProcessPlatformProperty(wxXmlNode
*node
)
274 wxXmlNode
*c
= node
->GetChildren();
278 if (!c
->GetPropVal(wxT("platform"), &s
))
282 wxStringTokenizer
tkn(s
, wxT(" |"));
284 while (tkn
.HasMoreTokens())
286 s
= tkn
.GetNextToken();
288 if (s
== wxT("win")) isok
= true;
290 #if defined(__MAC__) || defined(__APPLE__)
291 if (s
== wxT("mac")) isok
= true;
292 #elif defined(__UNIX__)
293 if (s
== wxT("unix")) isok
= true;
296 if (s
== wxT("os2")) isok
= true;
306 ProcessPlatformProperty(c
);
311 wxXmlNode
*c2
= c
->GetNext();
312 node
->RemoveChild(c
);
321 bool wxXmlResource::UpdateResources()
325 # if wxUSE_FILESYSTEM
326 wxFSFile
*file
= NULL
;
331 wxString
encoding(wxT("UTF-8"));
332 #if !wxUSE_UNICODE && wxUSE_INTL
333 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
335 // In case we are not using wxLocale to translate strings, convert the
336 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
337 // is on, because it could break wxGetTranslation lookup.
338 encoding
= wxLocale::GetSystemEncodingName();
342 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
344 modif
= (m_data
[i
].Doc
== NULL
);
348 # if wxUSE_FILESYSTEM
349 file
= fsys
.OpenFile(m_data
[i
].File
);
350 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
353 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
359 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
365 wxInputStream
*stream
= NULL
;
367 # if wxUSE_FILESYSTEM
368 file
= fsys
.OpenFile(m_data
[i
].File
);
370 stream
= file
->GetStream();
372 stream
= new wxFileInputStream(m_data
[i
].File
);
377 delete m_data
[i
].Doc
;
378 m_data
[i
].Doc
= new wxXmlDocument
;
380 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
382 wxLogError(_("Cannot load resources from file '%s'."),
383 m_data
[i
].File
.c_str());
384 wxDELETE(m_data
[i
].Doc
);
387 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
389 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
390 wxDELETE(m_data
[i
].Doc
);
397 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
398 wxT("version"), wxT("0.0.0.0"));
399 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
400 &v1
, &v2
, &v3
, &v4
) == 4)
401 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
406 if (m_version
!= version
)
408 wxLogError(_("Resource files must have same version number!"));
412 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
413 m_data
[i
].Time
= file
->GetModificationTime();
416 # if wxUSE_FILESYSTEM
429 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
430 const wxString
& name
,
431 const wxString
& classname
,
437 // first search for match at the top-level nodes (as this is
438 // where the resource is most commonly looked for):
439 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
441 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
442 (node
->GetName() == wxT("object") ||
443 node
->GetName() == wxT("object_ref")) &&
444 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
446 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
447 if (!classname
|| cls
== classname
)
449 // object_ref may not have 'class' property:
450 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
452 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
455 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
457 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
466 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
468 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
469 (node
->GetName() == wxT("object") ||
470 node
->GetName() == wxT("object_ref")) )
472 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
481 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
482 const wxString
& classname
,
485 UpdateResources(); //ensure everything is up-to-date
488 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
490 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
493 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
494 name
, classname
, recursive
);
498 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
504 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
505 name
.c_str(), classname
.c_str());
509 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
512 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
514 wxXmlProperty
*dprop
;
515 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
518 if ( dprop
->GetName() == prop
->GetName() )
520 dprop
->SetValue(prop
->GetValue());
526 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
529 // Merge child nodes:
530 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
532 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
535 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
537 if ( dnode
->GetName() == node
->GetName() &&
538 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
539 dnode
->GetType() == node
->GetType() )
541 MergeNodes(*dnode
, *node
);
547 dest
.AddChild(new wxXmlNode(*node
));
550 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
551 dest
.SetContent(with
.GetContent());
554 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
556 wxXmlResourceHandler
*handlerToUse
)
558 if (node
== NULL
) return NULL
;
560 // handling of referenced resource
561 if ( node
->GetName() == wxT("object_ref") )
563 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
564 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
568 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
573 wxXmlNode
copy(*refNode
);
574 MergeNodes(copy
, *node
);
576 return CreateResFromNode(©
, parent
, instance
);
579 wxXmlResourceHandler
*handler
;
583 if (handlerToUse
->CanHandle(node
))
585 return handlerToUse
->CreateResource(node
, parent
, instance
);
588 else if (node
->GetName() == wxT("object"))
590 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
593 handler
= (wxXmlResourceHandler
*)ND
->GetData();
594 if (handler
->CanHandle(node
))
596 return handler
->CreateResource(node
, parent
, instance
);
602 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
603 node
->GetName().c_str(),
604 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
609 #include "wx/listimpl.cpp"
610 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
611 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
613 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
615 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
617 if (!ms_subclassFactories
)
619 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
621 ms_subclassFactories
->Append(factory
);
624 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
627 ~wxXmlSubclassFactoryCXX() {}
629 wxObject
*Create(const wxString
& className
)
631 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
634 return classInfo
->CreateObject();
644 wxXmlResourceHandler::wxXmlResourceHandler()
645 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
646 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
651 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
653 wxXmlNode
*myNode
= m_node
;
654 wxString myClass
= m_class
;
655 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
656 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
658 m_instance
= instance
;
659 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
660 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
662 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
663 if (!subclass
.empty())
665 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
668 m_instance
= i
->GetData()->Create(subclass
);
675 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
676 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
677 subclass
.c_str(), name
.c_str());
683 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
685 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
686 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
688 wxObject
*returned
= DoCreateResource();
692 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
693 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
699 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
701 m_styleNames
.Add(name
);
702 m_styleValues
.Add(value
);
707 void wxXmlResourceHandler::AddWindowStyles()
709 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
710 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
711 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
712 XRC_ADD_STYLE(wxRAISED_BORDER
);
713 XRC_ADD_STYLE(wxSTATIC_BORDER
);
714 XRC_ADD_STYLE(wxNO_BORDER
);
715 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
716 XRC_ADD_STYLE(wxWANTS_CHARS
);
717 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
718 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
723 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
725 return (GetParamNode(param
) != NULL
);
729 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
731 wxString s
= GetParamValue(param
);
733 if (!s
) return defaults
;
735 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
739 while (tkn
.HasMoreTokens())
741 fl
= tkn
.GetNextToken();
742 index
= m_styleNames
.Index(fl
);
743 if (index
!= wxNOT_FOUND
)
744 style
|= m_styleValues
[index
];
746 wxLogError(_("Unknown style flag ") + fl
);
753 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
755 wxString
str1(GetParamValue(param
));
760 // VS: First version of XRC resources used $ instead of & (which is
761 // illegal in XML), but later I realized that '_' fits this purpose
762 // much better (because &File means "File with F underlined").
763 if (m_resource
->CompareVersion(2,3,0,1) < 0)
768 for (dt
= str1
.c_str(); *dt
; dt
++)
770 // Remap amp_char to &, map double amp_char to amp_char (for things
771 // like "&File..." -- this is illegal in XML, so we use "_File..."):
774 if ( *(++dt
) == amp_char
)
777 str2
<< wxT('&') << *dt
;
779 // Remap \n to CR, \r to LF, \t to TAB:
780 else if (*dt
== wxT('\\'))
783 case wxT('n') : str2
<< wxT('\n'); break;
784 case wxT('t') : str2
<< wxT('\t'); break;
785 case wxT('r') : str2
<< wxT('\r'); break;
786 default : str2
<< wxT('\\') << *dt
; break;
791 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
792 return wxGetTranslation(str2
);
800 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
803 wxString str1
= GetParamValue(param
);
805 if (!str1
.ToLong(&value
))
813 int wxXmlResourceHandler::GetID()
815 return wxXmlResource::GetXRCID(GetName());
820 wxString
wxXmlResourceHandler::GetName()
822 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
827 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
829 wxString v
= GetParamValue(param
);
831 if (!v
) return defaultv
;
832 else return (v
== wxT("1"));
837 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
839 wxString v
= GetParamValue(param
);
840 unsigned long tmp
= 0;
842 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
843 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
845 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
846 v
.c_str(), param
.c_str());
850 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
851 (unsigned char) ((tmp
& 0x00FF00) >> 8),
852 (unsigned char) ((tmp
& 0x0000FF)));
857 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
858 const wxArtClient
& defaultArtClient
,
861 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
862 wxXmlNode
*bmpNode
= GetParamNode(param
);
865 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
868 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
870 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
871 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
878 /* ...or load the bitmap from file: */
879 wxString name
= GetParamValue(param
);
880 if (name
.IsEmpty()) return wxNullBitmap
;
882 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
885 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
889 wxImage
img(*(fsfile
->GetStream()));
892 wxImage
img(GetParamValue(wxT("bitmap")));
897 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
900 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
901 return wxBitmap(img
);
907 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
908 const wxArtClient
& defaultArtClient
,
912 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
918 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
920 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
922 wxXmlNode
*n
= m_node
->GetChildren();
926 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
934 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
937 if (n
== NULL
) return wxEmptyString
;
938 n
= n
->GetChildren();
942 if (n
->GetType() == wxXML_TEXT_NODE
||
943 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
944 return n
->GetContent();
947 return wxEmptyString
;
952 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
955 return GetNodeContent(m_node
);
957 return GetNodeContent(GetParamNode(param
));
962 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
964 wxString s
= GetParamValue(param
);
965 if (s
.IsEmpty()) s
= wxT("-1,-1");
969 is_dlg
= s
[s
.Length()-1] == wxT('d');
970 if (is_dlg
) s
.RemoveLast();
972 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
973 !s
.AfterLast(wxT(',')).ToLong(&sy
))
975 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
976 return wxDefaultSize
;
981 if (m_instanceAsWindow
)
982 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
983 else if (m_parentAsWindow
)
984 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
987 wxLogError(_("Cannot convert dialog units: dialog unknown."));
988 return wxDefaultSize
;
991 else return wxSize(sx
, sy
);
996 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
998 wxSize sz
= GetSize(param
);
999 return wxPoint(sz
.x
, sz
.y
);
1004 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
1006 wxString s
= GetParamValue(param
);
1007 if (s
.IsEmpty()) return defaultv
;
1011 is_dlg
= s
[s
.Length()-1] == wxT('d');
1012 if (is_dlg
) s
.RemoveLast();
1016 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1022 if (m_instanceAsWindow
)
1023 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1024 else if (m_parentAsWindow
)
1025 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1028 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1037 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1039 wxXmlNode
*font_node
= GetParamNode(param
);
1040 if (font_node
== NULL
)
1042 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1046 wxXmlNode
*oldnode
= m_node
;
1049 long size
= GetLong(wxT("size"), 12);
1051 wxString style
= GetParamValue(wxT("style"));
1052 wxString weight
= GetParamValue(wxT("weight"));
1053 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1054 if (style
== wxT("italic")) istyle
= wxITALIC
;
1055 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1056 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1057 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1059 wxString family
= GetParamValue(wxT("family"));
1060 int ifamily
= wxDEFAULT
;
1061 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1062 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1063 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1064 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1065 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1067 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1069 wxString encoding
= GetParamValue(wxT("encoding"));
1070 wxFontMapper mapper
;
1071 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1072 if (!encoding
.IsEmpty())
1073 enc
= mapper
.CharsetToEncoding(encoding
);
1074 if (enc
== wxFONTENCODING_SYSTEM
)
1075 enc
= wxFONTENCODING_DEFAULT
;
1077 wxString faces
= GetParamValue(wxT("face"));
1078 wxString facename
= wxEmptyString
;
1079 wxFontEnumerator enu
;
1080 enu
.EnumerateFacenames();
1081 wxStringTokenizer
tk(faces
, wxT(","));
1082 while (tk
.HasMoreTokens())
1084 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1085 if (index
!= wxNOT_FOUND
)
1087 facename
= (*enu
.GetFacenames())[index
];
1094 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1099 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1101 //FIXME : add cursor
1103 if (HasParam(wxT("exstyle")))
1104 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1105 if (HasParam(wxT("bg")))
1106 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1107 if (HasParam(wxT("fg")))
1108 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1109 if (GetBool(wxT("enabled"), 1) == 0)
1111 if (GetBool(wxT("focused"), 0) == 1)
1113 if (GetBool(wxT("hidden"), 0) == 1)
1116 if (HasParam(wxT("tooltip")))
1117 wnd
->SetToolTip(GetText(wxT("tooltip")));
1119 if (HasParam(wxT("font")))
1120 wnd
->SetFont(GetFont());
1124 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1126 wxXmlNode
*n
= m_node
->GetChildren();
1130 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1131 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1133 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1134 this_hnd_only
? this : NULL
);
1141 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1144 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1145 wxXmlNode
*n
= root
->GetChildren();
1149 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1151 CreateResource(n
, parent
, NULL
);
1163 // --------------- XRCID implementation -----------------------------
1165 #define XRCID_TABLE_SIZE 1024
1175 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1177 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1179 static int XRCID_LastID
= wxID_HIGHEST
;
1183 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1184 index
%= XRCID_TABLE_SIZE
;
1186 XRCID_record
*oldrec
= NULL
;
1187 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1189 if (wxStrcmp(rec
->key
, str_id
) == 0)
1196 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1197 &XRCID_Records
[index
] : &oldrec
->next
;
1198 *rec_var
= new XRCID_record
;
1199 (*rec_var
)->key
= wxStrdup(str_id
);
1200 (*rec_var
)->next
= NULL
;
1203 if (value_if_not_found
!= -2)
1204 (*rec_var
)->id
= value_if_not_found
;
1207 int asint
= wxStrtol(str_id
, &end
, 10);
1208 if (*str_id
&& *end
== 0)
1210 // if str_id was integer, keep it verbosely:
1211 (*rec_var
)->id
= asint
;
1215 (*rec_var
)->id
= ++XRCID_LastID
;
1219 return (*rec_var
)->id
;
1222 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1224 return XRCID_Lookup(str_id
);
1228 static void CleanXRCID_Record(XRCID_record
*rec
)
1232 CleanXRCID_Record(rec
->next
);
1238 static void CleanXRCID_Records()
1240 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1242 CleanXRCID_Record(XRCID_Records
[i
]);
1243 XRCID_Records
[i
] = NULL
;
1247 static void AddStdXRCID_Records()
1249 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1251 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
1252 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
1253 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
1254 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
1255 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
1256 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
1257 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
1258 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
1259 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
1260 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
1261 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
1262 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
1263 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
1264 stdID(wxID_CLOSE_ALL
);
1272 // --------------- module and globals -----------------------------
1274 class wxXmlResourceModule
: public wxModule
1276 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1278 wxXmlResourceModule() {}
1281 AddStdXRCID_Records();
1282 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1287 delete wxXmlResource::Set(NULL
);
1288 if(wxXmlResource::ms_subclassFactories
)
1289 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1290 wxDELETE(wxXmlResource::ms_subclassFactories
);
1291 CleanXRCID_Records();
1295 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1298 // When wxXml is loaded dynamically after the application is already running
1299 // then the built-in module system won't pick this one up. Add it manually.
1300 void wxXmlInitResourceModule()
1302 wxModule
* module = new wxXmlResourceModule
;
1304 wxModule::RegisterModule(module);