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();
121 if (fnd
.Lower().Matches(wxT("*.zip")) ||
122 fnd
.Lower().Matches(wxT("*.xrs")))
124 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
129 drec
= new wxXmlResourceDataRecord
;
139 # undef wxXmlFindFirst
140 # undef wxXmlFindNext
146 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
148 m_handlers
.Append(handler
);
149 handler
->SetParentResource(this);
152 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
154 m_handlers
.Insert(handler
);
155 handler
->SetParentResource(this);
160 void wxXmlResource::ClearHandlers()
166 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
168 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
173 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
175 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
181 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
183 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
188 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
190 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
193 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
195 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
200 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
202 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
205 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
207 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
210 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
212 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
215 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
217 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
220 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
222 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
223 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
226 if (bmp
) { rt
= *bmp
; delete bmp
; }
230 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
232 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
233 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
236 if (icon
) { rt
= *icon
; delete icon
; }
241 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
243 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
246 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
248 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
252 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
253 wxWindow
*control
, wxWindow
*parent
)
256 parent
= control
->GetParent();
257 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
260 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
263 return control
->Reparent(container
);
267 static void ProcessPlatformProperty(wxXmlNode
*node
)
272 wxXmlNode
*c
= node
->GetChildren();
276 if (!c
->GetPropVal(wxT("platform"), &s
))
280 wxStringTokenizer
tkn(s
, wxT(" |"));
282 while (tkn
.HasMoreTokens())
284 s
= tkn
.GetNextToken();
287 s
== wxString(wxT("win"))
288 #elif defined(__UNIX__)
289 s
== wxString(wxT("unix"))
290 #elif defined(__MAC__)
291 s
== wxString(wxT("mac"))
292 #elif defined(__OS2__)
293 s
== wxString(wxT("os2"))
303 ProcessPlatformProperty(c
);
308 wxXmlNode
*c2
= c
->GetNext();
309 node
->RemoveChild(c
);
318 void wxXmlResource::UpdateResources()
321 # if wxUSE_FILESYSTEM
322 wxFSFile
*file
= NULL
;
326 wxString
encoding(wxT("UTF-8"));
327 #if !wxUSE_UNICODE && wxUSE_INTL
328 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
330 // In case we are not using wxLocale to translate strings, convert the strings
331 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
332 // it could break wxGetTranslation lookup.
333 encoding
= wxLocale::GetSystemEncodingName();
337 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
339 modif
= (m_data
[i
].Doc
== NULL
);
343 # if wxUSE_FILESYSTEM
344 file
= fsys
.OpenFile(m_data
[i
].File
);
345 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
347 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
350 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
356 wxInputStream
*stream
= NULL
;
358 # if wxUSE_FILESYSTEM
359 file
= fsys
.OpenFile(m_data
[i
].File
);
361 stream
= file
->GetStream();
363 stream
= new wxFileInputStream(m_data
[i
].File
);
368 delete m_data
[i
].Doc
;
369 m_data
[i
].Doc
= new wxXmlDocument
;
371 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
373 wxLogError(_("Cannot load resources from file '%s'."),
374 m_data
[i
].File
.c_str());
375 wxDELETE(m_data
[i
].Doc
);
377 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
379 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
380 wxDELETE(m_data
[i
].Doc
);
386 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
387 wxT("version"), wxT("0.0.0.0"));
388 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
389 &v1
, &v2
, &v3
, &v4
) == 4)
390 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
395 if (m_version
!= version
)
396 wxLogError(_("Resource files must have same version number!"));
398 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
399 m_data
[i
].Time
= file
->GetModificationTime();
402 # if wxUSE_FILESYSTEM
412 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
413 const wxString
& name
,
414 const wxString
& classname
,
420 // first search for match at the top-level nodes (as this is
421 // where the resource is most commonly looked for):
422 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
424 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
425 (node
->GetName() == wxT("object") ||
426 node
->GetName() == wxT("object_ref")) &&
427 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
429 wxString
cls(node
->GetPropVal(wxT("class"), wxEmptyString
));
430 if (!classname
|| cls
== classname
)
432 // object_ref may not have 'class' property:
433 if (cls
.empty() && node
->GetName() == wxT("object_ref"))
435 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
438 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
440 refNode
->GetPropVal(wxT("class"), wxEmptyString
) == classname
)
449 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
451 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
452 (node
->GetName() == wxT("object") ||
453 node
->GetName() == wxT("object_ref")) )
455 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
464 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
465 const wxString
& classname
,
468 UpdateResources(); //ensure everything is up-to-date
471 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
473 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
476 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
477 name
, classname
, recursive
);
481 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
487 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
488 name
.c_str(), classname
.c_str());
492 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
495 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
497 wxXmlProperty
*dprop
;
498 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
501 if ( dprop
->GetName() == prop
->GetName() )
503 dprop
->SetValue(prop
->GetValue());
509 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
512 // Merge child nodes:
513 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
515 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
518 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
520 if ( dnode
->GetName() == node
->GetName() &&
521 dnode
->GetPropVal(wxT("name"), wxEmptyString
) == name
&&
522 dnode
->GetType() == node
->GetType() )
524 MergeNodes(*dnode
, *node
);
530 dest
.AddChild(new wxXmlNode(*node
));
533 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
534 dest
.SetContent(with
.GetContent());
537 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
,
539 wxXmlResourceHandler
*handlerToUse
)
541 if (node
== NULL
) return NULL
;
543 // handling of referenced resource
544 if ( node
->GetName() == wxT("object_ref") )
546 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
547 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
551 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
556 wxXmlNode
copy(*refNode
);
557 MergeNodes(copy
, *node
);
559 return CreateResFromNode(©
, parent
, instance
);
562 wxXmlResourceHandler
*handler
;
566 if (handlerToUse
->CanHandle(node
))
568 return handlerToUse
->CreateResource(node
, parent
, instance
);
571 else if (node
->GetName() == wxT("object"))
573 wxNode
*ND
= m_handlers
.GetFirst();
576 handler
= (wxXmlResourceHandler
*)ND
->GetData();
577 if (handler
->CanHandle(node
))
579 return handler
->CreateResource(node
, parent
, instance
);
585 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
586 node
->GetName().c_str(),
587 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
592 #include "wx/listimpl.cpp"
593 WX_DECLARE_LIST(wxXmlSubclassFactory
, wxXmlSubclassFactoriesList
);
594 WX_DEFINE_LIST(wxXmlSubclassFactoriesList
);
596 wxXmlSubclassFactoriesList
*wxXmlResource::ms_subclassFactories
= NULL
;
598 /*static*/ void wxXmlResource::AddSubclassFactory(wxXmlSubclassFactory
*factory
)
600 if (!ms_subclassFactories
)
602 ms_subclassFactories
= new wxXmlSubclassFactoriesList
;
603 ms_subclassFactories
->DeleteContents(TRUE
);
605 ms_subclassFactories
->Append(factory
);
608 class wxXmlSubclassFactoryCXX
: public wxXmlSubclassFactory
611 ~wxXmlSubclassFactoryCXX() {}
613 wxObject
*Create(const wxString
& className
)
615 wxClassInfo
* classInfo
= wxClassInfo::FindClass(className
);
618 return classInfo
->CreateObject();
628 wxXmlResourceHandler::wxXmlResourceHandler()
629 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
630 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
635 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
637 wxXmlNode
*myNode
= m_node
;
638 wxString myClass
= m_class
;
639 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
640 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
642 m_instance
= instance
;
643 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
644 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
646 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
647 if (!subclass
.empty())
649 for (wxXmlSubclassFactoriesList::Node
*i
= wxXmlResource::ms_subclassFactories
->GetFirst();
652 m_instance
= i
->GetData()->Create(subclass
);
659 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
660 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
661 subclass
.c_str(), name
.c_str());
667 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
669 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
670 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
672 wxObject
*returned
= DoCreateResource();
676 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
677 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
683 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
685 m_styleNames
.Add(name
);
686 m_styleValues
.Add(value
);
691 void wxXmlResourceHandler::AddWindowStyles()
693 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
694 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
695 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
696 XRC_ADD_STYLE(wxRAISED_BORDER
);
697 XRC_ADD_STYLE(wxSTATIC_BORDER
);
698 XRC_ADD_STYLE(wxNO_BORDER
);
699 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
700 XRC_ADD_STYLE(wxWANTS_CHARS
);
701 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
706 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
708 return (GetParamNode(param
) != NULL
);
712 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
714 wxString s
= GetParamValue(param
);
716 if (!s
) return defaults
;
718 wxStringTokenizer
tkn(s
, wxT("| \t\n"), wxTOKEN_STRTOK
);
722 while (tkn
.HasMoreTokens())
724 fl
= tkn
.GetNextToken();
725 index
= m_styleNames
.Index(fl
);
726 if (index
!= wxNOT_FOUND
)
727 style
|= m_styleValues
[index
];
729 wxLogError(_("Unknown style flag ") + fl
);
736 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
738 wxString
str1(GetParamValue(param
));
743 // VS: First version of XRC resources used $ instead of & (which is
744 // illegal in XML), but later I realized that '_' fits this purpose
745 // much better (because &File means "File with F underlined").
746 if (m_resource
->CompareVersion(2,3,0,1) < 0)
751 for (dt
= str1
.c_str(); *dt
; dt
++)
753 // Remap amp_char to &, map double amp_char to amp_char (for things
754 // like "&File..." -- this is illegal in XML, so we use "_File..."):
757 if ( *(++dt
) == amp_char
)
760 str2
<< wxT('&') << *dt
;
762 // Remap \n to CR, \r to LF, \t to TAB:
763 else if (*dt
== wxT('\\'))
766 case wxT('n') : str2
<< wxT('\n'); break;
767 case wxT('t') : str2
<< wxT('\t'); break;
768 case wxT('r') : str2
<< wxT('\r'); break;
769 default : str2
<< wxT('\\') << *dt
; break;
774 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
775 return wxGetTranslation(str2
);
783 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
786 wxString str1
= GetParamValue(param
);
788 if (!str1
.ToLong(&value
))
796 int wxXmlResourceHandler::GetID()
798 wxString sid
= GetName();
801 if (sid
== wxT("-1")) return -1;
802 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
803 #define stdID(id) else if (sid == wxT(#id)) return id
804 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
805 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
806 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
807 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
808 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
809 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
810 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
811 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
812 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
813 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
814 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
815 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
816 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
817 stdID(wxID_CLOSE_ALL
);
819 else return wxXmlResource::GetXRCID(sid
);
824 wxString
wxXmlResourceHandler::GetName()
826 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
831 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
833 wxString v
= GetParamValue(param
);
835 if (!v
) return defaultv
;
836 else return (v
== wxT("1"));
841 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
843 wxString v
= GetParamValue(param
);
844 unsigned long tmp
= 0;
846 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
847 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
849 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
850 v
.c_str(), param
.c_str());
854 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
855 (unsigned char) ((tmp
& 0x00FF00) >> 8),
856 (unsigned char) ((tmp
& 0x0000FF)));
861 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
862 const wxArtClient
& defaultArtClient
,
865 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
866 wxXmlNode
*bmpNode
= GetParamNode(param
);
869 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
872 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
874 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
875 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
882 /* ...or load the bitmap from file: */
883 wxString name
= GetParamValue(param
);
884 if (name
.IsEmpty()) return wxNullBitmap
;
886 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
889 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
892 wxImage
img(*(fsfile
->GetStream()));
895 wxImage
img(GetParamValue(wxT("bitmap")));
900 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
903 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
904 return wxBitmap(img
);
910 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
911 const wxArtClient
& defaultArtClient
,
915 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
921 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
923 wxCHECK_MSG(m_node
, NULL
, wxT("You can't access handler data before it was initialized!"));
925 wxXmlNode
*n
= m_node
->GetChildren();
929 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
937 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
940 if (n
== NULL
) return wxEmptyString
;
941 n
= n
->GetChildren();
945 if (n
->GetType() == wxXML_TEXT_NODE
||
946 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
947 return n
->GetContent();
950 return wxEmptyString
;
955 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
958 return GetNodeContent(m_node
);
960 return GetNodeContent(GetParamNode(param
));
965 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
967 wxString s
= GetParamValue(param
);
968 if (s
.IsEmpty()) s
= wxT("-1,-1");
972 is_dlg
= s
[s
.Length()-1] == wxT('d');
973 if (is_dlg
) s
.RemoveLast();
975 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
976 !s
.AfterLast(wxT(',')).ToLong(&sy
))
978 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
979 return wxDefaultSize
;
984 if (m_instanceAsWindow
)
985 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
986 else if (m_parentAsWindow
)
987 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
990 wxLogError(_("Cannot convert dialog units: dialog unknown."));
991 return wxDefaultSize
;
994 else return wxSize(sx
, sy
);
999 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
1001 wxSize sz
= GetSize(param
);
1002 return wxPoint(sz
.x
, sz
.y
);
1007 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
1009 wxString s
= GetParamValue(param
);
1010 if (s
.IsEmpty()) return defaultv
;
1014 is_dlg
= s
[s
.Length()-1] == wxT('d');
1015 if (is_dlg
) s
.RemoveLast();
1019 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
1025 if (m_instanceAsWindow
)
1026 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
1027 else if (m_parentAsWindow
)
1028 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
1031 wxLogError(_("Cannot convert dialog units: dialog unknown."));
1040 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
1042 wxXmlNode
*font_node
= GetParamNode(param
);
1043 if (font_node
== NULL
)
1045 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1049 wxXmlNode
*oldnode
= m_node
;
1052 long size
= GetLong(wxT("size"), 12);
1054 wxString style
= GetParamValue(wxT("style"));
1055 wxString weight
= GetParamValue(wxT("weight"));
1056 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1057 if (style
== wxT("italic")) istyle
= wxITALIC
;
1058 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1059 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1060 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1062 wxString family
= GetParamValue(wxT("family"));
1063 int ifamily
= wxDEFAULT
;
1064 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1065 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1066 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1067 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1068 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1070 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1072 wxString encoding
= GetParamValue(wxT("encoding"));
1073 wxFontMapper mapper
;
1074 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1075 if (!encoding
.IsEmpty())
1076 enc
= mapper
.CharsetToEncoding(encoding
);
1077 if (enc
== wxFONTENCODING_SYSTEM
)
1078 enc
= wxFONTENCODING_DEFAULT
;
1080 wxString faces
= GetParamValue(wxT("face"));
1081 wxString facename
= wxEmptyString
;
1082 wxFontEnumerator enu
;
1083 enu
.EnumerateFacenames();
1084 wxStringTokenizer
tk(faces
, wxT(","));
1085 while (tk
.HasMoreTokens())
1087 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1088 if (index
!= wxNOT_FOUND
)
1090 facename
= (*enu
.GetFacenames())[index
];
1097 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1102 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1104 //FIXME : add cursor
1106 if (HasParam(wxT("exstyle")))
1107 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1108 if (HasParam(wxT("bg")))
1109 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1110 if (HasParam(wxT("fg")))
1111 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1112 if (GetBool(wxT("enabled"), 1) == 0)
1114 if (GetBool(wxT("focused"), 0) == 1)
1116 if (GetBool(wxT("hidden"), 0) == 1)
1119 if (HasParam(wxT("tooltip")))
1120 wnd
->SetToolTip(GetText(wxT("tooltip")));
1122 if (HasParam(wxT("font")))
1123 wnd
->SetFont(GetFont());
1127 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1129 wxXmlNode
*n
= m_node
->GetChildren();
1133 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1134 (n
->GetName() == wxT("object") || n
->GetName() == wxT("object_ref")))
1136 m_resource
->CreateResFromNode(n
, parent
, NULL
,
1137 this_hnd_only
? this : NULL
);
1144 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1147 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1148 wxXmlNode
*n
= root
->GetChildren();
1152 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1154 CreateResource(n
, parent
, NULL
);
1166 // --------------- XRCID implementation -----------------------------
1168 #define XRCID_TABLE_SIZE 1024
1178 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1180 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1182 static int XRCID_LastID
= wxID_HIGHEST
;
1186 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1187 index
%= XRCID_TABLE_SIZE
;
1189 XRCID_record
*oldrec
= NULL
;
1191 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1193 if (wxStrcmp(rec
->key
, str_id
) == 0)
1201 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1202 &XRCID_Records
[index
] : &oldrec
->next
;
1203 *rec_var
= new XRCID_record
;
1204 (*rec_var
)->key
= wxStrdup(str_id
);
1205 (*rec_var
)->next
= NULL
;
1208 int asint
= wxStrtol(str_id
, &end
, 10);
1209 if (*str_id
&& *end
== 0)
1211 // if str_id was integer, keep it verbosely:
1212 (*rec_var
)->id
= asint
;
1216 (*rec_var
)->id
= ++XRCID_LastID
;
1219 return (*rec_var
)->id
;
1223 static void CleanXRCID_Record(XRCID_record
*rec
)
1227 CleanXRCID_Record(rec
->next
);
1233 static void CleanXRCID_Records()
1235 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1236 CleanXRCID_Record(XRCID_Records
[i
]);
1246 // --------------- module and globals -----------------------------
1248 class wxXmlResourceModule
: public wxModule
1250 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1252 wxXmlResourceModule() {}
1255 wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX
);
1260 delete wxXmlResource::Set(NULL
);
1261 wxDELETE(wxXmlResource::ms_subclassFactories
);
1262 CleanXRCID_Records();
1266 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1269 // When wxXml is loaded dynamically after the application is already running
1270 // then the built-in module system won't pick this one up. Add it manually.
1271 void wxXmlInitResourceModule()
1273 wxModule
* module = new wxXmlResourceModule
;
1275 wxModule::RegisterModule(module);