1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XML resources
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2000 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "xmlres.h"
15 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
22 #include "wx/dialog.h"
24 #include "wx/wfstream.h"
25 #include "wx/filesys.h"
28 #include "wx/tokenzr.h"
29 #include "wx/module.h"
31 #include "wx/xml/xml.h"
32 #include "wx/xml/xmlres.h"
34 #include "wx/arrimpl.cpp"
35 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
38 wxXmlResource::wxXmlResource()
40 m_Handlers
.DeleteContents(TRUE
);
43 wxXmlResource::wxXmlResource(const wxString
& filemask
, int type
)
45 m_Handlers
.DeleteContents(TRUE
);
49 wxXmlResource::~wxXmlResource()
55 bool wxXmlResource::Load(const wxString
& filemask
, int type
)
58 wxXmlResourceDataRecord
*drec
;
62 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
63 # define wxXmlFindNext fsys.FindNext()
65 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
66 # define wxXmlFindNext wxFindNextFile()
67 wxASSERT_MSG(type
!= wxXML_ARCHIVE
, wxT("ZIP archive XML resources supported only with wxUSE_FILESYSTEM set to 1!"));
73 if (type
== wxXML_ARCHIVE
)
78 fnd2
= fs2
.FindFirst(fnd
+ wxT("#zip:*.xmb"), wxFILE
);
81 drec
= new wxXmlResourceDataRecord
;
84 fnd2
= fs2
.FindNext();
90 drec
= new wxXmlResourceDataRecord
;
96 # undef wxXmlFindFirst
103 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
105 m_Handlers
.Append(handler
);
106 handler
->SetParentResource(this);
111 void wxXmlResource::ClearHandlers()
118 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
120 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("menu")), NULL
, NULL
);
125 wxMenuBar
*wxXmlResource::LoadMenuBar(const wxString
& name
)
127 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("menubar")), NULL
, NULL
);
132 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
134 wxDialog
*dialog
= new wxDialog
;
135 if (!LoadDialog(dialog
, parent
, name
))
136 { delete dialog
; return NULL
; }
140 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
142 return CreateResFromNode(FindResource(name
, wxT("dialog")), parent
, dlg
) != NULL
;
147 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
149 wxPanel
*panel
= new wxPanel
;
150 if (!LoadPanel(panel
, parent
, name
))
151 { delete panel
; return NULL
; }
155 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
157 return CreateResFromNode(FindResource(name
, wxT("panel")), parent
, panel
) != NULL
;
162 void wxXmlResource::UpdateResources()
165 # if wxUSE_FILESYSTEM
170 for (size_t i
= 0; i
< m_Data
.GetCount(); i
++)
172 modif
= (m_Data
[i
].Doc
== NULL
);
176 # if wxUSE_FILESYSTEM
177 file
= fsys
.OpenFile(m_Data
[i
].File
);
178 modif
= file
&& file
->GetModificationTime() > m_Data
[i
].Time
;
180 wxLogError(_("Cannot open file '%s'."), m_Data
[i
].File
.c_str());
183 modif
= wxDateTime(wxFileModificationTime(m_Data
[i
].File
)) > m_Data
[i
].Time
;
189 wxInputStream
*stream
;
191 # if wxUSE_FILESYSTEM
192 file
= fsys
.OpenFile(m_Data
[i
].File
);
193 stream
= file
->GetStream();
195 stream
= new wxFileInputStream(m_Data
[i
].File
);
200 delete m_Data
[i
].Doc
;
201 m_Data
[i
].Doc
= new wxXmlDocument
;
203 if (!stream
|| !m_Data
[i
].Doc
->Load(*stream
))
204 wxLogError(_("Cannot load resources from file '%s'."), m_Data
[i
].File
.c_str());
206 if (m_Data
[i
].Doc
->GetRoot()->GetName() != _T("resource"))
207 wxLogError(_("Invalid XML resource '%s': doesn't have root node 'resource'."), m_Data
[i
].File
.c_str());
209 # if wxUSE_FILESYSTEM
220 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
, const wxString
& type
)
222 UpdateResources(); //ensure everything is up-to-date
225 for (size_t f
= 0; f
< m_Data
.GetCount(); f
++)
227 for (wxXmlNode
*node
= m_Data
[f
].Doc
->GetRoot()->GetChildren();
228 node
; node
= node
->GetNext())
229 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
230 (!type
|| node
->GetName() == type
) &&
231 node
->GetPropVal(wxT("name"), &dummy
) &&
233 wxXmlResourceHandler::CheckPlatform(node
))
237 wxLogError(_("XML resource '%s' (type '%s') not found!"),
238 name
.c_str(), type
.c_str());
244 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
246 if (node
== NULL
) return NULL
;
248 wxXmlResourceHandler
*handler
;
250 wxNode
* ND
= m_Handlers
.GetFirst();
253 handler
= (wxXmlResourceHandler
*)ND
->GetData();
254 if (handler
->CanHandle(node
))
256 ret
= handler
->CreateResource(node
, parent
, instance
);
262 wxLogError(_("No handler found for XML node '%s'!"), node
->GetName().c_str());
274 wxXmlResourceHandler::wxXmlResourceHandler()
275 : m_Node(NULL
), m_Parent(NULL
), m_Instance(NULL
),
276 m_ParentAsWindow(NULL
), m_InstanceAsWindow(NULL
)
281 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
283 if (!CheckPlatform(node
)) return NULL
;
285 wxXmlNode
*myNode
= m_Node
;
286 wxObject
*myParent
= m_Parent
, *myInstance
= m_Instance
;
287 wxWindow
*myParentAW
= m_ParentAsWindow
, *myInstanceAW
= m_InstanceAsWindow
;
291 m_Instance
= instance
;
292 m_ParentAsWindow
= wxDynamicCast(m_Parent
, wxWindow
);
293 m_InstanceAsWindow
= wxDynamicCast(m_Instance
, wxWindow
);
295 wxObject
*returned
= DoCreateResource();
298 m_Parent
= myParent
; m_ParentAsWindow
= myParentAW
;
299 m_Instance
= myInstance
; m_InstanceAsWindow
= myInstanceAW
;
306 /*static*/ bool wxXmlResourceHandler::CheckPlatform(wxXmlNode
*node
)
309 if (!node
->GetPropVal(_T("platform"), &s
)) return TRUE
;
311 return s
== wxString(_T("win"));
312 #elif defined(__UNIX__)
313 return s
== wxString(_T("unix"));
314 #elif defined(__MAC__)
315 return s
== wxString(_T("mac"));
316 #elif defined(__OS2__)
317 return s
== wxString(_T("os2"));
319 wxLogWarning(_("You're running the application on unknown platform, check for platfrom '%s' failed."), s
.mb_str());
320 return TRUE
; // unknown platform
325 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
327 m_StyleNames
.Add(name
);
328 m_StyleValues
.Add(value
);
332 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
334 return (GetParamNode(param
) != NULL
);
338 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
340 wxString s
= GetParamValue(param
);
342 if (!s
) return defaults
;
344 wxStringTokenizer
tkn(s
, _T("| "), wxTOKEN_STRTOK
);
348 while (tkn
.HasMoreTokens())
350 fl
= tkn
.GetNextToken();
351 index
= m_StyleNames
.Index(fl
);
352 if (index
!= wxNOT_FOUND
)
353 style
|= m_StyleValues
[index
];
355 wxLogError(_("Unknown style flag ") + fl
);
362 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
364 wxString str1
= GetParamValue(param
);
368 for (dt
= str1
.c_str(); *dt
; dt
++)
370 // Remap $ to &, map $$ to $ (for things like "&File..." --
371 // this is illegal in XML, so we use "$File..."):
375 case '$' : str2
<< '$'; break;
376 default : str2
<< '&' << *dt
; break;
378 // Remap \n to CR, \r LF, \t to TAB:
379 else if (*dt
== '\\')
382 case 'n' : str2
<< '\n'; break;
383 case 't' : str2
<< '\t'; break;
384 case 'r' : str2
<< '\r'; break;
385 default : str2
<< '\\' << *dt
; break;
394 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
397 wxString str1
= GetParamValue(param
);
399 if (!str1
.ToLong(&value
))
406 int wxXmlResourceHandler::GetID()
408 wxString sid
= GetName();
411 if (sid
== _T("-1")) return -1;
412 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
413 #define stdID(id) else if (sid == _T(#id)) return id
414 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
415 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
416 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
417 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
418 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
419 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
420 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
421 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
422 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
423 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
424 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
425 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
426 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
428 else return XMLID(sid
.c_str());
432 wxString
wxXmlResourceHandler::GetName()
434 return m_Node
->GetPropVal(_T("name"), _T("-1"));
439 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
441 wxString v
= GetParamValue(param
);
443 if (!v
) return defaultv
;
444 else return (v
== _T("1"));
449 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
451 wxString v
= GetParamValue(param
);
452 unsigned long tmp
= 0;
454 if (v
.Length() != 7 || v
[0] != _T('#') ||
455 wxSscanf(v
.c_str(), _T("#%lX"), &tmp
) != 1)
457 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
458 v
.c_str(), param
.c_str());
462 return wxColour((tmp
& 0xFF0000) >> 16 ,
463 (tmp
& 0x00FF00) >> 8,
468 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
470 wxXmlNode
*n
= m_Node
->GetChildren();
474 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
482 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
485 if (n
== NULL
) return wxEmptyString
;
486 n
= n
->GetChildren();
490 if (n
->GetType() == wxXML_TEXT_NODE
||
491 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
492 return n
->GetContent();
495 return wxEmptyString
;
500 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
502 return GetNodeContent(GetParamNode(param
));
507 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
509 wxString s
= GetParamValue(param
);
510 if (!s
) s
= _T("-1,-1");
514 is_dlg
= s
[s
.Length()-1] == _T('d');
515 if (is_dlg
) s
.RemoveLast();
517 if (!s
.BeforeFirst(_T(',')).ToLong(&sx
) ||
518 !s
.AfterLast(_T(',')).ToLong(&sy
))
520 wxLogError(_("Cannot parse coordinates from '%s'."), s
.mb_str());
521 return wxDefaultSize
;
526 if (m_InstanceAsWindow
)
527 return wxDLG_UNIT(m_InstanceAsWindow
, wxSize(sx
, sy
));
528 else if (m_ParentAsWindow
)
529 return wxDLG_UNIT(m_ParentAsWindow
, wxSize(sx
, sy
));
532 wxLogError(_("Cannot convert dialog units: dialog unknown."));
533 return wxDefaultSize
;
536 else return wxSize(sx
, sy
);
541 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
543 wxSize sz
= GetSize(param
);
544 return wxPoint(sz
.x
, sz
.y
);
549 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
551 //FIXME : add font, cursor
553 if (HasParam(_T("exstyle")))
554 wnd
->SetExtraStyle(GetStyle(_T("exstyle")));
555 if (HasParam(_T("bg")))
556 wnd
->SetBackgroundColour(GetColour(_T("bg")));
557 if (HasParam(_T("fg")))
558 wnd
->SetForegroundColour(GetColour(_T("fg")));
559 if (GetBool(_T("enabled"), 1) == 0)
561 if (GetBool(_T("focused"), 0) == 1)
563 if (GetBool(_T("hidden"), 0) == 1)
566 if (HasParam(_T("tooltip")))
567 wnd
->SetToolTip(GetText(_T("tooltip")));
572 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
,
573 bool only_this_handler
, wxXmlNode
*children_node
)
575 if (children_node
== NULL
) children_node
= GetParamNode(_T("children"));
576 if (children_node
== NULL
) return;
578 wxXmlNode
*n
= children_node
->GetChildren();
582 if (n
->GetType() == wxXML_ELEMENT_NODE
)
584 if (only_this_handler
)
587 CreateResource(n
, parent
, NULL
);
590 m_Resource
->CreateResFromNode(n
, parent
, NULL
);
604 // --------------- XMLID implementation -----------------------------
607 #define XMLID_TABLE_SIZE 1024
616 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
617 static int XMLID_LastID
= wxID_HIGHEST
;
619 /*static*/ int wxXmlResource::GetXMLID(const char *str_id
)
623 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
624 index
%= XMLID_TABLE_SIZE
;
626 XMLID_record
*oldrec
= NULL
;
627 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
629 if (strcmp(rec
->key
, str_id
) == 0) return rec
->id
;
633 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
634 &XMLID_Records
[index
] : &oldrec
->next
;
635 *rec_var
= new XMLID_record
;
636 (*rec_var
)->id
= ++XMLID_LastID
;
637 (*rec_var
)->key
= str_id
;
638 (*rec_var
)->next
= NULL
;
640 return (*rec_var
)->id
;
644 static void CleanXMLID_Record(XMLID_record
*rec
)
648 CleanXMLID_Record(rec
->next
);
653 static void CleanXMLID_Records()
655 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
656 CleanXMLID_Record(XMLID_Records
[i
]);
666 // --------------- module and globals -----------------------------
669 static wxXmlResource gs_XmlResource
;
671 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
674 class wxXmlResourceModule
: public wxModule
676 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
678 wxXmlResourceModule() {}
679 bool OnInit() {return TRUE
;}
682 wxTheXmlResource
->ClearHandlers();
683 CleanXMLID_Records();
687 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)