]> git.saurik.com Git - wxWidgets.git/blob - contrib/include/wx/xml/xmlres.h
34df9e753840798eb4b322db2863d2605d2b909e
[wxWidgets.git] / contrib / include / wx / xml / 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 class WXDLLEXPORT wxMenu;
29 class WXDLLEXPORT wxMenuBar;
30 class WXDLLEXPORT wxDialog;
31 class WXDLLEXPORT wxPanel;
32 class WXDLLEXPORT wxWindow;
33 class WXDLLEXPORT wxToolBar;
34
35 class WXDLLEXPORT wxXmlResourceHandler;
36
37 #include "wx/xml/xml.h"
38
39
40 class WXDLLEXPORT wxXmlResourceDataRecord
41 {
42 public:
43 wxXmlResourceDataRecord() : Doc(NULL), Time(wxDateTime::Now()) {}
44 ~wxXmlResourceDataRecord() {delete Doc;}
45
46 wxString File;
47 wxXmlDocument *Doc;
48 wxDateTime Time;
49 };
50
51 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord, wxXmlResourceDataRecords);
52
53 // This class holds XML resources from one or more .xml files
54 // (or derived forms, either binary or zipped -- see manual for
55 // details).
56
57 class WXDLLEXPORT wxXmlResource : public wxObject
58 {
59 public:
60 wxXmlResource();
61 wxXmlResource(const wxString& filemask);
62 ~wxXmlResource();
63
64 // Loads resources from XML files that match given filemask.
65 // This method understands VFS (see filesys.h).
66 bool Load(const wxString& filemask);
67
68 // Initialize handlers for all supported controls/windows. This will
69 // make the executable quite big because it forces linking against
70 // most of wxWin library
71 void InitAllHandlers();
72
73 // Initialize only specific handler (or custom handler). Convention says
74 // that handler name is equal to control's name plus 'XmlHandler', e.g.
75 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
76 // (xmlres) can create include file that contains initialization code for
77 // all controls used within the resource.
78 void AddHandler(wxXmlResourceHandler *handler);
79
80 // Removes all handlers
81 void ClearHandlers();
82
83 // Loads menu from resource. Returns NULL on failure.
84 wxMenu *LoadMenu(const wxString& name);
85
86 // Loads menubar from resource. Returns NULL on failure.
87 wxMenuBar *LoadMenuBar(const wxString& name);
88
89 #if wxUSE_TOOLBAR
90 // Loads toolbar
91 wxToolBar *LoadToolBar(wxWindow *parent, const wxString& name);
92 #endif
93
94 // Loads dialog. dlg points to parent window (if any). Second form
95 // is used to finish creation of already existing instance (main reason
96 // for this is that you may want to use derived class with new event table)
97 // Example (typical usage):
98 // MyDialog dlg;
99 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
100 // dlg->ShowModal();
101 wxDialog *LoadDialog(wxWindow *parent, const wxString& name);
102 bool LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name);
103
104 // Loads panel. panel points to parent window (if any). Second form
105 // is used to finish creation of already existing instance.
106 wxPanel *LoadPanel(wxWindow *parent, const wxString& name);
107 bool LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name);
108
109 // Returns numeric ID that is equivalent to string id used in XML
110 // resource. To be used in event tables
111 // Macro XMLID is provided for convenience
112 static int GetXMLID(const char *str_id);
113
114 protected:
115 // Scans resources list for unloaded files and loads them. Also reloads
116 // files that have been modified since last loading.
117 void UpdateResources();
118
119 // Finds resource (calls UpdateResources) and returns node containing it
120 wxXmlNode *FindResource(const wxString& name, const wxString& type);
121
122 // Creates resource from info in given node:
123 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL);
124
125 // Remove nodes with property "platform" that does not
126 // match current platform
127 void ProcessPlatformProperty(wxXmlNode *node);
128
129 private:
130 wxList m_Handlers;
131 wxXmlResourceDataRecords m_Data;
132 #if wxUSE_FILESYSTEM
133 wxFileSystem m_CurFileSystem;
134 wxFileSystem& GetCurFileSystem() { return m_CurFileSystem; }
135 #endif
136
137 friend class wxXmlResourceHandler;
138 };
139
140
141 // Global instance of resource class. For your convenience.
142 extern wxXmlResource *wxTheXmlResource;
143
144 // This macro translates string identifier (as used in XML resource,
145 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
146 // wxWindows event tables.
147 // Example:
148 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
149 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
150 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
151 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
152 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
153 // END_EVENT_TABLE()
154
155 #define XMLID(str_id) \
156 wxXmlResource::GetXMLID(_T(str_id))
157
158
159
160 // This macro returns pointer to particular control in dialog
161 // created using XML resources. You can use it to set/get values from
162 // controls.
163 // Example:
164 // wxDialog dlg;
165 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
166 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(_T("default value"));
167
168 #define XMLCTRL(window, id, type) \
169 ((type*)((window).FindWindow(XMLID(id))))
170
171
172
173 class WXDLLEXPORT wxXmlResourceHandler : public wxObject
174 {
175 public:
176 wxXmlResourceHandler();
177 virtual ~wxXmlResourceHandler() {}
178
179 // Creates object (menu, dialog, control, ...) from XML node.
180 // Should check for validity.
181 // parent is higher-level object (usually window, dialog or panel)
182 // that is often neccessary to create resource
183 // if instance != NULL it should not create new instance via 'new' but
184 // rather use this one and call its Create method
185 wxObject *CreateResource(wxXmlNode *node, wxObject *parent, wxObject *instance);
186
187 // This one is called from CreateResource after variables
188 // were filled
189 virtual wxObject *DoCreateResource() = 0;
190
191 // Returns TRUE if it understands this node and can create
192 // resource from it, FALSE otherwise.
193 virtual bool CanHandle(wxXmlNode *node) = 0;
194
195 void SetParentResource(wxXmlResource *res) { m_Resource = res; }
196
197
198 protected:
199
200 wxXmlResource *m_Resource;
201 wxArrayString m_StyleNames;
202 wxArrayInt m_StyleValues;
203
204 // Variables (filled by CreateResource)
205 wxXmlNode *m_Node;
206 wxObject *m_Parent, *m_Instance;
207 wxWindow *m_ParentAsWindow, *m_InstanceAsWindow;
208
209 // --- Handy methods:
210
211 // Gets node content from wxXML_ENTITY_NODE
212 // (the problem is, <tag>content<tag> is represented as
213 // wxXML_ENTITY_NODE name="tag", content=""
214 // |-- wxXML_TEXT_NODE or
215 // wxXML_CDATA_SECTION_NODE name="" content="content"
216 wxString GetNodeContent(wxXmlNode *node);
217
218 // Check to see if a param exists
219 bool HasParam(const wxString& param);
220
221 // Finds the node or returns NULL
222 wxXmlNode *GetParamNode(const wxString& param);
223 wxString GetParamValue(const wxString& param);
224
225 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
226 // understood by this handler
227 void AddStyle(const wxString& name, int value);
228
229 // Gets style flags from text in form "flag | flag2| flag3 |..."
230 // Only understads flags added with AddStyle
231 int GetStyle(const wxString& param = _T("style"), int defaults = 0);
232
233 // Gets text from param and does some convertions:
234 // - replaces \n, \r, \t by respective chars (according to C syntax)
235 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
236 // - converts encodings if neccessary
237 wxString GetText(const wxString& param);
238
239 // Return XMLID
240 int GetID();
241 wxString GetName();
242
243 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
244 bool GetBool(const wxString& param, bool defaultv = FALSE);
245
246 // Get integer value from param
247 long GetLong( const wxString& param, long defaultv = 0 );
248
249 // Get colour in HTML syntax (#RRGGBB)
250 wxColour GetColour(const wxString& param);
251
252 // Get size/position:
253 wxSize GetSize(const wxString& param = _T("size"));
254 wxPoint GetPosition(const wxString& param = _T("pos"));
255
256 // Get bitmap:
257 wxBitmap GetBitmap(const wxString& param = _T("bitmap"), wxSize size = wxDefaultSize);
258 wxIcon GetIcon(const wxString& param = _T("icon"), wxSize size = wxDefaultSize);
259
260 // Sets common window options:
261 void SetupWindow(wxWindow *wnd);
262
263 void CreateChildren(wxObject *parent, bool only_this_handler = FALSE,
264 wxXmlNode *children_node = NULL /*stands for
265 GetParamNode("children")*/);
266 wxObject *CreateResFromNode(wxXmlNode *node, wxObject *parent, wxObject *instance = NULL)
267 { return m_Resource->CreateResFromNode(node, parent, instance); }
268
269 // helper
270 wxFileSystem& GetCurFileSystem() { return m_Resource->GetCurFileSystem(); }
271 };
272
273 #define ADD_STYLE(style) AddStyle(_T(#style), style)
274
275
276
277 #endif // _WX_XMLRES_H_