1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
15 #pragma interface "xmlres.h"
19 #include "wx/string.h"
20 #include "wx/dynarray.h"
21 #include "wx/datetime.h"
23 #include "wx/gdicmn.h"
24 #include "wx/filesys.h"
25 #include "wx/bitmap.h"
28 class WXDLLEXPORT wxMenu
;
29 class WXDLLEXPORT wxMenuBar
;
30 class WXDLLEXPORT wxDialog
;
31 class WXDLLEXPORT wxPanel
;
32 class WXDLLEXPORT wxWindow
;
33 class WXDLLEXPORT wxToolBar
;
35 class WXDLLEXPORT wxXmlResourceHandler
;
37 #include "wx/xml/xml.h"
40 class WXDLLEXPORT wxXmlResourceDataRecord
43 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
44 ~wxXmlResourceDataRecord() {delete Doc
;}
51 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
53 // This class holds XML resources from one or more .xml files
54 // (or derived forms, either binary or zipped -- see manual for
57 class WXDLLEXPORT wxXmlResource
: public wxObject
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
);
67 // Loads resources from XML files that match given filemask.
68 // This method understands VFS (see filesys.h).
69 bool Load(const wxString
& filemask
);
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();
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
);
83 // Removes all handlers
86 // Loads menu from resource. Returns NULL on failure.
87 wxMenu
*LoadMenu(const wxString
& name
);
89 // Loads menubar from resource. Returns NULL on failure.
90 wxMenuBar
*LoadMenuBar(const wxString
& name
);
94 wxToolBar
*LoadToolBar(wxWindow
*parent
, const wxString
& name
);
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):
102 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
104 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
105 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
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
);
112 // Loads bitmap or icon resource from file:
113 wxBitmap
LoadBitmap(const wxString
& name
);
114 wxIcon
LoadIcon(const wxString
& name
);
116 // Returns numeric ID that is equivalent to string id used in XML
117 // resource. To be used in event tables
118 // Macro XMLID is provided for convenience
119 static int GetXMLID(const char *str_id
);
122 // Scans resources list for unloaded files and loads them. Also reloads
123 // files that have been modified since last loading.
124 void UpdateResources();
126 // Finds resource (calls UpdateResources) and returns node containing it
127 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& classname
);
129 // Creates resource from info in given node:
130 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
132 // Remove nodes with property "platform" that does not
133 // match current platform
134 void ProcessPlatformProperty(wxXmlNode
*node
);
136 bool GetUseLocale() { return m_UseLocale
; }
141 wxXmlResourceDataRecords m_Data
;
143 wxFileSystem m_CurFileSystem
;
144 wxFileSystem
& GetCurFileSystem() { return m_CurFileSystem
; }
147 friend class wxXmlResourceHandler
;
151 // Global instance of resource class. For your convenience.
152 extern wxXmlResource
*wxTheXmlResource
;
154 // This macro translates string identifier (as used in XML resource,
155 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
156 // wxWindows event tables.
158 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
159 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
160 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
161 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
162 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
165 #define XMLID(str_id) \
166 wxXmlResource::GetXMLID(_T(str_id))
169 // This macro returns pointer to particular control in dialog
170 // created using XML resources. You can use it to set/get values from
174 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
175 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(_T("default value"));
177 #define XMLCTRL(window, id, type) \
178 ((type*)((window).FindWindow(XMLID(id))))
182 class WXDLLEXPORT wxXmlResourceHandler
: public wxObject
185 wxXmlResourceHandler();
186 virtual ~wxXmlResourceHandler() {}
188 // Creates object (menu, dialog, control, ...) from XML node.
189 // Should check for validity.
190 // parent is higher-level object (usually window, dialog or panel)
191 // that is often neccessary to create resource
192 // if instance != NULL it should not create new instance via 'new' but
193 // rather use this one and call its Create method
194 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
);
196 // This one is called from CreateResource after variables
198 virtual wxObject
*DoCreateResource() = 0;
200 // Returns TRUE if it understands this node and can create
201 // resource from it, FALSE otherwise.
202 virtual bool CanHandle(wxXmlNode
*node
) = 0;
204 void SetParentResource(wxXmlResource
*res
) { m_Resource
= res
; }
209 wxXmlResource
*m_Resource
;
210 wxArrayString m_StyleNames
;
211 wxArrayInt m_StyleValues
;
213 // Variables (filled by CreateResource)
216 wxObject
*m_Parent
, *m_Instance
;
217 wxWindow
*m_ParentAsWindow
, *m_InstanceAsWindow
;
219 // --- Handy methods:
221 // Returns true if the node has property class equal to classname,
222 // e.g. <object class="wxDialog">
223 bool IsOfClass(wxXmlNode
*node
, const wxString
& classname
)
224 { return node
->GetPropVal(_T("class"), wxEmptyString
) == classname
; }
226 // Gets node content from wxXML_ENTITY_NODE
227 // (the problem is, <tag>content<tag> is represented as
228 // wxXML_ENTITY_NODE name="tag", content=""
229 // |-- wxXML_TEXT_NODE or
230 // wxXML_CDATA_SECTION_NODE name="" content="content"
231 wxString
GetNodeContent(wxXmlNode
*node
);
233 // Check to see if a param exists
234 bool HasParam(const wxString
& param
);
236 // Finds the node or returns NULL
237 wxXmlNode
*GetParamNode(const wxString
& param
);
238 wxString
GetParamValue(const wxString
& param
);
240 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
241 // understood by this handler
242 void AddStyle(const wxString
& name
, int value
);
244 // Add styles common to all wxWindow-derived classes
245 void AddWindowStyles();
247 // Gets style flags from text in form "flag | flag2| flag3 |..."
248 // Only understads flags added with AddStyle
249 int GetStyle(const wxString
& param
= _T("style"), int defaults
= 0);
251 // Gets text from param and does some convertions:
252 // - replaces \n, \r, \t by respective chars (according to C syntax)
253 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
254 // - calls wxGetTranslations (unless disabled in wxXmlResource)
255 wxString
GetText(const wxString
& param
);
261 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
262 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
264 // Get integer value from param
265 long GetLong( const wxString
& param
, long defaultv
= 0 );
267 // Get colour in HTML syntax (#RRGGBB)
268 wxColour
GetColour(const wxString
& param
);
270 // Get size/position (may be in dlg units):
271 wxSize
GetSize(const wxString
& param
= _T("size"));
272 wxPoint
GetPosition(const wxString
& param
= _T("pos"));
274 // Get dimension (may be in dlg units):
275 wxCoord
GetDimension(const wxString
& param
, wxCoord defaultv
= 0);
278 wxBitmap
GetBitmap(const wxString
& param
= _T("bitmap"), wxSize size
= wxDefaultSize
);
279 wxIcon
GetIcon(const wxString
& param
= _T("icon"), wxSize size
= wxDefaultSize
);
282 wxFont
GetFont(const wxString
& param
= _T("font"));
284 // Sets common window options:
285 void SetupWindow(wxWindow
*wnd
);
287 void CreateChildren(wxObject
*parent
, bool this_hnd_only
= FALSE
);
288 void CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
= NULL
);
289 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
)
290 { return m_Resource
->CreateResFromNode(node
, parent
, instance
); }
293 wxFileSystem
& GetCurFileSystem() { return m_Resource
->GetCurFileSystem(); }
296 #define ADD_STYLE(style) AddStyle(_T(#style), style)
300 #endif // _WX_XMLRES_H_