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/xrc/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
)
63 m_handlers
.DeleteContents(TRUE
);
68 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
72 m_handlers
.DeleteContents(TRUE
);
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 wxString
url(wxFileSystem::FileNameToURL(fnd
));
128 rt
= rt
&& Load(url
+ wxT("#zip:*.xrc"));
133 drec
= new wxXmlResourceDataRecord
;
143 # undef wxXmlFindFirst
144 # undef wxXmlFindNext
149 IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler
, wxObject
)
151 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
153 m_handlers
.Append(handler
);
154 handler
->SetParentResource(this);
157 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
159 m_handlers
.Insert(handler
);
160 handler
->SetParentResource(this);
165 void wxXmlResource::ClearHandlers()
171 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
173 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
178 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
180 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
186 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
188 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
193 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
195 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
198 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
200 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
205 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
207 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
210 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
212 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
215 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
217 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
220 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
222 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
225 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
227 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
228 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
231 if (bmp
) { rt
= *bmp
; delete bmp
; }
235 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
237 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
238 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
241 if (icon
) { rt
= *icon
; delete icon
; }
246 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
248 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
251 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
253 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
257 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
258 wxWindow
*control
, wxWindow
*parent
)
261 parent
= control
->GetParent();
262 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
265 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
268 return control
->Reparent(container
);
272 static void ProcessPlatformProperty(wxXmlNode
*node
)
277 wxXmlNode
*c
= node
->GetChildren();
281 if (!c
->GetPropVal(wxT("platform"), &s
))
285 wxStringTokenizer
tkn(s
, wxT(" |"));
287 while (tkn
.HasMoreTokens())
289 s
= tkn
.GetNextToken();
291 if (s
== wxT("win")) isok
= true;
294 if (s
== wxT("unix")) isok
= true;
297 if (s
== wxT("mac")) isok
= true;
300 if (s
== wxT("os2")) isok
= true;
310 ProcessPlatformProperty(c
);
315 wxXmlNode
*c2
= c
->GetNext();
316 node
->RemoveChild(c
);
325 void wxXmlResource::UpdateResources()
328 # if wxUSE_FILESYSTEM
329 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
);
350 # if wxUSE_FILESYSTEM
351 file
= fsys
.OpenFile(m_data
[i
].File
);
352 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
354 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
357 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
363 wxInputStream
*stream
= NULL
;
365 # if wxUSE_FILESYSTEM
366 file
= fsys
.OpenFile(m_data
[i
].File
);
368 stream
= file
->GetStream();
370 stream
= new wxFileInputStream(m_data
[i
].File
);
375 delete m_data
[i
].Doc
;
376 m_data
[i
].Doc
= new wxXmlDocument
;
378 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
380 wxLogError(_("Cannot load resources from file '%s'."),
381 m_data
[i
].File
.c_str());
382 wxDELETE(m_data
[i
].Doc
);
384 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
386 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
387 wxDELETE(m_data
[i
].Doc
);
393 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
394 wxT("version"), wxT("0.0.0.0"));
395 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
396 &v1
, &v2
, &v3
, &v4
) == 4)
397 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
402 if (m_version
!= version
)
403 wxLogError(_("Resource files must have same version number!"));
405 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
406 m_data
[i
].Time
= file
->GetModificationTime();
409 # if wxUSE_FILESYSTEM
419 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
420 const wxString
& name
,
421 const wxString
& classname
,
427 // first search for match at the top-level nodes (as this is
428 // where the resource is most commonly looked for):
429 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
431 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
432 (node
->GetName() == wxT("object") ||
433 node
->GetName() == wxT("object_ref")) &&
434 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
436 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
437 if (!classname
|| cls
== classname
)
439 // object_ref may not have 'class' property:
440 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
442 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
445 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
447 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
456 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
458 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
459 (node
->GetName() == wxT("object") ||
460 node
->GetName() == wxT("object_ref")) )
462 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
471 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
472 const wxString
& classname
,
475 UpdateResources(); //ensure everything is up-to-date
478 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
480 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
483 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
484 name
, classname
, recursive
);
488 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
494 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
495 name
.c_str(), classname
.c_str());
499 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
502 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
504 wxXmlProperty
*dprop
;
505 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
508 if ( dprop
->GetName() == prop
->GetName() )
510 dprop
->SetValue(prop
->GetValue());
516 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
519 // Merge child nodes:
520 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
522 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
525 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
527 if ( dnode
->GetName() == node
->GetName() &&
528 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
529 dnode
->GetType() == node
->GetType() )
531 MergeNodes(*dnode
, *node
);
537 dest
.AddChild(new wxXmlNode(*node
));
540 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
541 dest
.SetContent(with
.GetContent());
544 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
546 wxXmlResourceHandler
*handlerToUse
)
548 if (node
== NULL
) return NULL
;
550 // handling of referenced resource
551 if ( node
->GetName() == wxT("object_ref") )
553 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
554 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
558 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
563 wxXmlNode
copy(*refNode
);
564 MergeNodes(copy
, *node
);
566 return CreateResFromNode(©
, parent
, instance
);
569 wxXmlResourceHandler
*handler
;
573 if (handlerToUse
->CanHandle(node
))
575 return handlerToUse
->CreateResource(node
, parent
, instance
);
578 else if (node
->GetName() == wxT("object"))
580 wxNode
*ND
= m_handlers
.GetFirst();
583 handler
= (wxXmlResourceHandler
*)ND
->GetData();
584 if (handler
->CanHandle(node
))
586 return handler
->CreateResource(node
, parent
, instance
);
592 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
593 node
->GetName().c_str(),
594 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
599 #include "wx/listimpl.cpp"
600 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
601 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
603 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
605 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
607 if (!ms_subclassFactories
)
609 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
610 ms_subclassFactories
->DeleteContents(TRUE
);
612 ms_subclassFactories
->Append(factory
);
615 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
618 ~wxXmlSubclassFactoryCXX() {}
620 wxObject
*Create(const wxString
& className
)
622 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
625 return classInfo
->CreateObject();
635 wxXmlResourceHandler::wxXmlResourceHandler()
636 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
637 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
642 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
644 wxXmlNode
*myNode
= m_node
;
645 wxString myClass
= m_class
;
646 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
647 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
649 m_instance
= instance
;
650 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
651 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
653 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
654 if (!subclass
.empty())
656 for (wxXmlSubclassFactoriesList::Node
*i
= wxXmlResource::ms_subclassFactories
->GetFirst();
659 m_instance
= i
->GetData()->Create(subclass
);
666 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
667 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
668 subclass
.c_str(), name
.c_str());
674 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
676 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
677 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
679 wxObject
*returned
= DoCreateResource();
683 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
684 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
690 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
692 m_styleNames
.Add(name
);
693 m_styleValues
.Add(value
);
698 void wxXmlResourceHandler::AddWindowStyles()
700 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
701 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
702 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
703 XRC_ADD_STYLE(wxRAISED_BORDER
);
704 XRC_ADD_STYLE(wxSTATIC_BORDER
);
705 XRC_ADD_STYLE(wxNO_BORDER
);
706 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
707 XRC_ADD_STYLE(wxWANTS_CHARS
);
708 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
713 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
715 return (GetParamNode(param
) != NULL
);
719 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
721 wxString s
= GetParamValue(param
);
723 if (!s
) return defaults
;
725 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
729 while (tkn
.HasMoreTokens())
731 fl
= tkn
.GetNextToken();
732 index
= m_styleNames
.Index(fl
);
733 if (index
!= wxNOT_FOUND
)
734 style
|= m_styleValues
[index
];
736 wxLogError(_("Unknown style flag ") + fl
);
743 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
745 wxString
str1(GetParamValue(param
));
750 // VS: First version of XRC resources used $ instead of & (which is
751 // illegal in XML), but later I realized that '_' fits this purpose
752 // much better (because &File means "File with F underlined").
753 if (m_resource
->CompareVersion(2,3,0,1) < 0)
758 for (dt
= str1
.c_str(); *dt
; dt
++)
760 // Remap amp_char to &, map double amp_char to amp_char (for things
761 // like "&File..." -- this is illegal in XML, so we use "_File..."):
764 if ( *(++dt
) == amp_char
)
767 str2
<< wxT('&') << *dt
;
769 // Remap \n to CR, \r to LF, \t to TAB:
770 else if (*dt
== wxT('\\'))
773 case wxT('n') : str2
<< wxT('\n'); break;
774 case wxT('t') : str2
<< wxT('\t'); break;
775 case wxT('r') : str2
<< wxT('\r'); break;
776 default : str2
<< wxT('\\') << *dt
; break;
781 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
782 return wxGetTranslation(str2
);
790 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
793 wxString str1
= GetParamValue(param
);
795 if (!str1
.ToLong(&value
))
803 int wxXmlResourceHandler::GetID()
805 return wxXmlResource::GetXRCID(GetName());
810 wxString
wxXmlResourceHandler::GetName()
812 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
817 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
819 wxString v
= GetParamValue(param
);
821 if (!v
) return defaultv
;
822 else return (v
== wxT("1"));
827 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
829 wxString v
= GetParamValue(param
);
830 unsigned long tmp
= 0;
832 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
833 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
835 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
836 v
.c_str(), param
.c_str());
840 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
841 (unsigned char) ((tmp
& 0x00FF00) >> 8),
842 (unsigned char) ((tmp
& 0x0000FF)));
847 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
848 const wxArtClient
& defaultArtClient
,
851 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
852 wxXmlNode
*bmpNode
= GetParamNode(param
);
855 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
858 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
860 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
861 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
868 /* ...or load the bitmap from file: */
869 wxString name
= GetParamValue(param
);
870 if (name
.IsEmpty()) return wxNullBitmap
;
872 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
875 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."),
879 wxImage
img(*(fsfile
->GetStream()));
882 wxImage
img(GetParamValue(wxT("bitmap")));
887 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
890 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
891 return wxBitmap(img
);
897 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
898 const wxArtClient
& defaultArtClient
,
902 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
908 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
910 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
912 wxXmlNode
*n
= m_node
->GetChildren();
916 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
924 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
927 if (n
== NULL
) return wxEmptyString
;
928 n
= n
->GetChildren();
932 if (n
->GetType() == wxXML_TEXT_NODE
||
933 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
934 return n
->GetContent();
937 return wxEmptyString
;
942 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
945 return GetNodeContent(m_node
);
947 return GetNodeContent(GetParamNode(param
));
952 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
954 wxString s
= GetParamValue(param
);
955 if (s
.IsEmpty()) s
= wxT("-1,-1");
959 is_dlg
= s
[s
.Length()-1] == wxT('d');
960 if (is_dlg
) s
.RemoveLast();
962 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
963 !s
.AfterLast(wxT(',')).ToLong(&sy
))
965 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
966 return wxDefaultSize
;
971 if (m_instanceAsWindow
)
972 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
973 else if (m_parentAsWindow
)
974 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
977 wxLogError(_("Cannot convert dialog units: dialog unknown."));
978 return wxDefaultSize
;
981 else return wxSize(sx
, sy
);
986 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
988 wxSize sz
= GetSize(param
);
989 return wxPoint(sz
.x
, sz
.y
);
994 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
996 wxString s
= GetParamValue(param
);
997 if (s
.IsEmpty()) return defaultv
;
1001 is_dlg
= s
[s
.Length()-1] == wxT('d');
1002 if (is_dlg
) s
.RemoveLast();
1006 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1012 if (m_instanceAsWindow
)
1013 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1014 else if (m_parentAsWindow
)
1015 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1018 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1027 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1029 wxXmlNode
*font_node
= GetParamNode(param
);
1030 if (font_node
== NULL
)
1032 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1036 wxXmlNode
*oldnode
= m_node
;
1039 long size
= GetLong(wxT("size"), 12);
1041 wxString style
= GetParamValue(wxT("style"));
1042 wxString weight
= GetParamValue(wxT("weight"));
1043 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1044 if (style
== wxT("italic")) istyle
= wxITALIC
;
1045 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1046 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1047 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1049 wxString family
= GetParamValue(wxT("family"));
1050 int ifamily
= wxDEFAULT
;
1051 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1052 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1053 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1054 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1055 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1057 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1059 wxString encoding
= GetParamValue(wxT("encoding"));
1060 wxFontMapper mapper
;
1061 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1062 if (!encoding
.IsEmpty())
1063 enc
= mapper
.CharsetToEncoding(encoding
);
1064 if (enc
== wxFONTENCODING_SYSTEM
)
1065 enc
= wxFONTENCODING_DEFAULT
;
1067 wxString faces
= GetParamValue(wxT("face"));
1068 wxString facename
= wxEmptyString
;
1069 wxFontEnumerator enu
;
1070 enu
.EnumerateFacenames();
1071 wxStringTokenizer
tk(faces
, wxT(","));
1072 while (tk
.HasMoreTokens())
1074 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1075 if (index
!= wxNOT_FOUND
)
1077 facename
= (*enu
.GetFacenames())[index
];
1084 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1089 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1091 //FIXME : add cursor
1093 if (HasParam(wxT("exstyle")))
1094 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1095 if (HasParam(wxT("bg")))
1096 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1097 if (HasParam(wxT("fg")))
1098 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1099 if (GetBool(wxT("enabled"), 1) == 0)
1101 if (GetBool(wxT("focused"), 0) == 1)
1103 if (GetBool(wxT("hidden"), 0) == 1)
1106 if (HasParam(wxT("tooltip")))
1107 wnd
->SetToolTip(GetText(wxT("tooltip")));
1109 if (HasParam(wxT("font")))
1110 wnd
->SetFont(GetFont());
1114 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1116 wxXmlNode
*n
= m_node
->GetChildren();
1120 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1121 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1123 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1124 this_hnd_only
? this : NULL
);
1131 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1134 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1135 wxXmlNode
*n
= root
->GetChildren();
1139 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1141 CreateResource(n
, parent
, NULL
);
1153 // --------------- XRCID implementation -----------------------------
1155 #define XRCID_TABLE_SIZE 1024
1165 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1167 static int XRCID_Lookup(const wxChar
*str_id
, int value_if_not_found
= -2)
1169 static int XRCID_LastID
= wxID_HIGHEST
;
1173 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1174 index
%= XRCID_TABLE_SIZE
;
1176 XRCID_record
*oldrec
= NULL
;
1178 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1180 if (wxStrcmp(rec
->key
, str_id
) == 0)
1188 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1189 &XRCID_Records
[index
] : &oldrec
->next
;
1190 *rec_var
= new XRCID_record
;
1191 (*rec_var
)->key
= wxStrdup(str_id
);
1192 (*rec_var
)->next
= NULL
;
1195 if (value_if_not_found
!= -2)
1196 (*rec_var
)->id
= value_if_not_found
;
1199 int asint
= wxStrtol(str_id
, &end
, 10);
1200 if (*str_id
&& *end
== 0)
1202 // if str_id was integer, keep it verbosely:
1203 (*rec_var
)->id
= asint
;
1207 (*rec_var
)->id
= ++XRCID_LastID
;
1211 return (*rec_var
)->id
;
1214 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1216 return XRCID_Lookup(str_id
);
1220 static void CleanXRCID_Record(XRCID_record
*rec
)
1224 CleanXRCID_Record(rec
->next
);
1230 static void CleanXRCID_Records()
1232 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1234 CleanXRCID_Record(XRCID_Records
[i
]);
1235 XRCID_Records
[i
] = NULL
;
1239 static void AddStdXRCID_Records()
1241 #define stdID(id) XRCID_Lookup(wxT(#id), id)
1243 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
1244 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
1245 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
1246 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
1247 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
1248 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
1249 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
1250 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
1251 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
1252 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
1253 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
1254 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
1255 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
1256 stdID(wxID_CLOSE_ALL
);
1264 // --------------- module and globals -----------------------------
1266 class wxXmlResourceModule
: public wxModule
1268 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1270 wxXmlResourceModule() {}
1273 AddStdXRCID_Records();
1274 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1279 delete wxXmlResource::Set(NULL
);
1280 wxDELETE(wxXmlResource::ms_subclassFactories
);
1281 CleanXRCID_Records();
1285 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1288 // When wxXml is loaded dynamically after the application is already running
1289 // then the built-in module system won't pick this one up. Add it manually.
1290 void wxXmlInitResourceModule()
1292 wxModule
* module = new wxXmlResourceModule
;
1294 wxModule::RegisterModule(module);