1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #pragma interface "xmlres.h"
19 #include "wx/string.h"
20 #include "wx/dynarray.h"
21 #include "wx/datetime.h"
23 #include "wx/gdicmn.h"
24 #include "wx/filesys.h"
25 #include "wx/bitmap.h"
28 class WXDLLEXPORT wxMenu
;
29 class WXDLLEXPORT wxMenuBar
;
30 class WXDLLEXPORT wxDialog
;
31 class WXDLLEXPORT wxPanel
;
32 class WXDLLEXPORT wxWindow
;
33 class WXDLLEXPORT wxToolBar
;
35 class WXDLLEXPORT wxXmlResourceHandler
;
37 #include "wx/xml/xml.h"
40 class WXDLLEXPORT wxXmlResourceDataRecord
43 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
44 ~wxXmlResourceDataRecord() {delete Doc
;}
51 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
53 // This class holds XML resources from one or more .xml files
54 // (or derived forms, either binary or zipped -- see manual for
57 class WXDLLEXPORT wxXmlResource
: public wxObject
61 wxXmlResource(const wxString
& filemask
);
64 // Loads resources from XML files that match given filemask.
65 // This method understands VFS (see filesys.h).
66 bool Load(const wxString
& filemask
);
68 // Initialize handlers for all supported controls/windows. This will
69 // make the executable quite big because it forces linking against
70 // most of wxWin library
71 void InitAllHandlers();
73 // Initialize only specific handler (or custom handler). Convention says
74 // that handler name is equal to control's name plus 'XmlHandler', e.g.
75 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
76 // (xmlres) can create include file that contains initialization code for
77 // all controls used within the resource.
78 void AddHandler(wxXmlResourceHandler
*handler
);
80 // Removes all handlers
83 // Loads menu from resource. Returns NULL on failure.
84 wxMenu
*LoadMenu(const wxString
& name
);
86 // Loads menubar from resource. Returns NULL on failure.
87 wxMenuBar
*LoadMenuBar(const wxString
& name
);
91 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
94 // Loads dialog. dlg points to parent window (if any). Second form
95 // is used to finish creation of already existing instance (main reason
96 // for this is that you may want to use derived class with new event table)
97 // Example (typical usage):
99 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
101 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
102 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
104 // Loads panel. panel points to parent window (if any). Second form
105 // is used to finish creation of already existing instance.
106 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
107 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
109 // Returns numeric ID that is equivalent to string id used in XML
110 // resource. To be used in event tables
111 // Macro XMLID is provided for convenience
112 static int GetXMLID(const char *str_id
);
115 // Scans resources list for unloaded files and loads them. Also reloads
116 // files that have been modified since last loading.
117 void UpdateResources();
119 // Finds resource (calls UpdateResources) and returns node containing it
120 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& type
);
122 // Creates resource from info in given node:
123 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
125 // Remove nodes with property "platform" that does not
126 // match current platform
127 void ProcessPlatformProperty(wxXmlNode
*node
);
131 wxXmlResourceDataRecords m_Data
;
133 wxFileSystem m_CurFileSystem
;
134 wxFileSystem
& GetCurFileSystem() { return m_CurFileSystem
; }
137 friend class wxXmlResourceHandler
;
141 // Global instance of resource class. For your convenience.
142 extern wxXmlResource
*wxTheXmlResource
;
144 // This macro translates string identifier (as used in XML resource,
145 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
146 // wxWindows event tables.
148 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
149 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
150 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
151 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
152 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
155 #define XMLID(str_id) \
156 wxXmlResource::GetXMLID(_T(str_id))
160 // This macro returns pointer to particular control in dialog
161 // created using XML resources. You can use it to set/get values from
165 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
166 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(_T("default value"));
168 #define XMLCTRL(window, id, type) \
169 ((type*)((window).FindWindow(XMLID(id))))
173 class WXDLLEXPORT wxXmlResourceHandler
: public wxObject
176 wxXmlResourceHandler();
177 virtual ~wxXmlResourceHandler() {}
179 // Creates object (menu, dialog, control, ...) from XML node.
180 // Should check for validity.
181 // parent is higher-level object (usually window, dialog or panel)
182 // that is often neccessary to create resource
183 // if instance != NULL it should not create new instance via 'new' but
184 // rather use this one and call its Create method
185 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
);
187 // This one is called from CreateResource after variables
189 virtual wxObject
*DoCreateResource() = 0;
191 // Returns TRUE if it understands this node and can create
192 // resource from it, FALSE otherwise.
193 virtual bool CanHandle(wxXmlNode
*node
) = 0;
195 void SetParentResource(wxXmlResource
*res
) { m_Resource
= res
; }
200 wxXmlResource
*m_Resource
;
201 wxArrayString m_StyleNames
;
202 wxArrayInt m_StyleValues
;
204 // Variables (filled by CreateResource)
206 wxObject
*m_Parent
, *m_Instance
;
207 wxWindow
*m_ParentAsWindow
, *m_InstanceAsWindow
;
209 // --- Handy methods:
211 // Gets node content from wxXML_ENTITY_NODE
212 // (the problem is, <tag>content<tag> is represented as
213 // wxXML_ENTITY_NODE name="tag", content=""
214 // |-- wxXML_TEXT_NODE or
215 // wxXML_CDATA_SECTION_NODE name="" content="content"
216 wxString
GetNodeContent(wxXmlNode
*node
);
218 // Check to see if a param exists
219 bool HasParam(const wxString
& param
);
221 // Finds the node or returns NULL
222 wxXmlNode
*GetParamNode(const wxString
& param
);
223 wxString
GetParamValue(const wxString
& param
);
225 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
226 // understood by this handler
227 void AddStyle(const wxString
& name
, int value
);
229 // Gets style flags from text in form "flag | flag2| flag3 |..."
230 // Only understads flags added with AddStyle
231 int GetStyle(const wxString
& param
= _T("style"), int defaults
= 0);
233 // Gets text from param and does some convertions:
234 // - replaces \n, \r, \t by respective chars (according to C syntax)
235 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
236 // - converts encodings if neccessary
237 wxString
GetText(const wxString
& param
);
243 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
244 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
246 // Get integer value from param
247 long GetLong( const wxString
& param
, long defaultv
= 0 );
249 // Get colour in HTML syntax (#RRGGBB)
250 wxColour
GetColour(const wxString
& param
);
252 // Get size/position:
253 wxSize
GetSize(const wxString
& param
= _T("size"));
254 wxPoint
GetPosition(const wxString
& param
= _T("pos"));
257 wxBitmap
GetBitmap(const wxString
& param
= _T("bitmap"), wxSize size
= wxDefaultSize
);
258 wxIcon
GetIcon(const wxString
& param
= _T("icon"), wxSize size
= wxDefaultSize
);
260 // Sets common window options:
261 void SetupWindow(wxWindow
*wnd
);
263 void CreateChildren(wxObject
*parent
, bool only_this_handler
= FALSE
,
264 wxXmlNode
*children_node
= NULL
/*stands for
265 GetParamNode("children")*/);
266 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
)
267 { return m_Resource
->CreateResFromNode(node
, parent
, instance
); }
270 wxFileSystem
& GetCurFileSystem() { return m_Resource
->GetCurFileSystem(); }
273 #define ADD_STYLE(style) AddStyle(_T(#style), style)
277 #endif // _WX_XMLRES_H_