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/xrc/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
)
63 m_handlers
.DeleteContents(TRUE
);
68 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
72 m_handlers
.DeleteContents(TRUE
);
76 wxXmlResource::~wxXmlResource()
82 bool wxXmlResource::Load(const wxString
& filemask
)
85 wxXmlResourceDataRecord
*drec
;
86 bool iswild
= wxIsWild(filemask
);
91 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
92 # define wxXmlFindNext fsys.FindNext()
94 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
95 # define wxXmlFindNext wxFindNextFile()
103 // NB: Load() accepts both filenames and URLs (should probably be
104 // changed to filenames only, but embedded resources currently
105 // rely on its ability to handle URLs - FIXME). This check
106 // serves as a quick way to determine whether found name is
107 // filename and not URL:
108 if (wxFileName::FileExists(fnd
))
110 // Make the name absolute filename, because the app may
111 // change working directory later:
116 fnd
= fn
.GetFullPath();
121 if (fnd
.Lower().Matches(wxT("*.zip")) ||
122 fnd
.Lower().Matches(wxT("*.xrs")))
124 wxString
url(wxFileSystem::FileNameToURL(fnd
));
125 rt
= rt
&& Load(url
+ wxT("#zip:*.xrc"));
130 drec
= new wxXmlResourceDataRecord
;
140 # undef wxXmlFindFirst
141 # undef wxXmlFindNext
147 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
149 m_handlers
.Append(handler
);
150 handler
->SetParentResource(this);
153 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
155 m_handlers
.Insert(handler
);
156 handler
->SetParentResource(this);
161 void wxXmlResource::ClearHandlers()
167 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
169 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
174 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
176 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
182 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
184 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
189 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
191 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
194 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
196 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
201 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
203 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
206 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
208 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
211 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
213 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
216 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
218 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
221 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
223 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
224 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
227 if (bmp
) { rt
= *bmp
; delete bmp
; }
231 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
233 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
234 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
237 if (icon
) { rt
= *icon
; delete icon
; }
242 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
244 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
247 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
249 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
253 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
254 wxWindow
*control
, wxWindow
*parent
)
257 parent
= control
->GetParent();
258 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
261 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
264 return control
->Reparent(container
);
268 static void ProcessPlatformProperty(wxXmlNode
*node
)
273 wxXmlNode
*c
= node
->GetChildren();
277 if (!c
->GetPropVal(wxT("platform"), &s
))
281 wxStringTokenizer
tkn(s
, wxT(" |"));
283 while (tkn
.HasMoreTokens())
285 s
= tkn
.GetNextToken();
287 if (s
== wxT("win")) isok
= true;
290 if (s
== wxT("unix")) isok
= true;
293 if (s
== wxT("mac")) isok
= true;
296 if (s
== wxT("os2")) isok
= true;
306 ProcessPlatformProperty(c
);
311 wxXmlNode
*c2
= c
->GetNext();
312 node
->RemoveChild(c
);
321 void wxXmlResource::UpdateResources()
324 # if wxUSE_FILESYSTEM
325 wxFSFile
*file
= NULL
;
329 wxString
encoding(wxT("UTF-8"));
330 #if !wxUSE_UNICODE && wxUSE_INTL
331 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
333 // In case we are not using wxLocale to translate strings, convert the strings
334 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
335 // it could break wxGetTranslation lookup.
336 encoding
= wxLocale::GetSystemEncodingName();
340 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
342 modif
= (m_data
[i
].Doc
== NULL
);
346 # if wxUSE_FILESYSTEM
347 file
= fsys
.OpenFile(m_data
[i
].File
);
348 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
350 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
353 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
359 wxInputStream
*stream
= NULL
;
361 # if wxUSE_FILESYSTEM
362 file
= fsys
.OpenFile(m_data
[i
].File
);
364 stream
= file
->GetStream();
366 stream
= new wxFileInputStream(m_data
[i
].File
);
371 delete m_data
[i
].Doc
;
372 m_data
[i
].Doc
= new wxXmlDocument
;
374 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
376 wxLogError(_("Cannot load resources from file '%s'."),
377 m_data
[i
].File
.c_str());
378 wxDELETE(m_data
[i
].Doc
);
380 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
382 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
383 wxDELETE(m_data
[i
].Doc
);
389 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
390 wxT("version"), wxT("0.0.0.0"));
391 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
392 &v1
, &v2
, &v3
, &v4
) == 4)
393 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
398 if (m_version
!= version
)
399 wxLogError(_("Resource files must have same version number!"));
401 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
402 m_data
[i
].Time
= file
->GetModificationTime();
405 # if wxUSE_FILESYSTEM
415 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
416 const wxString
& name
,
417 const wxString
& classname
,
423 // first search for match at the top-level nodes (as this is
424 // where the resource is most commonly looked for):
425 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
427 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
428 (node
->GetName() == wxT("object") ||
429 node
->GetName() == wxT("object_ref")) &&
430 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
432 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
433 if (!classname
|| cls
== classname
)
435 // object_ref may not have 'class' property:
436 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
438 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
441 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
443 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
452 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
454 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
455 (node
->GetName() == wxT("object") ||
456 node
->GetName() == wxT("object_ref")) )
458 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
467 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
468 const wxString
& classname
,
471 UpdateResources(); //ensure everything is up-to-date
474 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
476 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
479 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
480 name
, classname
, recursive
);
484 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
490 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
491 name
.c_str(), classname
.c_str());
495 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
498 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
500 wxXmlProperty
*dprop
;
501 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
504 if ( dprop
->GetName() == prop
->GetName() )
506 dprop
->SetValue(prop
->GetValue());
512 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
515 // Merge child nodes:
516 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
518 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
521 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
523 if ( dnode
->GetName() == node
->GetName() &&
524 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
525 dnode
->GetType() == node
->GetType() )
527 MergeNodes(*dnode
, *node
);
533 dest
.AddChild(new wxXmlNode(*node
));
536 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
537 dest
.SetContent(with
.GetContent());
540 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
542 wxXmlResourceHandler
*handlerToUse
)
544 if (node
== NULL
) return NULL
;
546 // handling of referenced resource
547 if ( node
->GetName() == wxT("object_ref") )
549 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
550 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
554 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
559 wxXmlNode
copy(*refNode
);
560 MergeNodes(copy
, *node
);
562 return CreateResFromNode(©
, parent
, instance
);
565 wxXmlResourceHandler
*handler
;
569 if (handlerToUse
->CanHandle(node
))
571 return handlerToUse
->CreateResource(node
, parent
, instance
);
574 else if (node
->GetName() == wxT("object"))
576 wxNode
*ND
= m_handlers
.GetFirst();
579 handler
= (wxXmlResourceHandler
*)ND
->GetData();
580 if (handler
->CanHandle(node
))
582 return handler
->CreateResource(node
, parent
, instance
);
588 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
589 node
->GetName().c_str(),
590 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
595 #include "wx/listimpl.cpp"
596 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
597 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
599 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
601 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
603 if (!ms_subclassFactories
)
605 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
606 ms_subclassFactories
->DeleteContents(TRUE
);
608 ms_subclassFactories
->Append(factory
);
611 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
614 ~wxXmlSubclassFactoryCXX() {}
616 wxObject
*Create(const wxString
& className
)
618 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
621 return classInfo
->CreateObject();
631 wxXmlResourceHandler::wxXmlResourceHandler()
632 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
633 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
638 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
640 wxXmlNode
*myNode
= m_node
;
641 wxString myClass
= m_class
;
642 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
643 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
645 m_instance
= instance
;
646 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
647 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
649 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
650 if (!subclass
.empty())
652 for (wxXmlSubclassFactoriesList::Node
*i
= wxXmlResource::ms_subclassFactories
->GetFirst();
655 m_instance
= i
->GetData()->Create(subclass
);
662 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
663 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
664 subclass
.c_str(), name
.c_str());
670 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
672 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
673 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
675 wxObject
*returned
= DoCreateResource();
679 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
680 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
686 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
688 m_styleNames
.Add(name
);
689 m_styleValues
.Add(value
);
694 void wxXmlResourceHandler::AddWindowStyles()
696 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
697 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
698 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
699 XRC_ADD_STYLE(wxRAISED_BORDER
);
700 XRC_ADD_STYLE(wxSTATIC_BORDER
);
701 XRC_ADD_STYLE(wxNO_BORDER
);
702 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
703 XRC_ADD_STYLE(wxWANTS_CHARS
);
704 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
709 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
711 return (GetParamNode(param
) != NULL
);
715 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
717 wxString s
= GetParamValue(param
);
719 if (!s
) return defaults
;
721 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
725 while (tkn
.HasMoreTokens())
727 fl
= tkn
.GetNextToken();
728 index
= m_styleNames
.Index(fl
);
729 if (index
!= wxNOT_FOUND
)
730 style
|= m_styleValues
[index
];
732 wxLogError(_("Unknown style flag ") + fl
);
739 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
741 wxString
str1(GetParamValue(param
));
746 // VS: First version of XRC resources used $ instead of & (which is
747 // illegal in XML), but later I realized that '_' fits this purpose
748 // much better (because &File means "File with F underlined").
749 if (m_resource
->CompareVersion(2,3,0,1) < 0)
754 for (dt
= str1
.c_str(); *dt
; dt
++)
756 // Remap amp_char to &, map double amp_char to amp_char (for things
757 // like "&File..." -- this is illegal in XML, so we use "_File..."):
760 if ( *(++dt
) == amp_char
)
763 str2
<< wxT('&') << *dt
;
765 // Remap \n to CR, \r to LF, \t to TAB:
766 else if (*dt
== wxT('\\'))
769 case wxT('n') : str2
<< wxT('\n'); break;
770 case wxT('t') : str2
<< wxT('\t'); break;
771 case wxT('r') : str2
<< wxT('\r'); break;
772 default : str2
<< wxT('\\') << *dt
; break;
777 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
778 return wxGetTranslation(str2
);
786 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
789 wxString str1
= GetParamValue(param
);
791 if (!str1
.ToLong(&value
))
799 int wxXmlResourceHandler::GetID()
801 return wxXmlResource::GetXRCID(GetName());
806 wxString
wxXmlResourceHandler::GetName()
808 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
813 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
815 wxString v
= GetParamValue(param
);
817 if (!v
) return defaultv
;
818 else return (v
== wxT("1"));
823 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
825 wxString v
= GetParamValue(param
);
826 unsigned long tmp
= 0;
828 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
829 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
831 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
832 v
.c_str(), param
.c_str());
836 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
837 (unsigned char) ((tmp
& 0x00FF00) >> 8),
838 (unsigned char) ((tmp
& 0x0000FF)));
843 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
844 const wxArtClient
& defaultArtClient
,
847 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
848 wxXmlNode
*bmpNode
= GetParamNode(param
);
851 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
854 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
856 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
857 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
864 /* ...or load the bitmap from file: */
865 wxString name
= GetParamValue(param
);
866 if (name
.IsEmpty()) return wxNullBitmap
;
868 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
871 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
874 wxImage
img(*(fsfile
->GetStream()));
877 wxImage
img(GetParamValue(wxT("bitmap")));
882 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
885 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
886 return wxBitmap(img
);
892 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
893 const wxArtClient
& defaultArtClient
,
897 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
903 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
905 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
907 wxXmlNode
*n
= m_node
->GetChildren();
911 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
919 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
922 if (n
== NULL
) return wxEmptyString
;
923 n
= n
->GetChildren();
927 if (n
->GetType() == wxXML_TEXT_NODE
||
928 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
929 return n
->GetContent();
932 return wxEmptyString
;
937 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
940 return GetNodeContent(m_node
);
942 return GetNodeContent(GetParamNode(param
));
947 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
949 wxString s
= GetParamValue(param
);
950 if (s
.IsEmpty()) s
= wxT("-1,-1");
954 is_dlg
= s
[s
.Length()-1] == wxT('d');
955 if (is_dlg
) s
.RemoveLast();
957 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
958 !s
.AfterLast(wxT(',')).ToLong(&sy
))
960 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
961 return wxDefaultSize
;
966 if (m_instanceAsWindow
)
967 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
968 else if (m_parentAsWindow
)
969 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
972 wxLogError(_("Cannot convert dialog units: dialog unknown."));
973 return wxDefaultSize
;
976 else return wxSize(sx
, sy
);
981 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
983 wxSize sz
= GetSize(param
);
984 return wxPoint(sz
.x
, sz
.y
);
989 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
991 wxString s
= GetParamValue(param
);
992 if (s
.IsEmpty()) return defaultv
;
996 is_dlg
= s
[s
.Length()-1] == wxT('d');
997 if (is_dlg
) s
.RemoveLast();
1001 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1007 if (m_instanceAsWindow
)
1008 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1009 else if (m_parentAsWindow
)
1010 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1013 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1022 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1024 wxXmlNode
*font_node
= GetParamNode(param
);
1025 if (font_node
== NULL
)
1027 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1031 wxXmlNode
*oldnode
= m_node
;
1034 long size
= GetLong(wxT("size"), 12);
1036 wxString style
= GetParamValue(wxT("style"));
1037 wxString weight
= GetParamValue(wxT("weight"));
1038 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1039 if (style
== wxT("italic")) istyle
= wxITALIC
;
1040 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1041 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1042 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1044 wxString family
= GetParamValue(wxT("family"));
1045 int ifamily
= wxDEFAULT
;
1046 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1047 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1048 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1049 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1050 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1052 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1054 wxString encoding
= GetParamValue(wxT("encoding"));
1055 wxFontMapper mapper
;
1056 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1057 if (!encoding
.IsEmpty())
1058 enc
= mapper
.CharsetToEncoding(encoding
);
1059 if (enc
== wxFONTENCODING_SYSTEM
)
1060 enc
= wxFONTENCODING_DEFAULT
;
1062 wxString faces
= GetParamValue(wxT("face"));
1063 wxString facename
= wxEmptyString
;
1064 wxFontEnumerator enu
;
1065 enu
.EnumerateFacenames();
1066 wxStringTokenizer
tk(faces
, wxT(","));
1067 while (tk
.HasMoreTokens())
1069 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1070 if (index
!= wxNOT_FOUND
)
1072 facename
= (*enu
.GetFacenames())[index
];
1079 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1084 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1086 //FIXME : add cursor
1088 if (HasParam(wxT("exstyle")))
1089 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1090 if (HasParam(wxT("bg")))
1091 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1092 if (HasParam(wxT("fg")))
1093 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1094 if (GetBool(wxT("enabled"), 1) == 0)
1096 if (GetBool(wxT("focused"), 0) == 1)
1098 if (GetBool(wxT("hidden"), 0) == 1)
1101 if (HasParam(wxT("tooltip")))
1102 wnd
->SetToolTip(GetText(wxT("tooltip")));
1104 if (HasParam(wxT("font")))
1105 wnd
->SetFont(GetFont());
1109 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1111 wxXmlNode
*n
= m_node
->GetChildren();
1115 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1116 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1118 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1119 this_hnd_only
? this : NULL
);
1126 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1129 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1130 wxXmlNode
*n
= root
->GetChildren();
1134 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1136 CreateResource(n
, parent
, NULL
);
1148 // --------------- XRCID implementation -----------------------------
1150 #define XRCID_TABLE_SIZE 1024
1160 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1162 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1164 static int XRCID_LastID
= wxID_HIGHEST
;
1168 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1169 index
%= XRCID_TABLE_SIZE
;
1171 XRCID_record
*oldrec
= NULL
;
1173 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1175 if (wxStrcmp(rec
->key
, str_id
) == 0)
1183 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1184 &XRCID_Records
[index
] : &oldrec
->next
;
1185 *rec_var
= new XRCID_record
;
1186 (*rec_var
)->key
= wxStrdup(str_id
);
1187 (*rec_var
)->next
= NULL
;
1190 if (value_if_not_found
!= -2)
1191 (*rec_var
)->id
= value_if_not_found
;
1194 int asint
= wxStrtol(str_id
, &end
, 10);
1195 if (*str_id
&& *end
== 0)
1197 // if str_id was integer, keep it verbosely:
1198 (*rec_var
)->id
= asint
;
1202 (*rec_var
)->id
= ++XRCID_LastID
;
1206 return (*rec_var
)->id
;
1209 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1211 return XRCID_Lookup(str_id
);
1215 static void CleanXRCID_Record(XRCID_record
*rec
)
1219 CleanXRCID_Record(rec
->next
);
1225 static void CleanXRCID_Records()
1227 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1228 CleanXRCID_Record(XRCID_Records
[i
]);
1231 static void AddStdXRCID_Records()
1233 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1235 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
1236 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
1237 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
1238 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
1239 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
1240 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
1241 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
1242 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
1243 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
1244 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
1245 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
1246 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
1247 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
1248 stdID(wxID_CLOSE_ALL
);
1256 // --------------- module and globals -----------------------------
1258 class wxXmlResourceModule
: public wxModule
1260 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1262 wxXmlResourceModule() {}
1265 AddStdXRCID_Records();
1266 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1271 delete wxXmlResource::Set(NULL
);
1272 wxDELETE(wxXmlResource::ms_subclassFactories
);
1273 CleanXRCID_Records();
1277 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1280 // When wxXml is loaded dynamically after the application is already running
1281 // then the built-in module system won't pick this one up. Add it manually.
1282 void wxXmlInitResourceModule()
1284 wxModule
* module = new wxXmlResourceModule
;
1286 wxModule::RegisterModule(module);