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
);
415 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
416 # else // wxUSE_DATETIME
418 # endif // wxUSE_DATETIME
421 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
426 # else // wxUSE_FILESYSTEM
428 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
429 # else // wxUSE_DATETIME
431 # endif // wxUSE_DATETIME
432 # endif // wxUSE_FILESYSTEM
437 wxLogTrace(_T("xrc"),
438 _T("opening file '%s'"), m_data
[i
].File
.c_str());
440 wxInputStream
*stream
= NULL
;
442 # if wxUSE_FILESYSTEM
443 file
= fsys
.OpenFile(m_data
[i
].File
);
445 stream
= file
->GetStream();
447 stream
= new wxFileInputStream(m_data
[i
].File
);
452 delete m_data
[i
].Doc
;
453 m_data
[i
].Doc
= new wxXmlDocument
;
455 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
457 wxLogError(_("Cannot load resources from file '%s'."),
458 m_data
[i
].File
.c_str());
459 wxDELETE(m_data
[i
].Doc
);
462 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
464 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
465 wxDELETE(m_data
[i
].Doc
);
472 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
473 wxT("version"), wxT("0.0.0.0"));
474 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
475 &v1
, &v2
, &v3
, &v4
) == 4)
476 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
481 if (m_version
!= version
)
483 wxLogError(_("Resource files must have same version number!"));
487 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
490 m_data
[i
].Time
= file
->GetModificationTime();
491 #else // wxUSE_FILESYSTEM
492 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
493 #endif // wxUSE_FILESYSTEM
494 #endif // wxUSE_DATETIME
497 # if wxUSE_FILESYSTEM
510 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
511 const wxString
& name
,
512 const wxString
& classname
,
518 // first search for match at the top-level nodes (as this is
519 // where the resource is most commonly looked for):
520 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
522 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
523 (node
->GetName() == wxT("object") ||
524 node
->GetName() == wxT("object_ref")) &&
525 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
527 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
528 if (!classname
|| cls
== classname
)
530 // object_ref may not have 'class' property:
531 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
533 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
536 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
538 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
547 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
549 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
550 (node
->GetName() == wxT("object") ||
551 node
->GetName() == wxT("object_ref")) )
553 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
562 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
563 const wxString
& classname
,
566 UpdateResources(); //ensure everything is up-to-date
569 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
571 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
574 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
575 name
, classname
, recursive
);
579 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
585 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
586 name
.c_str(), classname
.c_str());
590 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
593 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
595 wxXmlProperty
*dprop
;
596 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
599 if ( dprop
->GetName() == prop
->GetName() )
601 dprop
->SetValue(prop
->GetValue());
607 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
610 // Merge child nodes:
611 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
613 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
616 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
618 if ( dnode
->GetName() == node
->GetName() &&
619 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
620 dnode
->GetType() == node
->GetType() )
622 MergeNodes(*dnode
, *node
);
628 dest
.AddChild(new wxXmlNode(*node
));
631 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
632 dest
.SetContent(with
.GetContent());
635 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
637 wxXmlResourceHandler
*handlerToUse
)
639 if (node
== NULL
) return NULL
;
641 // handling of referenced resource
642 if ( node
->GetName() == wxT("object_ref") )
644 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
645 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
649 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
654 wxXmlNode
copy(*refNode
);
655 MergeNodes(copy
, *node
);
657 return CreateResFromNode(©
, parent
, instance
);
660 wxXmlResourceHandler
*handler
;
664 if (handlerToUse
->CanHandle(node
))
666 return handlerToUse
->CreateResource(node
, parent
, instance
);
669 else if (node
->GetName() == wxT("object"))
671 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
674 handler
= (wxXmlResourceHandler
*)ND
->GetData();
675 if (handler
->CanHandle(node
))
677 return handler
->CreateResource(node
, parent
, instance
);
683 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
684 node
->GetName().c_str(),
685 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
690 #include "wx/listimpl.cpp"
691 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
692 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
)
694 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
696 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
698 if (!ms_subclassFactories
)
700 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
702 ms_subclassFactories
->Append(factory
);
705 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
708 ~wxXmlSubclassFactoryCXX() {}
710 wxObject
*Create(const wxString
& className
)
712 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
715 return classInfo
->CreateObject();
724 wxXmlResourceHandler::wxXmlResourceHandler()
725 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
726 m_parentAsWindow(NULL
)
731 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
733 wxXmlNode
*myNode
= m_node
;
734 wxString myClass
= m_class
;
735 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
736 wxWindow
*myParentAW
= m_parentAsWindow
;
738 m_instance
= instance
;
739 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
740 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
742 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
743 if (!subclass
.empty())
745 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
748 m_instance
= i
->GetData()->Create(subclass
);
755 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
756 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
757 subclass
.c_str(), name
.c_str());
763 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
765 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
767 wxObject
*returned
= DoCreateResource();
771 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
772 m_instance
= myInstance
;
778 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
780 m_styleNames
.Add(name
);
781 m_styleValues
.Add(value
);
786 void wxXmlResourceHandler::AddWindowStyles()
788 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
790 // the border styles all have the old and new names, recognize both for now
791 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
792 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
793 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
794 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
795 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
796 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
798 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
799 XRC_ADD_STYLE(wxWANTS_CHARS
);
800 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
801 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
802 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
803 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
804 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
805 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
810 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
812 return (GetParamNode(param
) != NULL
);
816 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
818 wxString s
= GetParamValue(param
);
820 if (!s
) return defaults
;
822 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
826 while (tkn
.HasMoreTokens())
828 fl
= tkn
.GetNextToken();
829 index
= m_styleNames
.Index(fl
);
830 if (index
!= wxNOT_FOUND
)
831 style
|= m_styleValues
[index
];
833 wxLogError(_("Unknown style flag ") + fl
);
840 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
842 wxXmlNode
*parNode
= GetParamNode(param
);
843 wxString
str1(GetNodeContent(parNode
));
848 // VS: First version of XRC resources used $ instead of & (which is
849 // illegal in XML), but later I realized that '_' fits this purpose
850 // much better (because &File means "File with F underlined").
851 if (m_resource
->CompareVersion(2,3,0,1) < 0)
856 for (dt
= str1
.c_str(); *dt
; dt
++)
858 // Remap amp_char to &, map double amp_char to amp_char (for things
859 // like "&File..." -- this is illegal in XML, so we use "_File..."):
862 if ( *(++dt
) == amp_char
)
865 str2
<< wxT('&') << *dt
;
867 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
868 else if (*dt
== wxT('\\'))
884 // "\\" wasn't translated to "\" prior to 2.5.3.0:
885 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
890 // else fall-through to default: branch below
893 str2
<< wxT('\\') << *dt
;
899 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
901 if (translate
&& parNode
&&
902 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
904 return wxGetTranslation(str2
);
911 // The string is internally stored as UTF-8, we have to convert
912 // it into system's default encoding so that it can be displayed:
913 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
918 // If wxXRC_USE_LOCALE is not set, then the string is already in
919 // system's default encoding in ANSI build, so we don't have to
920 // do anything special here.
926 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
929 wxString str1
= GetParamValue(param
);
931 if (!str1
.ToLong(&value
))
937 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
940 wxString str1
= GetParamValue(param
);
943 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
946 if (!str1
.ToDouble(&value
))
950 setlocale(LC_NUMERIC
, prevlocale
);
953 return wx_truncate_cast(float, value
);
957 int wxXmlResourceHandler::GetID()
959 return wxXmlResource::GetXRCID(GetName());
964 wxString
wxXmlResourceHandler::GetName()
966 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
971 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
973 wxString v
= GetParamValue(param
);
975 if (!v
) return defaultv
;
977 return (v
== wxT("1"));
981 static wxColour
GetSystemColour(const wxString
& name
)
985 #define SYSCLR(clr) \
986 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
987 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
988 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
989 SYSCLR(wxSYS_COLOUR_DESKTOP
)
990 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
991 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
992 SYSCLR(wxSYS_COLOUR_MENU
)
993 SYSCLR(wxSYS_COLOUR_WINDOW
)
994 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
995 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
996 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
997 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
998 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
999 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1000 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1001 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1002 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1003 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1004 SYSCLR(wxSYS_COLOUR_3DFACE
)
1005 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1006 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1007 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1008 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1009 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1010 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1011 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1012 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1013 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1014 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1015 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1016 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1017 SYSCLR(wxSYS_COLOUR_INFOBK
)
1018 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1019 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1020 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1021 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1022 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1023 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1027 return wxNullColour
;
1030 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
1032 wxString v
= GetParamValue(param
);
1034 // find colour using HTML syntax (#RRGGBB)
1035 unsigned long tmp
= 0;
1037 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
1038 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
1040 // the colour doesn't use #RRGGBB format, check if it is symbolic
1042 wxColour clr
= GetSystemColour(v
);
1046 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
1047 v
.c_str(), param
.c_str());
1048 return wxNullColour
;
1051 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
1052 (unsigned char) ((tmp
& 0x00FF00) >> 8),
1053 (unsigned char) ((tmp
& 0x0000FF)));
1058 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1059 const wxArtClient
& defaultArtClient
,
1062 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1063 wxXmlNode
*bmpNode
= GetParamNode(param
);
1066 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
1069 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
1071 scl
= defaultArtClient
;
1073 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1076 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1078 if ( stockArt
.Ok() )
1083 /* ...or load the bitmap from file: */
1084 wxString name
= GetParamValue(param
);
1085 if (name
.empty()) return wxNullBitmap
;
1086 #if wxUSE_FILESYSTEM
1087 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
1090 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1092 return wxNullBitmap
;
1094 wxImage
img(*(fsfile
->GetStream()));
1097 wxImage
img(GetParamValue(wxT("bitmap")));
1102 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1104 return wxNullBitmap
;
1106 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1107 #if !defined(__WXMSW__) || wxUSE_WXDIB
1108 return wxBitmap(img
);
1116 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1117 const wxArtClient
& defaultArtClient
,
1121 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1127 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1129 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1131 wxXmlNode
*n
= m_node
->GetChildren();
1135 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1143 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1145 wxXmlNode
*n
= node
;
1146 if (n
== NULL
) return wxEmptyString
;
1147 n
= n
->GetChildren();
1151 if (n
->GetType() == wxXML_TEXT_NODE
||
1152 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1153 return n
->GetContent();
1156 return wxEmptyString
;
1161 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1164 return GetNodeContent(m_node
);
1166 return GetNodeContent(GetParamNode(param
));
1171 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1172 wxWindow
*windowToUse
)
1174 wxString s
= GetParamValue(param
);
1175 if (s
.empty()) s
= wxT("-1,-1");
1179 is_dlg
= s
[s
.Length()-1] == wxT('d');
1180 if (is_dlg
) s
.RemoveLast();
1182 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1183 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1185 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1186 return wxDefaultSize
;
1193 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1195 else if (m_parentAsWindow
)
1197 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1201 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1202 return wxDefaultSize
;
1206 return wxSize(sx
, sy
);
1211 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1213 wxSize sz
= GetSize(param
);
1214 return wxPoint(sz
.x
, sz
.y
);
1219 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1221 wxWindow
*windowToUse
)
1223 wxString s
= GetParamValue(param
);
1224 if (s
.empty()) return defaultv
;
1228 is_dlg
= s
[s
.Length()-1] == wxT('d');
1229 if (is_dlg
) s
.RemoveLast();
1233 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1241 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1243 else if (m_parentAsWindow
)
1245 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1249 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1258 // Get system font index using indexname
1259 static wxFont
GetSystemFont(const wxString
& name
)
1263 #define SYSFNT(fnt) \
1264 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1265 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1266 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1267 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1268 SYSFNT(wxSYS_SYSTEM_FONT
)
1269 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1270 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1271 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1272 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1279 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1281 wxXmlNode
*font_node
= GetParamNode(param
);
1282 if (font_node
== NULL
)
1284 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1288 wxXmlNode
*oldnode
= m_node
;
1295 bool hasSize
= HasParam(wxT("size"));
1297 isize
= GetLong(wxT("size"), -1);
1300 int istyle
= wxNORMAL
;
1301 bool hasStyle
= HasParam(wxT("style"));
1304 wxString style
= GetParamValue(wxT("style"));
1305 if (style
== wxT("italic"))
1307 else if (style
== wxT("slant"))
1312 int iweight
= wxNORMAL
;
1313 bool hasWeight
= HasParam(wxT("weight"));
1316 wxString weight
= GetParamValue(wxT("weight"));
1317 if (weight
== wxT("bold"))
1319 else if (weight
== wxT("light"))
1324 bool hasUnderlined
= HasParam(wxT("underlined"));
1325 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1327 // family and facename
1328 int ifamily
= wxDEFAULT
;
1329 bool hasFamily
= HasParam(wxT("family"));
1332 wxString family
= GetParamValue(wxT("family"));
1333 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1334 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1335 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1336 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1337 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1338 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1343 bool hasFacename
= HasParam(wxT("face"));
1346 wxString faces
= GetParamValue(wxT("face"));
1347 wxFontEnumerator enu
;
1348 enu
.EnumerateFacenames();
1349 wxStringTokenizer
tk(faces
, wxT(","));
1350 while (tk
.HasMoreTokens())
1352 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), false);
1353 if (index
!= wxNOT_FOUND
)
1355 facename
= (*enu
.GetFacenames())[index
];
1362 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1363 bool hasEncoding
= HasParam(wxT("encoding"));
1366 wxString encoding
= GetParamValue(wxT("encoding"));
1367 wxFontMapper mapper
;
1368 if (!encoding
.empty())
1369 enc
= mapper
.CharsetToEncoding(encoding
);
1370 if (enc
== wxFONTENCODING_SYSTEM
)
1371 enc
= wxFONTENCODING_DEFAULT
;
1374 // is this font based on a system font?
1375 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1379 if (hasSize
&& isize
!= -1)
1380 font
.SetPointSize(isize
);
1381 else if (HasParam(wxT("relativesize")))
1382 font
.SetPointSize(int(font
.GetPointSize() *
1383 GetFloat(wxT("relativesize"))));
1386 font
.SetStyle(istyle
);
1388 font
.SetWeight(iweight
);
1390 font
.SetUnderlined(underlined
);
1392 font
.SetFamily(ifamily
);
1394 font
.SetFaceName(facename
);
1396 font
.SetDefaultEncoding(enc
);
1398 else // not based on system font
1400 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1401 ifamily
, istyle
, iweight
,
1402 underlined
, facename
, enc
);
1410 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1412 //FIXME : add cursor
1414 if (HasParam(wxT("exstyle")))
1415 // Have to OR it with existing style, since
1416 // some implementations (e.g. wxGTK) use the extra style
1418 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1419 if (HasParam(wxT("bg")))
1420 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1421 if (HasParam(wxT("fg")))
1422 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1423 if (GetBool(wxT("enabled"), 1) == 0)
1425 if (GetBool(wxT("focused"), 0) == 1)
1427 if (GetBool(wxT("hidden"), 0) == 1)
1430 if (HasParam(wxT("tooltip")))
1431 wnd
->SetToolTip(GetText(wxT("tooltip")));
1433 if (HasParam(wxT("font")))
1434 wnd
->SetFont(GetFont());
1435 if (HasParam(wxT("help")))
1436 wnd
->SetHelpText(GetText(wxT("help")));
1440 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1442 wxXmlNode
*n
= m_node
->GetChildren();
1446 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1447 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1449 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1450 this_hnd_only
? this : NULL
);
1457 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1460 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1461 wxXmlNode
*n
= root
->GetChildren();
1465 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1467 CreateResource(n
, parent
, NULL
);
1479 // --------------- XRCID implementation -----------------------------
1481 #define XRCID_TABLE_SIZE 1024
1491 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1493 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1497 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1498 index
%= XRCID_TABLE_SIZE
;
1500 XRCID_record
*oldrec
= NULL
;
1501 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1503 if (wxStrcmp(rec
->key
, str_id
) == 0)
1510 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1511 &XRCID_Records
[index
] : &oldrec
->next
;
1512 *rec_var
= new XRCID_record
;
1513 (*rec_var
)->key
= wxStrdup(str_id
);
1514 (*rec_var
)->next
= NULL
;
1517 if (value_if_not_found
!= -2)
1518 (*rec_var
)->id
= value_if_not_found
;
1521 int asint
= wxStrtol(str_id
, &end
, 10);
1522 if (*str_id
&& *end
== 0)
1524 // if str_id was integer, keep it verbosely:
1525 (*rec_var
)->id
= asint
;
1529 (*rec_var
)->id
= wxNewId();
1533 return (*rec_var
)->id
;
1536 static void AddStdXRCID_Records();
1538 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1540 static bool s_stdIDsAdded
= false;
1542 if ( !s_stdIDsAdded
)
1544 s_stdIDsAdded
= true;
1545 AddStdXRCID_Records();
1548 return XRCID_Lookup(str_id
);
1552 static void CleanXRCID_Record(XRCID_record
*rec
)
1556 CleanXRCID_Record(rec
->next
);
1562 static void CleanXRCID_Records()
1564 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1566 CleanXRCID_Record(XRCID_Records
[i
]);
1567 XRCID_Records
[i
] = NULL
;
1571 static void AddStdXRCID_Records()
1573 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1577 stdID(wxID_SEPARATOR
);
1590 stdID(wxID_PRINT_SETUP
);
1591 stdID(wxID_PREVIEW
);
1593 stdID(wxID_HELP_CONTENTS
);
1594 stdID(wxID_HELP_COMMANDS
);
1595 stdID(wxID_HELP_PROCEDURES
);
1596 stdID(wxID_HELP_CONTEXT
);
1597 stdID(wxID_CLOSE_ALL
);
1598 stdID(wxID_PREFERENCES
);
1604 stdID(wxID_DUPLICATE
);
1605 stdID(wxID_SELECTALL
);
1607 stdID(wxID_REPLACE
);
1608 stdID(wxID_REPLACE_ALL
);
1609 stdID(wxID_PROPERTIES
);
1610 stdID(wxID_VIEW_DETAILS
);
1611 stdID(wxID_VIEW_LARGEICONS
);
1612 stdID(wxID_VIEW_SMALLICONS
);
1613 stdID(wxID_VIEW_LIST
);
1614 stdID(wxID_VIEW_SORTDATE
);
1615 stdID(wxID_VIEW_SORTNAME
);
1616 stdID(wxID_VIEW_SORTSIZE
);
1617 stdID(wxID_VIEW_SORTTYPE
);
1633 stdID(wxID_FORWARD
);
1634 stdID(wxID_BACKWARD
);
1635 stdID(wxID_DEFAULT
);
1639 stdID(wxID_CONTEXT_HELP
);
1640 stdID(wxID_YESTOALL
);
1641 stdID(wxID_NOTOALL
);
1650 stdID(wxID_REFRESH
);
1655 stdID(wxID_JUSTIFY_CENTER
);
1656 stdID(wxID_JUSTIFY_FILL
);
1657 stdID(wxID_JUSTIFY_RIGHT
);
1658 stdID(wxID_JUSTIFY_LEFT
);
1659 stdID(wxID_UNDERLINE
);
1661 stdID(wxID_UNINDENT
);
1662 stdID(wxID_ZOOM_100
);
1663 stdID(wxID_ZOOM_FIT
);
1664 stdID(wxID_ZOOM_IN
);
1665 stdID(wxID_ZOOM_OUT
);
1666 stdID(wxID_UNDELETE
);
1667 stdID(wxID_REVERT_TO_SAVED
);
1668 stdID(wxID_SYSTEM_MENU
);
1669 stdID(wxID_CLOSE_FRAME
);
1670 stdID(wxID_MOVE_FRAME
);
1671 stdID(wxID_RESIZE_FRAME
);
1672 stdID(wxID_MAXIMIZE_FRAME
);
1673 stdID(wxID_ICONIZE_FRAME
);
1674 stdID(wxID_RESTORE_FRAME
);
1683 // --------------- module and globals -----------------------------
1685 class wxXmlResourceModule
: public wxModule
1687 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1689 wxXmlResourceModule() {}
1692 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1697 delete wxXmlResource::Set(NULL
);
1698 if(wxXmlResource::ms_subclassFactories
)
1699 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1700 wxDELETE(wxXmlResource::ms_subclassFactories
);
1701 CleanXRCID_Records();
1705 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1708 // When wxXml is loaded dynamically after the application is already running
1709 // then the built-in module system won't pick this one up. Add it manually.
1710 void wxXmlInitResourceModule()
1712 wxModule
* module = new wxXmlResourceModule
;
1714 wxModule::RegisterModule(module);