]>
Commit | Line | Data |
---|---|---|
56d2f750 VS |
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" | |
792064e9 VS |
24 | #include "wx/filesys.h" |
25 | #include "wx/bitmap.h" | |
26 | #include "wx/icon.h" | |
56d2f750 | 27 | |
999d9a9f | 28 | #include "wx/xrc/xml.h" |
ea89ec17 | 29 | |
1ad83f6b VS |
30 | class WXDLLEXPORT wxMenu; |
31 | class WXDLLEXPORT wxMenuBar; | |
32 | class WXDLLEXPORT wxDialog; | |
33 | class WXDLLEXPORT wxPanel; | |
34 | class WXDLLEXPORT wxWindow; | |
1486ed87 | 35 | class WXDLLEXPORT wxFrame; |
792064e9 | 36 | class WXDLLEXPORT wxToolBar; |
56d2f750 | 37 | |
ea89ec17 | 38 | class WXXMLDLLEXPORT wxXmlResourceHandler; |
56d2f750 | 39 | |
56d2f750 | 40 | |
1ce70313 VS |
41 | // These macros indicate current version of XML resources (this information is |
42 | // encoded in root node of XRC file as "version" property). | |
43 | // | |
44 | // Rules for increasing version number: | |
45 | // - change it only if you made incompatible change to the format. Addition of new | |
46 | // attribute to control handler is _not_ incompatible change, because older | |
47 | // versions of the library may ignore it. | |
48 | // - if you change version number, follow these steps: | |
49 | // - set major, minor and release numbers to respective version numbers of | |
50 | // the wxWindows library (see wx/version.h) | |
51 | // - reset revision to 0 unless the first three are same as before, in which | |
52 | // case you should increase revision by one | |
53 | #define WX_XMLRES_CURRENT_VERSION_MAJOR 2 | |
54 | #define WX_XMLRES_CURRENT_VERSION_MINOR 3 | |
55 | #define WX_XMLRES_CURRENT_VERSION_RELEASE 0 | |
56 | #define WX_XMLRES_CURRENT_VERSION_REVISION 1 | |
57 | #define WX_XMLRES_CURRENT_VERSION_STRING "2.3.0.1" | |
58 | ||
59 | #define WX_XMLRES_CURRENT_VERSION \ | |
60 | (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \ | |
61 | WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \ | |
62 | WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \ | |
63 | WX_XMLRES_CURRENT_VERSION_REVISION) | |
56d2f750 | 64 | |
ea89ec17 | 65 | class WXXMLDLLEXPORT wxXmlResourceDataRecord |
56d2f750 | 66 | { |
eb8671f2 VS |
67 | public: |
68 | wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {} | |
69 | ~wxXmlResourceDataRecord() {delete Doc;} | |
70 | ||
71 | wxString File; | |
72 | wxXmlDocument *Doc; | |
73 | wxDateTime Time; | |
56d2f750 VS |
74 | }; |
75 | ||
ea89ec17 RD |
76 | |
77 | #ifdef WXXMLISDLL | |
1ad83f6b | 78 | WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords); |
ea89ec17 RD |
79 | #else |
80 | WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords); | |
81 | #endif | |
56d2f750 | 82 | |
ea89ec17 RD |
83 | |
84 | // This class holds XML resources from one or more .xml files | |
56d2f750 | 85 | // (or derived forms, either binary or zipped -- see manual for |
ea89ec17 | 86 | // details). |
56d2f750 | 87 | |
ea89ec17 | 88 | class WXXMLDLLEXPORT wxXmlResource : public wxObject |
56d2f750 | 89 | { |
eb8671f2 VS |
90 | public: |
91 | // Ctor. If use_locale is TRUE, translatable strings are | |
92 | // translated via _(). You can disable it by passing use_locale=FALSE | |
93 | // (for example if you provide resource file for each locale) | |
94 | wxXmlResource(bool use_locale = TRUE); | |
95 | wxXmlResource(const wxString& filemask, bool use_locale = TRUE); | |
96 | ~wxXmlResource(); | |
97 | ||
98 | // Loads resources from XML files that match given filemask. | |
99 | // This method understands VFS (see filesys.h). | |
100 | bool Load(const wxString& filemask); | |
101 | ||
102 | // Initialize handlers for all supported controls/windows. This will | |
103 | // make the executable quite big because it forces linking against | |
104 | // most of wxWin library | |
105 | void InitAllHandlers(); | |
106 | ||
107 | // Initialize only specific handler (or custom handler). Convention says | |
108 | // that handler name is equal to control's name plus 'XmlHandler', e.g. | |
109 | // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler | |
ea89ec17 | 110 | // (xmlres) can create include file that contains initialization code for |
eb8671f2 VS |
111 | // all controls used within the resource. |
112 | void AddHandler(wxXmlResourceHandler *handler); | |
113 | ||
114 | // Removes all handlers | |
115 | void ClearHandlers(); | |
116 | ||
117 | // Loads menu from resource. Returns NULL on failure. | |
118 | wxMenu *LoadMenu(const wxString& name); | |
119 | ||
120 | // Loads menubar from resource. Returns NULL on failure. | |
121 | wxMenuBar *LoadMenuBar(const wxString& name); | |
56d2f750 | 122 | |
792064e9 | 123 | #if wxUSE_TOOLBAR |
eb8671f2 VS |
124 | // Loads toolbar |
125 | wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name); | |
792064e9 VS |
126 | #endif |
127 | ||
eb8671f2 VS |
128 | // Loads dialog. dlg points to parent window (if any). Second form |
129 | // is used to finish creation of already existing instance (main reason | |
130 | // for this is that you may want to use derived class with new event table) | |
131 | // Example (typical usage): | |
132 | // MyDialog dlg; | |
133 | // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog"); | |
134 | // dlg->ShowModal(); | |
135 | wxDialog *LoadDialog(wxWindow *parent, const wxString& name); | |
136 | bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name); | |
137 | ||
138 | // Loads panel. panel points to parent window (if any). Second form | |
139 | // is used to finish creation of already existing instance. | |
140 | wxPanel *LoadPanel(wxWindow *parent, const wxString& name); | |
141 | bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name); | |
142 | ||
143 | bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name); | |
144 | ||
145 | // Loads bitmap or icon resource from file: | |
146 | wxBitmap LoadBitmap(const wxString& name); | |
147 | wxIcon LoadIcon(const wxString& name); | |
ea89ec17 | 148 | |
e6c3f404 VS |
149 | // Attaches unknown control into given panel/window/dialog: |
150 | // (unknown controls are used in conjunction with <object class="unknown">) | |
151 | bool AttachUnknownControl(const wxString& name, wxWindow *control, | |
152 | wxWindow *parent = NULL); | |
eb8671f2 VS |
153 | |
154 | // Returns numeric ID that is equivalent to string id used in XML | |
155 | // resource. To be used in event tables | |
156 | // Macro XMLID is provided for convenience | |
00393283 | 157 | static int GetXMLID(const wxChar *str_id); |
ea89ec17 | 158 | |
1ce70313 VS |
159 | // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a) |
160 | long GetVersion() const { return m_version; } | |
ea89ec17 | 161 | |
1ce70313 VS |
162 | // Compares resources version to argument. Returns -1 if resources version |
163 | // is less than the argument, +1 if greater and 0 if they equal. | |
164 | int CompareVersion(int major, int minor, int release, int revision) const | |
ea89ec17 | 165 | { return GetVersion() - |
1ce70313 | 166 | (major*256*256*256 + minor*256*256 + release*256 + revision); } |
eb8671f2 VS |
167 | |
168 | protected: | |
169 | // Scans resources list for unloaded files and loads them. Also reloads | |
170 | // files that have been modified since last loading. | |
171 | void UpdateResources(); | |
172 | ||
173 | // Finds resource (calls UpdateResources) and returns node containing it | |
174 | wxXmlNode *FindResource(const wxString& name, const wxString& classname); | |
175 | ||
176 | // Creates resource from info in given node: | |
177 | wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL); | |
178 | ||
179 | // Remove nodes with property "platform" that does not | |
180 | // match current platform | |
181 | void ProcessPlatformProperty(wxXmlNode *node); | |
182 | ||
183 | bool GetUseLocale() { return m_useLocale; } | |
184 | ||
185 | private: | |
1ce70313 | 186 | long m_version; |
ea89ec17 | 187 | |
eb8671f2 VS |
188 | bool m_useLocale; |
189 | wxList m_handlers; | |
190 | wxXmlResourceDataRecords m_data; | |
792064e9 | 191 | #if wxUSE_FILESYSTEM |
eb8671f2 VS |
192 | wxFileSystem m_curFileSystem; |
193 | wxFileSystem& GetCurFileSystem() { return m_curFileSystem; } | |
792064e9 VS |
194 | #endif |
195 | ||
eb8671f2 | 196 | friend class wxXmlResourceHandler; |
56d2f750 VS |
197 | }; |
198 | ||
199 | ||
200 | // Global instance of resource class. For your convenience. | |
5ab8bd57 | 201 | extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource; |
56d2f750 VS |
202 | |
203 | // This macro translates string identifier (as used in XML resource, | |
204 | // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by | |
205 | // wxWindows event tables. | |
206 | // Example: | |
207 | // BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
208 | // EVT_MENU(XMLID("quit"), MyFrame::OnQuit) | |
209 | // EVT_MENU(XMLID("about"), MyFrame::OnAbout) | |
210 | // EVT_MENU(XMLID("new"), MyFrame::OnNew) | |
211 | // EVT_MENU(XMLID("open"), MyFrame::OnOpen) | |
ea89ec17 | 212 | // END_EVENT_TABLE() |
45a24817 VS |
213 | |
214 | #define XMLID(str_id) \ | |
a559d708 | 215 | wxXmlResource::GetXMLID(wxT(str_id)) |
45a24817 VS |
216 | |
217 | ||
45a24817 VS |
218 | // This macro returns pointer to particular control in dialog |
219 | // created using XML resources. You can use it to set/get values from | |
220 | // controls. | |
221 | // Example: | |
222 | // wxDialog dlg; | |
223 | // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog"); | |
a559d708 | 224 | // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value")); |
45a24817 | 225 | |
9c9fc51b VS |
226 | #ifdef __WXDEBUG__ |
227 | #define XMLCTRL(window, id, type) \ | |
228 | (wxDynamicCast((window).FindWindow(XMLID(id)), type)) | |
229 | #else | |
45a24817 VS |
230 | #define XMLCTRL(window, id, type) \ |
231 | ((type*)((window).FindWindow(XMLID(id)))) | |
9c9fc51b | 232 | #endif |
56d2f750 VS |
233 | |
234 | ||
ea89ec17 | 235 | class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject |
56d2f750 | 236 | { |
eb8671f2 VS |
237 | public: |
238 | wxXmlResourceHandler(); | |
239 | virtual ~wxXmlResourceHandler() {} | |
240 | ||
241 | // Creates object (menu, dialog, control, ...) from XML node. | |
242 | // Should check for validity. | |
243 | // parent is higher-level object (usually window, dialog or panel) | |
244 | // that is often neccessary to create resource | |
245 | // if instance != NULL it should not create new instance via 'new' but | |
246 | // rather use this one and call its Create method | |
ea89ec17 | 247 | wxObject *CreateResource(wxXmlNode *node, wxObject *parent, |
eb8671f2 VS |
248 | wxObject *instance); |
249 | ||
250 | // This one is called from CreateResource after variables | |
251 | // were filled | |
252 | virtual wxObject *DoCreateResource() = 0; | |
253 | ||
254 | // Returns TRUE if it understands this node and can create | |
255 | // resource from it, FALSE otherwise. | |
256 | virtual bool CanHandle(wxXmlNode *node) = 0; | |
257 | ||
258 | void SetParentResource(wxXmlResource *res) { m_resource = res; } | |
259 | ||
260 | ||
261 | protected: | |
262 | ||
263 | wxXmlResource *m_resource; | |
264 | wxArrayString m_styleNames; | |
265 | wxArrayInt m_styleValues; | |
266 | ||
267 | // Variables (filled by CreateResource) | |
268 | wxXmlNode *m_node; | |
269 | wxString m_class; | |
270 | wxObject *m_parent, *m_instance; | |
271 | wxWindow *m_parentAsWindow, *m_instanceAsWindow; | |
272 | ||
273 | // --- Handy methods: | |
274 | ||
275 | // Returns true if the node has property class equal to classname, | |
276 | // e.g. <object class="wxDialog"> | |
277 | bool IsOfClass(wxXmlNode *node, const wxString& classname) | |
278 | { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; } | |
279 | ||
280 | // Gets node content from wxXML_ENTITY_NODE | |
281 | // (the problem is, <tag>content<tag> is represented as | |
282 | // wxXML_ENTITY_NODE name="tag", content="" | |
ea89ec17 | 283 | // |-- wxXML_TEXT_NODE or |
eb8671f2 VS |
284 | // wxXML_CDATA_SECTION_NODE name="" content="content" |
285 | wxString GetNodeContent(wxXmlNode *node); | |
286 | ||
287 | // Check to see if a param exists | |
ea89ec17 | 288 | bool HasParam(const wxString& param); |
eb8671f2 VS |
289 | |
290 | // Finds the node or returns NULL | |
291 | wxXmlNode *GetParamNode(const wxString& param); | |
292 | wxString GetParamValue(const wxString& param); | |
293 | ||
294 | // Add style flag (e.g. wxMB_DOCKABLE) to list of flags | |
295 | // understood by this handler | |
296 | void AddStyle(const wxString& name, int value); | |
297 | ||
298 | // Add styles common to all wxWindow-derived classes | |
299 | void AddWindowStyles(); | |
300 | ||
301 | // Gets style flags from text in form "flag | flag2| flag3 |..." | |
302 | // Only understads flags added with AddStyle | |
303 | int GetStyle(const wxString& param = wxT("style"), int defaults = 0); | |
304 | ||
305 | // Gets text from param and does some convertions: | |
306 | // - replaces \n, \r, \t by respective chars (according to C syntax) | |
307 | // - replaces $ by & and $$ by $ (needed for $File => &File because of XML) | |
308 | // - calls wxGetTranslations (unless disabled in wxXmlResource) | |
309 | wxString GetText(const wxString& param); | |
310 | ||
311 | // Return XMLID | |
312 | int GetID(); | |
313 | wxString GetName(); | |
314 | ||
315 | // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE) | |
316 | bool GetBool(const wxString& param, bool defaultv = FALSE); | |
317 | ||
318 | // Get integer value from param | |
319 | long GetLong( const wxString& param, long defaultv = 0 ); | |
320 | ||
321 | // Get colour in HTML syntax (#RRGGBB) | |
322 | wxColour GetColour(const wxString& param); | |
323 | ||
324 | // Get size/position (may be in dlg units): | |
325 | wxSize GetSize(const wxString& param = wxT("size")); | |
326 | wxPoint GetPosition(const wxString& param = wxT("pos")); | |
327 | ||
328 | // Get dimension (may be in dlg units): | |
329 | wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0); | |
330 | ||
331 | // Get bitmap: | |
1ce70313 | 332 | wxBitmap GetBitmap(const wxString& param = wxT("bitmap"), |
eb8671f2 | 333 | wxSize size = wxDefaultSize); |
1ce70313 | 334 | wxIcon GetIcon(const wxString& param = wxT("icon"), |
eb8671f2 VS |
335 | wxSize size = wxDefaultSize); |
336 | ||
337 | // Get font: | |
338 | wxFont GetFont(const wxString& param = wxT("font")); | |
339 | ||
340 | // Sets common window options: | |
341 | void SetupWindow(wxWindow *wnd); | |
342 | ||
343 | void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE); | |
344 | void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL); | |
ea89ec17 | 345 | wxObject *CreateResFromNode(wxXmlNode *node, |
eb8671f2 VS |
346 | wxObject *parent, wxObject *instance = NULL) |
347 | { return m_resource->CreateResFromNode(node, parent, instance); } | |
348 | ||
349 | // helper | |
350 | wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); } | |
56d2f750 VS |
351 | }; |
352 | ||
a559d708 | 353 | #define ADD_STYLE(style) AddStyle(wxT(#style), style) |
56d2f750 VS |
354 | |
355 | ||
ea89ec17 | 356 | void wxXmlInitResourceModule(); |
56d2f750 VS |
357 | |
358 | #endif // _WX_XMLRES_H_ |