1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "xmlres.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
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"
40 #include "wx/xml/xml.h"
41 #include "wx/xrc/xmlres.h"
43 #include "wx/arrimpl.cpp"
44 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
47 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
49 /*static*/ wxXmlResource
*wxXmlResource::Get()
52 ms_instance
= new wxXmlResource
;
56 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
58 wxXmlResource
*old
= ms_instance
;
63 wxXmlResource::wxXmlResource(int flags
)
69 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
76 wxXmlResource::~wxXmlResource()
82 bool wxXmlResource::Load(const wxString
& filemask
)
85 wxXmlResourceDataRecord
*drec
;
86 bool iswild
= wxIsWild(filemask
);
91 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
92 # define wxXmlFindNext fsys.FindNext()
94 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
95 # define wxXmlFindNext wxFindNextFile()
103 // NB: Load() accepts both filenames and URLs (should probably be
104 // changed to filenames only, but embedded resources currently
105 // rely on its ability to handle URLs - FIXME). This check
106 // serves as a quick way to determine whether found name is
107 // filename and not URL:
108 if (wxFileName::FileExists(fnd
))
110 // Make the name absolute filename, because the app may
111 // change working directory later:
116 fnd
= fn
.GetFullPath();
119 fnd
= wxFileSystem::FileNameToURL(fnd
);
124 if (fnd
.Lower().Matches(wxT("*.zip")) ||
125 fnd
.Lower().Matches(wxT("*.xrs")))
127 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
132 drec
= new wxXmlResourceDataRecord
;
142 # undef wxXmlFindFirst
143 # undef wxXmlFindNext
144 return rt
&& UpdateResources();
148 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
150 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
152 m_handlers
.Append(handler
);
153 handler
->SetParentResource(this);
156 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
158 m_handlers
.Insert(handler
);
159 handler
->SetParentResource(this);
164 void wxXmlResource::ClearHandlers()
166 WX_CLEAR_LIST(wxList
, m_handlers
);
170 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
172 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
177 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
179 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
185 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
187 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
192 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
194 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
197 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
199 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
204 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
206 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
209 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
211 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
214 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
216 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
219 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
221 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
224 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
226 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
227 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
230 if (bmp
) { rt
= *bmp
; delete bmp
; }
234 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
236 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
237 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
240 if (icon
) { rt
= *icon
; delete icon
; }
245 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
247 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
250 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
252 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
256 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
257 wxWindow
*control
, wxWindow
*parent
)
260 parent
= control
->GetParent();
261 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
264 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
267 return control
->Reparent(container
);
271 static void ProcessPlatformProperty(wxXmlNode
*node
)
276 wxXmlNode
*c
= node
->GetChildren();
280 if (!c
->GetPropVal(wxT("platform"), &s
))
284 wxStringTokenizer
tkn(s
, wxT(" |"));
286 while (tkn
.HasMoreTokens())
288 s
= tkn
.GetNextToken();
290 if (s
== wxT("win")) isok
= true;
292 #if defined(__MAC__) || defined(__APPLE__)
293 if (s
== wxT("mac")) isok
= true;
294 #elif defined(__UNIX__)
295 if (s
== wxT("unix")) isok
= true;
298 if (s
== wxT("os2")) isok
= true;
308 ProcessPlatformProperty(c
);
313 wxXmlNode
*c2
= c
->GetNext();
314 node
->RemoveChild(c
);
323 bool wxXmlResource::UpdateResources()
327 # if wxUSE_FILESYSTEM
328 wxFSFile
*file
= NULL
;
333 wxString
encoding(wxT("UTF-8"));
334 #if !wxUSE_UNICODE && wxUSE_INTL
335 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
337 // In case we are not using wxLocale to translate strings, convert the
338 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
339 // is on, because it could break wxGetTranslation lookup.
340 encoding
= wxLocale::GetSystemEncodingName();
344 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
346 modif
= (m_data
[i
].Doc
== NULL
);
348 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
350 # if wxUSE_FILESYSTEM
351 file
= fsys
.OpenFile(m_data
[i
].File
);
352 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
355 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
361 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
367 wxLogTrace(_T("xrc"),
368 _T("opening file '%s'"), m_data
[i
].File
.c_str());
370 wxInputStream
*stream
= NULL
;
372 # if wxUSE_FILESYSTEM
373 file
= fsys
.OpenFile(m_data
[i
].File
);
375 stream
= file
->GetStream();
377 stream
= new wxFileInputStream(m_data
[i
].File
);
382 delete m_data
[i
].Doc
;
383 m_data
[i
].Doc
= new wxXmlDocument
;
385 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
387 wxLogError(_("Cannot load resources from file '%s'."),
388 m_data
[i
].File
.c_str());
389 wxDELETE(m_data
[i
].Doc
);
392 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
394 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
395 wxDELETE(m_data
[i
].Doc
);
402 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
403 wxT("version"), wxT("0.0.0.0"));
404 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
405 &v1
, &v2
, &v3
, &v4
) == 4)
406 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
411 if (m_version
!= version
)
413 wxLogError(_("Resource files must have same version number!"));
417 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
419 m_data
[i
].Time
= file
->GetModificationTime();
421 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
425 # if wxUSE_FILESYSTEM
438 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
439 const wxString
& name
,
440 const wxString
& classname
,
446 // first search for match at the top-level nodes (as this is
447 // where the resource is most commonly looked for):
448 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
450 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
451 (node
->GetName() == wxT("object") ||
452 node
->GetName() == wxT("object_ref")) &&
453 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
455 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
456 if (!classname
|| cls
== classname
)
458 // object_ref may not have 'class' property:
459 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
461 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
464 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
466 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
475 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
477 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
478 (node
->GetName() == wxT("object") ||
479 node
->GetName() == wxT("object_ref")) )
481 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
490 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
491 const wxString
& classname
,
494 UpdateResources(); //ensure everything is up-to-date
497 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
499 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
502 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
503 name
, classname
, recursive
);
507 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
513 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
514 name
.c_str(), classname
.c_str());
518 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
521 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
523 wxXmlProperty
*dprop
;
524 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
527 if ( dprop
->GetName() == prop
->GetName() )
529 dprop
->SetValue(prop
->GetValue());
535 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
538 // Merge child nodes:
539 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
541 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
544 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
546 if ( dnode
->GetName() == node
->GetName() &&
547 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
548 dnode
->GetType() == node
->GetType() )
550 MergeNodes(*dnode
, *node
);
556 dest
.AddChild(new wxXmlNode(*node
));
559 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
560 dest
.SetContent(with
.GetContent());
563 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
565 wxXmlResourceHandler
*handlerToUse
)
567 if (node
== NULL
) return NULL
;
569 // handling of referenced resource
570 if ( node
->GetName() == wxT("object_ref") )
572 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
573 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
577 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
582 wxXmlNode
copy(*refNode
);
583 MergeNodes(copy
, *node
);
585 return CreateResFromNode(©
, parent
, instance
);
588 wxXmlResourceHandler
*handler
;
592 if (handlerToUse
->CanHandle(node
))
594 return handlerToUse
->CreateResource(node
, parent
, instance
);
597 else if (node
->GetName() == wxT("object"))
599 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
602 handler
= (wxXmlResourceHandler
*)ND
->GetData();
603 if (handler
->CanHandle(node
))
605 return handler
->CreateResource(node
, parent
, instance
);
611 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
612 node
->GetName().c_str(),
613 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
618 #include "wx/listimpl.cpp"
619 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
620 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
622 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
624 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
626 if (!ms_subclassFactories
)
628 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
630 ms_subclassFactories
->Append(factory
);
633 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
636 ~wxXmlSubclassFactoryCXX() {}
638 wxObject
*Create(const wxString
& className
)
640 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
643 return classInfo
->CreateObject();
653 wxXmlResourceHandler::wxXmlResourceHandler()
654 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
655 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
660 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
662 wxXmlNode
*myNode
= m_node
;
663 wxString myClass
= m_class
;
664 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
665 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
667 m_instance
= instance
;
668 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
669 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
671 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
672 if (!subclass
.empty())
674 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
677 m_instance
= i
->GetData()->Create(subclass
);
684 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
685 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
686 subclass
.c_str(), name
.c_str());
692 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
694 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
695 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
697 wxObject
*returned
= DoCreateResource();
701 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
702 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
708 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
710 m_styleNames
.Add(name
);
711 m_styleValues
.Add(value
);
716 void wxXmlResourceHandler::AddWindowStyles()
718 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
719 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
720 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
721 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
722 XRC_ADD_STYLE(wxRAISED_BORDER
);
723 XRC_ADD_STYLE(wxSTATIC_BORDER
);
724 XRC_ADD_STYLE(wxNO_BORDER
);
725 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
726 XRC_ADD_STYLE(wxWANTS_CHARS
);
727 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
728 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
729 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
734 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
736 return (GetParamNode(param
) != NULL
);
740 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
742 wxString s
= GetParamValue(param
);
744 if (!s
) return defaults
;
746 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
750 while (tkn
.HasMoreTokens())
752 fl
= tkn
.GetNextToken();
753 index
= m_styleNames
.Index(fl
);
754 if (index
!= wxNOT_FOUND
)
755 style
|= m_styleValues
[index
];
757 wxLogError(_("Unknown style flag ") + fl
);
764 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
766 wxXmlNode
*parNode
= GetParamNode(param
);
767 wxString
str1(GetNodeContent(parNode
));
772 // VS: First version of XRC resources used $ instead of & (which is
773 // illegal in XML), but later I realized that '_' fits this purpose
774 // much better (because &File means "File with F underlined").
775 if (m_resource
->CompareVersion(2,3,0,1) < 0)
780 for (dt
= str1
.c_str(); *dt
; dt
++)
782 // Remap amp_char to &, map double amp_char to amp_char (for things
783 // like "&File..." -- this is illegal in XML, so we use "_File..."):
786 if ( *(++dt
) == amp_char
)
789 str2
<< wxT('&') << *dt
;
791 // Remap \n to CR, \r to LF, \t to TAB, \\ to \:
792 else if (*dt
== wxT('\\'))
808 // "\\" wasn't translated to "\" prior to 2.5.3.0:
809 if (m_resource
->CompareVersion(2,5,3,0) >= 0)
814 // else fall-through to default: branch below
817 str2
<< wxT('\\') << *dt
;
823 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
825 if (translate
&& parNode
&&
826 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
828 return wxGetTranslation(str2
);
835 // The string is internally stored as UTF-8, we have to convert
836 // it into system's default encoding so that it can be displayed:
837 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
843 // If wxXRC_USE_LOCALE is not set, then the string is already in
844 // system's default encoding in ANSI build, so we don't have to
845 // do anything special here.
852 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
855 wxString str1
= GetParamValue(param
);
857 if (!str1
.ToLong(&value
))
865 int wxXmlResourceHandler::GetID()
867 return wxXmlResource::GetXRCID(GetName());
872 wxString
wxXmlResourceHandler::GetName()
874 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
879 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
881 wxString v
= GetParamValue(param
);
883 if (!v
) return defaultv
;
884 else return (v
== wxT("1"));
889 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
891 wxString v
= GetParamValue(param
);
892 unsigned long tmp
= 0;
894 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
895 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
897 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
898 v
.c_str(), param
.c_str());
902 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
903 (unsigned char) ((tmp
& 0x00FF00) >> 8),
904 (unsigned char) ((tmp
& 0x0000FF)));
909 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
910 const wxArtClient
& defaultArtClient
,
913 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
914 wxXmlNode
*bmpNode
= GetParamNode(param
);
917 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
920 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
922 scl
= defaultArtClient
;
924 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
927 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
934 /* ...or load the bitmap from file: */
935 wxString name
= GetParamValue(param
);
936 if (name
.IsEmpty()) return wxNullBitmap
;
938 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
941 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
945 wxImage
img(*(fsfile
->GetStream()));
948 wxImage
img(GetParamValue(wxT("bitmap")));
953 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
956 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
957 return wxBitmap(img
);
963 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
964 const wxArtClient
& defaultArtClient
,
968 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
974 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
976 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
978 wxXmlNode
*n
= m_node
->GetChildren();
982 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
990 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
993 if (n
== NULL
) return wxEmptyString
;
994 n
= n
->GetChildren();
998 if (n
->GetType() == wxXML_TEXT_NODE
||
999 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
1000 return n
->GetContent();
1003 return wxEmptyString
;
1008 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
1010 if (param
.IsEmpty())
1011 return GetNodeContent(m_node
);
1013 return GetNodeContent(GetParamNode(param
));
1018 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
1020 wxString s
= GetParamValue(param
);
1021 if (s
.IsEmpty()) s
= wxT("-1,-1");
1025 is_dlg
= s
[s
.Length()-1] == wxT('d');
1026 if (is_dlg
) s
.RemoveLast();
1028 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1029 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1031 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1032 return wxDefaultSize
;
1037 if (m_instanceAsWindow
)
1038 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
1039 else if (m_parentAsWindow
)
1040 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1043 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1044 return wxDefaultSize
;
1047 else return wxSize(sx
, sy
);
1052 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1054 wxSize sz
= GetSize(param
);
1055 return wxPoint(sz
.x
, sz
.y
);
1060 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
1062 wxString s
= GetParamValue(param
);
1063 if (s
.IsEmpty()) return defaultv
;
1067 is_dlg
= s
[s
.Length()-1] == wxT('d');
1068 if (is_dlg
) s
.RemoveLast();
1072 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1078 if (m_instanceAsWindow
)
1079 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1080 else if (m_parentAsWindow
)
1081 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1084 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1093 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1095 wxXmlNode
*font_node
= GetParamNode(param
);
1096 if (font_node
== NULL
)
1098 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1102 wxXmlNode
*oldnode
= m_node
;
1105 long size
= GetLong(wxT("size"), 12);
1107 wxString style
= GetParamValue(wxT("style"));
1108 wxString weight
= GetParamValue(wxT("weight"));
1109 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1110 if (style
== wxT("italic")) istyle
= wxITALIC
;
1111 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1112 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1113 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1115 wxString family
= GetParamValue(wxT("family"));
1116 int ifamily
= wxDEFAULT
;
1117 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1118 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1119 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1120 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1121 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1123 bool underlined
= GetBool(wxT("underlined"), false);
1125 wxString encoding
= GetParamValue(wxT("encoding"));
1126 wxFontMapper mapper
;
1127 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1128 if (!encoding
.IsEmpty())
1129 enc
= mapper
.CharsetToEncoding(encoding
);
1130 if (enc
== wxFONTENCODING_SYSTEM
)
1131 enc
= wxFONTENCODING_DEFAULT
;
1133 wxString faces
= GetParamValue(wxT("face"));
1134 wxString facename
= wxEmptyString
;
1135 wxFontEnumerator enu
;
1136 enu
.EnumerateFacenames();
1137 wxStringTokenizer
tk(faces
, wxT(","));
1138 while (tk
.HasMoreTokens())
1140 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), false);
1141 if (index
!= wxNOT_FOUND
)
1143 facename
= (*enu
.GetFacenames())[index
];
1150 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1155 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1157 //FIXME : add cursor
1159 if (HasParam(wxT("exstyle")))
1160 // Have to OR it with existing style, since
1161 // some implementations (e.g. wxGTK) use the extra style
1163 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1164 if (HasParam(wxT("bg")))
1165 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1166 if (HasParam(wxT("fg")))
1167 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1168 if (GetBool(wxT("enabled"), 1) == 0)
1170 if (GetBool(wxT("focused"), 0) == 1)
1172 if (GetBool(wxT("hidden"), 0) == 1)
1175 if (HasParam(wxT("tooltip")))
1176 wnd
->SetToolTip(GetText(wxT("tooltip")));
1178 if (HasParam(wxT("font")))
1179 wnd
->SetFont(GetFont());
1183 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1185 wxXmlNode
*n
= m_node
->GetChildren();
1189 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1190 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1192 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1193 this_hnd_only
? this : NULL
);
1200 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1203 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1204 wxXmlNode
*n
= root
->GetChildren();
1208 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1210 CreateResource(n
, parent
, NULL
);
1222 // --------------- XRCID implementation -----------------------------
1224 #define XRCID_TABLE_SIZE 1024
1234 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1236 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1238 static int XRCID_LastID
= wxID_HIGHEST
;
1242 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1243 index
%= XRCID_TABLE_SIZE
;
1245 XRCID_record
*oldrec
= NULL
;
1246 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1248 if (wxStrcmp(rec
->key
, str_id
) == 0)
1255 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1256 &XRCID_Records
[index
] : &oldrec
->next
;
1257 *rec_var
= new XRCID_record
;
1258 (*rec_var
)->key
= wxStrdup(str_id
);
1259 (*rec_var
)->next
= NULL
;
1262 if (value_if_not_found
!= -2)
1263 (*rec_var
)->id
= value_if_not_found
;
1266 int asint
= wxStrtol(str_id
, &end
, 10);
1267 if (*str_id
&& *end
== 0)
1269 // if str_id was integer, keep it verbosely:
1270 (*rec_var
)->id
= asint
;
1274 (*rec_var
)->id
= ++XRCID_LastID
;
1278 return (*rec_var
)->id
;
1281 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1283 return XRCID_Lookup(str_id
);
1287 static void CleanXRCID_Record(XRCID_record
*rec
)
1291 CleanXRCID_Record(rec
->next
);
1297 static void CleanXRCID_Records()
1299 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1301 CleanXRCID_Record(XRCID_Records
[i
]);
1302 XRCID_Records
[i
] = NULL
;
1306 static void AddStdXRCID_Records()
1308 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1312 stdID(wxID_SEPARATOR
);
1325 stdID(wxID_PRINT_SETUP
);
1326 stdID(wxID_PREVIEW
);
1328 stdID(wxID_HELP_CONTENTS
);
1329 stdID(wxID_HELP_COMMANDS
);
1330 stdID(wxID_HELP_PROCEDURES
);
1331 stdID(wxID_HELP_CONTEXT
);
1332 stdID(wxID_CLOSE_ALL
);
1333 stdID(wxID_PREFERENCES
);
1339 stdID(wxID_DUPLICATE
);
1340 stdID(wxID_SELECTALL
);
1342 stdID(wxID_REPLACE
);
1343 stdID(wxID_REPLACE_ALL
);
1344 stdID(wxID_PROPERTIES
);
1345 stdID(wxID_VIEW_DETAILS
);
1346 stdID(wxID_VIEW_LARGEICONS
);
1347 stdID(wxID_VIEW_SMALLICONS
);
1348 stdID(wxID_VIEW_LIST
);
1349 stdID(wxID_VIEW_SORTDATE
);
1350 stdID(wxID_VIEW_SORTNAME
);
1351 stdID(wxID_VIEW_SORTSIZE
);
1352 stdID(wxID_VIEW_SORTTYPE
);
1368 stdID(wxID_FORWARD
);
1369 stdID(wxID_BACKWARD
);
1370 stdID(wxID_DEFAULT
);
1374 stdID(wxID_CONTEXT_HELP
);
1375 stdID(wxID_YESTOALL
);
1376 stdID(wxID_NOTOALL
);
1385 stdID(wxID_REFRESH
);
1390 stdID(wxID_JUSTIFY_CENTER
);
1391 stdID(wxID_JUSTIFY_FILL
);
1392 stdID(wxID_JUSTIFY_RIGHT
);
1393 stdID(wxID_JUSTIFY_LEFT
);
1394 stdID(wxID_UNDERLINE
);
1396 stdID(wxID_UNINDENT
);
1397 stdID(wxID_ZOOM_100
);
1398 stdID(wxID_ZOOM_FIT
);
1399 stdID(wxID_ZOOM_IN
);
1400 stdID(wxID_ZOOM_OUT
);
1401 stdID(wxID_UNDELETE
);
1402 stdID(wxID_REVERT_TO_SAVED
);
1403 stdID(wxID_SYSTEM_MENU
);
1404 stdID(wxID_CLOSE_FRAME
);
1405 stdID(wxID_MOVE_FRAME
);
1406 stdID(wxID_RESIZE_FRAME
);
1407 stdID(wxID_MAXIMIZE_FRAME
);
1408 stdID(wxID_ICONIZE_FRAME
);
1409 stdID(wxID_RESTORE_FRAME
);
1418 // --------------- module and globals -----------------------------
1420 class wxXmlResourceModule
: public wxModule
1422 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1424 wxXmlResourceModule() {}
1427 AddStdXRCID_Records();
1428 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1433 delete wxXmlResource::Set(NULL
);
1434 if(wxXmlResource::ms_subclassFactories
)
1435 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1436 wxDELETE(wxXmlResource::ms_subclassFactories
);
1437 CleanXRCID_Records();
1441 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1444 // When wxXml is loaded dynamically after the application is already running
1445 // then the built-in module system won't pick this one up. Add it manually.
1446 void wxXmlInitResourceModule()
1448 wxModule
* module = new wxXmlResourceModule
;
1450 wxModule::RegisterModule(module);