1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
24 #include "wx/dialog.h"
27 #include "wx/wfstream.h"
28 #include "wx/filesys.h"
29 #include "wx/filename.h"
32 #include "wx/tokenzr.h"
33 #include "wx/fontenum.h"
34 #include "wx/module.h"
35 #include "wx/bitmap.h"
37 #include "wx/fontmap.h"
38 #include "wx/artprov.h"
39 #include "wx/settings.h"
41 #include "wx/xml/xml.h"
42 #include "wx/xrc/xmlres.h"
44 #include "wx/arrimpl.cpp"
45 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
48 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
50 /*static*/ wxXmlResource
*wxXmlResource::Get()
53 ms_instance
= new wxXmlResource
;
57 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
59 wxXmlResource
*old
= ms_instance
;
64 wxXmlResource::wxXmlResource(int flags
)
70 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
77 wxXmlResource::~wxXmlResource()
84 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
86 wxString
fnd(filename
);
88 // NB: as Load() and Unload() accept both filenames and URLs (should
89 // probably be changed to filenames only, but embedded resources
90 // currently rely on its ability to handle URLs - FIXME) we need to
91 // determine whether found name is filename and not URL and this is the
92 // fastest/simplest way to do it
93 if (wxFileName::FileExists(fnd
))
95 // Make the name absolute filename, because the app may
96 // change working directory later:
101 fnd
= fn
.GetFullPath();
104 fnd
= wxFileSystem::FileNameToURL(fnd
);
114 bool wxXmlResource::IsArchive(const wxString
& filename
)
116 const wxString fnd
= filename
.Lower();
118 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
121 #endif // wxUSE_FILESYSTEM
123 bool wxXmlResource::Load(const wxString
& filemask
)
126 wxXmlResourceDataRecord
*drec
;
127 bool iswild
= wxIsWild(filemask
);
132 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
133 # define wxXmlFindNext fsys.FindNext()
135 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
136 # define wxXmlFindNext wxFindNextFile()
139 fnd
= wxXmlFindFirst
;
144 fnd
= ConvertFileNameToURL(fnd
);
147 if ( IsArchive(fnd
) )
149 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
151 else // a single resource URL
152 #endif // wxUSE_FILESYSTEM
154 drec
= new wxXmlResourceDataRecord
;
164 # undef wxXmlFindFirst
165 # undef wxXmlFindNext
166 return rt
&& UpdateResources();
169 bool wxXmlResource::Unload(const wxString
& filename
)
171 wxASSERT_MSG( !wxIsWild(filename
),
172 _T("wildcards not supported by wxXmlResource::Unload()") );
174 wxString fnd
= ConvertFileNameToURL(filename
);
176 const bool isArchive
= IsArchive(fnd
);
179 #endif // wxUSE_FILESYSTEM
181 bool unloaded
= false;
182 const size_t count
= m_data
.GetCount();
183 for ( size_t i
= 0; i
< count
; i
++ )
188 if ( m_data
[i
].File
.StartsWith(fnd
) )
190 // don't break from the loop, we can have other matching files
192 else // a single resource URL
193 #endif // wxUSE_FILESYSTEM
195 if ( m_data
[i
].File
== fnd
)
200 // no sense in continuing, there is only one file with this URL
210 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
212 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
214 m_handlers
.Append(handler
);
215 handler
->SetParentResource(this);
218 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
220 m_handlers
.Insert(handler
);
221 handler
->SetParentResource(this);
226 void wxXmlResource::ClearHandlers()
228 WX_CLEAR_LIST(wxList
, m_handlers
);
232 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
234 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
239 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
241 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
247 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
249 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
254 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
256 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
259 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
261 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
266 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
268 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
271 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
273 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
276 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
278 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
281 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
283 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
286 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
288 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
289 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
292 if (bmp
) { rt
= *bmp
; delete bmp
; }
296 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
298 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
299 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
302 if (icon
) { rt
= *icon
; delete icon
; }
307 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
309 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
312 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
314 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
318 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
319 wxWindow
*control
, wxWindow
*parent
)
322 parent
= control
->GetParent();
323 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
326 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
329 return control
->Reparent(container
);
333 static void ProcessPlatformProperty(wxXmlNode
*node
)
338 wxXmlNode
*c
= node
->GetChildren();
342 if (!c
->GetPropVal(wxT("platform"), &s
))
346 wxStringTokenizer
tkn(s
, wxT(" |"));
348 while (tkn
.HasMoreTokens())
350 s
= tkn
.GetNextToken();
352 if (s
== wxT("win")) isok
= true;
354 #if defined(__MAC__) || defined(__APPLE__)
355 if (s
== wxT("mac")) isok
= true;
356 #elif defined(__UNIX__)
357 if (s
== wxT("unix")) isok
= true;
360 if (s
== wxT("os2")) isok
= true;
370 ProcessPlatformProperty(c
);
375 wxXmlNode
*c2
= c
->GetNext();
376 node
->RemoveChild(c
);
385 bool wxXmlResource::UpdateResources()
389 # if wxUSE_FILESYSTEM
390 wxFSFile
*file
= NULL
;
395 wxString
encoding(wxT("UTF-8"));
396 #if !wxUSE_UNICODE && wxUSE_INTL
397 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
399 // In case we are not using wxLocale to translate strings, convert the
400 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
401 // is on, because it could break wxGetTranslation lookup.
402 encoding
= wxLocale::GetSystemEncodingName();
406 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
408 modif
= (m_data
[i
].Doc
== NULL
);
410 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
412 # if wxUSE_FILESYSTEM
413 file
= fsys
.OpenFile(m_data
[i
].File
);
414 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
417 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
423 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
429 wxLogTrace(_T("xrc"),
430 _T("opening file '%s'"), m_data
[i
].File
.c_str());
432 wxInputStream
*stream
= NULL
;
434 # if wxUSE_FILESYSTEM
435 file
= fsys
.OpenFile(m_data
[i
].File
);
437 stream
= file
->GetStream();
439 stream
= new wxFileInputStream(m_data
[i
].File
);
444 delete m_data
[i
].Doc
;
445 m_data
[i
].Doc
= new wxXmlDocument
;
447 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
449 wxLogError(_("Cannot load resources from file '%s'."),
450 m_data
[i
].File
.c_str());
451 wxDELETE(m_data
[i
].Doc
);
454 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
456 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
457 wxDELETE(m_data
[i
].Doc
);
464 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
465 wxT("version"), wxT("0.0.0.0"));
466 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
467 &v1
, &v2
, &v3
, &v4
) == 4)
468 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
473 if (m_version
!= version
)
475 wxLogError(_("Resource files must have same version number!"));
479 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
481 m_data
[i
].Time
= file
->GetModificationTime();
483 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
487 # if wxUSE_FILESYSTEM
500 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
501 const wxString
& name
,
502 const wxString
& classname
,
508 // first search for match at the top-level nodes (as this is
509 // where the resource is most commonly looked for):
510 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
512 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
513 (node
->GetName() == wxT("object") ||
514 node
->GetName() == wxT("object_ref")) &&
515 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
517 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
518 if (!classname
|| cls
== classname
)
520 // object_ref may not have 'class' property:
521 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
523 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
526 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
528 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
537 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
539 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
540 (node
->GetName() == wxT("object") ||
541 node
->GetName() == wxT("object_ref")) )
543 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
552 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
553 const wxString
& classname
,
556 UpdateResources(); //ensure everything is up-to-date
559 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
561 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
564 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
565 name
, classname
, recursive
);
569 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
575 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
576 name
.c_str(), classname
.c_str());
580 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
583 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
585 wxXmlProperty
*dprop
;
586 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
589 if ( dprop
->GetName() == prop
->GetName() )
591 dprop
->SetValue(prop
->GetValue());
597 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
600 // Merge child nodes:
601 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
603 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
606 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
608 if ( dnode
->GetName() == node
->GetName() &&
609 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
610 dnode
->GetType() == node
->GetType() )
612 MergeNodes(*dnode
, *node
);
618 dest
.AddChild(new wxXmlNode(*node
));
621 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
622 dest
.SetContent(with
.GetContent());
625 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
627 wxXmlResourceHandler
*handlerToUse
)
629 if (node
== NULL
) return NULL
;
631 // handling of referenced resource
632 if ( node
->GetName() == wxT("object_ref") )
634 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
635 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
639 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
644 wxXmlNode
copy(*refNode
);
645 MergeNodes(copy
, *node
);
647 return CreateResFromNode(©
, parent
, instance
);
650 wxXmlResourceHandler
*handler
;
654 if (handlerToUse
->CanHandle(node
))
656 return handlerToUse
->CreateResource(node
, parent
, instance
);
659 else if (node
->GetName() == wxT("object"))
661 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
664 handler
= (wxXmlResourceHandler
*)ND
->GetData();
665 if (handler
->CanHandle(node
))
667 return handler
->CreateResource(node
, parent
, instance
);
673 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
674 node
->GetName().c_str(),
675 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
680 #include "wx/listimpl.cpp"
681 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
682 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
684 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
686 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
688 if (!ms_subclassFactories
)
690 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
692 ms_subclassFactories
->Append(factory
);
695 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
698 ~wxXmlSubclassFactoryCXX() {}
700 wxObject
*Create(const wxString
& className
)
702 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
705 return classInfo
->CreateObject();
714 wxXmlResourceHandler::wxXmlResourceHandler()
715 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
716 m_parentAsWindow(NULL
)
721 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
723 wxXmlNode
*myNode
= m_node
;
724 wxString myClass
= m_class
;
725 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
726 wxWindow
*myParentAW
= m_parentAsWindow
;
728 m_instance
= instance
;
729 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
730 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
732 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
733 if (!subclass
.empty())
735 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
738 m_instance
= i
->GetData()->Create(subclass
);
745 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
746 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
747 subclass
.c_str(), name
.c_str());
753 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
755 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
757 wxObject
*returned
= DoCreateResource();
761 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
762 m_instance
= myInstance
;
768 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
770 m_styleNames
.Add(name
);
771 m_styleValues
.Add(value
);
776 void wxXmlResourceHandler::AddWindowStyles()
778 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
779 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
780 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
781 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
782 XRC_ADD_STYLE(wxRAISED_BORDER
);
783 XRC_ADD_STYLE(wxSTATIC_BORDER
);
784 XRC_ADD_STYLE(wxNO_BORDER
);
785 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
786 XRC_ADD_STYLE(wxWANTS_CHARS
);
787 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
788 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
789 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
790 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
791 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
796 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
798 return (GetParamNode(param
) != NULL
);
802 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
804 wxString s
= GetParamValue(param
);
806 if (!s
) return defaults
;
808 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
812 while (tkn
.HasMoreTokens())
814 fl
= tkn
.GetNextToken();
815 index
= m_styleNames
.Index(fl
);
816 if (index
!= wxNOT_FOUND
)
817 style
|= m_styleValues
[index
];
819 wxLogError(_("Unknown style flag ") + fl
);
826 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
828 wxXmlNode
*parNode
= GetParamNode(param
);
829 wxString
str1(GetNodeContent(parNode
));
834 // VS: First version of XRC resources used $ instead of & (which is
835 // illegal in XML), but later I realized that '_' fits this purpose
836 // much better (because &File means "File with F underlined").
837 if (m_resource
->CompareVersion(2,3,0,1) < 0)
842 for (dt
= str1
.c_str(); *dt
; dt
++)
844 // Remap amp_char to &, map double amp_char to amp_char (for things
845 // like "&File..." -- this is illegal in XML, so we use "_File..."):
848 if ( *(++dt
) == amp_char
)
851 str2
<< wxT('&') << *dt
;
853 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
854 else if (*dt
== wxT('\\'))
870 // "\\" wasn't translated to "\" prior to 2.5.3.0:
871 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
876 // else fall-through to default: branch below
879 str2
<< wxT('\\') << *dt
;
885 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
887 if (translate
&& parNode
&&
888 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
890 return wxGetTranslation(str2
);
897 // The string is internally stored as UTF-8, we have to convert
898 // it into system's default encoding so that it can be displayed:
899 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
904 // If wxXRC_USE_LOCALE is not set, then the string is already in
905 // system's default encoding in ANSI build, so we don't have to
906 // do anything special here.
912 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
915 wxString str1
= GetParamValue(param
);
917 if (!str1
.ToLong(&value
))
923 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
926 wxString str1
= GetParamValue(param
);
929 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
932 if (!str1
.ToDouble(&value
))
936 setlocale(LC_NUMERIC
, prevlocale
);
943 int wxXmlResourceHandler::GetID()
945 return wxXmlResource::GetXRCID(GetName());
950 wxString
wxXmlResourceHandler::GetName()
952 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
957 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
959 wxString v
= GetParamValue(param
);
961 if (!v
) return defaultv
;
963 return (v
== wxT("1"));
967 static wxColour
GetSystemColour(const wxString
& name
)
971 #define SYSCLR(clr) \
972 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
973 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
974 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
975 SYSCLR(wxSYS_COLOUR_DESKTOP
)
976 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
977 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
978 SYSCLR(wxSYS_COLOUR_MENU
)
979 SYSCLR(wxSYS_COLOUR_WINDOW
)
980 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
981 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
982 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
983 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
984 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
985 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
986 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
987 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
988 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
989 SYSCLR(wxSYS_COLOUR_BTNFACE
)
990 SYSCLR(wxSYS_COLOUR_3DFACE
)
991 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
992 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
993 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
994 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
995 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
996 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
997 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
998 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
999 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1000 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1001 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1002 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1003 SYSCLR(wxSYS_COLOUR_INFOBK
)
1004 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1005 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1006 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1007 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1008 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1009 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1013 return wxNullColour
;
1016 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
1018 wxString v
= GetParamValue(param
);
1020 // find colour using HTML syntax (#RRGGBB)
1021 unsigned long tmp
= 0;
1023 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
1024 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
1026 // the colour doesn't use #RRGGBB format, check if it is symbolic
1028 wxColour clr
= GetSystemColour(v
);
1032 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
1033 v
.c_str(), param
.c_str());
1034 return wxNullColour
;
1037 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
1038 (unsigned char) ((tmp
& 0x00FF00) >> 8),
1039 (unsigned char) ((tmp
& 0x0000FF)));
1044 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1045 const wxArtClient
& defaultArtClient
,
1048 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1049 wxXmlNode
*bmpNode
= GetParamNode(param
);
1052 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
1055 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
1057 scl
= defaultArtClient
;
1059 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1062 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1064 if ( stockArt
.Ok() )
1069 /* ...or load the bitmap from file: */
1070 wxString name
= GetParamValue(param
);
1071 if (name
.empty()) return wxNullBitmap
;
1072 #if wxUSE_FILESYSTEM
1073 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
1076 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1078 return wxNullBitmap
;
1080 wxImage
img(*(fsfile
->GetStream()));
1083 wxImage
img(GetParamValue(wxT("bitmap")));
1088 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1090 return wxNullBitmap
;
1092 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1093 return wxBitmap(img
);
1099 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1100 const wxArtClient
& defaultArtClient
,
1104 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1110 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1112 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1114 wxXmlNode
*n
= m_node
->GetChildren();
1118 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1126 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1128 wxXmlNode
*n
= node
;
1129 if (n
== NULL
) return wxEmptyString
;
1130 n
= n
->GetChildren();
1134 if (n
->GetType() == wxXML_TEXT_NODE
||
1135 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1136 return n
->GetContent();
1139 return wxEmptyString
;
1144 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1147 return GetNodeContent(m_node
);
1149 return GetNodeContent(GetParamNode(param
));
1154 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1155 wxWindow
*windowToUse
)
1157 wxString s
= GetParamValue(param
);
1158 if (s
.empty()) s
= wxT("-1,-1");
1162 is_dlg
= s
[s
.Length()-1] == wxT('d');
1163 if (is_dlg
) s
.RemoveLast();
1165 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1166 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1168 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1169 return wxDefaultSize
;
1176 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1178 else if (m_parentAsWindow
)
1180 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1184 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1185 return wxDefaultSize
;
1189 return wxSize(sx
, sy
);
1194 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1196 wxSize sz
= GetSize(param
);
1197 return wxPoint(sz
.x
, sz
.y
);
1202 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1204 wxWindow
*windowToUse
)
1206 wxString s
= GetParamValue(param
);
1207 if (s
.empty()) return defaultv
;
1211 is_dlg
= s
[s
.Length()-1] == wxT('d');
1212 if (is_dlg
) s
.RemoveLast();
1216 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1224 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1226 else if (m_parentAsWindow
)
1228 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1232 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1241 // Get system font index using indexname
1242 static wxFont
GetSystemFont(const wxString
& name
)
1246 #define SYSFNT(fnt) \
1247 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1248 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1249 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1250 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1251 SYSFNT(wxSYS_SYSTEM_FONT
)
1252 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1253 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1254 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1255 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1262 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1264 wxXmlNode
*font_node
= GetParamNode(param
);
1265 if (font_node
== NULL
)
1267 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1271 wxXmlNode
*oldnode
= m_node
;
1277 int isize
= wxDEFAULT
;
1278 bool hasSize
= HasParam(wxT("size"));
1280 isize
= GetLong(wxT("size"), wxDEFAULT
);
1283 int istyle
= wxNORMAL
;
1284 bool hasStyle
= HasParam(wxT("style"));
1287 wxString style
= GetParamValue(wxT("style"));
1288 if (style
== wxT("italic"))
1290 else if (style
== wxT("slant"))
1295 int iweight
= wxNORMAL
;
1296 bool hasWeight
= HasParam(wxT("weight"));
1299 wxString weight
= GetParamValue(wxT("weight"));
1300 if (weight
== wxT("bold"))
1302 else if (weight
== wxT("light"))
1307 bool hasUnderlined
= HasParam(wxT("underlined"));
1308 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1310 // family and facename
1311 int ifamily
= wxDEFAULT
;
1312 bool hasFamily
= HasParam(wxT("family"));
1315 wxString family
= GetParamValue(wxT("family"));
1316 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1317 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1318 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1319 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1320 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1321 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1326 bool hasFacename
= HasParam(wxT("face"));
1329 wxString faces
= GetParamValue(wxT("face"));
1330 wxFontEnumerator enu
;
1331 enu
.EnumerateFacenames();
1332 wxStringTokenizer
tk(faces
, wxT(","));
1333 while (tk
.HasMoreTokens())
1335 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), false);
1336 if (index
!= wxNOT_FOUND
)
1338 facename
= (*enu
.GetFacenames())[index
];
1345 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1346 bool hasEncoding
= HasParam(wxT("encoding"));
1349 wxString encoding
= GetParamValue(wxT("encoding"));
1350 wxFontMapper mapper
;
1351 if (!encoding
.empty())
1352 enc
= mapper
.CharsetToEncoding(encoding
);
1353 if (enc
== wxFONTENCODING_SYSTEM
)
1354 enc
= wxFONTENCODING_DEFAULT
;
1357 // is this font based on a system font?
1358 wxFont sysfont
= GetSystemFont(GetParamValue(wxT("sysfont")));
1363 sysfont
.SetPointSize(isize
);
1364 else if (HasParam(wxT("relativesize")))
1365 sysfont
.SetPointSize(int(sysfont
.GetPointSize() *
1366 GetFloat(wxT("relativesize"))));
1369 sysfont
.SetStyle(istyle
);
1371 sysfont
.SetWeight(iweight
);
1373 sysfont
.SetUnderlined(underlined
);
1375 sysfont
.SetFamily(ifamily
);
1377 sysfont
.SetFaceName(facename
);
1379 sysfont
.SetDefaultEncoding(enc
);
1386 return wxFont(isize
, ifamily
, istyle
, iweight
,
1387 underlined
, facename
, enc
);
1391 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1393 //FIXME : add cursor
1395 if (HasParam(wxT("exstyle")))
1396 // Have to OR it with existing style, since
1397 // some implementations (e.g. wxGTK) use the extra style
1399 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1400 if (HasParam(wxT("bg")))
1401 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1402 if (HasParam(wxT("fg")))
1403 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1404 if (GetBool(wxT("enabled"), 1) == 0)
1406 if (GetBool(wxT("focused"), 0) == 1)
1408 if (GetBool(wxT("hidden"), 0) == 1)
1411 if (HasParam(wxT("tooltip")))
1412 wnd
->SetToolTip(GetText(wxT("tooltip")));
1414 if (HasParam(wxT("font")))
1415 wnd
->SetFont(GetFont());
1416 if (HasParam(wxT("help")))
1417 wnd
->SetHelpText(GetText(wxT("help")));
1421 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1423 wxXmlNode
*n
= m_node
->GetChildren();
1427 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1428 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1430 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1431 this_hnd_only
? this : NULL
);
1438 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1441 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1442 wxXmlNode
*n
= root
->GetChildren();
1446 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1448 CreateResource(n
, parent
, NULL
);
1460 // --------------- XRCID implementation -----------------------------
1462 #define XRCID_TABLE_SIZE 1024
1472 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1474 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1478 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1479 index
%= XRCID_TABLE_SIZE
;
1481 XRCID_record
*oldrec
= NULL
;
1482 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1484 if (wxStrcmp(rec
->key
, str_id
) == 0)
1491 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1492 &XRCID_Records
[index
] : &oldrec
->next
;
1493 *rec_var
= new XRCID_record
;
1494 (*rec_var
)->key
= wxStrdup(str_id
);
1495 (*rec_var
)->next
= NULL
;
1498 if (value_if_not_found
!= -2)
1499 (*rec_var
)->id
= value_if_not_found
;
1502 int asint
= wxStrtol(str_id
, &end
, 10);
1503 if (*str_id
&& *end
== 0)
1505 // if str_id was integer, keep it verbosely:
1506 (*rec_var
)->id
= asint
;
1510 (*rec_var
)->id
= wxNewId();
1514 return (*rec_var
)->id
;
1517 static void AddStdXRCID_Records();
1519 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1521 static bool s_stdIDsAdded
= false;
1523 if ( !s_stdIDsAdded
)
1525 s_stdIDsAdded
= true;
1526 AddStdXRCID_Records();
1529 return XRCID_Lookup(str_id
);
1533 static void CleanXRCID_Record(XRCID_record
*rec
)
1537 CleanXRCID_Record(rec
->next
);
1543 static void CleanXRCID_Records()
1545 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1547 CleanXRCID_Record(XRCID_Records
[i
]);
1548 XRCID_Records
[i
] = NULL
;
1552 static void AddStdXRCID_Records()
1554 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1558 stdID(wxID_SEPARATOR
);
1571 stdID(wxID_PRINT_SETUP
);
1572 stdID(wxID_PREVIEW
);
1574 stdID(wxID_HELP_CONTENTS
);
1575 stdID(wxID_HELP_COMMANDS
);
1576 stdID(wxID_HELP_PROCEDURES
);
1577 stdID(wxID_HELP_CONTEXT
);
1578 stdID(wxID_CLOSE_ALL
);
1579 stdID(wxID_PREFERENCES
);
1585 stdID(wxID_DUPLICATE
);
1586 stdID(wxID_SELECTALL
);
1588 stdID(wxID_REPLACE
);
1589 stdID(wxID_REPLACE_ALL
);
1590 stdID(wxID_PROPERTIES
);
1591 stdID(wxID_VIEW_DETAILS
);
1592 stdID(wxID_VIEW_LARGEICONS
);
1593 stdID(wxID_VIEW_SMALLICONS
);
1594 stdID(wxID_VIEW_LIST
);
1595 stdID(wxID_VIEW_SORTDATE
);
1596 stdID(wxID_VIEW_SORTNAME
);
1597 stdID(wxID_VIEW_SORTSIZE
);
1598 stdID(wxID_VIEW_SORTTYPE
);
1614 stdID(wxID_FORWARD
);
1615 stdID(wxID_BACKWARD
);
1616 stdID(wxID_DEFAULT
);
1620 stdID(wxID_CONTEXT_HELP
);
1621 stdID(wxID_YESTOALL
);
1622 stdID(wxID_NOTOALL
);
1631 stdID(wxID_REFRESH
);
1636 stdID(wxID_JUSTIFY_CENTER
);
1637 stdID(wxID_JUSTIFY_FILL
);
1638 stdID(wxID_JUSTIFY_RIGHT
);
1639 stdID(wxID_JUSTIFY_LEFT
);
1640 stdID(wxID_UNDERLINE
);
1642 stdID(wxID_UNINDENT
);
1643 stdID(wxID_ZOOM_100
);
1644 stdID(wxID_ZOOM_FIT
);
1645 stdID(wxID_ZOOM_IN
);
1646 stdID(wxID_ZOOM_OUT
);
1647 stdID(wxID_UNDELETE
);
1648 stdID(wxID_REVERT_TO_SAVED
);
1649 stdID(wxID_SYSTEM_MENU
);
1650 stdID(wxID_CLOSE_FRAME
);
1651 stdID(wxID_MOVE_FRAME
);
1652 stdID(wxID_RESIZE_FRAME
);
1653 stdID(wxID_MAXIMIZE_FRAME
);
1654 stdID(wxID_ICONIZE_FRAME
);
1655 stdID(wxID_RESTORE_FRAME
);
1664 // --------------- module and globals -----------------------------
1666 class wxXmlResourceModule
: public wxModule
1668 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1670 wxXmlResourceModule() {}
1673 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1678 delete wxXmlResource::Set(NULL
);
1679 if(wxXmlResource::ms_subclassFactories
)
1680 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1681 wxDELETE(wxXmlResource::ms_subclassFactories
);
1682 CleanXRCID_Records();
1686 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1689 // When wxXml is loaded dynamically after the application is already running
1690 // then the built-in module system won't pick this one up. Add it manually.
1691 void wxXmlInitResourceModule()
1693 wxModule
* module = new wxXmlResourceModule
;
1695 wxModule::RegisterModule(module);