]> git.saurik.com Git - wxWidgets.git/blob - contrib/include/wx/xml/xmlres.h
006c166a20e5a83d072b957eca49ef834c41b92b
[wxWidgets.git] / contrib / include / wx / xml / xmlres.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: xmlres.h
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
5 // Created: 2000/03/05
6 // RCS-ID: $Id$
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_XMLRES_H_
12 #define _WX_XMLRES_H_
13
14 #ifdef __GNUG__
15 #pragma interface "xmlres.h"
16 #endif
17
18 #include "wx/defs.h"
19 #include "wx/string.h"
20 #include "wx/dynarray.h"
21 #include "wx/datetime.h"
22 #include "wx/list.h"
23 #include "wx/gdicmn.h"
24 #include "wx/filesys.h"
25 #include "wx/bitmap.h"
26 #include "wx/icon.h"
27
28 class WXDLLEXPORT wxMenu;
29 class WXDLLEXPORT wxMenuBar;
30 class WXDLLEXPORT wxDialog;
31 class WXDLLEXPORT wxPanel;
32 class WXDLLEXPORT wxWindow;
33 class WXDLLEXPORT wxToolBar;
34
35 class WXDLLEXPORT wxXmlResourceHandler;
36
37 #include "wx/xml/xml.h"
38
39
40 class WXDLLEXPORT wxXmlResourceDataRecord
41 {
42 public:
43 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
44 ~wxXmlResourceDataRecord() {delete Doc;}
45
46 wxString File;
47 wxXmlDocument *Doc;
48 bool DocOwned;
49 wxDateTime Time;
50 };
51
52 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
53
54 // This class holds XML resources from one or more .xml files
55 // (or derived forms, either binary or zipped -- see manual for
56 // details).
57
58 class WXDLLEXPORT wxXmlResource : public wxObject
59 {
60 public:
61 // Ctor. If use_locale is TRUE, translatable strings are
62 // translated via _(). You can disable it by passing use_locale=FALSE
63 // (for example if you provide resource file for each locale)
64 wxXmlResource(bool use_locale = TRUE);
65 wxXmlResource(const wxString& filemask, bool use_locale = TRUE);
66 ~wxXmlResource();
67
68 // Loads resources from XML files that match given filemask.
69 // This method understands VFS (see filesys.h).
70 bool Load(const wxString& filemask);
71
72 // Initialize handlers for all supported controls/windows. This will
73 // make the executable quite big because it forces linking against
74 // most of wxWin library
75 void InitAllHandlers();
76
77 // Initialize only specific handler (or custom handler). Convention says
78 // that handler name is equal to control's name plus 'XmlHandler', e.g.
79 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
80 // (xmlres) can create include file that contains initialization code for
81 // all controls used within the resource.
82 void AddHandler(wxXmlResourceHandler *handler);
83
84 // Removes all handlers
85 void ClearHandlers();
86
87 // Loads menu from resource. Returns NULL on failure.
88 wxMenu *LoadMenu(const wxString& name);
89
90 // Loads menubar from resource. Returns NULL on failure.
91 wxMenuBar *LoadMenuBar(const wxString& name);
92
93 #if wxUSE_TOOLBAR
94 // Loads toolbar
95 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
96 #endif
97
98 // Loads dialog. dlg points to parent window (if any). Second form
99 // is used to finish creation of already existing instance (main reason
100 // for this is that you may want to use derived class with new event table)
101 // Example (typical usage):
102 // MyDialog dlg;
103 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
104 // dlg->ShowModal();
105 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
106 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
107
108 // Loads panel. panel points to parent window (if any). Second form
109 // is used to finish creation of already existing instance.
110 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
111 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
112
113 // Load bitmap or icon resource from file:
114 wxBitmap LoadBitmap(const wxString& name);
115 wxIcon LoadIcon(const wxString& name);
116
117 // Returns numeric ID that is equivalent to string id used in XML
118 // resource. To be used in event tables
119 // Macro XMLID is provided for convenience
120 static int GetXMLID(const char *str_id);
121
122 protected:
123 // Scans resources list for unloaded files and loads them. Also reloads
124 // files that have been modified since last loading.
125 void UpdateResources();
126
127 // Finds resource (calls UpdateResources) and returns node containing it
128 wxXmlNode *FindResource(const wxString& name, const wxString& type);
129
130 // Creates resource from info in given node:
131 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
132
133 // Remove nodes with property "platform" that does not
134 // match current platform
135 void ProcessPlatformProperty(wxXmlNode *node);
136
137 bool GetUseLocale() { return m_UseLocale; }
138
139 private:
140 bool m_UseLocale;
141 wxList m_Handlers;
142 wxXmlResourceDataRecords m_Data;
143 #if wxUSE_FILESYSTEM
144 wxFileSystem m_CurFileSystem;
145 wxFileSystem& GetCurFileSystem() { return m_CurFileSystem; }
146 #endif
147
148 friend class wxXmlResourceHandler;
149 };
150
151
152 // Global instance of resource class. For your convenience.
153 extern wxXmlResource *wxTheXmlResource;
154
155 // This macro translates string identifier (as used in XML resource,
156 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
157 // wxWindows event tables.
158 // Example:
159 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
160 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
161 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
162 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
163 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
164 // END_EVENT_TABLE()
165
166 #define XMLID(str_id) \
167 wxXmlResource::GetXMLID(_T(str_id))
168
169
170 // This macro returns pointer to particular control in dialog
171 // created using XML resources. You can use it to set/get values from
172 // controls.
173 // Example:
174 // wxDialog dlg;
175 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
176 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(_T("default value"));
177
178 #define XMLCTRL(window, id, type) \
179 ((type*)((window).FindWindow(XMLID(id))))
180
181
182
183 class WXDLLEXPORT wxXmlResourceHandler : public wxObject
184 {
185 public:
186 wxXmlResourceHandler();
187 virtual ~wxXmlResourceHandler() {}
188
189 // Creates object (menu, dialog, control, ...) from XML node.
190 // Should check for validity.
191 // parent is higher-level object (usually window, dialog or panel)
192 // that is often neccessary to create resource
193 // if instance != NULL it should not create new instance via 'new' but
194 // rather use this one and call its Create method
195 wxObject *CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance);
196
197 // This one is called from CreateResource after variables
198 // were filled
199 virtual wxObject *DoCreateResource() = 0;
200
201 // Returns TRUE if it understands this node and can create
202 // resource from it, FALSE otherwise.
203 virtual bool CanHandle(wxXmlNode *node) = 0;
204
205 void SetParentResource(wxXmlResource *res) { m_Resource = res; }
206
207
208 protected:
209
210 wxXmlResource *m_Resource;
211 wxArrayString m_StyleNames;
212 wxArrayInt m_StyleValues;
213
214 // Variables (filled by CreateResource)
215 wxXmlNode *m_Node;
216 wxObject *m_Parent, *m_Instance;
217 wxWindow *m_ParentAsWindow, *m_InstanceAsWindow;
218
219 // --- Handy methods:
220
221 // Gets node content from wxXML_ENTITY_NODE
222 // (the problem is, <tag>content<tag> is represented as
223 // wxXML_ENTITY_NODE name="tag", content=""
224 // |-- wxXML_TEXT_NODE or
225 // wxXML_CDATA_SECTION_NODE name="" content="content"
226 wxString GetNodeContent(wxXmlNode *node);
227
228 // Check to see if a param exists
229 bool HasParam(const wxString& param);
230
231 // Finds the node or returns NULL
232 wxXmlNode *GetParamNode(const wxString& param);
233 wxString GetParamValue(const wxString& param);
234
235 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
236 // understood by this handler
237 void AddStyle(const wxString& name, int value);
238
239 // Add styles common to all wxWindow-derived classes
240 void AddWindowStyles();
241
242 // Gets style flags from text in form "flag | flag2| flag3 |..."
243 // Only understads flags added with AddStyle
244 int GetStyle(const wxString& param = _T("style"), int defaults = 0);
245
246 // Gets text from param and does some convertions:
247 // - replaces \n, \r, \t by respective chars (according to C syntax)
248 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
249 // - calls wxGetTranslations (unless disabled in wxXmlResource)
250 wxString GetText(const wxString& param);
251
252 // Return XMLID
253 int GetID();
254 wxString GetName();
255
256 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
257 bool GetBool(const wxString& param, bool defaultv = FALSE);
258
259 // Get integer value from param
260 long GetLong( const wxString& param, long defaultv = 0 );
261
262 // Get colour in HTML syntax (#RRGGBB)
263 wxColour GetColour(const wxString& param);
264
265 // Get size/position (may be in dlg units):
266 wxSize GetSize(const wxString& param = _T("size"));
267 wxPoint GetPosition(const wxString& param = _T("pos"));
268
269 // Get dimension (may be in dlg units):
270 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
271
272 // Get bitmap:
273 wxBitmap GetBitmap(const wxString& param = _T("bitmap"), wxSize size = wxDefaultSize);
274 wxIcon GetIcon(const wxString& param = _T("icon"), wxSize size = wxDefaultSize);
275
276 // Get font:
277 wxFont GetFont(const wxString& param = _T("font"));
278
279 // Sets common window options:
280 void SetupWindow(wxWindow *wnd);
281
282 void CreateChildren(wxObject *parent, bool only_this_handler = FALSE,
283 wxXmlNode *children_node = NULL /*stands for
284 GetParamNode("children")*/);
285 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL)
286 { return m_Resource->CreateResFromNode(node, parent, instance); }
287
288 // helper
289 wxFileSystem& GetCurFileSystem() { return m_Resource->GetCurFileSystem(); }
290 };
291
292 #define ADD_STYLE(style) AddStyle(_T(#style), style)
293
294
295
296 #endif // _WX_XMLRES_H_