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"
22 #include "wx/dialog.h"
25 #include "wx/wfstream.h"
26 #include "wx/filesys.h"
27 #include "wx/filename.h"
30 #include "wx/tokenzr.h"
31 #include "wx/fontenum.h"
32 #include "wx/module.h"
33 #include "wx/bitmap.h"
35 #include "wx/fontmap.h"
36 #include "wx/artprov.h"
38 #include "wx/xml/xml.h"
39 #include "wx/xrc/xmlres.h"
41 #include "wx/arrimpl.cpp"
42 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
45 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
47 /*static*/ wxXmlResource
*wxXmlResource::Get()
50 ms_instance
= new wxXmlResource
;
54 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
56 wxXmlResource
*old
= ms_instance
;
61 wxXmlResource::wxXmlResource(int flags
)
67 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
74 wxXmlResource::~wxXmlResource()
80 bool wxXmlResource::Load(const wxString
& filemask
)
83 wxXmlResourceDataRecord
*drec
;
84 bool iswild
= wxIsWild(filemask
);
89 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
90 # define wxXmlFindNext fsys.FindNext()
92 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
93 # define wxXmlFindNext wxFindNextFile()
101 // NB: Load() accepts both filenames and URLs (should probably be
102 // changed to filenames only, but embedded resources currently
103 // rely on its ability to handle URLs - FIXME). This check
104 // serves as a quick way to determine whether found name is
105 // filename and not URL:
106 if (wxFileName::FileExists(fnd
))
108 // Make the name absolute filename, because the app may
109 // change working directory later:
114 fnd
= fn
.GetFullPath();
117 fnd
= wxFileSystem::FileNameToURL(fnd
);
122 if (fnd
.Lower().Matches(wxT("*.zip")) ||
123 fnd
.Lower().Matches(wxT("*.xrs")))
125 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
130 drec
= new wxXmlResourceDataRecord
;
140 # undef wxXmlFindFirst
141 # undef wxXmlFindNext
142 return rt
&& UpdateResources();
146 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
148 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
150 m_handlers
.Append(handler
);
151 handler
->SetParentResource(this);
154 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
156 m_handlers
.Insert(handler
);
157 handler
->SetParentResource(this);
162 void wxXmlResource::ClearHandlers()
164 WX_CLEAR_LIST(wxList
, m_handlers
);
168 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
170 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
175 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
177 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
183 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
185 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
190 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
192 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
195 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
197 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
202 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
204 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
207 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
209 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
212 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
214 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
217 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
219 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
222 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
224 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
225 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
228 if (bmp
) { rt
= *bmp
; delete bmp
; }
232 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
234 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
235 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
238 if (icon
) { rt
= *icon
; delete icon
; }
243 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
245 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
248 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
250 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
254 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
255 wxWindow
*control
, wxWindow
*parent
)
258 parent
= control
->GetParent();
259 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
262 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
265 return control
->Reparent(container
);
269 static void ProcessPlatformProperty(wxXmlNode
*node
)
274 wxXmlNode
*c
= node
->GetChildren();
278 if (!c
->GetPropVal(wxT("platform"), &s
))
282 wxStringTokenizer
tkn(s
, wxT(" |"));
284 while (tkn
.HasMoreTokens())
286 s
= tkn
.GetNextToken();
288 if (s
== wxT("win")) isok
= true;
290 #if defined(__MAC__) || defined(__APPLE__)
291 if (s
== wxT("mac")) isok
= true;
292 #elif defined(__UNIX__)
293 if (s
== wxT("unix")) isok
= true;
296 if (s
== wxT("os2")) isok
= true;
306 ProcessPlatformProperty(c
);
311 wxXmlNode
*c2
= c
->GetNext();
312 node
->RemoveChild(c
);
321 bool wxXmlResource::UpdateResources()
325 # if wxUSE_FILESYSTEM
326 wxFSFile
*file
= NULL
;
331 wxString
encoding(wxT("UTF-8"));
332 #if !wxUSE_UNICODE && wxUSE_INTL
333 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
335 // In case we are not using wxLocale to translate strings, convert the
336 // strings GUI's charset. This must not be done when wxXRC_USE_LOCALE
337 // is on, because it could break wxGetTranslation lookup.
338 encoding
= wxLocale::GetSystemEncodingName();
342 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
344 modif
= (m_data
[i
].Doc
== NULL
);
346 if (!modif
&& !(m_flags
& wxXRC_NO_RELOADING
))
348 # if wxUSE_FILESYSTEM
349 file
= fsys
.OpenFile(m_data
[i
].File
);
350 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
353 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
359 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
365 wxLogTrace(_T("xrc"),
366 _T("opening file '%s'"), m_data
[i
].File
.c_str());
368 wxInputStream
*stream
= NULL
;
370 # if wxUSE_FILESYSTEM
371 file
= fsys
.OpenFile(m_data
[i
].File
);
373 stream
= file
->GetStream();
375 stream
= new wxFileInputStream(m_data
[i
].File
);
380 delete m_data
[i
].Doc
;
381 m_data
[i
].Doc
= new wxXmlDocument
;
383 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
385 wxLogError(_("Cannot load resources from file '%s'."),
386 m_data
[i
].File
.c_str());
387 wxDELETE(m_data
[i
].Doc
);
390 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
392 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
393 wxDELETE(m_data
[i
].Doc
);
400 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
401 wxT("version"), wxT("0.0.0.0"));
402 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
403 &v1
, &v2
, &v3
, &v4
) == 4)
404 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
409 if (m_version
!= version
)
411 wxLogError(_("Resource files must have same version number!"));
415 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
417 m_data
[i
].Time
= file
->GetModificationTime();
419 m_data
[i
].Time
= wxDateTime(wxFileModificationTime(m_data
[i
].File
));
423 # if wxUSE_FILESYSTEM
436 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
437 const wxString
& name
,
438 const wxString
& classname
,
444 // first search for match at the top-level nodes (as this is
445 // where the resource is most commonly looked for):
446 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
448 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
449 (node
->GetName() == wxT("object") ||
450 node
->GetName() == wxT("object_ref")) &&
451 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
453 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
454 if (!classname
|| cls
== classname
)
456 // object_ref may not have 'class' property:
457 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
459 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
462 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
464 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
473 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
475 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
476 (node
->GetName() == wxT("object") ||
477 node
->GetName() == wxT("object_ref")) )
479 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, true);
488 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
489 const wxString
& classname
,
492 UpdateResources(); //ensure everything is up-to-date
495 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
497 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
500 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
501 name
, classname
, recursive
);
505 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
511 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
512 name
.c_str(), classname
.c_str());
516 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
519 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
521 wxXmlProperty
*dprop
;
522 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
525 if ( dprop
->GetName() == prop
->GetName() )
527 dprop
->SetValue(prop
->GetValue());
533 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
536 // Merge child nodes:
537 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
539 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
542 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
544 if ( dnode
->GetName() == node
->GetName() &&
545 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
546 dnode
->GetType() == node
->GetType() )
548 MergeNodes(*dnode
, *node
);
554 dest
.AddChild(new wxXmlNode(*node
));
557 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
558 dest
.SetContent(with
.GetContent());
561 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
563 wxXmlResourceHandler
*handlerToUse
)
565 if (node
== NULL
) return NULL
;
567 // handling of referenced resource
568 if ( node
->GetName() == wxT("object_ref") )
570 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
571 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, true);
575 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
580 wxXmlNode
copy(*refNode
);
581 MergeNodes(copy
, *node
);
583 return CreateResFromNode(©
, parent
, instance
);
586 wxXmlResourceHandler
*handler
;
590 if (handlerToUse
->CanHandle(node
))
592 return handlerToUse
->CreateResource(node
, parent
, instance
);
595 else if (node
->GetName() == wxT("object"))
597 wxList::compatibility_iterator ND
= m_handlers
.GetFirst();
600 handler
= (wxXmlResourceHandler
*)ND
->GetData();
601 if (handler
->CanHandle(node
))
603 return handler
->CreateResource(node
, parent
, instance
);
609 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
610 node
->GetName().c_str(),
611 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
616 #include "wx/listimpl.cpp"
617 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
618 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
620 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
622 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
624 if (!ms_subclassFactories
)
626 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
628 ms_subclassFactories
->Append(factory
);
631 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
634 ~wxXmlSubclassFactoryCXX() {}
636 wxObject
*Create(const wxString
& className
)
638 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
641 return classInfo
->CreateObject();
651 wxXmlResourceHandler::wxXmlResourceHandler()
652 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
653 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
658 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
660 wxXmlNode
*myNode
= m_node
;
661 wxString myClass
= m_class
;
662 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
663 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
665 m_instance
= instance
;
666 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
667 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
669 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
670 if (!subclass
.empty())
672 for (wxXmlSubclassFactoriesList::compatibility_iterator i
= wxXmlResource::ms_subclassFactories
->GetFirst();
675 m_instance
= i
->GetData()->Create(subclass
);
682 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
683 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
684 subclass
.c_str(), name
.c_str());
690 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
692 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
693 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
695 wxObject
*returned
= DoCreateResource();
699 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
700 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
706 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
708 m_styleNames
.Add(name
);
709 m_styleValues
.Add(value
);
714 void wxXmlResourceHandler::AddWindowStyles()
716 XRC_ADD_STYLE(wxCLIP_CHILDREN
);
717 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
718 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
719 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
720 XRC_ADD_STYLE(wxRAISED_BORDER
);
721 XRC_ADD_STYLE(wxSTATIC_BORDER
);
722 XRC_ADD_STYLE(wxNO_BORDER
);
723 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
724 XRC_ADD_STYLE(wxWANTS_CHARS
);
725 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
726 XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE
);
727 XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS
);
732 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
734 return (GetParamNode(param
) != NULL
);
738 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
740 wxString s
= GetParamValue(param
);
742 if (!s
) return defaults
;
744 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
748 while (tkn
.HasMoreTokens())
750 fl
= tkn
.GetNextToken();
751 index
= m_styleNames
.Index(fl
);
752 if (index
!= wxNOT_FOUND
)
753 style
|= m_styleValues
[index
];
755 wxLogError(_("Unknown style flag ") + fl
);
762 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
764 wxXmlNode
*parNode
= GetParamNode(param
);
765 wxString
str1(GetNodeContent(parNode
));
770 // VS: First version of XRC resources used $ instead of & (which is
771 // illegal in XML), but later I realized that '_' fits this purpose
772 // much better (because &File means "File with F underlined").
773 if (m_resource
->CompareVersion(2,3,0,1) < 0)
778 for (dt
= str1
.c_str(); *dt
; dt
++)
780 // Remap amp_char to &, map double amp_char to amp_char (for things
781 // like "&File..." -- this is illegal in XML, so we use "_File..."):
784 if ( *(++dt
) == amp_char
)
787 str2
<< wxT('&') << *dt
;
789 // Remap \n to CR, \r to LF, \t to TAB:
790 else if (*dt
== wxT('\\'))
793 case wxT('n') : str2
<< wxT('\n'); break;
794 case wxT('t') : str2
<< wxT('\t'); break;
795 case wxT('r') : str2
<< wxT('\r'); break;
796 default : str2
<< wxT('\\') << *dt
; break;
801 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
803 if (translate
&& parNode
&&
804 parNode
->GetPropVal(wxT("translate"), wxEmptyString
) != wxT("0"))
806 return wxGetTranslation(str2
);
813 // The string is internally stored as UTF-8, we have to convert
814 // it into system's default encoding so that it can be displayed:
815 return wxString(str2
.mb_str(wxConvUTF8
), wxConvLocal
);
821 // If wxXRC_USE_LOCALE is not set, then the string is already in
822 // system's default encoding in ANSI build, so we don't have to
823 // do anything special here.
830 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
833 wxString str1
= GetParamValue(param
);
835 if (!str1
.ToLong(&value
))
843 int wxXmlResourceHandler::GetID()
845 return wxXmlResource::GetXRCID(GetName());
850 wxString
wxXmlResourceHandler::GetName()
852 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
857 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
859 wxString v
= GetParamValue(param
);
861 if (!v
) return defaultv
;
862 else return (v
== wxT("1"));
867 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
869 wxString v
= GetParamValue(param
);
870 unsigned long tmp
= 0;
872 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
873 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
875 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
876 v
.c_str(), param
.c_str());
880 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
881 (unsigned char) ((tmp
& 0x00FF00) >> 8),
882 (unsigned char) ((tmp
& 0x0000FF)));
887 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
888 const wxArtClient
& defaultArtClient
,
891 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
892 wxXmlNode
*bmpNode
= GetParamNode(param
);
895 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
898 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), wxEmptyString
);
900 scl
= defaultArtClient
;
902 scl
= wxART_MAKE_CLIENT_ID_FROM_STR(scl
);
905 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
912 /* ...or load the bitmap from file: */
913 wxString name
= GetParamValue(param
);
914 if (name
.IsEmpty()) return wxNullBitmap
;
916 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
919 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
923 wxImage
img(*(fsfile
->GetStream()));
926 wxImage
img(GetParamValue(wxT("bitmap")));
931 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
934 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
935 return wxBitmap(img
);
941 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
942 const wxArtClient
& defaultArtClient
,
946 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
952 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
954 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
956 wxXmlNode
*n
= m_node
->GetChildren();
960 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
968 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
971 if (n
== NULL
) return wxEmptyString
;
972 n
= n
->GetChildren();
976 if (n
->GetType() == wxXML_TEXT_NODE
||
977 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
978 return n
->GetContent();
981 return wxEmptyString
;
986 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
989 return GetNodeContent(m_node
);
991 return GetNodeContent(GetParamNode(param
));
996 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
998 wxString s
= GetParamValue(param
);
999 if (s
.IsEmpty()) s
= wxT("-1,-1");
1003 is_dlg
= s
[s
.Length()-1] == wxT('d');
1004 if (is_dlg
) s
.RemoveLast();
1006 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
1007 !s
.AfterLast(wxT(',')).ToLong(&sy
))
1009 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
1010 return wxDefaultSize
;
1015 if (m_instanceAsWindow
)
1016 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
1017 else if (m_parentAsWindow
)
1018 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
1021 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1022 return wxDefaultSize
;
1025 else return wxSize(sx
, sy
);
1030 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1032 wxSize sz
= GetSize(param
);
1033 return wxPoint(sz
.x
, sz
.y
);
1038 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
1040 wxString s
= GetParamValue(param
);
1041 if (s
.IsEmpty()) return defaultv
;
1045 is_dlg
= s
[s
.Length()-1] == wxT('d');
1046 if (is_dlg
) s
.RemoveLast();
1050 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1056 if (m_instanceAsWindow
)
1057 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1058 else if (m_parentAsWindow
)
1059 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1062 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1071 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1073 wxXmlNode
*font_node
= GetParamNode(param
);
1074 if (font_node
== NULL
)
1076 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1080 wxXmlNode
*oldnode
= m_node
;
1083 long size
= GetLong(wxT("size"), 12);
1085 wxString style
= GetParamValue(wxT("style"));
1086 wxString weight
= GetParamValue(wxT("weight"));
1087 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1088 if (style
== wxT("italic")) istyle
= wxITALIC
;
1089 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1090 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1091 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1093 wxString family
= GetParamValue(wxT("family"));
1094 int ifamily
= wxDEFAULT
;
1095 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1096 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1097 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1098 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1099 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1101 bool underlined
= GetBool(wxT("underlined"), false);
1103 wxString encoding
= GetParamValue(wxT("encoding"));
1104 wxFontMapper mapper
;
1105 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1106 if (!encoding
.IsEmpty())
1107 enc
= mapper
.CharsetToEncoding(encoding
);
1108 if (enc
== wxFONTENCODING_SYSTEM
)
1109 enc
= wxFONTENCODING_DEFAULT
;
1111 wxString faces
= GetParamValue(wxT("face"));
1112 wxString facename
= wxEmptyString
;
1113 wxFontEnumerator enu
;
1114 enu
.EnumerateFacenames();
1115 wxStringTokenizer
tk(faces
, wxT(","));
1116 while (tk
.HasMoreTokens())
1118 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), false);
1119 if (index
!= wxNOT_FOUND
)
1121 facename
= (*enu
.GetFacenames())[index
];
1128 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1133 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1135 //FIXME : add cursor
1137 if (HasParam(wxT("exstyle")))
1138 // Have to OR it with existing style, since
1139 // some implementations (e.g. wxGTK) use the extra style
1141 wnd
->SetExtraStyle(wnd
->GetExtraStyle() | GetStyle(wxT("exstyle")));
1142 if (HasParam(wxT("bg")))
1143 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1144 if (HasParam(wxT("fg")))
1145 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1146 if (GetBool(wxT("enabled"), 1) == 0)
1148 if (GetBool(wxT("focused"), 0) == 1)
1150 if (GetBool(wxT("hidden"), 0) == 1)
1153 if (HasParam(wxT("tooltip")))
1154 wnd
->SetToolTip(GetText(wxT("tooltip")));
1156 if (HasParam(wxT("font")))
1157 wnd
->SetFont(GetFont());
1161 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1163 wxXmlNode
*n
= m_node
->GetChildren();
1167 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1168 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1170 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1171 this_hnd_only
? this : NULL
);
1178 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1181 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1182 wxXmlNode
*n
= root
->GetChildren();
1186 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1188 CreateResource(n
, parent
, NULL
);
1200 // --------------- XRCID implementation -----------------------------
1202 #define XRCID_TABLE_SIZE 1024
1212 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1214 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1216 static int XRCID_LastID
= wxID_HIGHEST
;
1220 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1221 index
%= XRCID_TABLE_SIZE
;
1223 XRCID_record
*oldrec
= NULL
;
1224 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1226 if (wxStrcmp(rec
->key
, str_id
) == 0)
1233 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1234 &XRCID_Records
[index
] : &oldrec
->next
;
1235 *rec_var
= new XRCID_record
;
1236 (*rec_var
)->key
= wxStrdup(str_id
);
1237 (*rec_var
)->next
= NULL
;
1240 if (value_if_not_found
!= -2)
1241 (*rec_var
)->id
= value_if_not_found
;
1244 int asint
= wxStrtol(str_id
, &end
, 10);
1245 if (*str_id
&& *end
== 0)
1247 // if str_id was integer, keep it verbosely:
1248 (*rec_var
)->id
= asint
;
1252 (*rec_var
)->id
= ++XRCID_LastID
;
1256 return (*rec_var
)->id
;
1259 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1261 return XRCID_Lookup(str_id
);
1265 static void CleanXRCID_Record(XRCID_record
*rec
)
1269 CleanXRCID_Record(rec
->next
);
1275 static void CleanXRCID_Records()
1277 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1279 CleanXRCID_Record(XRCID_Records
[i
]);
1280 XRCID_Records
[i
] = NULL
;
1284 static void AddStdXRCID_Records()
1286 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1290 stdID(wxID_SEPARATOR
);
1303 stdID(wxID_PRINT_SETUP
);
1304 stdID(wxID_PREVIEW
);
1306 stdID(wxID_HELP_CONTENTS
);
1307 stdID(wxID_HELP_COMMANDS
);
1308 stdID(wxID_HELP_PROCEDURES
);
1309 stdID(wxID_HELP_CONTEXT
);
1310 stdID(wxID_CLOSE_ALL
);
1311 stdID(wxID_PREFERENCES
);
1317 stdID(wxID_DUPLICATE
);
1318 stdID(wxID_SELECTALL
);
1320 stdID(wxID_REPLACE
);
1321 stdID(wxID_REPLACE_ALL
);
1322 stdID(wxID_PROPERTIES
);
1323 stdID(wxID_VIEW_DETAILS
);
1324 stdID(wxID_VIEW_LARGEICONS
);
1325 stdID(wxID_VIEW_SMALLICONS
);
1326 stdID(wxID_VIEW_LIST
);
1327 stdID(wxID_VIEW_SORTDATE
);
1328 stdID(wxID_VIEW_SORTNAME
);
1329 stdID(wxID_VIEW_SORTSIZE
);
1330 stdID(wxID_VIEW_SORTTYPE
);
1346 stdID(wxID_FORWARD
);
1347 stdID(wxID_BACKWARD
);
1348 stdID(wxID_DEFAULT
);
1352 stdID(wxID_CONTEXT_HELP
);
1353 stdID(wxID_YESTOALL
);
1354 stdID(wxID_NOTOALL
);
1363 stdID(wxID_REFRESH
);
1368 stdID(wxID_JUSTIFY_CENTER
);
1369 stdID(wxID_JUSTIFY_FILL
);
1370 stdID(wxID_JUSTIFY_RIGHT
);
1371 stdID(wxID_JUSTIFY_LEFT
);
1372 stdID(wxID_UNDERLINE
);
1374 stdID(wxID_UNINDENT
);
1375 stdID(wxID_ZOOM_100
);
1376 stdID(wxID_ZOOM_FIT
);
1377 stdID(wxID_ZOOM_IN
);
1378 stdID(wxID_ZOOM_OUT
);
1379 stdID(wxID_UNDELETE
);
1380 stdID(wxID_REVERT_TO_SAVED
);
1381 stdID(wxID_SYSTEM_MENU
);
1382 stdID(wxID_CLOSE_FRAME
);
1383 stdID(wxID_MOVE_FRAME
);
1384 stdID(wxID_RESIZE_FRAME
);
1385 stdID(wxID_MAXIMIZE_FRAME
);
1386 stdID(wxID_ICONIZE_FRAME
);
1387 stdID(wxID_RESTORE_FRAME
);
1396 // --------------- module and globals -----------------------------
1398 class wxXmlResourceModule
: public wxModule
1400 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1402 wxXmlResourceModule() {}
1405 AddStdXRCID_Records();
1406 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1411 delete wxXmlResource::Set(NULL
);
1412 if(wxXmlResource::ms_subclassFactories
)
1413 WX_CLEAR_LIST(wxXmlSubclassFactoriesList
, *wxXmlResource::ms_subclassFactories
);
1414 wxDELETE(wxXmlResource::ms_subclassFactories
);
1415 CleanXRCID_Records();
1419 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1422 // When wxXml is loaded dynamically after the application is already running
1423 // then the built-in module system won't pick this one up. Add it manually.
1424 void wxXmlInitResourceModule()
1426 wxModule
* module = new wxXmlResourceModule
;
1428 wxModule::RegisterModule(module);