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"
25 class WXDLLEXPORT wxMenu
;
26 class WXDLLEXPORT wxMenuBar
;
27 class WXDLLEXPORT wxDialog
;
28 class WXDLLEXPORT wxPanel
;
29 class WXDLLEXPORT wxWindow
;
31 class WXDLLEXPORT wxXmlResourceHandler
;
33 #include "wx/xml/xml.h"
36 class WXDLLEXPORT wxXmlResourceDataRecord
39 wxXmlResourceDataRecord() : Doc(NULL
), Time(wxDateTime::Now()) {}
40 ~wxXmlResourceDataRecord() {delete Doc
;}
47 WX_DECLARE_EXPORTED_OBJARRAY(wxXmlResourceDataRecord
, wxXmlResourceDataRecords
);
49 // This class holds XML resources from one or more .xml files
50 // (or derived forms, either binary or zipped -- see manual for
53 class WXDLLEXPORT wxXmlResource
: public wxObject
57 wxXmlResource(const wxString
& filemask
);
60 // Loads resources from XML files that match given filemask.
61 // This method understands VFS (see filesys.h).
62 bool Load(const wxString
& filemask
);
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();
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
);
76 // Removes all handlers
79 // Loads menu from resource. Returns NULL on failure.
80 wxMenu
*LoadMenu(const wxString
& name
);
82 // Loads menubar from resource. Returns NULL on failure.
83 wxMenuBar
*LoadMenuBar(const wxString
& name
);
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):
90 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
92 wxDialog
*LoadDialog(wxWindow
*parent
, const wxString
& name
);
93 bool LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
);
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
);
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
);
106 // Scans resources list for unloaded files and loads them. Also reloads
107 // files that have been modified since last loading.
108 void UpdateResources();
110 // Finds resource (calls UpdateResources) and returns node containing it
111 wxXmlNode
*FindResource(const wxString
& name
, const wxString
& type
);
113 // Creates resource from info in given node:
114 wxObject
*CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
= NULL
);
116 // Remove nodes with property "platform" that does not
117 // match current platform
118 void ProcessPlatformProperty(wxXmlNode
*node
);
122 wxXmlResourceDataRecords m_Data
;
124 friend class wxXmlResourceHandler
;
128 // Global instance of resource class. For your convenience.
129 extern wxXmlResource
*wxTheXmlResource
;
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.
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)
142 #define XMLID(str_id) \
143 wxXmlResource::GetXMLID(_T(str_id))
147 // This macro returns pointer to particular control in dialog
148 // created using XML resources. You can use it to set/get values from
152 // wxTheXmlResource->LoadDialog(&dlg, mainFrame, "my_dialog");
153 // XMLCTRL(dlg, "my_textctrl", wxTextCtrl)->SetValue(_T("default value"));
155 #define XMLCTRL(window, id, type) \
156 ((type*)((window).FindWindow(XMLID(id))))
160 class WXDLLEXPORT wxXmlResourceHandler
: public wxObject
163 wxXmlResourceHandler();
164 virtual ~wxXmlResourceHandler() {}
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
);
174 // This one is called from CreateResource after variables
176 virtual wxObject
*DoCreateResource() = 0;
178 // Returns TRUE if it understands this node and can create
179 // resource from it, FALSE otherwise.
180 virtual bool CanHandle(wxXmlNode
*node
) = 0;
182 void SetParentResource(wxXmlResource
*res
) { m_Resource
= res
; }
187 wxXmlResource
*m_Resource
;
188 wxArrayString m_StyleNames
;
189 wxArrayInt m_StyleValues
;
191 // Variables (filled by CreateResource)
193 wxObject
*m_Parent
, *m_Instance
;
194 wxWindow
*m_ParentAsWindow
, *m_InstanceAsWindow
;
196 // --- Handy methods:
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
);
205 // Check to see if a param exists
206 bool HasParam(const wxString
& param
);
208 // Finds the node or returns NULL
209 wxXmlNode
*GetParamNode(const wxString
& param
);
210 wxString
GetParamValue(const wxString
& param
);
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
);
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);
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
);
230 // Get bool flag (1,t,yes,on,true are TRUE, everything else is FALSE)
231 bool GetBool(const wxString
& param
, bool defaultv
= FALSE
);
233 // Get integer value from param
234 long GetLong( const wxString
& param
, long defaultv
= 0 );
236 // Get colour in HTML syntax (#RRGGBB)
237 wxColour
GetColour(const wxString
& param
);
239 wxSize
GetSize(const wxString
& param
= _T("size"));
240 wxPoint
GetPosition(const wxString
& param
= _T("pos"));
242 // Sets common window options:
243 void SetupWindow(wxWindow
*wnd
);
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
); }
252 #define ADD_STYLE(style) AddStyle(_T(#style), style)
256 #endif // _WX_XMLRES_H_