1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/xrc/xmlres.cpp
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"
20 #include "wx/xrc/xmlres.h"
27 #include "wx/dialog.h"
28 #include "wx/settings.h"
29 #include "wx/bitmap.h"
31 #include "wx/module.h"
38 #include "wx/wfstream.h"
39 #include "wx/filesys.h"
40 #include "wx/filename.h"
41 #include "wx/tokenzr.h"
42 #include "wx/fontenum.h"
43 #include "wx/fontmap.h"
44 #include "wx/artprov.h"
46 #include "wx/xml/xml.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
, const wxString
& domain
)
75 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
, const wxString
& domain
)
83 wxXmlResource::~wxXmlResource()
88 void wxXmlResource::SetDomain(const wxString
& domain
)
95 wxString
wxXmlResource::ConvertFileNameToURL(const wxString
& filename
)
97 wxString
fnd(filename
);
99 // NB: as Load() and Unload() accept both filenames and URLs (should
100 // probably be changed to filenames only, but embedded resources
101 // currently rely on its ability to handle URLs - FIXME) we need to
102 // determine whether found name is filename and not URL and this is the
103 // fastest/simplest way to do it
104 if (wxFileName::FileExists(fnd
))
106 // Make the name absolute filename, because the app may
107 // change working directory later:
112 fnd
= fn
.GetFullPath();
115 fnd
= wxFileSystem::FileNameToURL(fnd
);
125 bool wxXmlResource::IsArchive(const wxString
& filename
)
127 const wxString fnd
= filename
.Lower();
129 return fnd
.Matches(wxT("*.zip")) || fnd
.Matches(wxT("*.xrs"));
132 #endif // wxUSE_FILESYSTEM
134 bool wxXmlResource::Load(const wxString
& filemask
)
137 wxXmlResourceDataRecord
*drec
;
138 bool iswild
= wxIsWild(filemask
);
143 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
144 # define wxXmlFindNext fsys.FindNext()
146 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
147 # define wxXmlFindNext wxFindNextFile()
150 fnd
= wxXmlFindFirst
;
155 fnd
= ConvertFileNameToURL(fnd
);
158 if ( IsArchive(fnd
) )
160 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
162 else // a single resource URL
163 #endif // wxUSE_FILESYSTEM
165 drec
= new wxXmlResourceDataRecord
;
175 # undef wxXmlFindFirst
176 # undef wxXmlFindNext
177 return rt
&& UpdateResources();
180 bool wxXmlResource::Unload(const wxString
& filename
)
182 wxASSERT_MSG( !wxIsWild(filename
),
183 _T("wildcards not supported by wxXmlResource::Unload()") );
185 wxString fnd
= ConvertFileNameToURL(filename
);
187 const bool isArchive
= IsArchive(fnd
);
190 #endif // wxUSE_FILESYSTEM
192 bool unloaded
= false;
193 const size_t count
= m_data
.GetCount();
194 for ( size_t i
= 0; i
< count
; i
++ )
199 if ( m_data
[i
].File
.StartsWith(fnd
) )
201 // don't break from the loop, we can have other matching files
203 else // a single resource URL
204 #endif // wxUSE_FILESYSTEM
206 if ( m_data
[i
].File
== fnd
)
211 // no sense in continuing, there is only one file with this URL
221 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
223 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
225 m_handlers
.Append(handler
);
226 handler
->SetParentResource(this);
229 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
231 m_handlers
.Insert(handler
);
232 handler
->SetParentResource(this);
237 void wxXmlResource::ClearHandlers()
239 WX_CLEAR_LIST(wxList
, m_handlers
);
243 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
245 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
250 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
252 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
258 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
260 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
265 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
267 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
270 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
272 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
277 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
279 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
282 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
284 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
287 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
289 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
292 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
294 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
297 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
299 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
300 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
303 if (bmp
) { rt
= *bmp
; delete bmp
; }
307 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
309 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
310 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
313 if (icon
) { rt
= *icon
; delete icon
; }
318 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
320 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
323 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
325 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
329 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
330 wxWindow
*control
, wxWindow
*parent
)
333 parent
= control
->GetParent();
334 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
337 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
340 return control
->Reparent(container
);
344 static void ProcessPlatformProperty(wxXmlNode
*node
)
349 wxXmlNode
*c
= node
->GetChildren();
353 if (!c
->GetPropVal(wxT("platform"), &s
))
357 wxStringTokenizer
tkn(s
, wxT(" |"));
359 while (tkn
.HasMoreTokens())
361 s
= tkn
.GetNextToken();
363 if (s
== wxT("win")) isok
= true;
365 #if defined(__MAC__) || defined(__APPLE__)
366 if (s
== wxT("mac")) isok
= true;
367 #elif defined(__UNIX__)
368 if (s
== wxT("unix")) isok
= true;
371 if (s
== wxT("os2")) isok
= true;
381 ProcessPlatformProperty(c
);
386 wxXmlNode
*c2
= c
->GetNext();
387 node
->RemoveChild(c
);
396 bool wxXmlResource::UpdateResources()
400 # if wxUSE_FILESYSTEM
401 wxFSFile
*file
= NULL
;
406 wxString
encoding(wxT("UTF-8"));
407 #if !wxUSE_UNICODE && wxUSE_INTL
408 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
410 // In case we are not using wxLocale to translate strings, convert the
411 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
412 // is on, because it could break wxGetTranslation lookup.
413 encoding
= wxLocale::GetSystemEncodingName();
417 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
419 modif
= (m_data
[i
].Doc
== NULL
);
421 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
423 # if wxUSE_FILESYSTEM
424 file
= fsys
.OpenFile(m_data
[i
].File
);
426 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
427 # else // wxUSE_DATETIME
429 # endif // wxUSE_DATETIME
432 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
437 # else // wxUSE_FILESYSTEM
439 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
440 # else // wxUSE_DATETIME
442 # endif // wxUSE_DATETIME
443 # endif // wxUSE_FILESYSTEM
448 wxLogTrace(_T("xrc"),
449 _T("opening file '%s'"), m_data
[i
].File
.c_str());
451 wxInputStream
*stream
= NULL
;
453 # if wxUSE_FILESYSTEM
454 file
= fsys
.OpenFile(m_data
[i
].File
);
456 stream
= file
->GetStream();
458 stream
= new wxFileInputStream(m_data
[i
].File
);
463 delete m_data
[i
].Doc
;
464 m_data
[i
].Doc
= new wxXmlDocument
;
466 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
468 wxLogError(_("Cannot load resources from file '%s'."),
469 m_data
[i
].File
.c_str());
470 wxDELETE(m_data
[i
].Doc
);
473 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
475 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
476 wxDELETE(m_data
[i
].Doc
);
483 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
484 wxT("version"), wxT("0.0.0.0"));
485 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
486 &v1
, &v2
, &v3
, &v4
) == 4)
487 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
492 if (m_version
!= version
)
494 wxLogError(_("Resource files must have same version number!"));
498 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
501 m_data
[i
].Time
= file
->GetModificationTime();
502 #else // wxUSE_FILESYSTEM
503 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
504 #endif // wxUSE_FILESYSTEM
505 #endif // wxUSE_DATETIME
508 # if wxUSE_FILESYSTEM
521 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
522 const wxString
& name
,
523 const wxString
& classname
,
529 // first search for match at the top-level nodes (as this is
530 // where the resource is most commonly looked for):
531 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
533 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
534 (node
->GetName() == wxT("object") ||
535 node
->GetName() == wxT("object_ref")) &&
536 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
538 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
539 if (!classname
|| cls
== classname
)
541 // object_ref may not have 'class' property:
542 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
544 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
547 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
549 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
558 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
560 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
561 (node
->GetName() == wxT("object") ||
562 node
->GetName() == wxT("object_ref")) )
564 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
573 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
574 const wxString
& classname
,
577 UpdateResources(); //ensure everything is up-to-date
580 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
582 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
585 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
586 name
, classname
, recursive
);
590 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
596 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
597 name
.c_str(), classname
.c_str());
601 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
604 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
606 wxXmlProperty
*dprop
;
607 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
610 if ( dprop
->GetName() == prop
->GetName() )
612 dprop
->SetValue(prop
->GetValue());
618 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
621 // Merge child nodes:
622 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
624 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
627 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
629 if ( dnode
->GetName() == node
->GetName() &&
630 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
631 dnode
->GetType() == node
->GetType() )
633 MergeNodes(*dnode
, *node
);
640 static const wxChar
*AT_END
= wxT("end");
641 wxString insert_pos
= node
->GetPropVal(wxT("insert_at"), AT_END
);
642 if ( insert_pos
== AT_END
)
644 dest
.AddChild(new wxXmlNode(*node
));
646 else if ( insert_pos
== wxT("begin") )
648 dest
.InsertChild(new wxXmlNode(*node
), dest
.GetChildren());
653 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().length() )
654 dest
.SetContent(with
.GetContent());
657 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
659 wxXmlResourceHandler
*handlerToUse
)
661 if (node
== NULL
) return NULL
;
663 // handling of referenced resource
664 if ( node
->GetName() == wxT("object_ref") )
666 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
667 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
671 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
676 wxXmlNode
copy(*refNode
);
677 MergeNodes(copy
, *node
);
679 return CreateResFromNode(©
, parent
, instance
);
682 wxXmlResourceHandler
*handler
;
686 if (handlerToUse
->CanHandle(node
))
688 return handlerToUse
->CreateResource(node
, parent
, instance
);
691 else if (node
->GetName() == wxT("object"))
693 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
696 handler
= (wxXmlResourceHandler
*)ND
->GetData();
697 if (handler
->CanHandle(node
))
699 return handler
->CreateResource(node
, parent
, instance
);
705 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
706 node
->GetName().c_str(),
707 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
712 #include "wx/listimpl.cpp"
713 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
714 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
)
716 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
718 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
720 if (!ms_subclassFactories
)
722 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
724 ms_subclassFactories
->Append(factory
);
727 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
730 ~wxXmlSubclassFactoryCXX() {}
732 wxObject
*Create(const wxString
& className
)
734 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
737 return classInfo
->CreateObject();
746 wxXmlResourceHandler::wxXmlResourceHandler()
747 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
748 m_parentAsWindow(NULL
)
753 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
755 wxXmlNode
*myNode
= m_node
;
756 wxString myClass
= m_class
;
757 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
758 wxWindow
*myParentAW
= m_parentAsWindow
;
760 m_instance
= instance
;
761 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
762 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
764 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
765 if (!subclass
.empty())
767 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
770 m_instance
= i
->GetData()->Create(subclass
);
777 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
778 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
779 subclass
.c_str(), name
.c_str());
785 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
787 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
789 wxObject
*returned
= DoCreateResource();
793 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
794 m_instance
= myInstance
;
800 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
802 m_styleNames
.Add(name
);
803 m_styleValues
.Add(value
);
808 void wxXmlResourceHandler::AddWindowStyles()
810 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
812 // the border styles all have the old and new names, recognize both for now
813 XRC_ADD_STYLE(wxSIMPLE_BORDER
); XRC_ADD_STYLE(wxBORDER_SIMPLE
);
814 XRC_ADD_STYLE(wxSUNKEN_BORDER
); XRC_ADD_STYLE(wxBORDER_SUNKEN
);
815 XRC_ADD_STYLE(wxDOUBLE_BORDER
); XRC_ADD_STYLE(wxBORDER_DOUBLE
);
816 XRC_ADD_STYLE(wxRAISED_BORDER
); XRC_ADD_STYLE(wxBORDER_RAISED
);
817 XRC_ADD_STYLE(wxSTATIC_BORDER
); XRC_ADD_STYLE(wxBORDER_STATIC
);
818 XRC_ADD_STYLE(wxNO_BORDER
); XRC_ADD_STYLE(wxBORDER_NONE
);
820 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
821 XRC_ADD_STYLE(wxWANTS_CHARS
);
822 XRC_ADD_STYLE(wxTAB_TRAVERSAL
);
823 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
824 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
825 XRC_ADD_STYLE(wxALWAYS_SHOW_SB
);
826 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
827 XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY
);
832 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
834 return (GetParamNode(param
) != NULL
);
838 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
840 wxString s
= GetParamValue(param
);
842 if (!s
) return defaults
;
844 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
848 while (tkn
.HasMoreTokens())
850 fl
= tkn
.GetNextToken();
851 index
= m_styleNames
.Index(fl
);
852 if (index
!= wxNOT_FOUND
)
853 style
|= m_styleValues
[index
];
855 wxLogError(_("Unknown style flag ") + fl
);
862 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
864 wxXmlNode
*parNode
= GetParamNode(param
);
865 wxString
str1(GetNodeContent(parNode
));
870 // VS: First version of XRC resources used $ instead of & (which is
871 // illegal in XML), but later I realized that '_' fits this purpose
872 // much better (because &File means "File with F underlined").
873 if (m_resource
->CompareVersion(2,3,0,1) < 0)
878 for (dt
= str1
.c_str(); *dt
; dt
++)
880 // Remap amp_char to &, map double amp_char to amp_char (for things
881 // like "&File..." -- this is illegal in XML, so we use "_File..."):
884 if ( *(++dt
) == amp_char
)
887 str2
<< wxT('&') << *dt
;
889 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
890 else if (*dt
== wxT('\\'))
906 // "\\" wasn't translated to "\" prior to 2.5.3.0:
907 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
912 // else fall-through to default: branch below
915 str2
<< wxT('\\') << *dt
;
921 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
923 if (translate
&& parNode
&&
924 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
926 return wxGetTranslation(str2
, m_resource
->GetDomain());
933 // The string is internally stored as UTF-8, we have to convert
934 // it into system's default encoding so that it can be displayed:
935 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
940 // If wxXRC_USE_LOCALE is not set, then the string is already in
941 // system's default encoding in ANSI build, so we don't have to
942 // do anything special here.
948 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
951 wxString str1
= GetParamValue(param
);
953 if (!str1
.ToLong(&value
))
959 float wxXmlResourceHandler::GetFloat(const wxString
& param
, float defaultv
)
962 wxString str1
= GetParamValue(param
);
965 const char *prevlocale
= setlocale(LC_NUMERIC
, "C");
968 if (!str1
.ToDouble(&value
))
972 setlocale(LC_NUMERIC
, prevlocale
);
975 return wx_truncate_cast(float, value
);
979 int wxXmlResourceHandler::GetID()
981 return wxXmlResource::GetXRCID(GetName());
986 wxString
wxXmlResourceHandler::GetName()
988 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
993 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
995 wxString v
= GetParamValue(param
);
997 if (!v
) return defaultv
;
999 return (v
== wxT("1"));
1003 static wxColour
GetSystemColour(const wxString
& name
)
1007 #define SYSCLR(clr) \
1008 if (name == _T(#clr)) return wxSystemSettings::GetColour(clr);
1009 SYSCLR(wxSYS_COLOUR_SCROLLBAR
)
1010 SYSCLR(wxSYS_COLOUR_BACKGROUND
)
1011 SYSCLR(wxSYS_COLOUR_DESKTOP
)
1012 SYSCLR(wxSYS_COLOUR_ACTIVECAPTION
)
1013 SYSCLR(wxSYS_COLOUR_INACTIVECAPTION
)
1014 SYSCLR(wxSYS_COLOUR_MENU
)
1015 SYSCLR(wxSYS_COLOUR_WINDOW
)
1016 SYSCLR(wxSYS_COLOUR_WINDOWFRAME
)
1017 SYSCLR(wxSYS_COLOUR_MENUTEXT
)
1018 SYSCLR(wxSYS_COLOUR_WINDOWTEXT
)
1019 SYSCLR(wxSYS_COLOUR_CAPTIONTEXT
)
1020 SYSCLR(wxSYS_COLOUR_ACTIVEBORDER
)
1021 SYSCLR(wxSYS_COLOUR_INACTIVEBORDER
)
1022 SYSCLR(wxSYS_COLOUR_APPWORKSPACE
)
1023 SYSCLR(wxSYS_COLOUR_HIGHLIGHT
)
1024 SYSCLR(wxSYS_COLOUR_HIGHLIGHTTEXT
)
1025 SYSCLR(wxSYS_COLOUR_BTNFACE
)
1026 SYSCLR(wxSYS_COLOUR_3DFACE
)
1027 SYSCLR(wxSYS_COLOUR_BTNSHADOW
)
1028 SYSCLR(wxSYS_COLOUR_3DSHADOW
)
1029 SYSCLR(wxSYS_COLOUR_GRAYTEXT
)
1030 SYSCLR(wxSYS_COLOUR_BTNTEXT
)
1031 SYSCLR(wxSYS_COLOUR_INACTIVECAPTIONTEXT
)
1032 SYSCLR(wxSYS_COLOUR_BTNHIGHLIGHT
)
1033 SYSCLR(wxSYS_COLOUR_BTNHILIGHT
)
1034 SYSCLR(wxSYS_COLOUR_3DHIGHLIGHT
)
1035 SYSCLR(wxSYS_COLOUR_3DHILIGHT
)
1036 SYSCLR(wxSYS_COLOUR_3DDKSHADOW
)
1037 SYSCLR(wxSYS_COLOUR_3DLIGHT
)
1038 SYSCLR(wxSYS_COLOUR_INFOTEXT
)
1039 SYSCLR(wxSYS_COLOUR_INFOBK
)
1040 SYSCLR(wxSYS_COLOUR_LISTBOX
)
1041 SYSCLR(wxSYS_COLOUR_HOTLIGHT
)
1042 SYSCLR(wxSYS_COLOUR_GRADIENTACTIVECAPTION
)
1043 SYSCLR(wxSYS_COLOUR_GRADIENTINACTIVECAPTION
)
1044 SYSCLR(wxSYS_COLOUR_MENUHILIGHT
)
1045 SYSCLR(wxSYS_COLOUR_MENUBAR
)
1049 return wxNullColour
;
1052 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
, const wxColour
& defaultv
)
1054 wxString v
= GetParamValue(param
);
1061 // wxString -> wxColour conversion
1064 // the colour doesn't use #RRGGBB format, check if it is symbolic
1066 clr
= GetSystemColour(v
);
1070 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
1071 v
.c_str(), param
.c_str());
1072 return wxNullColour
;
1080 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
1081 const wxArtClient
& defaultArtClient
,
1084 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
1085 wxXmlNode
*bmpNode
= GetParamNode(param
);
1088 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
1091 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
1093 scl
= defaultArtClient
;
1095 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
1098 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
1100 if ( stockArt
.Ok() )
1105 /* ...or load the bitmap from file: */
1106 wxString name
= GetParamValue(param
);
1107 if (name
.empty()) return wxNullBitmap
;
1108 #if wxUSE_FILESYSTEM
1109 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1112 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1114 return wxNullBitmap
;
1116 wxImage
img(*(fsfile
->GetStream()));
1124 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
1126 return wxNullBitmap
;
1128 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
1129 return wxBitmap(img
);
1132 #if wxUSE_ANIMATIONCTRL
1133 wxAnimation
wxXmlResourceHandler::GetAnimation(const wxString
& param
)
1137 /* load the animation from file: */
1138 wxString name
= GetParamValue(param
);
1139 if (name
.empty()) return wxNullAnimation
;
1140 #if wxUSE_FILESYSTEM
1141 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
, wxFS_READ
| wxFS_SEEKABLE
);
1144 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1146 return wxNullAnimation
;
1148 ani
.Load(*(fsfile
->GetStream()));
1156 wxLogError(_("XRC resource: Cannot create animation from '%s'."),
1158 return wxNullAnimation
;
1163 #endif // wxUSE_ANIMATIONCTRL
1167 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
1168 const wxArtClient
& defaultArtClient
,
1172 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
1178 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
1180 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
1182 wxXmlNode
*n
= m_node
->GetChildren();
1186 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
1195 bool wxXmlResourceHandler::IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
1197 return node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
;
1202 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
1204 wxXmlNode
*n
= node
;
1205 if (n
== NULL
) return wxEmptyString
;
1206 n
= n
->GetChildren();
1210 if (n
->GetType() == wxXML_TEXT_NODE
||
1211 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1212 return n
->GetContent();
1215 return wxEmptyString
;
1220 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1223 return GetNodeContent(m_node
);
1225 return GetNodeContent(GetParamNode(param
));
1230 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
,
1231 wxWindow
*windowToUse
)
1233 wxString s
= GetParamValue(param
);
1234 if (s
.empty()) s
= wxT("-1,-1");
1238 is_dlg
= s
[s
.length()-1] == wxT('d');
1239 if (is_dlg
) s
.RemoveLast();
1241 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1242 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1244 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1245 return wxDefaultSize
;
1252 return wxDLG_UNIT(windowToUse
, wxSize(sx
, sy
));
1254 else if (m_parentAsWindow
)
1256 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1260 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1261 return wxDefaultSize
;
1265 return wxSize(sx
, sy
);
1270 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1272 wxSize sz
= GetSize(param
);
1273 return wxPoint(sz
.x
, sz
.y
);
1278 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
,
1280 wxWindow
*windowToUse
)
1282 wxString s
= GetParamValue(param
);
1283 if (s
.empty()) return defaultv
;
1287 is_dlg
= s
[s
.length()-1] == wxT('d');
1288 if (is_dlg
) s
.RemoveLast();
1292 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1300 return wxDLG_UNIT(windowToUse
, wxSize(sx
, 0)).x
;
1302 else if (m_parentAsWindow
)
1304 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1308 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1317 // Get system font index using indexname
1318 static wxFont
GetSystemFont(const wxString
& name
)
1322 #define SYSFNT(fnt) \
1323 if (name == _T(#fnt)) return wxSystemSettings::GetFont(fnt);
1324 SYSFNT(wxSYS_OEM_FIXED_FONT
)
1325 SYSFNT(wxSYS_ANSI_FIXED_FONT
)
1326 SYSFNT(wxSYS_ANSI_VAR_FONT
)
1327 SYSFNT(wxSYS_SYSTEM_FONT
)
1328 SYSFNT(wxSYS_DEVICE_DEFAULT_FONT
)
1329 SYSFNT(wxSYS_DEFAULT_PALETTE
)
1330 SYSFNT(wxSYS_SYSTEM_FIXED_FONT
)
1331 SYSFNT(wxSYS_DEFAULT_GUI_FONT
)
1338 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1340 wxXmlNode
*font_node
= GetParamNode(param
);
1341 if (font_node
== NULL
)
1343 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1347 wxXmlNode
*oldnode
= m_node
;
1354 bool hasSize
= HasParam(wxT("size"));
1356 isize
= GetLong(wxT("size"), -1);
1359 int istyle
= wxNORMAL
;
1360 bool hasStyle
= HasParam(wxT("style"));
1363 wxString style
= GetParamValue(wxT("style"));
1364 if (style
== wxT("italic"))
1366 else if (style
== wxT("slant"))
1371 int iweight
= wxNORMAL
;
1372 bool hasWeight
= HasParam(wxT("weight"));
1375 wxString weight
= GetParamValue(wxT("weight"));
1376 if (weight
== wxT("bold"))
1378 else if (weight
== wxT("light"))
1383 bool hasUnderlined
= HasParam(wxT("underlined"));
1384 bool underlined
= hasUnderlined
? GetBool(wxT("underlined"), false) : false;
1386 // family and facename
1387 int ifamily
= wxDEFAULT
;
1388 bool hasFamily
= HasParam(wxT("family"));
1391 wxString family
= GetParamValue(wxT("family"));
1392 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1393 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1394 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1395 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1396 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1397 else if (family
== wxT("teletype")) ifamily
= wxTELETYPE
;
1402 bool hasFacename
= HasParam(wxT("face"));
1405 wxString faces
= GetParamValue(wxT("face"));
1406 wxArrayString
facenames(wxFontEnumerator::GetFacenames());
1407 wxStringTokenizer
tk(faces
, wxT(","));
1408 while (tk
.HasMoreTokens())
1410 int index
= facenames
.Index(tk
.GetNextToken(), false);
1411 if (index
!= wxNOT_FOUND
)
1413 facename
= facenames
[index
];
1420 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1421 bool hasEncoding
= HasParam(wxT("encoding"));
1424 wxString encoding
= GetParamValue(wxT("encoding"));
1425 wxFontMapper mapper
;
1426 if (!encoding
.empty())
1427 enc
= mapper
.CharsetToEncoding(encoding
);
1428 if (enc
== wxFONTENCODING_SYSTEM
)
1429 enc
= wxFONTENCODING_DEFAULT
;
1432 // is this font based on a system font?
1433 wxFont font
= GetSystemFont(GetParamValue(wxT("sysfont")));
1437 if (hasSize
&& isize
!= -1)
1438 font
.SetPointSize(isize
);
1439 else if (HasParam(wxT("relativesize")))
1440 font
.SetPointSize(int(font
.GetPointSize() *
1441 GetFloat(wxT("relativesize"))));
1444 font
.SetStyle(istyle
);
1446 font
.SetWeight(iweight
);
1448 font
.SetUnderlined(underlined
);
1450 font
.SetFamily(ifamily
);
1452 font
.SetFaceName(facename
);
1454 font
.SetDefaultEncoding(enc
);
1456 else // not based on system font
1458 font
= wxFont(isize
== -1 ? wxNORMAL_FONT
->GetPointSize() : isize
,
1459 ifamily
, istyle
, iweight
,
1460 underlined
, facename
, enc
);
1468 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1470 //FIXME : add cursor
1472 if (HasParam(wxT("exstyle")))
1473 // Have to OR it with existing style, since
1474 // some implementations (e.g. wxGTK) use the extra style
1476 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1477 if (HasParam(wxT("bg")))
1478 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1479 if (HasParam(wxT("fg")))
1480 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1481 if (GetBool(wxT("enabled"), 1) == 0)
1483 if (GetBool(wxT("focused"), 0) == 1)
1485 if (GetBool(wxT("hidden"), 0) == 1)
1488 if (HasParam(wxT("tooltip")))
1489 wnd
->SetToolTip(GetText(wxT("tooltip")));
1491 if (HasParam(wxT("font")))
1492 wnd
->SetFont(GetFont());
1493 if (HasParam(wxT("help")))
1494 wnd
->SetHelpText(GetText(wxT("help")));
1498 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1500 wxXmlNode
*n
= m_node
->GetChildren();
1504 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1505 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1507 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1508 this_hnd_only
? this : NULL
);
1515 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1518 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1519 wxXmlNode
*n
= root
->GetChildren();
1523 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1525 CreateResource(n
, parent
, NULL
);
1537 // --------------- XRCID implementation -----------------------------
1539 #define XRCID_TABLE_SIZE 1024
1549 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1551 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= wxID_NONE
)
1555 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1556 index
%= XRCID_TABLE_SIZE
;
1558 XRCID_record
*oldrec
= NULL
;
1559 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1561 if (wxStrcmp(rec
->key
, str_id
) == 0)
1568 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1569 &XRCID_Records
[index
] : &oldrec
->next
;
1570 *rec_var
= new XRCID_record
;
1571 (*rec_var
)->key
= wxStrdup(str_id
);
1572 (*rec_var
)->next
= NULL
;
1575 if (value_if_not_found
!= wxID_NONE
)
1576 (*rec_var
)->id
= value_if_not_found
;
1579 int asint
= wxStrtol(str_id
, &end
, 10);
1580 if (*str_id
&& *end
== 0)
1582 // if str_id was integer, keep it verbosely:
1583 (*rec_var
)->id
= asint
;
1587 (*rec_var
)->id
= wxNewId();
1591 return (*rec_var
)->id
;
1594 static void AddStdXRCID_Records();
1597 int wxXmlResource::GetXRCID(const wxChar
*str_id
, int value_if_not_found
)
1599 static bool s_stdIDsAdded
= false;
1601 if ( !s_stdIDsAdded
)
1603 s_stdIDsAdded
= true;
1604 AddStdXRCID_Records();
1607 return XRCID_Lookup(str_id
, value_if_not_found
);
1611 static void CleanXRCID_Record(XRCID_record
*rec
)
1615 CleanXRCID_Record(rec
->next
);
1621 static void CleanXRCID_Records()
1623 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1625 CleanXRCID_Record(XRCID_Records
[i
]);
1626 XRCID_Records
[i
] = NULL
;
1630 static void AddStdXRCID_Records()
1632 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1636 stdID(wxID_SEPARATOR
);
1649 stdID(wxID_PRINT_SETUP
);
1650 stdID(wxID_PAGE_SETUP
);
1651 stdID(wxID_PREVIEW
);
1653 stdID(wxID_HELP_CONTENTS
);
1654 stdID(wxID_HELP_COMMANDS
);
1655 stdID(wxID_HELP_PROCEDURES
);
1656 stdID(wxID_HELP_CONTEXT
);
1657 stdID(wxID_CLOSE_ALL
);
1658 stdID(wxID_PREFERENCES
);
1664 stdID(wxID_DUPLICATE
);
1665 stdID(wxID_SELECTALL
);
1667 stdID(wxID_REPLACE
);
1668 stdID(wxID_REPLACE_ALL
);
1669 stdID(wxID_PROPERTIES
);
1670 stdID(wxID_VIEW_DETAILS
);
1671 stdID(wxID_VIEW_LARGEICONS
);
1672 stdID(wxID_VIEW_SMALLICONS
);
1673 stdID(wxID_VIEW_LIST
);
1674 stdID(wxID_VIEW_SORTDATE
);
1675 stdID(wxID_VIEW_SORTNAME
);
1676 stdID(wxID_VIEW_SORTSIZE
);
1677 stdID(wxID_VIEW_SORTTYPE
);
1693 stdID(wxID_FORWARD
);
1694 stdID(wxID_BACKWARD
);
1695 stdID(wxID_DEFAULT
);
1699 stdID(wxID_CONTEXT_HELP
);
1700 stdID(wxID_YESTOALL
);
1701 stdID(wxID_NOTOALL
);
1710 stdID(wxID_REFRESH
);
1715 stdID(wxID_JUSTIFY_CENTER
);
1716 stdID(wxID_JUSTIFY_FILL
);
1717 stdID(wxID_JUSTIFY_RIGHT
);
1718 stdID(wxID_JUSTIFY_LEFT
);
1719 stdID(wxID_UNDERLINE
);
1721 stdID(wxID_UNINDENT
);
1722 stdID(wxID_ZOOM_100
);
1723 stdID(wxID_ZOOM_FIT
);
1724 stdID(wxID_ZOOM_IN
);
1725 stdID(wxID_ZOOM_OUT
);
1726 stdID(wxID_UNDELETE
);
1727 stdID(wxID_REVERT_TO_SAVED
);
1728 stdID(wxID_SYSTEM_MENU
);
1729 stdID(wxID_CLOSE_FRAME
);
1730 stdID(wxID_MOVE_FRAME
);
1731 stdID(wxID_RESIZE_FRAME
);
1732 stdID(wxID_MAXIMIZE_FRAME
);
1733 stdID(wxID_ICONIZE_FRAME
);
1734 stdID(wxID_RESTORE_FRAME
);
1743 // --------------- module and globals -----------------------------
1745 class wxXmlResourceModule
: public wxModule
1747 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1749 wxXmlResourceModule() {}
1752 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1757 delete wxXmlResource::Set(NULL
);
1758 if(wxXmlResource::ms_subclassFactories
)
1759 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1760 wxDELETE(wxXmlResource::ms_subclassFactories
);
1761 CleanXRCID_Records();
1765 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1768 // When wxXml is loaded dynamically after the application is already running
1769 // then the built-in module system won't pick this one up. Add it manually.
1770 void wxXmlInitResourceModule()
1772 wxModule
* module = new wxXmlResourceModule
;
1774 wxModule::RegisterModule(module);