]> git.saurik.com Git - wxWidgets.git/blob - include/wx/xrc/xmlres.h
fixed redraw problems in wxStaticText
[wxWidgets.git] / include / wx / xrc / 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 #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
84 // This class holds XML resources from one or more .xml files
85 // (or derived forms, either binary or zipped -- see manual for
86 // details).
87
88 class WXXMLDLLEXPORT wxXmlResource : public wxObject
89 {
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
110 // (xmlres) can create include file that contains initialization code for
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);
122
123 #if wxUSE_TOOLBAR
124 // Loads toolbar
125 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
126 #endif
127
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);
148
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);
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
157 static int GetXMLID(const wxChar *str_id);
158
159 // Returns version info (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a)
160 long GetVersion() const { return m_version; }
161
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
165 { return GetVersion() -
166 (major*256*256*256 + minor*256*256 + release*256 + revision); }
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 bool GetUseLocale() { return m_useLocale; }
180
181 private:
182 long m_version;
183
184 bool m_useLocale;
185 wxList m_handlers;
186 wxXmlResourceDataRecords m_data;
187 #if wxUSE_FILESYSTEM
188 wxFileSystem m_curFileSystem;
189 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
190 #endif
191
192 friend class wxXmlResourceHandler;
193 };
194
195
196 // Global instance of resource class. For your convenience.
197 extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource;
198
199 // This macro translates string identifier (as used in XML resource,
200 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
201 // wxWindows event tables.
202 // Example:
203 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
204 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
205 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
206 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
207 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
208 // END_EVENT_TABLE()
209
210 #define XMLID(str_id) \
211 wxXmlResource::GetXMLID(wxT(str_id))
212
213
214 // This macro returns pointer to particular control in dialog
215 // created using XML resources. You can use it to set/get values from
216 // controls.
217 // Example:
218 // wxDialog dlg;
219 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
220 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
221
222 #ifdef __WXDEBUG__
223 #define XMLCTRL(window, id, type) \
224 (wxDynamicCast((window).FindWindow(XMLID(id)), type))
225 #else
226 #define XMLCTRL(window, id, type) \
227 ((type*)((window).FindWindow(XMLID(id))))
228 #endif
229
230
231 class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
232 {
233 public:
234 wxXmlResourceHandler();
235 virtual ~wxXmlResourceHandler() {}
236
237 // Creates object (menu, dialog, control, ...) from XML node.
238 // Should check for validity.
239 // parent is higher-level object (usually window, dialog or panel)
240 // that is often neccessary to create resource
241 // if instance != NULL it should not create new instance via 'new' but
242 // rather use this one and call its Create method
243 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
244 wxObject *instance);
245
246 // This one is called from CreateResource after variables
247 // were filled
248 virtual wxObject *DoCreateResource() = 0;
249
250 // Returns TRUE if it understands this node and can create
251 // resource from it, FALSE otherwise.
252 virtual bool CanHandle(wxXmlNode *node) = 0;
253
254 void SetParentResource(wxXmlResource *res) { m_resource = res; }
255
256
257 protected:
258
259 wxXmlResource *m_resource;
260 wxArrayString m_styleNames;
261 wxArrayInt m_styleValues;
262
263 // Variables (filled by CreateResource)
264 wxXmlNode *m_node;
265 wxString m_class;
266 wxObject *m_parent, *m_instance;
267 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
268
269 // --- Handy methods:
270
271 // Returns true if the node has property class equal to classname,
272 // e.g. <object class="wxDialog">
273 bool IsOfClass(wxXmlNode *node, const wxString& classname)
274 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
275
276 // Gets node content from wxXML_ENTITY_NODE
277 // (the problem is, <tag>content<tag> is represented as
278 // wxXML_ENTITY_NODE name="tag", content=""
279 // |-- wxXML_TEXT_NODE or
280 // wxXML_CDATA_SECTION_NODE name="" content="content"
281 wxString GetNodeContent(wxXmlNode *node);
282
283 // Check to see if a param exists
284 bool HasParam(const wxString& param);
285
286 // Finds the node or returns NULL
287 wxXmlNode *GetParamNode(const wxString& param);
288 wxString GetParamValue(const wxString& param);
289
290 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
291 // understood by this handler
292 void AddStyle(const wxString& name, int value);
293
294 // Add styles common to all wxWindow-derived classes
295 void AddWindowStyles();
296
297 // Gets style flags from text in form "flag | flag2| flag3 |..."
298 // Only understads flags added with AddStyle
299 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
300
301 // Gets text from param and does some convertions:
302 // - replaces \n, \r, \t by respective chars (according to C syntax)
303 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
304 // - calls wxGetTranslations (unless disabled in wxXmlResource)
305 wxString GetText(const wxString& param);
306
307 // Return XMLID
308 int GetID();
309 wxString GetName();
310
311 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
312 bool GetBool(const wxString& param, bool defaultv = FALSE);
313
314 // Get integer value from param
315 long GetLong( const wxString& param, long defaultv = 0 );
316
317 // Get colour in HTML syntax (#RRGGBB)
318 wxColour GetColour(const wxString& param);
319
320 // Get size/position (may be in dlg units):
321 wxSize GetSize(const wxString& param = wxT("size"));
322 wxPoint GetPosition(const wxString& param = wxT("pos"));
323
324 // Get dimension (may be in dlg units):
325 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
326
327 // Get bitmap:
328 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
329 wxSize size = wxDefaultSize);
330 wxIcon GetIcon(const wxString& param = wxT("icon"),
331 wxSize size = wxDefaultSize);
332
333 // Get font:
334 wxFont GetFont(const wxString& param = wxT("font"));
335
336 // Sets common window options:
337 void SetupWindow(wxWindow *wnd);
338
339 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
340 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
341 wxObject *CreateResFromNode(wxXmlNode *node,
342 wxObject *parent, wxObject *instance = NULL)
343 { return m_resource->CreateResFromNode(node, parent, instance); }
344
345 // helper
346 #if wxUSE_FILESYSTEM
347 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
348 #endif
349 };
350
351 #define ADD_STYLE(style) AddStyle(wxT(#style), style)
352
353
354 void wxXmlInitResourceModule();
355
356 #endif // _WX_XMLRES_H_