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:*.xrc"));
111 drec
= new wxXmlResourceDataRecord
;
121 # undef wxXmlFindFirst
122 # undef wxXmlFindNext
128 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
130 m_handlers
.Append(handler
);
131 handler
->SetParentResource(this);
134 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
136 m_handlers
.Insert(handler
);
137 handler
->SetParentResource(this);
142 void wxXmlResource::ClearHandlers()
148 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
150 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
155 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
157 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
163 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
165 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
170 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
172 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
175 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
177 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
182 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
184 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
187 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
189 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
192 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
194 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
197 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
199 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
202 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
204 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
205 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
208 if (bmp
) { rt
= *bmp
; delete bmp
; }
212 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
214 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
215 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
218 if (icon
) { rt
= *icon
; delete icon
; }
223 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
225 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
228 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
230 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
234 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
235 wxWindow
*control
, wxWindow
*parent
)
238 parent
= control
->GetParent();
239 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
242 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
245 return control
->Reparent(container
);
249 static void ProcessPlatformProperty(wxXmlNode
*node
)
254 wxXmlNode
*c
= node
->GetChildren();
258 if (!c
->GetPropVal(wxT("platform"), &s
))
262 wxStringTokenizer
tkn(s
, wxT(" |"));
264 while (tkn
.HasMoreTokens())
266 s
= tkn
.GetNextToken();
269 s
== wxString(wxT("win"))
270 #elif defined(__UNIX__)
271 s
== wxString(wxT("unix"))
272 #elif defined(__MAC__)
273 s
== wxString(wxT("mac"))
274 #elif defined(__OS2__)
275 s
== wxString(wxT("os2"))
285 ProcessPlatformProperty(c
);
290 wxXmlNode
*c2
= c
->GetNext();
291 node
->RemoveChild(c
);
300 void wxXmlResource::UpdateResources()
303 # if wxUSE_FILESYSTEM
304 wxFSFile
*file
= NULL
;
308 wxString
encoding(wxT("UTF-8"));
309 #if !wxUSE_UNICODE && wxUSE_INTL
310 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
312 // In case we are not using wxLocale to translate strings, convert the strings
313 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
314 // it could break wxGetTranslation lookup.
315 encoding
= wxLocale::GetSystemEncodingName();
319 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
321 modif
= (m_data
[i
].Doc
== NULL
);
325 # if wxUSE_FILESYSTEM
326 file
= fsys
.OpenFile(m_data
[i
].File
);
327 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
329 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
332 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
338 wxInputStream
*stream
= NULL
;
340 # if wxUSE_FILESYSTEM
341 file
= fsys
.OpenFile(m_data
[i
].File
);
343 stream
= file
->GetStream();
345 stream
= new wxFileInputStream(m_data
[i
].File
);
350 delete m_data
[i
].Doc
;
351 m_data
[i
].Doc
= new wxXmlDocument
;
353 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
355 wxLogError(_("Cannot load resources from file '%s'."),
356 m_data
[i
].File
.c_str());
357 wxDELETE(m_data
[i
].Doc
);
359 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
361 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
362 wxDELETE(m_data
[i
].Doc
);
368 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
369 wxT("version"), wxT("0.0.0.0"));
370 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
371 &v1
, &v2
, &v3
, &v4
) == 4)
372 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
377 if (m_version
!= version
)
378 wxLogError(_("Resource files must have same version number!"));
380 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
381 m_data
[i
].Time
= file
->GetModificationTime();
384 # if wxUSE_FILESYSTEM
394 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
395 const wxString
& name
,
396 const wxString
& classname
,
402 // first search for match at the top-level nodes (as this is
403 // where the resource is most commonly looked for):
404 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
406 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
407 (node
->GetName() == wxT("object") ||
408 node
->GetName() == wxT("object_ref")) &&
409 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
411 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
412 if (!classname
|| cls
== classname
)
414 // object_ref may not have 'class' property:
415 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
417 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
420 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
422 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
431 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
433 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
434 (node
->GetName() == wxT("object") ||
435 node
->GetName() == wxT("object_ref")) )
437 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
446 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
447 const wxString
& classname
,
450 UpdateResources(); //ensure everything is up-to-date
453 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
455 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
458 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
459 name
, classname
, recursive
);
463 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
469 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
470 name
.c_str(), classname
.c_str());
474 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
477 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
479 wxXmlProperty
*dprop
;
480 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
483 if ( dprop
->GetName() == prop
->GetName() )
485 dprop
->SetValue(prop
->GetValue());
491 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
494 // Merge child nodes:
495 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
497 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
500 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
502 if ( dnode
->GetName() == node
->GetName() &&
503 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
504 dnode
->GetType() == node
->GetType() )
506 MergeNodes(*dnode
, *node
);
512 dest
.AddChild(new wxXmlNode(*node
));
515 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
516 dest
.SetContent(with
.GetContent());
519 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
521 if (node
== NULL
) return NULL
;
523 // handling of referenced resource
524 if ( node
->GetName() == wxT("object_ref") )
526 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
527 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
531 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
536 wxXmlNode
copy(*refNode
);
537 MergeNodes(copy
, *node
);
539 return CreateResFromNode(©
, parent
, instance
);
542 wxXmlResourceHandler
*handler
;
544 wxNode
* ND
= m_handlers
.GetFirst();
547 handler
= (wxXmlResourceHandler
*)ND
->GetData();
548 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
550 ret
= handler
->CreateResource(node
, parent
, instance
);
556 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
557 node
->GetName().c_str(),
558 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
563 #include "wx/listimpl.cpp"
564 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
565 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
567 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
569 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
571 if (!ms_subclassFactories
)
573 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
574 ms_subclassFactories
->DeleteContents(TRUE
);
576 ms_subclassFactories
->Append(factory
);
579 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
582 ~wxXmlSubclassFactoryCXX() {}
584 wxObject
*Create(const wxString
& className
)
586 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
589 return classInfo
->CreateObject();
599 wxXmlResourceHandler::wxXmlResourceHandler()
600 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
601 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
606 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
608 wxXmlNode
*myNode
= m_node
;
609 wxString myClass
= m_class
;
610 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
611 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
613 m_instance
= instance
;
614 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
615 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
617 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
618 if (!subclass
.empty())
620 for (wxXmlSubclassFactoriesList::Node
*i
= wxXmlResource::ms_subclassFactories
->GetFirst();
623 m_instance
= i
->GetData()->Create(subclass
);
630 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
631 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
632 subclass
.c_str(), name
.c_str());
638 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
640 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
641 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
643 wxObject
*returned
= DoCreateResource();
647 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
648 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
654 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
656 m_styleNames
.Add(name
);
657 m_styleValues
.Add(value
);
662 void wxXmlResourceHandler::AddWindowStyles()
664 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
665 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
666 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
667 XRC_ADD_STYLE(wxRAISED_BORDER
);
668 XRC_ADD_STYLE(wxSTATIC_BORDER
);
669 XRC_ADD_STYLE(wxNO_BORDER
);
670 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
671 XRC_ADD_STYLE(wxWANTS_CHARS
);
672 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
677 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
679 return (GetParamNode(param
) != NULL
);
683 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
685 wxString s
= GetParamValue(param
);
687 if (!s
) return defaults
;
689 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
693 while (tkn
.HasMoreTokens())
695 fl
= tkn
.GetNextToken();
696 index
= m_styleNames
.Index(fl
);
697 if (index
!= wxNOT_FOUND
)
698 style
|= m_styleValues
[index
];
700 wxLogError(_("Unknown style flag ") + fl
);
707 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
709 wxString
str1(GetParamValue(param
));
714 // VS: First version of XRC resources used $ instead of & (which is
715 // illegal in XML), but later I realized that '_' fits this purpose
716 // much better (because &File means "File with F underlined").
717 if (m_resource
->CompareVersion(2,3,0,1) < 0)
722 for (dt
= str1
.c_str(); *dt
; dt
++)
724 // Remap amp_char to &, map double amp_char to amp_char (for things
725 // like "&File..." -- this is illegal in XML, so we use "_File..."):
728 if ( *(++dt
) == amp_char
)
731 str2
<< wxT('&') << *dt
;
733 // Remap \n to CR, \r to LF, \t to TAB:
734 else if (*dt
== wxT('\\'))
737 case wxT('n') : str2
<< wxT('\n'); break;
738 case wxT('t') : str2
<< wxT('\t'); break;
739 case wxT('r') : str2
<< wxT('\r'); break;
740 default : str2
<< wxT('\\') << *dt
; break;
745 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
746 return wxGetTranslation(str2
);
754 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
757 wxString str1
= GetParamValue(param
);
759 if (!str1
.ToLong(&value
))
767 int wxXmlResourceHandler::GetID()
769 wxString sid
= GetName();
772 if (sid
== wxT("-1")) return -1;
773 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
774 #define stdID(id) else if (sid == wxT(#id)) return id
775 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
776 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
777 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
778 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
779 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
780 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
781 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
782 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
783 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
784 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
785 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
786 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
787 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
788 stdID(wxID_CLOSE_ALL
);
790 else return wxXmlResource::GetXRCID(sid
);
795 wxString
wxXmlResourceHandler::GetName()
797 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
802 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
804 wxString v
= GetParamValue(param
);
806 if (!v
) return defaultv
;
807 else return (v
== wxT("1"));
812 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
814 wxString v
= GetParamValue(param
);
815 unsigned long tmp
= 0;
817 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
818 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
820 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
821 v
.c_str(), param
.c_str());
825 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
826 (unsigned char) ((tmp
& 0x00FF00) >> 8),
827 (unsigned char) ((tmp
& 0x0000FF)));
832 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
833 const wxArtClient
& defaultArtClient
,
836 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
837 wxXmlNode
*bmpNode
= GetParamNode(param
);
840 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
843 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
845 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
846 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
853 /* ...or load the bitmap from file: */
854 wxString name
= GetParamValue(param
);
855 if (name
.IsEmpty()) return wxNullBitmap
;
857 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
860 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
863 wxImage
img(*(fsfile
->GetStream()));
866 wxImage
img(GetParamValue(wxT("bitmap")));
871 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
874 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
875 return wxBitmap(img
);
881 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
882 const wxArtClient
& defaultArtClient
,
885 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
887 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
890 wxBitmap bmppt
= GetBitmap(param
, size
);
891 iconpt
= (wxIcon
*)(&bmppt
);
892 wxIcon
icon(*iconpt
);
899 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
901 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
903 wxXmlNode
*n
= m_node
->GetChildren();
907 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
915 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
918 if (n
== NULL
) return wxEmptyString
;
919 n
= n
->GetChildren();
923 if (n
->GetType() == wxXML_TEXT_NODE
||
924 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
925 return n
->GetContent();
928 return wxEmptyString
;
933 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
936 return GetNodeContent(m_node
);
938 return GetNodeContent(GetParamNode(param
));
943 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
945 wxString s
= GetParamValue(param
);
946 if (s
.IsEmpty()) s
= wxT("-1,-1");
950 is_dlg
= s
[s
.Length()-1] == wxT('d');
951 if (is_dlg
) s
.RemoveLast();
953 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
954 !s
.AfterLast(wxT(',')).ToLong(&sy
))
956 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
957 return wxDefaultSize
;
962 if (m_instanceAsWindow
)
963 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
964 else if (m_parentAsWindow
)
965 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
968 wxLogError(_("Cannot convert dialog units: dialog unknown."));
969 return wxDefaultSize
;
972 else return wxSize(sx
, sy
);
977 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
979 wxSize sz
= GetSize(param
);
980 return wxPoint(sz
.x
, sz
.y
);
985 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
987 wxString s
= GetParamValue(param
);
988 if (s
.IsEmpty()) return defaultv
;
992 is_dlg
= s
[s
.Length()-1] == wxT('d');
993 if (is_dlg
) s
.RemoveLast();
997 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1003 if (m_instanceAsWindow
)
1004 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1005 else if (m_parentAsWindow
)
1006 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1009 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1018 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1020 wxXmlNode
*font_node
= GetParamNode(param
);
1021 if (font_node
== NULL
)
1023 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1027 wxXmlNode
*oldnode
= m_node
;
1030 long size
= GetLong(wxT("size"), 12);
1032 wxString style
= GetParamValue(wxT("style"));
1033 wxString weight
= GetParamValue(wxT("weight"));
1034 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1035 if (style
== wxT("italic")) istyle
= wxITALIC
;
1036 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1037 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1038 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1040 wxString family
= GetParamValue(wxT("family"));
1041 int ifamily
= wxDEFAULT
;
1042 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1043 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1044 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1045 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1046 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1048 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1050 wxString encoding
= GetParamValue(wxT("encoding"));
1051 wxFontMapper mapper
;
1052 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1053 if (!encoding
.IsEmpty())
1054 enc
= mapper
.CharsetToEncoding(encoding
);
1055 if (enc
== wxFONTENCODING_SYSTEM
)
1056 enc
= wxFONTENCODING_DEFAULT
;
1058 wxString faces
= GetParamValue(wxT("face"));
1059 wxString facename
= wxEmptyString
;
1060 wxFontEnumerator enu
;
1061 enu
.EnumerateFacenames();
1062 wxStringTokenizer
tk(faces
, wxT(","));
1063 while (tk
.HasMoreTokens())
1065 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1066 if (index
!= wxNOT_FOUND
)
1068 facename
= (*enu
.GetFacenames())[index
];
1075 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1080 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1082 //FIXME : add cursor
1084 if (HasParam(wxT("exstyle")))
1085 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1086 if (HasParam(wxT("bg")))
1087 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1088 if (HasParam(wxT("fg")))
1089 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1090 if (GetBool(wxT("enabled"), 1) == 0)
1092 if (GetBool(wxT("focused"), 0) == 1)
1094 if (GetBool(wxT("hidden"), 0) == 1)
1097 if (HasParam(wxT("tooltip")))
1098 wnd
->SetToolTip(GetText(wxT("tooltip")));
1100 if (HasParam(wxT("font")))
1101 wnd
->SetFont(GetFont());
1105 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1107 wxXmlNode
*n
= m_node
->GetChildren();
1111 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1112 n
->GetName() == wxT("object"))
1114 if (this_hnd_only
&& CanHandle(n
))
1115 CreateResource(n
, parent
, NULL
);
1117 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1124 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1127 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1128 wxXmlNode
*n
= root
->GetChildren();
1132 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1134 CreateResource(n
, parent
, NULL
);
1146 // --------------- XRCID implementation -----------------------------
1148 #define XRCID_TABLE_SIZE 1024
1158 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1160 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1162 static int XRCID_LastID
= wxID_HIGHEST
;
1166 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1167 index
%= XRCID_TABLE_SIZE
;
1169 XRCID_record
*oldrec
= NULL
;
1171 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1173 if (wxStrcmp(rec
->key
, str_id
) == 0)
1181 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1182 &XRCID_Records
[index
] : &oldrec
->next
;
1183 *rec_var
= new XRCID_record
;
1184 (*rec_var
)->id
= ++XRCID_LastID
;
1185 (*rec_var
)->key
= wxStrdup(str_id
);
1186 (*rec_var
)->next
= NULL
;
1188 return (*rec_var
)->id
;
1192 static void CleanXRCID_Record(XRCID_record
*rec
)
1196 CleanXRCID_Record(rec
->next
);
1202 static void CleanXRCID_Records()
1204 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1205 CleanXRCID_Record(XRCID_Records
[i
]);
1215 // --------------- module and globals -----------------------------
1217 class wxXmlResourceModule
: public wxModule
1219 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1221 wxXmlResourceModule() {}
1224 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1229 delete wxXmlResource::Set(NULL
);
1230 wxDELETE(wxXmlResource::ms_subclassFactories
);
1231 CleanXRCID_Records();
1235 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1238 // When wxXml is loaded dynamically after the application is already running
1239 // then the built-in module system won't pick this one up. Add it manually.
1240 void wxXmlInitResourceModule()
1242 wxModule
* module = new wxXmlResourceModule
;
1244 wxModule::RegisterModule(module);