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