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