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