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