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"
29 #include "wx/tokenzr.h"
30 #include "wx/fontenum.h"
31 #include "wx/module.h"
32 #include "wx/bitmap.h"
34 #include "wx/fontmap.h"
35 #include "wx/artprov.h"
37 #include "wx/xrc/xml.h"
38 #include "wx/xrc/xmlres.h"
40 #include "wx/arrimpl.cpp"
41 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
44 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
46 /*static*/ wxXmlResource
*wxXmlResource::Get()
49 ms_instance
= new wxXmlResource
;
53 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
55 wxXmlResource
*old
= ms_instance
;
60 wxXmlResource::wxXmlResource(int flags
)
62 m_handlers
.DeleteContents(TRUE
);
67 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
71 m_handlers
.DeleteContents(TRUE
);
75 wxXmlResource::~wxXmlResource()
81 bool wxXmlResource::Load(const wxString
& filemask
)
84 wxXmlResourceDataRecord
*drec
;
85 bool iswild
= wxIsWild(filemask
);
90 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
91 # define wxXmlFindNext fsys.FindNext()
93 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
94 # define wxXmlFindNext wxFindNextFile()
103 if (filemask
.Lower().Matches(wxT("*.zip")) ||
104 filemask
.Lower().Matches(wxT("*.xrs")))
106 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xmlbin"));
107 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
112 drec
= new wxXmlResourceDataRecord
;
122 # undef wxXmlFindFirst
123 # undef wxXmlFindNext
129 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
131 m_handlers
.Append(handler
);
132 handler
->SetParentResource(this);
135 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
137 m_handlers
.Insert(handler
);
138 handler
->SetParentResource(this);
143 void wxXmlResource::ClearHandlers()
149 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
151 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
156 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
158 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
164 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
166 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
171 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
173 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
176 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
178 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
183 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
185 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
188 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
190 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
193 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
195 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
198 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
200 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
203 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
205 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
206 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
209 if (bmp
) { rt
= *bmp
; delete bmp
; }
213 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
215 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
216 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
219 if (icon
) { rt
= *icon
; delete icon
; }
224 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
226 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
229 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
231 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
235 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
236 wxWindow
*control
, wxWindow
*parent
)
239 parent
= control
->GetParent();
240 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
243 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
246 return control
->Reparent(container
);
250 static void ProcessPlatformProperty(wxXmlNode
*node
)
255 wxXmlNode
*c
= node
->GetChildren();
259 if (!c
->GetPropVal(wxT("platform"), &s
))
263 wxStringTokenizer
tkn(s
, wxT(" |"));
265 while (tkn
.HasMoreTokens())
267 s
= tkn
.GetNextToken();
270 s
== wxString(wxT("win"))
271 #elif defined(__UNIX__)
272 s
== wxString(wxT("unix"))
273 #elif defined(__MAC__)
274 s
== wxString(wxT("mac"))
275 #elif defined(__OS2__)
276 s
== wxString(wxT("os2"))
286 ProcessPlatformProperty(c
);
291 wxXmlNode
*c2
= c
->GetNext();
292 node
->RemoveChild(c
);
301 void wxXmlResource::UpdateResources()
304 # if wxUSE_FILESYSTEM
305 wxFSFile
*file
= NULL
;
309 wxString
encoding(wxT("UTF-8"));
310 #if !wxUSE_UNICODE && wxUSE_INTL
311 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
313 // In case we are not using wxLocale to translate strings, convert the strings
314 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
315 // it could break wxGetTranslation lookup.
316 encoding
= wxLocale::GetSystemEncodingName();
320 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
322 modif
= (m_data
[i
].Doc
== NULL
);
326 # if wxUSE_FILESYSTEM
327 file
= fsys
.OpenFile(m_data
[i
].File
);
328 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
330 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
333 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
339 wxInputStream
*stream
= NULL
;
341 # if wxUSE_FILESYSTEM
342 file
= fsys
.OpenFile(m_data
[i
].File
);
344 stream
= file
->GetStream();
346 stream
= new wxFileInputStream(m_data
[i
].File
);
351 delete m_data
[i
].Doc
;
352 m_data
[i
].Doc
= new wxXmlDocument
;
354 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
356 wxLogError(_("Cannot load resources from file '%s'."),
357 m_data
[i
].File
.c_str());
358 wxDELETE(m_data
[i
].Doc
);
360 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
362 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
363 wxDELETE(m_data
[i
].Doc
);
369 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
370 wxT("version"), wxT("0.0.0.0"));
371 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
372 &v1
, &v2
, &v3
, &v4
) == 4)
373 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
378 if (m_version
!= version
)
379 wxLogError(_("Resource files must have same version number!"));
381 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
382 m_data
[i
].Time
= file
->GetModificationTime();
385 # if wxUSE_FILESYSTEM
395 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
396 const wxString
& name
,
397 const wxString
& classname
,
403 // first search for match at the top-level nodes (as this is
404 // where the resource is most commonly looked for):
405 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
407 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
408 (node
->GetName() == wxT("object") ||
409 node
->GetName() == wxT("object_ref")) &&
410 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
412 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
413 if (!classname
|| cls
== classname
)
415 // object_ref may not have 'class' property:
416 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
418 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
421 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
423 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
432 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
434 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
435 (node
->GetName() == wxT("object") ||
436 node
->GetName() == wxT("object_ref")) )
438 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
447 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
448 const wxString
& classname
,
451 UpdateResources(); //ensure everything is up-to-date
454 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
456 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
459 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
460 name
, classname
, recursive
);
464 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
470 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
471 name
.c_str(), classname
.c_str());
475 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
478 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
480 wxXmlProperty
*dprop
;
481 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
484 if ( dprop
->GetName() == prop
->GetName() )
486 dprop
->SetValue(prop
->GetValue());
492 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
495 // Merge child nodes:
496 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
498 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
501 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
503 if ( dnode
->GetName() == node
->GetName() &&
504 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
505 dnode
->GetType() == node
->GetType() )
507 MergeNodes(*dnode
, *node
);
513 dest
.AddChild(new wxXmlNode(*node
));
516 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
517 dest
.SetContent(with
.GetContent());
520 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
522 if (node
== NULL
) return NULL
;
524 // handling of referenced resource
525 if ( node
->GetName() == wxT("object_ref") )
527 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
528 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
532 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
537 wxXmlNode
copy(*refNode
);
538 MergeNodes(copy
, *node
);
540 return CreateResFromNode(©
, parent
, instance
);
543 wxXmlResourceHandler
*handler
;
545 wxNode
* ND
= m_handlers
.GetFirst();
548 handler
= (wxXmlResourceHandler
*)ND
->GetData();
549 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
551 ret
= handler
->CreateResource(node
, parent
, instance
);
557 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
558 node
->GetName().c_str(),
559 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
564 #include "wx/listimpl.cpp"
565 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
566 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
568 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
570 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
572 if (!ms_subclassFactories
)
574 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
575 ms_subclassFactories
->DeleteContents(TRUE
);
577 ms_subclassFactories
->Append(factory
);
580 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
583 ~wxXmlSubclassFactoryCXX() {}
585 wxObject
*Create(const wxString
& className
)
587 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
590 return classInfo
->CreateObject();
600 wxXmlResourceHandler::wxXmlResourceHandler()
601 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
602 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
607 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
609 wxXmlNode
*myNode
= m_node
;
610 wxString myClass
= m_class
;
611 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
612 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
614 m_instance
= instance
;
615 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
616 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
618 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
619 if (!subclass
.empty())
621 for (wxXmlSubclassFactoriesList::Node
*i
= wxXmlResource::ms_subclassFactories
->GetFirst();
624 m_instance
= i
->GetData()->Create(subclass
);
631 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
632 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
633 subclass
.c_str(), name
.c_str());
639 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
641 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
642 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
644 wxObject
*returned
= DoCreateResource();
648 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
649 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
655 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
657 m_styleNames
.Add(name
);
658 m_styleValues
.Add(value
);
663 void wxXmlResourceHandler::AddWindowStyles()
665 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
666 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
667 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
668 XRC_ADD_STYLE(wxRAISED_BORDER
);
669 XRC_ADD_STYLE(wxSTATIC_BORDER
);
670 XRC_ADD_STYLE(wxNO_BORDER
);
671 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
672 XRC_ADD_STYLE(wxWANTS_CHARS
);
673 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
678 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
680 return (GetParamNode(param
) != NULL
);
684 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
686 wxString s
= GetParamValue(param
);
688 if (!s
) return defaults
;
690 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
694 while (tkn
.HasMoreTokens())
696 fl
= tkn
.GetNextToken();
697 index
= m_styleNames
.Index(fl
);
698 if (index
!= wxNOT_FOUND
)
699 style
|= m_styleValues
[index
];
701 wxLogError(_("Unknown style flag ") + fl
);
708 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
710 wxString
str1(GetParamValue(param
));
715 // VS: First version of XRC resources used $ instead of & (which is
716 // illegal in XML), but later I realized that '_' fits this purpose
717 // much better (because &File means "File with F underlined").
718 if (m_resource
->CompareVersion(2,3,0,1) < 0)
723 for (dt
= str1
.c_str(); *dt
; dt
++)
725 // Remap amp_char to &, map double amp_char to amp_char (for things
726 // like "&File..." -- this is illegal in XML, so we use "_File..."):
729 if ( *(++dt
) == amp_char
)
732 str2
<< wxT('&') << *dt
;
734 // Remap \n to CR, \r to LF, \t to TAB:
735 else if (*dt
== wxT('\\'))
738 case wxT('n') : str2
<< wxT('\n'); break;
739 case wxT('t') : str2
<< wxT('\t'); break;
740 case wxT('r') : str2
<< wxT('\r'); break;
741 default : str2
<< wxT('\\') << *dt
; break;
746 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
747 return wxGetTranslation(str2
);
755 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
758 wxString str1
= GetParamValue(param
);
760 if (!str1
.ToLong(&value
))
768 int wxXmlResourceHandler::GetID()
770 wxString sid
= GetName();
773 if (sid
== wxT("-1")) return -1;
774 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
775 #define stdID(id) else if (sid == wxT(#id)) return id
776 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
777 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
778 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
779 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
780 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
781 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
782 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
783 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
784 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
785 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
786 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
787 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
788 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
789 stdID(wxID_CLOSE_ALL
);
791 else return wxXmlResource::GetXRCID(sid
);
796 wxString
wxXmlResourceHandler::GetName()
798 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
803 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
805 wxString v
= GetParamValue(param
);
807 if (!v
) return defaultv
;
808 else return (v
== wxT("1"));
813 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
815 wxString v
= GetParamValue(param
);
816 unsigned long tmp
= 0;
818 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
819 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
821 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
822 v
.c_str(), param
.c_str());
826 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
827 (unsigned char) ((tmp
& 0x00FF00) >> 8),
828 (unsigned char) ((tmp
& 0x0000FF)));
833 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
834 const wxArtClient
& defaultArtClient
,
837 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
838 wxXmlNode
*bmpNode
= GetParamNode(param
);
841 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
844 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
846 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
847 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
854 /* ...or load the bitmap from file: */
855 wxString name
= GetParamValue(param
);
856 if (name
.IsEmpty()) return wxNullBitmap
;
858 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
861 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
864 wxImage
img(*(fsfile
->GetStream()));
867 wxImage
img(GetParamValue(wxT("bitmap")));
872 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
875 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
876 return wxBitmap(img
);
882 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
883 const wxArtClient
& defaultArtClient
,
886 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
888 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
891 wxBitmap bmppt
= GetBitmap(param
, size
);
892 iconpt
= (wxIcon
*)(&bmppt
);
893 wxIcon
icon(*iconpt
);
900 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
902 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
904 wxXmlNode
*n
= m_node
->GetChildren();
908 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
916 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
919 if (n
== NULL
) return wxEmptyString
;
920 n
= n
->GetChildren();
924 if (n
->GetType() == wxXML_TEXT_NODE
||
925 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
926 return n
->GetContent();
929 return wxEmptyString
;
934 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
937 return GetNodeContent(m_node
);
939 return GetNodeContent(GetParamNode(param
));
944 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
946 wxString s
= GetParamValue(param
);
947 if (s
.IsEmpty()) s
= wxT("-1,-1");
951 is_dlg
= s
[s
.Length()-1] == wxT('d');
952 if (is_dlg
) s
.RemoveLast();
954 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
955 !s
.AfterLast(wxT(',')).ToLong(&sy
))
957 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
958 return wxDefaultSize
;
963 if (m_instanceAsWindow
)
964 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
965 else if (m_parentAsWindow
)
966 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
969 wxLogError(_("Cannot convert dialog units: dialog unknown."));
970 return wxDefaultSize
;
973 else return wxSize(sx
, sy
);
978 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
980 wxSize sz
= GetSize(param
);
981 return wxPoint(sz
.x
, sz
.y
);
986 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
988 wxString s
= GetParamValue(param
);
989 if (s
.IsEmpty()) return defaultv
;
993 is_dlg
= s
[s
.Length()-1] == wxT('d');
994 if (is_dlg
) s
.RemoveLast();
998 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1004 if (m_instanceAsWindow
)
1005 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1006 else if (m_parentAsWindow
)
1007 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1010 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1019 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1021 wxXmlNode
*font_node
= GetParamNode(param
);
1022 if (font_node
== NULL
)
1024 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1028 wxXmlNode
*oldnode
= m_node
;
1031 long size
= GetLong(wxT("size"), 12);
1033 wxString style
= GetParamValue(wxT("style"));
1034 wxString weight
= GetParamValue(wxT("weight"));
1035 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1036 if (style
== wxT("italic")) istyle
= wxITALIC
;
1037 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1038 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1039 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1041 wxString family
= GetParamValue(wxT("family"));
1042 int ifamily
= wxDEFAULT
;
1043 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1044 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1045 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1046 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1047 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1049 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1051 wxString encoding
= GetParamValue(wxT("encoding"));
1052 wxFontMapper mapper
;
1053 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1054 if (!encoding
.IsEmpty())
1055 enc
= mapper
.CharsetToEncoding(encoding
);
1056 if (enc
== wxFONTENCODING_SYSTEM
)
1057 enc
= wxFONTENCODING_DEFAULT
;
1059 wxString faces
= GetParamValue(wxT("face"));
1060 wxString facename
= wxEmptyString
;
1061 wxFontEnumerator enu
;
1062 enu
.EnumerateFacenames();
1063 wxStringTokenizer
tk(faces
, wxT(","));
1064 while (tk
.HasMoreTokens())
1066 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1067 if (index
!= wxNOT_FOUND
)
1069 facename
= (*enu
.GetFacenames())[index
];
1076 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1081 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1083 //FIXME : add cursor
1085 if (HasParam(wxT("exstyle")))
1086 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1087 if (HasParam(wxT("bg")))
1088 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1089 if (HasParam(wxT("fg")))
1090 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1091 if (GetBool(wxT("enabled"), 1) == 0)
1093 if (GetBool(wxT("focused"), 0) == 1)
1095 if (GetBool(wxT("hidden"), 0) == 1)
1098 if (HasParam(wxT("tooltip")))
1099 wnd
->SetToolTip(GetText(wxT("tooltip")));
1101 if (HasParam(wxT("font")))
1102 wnd
->SetFont(GetFont());
1106 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1108 wxXmlNode
*n
= m_node
->GetChildren();
1112 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1113 n
->GetName() == wxT("object"))
1115 if (this_hnd_only
&& CanHandle(n
))
1116 CreateResource(n
, parent
, NULL
);
1118 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1125 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1128 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1129 wxXmlNode
*n
= root
->GetChildren();
1133 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1135 CreateResource(n
, parent
, NULL
);
1147 // --------------- XRCID implementation -----------------------------
1149 #define XRCID_TABLE_SIZE 1024
1159 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1161 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1163 static int XRCID_LastID
= wxID_HIGHEST
;
1167 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1168 index
%= XRCID_TABLE_SIZE
;
1170 XRCID_record
*oldrec
= NULL
;
1172 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1174 if (wxStrcmp(rec
->key
, str_id
) == 0)
1182 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1183 &XRCID_Records
[index
] : &oldrec
->next
;
1184 *rec_var
= new XRCID_record
;
1185 (*rec_var
)->id
= ++XRCID_LastID
;
1186 (*rec_var
)->key
= wxStrdup(str_id
);
1187 (*rec_var
)->next
= NULL
;
1189 return (*rec_var
)->id
;
1193 static void CleanXRCID_Record(XRCID_record
*rec
)
1197 CleanXRCID_Record(rec
->next
);
1203 static void CleanXRCID_Records()
1205 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1206 CleanXRCID_Record(XRCID_Records
[i
]);
1216 // --------------- module and globals -----------------------------
1218 class wxXmlResourceModule
: public wxModule
1220 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1222 wxXmlResourceModule() {}
1225 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1230 delete wxXmlResource::Set(NULL
);
1231 wxDELETE(wxXmlResource::ms_subclassFactories
);
1232 CleanXRCID_Records();
1236 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1239 // When wxXml is loaded dynamically after the application is already running
1240 // then the built-in module system won't pick this one up. Add it manually.
1241 void wxXmlInitResourceModule()
1243 wxModule
* module = new wxXmlResourceModule
;
1245 wxModule::RegisterModule(module);