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