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"
31 class wxXmlResourceHandler
;
33 #include "wx/xml/xml.h"
43 class WXDLLEXPORT wxXmlResourceDataRecord
46 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
47 ~wxXmlResourceDataRecord() {delete Doc
;}
54 WX_DECLARE_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
56 // This class holds XML resources from one or more .xml files
57 // (or derived forms, either binary or zipped -- see manual for
60 class WXDLLEXPORT wxXmlResource
: public wxObject
64 wxXmlResource(const wxString
& filemask
, int type
);
67 // Loads resources from XML files that match given filemask.
68 // This method understands VFS (see filesys.h). Type is one of
69 // wxXML_TEXT, wxXML_BINARY, wxXML_ARCHIVE and specifies type of
70 // data to be expected:
71 // wxXML_BINARY - binary version of .xml file, as produced
72 // by wxXmlDocument::SaveBinary
73 // wxXML_ARCHIVE - ZIP archive that contains arbitrary number
74 // of files with .xmb extension
75 // (this kind of ZIP archive is produced by
76 // XML resources compiler that ships with wxWin)
77 bool Load(const wxString
& filemask
, int type
= wxXML_ARCHIVE
);
79 // Initialize handlers for all supported controls/windows. This will
80 // make the executable quite big because it forces linking against
81 // most of wxWin library
82 void InitAllHandlers();
84 // Initialize only specific handler (or custom handler). Convention says
85 // that handler name is equal to control's name plus 'XmlHandler', e.g.
86 // wxTextCtrlXmlHandler, wxHtmlWindowXmlHandler. XML resource compiler
87 // (xmlres) can create include file that contains initialization code for
88 // all controls used within the resource.
89 void AddHandler(wxXmlResourceHandler
*handler
);
91 // Removes all handlers
94 // Loads menu from resource. Returns NULL on failure.
95 wxMenu
*LoadMenu(const wxString
& name
);
97 // Loads menubar from resource. Returns NULL on failure.
98 wxMenuBar
*LoadMenuBar(const wxString
& name
);
100 // Loads dialog. dlg points to parent window (if any). Second form
101 // is used to finish creation of already existing instance (main reason
102 // for this is that you may want to use derived class with new event table)
103 // Example (typical usage):
105 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
107 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
108 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
110 // Loads panel. panel points to parent window (if any). Second form
111 // is used to finish creation of already existing instance.
112 wxPanel
*LoadPanel(wxWindow
*parent
, const wxString
& name
);
113 bool LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
);
115 // Returns numeric ID that is equivalent to string id used in XML
116 // resource. To be used in event tables
117 // Macro XMLID is provided for convenience
118 static int GetXMLID(const char *str_id
);
121 // Scans resources list for unloaded files and loads them. Also reloads
122 // files that have been modified since last loading.
123 void UpdateResources();
125 // Finds resource (calls UpdateResources) and returns node containing it
126 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& type
);
128 // Creates resource from info in given node:
129 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
133 wxXmlResourceDataRecords m_Data
;
135 friend class wxXmlResourceHandler
;
139 // Global instance of resource class. For your convenience.
140 extern wxXmlResource
*wxTheXmlResource
;
142 // This macro translates string identifier (as used in XML resource,
143 // e.g. <menuitem id="my_menu">...</menuitem>) to integer id that is needed by
144 // wxWindows event tables.
146 // BEGIN_EVENT_TABLE(MyFrame, wxFrame)
147 // EVT_MENU(XMLID("quit"), MyFrame::OnQuit)
148 // EVT_MENU(XMLID("about"), MyFrame::OnAbout)
149 // EVT_MENU(XMLID("new"), MyFrame::OnNew)
150 // EVT_MENU(XMLID("open"), MyFrame::OnOpen)
152 #define XMLID(str_id) wxXmlResource::GetXMLID(str_id)
155 class WXDLLEXPORT wxXmlResourceHandler
: public wxObject
158 wxXmlResourceHandler();
159 virtual ~wxXmlResourceHandler() {}
161 // Creates object (menu, dialog, control, ...) from XML node.
162 // Should check for validity.
163 // parent is higher-level object (usually window, dialog or panel)
164 // that is often neccessary to create resource
165 // if instance != NULL it should not create new instance via 'new' but
166 // rather use this one and call its Create method
167 wxObject
*CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
);
169 // This one is called from CreateResource after variables
171 virtual wxObject
*DoCreateResource() = 0;
173 // Returns TRUE if it understands this node and can create
174 // resource from it, FALSE otherwise.
175 virtual bool CanHandle(wxXmlNode
*node
) = 0;
177 // Check "platform" property if it matches this platform
178 // that is, if this node 'exists' under this platform
179 static bool CheckPlatform(wxXmlNode
*node
);
181 void SetParentResource(wxXmlResource
*res
) { m_Resource
= res
; }
186 wxXmlResource
*m_Resource
;
187 wxArrayString m_StyleNames
;
188 wxArrayInt m_StyleValues
;
190 // Variables (filled by CreateResource)
192 wxObject
*m_Parent
, *m_Instance
;
193 wxWindow
*m_ParentAsWindow
, *m_InstanceAsWindow
;
195 // --- Handy methods:
197 // Gets node content from wxXML_ENTITY_NODE
198 // (the problem is, <tag>content<tag> is represented as
199 // wxXML_ENTITY_NODE name="tag", content=""
200 // |-- wxXML_TEXT_NODE or
201 // wxXML_CDATA_SECTION_NODE name="" content="content"
202 wxString
GetNodeContent(wxXmlNode
*node
);
204 // Check to see if a param exists
205 bool HasParam(const wxString
& param
);
207 // Finds the node or returns NULL
208 wxXmlNode
*GetParamNode(const wxString
& param
);
209 wxString
GetParamValue(const wxString
& param
);
211 // Add style flag (e.g. wxMB_DOCKABLE) to list of flags
212 // understood by this handler
213 void AddStyle(const wxString
& name
, int value
);
215 // Gets style flags from text in form "flag | flag2| flag3 |..."
216 // Only understads flags added with AddStyle
217 int GetStyle(const wxString
& param
= _T("style"), int defaults
= 0);
219 // Gets text from param and does some convertions:
220 // - replaces \n, \r, \t by respective chars (according to C syntax)
221 // - replaces $ by & and $$ by $ (needed for $File => &File because of XML)
222 // - converts encodings if neccessary
223 wxString
GetText(const wxString
& param
);
229 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
230 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
232 // Get integer value from param
233 long GetLong( const wxString
& param
, long defaultv
= 0 );
235 // Get colour in HTML syntax (#RRGGBB)
236 wxColour
GetColour(const wxString
& param
);
238 wxSize
GetSize(const wxString
& param
= _T("size"));
239 wxPoint
GetPosition(const wxString
& param
= _T("pos"));
241 // Sets common window options:
242 void SetupWindow(wxWindow
*wnd
);
244 void CreateChildren(wxObject
*parent
, bool only_this_handler
= FALSE
,
245 wxXmlNode
*children_node
= NULL
/*stands for
246 GetParamNode("children")*/);
247 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
)
248 { return m_Resource
->CreateResFromNode(node
, parent
, instance
); }
251 #define ADD_STYLE(style) AddStyle(_T(#style), style)
255 #endif // _WX_XMLRES_H_