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"
26 #include "wx/dialog.h"
29 #include "wx/wfstream.h"
30 #include "wx/filesys.h"
31 #include "wx/filename.h"
34 #include "wx/tokenzr.h"
35 #include "wx/fontenum.h"
36 #include "wx/module.h"
37 #include "wx/bitmap.h"
39 #include "wx/fontmap.h"
40 #include "wx/artprov.h"
42 #include "wx/xml/xml.h"
43 #include "wx/xrc/xmlres.h"
45 #include "wx/arrimpl.cpp"
46 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
49 wxXmlResource
*wxXmlResource
::ms_instance
= NULL
;
51 /*static*/ wxXmlResource
*wxXmlResource
::Get()
54 ms_instance
= new wxXmlResource
;
58 /*static*/ wxXmlResource
*wxXmlResource
::Set(wxXmlResource
*res
)
60 wxXmlResource
*old
= ms_instance
;
65 wxXmlResource
::wxXmlResource(int flags
)
71 wxXmlResource
::wxXmlResource(const wxString
& filemask
, int flags
)
78 wxXmlResource
::~wxXmlResource()
84 bool wxXmlResource
::Load(const wxString
& filemask
)
87 wxXmlResourceDataRecord
*drec
;
88 bool iswild
= wxIsWild(filemask
);
93 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
94 # define wxXmlFindNext fsys.FindNext()
96 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
97 # define wxXmlFindNext wxFindNextFile()
100 fnd
= wxXmlFindFirst
;
105 // NB: Load() accepts both filenames and URLs (should probably be
106 // changed to filenames only, but embedded resources currently
107 // rely on its ability to handle URLs - FIXME). This check
108 // serves as a quick way to determine whether found name is
109 // filename and not URL:
110 if (wxFileName
::FileExists(fnd
))
112 // Make the name absolute filename, because the app may
113 // change working directory later:
118 fnd
= fn
.GetFullPath();
121 fnd
= wxFileSystem
::FileNameToURL(fnd
);
126 if (fnd
.Lower().Matches(wxT("*.zip")) ||
127 fnd
.Lower().Matches(wxT("*.xrs")))
129 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
134 drec
= new wxXmlResourceDataRecord
;
144 # undef wxXmlFindFirst
145 # undef wxXmlFindNext
146 return rt
&& UpdateResources();
150 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
152 void wxXmlResource
::AddHandler(wxXmlResourceHandler
*handler
)
154 m_handlers
.Append(handler
);
155 handler
->SetParentResource(this);
158 void wxXmlResource
::InsertHandler(wxXmlResourceHandler
*handler
)
160 m_handlers
.Insert(handler
);
161 handler
->SetParentResource(this);
166 void wxXmlResource
::ClearHandlers()
168 WX_CLEAR_LIST(wxList
, m_handlers
);
172 wxMenu
*wxXmlResource
::LoadMenu(const wxString
& name
)
174 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
179 wxMenuBar
*wxXmlResource
::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
181 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
187 wxToolBar
*wxXmlResource
::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
189 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
194 wxDialog
*wxXmlResource
::LoadDialog(wxWindow
*parent
, const wxString
& name
)
196 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
199 bool wxXmlResource
::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
201 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
206 wxPanel
*wxXmlResource
::LoadPanel(wxWindow
*parent
, const wxString
& name
)
208 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
211 bool wxXmlResource
::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
213 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
216 wxFrame
*wxXmlResource
::LoadFrame(wxWindow
* parent
, const wxString
& name
)
218 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
221 bool wxXmlResource
::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
223 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
226 wxBitmap wxXmlResource
::LoadBitmap(const wxString
& name
)
228 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
229 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
232 if (bmp
) { rt
= *bmp
; delete bmp
; }
236 wxIcon wxXmlResource
::LoadIcon(const wxString
& name
)
238 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
239 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
242 if (icon
) { rt
= *icon
; delete icon
; }
247 wxObject
*wxXmlResource
::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
249 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
252 bool wxXmlResource
::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
254 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
258 bool wxXmlResource
::AttachUnknownControl(const wxString
& name
,
259 wxWindow
*control
, wxWindow
*parent
)
262 parent
= control
->GetParent();
263 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
266 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
269 return control
->Reparent(container
);
273 static void ProcessPlatformProperty(wxXmlNode
*node
)
278 wxXmlNode
*c
= node
->GetChildren();
282 if (!c
->GetPropVal(wxT("platform"), &s
))
286 wxStringTokenizer
tkn(s
, wxT(" |"));
288 while (tkn
.HasMoreTokens())
290 s
= tkn
.GetNextToken();
292 if (s
== wxT("win")) isok
= true;
294 #if defined(__MAC__) || defined(__APPLE__)
295 if (s
== wxT("mac")) isok
= true;
296 #elif defined(__UNIX__)
297 if (s
== wxT("unix")) isok
= true;
300 if (s
== wxT("os2")) isok
= true;
310 ProcessPlatformProperty(c
);
315 wxXmlNode
*c2
= c
->GetNext();
316 node
->RemoveChild(c
);
325 bool wxXmlResource
::UpdateResources()
329 # if wxUSE_FILESYSTEM
330 wxFSFile
*file
= NULL
;
335 wxString
encoding(wxT("UTF-8"));
336 #if !wxUSE_UNICODE && wxUSE_INTL
337 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
339 // In case we are not using wxLocale to translate strings, convert the
340 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
341 // is on, because it could break wxGetTranslation lookup.
342 encoding
= wxLocale
::GetSystemEncodingName();
346 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
348 modif
= (m_data
[i
].Doc
== NULL
);
350 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
352 # if wxUSE_FILESYSTEM
353 file
= fsys
.OpenFile(m_data
[i
].File
);
354 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
357 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
363 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
369 wxLogTrace(_T("xrc"),
370 _T("opening file '%s'"), m_data
[i
].File
.c_str());
372 wxInputStream
*stream
= NULL
;
374 # if wxUSE_FILESYSTEM
375 file
= fsys
.OpenFile(m_data
[i
].File
);
377 stream
= file
->GetStream();
379 stream
= new wxFileInputStream(m_data
[i
].File
);
384 delete m_data
[i
].Doc
;
385 m_data
[i
].Doc
= new wxXmlDocument
;
387 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
389 wxLogError(_("Cannot load resources from file '%s'."),
390 m_data
[i
].File
.c_str());
391 wxDELETE(m_data
[i
].Doc
);
394 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
396 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
397 wxDELETE(m_data
[i
].Doc
);
404 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
405 wxT("version"), wxT("0.0.0.0"));
406 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
407 &v1
, &v2
, &v3
, &v4
) == 4)
408 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
413 if (m_version
!= version
)
415 wxLogError(_("Resource files must have same version number!"));
419 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
421 m_data
[i
].Time
= file
->GetModificationTime();
423 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
427 # if wxUSE_FILESYSTEM
440 wxXmlNode
*wxXmlResource
::DoFindResource(wxXmlNode
*parent
,
441 const wxString
& name
,
442 const wxString
& classname
,
448 // first search for match at the top-level nodes (as this is
449 // where the resource is most commonly looked for):
450 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
452 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
453 (node
->GetName() == wxT("object") ||
454 node
->GetName() == wxT("object_ref")) &&
455 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
457 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
458 if (!classname
|| cls
== classname
)
460 // object_ref may not have 'class' property:
461 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
463 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
466 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
468 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
477 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
479 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
480 (node
->GetName() == wxT("object") ||
481 node
->GetName() == wxT("object_ref")) )
483 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
492 wxXmlNode
*wxXmlResource
::FindResource(const wxString
& name
,
493 const wxString
& classname
,
496 UpdateResources(); //ensure everything is up-to-date
499 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
501 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
504 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
505 name
, classname
, recursive
);
509 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
515 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
516 name
.c_str(), classname
.c_str());
520 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
523 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
525 wxXmlProperty
*dprop
;
526 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
529 if ( dprop
->GetName() == prop
->GetName() )
531 dprop
->SetValue(prop
->GetValue());
537 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
540 // Merge child nodes:
541 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
543 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
546 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
548 if ( dnode
->GetName() == node
->GetName() &&
549 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
550 dnode
->GetType() == node
->GetType() )
552 MergeNodes(*dnode
, *node
);
558 dest
.AddChild(new wxXmlNode(*node
));
561 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
562 dest
.SetContent(with
.GetContent());
565 wxObject
*wxXmlResource
::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
567 wxXmlResourceHandler
*handlerToUse
)
569 if (node
== NULL
) return NULL
;
571 // handling of referenced resource
572 if ( node
->GetName() == wxT("object_ref") )
574 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
575 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
579 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
584 wxXmlNode
copy(*refNode
);
585 MergeNodes(copy
, *node
);
587 return CreateResFromNode(©
, parent
, instance
);
590 wxXmlResourceHandler
*handler
;
594 if (handlerToUse
->CanHandle(node
))
596 return handlerToUse
->CreateResource(node
, parent
, instance
);
599 else if (node
->GetName() == wxT("object"))
601 wxList
::compatibility_iterator ND
= m_handlers
.GetFirst();
604 handler
= (wxXmlResourceHandler
*)ND
->GetData();
605 if (handler
->CanHandle(node
))
607 return handler
->CreateResource(node
, parent
, instance
);
613 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
614 node
->GetName().c_str(),
615 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
620 #include "wx/listimpl.cpp"
621 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
622 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
624 wxXmlSubclassFactoriesList
*wxXmlResource
::ms_subclassFactories
= NULL
;
626 /*static*/ void wxXmlResource
::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
628 if (!ms_subclassFactories
)
630 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
632 ms_subclassFactories
->Append(factory
);
635 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
638 ~wxXmlSubclassFactoryCXX() {}
640 wxObject
*Create(const wxString
& className
)
642 wxClassInfo
* classInfo
= wxClassInfo
::FindClass(className
);
645 return classInfo
->CreateObject();
654 wxXmlResourceHandler
::wxXmlResourceHandler()
655 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
656 m_parentAsWindow(NULL
)
661 wxObject
*wxXmlResourceHandler
::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
663 wxXmlNode
*myNode
= m_node
;
664 wxString myClass
= m_class
;
665 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
666 wxWindow
*myParentAW
= m_parentAsWindow
;
668 m_instance
= instance
;
669 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
670 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
672 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
673 if (!subclass
.empty())
675 for (wxXmlSubclassFactoriesList
::compatibility_iterator i
= wxXmlResource
::ms_subclassFactories
->GetFirst();
678 m_instance
= i
->GetData()->Create(subclass
);
685 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
686 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
687 subclass
.c_str(), name
.c_str());
693 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
695 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
697 wxObject
*returned
= DoCreateResource();
701 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
702 m_instance
= myInstance
;
708 void wxXmlResourceHandler
::AddStyle(const wxString
& name
, int value
)
710 m_styleNames
.Add(name
);
711 m_styleValues
.Add(value
);
716 void wxXmlResourceHandler
::AddWindowStyles()
718 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
719 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
720 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
721 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
722 XRC_ADD_STYLE(wxRAISED_BORDER
);
723 XRC_ADD_STYLE(wxSTATIC_BORDER
);
724 XRC_ADD_STYLE(wxNO_BORDER
);
725 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
726 XRC_ADD_STYLE(wxWANTS_CHARS
);
727 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
728 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
729 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
734 bool wxXmlResourceHandler
::HasParam(const wxString
& param
)
736 return (GetParamNode(param
) != NULL
);
740 int wxXmlResourceHandler
::GetStyle(const wxString
& param
, int defaults
)
742 wxString s
= GetParamValue(param
);
744 if (!s
) return defaults
;
746 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
750 while (tkn
.HasMoreTokens())
752 fl
= tkn
.GetNextToken();
753 index
= m_styleNames
.Index(fl
);
754 if (index
!= wxNOT_FOUND
)
755 style
|= m_styleValues
[index
];
757 wxLogError(_("Unknown style flag ") + fl
);
764 wxString wxXmlResourceHandler
::GetText(const wxString
& param
, bool translate
)
766 wxXmlNode
*parNode
= GetParamNode(param
);
767 wxString
str1(GetNodeContent(parNode
));
772 // VS: First version of XRC resources used $ instead of & (which is
773 // illegal in XML), but later I realized that '_' fits this purpose
774 // much better (because &File means "File with F underlined").
775 if (m_resource
->CompareVersion(2,3,0,1) < 0)
780 for (dt
= str1
.c_str(); *dt
; dt
++)
782 // Remap amp_char to &, map double amp_char to amp_char (for things
783 // like "&File..." -- this is illegal in XML, so we use "_File..."):
786 if ( *(++dt
) == amp_char
)
789 str2
<< wxT('&') << *dt
;
791 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
792 else if (*dt
== wxT('\\'))
808 // "\\" wasn't translated to "\" prior to 2.5.3.0:
809 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
814 // else fall-through to default: branch below
817 str2
<< wxT('\\') << *dt
;
823 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
825 if (translate
&& parNode
&&
826 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
828 return wxGetTranslation(str2
);
835 // The string is internally stored as UTF-8, we have to convert
836 // it into system's default encoding so that it can be displayed:
837 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
843 // If wxXRC_USE_LOCALE is not set, then the string is already in
844 // system's default encoding in ANSI build, so we don't have to
845 // do anything special here.
852 long wxXmlResourceHandler
::GetLong(const wxString
& param
, long defaultv
)
855 wxString str1
= GetParamValue(param
);
857 if (!str1
.ToLong(&value
))
863 float wxXmlResourceHandler
::GetFloat(const wxString
& param
, float defaultv
)
866 wxString str1
= GetParamValue(param
);
868 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
870 if (!str1
.ToDouble(&value
))
873 setlocale(LC_NUMERIC
, prevlocale
);
879 int wxXmlResourceHandler
::GetID()
881 return wxXmlResource
::GetXRCID(GetName());
886 wxString wxXmlResourceHandler
::GetName()
888 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
893 bool wxXmlResourceHandler
::GetBool(const wxString
& param
, bool defaultv
)
895 wxString v
= GetParamValue(param
);
897 if (!v
) return defaultv
;
898 else return (v
== wxT("1"));
902 static wxColour
GetSystemColour(const wxString
& name
)
906 #define SYSCLR(clr) \
907 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
908 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
909 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
910 SYSCLR(wxSYS_COLOUR_DESKTOP
)
911 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
912 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
913 SYSCLR(wxSYS_COLOUR_MENU
)
914 SYSCLR(wxSYS_COLOUR_WINDOW
)
915 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
916 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
917 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
918 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
919 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
920 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
921 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
922 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
923 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
924 SYSCLR(wxSYS_COLOUR_BTNFACE
)
925 SYSCLR(wxSYS_COLOUR_3DFACE
)
926 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
927 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
928 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
929 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
930 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
931 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
932 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
933 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
934 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
935 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
936 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
937 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
938 SYSCLR(wxSYS_COLOUR_INFOBK
)
939 SYSCLR(wxSYS_COLOUR_LISTBOX
)
940 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
941 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
942 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
943 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
944 SYSCLR(wxSYS_COLOUR_MENUBAR
)
951 wxColour wxXmlResourceHandler
::GetColour(const wxString
& param
)
953 wxString v
= GetParamValue(param
);
955 // find colour using HTML syntax (#RRGGBB)
956 unsigned long tmp
= 0;
958 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
959 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
961 // the colour doesn't use #RRGGBB format, check if it is symbolic
963 wxColour clr
= GetSystemColour(v
);
967 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
968 v
.c_str(), param
.c_str());
972 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
973 (unsigned char) ((tmp
& 0x00FF00) >> 8),
974 (unsigned char) ((tmp
& 0x0000FF)));
979 wxBitmap wxXmlResourceHandler
::GetBitmap(const wxString
& param
,
980 const wxArtClient
& defaultArtClient
,
983 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
984 wxXmlNode
*bmpNode
= GetParamNode(param
);
987 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
990 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
992 scl
= defaultArtClient
;
994 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
997 wxArtProvider
::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1004 /* ...or load the bitmap from file: */
1005 wxString name
= GetParamValue(param
);
1006 if (name
.IsEmpty()) return wxNullBitmap
;
1007 #if wxUSE_FILESYSTEM
1008 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
1011 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1013 return wxNullBitmap
;
1015 wxImage
img(*(fsfile
->GetStream()));
1018 wxImage
img(GetParamValue(wxT("bitmap")));
1023 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1025 return wxNullBitmap
;
1027 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1028 return wxBitmap(img
);
1034 wxIcon wxXmlResourceHandler
::GetIcon(const wxString
& param
,
1035 const wxArtClient
& defaultArtClient
,
1039 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1045 wxXmlNode
*wxXmlResourceHandler
::GetParamNode(const wxString
& param
)
1047 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1049 wxXmlNode
*n
= m_node
->GetChildren();
1053 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1061 wxString wxXmlResourceHandler
::GetNodeContent(wxXmlNode
*node
)
1063 wxXmlNode
*n
= node
;
1064 if (n
== NULL
) return wxEmptyString
;
1065 n
= n
->GetChildren();
1069 if (n
->GetType() == wxXML_TEXT_NODE
||
1070 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1071 return n
->GetContent();
1074 return wxEmptyString
;
1079 wxString wxXmlResourceHandler
::GetParamValue(const wxString
& param
)
1081 if (param
.IsEmpty())
1082 return GetNodeContent(m_node
);
1084 return GetNodeContent(GetParamNode(param
));
1089 wxSize wxXmlResourceHandler
::GetSize(const wxString
& param
)
1091 wxString s
= GetParamValue(param
);
1092 if (s
.IsEmpty()) s
= wxT("-1,-1");
1096 is_dlg
= s
[s
.Length()-1] == wxT('d');
1097 if (is_dlg
) s
.RemoveLast();
1099 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1100 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1102 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1103 return wxDefaultSize
;
1108 if (m_parentAsWindow
)
1109 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1112 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1113 return wxDefaultSize
;
1116 else return wxSize(sx
, sy
);
1121 wxPoint wxXmlResourceHandler
::GetPosition(const wxString
& param
)
1123 wxSize sz
= GetSize(param
);
1124 return wxPoint(sz
.x
, sz
.y
);
1129 wxCoord wxXmlResourceHandler
::GetDimension(const wxString
& param
, wxCoord defaultv
)
1131 wxString s
= GetParamValue(param
);
1132 if (s
.IsEmpty()) return defaultv
;
1136 is_dlg
= s
[s
.Length()-1] == wxT('d');
1137 if (is_dlg
) s
.RemoveLast();
1141 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1147 if (m_parentAsWindow
)
1148 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1151 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1159 // Get system font index using indexname
1160 static wxFont
GetSystemFont(const wxString
& name
)
1164 #define SYSFNT(fnt) \
1165 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1166 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1167 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1168 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1169 SYSFNT(wxSYS_SYSTEM_FONT
)
1170 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1171 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1172 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1173 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1180 wxFont wxXmlResourceHandler
::GetFont(const wxString
& param
)
1182 wxXmlNode
*font_node
= GetParamNode(param
);
1183 if (font_node
== NULL
)
1185 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1189 wxXmlNode
*oldnode
= m_node
;
1195 int isize
= wxDEFAULT
;
1196 bool hasSize
= HasParam(wxT("size"));
1198 isize
= GetLong(wxT("size"), wxDEFAULT
);
1201 int istyle
= wxNORMAL
;
1202 bool hasStyle
= HasParam(wxT("style"));
1205 wxString style
= GetParamValue(wxT("style"));
1206 if (style
== wxT("italic"))
1208 else if (style
== wxT("slant"))
1213 int iweight
= wxNORMAL
;
1214 bool hasWeight
= HasParam(wxT("weight"));
1217 wxString weight
= GetParamValue(wxT("weight"));
1218 if (weight
== wxT("bold"))
1220 else if (weight
== wxT("light"))
1225 bool hasUnderlined
= HasParam(wxT("underlined"));
1226 bool underlined
= hasUnderlined ?
GetBool(wxT("underlined"), false) : false;
1228 // family and facename
1229 int ifamily
= wxDEFAULT
;
1230 bool hasFamily
= HasParam(wxT("family"));
1233 wxString family
= GetParamValue(wxT("family"));
1234 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1235 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1236 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1237 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1238 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1239 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1244 bool hasFacename
= HasParam(wxT("face"));
1247 wxString faces
= GetParamValue(wxT("face"));
1248 wxFontEnumerator enu
;
1249 enu
.EnumerateFacenames();
1250 wxStringTokenizer
tk(faces
, wxT(","));
1251 while (tk
.HasMoreTokens())
1253 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), false);
1254 if (index
!= wxNOT_FOUND
)
1256 facename
= (*enu
.GetFacenames())[index
];
1263 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1264 bool hasEncoding
= HasParam(wxT("encoding"));
1267 wxString encoding
= GetParamValue(wxT("encoding"));
1268 wxFontMapper mapper
;
1269 if (!encoding
.IsEmpty())
1270 enc
= mapper
.CharsetToEncoding(encoding
);
1271 if (enc
== wxFONTENCODING_SYSTEM
)
1272 enc
= wxFONTENCODING_DEFAULT
;
1275 // is this font based on a system font?
1276 wxFont sysfont
= GetSystemFont(GetParamValue(wxT("sysfont")));
1281 sysfont
.SetPointSize(isize
);
1282 else if (HasParam(wxT("relativesize")))
1283 sysfont
.SetPointSize(int(sysfont
.GetPointSize() *
1284 GetFloat(wxT("relativesize"))));
1287 sysfont
.SetStyle(istyle
);
1289 sysfont
.SetWeight(iweight
);
1291 sysfont
.SetUnderlined(underlined
);
1293 sysfont
.SetFamily(ifamily
);
1295 sysfont
.SetFaceName(facename
);
1297 sysfont
.SetDefaultEncoding(enc
);
1305 return wxFont(isize
, ifamily
, istyle
, iweight
,
1306 underlined
, facename
, enc
);
1311 void wxXmlResourceHandler
::SetupWindow(wxWindow
*wnd
)
1313 //FIXME : add cursor
1315 if (HasParam(wxT("exstyle")))
1316 // Have to OR it with existing style, since
1317 // some implementations (e.g. wxGTK) use the extra style
1319 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1320 if (HasParam(wxT("bg")))
1321 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1322 if (HasParam(wxT("fg")))
1323 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1324 if (GetBool(wxT("enabled"), 1) == 0)
1326 if (GetBool(wxT("focused"), 0) == 1)
1328 if (GetBool(wxT("hidden"), 0) == 1)
1331 if (HasParam(wxT("tooltip")))
1332 wnd
->SetToolTip(GetText(wxT("tooltip")));
1334 if (HasParam(wxT("font")))
1335 wnd
->SetFont(GetFont());
1339 void wxXmlResourceHandler
::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1341 wxXmlNode
*n
= m_node
->GetChildren();
1345 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1346 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1348 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1349 this_hnd_only ?
this : NULL
);
1356 void wxXmlResourceHandler
::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1359 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1360 wxXmlNode
*n
= root
->GetChildren();
1364 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1366 CreateResource(n
, parent
, NULL
);
1378 // --------------- XRCID implementation -----------------------------
1380 #define XRCID_TABLE_SIZE 1024
1390 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1392 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1396 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1397 index
%= XRCID_TABLE_SIZE
;
1399 XRCID_record
*oldrec
= NULL
;
1400 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1402 if (wxStrcmp(rec
->key
, str_id
) == 0)
1409 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1410 &XRCID_Records
[index
] : &oldrec
->next
;
1411 *rec_var
= new XRCID_record
;
1412 (*rec_var
)->key
= wxStrdup(str_id
);
1413 (*rec_var
)->next
= NULL
;
1416 if (value_if_not_found
!= -2)
1417 (*rec_var
)->id
= value_if_not_found
;
1420 int asint
= wxStrtol(str_id
, &end
, 10);
1421 if (*str_id
&& *end
== 0)
1423 // if str_id was integer, keep it verbosely:
1424 (*rec_var
)->id
= asint
;
1428 (*rec_var
)->id
= wxNewId();
1432 return (*rec_var
)->id
;
1435 /*static*/ int wxXmlResource
::GetXRCID(const wxChar
*str_id
)
1437 return XRCID_Lookup(str_id
);
1441 static void CleanXRCID_Record(XRCID_record
*rec
)
1445 CleanXRCID_Record(rec
->next
);
1451 static void CleanXRCID_Records()
1453 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1455 CleanXRCID_Record(XRCID_Records
[i
]);
1456 XRCID_Records
[i
] = NULL
;
1460 static void AddStdXRCID_Records()
1462 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1466 stdID(wxID_SEPARATOR
);
1479 stdID(wxID_PRINT_SETUP
);
1480 stdID(wxID_PREVIEW
);
1482 stdID(wxID_HELP_CONTENTS
);
1483 stdID(wxID_HELP_COMMANDS
);
1484 stdID(wxID_HELP_PROCEDURES
);
1485 stdID(wxID_HELP_CONTEXT
);
1486 stdID(wxID_CLOSE_ALL
);
1487 stdID(wxID_PREFERENCES
);
1493 stdID(wxID_DUPLICATE
);
1494 stdID(wxID_SELECTALL
);
1496 stdID(wxID_REPLACE
);
1497 stdID(wxID_REPLACE_ALL
);
1498 stdID(wxID_PROPERTIES
);
1499 stdID(wxID_VIEW_DETAILS
);
1500 stdID(wxID_VIEW_LARGEICONS
);
1501 stdID(wxID_VIEW_SMALLICONS
);
1502 stdID(wxID_VIEW_LIST
);
1503 stdID(wxID_VIEW_SORTDATE
);
1504 stdID(wxID_VIEW_SORTNAME
);
1505 stdID(wxID_VIEW_SORTSIZE
);
1506 stdID(wxID_VIEW_SORTTYPE
);
1522 stdID(wxID_FORWARD
);
1523 stdID(wxID_BACKWARD
);
1524 stdID(wxID_DEFAULT
);
1528 stdID(wxID_CONTEXT_HELP
);
1529 stdID(wxID_YESTOALL
);
1530 stdID(wxID_NOTOALL
);
1539 stdID(wxID_REFRESH
);
1544 stdID(wxID_JUSTIFY_CENTER
);
1545 stdID(wxID_JUSTIFY_FILL
);
1546 stdID(wxID_JUSTIFY_RIGHT
);
1547 stdID(wxID_JUSTIFY_LEFT
);
1548 stdID(wxID_UNDERLINE
);
1550 stdID(wxID_UNINDENT
);
1551 stdID(wxID_ZOOM_100
);
1552 stdID(wxID_ZOOM_FIT
);
1553 stdID(wxID_ZOOM_IN
);
1554 stdID(wxID_ZOOM_OUT
);
1555 stdID(wxID_UNDELETE
);
1556 stdID(wxID_REVERT_TO_SAVED
);
1557 stdID(wxID_SYSTEM_MENU
);
1558 stdID(wxID_CLOSE_FRAME
);
1559 stdID(wxID_MOVE_FRAME
);
1560 stdID(wxID_RESIZE_FRAME
);
1561 stdID(wxID_MAXIMIZE_FRAME
);
1562 stdID(wxID_ICONIZE_FRAME
);
1563 stdID(wxID_RESTORE_FRAME
);
1572 // --------------- module and globals -----------------------------
1574 class wxXmlResourceModule
: public wxModule
1576 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1578 wxXmlResourceModule() {}
1581 AddStdXRCID_Records();
1582 wxXmlResource
::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1587 delete wxXmlResource
::Set(NULL
);
1588 if(wxXmlResource
::ms_subclassFactories
)
1589 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource
::ms_subclassFactories
);
1590 wxDELETE(wxXmlResource
::ms_subclassFactories
);
1591 CleanXRCID_Records();
1595 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1598 // When wxXml is loaded dynamically after the application is already running
1599 // then the built-in module system won't pick this one up. Add it manually.
1600 void wxXmlInitResourceModule()
1602 wxModule
* module
= new wxXmlResourceModule
;
1604 wxModule
::RegisterModule(module
);