1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
12 #pragma implementation "xmlres.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
28 #include "wx/dialog.h"
31 #include "wx/wfstream.h"
32 #include "wx/filesys.h"
33 #include "wx/filename.h"
36 #include "wx/tokenzr.h"
37 #include "wx/fontenum.h"
38 #include "wx/module.h"
39 #include "wx/bitmap.h"
41 #include "wx/fontmap.h"
42 #include "wx/artprov.h"
43 #include "wx/settings.h"
45 #include "wx/xml/xml.h"
46 #include "wx/xrc/xmlres.h"
48 #include "wx/arrimpl.cpp"
49 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
52 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
54 /*static*/ wxXmlResource
*wxXmlResource::Get()
57 ms_instance
= new wxXmlResource
;
61 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
63 wxXmlResource
*old
= ms_instance
;
68 wxXmlResource::wxXmlResource(int flags
)
74 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
81 wxXmlResource::~wxXmlResource()
87 bool wxXmlResource::Load(const wxString
& filemask
)
90 wxXmlResourceDataRecord
*drec
;
91 bool iswild
= wxIsWild(filemask
);
96 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
97 # define wxXmlFindNext fsys.FindNext()
99 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
100 # define wxXmlFindNext wxFindNextFile()
103 fnd
= wxXmlFindFirst
;
108 // NB: Load() accepts both filenames and URLs (should probably be
109 // changed to filenames only, but embedded resources currently
110 // rely on its ability to handle URLs - FIXME). This check
111 // serves as a quick way to determine whether found name is
112 // filename and not URL:
113 if (wxFileName::FileExists(fnd
))
115 // Make the name absolute filename, because the app may
116 // change working directory later:
121 fnd
= fn
.GetFullPath();
124 fnd
= wxFileSystem::FileNameToURL(fnd
);
129 if (fnd
.Lower().Matches(wxT("*.zip")) ||
130 fnd
.Lower().Matches(wxT("*.xrs")))
132 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
137 drec
= new wxXmlResourceDataRecord
;
147 # undef wxXmlFindFirst
148 # undef wxXmlFindNext
149 return rt
&& UpdateResources();
153 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
155 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
157 m_handlers
.Append(handler
);
158 handler
->SetParentResource(this);
161 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
163 m_handlers
.Insert(handler
);
164 handler
->SetParentResource(this);
169 void wxXmlResource::ClearHandlers()
171 WX_CLEAR_LIST(wxList
, m_handlers
);
175 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
177 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
182 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
184 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
190 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
192 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
197 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
199 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
202 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
204 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
209 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
211 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
214 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
216 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
219 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
221 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
224 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
226 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
229 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
231 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
232 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
235 if (bmp
) { rt
= *bmp
; delete bmp
; }
239 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
241 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
242 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
245 if (icon
) { rt
= *icon
; delete icon
; }
250 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
252 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
255 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
257 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
261 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
262 wxWindow
*control
, wxWindow
*parent
)
265 parent
= control
->GetParent();
266 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
269 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
272 return control
->Reparent(container
);
276 static void ProcessPlatformProperty(wxXmlNode
*node
)
281 wxXmlNode
*c
= node
->GetChildren();
285 if (!c
->GetPropVal(wxT("platform"), &s
))
289 wxStringTokenizer
tkn(s
, wxT(" |"));
291 while (tkn
.HasMoreTokens())
293 s
= tkn
.GetNextToken();
295 if (s
== wxT("win")) isok
= true;
297 #if defined(__MAC__) || defined(__APPLE__)
298 if (s
== wxT("mac")) isok
= true;
299 #elif defined(__UNIX__)
300 if (s
== wxT("unix")) isok
= true;
303 if (s
== wxT("os2")) isok
= true;
313 ProcessPlatformProperty(c
);
318 wxXmlNode
*c2
= c
->GetNext();
319 node
->RemoveChild(c
);
328 bool wxXmlResource::UpdateResources()
332 # if wxUSE_FILESYSTEM
333 wxFSFile
*file
= NULL
;
338 wxString
encoding(wxT("UTF-8"));
339 #if !wxUSE_UNICODE && wxUSE_INTL
340 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
342 // In case we are not using wxLocale to translate strings, convert the
343 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
344 // is on, because it could break wxGetTranslation lookup.
345 encoding
= wxLocale::GetSystemEncodingName();
349 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
351 modif
= (m_data
[i
].Doc
== NULL
);
353 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
355 # if wxUSE_FILESYSTEM
356 file
= fsys
.OpenFile(m_data
[i
].File
);
357 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
360 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
366 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
372 wxLogTrace(_T("xrc"),
373 _T("opening file '%s'"), m_data
[i
].File
.c_str());
375 wxInputStream
*stream
= NULL
;
377 # if wxUSE_FILESYSTEM
378 file
= fsys
.OpenFile(m_data
[i
].File
);
380 stream
= file
->GetStream();
382 stream
= new wxFileInputStream(m_data
[i
].File
);
387 delete m_data
[i
].Doc
;
388 m_data
[i
].Doc
= new wxXmlDocument
;
390 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
392 wxLogError(_("Cannot load resources from file '%s'."),
393 m_data
[i
].File
.c_str());
394 wxDELETE(m_data
[i
].Doc
);
397 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
399 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
400 wxDELETE(m_data
[i
].Doc
);
407 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
408 wxT("version"), wxT("0.0.0.0"));
409 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
410 &v1
, &v2
, &v3
, &v4
) == 4)
411 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
416 if (m_version
!= version
)
418 wxLogError(_("Resource files must have same version number!"));
422 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
424 m_data
[i
].Time
= file
->GetModificationTime();
426 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
430 # if wxUSE_FILESYSTEM
443 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
444 const wxString
& name
,
445 const wxString
& classname
,
451 // first search for match at the top-level nodes (as this is
452 // where the resource is most commonly looked for):
453 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
455 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
456 (node
->GetName() == wxT("object") ||
457 node
->GetName() == wxT("object_ref")) &&
458 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
460 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
461 if (!classname
|| cls
== classname
)
463 // object_ref may not have 'class' property:
464 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
466 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
469 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
471 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
480 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
482 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
483 (node
->GetName() == wxT("object") ||
484 node
->GetName() == wxT("object_ref")) )
486 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
495 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
496 const wxString
& classname
,
499 UpdateResources(); //ensure everything is up-to-date
502 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
504 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
507 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
508 name
, classname
, recursive
);
512 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
518 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
519 name
.c_str(), classname
.c_str());
523 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
526 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
528 wxXmlProperty
*dprop
;
529 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
532 if ( dprop
->GetName() == prop
->GetName() )
534 dprop
->SetValue(prop
->GetValue());
540 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
543 // Merge child nodes:
544 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
546 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
549 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
551 if ( dnode
->GetName() == node
->GetName() &&
552 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
553 dnode
->GetType() == node
->GetType() )
555 MergeNodes(*dnode
, *node
);
561 dest
.AddChild(new wxXmlNode(*node
));
564 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
565 dest
.SetContent(with
.GetContent());
568 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
570 wxXmlResourceHandler
*handlerToUse
)
572 if (node
== NULL
) return NULL
;
574 // handling of referenced resource
575 if ( node
->GetName() == wxT("object_ref") )
577 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
578 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
582 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
587 wxXmlNode
copy(*refNode
);
588 MergeNodes(copy
, *node
);
590 return CreateResFromNode(©
, parent
, instance
);
593 wxXmlResourceHandler
*handler
;
597 if (handlerToUse
->CanHandle(node
))
599 return handlerToUse
->CreateResource(node
, parent
, instance
);
602 else if (node
->GetName() == wxT("object"))
604 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
607 handler
= (wxXmlResourceHandler
*)ND
->GetData();
608 if (handler
->CanHandle(node
))
610 return handler
->CreateResource(node
, parent
, instance
);
616 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
617 node
->GetName().c_str(),
618 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
623 #include "wx/listimpl.cpp"
624 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
625 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
627 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
629 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
631 if (!ms_subclassFactories
)
633 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
635 ms_subclassFactories
->Append(factory
);
638 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
641 ~wxXmlSubclassFactoryCXX() {}
643 wxObject
*Create(const wxString
& className
)
645 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
648 return classInfo
->CreateObject();
657 wxXmlResourceHandler::wxXmlResourceHandler()
658 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
659 m_parentAsWindow(NULL
)
664 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
666 wxXmlNode
*myNode
= m_node
;
667 wxString myClass
= m_class
;
668 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
669 wxWindow
*myParentAW
= m_parentAsWindow
;
671 m_instance
= instance
;
672 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
673 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
675 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
676 if (!subclass
.empty())
678 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
681 m_instance
= i
->GetData()->Create(subclass
);
688 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
689 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
690 subclass
.c_str(), name
.c_str());
696 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
698 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
700 wxObject
*returned
= DoCreateResource();
704 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
705 m_instance
= myInstance
;
711 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
713 m_styleNames
.Add(name
);
714 m_styleValues
.Add(value
);
719 void wxXmlResourceHandler::AddWindowStyles()
721 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
722 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
723 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
724 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
725 XRC_ADD_STYLE(wxRAISED_BORDER
);
726 XRC_ADD_STYLE(wxSTATIC_BORDER
);
727 XRC_ADD_STYLE(wxNO_BORDER
);
728 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
729 XRC_ADD_STYLE(wxWANTS_CHARS
);
730 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
731 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
732 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
733 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
734 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
739 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
741 return (GetParamNode(param
) != NULL
);
745 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
747 wxString s
= GetParamValue(param
);
749 if (!s
) return defaults
;
751 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
755 while (tkn
.HasMoreTokens())
757 fl
= tkn
.GetNextToken();
758 index
= m_styleNames
.Index(fl
);
759 if (index
!= wxNOT_FOUND
)
760 style
|= m_styleValues
[index
];
762 wxLogError(_("Unknown style flag ") + fl
);
769 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
771 wxXmlNode
*parNode
= GetParamNode(param
);
772 wxString
str1(GetNodeContent(parNode
));
777 // VS: First version of XRC resources used $ instead of & (which is
778 // illegal in XML), but later I realized that '_' fits this purpose
779 // much better (because &File means "File with F underlined").
780 if (m_resource
->CompareVersion(2,3,0,1) < 0)
785 for (dt
= str1
.c_str(); *dt
; dt
++)
787 // Remap amp_char to &, map double amp_char to amp_char (for things
788 // like "&File..." -- this is illegal in XML, so we use "_File..."):
791 if ( *(++dt
) == amp_char
)
794 str2
<< wxT('&') << *dt
;
796 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
797 else if (*dt
== wxT('\\'))
813 // "\\" wasn't translated to "\" prior to 2.5.3.0:
814 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
819 // else fall-through to default: branch below
822 str2
<< wxT('\\') << *dt
;
828 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
830 if (translate
&& parNode
&&
831 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
833 return wxGetTranslation(str2
);
840 // The string is internally stored as UTF-8, we have to convert
841 // it into system's default encoding so that it can be displayed:
842 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
847 // If wxXRC_USE_LOCALE is not set, then the string is already in
848 // system's default encoding in ANSI build, so we don't have to
849 // do anything special here.
855 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
858 wxString str1
= GetParamValue(param
);
860 if (!str1
.ToLong(&value
))
866 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
869 wxString str1
= GetParamValue(param
);
872 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
875 if (!str1
.ToDouble(&value
))
879 setlocale(LC_NUMERIC
, prevlocale
);
886 int wxXmlResourceHandler::GetID()
888 return wxXmlResource::GetXRCID(GetName());
893 wxString
wxXmlResourceHandler::GetName()
895 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
900 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
902 wxString v
= GetParamValue(param
);
904 if (!v
) return defaultv
;
906 return (v
== wxT("1"));
910 static wxColour
GetSystemColour(const wxString
& name
)
914 #define SYSCLR(clr) \
915 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
916 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
917 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
918 SYSCLR(wxSYS_COLOUR_DESKTOP
)
919 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
920 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
921 SYSCLR(wxSYS_COLOUR_MENU
)
922 SYSCLR(wxSYS_COLOUR_WINDOW
)
923 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
924 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
925 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
926 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
927 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
928 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
929 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
930 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
931 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
932 SYSCLR(wxSYS_COLOUR_BTNFACE
)
933 SYSCLR(wxSYS_COLOUR_3DFACE
)
934 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
935 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
936 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
937 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
938 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
939 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
940 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
941 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
942 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
943 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
944 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
945 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
946 SYSCLR(wxSYS_COLOUR_INFOBK
)
947 SYSCLR(wxSYS_COLOUR_LISTBOX
)
948 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
949 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
950 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
951 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
952 SYSCLR(wxSYS_COLOUR_MENUBAR
)
959 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
961 wxString v
= GetParamValue(param
);
963 // find colour using HTML syntax (#RRGGBB)
964 unsigned long tmp
= 0;
966 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
967 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
969 // the colour doesn't use #RRGGBB format, check if it is symbolic
971 wxColour clr
= GetSystemColour(v
);
975 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
976 v
.c_str(), param
.c_str());
980 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
981 (unsigned char) ((tmp
& 0x00FF00) >> 8),
982 (unsigned char) ((tmp
& 0x0000FF)));
987 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
988 const wxArtClient
& defaultArtClient
,
991 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
992 wxXmlNode
*bmpNode
= GetParamNode(param
);
995 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
998 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
1000 scl
= defaultArtClient
;
1002 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1005 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1007 if ( stockArt
.Ok() )
1012 /* ...or load the bitmap from file: */
1013 wxString name
= GetParamValue(param
);
1014 if (name
.empty()) return wxNullBitmap
;
1015 #if wxUSE_FILESYSTEM
1016 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
1019 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1021 return wxNullBitmap
;
1023 wxImage
img(*(fsfile
->GetStream()));
1026 wxImage
img(GetParamValue(wxT("bitmap")));
1031 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1033 return wxNullBitmap
;
1035 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1036 return wxBitmap(img
);
1042 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1043 const wxArtClient
& defaultArtClient
,
1047 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1053 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1055 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1057 wxXmlNode
*n
= m_node
->GetChildren();
1061 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1069 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1071 wxXmlNode
*n
= node
;
1072 if (n
== NULL
) return wxEmptyString
;
1073 n
= n
->GetChildren();
1077 if (n
->GetType() == wxXML_TEXT_NODE
||
1078 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1079 return n
->GetContent();
1082 return wxEmptyString
;
1087 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1090 return GetNodeContent(m_node
);
1092 return GetNodeContent(GetParamNode(param
));
1097 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1098 wxWindow
*windowToUse
)
1100 wxString s
= GetParamValue(param
);
1101 if (s
.empty()) s
= wxT("-1,-1");
1105 is_dlg
= s
[s
.Length()-1] == wxT('d');
1106 if (is_dlg
) s
.RemoveLast();
1108 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1109 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1111 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1112 return wxDefaultSize
;
1119 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1121 else if (m_parentAsWindow
)
1123 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1127 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1128 return wxDefaultSize
;
1132 return wxSize(sx
, sy
);
1137 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1139 wxSize sz
= GetSize(param
);
1140 return wxPoint(sz
.x
, sz
.y
);
1145 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1147 wxWindow
*windowToUse
)
1149 wxString s
= GetParamValue(param
);
1150 if (s
.empty()) return defaultv
;
1154 is_dlg
= s
[s
.Length()-1] == wxT('d');
1155 if (is_dlg
) s
.RemoveLast();
1159 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1167 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1169 else if (m_parentAsWindow
)
1171 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1175 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1184 // Get system font index using indexname
1185 static wxFont
GetSystemFont(const wxString
& name
)
1189 #define SYSFNT(fnt) \
1190 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1191 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1192 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1193 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1194 SYSFNT(wxSYS_SYSTEM_FONT
)
1195 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1196 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1197 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1198 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1205 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1207 wxXmlNode
*font_node
= GetParamNode(param
);
1208 if (font_node
== NULL
)
1210 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1214 wxXmlNode
*oldnode
= m_node
;
1220 int isize
= wxDEFAULT
;
1221 bool hasSize
= HasParam(wxT("size"));
1223 isize
= GetLong(wxT("size"), wxDEFAULT
);
1226 int istyle
= wxNORMAL
;
1227 bool hasStyle
= HasParam(wxT("style"));
1230 wxString style
= GetParamValue(wxT("style"));
1231 if (style
== wxT("italic"))
1233 else if (style
== wxT("slant"))
1238 int iweight
= wxNORMAL
;
1239 bool hasWeight
= HasParam(wxT("weight"));
1242 wxString weight
= GetParamValue(wxT("weight"));
1243 if (weight
== wxT("bold"))
1245 else if (weight
== wxT("light"))
1250 bool hasUnderlined
= HasParam(wxT("underlined"));
1251 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1253 // family and facename
1254 int ifamily
= wxDEFAULT
;
1255 bool hasFamily
= HasParam(wxT("family"));
1258 wxString family
= GetParamValue(wxT("family"));
1259 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1260 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1261 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1262 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1263 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1264 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1269 bool hasFacename
= HasParam(wxT("face"));
1272 wxString faces
= GetParamValue(wxT("face"));
1273 wxFontEnumerator enu
;
1274 enu
.EnumerateFacenames();
1275 wxStringTokenizer
tk(faces
, wxT(","));
1276 while (tk
.HasMoreTokens())
1278 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), false);
1279 if (index
!= wxNOT_FOUND
)
1281 facename
= (*enu
.GetFacenames())[index
];
1288 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1289 bool hasEncoding
= HasParam(wxT("encoding"));
1292 wxString encoding
= GetParamValue(wxT("encoding"));
1293 wxFontMapper mapper
;
1294 if (!encoding
.empty())
1295 enc
= mapper
.CharsetToEncoding(encoding
);
1296 if (enc
== wxFONTENCODING_SYSTEM
)
1297 enc
= wxFONTENCODING_DEFAULT
;
1300 // is this font based on a system font?
1301 wxFont sysfont
= GetSystemFont(GetParamValue(wxT("sysfont")));
1306 sysfont
.SetPointSize(isize
);
1307 else if (HasParam(wxT("relativesize")))
1308 sysfont
.SetPointSize(int(sysfont
.GetPointSize() *
1309 GetFloat(wxT("relativesize"))));
1312 sysfont
.SetStyle(istyle
);
1314 sysfont
.SetWeight(iweight
);
1316 sysfont
.SetUnderlined(underlined
);
1318 sysfont
.SetFamily(ifamily
);
1320 sysfont
.SetFaceName(facename
);
1322 sysfont
.SetDefaultEncoding(enc
);
1329 return wxFont(isize
, ifamily
, istyle
, iweight
,
1330 underlined
, facename
, enc
);
1334 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1336 //FIXME : add cursor
1338 if (HasParam(wxT("exstyle")))
1339 // Have to OR it with existing style, since
1340 // some implementations (e.g. wxGTK) use the extra style
1342 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1343 if (HasParam(wxT("bg")))
1344 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1345 if (HasParam(wxT("fg")))
1346 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1347 if (GetBool(wxT("enabled"), 1) == 0)
1349 if (GetBool(wxT("focused"), 0) == 1)
1351 if (GetBool(wxT("hidden"), 0) == 1)
1354 if (HasParam(wxT("tooltip")))
1355 wnd
->SetToolTip(GetText(wxT("tooltip")));
1357 if (HasParam(wxT("font")))
1358 wnd
->SetFont(GetFont());
1362 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1364 wxXmlNode
*n
= m_node
->GetChildren();
1368 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1369 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1371 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1372 this_hnd_only
? this : NULL
);
1379 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1382 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1383 wxXmlNode
*n
= root
->GetChildren();
1387 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1389 CreateResource(n
, parent
, NULL
);
1401 // --------------- XRCID implementation -----------------------------
1403 #define XRCID_TABLE_SIZE 1024
1413 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1415 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1419 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1420 index
%= XRCID_TABLE_SIZE
;
1422 XRCID_record
*oldrec
= NULL
;
1423 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1425 if (wxStrcmp(rec
->key
, str_id
) == 0)
1432 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1433 &XRCID_Records
[index
] : &oldrec
->next
;
1434 *rec_var
= new XRCID_record
;
1435 (*rec_var
)->key
= wxStrdup(str_id
);
1436 (*rec_var
)->next
= NULL
;
1439 if (value_if_not_found
!= -2)
1440 (*rec_var
)->id
= value_if_not_found
;
1443 int asint
= wxStrtol(str_id
, &end
, 10);
1444 if (*str_id
&& *end
== 0)
1446 // if str_id was integer, keep it verbosely:
1447 (*rec_var
)->id
= asint
;
1451 (*rec_var
)->id
= wxNewId();
1455 return (*rec_var
)->id
;
1458 static void AddStdXRCID_Records();
1460 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1462 static bool s_stdIDsAdded
= false;
1464 if ( !s_stdIDsAdded
)
1466 s_stdIDsAdded
= true;
1467 AddStdXRCID_Records();
1470 return XRCID_Lookup(str_id
);
1474 static void CleanXRCID_Record(XRCID_record
*rec
)
1478 CleanXRCID_Record(rec
->next
);
1484 static void CleanXRCID_Records()
1486 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1488 CleanXRCID_Record(XRCID_Records
[i
]);
1489 XRCID_Records
[i
] = NULL
;
1493 static void AddStdXRCID_Records()
1495 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1499 stdID(wxID_SEPARATOR
);
1512 stdID(wxID_PRINT_SETUP
);
1513 stdID(wxID_PREVIEW
);
1515 stdID(wxID_HELP_CONTENTS
);
1516 stdID(wxID_HELP_COMMANDS
);
1517 stdID(wxID_HELP_PROCEDURES
);
1518 stdID(wxID_HELP_CONTEXT
);
1519 stdID(wxID_CLOSE_ALL
);
1520 stdID(wxID_PREFERENCES
);
1526 stdID(wxID_DUPLICATE
);
1527 stdID(wxID_SELECTALL
);
1529 stdID(wxID_REPLACE
);
1530 stdID(wxID_REPLACE_ALL
);
1531 stdID(wxID_PROPERTIES
);
1532 stdID(wxID_VIEW_DETAILS
);
1533 stdID(wxID_VIEW_LARGEICONS
);
1534 stdID(wxID_VIEW_SMALLICONS
);
1535 stdID(wxID_VIEW_LIST
);
1536 stdID(wxID_VIEW_SORTDATE
);
1537 stdID(wxID_VIEW_SORTNAME
);
1538 stdID(wxID_VIEW_SORTSIZE
);
1539 stdID(wxID_VIEW_SORTTYPE
);
1555 stdID(wxID_FORWARD
);
1556 stdID(wxID_BACKWARD
);
1557 stdID(wxID_DEFAULT
);
1561 stdID(wxID_CONTEXT_HELP
);
1562 stdID(wxID_YESTOALL
);
1563 stdID(wxID_NOTOALL
);
1572 stdID(wxID_REFRESH
);
1577 stdID(wxID_JUSTIFY_CENTER
);
1578 stdID(wxID_JUSTIFY_FILL
);
1579 stdID(wxID_JUSTIFY_RIGHT
);
1580 stdID(wxID_JUSTIFY_LEFT
);
1581 stdID(wxID_UNDERLINE
);
1583 stdID(wxID_UNINDENT
);
1584 stdID(wxID_ZOOM_100
);
1585 stdID(wxID_ZOOM_FIT
);
1586 stdID(wxID_ZOOM_IN
);
1587 stdID(wxID_ZOOM_OUT
);
1588 stdID(wxID_UNDELETE
);
1589 stdID(wxID_REVERT_TO_SAVED
);
1590 stdID(wxID_SYSTEM_MENU
);
1591 stdID(wxID_CLOSE_FRAME
);
1592 stdID(wxID_MOVE_FRAME
);
1593 stdID(wxID_RESIZE_FRAME
);
1594 stdID(wxID_MAXIMIZE_FRAME
);
1595 stdID(wxID_ICONIZE_FRAME
);
1596 stdID(wxID_RESTORE_FRAME
);
1605 // --------------- module and globals -----------------------------
1607 class wxXmlResourceModule
: public wxModule
1609 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1611 wxXmlResourceModule() {}
1614 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1619 delete wxXmlResource::Set(NULL
);
1620 if(wxXmlResource::ms_subclassFactories
)
1621 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1622 wxDELETE(wxXmlResource::ms_subclassFactories
);
1623 CleanXRCID_Records();
1627 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1630 // When wxXml is loaded dynamically after the application is already running
1631 // then the built-in module system won't pick this one up. Add it manually.
1632 void wxXmlInitResourceModule()
1634 wxModule
* module = new wxXmlResourceModule
;
1636 wxModule::RegisterModule(module);