]> git.saurik.com Git - wxWidgets.git/blame - include/wx/xrc/xmlres.h
implement wxHAVE_RAW_BITMAP
[wxWidgets.git] / include / wx / xrc / 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
ab7ce33c 14#if defined(__GNUG__) && !defined(__APPLE__)
56d2f750
VS
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"
af1337b0 27#include "wx/artprov.h"
56d2f750 28
999d9a9f 29#include "wx/xrc/xml.h"
ea89ec17 30
1ad83f6b
VS
31class WXDLLEXPORT wxMenu;
32class WXDLLEXPORT wxMenuBar;
33class WXDLLEXPORT wxDialog;
34class WXDLLEXPORT wxPanel;
35class WXDLLEXPORT wxWindow;
1486ed87 36class WXDLLEXPORT wxFrame;
792064e9 37class WXDLLEXPORT wxToolBar;
56d2f750 38
ea89ec17 39class WXXMLDLLEXPORT wxXmlResourceHandler;
2b5f62a0
VZ
40class WXXMLDLLEXPORT wxXmlSubclassFactory;
41class WXXMLDLLEXPORT wxXmlSubclassFactoriesList;
42class wxXmlResourceModule;
56d2f750 43
56d2f750 44
1ce70313
VS
45// These macros indicate current version of XML resources (this information is
46// encoded in root node of XRC file as "version" property).
47//
48// Rules for increasing version number:
49// - change it only if you made incompatible change to the format. Addition of new
50// attribute to control handler is _not_ incompatible change, because older
51// versions of the library may ignore it.
52// - if you change version number, follow these steps:
53// - set major, minor and release numbers to respective version numbers of
54// the wxWindows library (see wx/version.h)
55// - reset revision to 0 unless the first three are same as before, in which
56// case you should increase revision by one
57#define WX_XMLRES_CURRENT_VERSION_MAJOR 2
58#define WX_XMLRES_CURRENT_VERSION_MINOR 3
59#define WX_XMLRES_CURRENT_VERSION_RELEASE 0
60#define WX_XMLRES_CURRENT_VERSION_REVISION 1
437bd215 61#define WX_XMLRES_CURRENT_VERSION_STRING _T("2.3.0.1")
1ce70313
VS
62
63#define WX_XMLRES_CURRENT_VERSION \
64 (WX_XMLRES_CURRENT_VERSION_MAJOR * 256*256*256 + \
65 WX_XMLRES_CURRENT_VERSION_MINOR * 256*256 + \
66 WX_XMLRES_CURRENT_VERSION_RELEASE * 256 + \
67 WX_XMLRES_CURRENT_VERSION_REVISION)
56d2f750 68
ea89ec17 69class WXXMLDLLEXPORT wxXmlResourceDataRecord
56d2f750 70{
eb8671f2
VS
71public:
72 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
73 ~wxXmlResourceDataRecord() {delete Doc;}
74
75 wxString File;
76 wxXmlDocument *Doc;
77 wxDateTime Time;
56d2f750
VS
78};
79
ea89ec17
RD
80
81#ifdef WXXMLISDLL
1ad83f6b 82WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
ea89ec17
RD
83#else
84WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
85#endif
56d2f750 86
daa85ee3
VS
87enum wxXmlResourceFlags
88{
89 wxXRC_USE_LOCALE = 1,
90 wxXRC_NO_SUBCLASSING = 2
91};
ea89ec17
RD
92
93// This class holds XML resources from one or more .xml files
56d2f750 94// (or derived forms, either binary or zipped -- see manual for
ea89ec17 95// details).
ea89ec17 96class WXXMLDLLEXPORT wxXmlResource : public wxObject
56d2f750 97{
eb8671f2 98public:
273b399f 99 // Constructor.
daa85ee3
VS
100 // Flags: wxXRC_USE_LOCALE
101 // translatable strings will be translated via _()
102 // wxXRC_NO_SUBCLASSING
103 // subclass property of object nodes will be ignored
104 // (useful for previews in XRC editors)
105 wxXmlResource(int flags = wxXRC_USE_LOCALE);
273b399f
JS
106
107 // Constructor.
108 // Flags: wxXRC_USE_LOCALE
109 // translatable strings will be translated via _()
110 // wxXRC_NO_SUBCLASSING
111 // subclass property of object nodes will be ignored
112 // (useful for previews in XRC editors)
daa85ee3 113 wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE);
273b399f
JS
114
115 // Destructor.
eb8671f2 116 ~wxXmlResource();
92e898b0 117
eb8671f2
VS
118 // Loads resources from XML files that match given filemask.
119 // This method understands VFS (see filesys.h).
120 bool Load(const wxString& filemask);
121
122 // Initialize handlers for all supported controls/windows. This will
123 // make the executable quite big because it forces linking against
273b399f 124 // most of the wxWindows library.
eb8671f2
VS
125 void InitAllHandlers();
126
273b399f
JS
127 // Initialize only a specific handler (or custom handler). Convention says
128 // that handler name is equal to the control's name plus 'XmlHandler', for example
129 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. The XML resource compiler
ea89ec17 130 // (xmlres) can create include file that contains initialization code for
eb8671f2
VS
131 // all controls used within the resource.
132 void AddHandler(wxXmlResourceHandler *handler);
133
92e898b0
RD
134 // Add a new handler at the begining of the handler list
135 void InsertHandler(wxXmlResourceHandler *handler);
136
eb8671f2
VS
137 // Removes all handlers
138 void ClearHandlers();
2b5f62a0
VZ
139
140 // Registers subclasses factory for use in XRC. This function is not meant
141 // for public use, please see the comment above wxXmlSubclassFactory
142 // definition.
143 static void AddSubclassFactory(wxXmlSubclassFactory *factory);
eb8671f2
VS
144
145 // Loads menu from resource. Returns NULL on failure.
146 wxMenu *LoadMenu(const wxString& name);
147
148 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 149 wxMenuBar *LoadMenuBar(wxWindow *parent, const wxString& name);
273b399f
JS
150
151 // Loads menubar from resource. Returns NULL on failure.
4a1b9596 152 wxMenuBar *LoadMenuBar(const wxString& name) { return LoadMenuBar(NULL, name); }
56d2f750 153
792064e9 154#if wxUSE_TOOLBAR
273b399f 155 // Loads a toolbar.
eb8671f2 156 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
792064e9
VS
157#endif
158
273b399f
JS
159 // Loads a dialog. dlg points to parent window (if any).
160 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
161
162 // Loads a dialog. dlg points to parent window (if any). This form
eb8671f2
VS
163 // is used to finish creation of already existing instance (main reason
164 // for this is that you may want to use derived class with new event table)
165 // Example (typical usage):
166 // MyDialog dlg;
167 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
168 // dlg->ShowModal();
eb8671f2
VS
169 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
170
273b399f 171 // Loads a panel. panel points to parent window (if any).
eb8671f2 172 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
273b399f
JS
173
174 // Loads a panel. panel points to parent window (if any). This form
175 // is used to finish creation of already existing instance.
eb8671f2
VS
176 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
177
273b399f 178 // Loads a frame.
92e898b0 179 wxFrame *LoadFrame(wxWindow* parent, const wxString& name);
eb8671f2
VS
180 bool LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name);
181
92e898b0
RD
182 // Load an object from the resource specifying both the resource name and
183 // the classname. This lets you load nonstandard container windows.
184 wxObject *LoadObject(wxWindow *parent, const wxString& name,
185 const wxString& classname);
186
187 // Load an object from the resource specifying both the resource name and
188 // the classname. This form lets you finish the creation of an existing
189 // instance.
190 bool LoadObject(wxObject *instance, wxWindow *parent, const wxString& name,
191 const wxString& classname);
192
273b399f 193 // Loads a bitmap resource from a file.
eb8671f2 194 wxBitmap LoadBitmap(const wxString& name);
273b399f
JS
195
196 // Loads an icon resource from a file.
eb8671f2 197 wxIcon LoadIcon(const wxString& name);
ea89ec17 198
273b399f
JS
199 // Attaches an unknown control to the given panel/window/dialog.
200 // Unknown controls are used in conjunction with <object class="unknown">.
e6c3f404
VS
201 bool AttachUnknownControl(const wxString& name, wxWindow *control,
202 wxWindow *parent = NULL);
eb8671f2 203
273b399f
JS
204 // Returns a numeric ID that is equivalent to the string id used in an XML
205 // resource. To be used in event tables.
5ed345b7
VS
206 // Macro XRCID is provided for convenience
207 static int GetXRCID(const wxChar *str_id);
ea89ec17 208
273b399f 209 // Returns version information (a.b.c.d = d+ 256*c + 256^2*b + 256^3*a).
1ce70313 210 long GetVersion() const { return m_version; }
ea89ec17 211
1ce70313
VS
212 // Compares resources version to argument. Returns -1 if resources version
213 // is less than the argument, +1 if greater and 0 if they equal.
214 int CompareVersion(int major, int minor, int release, int revision) const
ea89ec17 215 { return GetVersion() -
1ce70313 216 (major*256*256*256 + minor*256*256 + release*256 + revision); }
92e898b0 217
273b399f 218//// Singleton accessors.
92e898b0 219
273b399f 220 // Gets the global resources object or creates one if none exists.
824e8eaa 221 static wxXmlResource *Get();
273b399f
JS
222
223 // Sets the global resources object and returns a pointer to the previous one (may be NULL).
824e8eaa 224 static wxXmlResource *Set(wxXmlResource *res);
eb8671f2 225
74c107ba 226 // Returns flags, which may be a bitlist of wxXRC_USE_LOCALE and wxXRC_NO_SUBCLASSING.
2b5f62a0
VZ
227 int GetFlags() const { return m_flags; }
228 // Set flags after construction.
229 void SetFlags(int flags) { m_flags = flags; }
74c107ba 230
eb8671f2 231protected:
273b399f 232 // Scans the resources list for unloaded files and loads them. Also reloads
eb8671f2
VS
233 // files that have been modified since last loading.
234 void UpdateResources();
235
273b399f 236 // Finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 237 wxXmlNode *FindResource(const wxString& name, const wxString& classname, bool recursive = FALSE);
273b399f
JS
238
239 // Helper function: finds a resource (calls UpdateResources) and returns a node containing it.
47793ab8 240 wxXmlNode *DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive);
eb8671f2 241
e5db1609
VS
242 // Creates a resource from information in the given node
243 // (Uses only 'handlerToUse' if != NULL)
244 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent,
245 wxObject *instance = NULL,
246 wxXmlResourceHandler *handlerToUse = NULL);
eb8671f2 247
eb8671f2 248private:
1ce70313 249 long m_version;
ea89ec17 250
daa85ee3 251 int m_flags;
eb8671f2
VS
252 wxList m_handlers;
253 wxXmlResourceDataRecords m_data;
792064e9 254#if wxUSE_FILESYSTEM
eb8671f2
VS
255 wxFileSystem m_curFileSystem;
256 wxFileSystem& GetCurFileSystem() { return m_curFileSystem; }
792064e9
VS
257#endif
258
eb8671f2 259 friend class wxXmlResourceHandler;
2b5f62a0
VZ
260 friend class wxXmlResourceModule;
261
262 static wxXmlSubclassFactoriesList *ms_subclassFactories;
92e898b0 263
824e8eaa
VS
264 // singleton instance:
265 static wxXmlResource *ms_instance;
56d2f750
VS
266};
267
268
56d2f750
VS
269// This macro translates string identifier (as used in XML resource,
270// e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
271// wxWindows event tables.
272// Example:
273// BEGIN_EVENT_TABLE(MyFrame, wxFrame)
5ed345b7
VS
274// EVT_MENU(XRCID("quit"), MyFrame::OnQuit)
275// EVT_MENU(XRCID("about"), MyFrame::OnAbout)
276// EVT_MENU(XRCID("new"), MyFrame::OnNew)
277// EVT_MENU(XRCID("open"), MyFrame::OnOpen)
ea89ec17 278// END_EVENT_TABLE()
45a24817 279
5ed345b7
VS
280#define XRCID(str_id) \
281 wxXmlResource::GetXRCID(wxT(str_id))
45a24817
VS
282
283
45a24817
VS
284// This macro returns pointer to particular control in dialog
285// created using XML resources. You can use it to set/get values from
286// controls.
287// Example:
288// wxDialog dlg;
546b0006 289// wxXmlResource::Get()->LoadDialog(&dlg, mainFrame, "my_dialog");
5ed345b7 290// XRCCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(wxT("default value"));
45a24817 291
9c9fc51b 292#ifdef __WXDEBUG__
5ed345b7
VS
293#define XRCCTRL(window, id, type) \
294 (wxDynamicCast((window).FindWindow(XRCID(id)), type))
9c9fc51b 295#else
5ed345b7
VS
296#define XRCCTRL(window, id, type) \
297 ((type*)((window).FindWindow(XRCID(id))))
9c9fc51b 298#endif
56d2f750 299
273b399f
JS
300// wxXmlResourceHandler is an abstract base class for resource handlers
301// capable of creating a control from an XML node.
56d2f750 302
ea89ec17 303class WXXMLDLLEXPORT wxXmlResourceHandler : public wxObject
56d2f750 304{
eb8671f2 305public:
273b399f 306 // Constructor.
eb8671f2 307 wxXmlResourceHandler();
273b399f
JS
308
309 // Destructor.
eb8671f2
VS
310 virtual ~wxXmlResourceHandler() {}
311
273b399f 312 // Creates an object (menu, dialog, control, ...) from an XML node.
eb8671f2 313 // Should check for validity.
273b399f
JS
314 // parent is a higher-level object (usually window, dialog or panel)
315 // that is often neccessary to create the resource.
316 // If instance is non-NULL it should not create a new instance via 'new' but
317 // should rather use this one, and call its Create method.
ea89ec17 318 wxObject *CreateResource(wxXmlNode *node, wxObject *parent,
eb8671f2
VS
319 wxObject *instance);
320
321 // This one is called from CreateResource after variables
273b399f 322 // were filled.
eb8671f2
VS
323 virtual wxObject *DoCreateResource() = 0;
324
325 // Returns TRUE if it understands this node and can create
273b399f 326 // a resource from it, FALSE otherwise.
eb8671f2
VS
327 virtual bool CanHandle(wxXmlNode *node) = 0;
328
273b399f 329 // Sets the parent resource.
eb8671f2
VS
330 void SetParentResource(wxXmlResource *res) { m_resource = res; }
331
eb8671f2 332protected:
eb8671f2
VS
333 wxXmlResource *m_resource;
334 wxArrayString m_styleNames;
335 wxArrayInt m_styleValues;
336
337 // Variables (filled by CreateResource)
338 wxXmlNode *m_node;
339 wxString m_class;
340 wxObject *m_parent, *m_instance;
341 wxWindow *m_parentAsWindow, *m_instanceAsWindow;
342
343 // --- Handy methods:
344
273b399f
JS
345 // Returns true if the node has a property class equal to classname,
346 // e.g. <object class="wxDialog">.
eb8671f2
VS
347 bool IsOfClass(wxXmlNode *node, const wxString& classname)
348 { return node->GetPropVal(wxT("class"), wxEmptyString) == classname; }
349
350 // Gets node content from wxXML_ENTITY_NODE
273b399f 351 // The problem is, <tag>content<tag> is represented as
eb8671f2 352 // wxXML_ENTITY_NODE name="tag", content=""
ea89ec17 353 // |-- wxXML_TEXT_NODE or
eb8671f2
VS
354 // wxXML_CDATA_SECTION_NODE name="" content="content"
355 wxString GetNodeContent(wxXmlNode *node);
356
273b399f 357 // Check to see if a parameter exists.
ea89ec17 358 bool HasParam(const wxString& param);
eb8671f2 359
273b399f 360 // Finds the node or returns NULL.
eb8671f2 361 wxXmlNode *GetParamNode(const wxString& param);
273b399f
JS
362
363 // Finds the parameter value or returns the empty string.
eb8671f2
VS
364 wxString GetParamValue(const wxString& param);
365
273b399f
JS
366 // Add a style flag (e.g. wxMB_DOCKABLE) to the list of flags
367 // understood by this handler.
eb8671f2
VS
368 void AddStyle(const wxString& name, int value);
369
273b399f 370 // Add styles common to all wxWindow-derived classes.
eb8671f2
VS
371 void AddWindowStyles();
372
373 // Gets style flags from text in form "flag | flag2| flag3 |..."
374 // Only understads flags added with AddStyle
375 int GetStyle(const wxString& param = wxT("style"), int defaults = 0);
376
273b399f 377 // Gets text from param and does some conversions:
eb8671f2 378 // - replaces \n, \r, \t by respective chars (according to C syntax)
ee1046d1 379 // - replaces _ by & and __ by _ (needed for _File => &File because of XML)
eb8671f2 380 // - calls wxGetTranslations (unless disabled in wxXmlResource)
ee1046d1 381 wxString GetText(const wxString& param, bool translate = TRUE);
eb8671f2 382
5ed345b7 383 // Returns the XRCID.
eb8671f2 384 int GetID();
273b399f
JS
385
386 // Returns the resource name.
eb8671f2
VS
387 wxString GetName();
388
273b399f 389 // Gets a bool flag (1, t, yes, on, true are TRUE, everything else is FALSE).
eb8671f2
VS
390 bool GetBool(const wxString& param, bool defaultv = FALSE);
391
273b399f 392 // Gets the integer value from the parameter.
eb8671f2
VS
393 long GetLong( const wxString& param, long defaultv = 0 );
394
273b399f 395 // Gets colour in HTML syntax (#RRGGBB).
eb8671f2
VS
396 wxColour GetColour(const wxString& param);
397
273b399f 398 // Gets the size (may be in dialog units).
eb8671f2 399 wxSize GetSize(const wxString& param = wxT("size"));
273b399f
JS
400
401 // Gets the position (may be in dialog units).
eb8671f2
VS
402 wxPoint GetPosition(const wxString& param = wxT("pos"));
403
273b399f 404 // Gets a dimension (may be in dialog units).
eb8671f2
VS
405 wxCoord GetDimension(const wxString& param, wxCoord defaultv = 0);
406
273b399f 407 // Gets a bitmap.
1ce70313 408 wxBitmap GetBitmap(const wxString& param = wxT("bitmap"),
7a442f31 409 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2 410 wxSize size = wxDefaultSize);
273b399f
JS
411
412 // Gets an icon.
1ce70313 413 wxIcon GetIcon(const wxString& param = wxT("icon"),
7a442f31 414 const wxArtClient& defaultArtClient = wxART_OTHER,
eb8671f2
VS
415 wxSize size = wxDefaultSize);
416
273b399f 417 // Gets a font.
eb8671f2
VS
418 wxFont GetFont(const wxString& param = wxT("font"));
419
273b399f 420 // Sets common window options.
eb8671f2
VS
421 void SetupWindow(wxWindow *wnd);
422
273b399f 423 // Creates children.
eb8671f2 424 void CreateChildren(wxObject *parent, bool this_hnd_only = FALSE);
273b399f
JS
425
426 // Helper function.
eb8671f2 427 void CreateChildrenPrivately(wxObject *parent, wxXmlNode *rootnode = NULL);
273b399f
JS
428
429 // Creates a resource from a node.
ea89ec17 430 wxObject *CreateResFromNode(wxXmlNode *node,
eb8671f2
VS
431 wxObject *parent, wxObject *instance = NULL)
432 { return m_resource->CreateResFromNode(node, parent, instance); }
433
434 // helper
150e58d8 435#if wxUSE_FILESYSTEM
eb8671f2 436 wxFileSystem& GetCurFileSystem() { return m_resource->GetCurFileSystem(); }
150e58d8 437#endif
56d2f750
VS
438};
439
daa85ee3
VS
440
441// Programmer-friendly macros for writing XRC handlers:
442
443#define XRC_ADD_STYLE(style) AddStyle(wxT(#style), style)
daa85ee3
VS
444
445#define XRC_MAKE_INSTANCE(variable, classname) \
446 classname *variable = NULL; \
447 if (m_instance) \
448 variable = wxStaticCast(m_instance, classname); \
449 if (!variable) \
450 variable = new classname;
56d2f750
VS
451
452
daa85ee3 453// FIXME -- remove this $%^#$%#$@# as soon as Ron checks his changes in!!
ea89ec17 454void wxXmlInitResourceModule();
56d2f750 455
546b0006 456
2b5f62a0
VZ
457// This class is used to create instances of XRC "object" nodes with "subclass"
458// property. It is _not_ supposed to be used by XRC users, you should instead
459// register your subclasses via wxWindows' RTTI mechanism. This class is useful
460// only for language bindings developer who need a way to implement subclassing
461// in wxWindows ports that don't support wxRTTI (e.g. wxPython).
462class WXXMLDLLEXPORT wxXmlSubclassFactory
463{
464public:
465 // Try to create instance of given class and return it, return NULL on failure:
466 virtual wxObject *Create(const wxString& className) = 0;
467 virtual ~wxXmlSubclassFactory() {}
468};
469
470
92e898b0 471/* -------------------------------------------------------------------------
546b0006
VS
472 Backward compatibility macros. Do *NOT* use, they may disappear in future
473 versions of the XRC library!
474 ------------------------------------------------------------------------- */
cf21abe5
VS
475#if WXWIN_COMPATIBILITY_2_4
476 #define ADD_STYLE XRC_ADD_STYLE
477 #define wxTheXmlResource wxXmlResource::Get()
478 #define XMLID XRCID
479 #define XMLCTRL XRCCTRL
480 #define GetXMLID GetXRCID
481#endif
546b0006
VS
482
483
56d2f750 484#endif // _WX_XMLRES_H_