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/fontenum.h"
30 #include "wx/module.h"
31 #include "wx/bitmap.h"
33 #include "wx/fontmap.h"
35 #include "wx/xml/xml.h"
36 #include "wx/xml/xmlres.h"
38 #include "wx/arrimpl.cpp"
39 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
42 wxXmlResource::wxXmlResource(bool use_locale
= TRUE
)
44 m_Handlers
.DeleteContents(TRUE
);
45 m_UseLocale
= use_locale
;
48 wxXmlResource::wxXmlResource(const wxString
& filemask
, bool use_locale
= TRUE
)
50 m_UseLocale
= use_locale
;
51 m_Handlers
.DeleteContents(TRUE
);
55 wxXmlResource::~wxXmlResource()
61 bool wxXmlResource::Load(const wxString
& filemask
)
64 wxXmlResourceDataRecord
*drec
;
65 bool iswild
= wxIsWild(filemask
);
69 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
70 # define wxXmlFindNext fsys.FindNext()
72 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
73 # define wxXmlFindNext wxFindNextFile()
82 if (filemask
.Lower().Matches("*.zip") ||
83 filemask
.Lower().Matches("*.rsc"))
88 fnd2
= fs2
.FindFirst(fnd
+ wxT("#zip:*.xmb"), wxFILE
);
91 drec
= new wxXmlResourceDataRecord
;
94 fnd2
= fs2
.FindNext();
100 drec
= new wxXmlResourceDataRecord
;
110 # undef wxXmlFindFirst
111 # undef wxXmlFindNext
117 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
119 m_Handlers
.Append(handler
);
120 handler
->SetParentResource(this);
125 void wxXmlResource::ClearHandlers()
132 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
134 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("menu")), NULL
, NULL
);
139 wxMenuBar
*wxXmlResource::LoadMenuBar(const wxString
& name
)
141 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("menubar")), NULL
, NULL
);
146 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
148 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("toolbar")), parent
, NULL
);
153 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
155 wxDialog
*dialog
= new wxDialog
;
156 if (!LoadDialog(dialog
, parent
, name
))
157 { delete dialog
; return NULL
; }
161 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
163 return CreateResFromNode(FindResource(name
, wxT("dialog")), parent
, dlg
) != NULL
;
168 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
170 wxPanel
*panel
= new wxPanel
;
171 if (!LoadPanel(panel
, parent
, name
))
172 { delete panel
; return NULL
; }
176 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
178 return CreateResFromNode(FindResource(name
, wxT("panel")), parent
, panel
) != NULL
;
183 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
185 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
186 FindResource(name
, wxT("bitmap")), NULL
, NULL
);
189 if (bmp
) { rt
= *bmp
; delete bmp
; }
193 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
195 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
196 FindResource(name
, wxT("icon")), NULL
, NULL
);
199 if (icon
) { rt
= *icon
; delete icon
; }
205 void wxXmlResource::ProcessPlatformProperty(wxXmlNode
*node
)
210 wxXmlNode
*c
= node
->GetChildren();
214 if (!c
->GetPropVal(_T("platform"), &s
))
218 wxStringTokenizer
tkn(s
, " |");
220 while (tkn
.HasMoreTokens())
222 s
= tkn
.GetNextToken();
225 s
== wxString(_T("win"))
226 #elif defined(__UNIX__)
227 s
== wxString(_T("unix"))
228 #elif defined(__MAC__)
229 s
== wxString(_T("mac"))
230 #elif defined(__OS2__)
231 s
== wxString(_T("os2"))
240 ProcessPlatformProperty(c
);
243 node
->RemoveChild(c
);
253 void wxXmlResource::UpdateResources()
256 # if wxUSE_FILESYSTEM
261 for (size_t i
= 0; i
< m_Data
.GetCount(); i
++)
263 modif
= (m_Data
[i
].Doc
== NULL
);
267 # if wxUSE_FILESYSTEM
268 file
= fsys
.OpenFile(m_Data
[i
].File
);
269 modif
= file
&& file
->GetModificationTime() > m_Data
[i
].Time
;
271 wxLogError(_("Cannot open file '%s'."), m_Data
[i
].File
.c_str());
274 modif
= wxDateTime(wxFileModificationTime(m_Data
[i
].File
)) > m_Data
[i
].Time
;
280 wxInputStream
*stream
;
282 # if wxUSE_FILESYSTEM
283 file
= fsys
.OpenFile(m_Data
[i
].File
);
284 stream
= file
->GetStream();
286 stream
= new wxFileInputStream(m_Data
[i
].File
);
291 delete m_Data
[i
].Doc
;
292 m_Data
[i
].Doc
= new wxXmlDocument
;
294 if (!stream
|| !m_Data
[i
].Doc
->Load(*stream
))
296 wxLogError(_("Cannot load resources from file '%s'."), m_Data
[i
].File
.c_str());
297 delete m_Data
[i
].Doc
;
298 m_Data
[i
].Doc
= NULL
;
300 else if (m_Data
[i
].Doc
->GetRoot()->GetName() != _T("resource"))
302 wxLogError(_("Invalid XML resource '%s': doesn't have root node 'resource'."), m_Data
[i
].File
.c_str());
303 delete m_Data
[i
].Doc
;
304 m_Data
[i
].Doc
= NULL
;
307 ProcessPlatformProperty(m_Data
[i
].Doc
->GetRoot());
309 # if wxUSE_FILESYSTEM
320 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
, const wxString
& type
)
322 UpdateResources(); //ensure everything is up-to-date
325 for (size_t f
= 0; f
< m_Data
.GetCount(); f
++)
327 if (m_Data
[f
].Doc
== NULL
|| m_Data
[f
].Doc
->GetRoot() == NULL
) continue;
328 for (wxXmlNode
*node
= m_Data
[f
].Doc
->GetRoot()->GetChildren();
329 node
; node
= node
->GetNext())
330 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
331 (!type
|| node
->GetName() == type
) &&
332 node
->GetPropVal(wxT("name"), &dummy
) &&
336 m_CurFileSystem
.ChangePathTo(m_Data
[f
].File
);
342 wxLogError(_("XML resource '%s' (type '%s') not found!"),
343 name
.c_str(), type
.c_str());
349 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
351 if (node
== NULL
) return NULL
;
353 wxXmlResourceHandler
*handler
;
355 wxNode
* ND
= m_Handlers
.GetFirst();
358 handler
= (wxXmlResourceHandler
*)ND
->GetData();
359 if (handler
->CanHandle(node
))
361 ret
= handler
->CreateResource(node
, parent
, instance
);
367 wxLogError(_("No handler found for XML node '%s'!"), node
->GetName().c_str());
379 wxXmlResourceHandler::wxXmlResourceHandler()
380 : m_Node(NULL
), m_Parent(NULL
), m_Instance(NULL
),
381 m_ParentAsWindow(NULL
), m_InstanceAsWindow(NULL
)
386 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
388 wxXmlNode
*myNode
= m_Node
;
389 wxObject
*myParent
= m_Parent
, *myInstance
= m_Instance
;
390 wxWindow
*myParentAW
= m_ParentAsWindow
, *myInstanceAW
= m_InstanceAsWindow
;
394 m_Instance
= instance
;
395 m_ParentAsWindow
= wxDynamicCast(m_Parent
, wxWindow
);
396 m_InstanceAsWindow
= wxDynamicCast(m_Instance
, wxWindow
);
398 wxObject
*returned
= DoCreateResource();
401 m_Parent
= myParent
; m_ParentAsWindow
= myParentAW
;
402 m_Instance
= myInstance
; m_InstanceAsWindow
= myInstanceAW
;
408 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
410 m_StyleNames
.Add(name
);
411 m_StyleValues
.Add(value
);
416 void wxXmlResourceHandler::AddWindowStyles()
418 ADD_STYLE(wxSIMPLE_BORDER
);
419 ADD_STYLE(wxSUNKEN_BORDER
);
420 ADD_STYLE(wxDOUBLE_BORDER
);
421 ADD_STYLE(wxRAISED_BORDER
);
422 ADD_STYLE(wxSTATIC_BORDER
);
423 ADD_STYLE(wxTRANSPARENT_WINDOW
);
424 ADD_STYLE(wxWANTS_CHARS
);
425 ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
430 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
432 return (GetParamNode(param
) != NULL
);
436 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
438 wxString s
= GetParamValue(param
);
440 if (!s
) return defaults
;
442 wxStringTokenizer
tkn(s
, _T("| "), wxTOKEN_STRTOK
);
446 while (tkn
.HasMoreTokens())
448 fl
= tkn
.GetNextToken();
449 index
= m_StyleNames
.Index(fl
);
450 if (index
!= wxNOT_FOUND
)
451 style
|= m_StyleValues
[index
];
453 wxLogError(_("Unknown style flag ") + fl
);
460 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
462 wxString str1
= GetParamValue(param
);
466 for (dt
= str1
.c_str(); *dt
; dt
++)
468 // Remap $ to &, map $$ to $ (for things like "&File..." --
469 // this is illegal in XML, so we use "$File..."):
473 case '$' : str2
<< '$'; break;
474 default : str2
<< '&' << *dt
; break;
476 // Remap \n to CR, \r LF, \t to TAB:
477 else if (*dt
== '\\')
480 case 'n' : str2
<< '\n'; break;
481 case 't' : str2
<< '\t'; break;
482 case 'r' : str2
<< '\r'; break;
483 default : str2
<< '\\' << *dt
; break;
488 if (m_Resource
->GetUseLocale())
489 return wxGetTranslation(str2
);
496 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
499 wxString str1
= GetParamValue(param
);
501 if (!str1
.ToLong(&value
))
508 int wxXmlResourceHandler::GetID()
510 wxString sid
= GetName();
513 if (sid
== _T("-1")) return -1;
514 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
515 #define stdID(id) else if (sid == _T(#id)) return id
516 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
517 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
518 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
519 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
520 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
521 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
522 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
523 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
524 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
525 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
526 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
527 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
528 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
530 else return XMLID(sid
.c_str());
534 wxString
wxXmlResourceHandler::GetName()
536 return m_Node
->GetPropVal(_T("name"), _T("-1"));
541 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
543 wxString v
= GetParamValue(param
);
545 if (!v
) return defaultv
;
546 else return (v
== _T("1"));
551 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
553 wxString v
= GetParamValue(param
);
554 unsigned long tmp
= 0;
556 if (v
.Length() != 7 || v
[0] != _T('#') ||
557 wxSscanf(v
.c_str(), _T("#%lX"), &tmp
) != 1)
559 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
560 v
.c_str(), param
.c_str());
564 return wxColour((tmp
& 0xFF0000) >> 16 ,
565 (tmp
& 0x00FF00) >> 8,
571 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
573 wxString name
= GetParamValue(param
);
574 if (name
.IsEmpty()) return wxNullBitmap
;
576 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
579 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.mb_str());
582 wxImage
img(*(fsfile
->GetStream()));
585 wxImage
img(GetParamValue(_T("bitmap")));
589 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.mb_str());
592 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
593 return img
.ConvertToBitmap();
598 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
600 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
602 icon
.CopyFromBitmap(GetBitmap(param
, size
));
605 wxBitmap bmppt
= GetBitmap(param
, size
);
606 iconpt
= (wxIcon
*)(&bmppt
);
607 wxIcon
icon(*iconpt
);
614 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
616 wxXmlNode
*n
= m_Node
->GetChildren();
620 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
628 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
631 if (n
== NULL
) return wxEmptyString
;
632 n
= n
->GetChildren();
636 if (n
->GetType() == wxXML_TEXT_NODE
||
637 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
638 return n
->GetContent();
641 return wxEmptyString
;
646 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
649 return GetNodeContent(m_Node
);
651 return GetNodeContent(GetParamNode(param
));
656 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
658 wxString s
= GetParamValue(param
);
659 if (s
.IsEmpty()) s
= _T("-1,-1");
663 is_dlg
= s
[s
.Length()-1] == _T('d');
664 if (is_dlg
) s
.RemoveLast();
666 if (!s
.BeforeFirst(_T(',')).ToLong(&sx
) ||
667 !s
.AfterLast(_T(',')).ToLong(&sy
))
669 wxLogError(_("Cannot parse coordinates from '%s'."), s
.mb_str());
670 return wxDefaultSize
;
675 if (m_InstanceAsWindow
)
676 return wxDLG_UNIT(m_InstanceAsWindow
, wxSize(sx
, sy
));
677 else if (m_ParentAsWindow
)
678 return wxDLG_UNIT(m_ParentAsWindow
, wxSize(sx
, sy
));
681 wxLogError(_("Cannot convert dialog units: dialog unknown."));
682 return wxDefaultSize
;
685 else return wxSize(sx
, sy
);
690 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
692 wxSize sz
= GetSize(param
);
693 return wxPoint(sz
.x
, sz
.y
);
698 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
700 wxString s
= GetParamValue(param
);
701 if (s
.IsEmpty()) return defaultv
;
705 is_dlg
= s
[s
.Length()-1] == _T('d');
706 if (is_dlg
) s
.RemoveLast();
710 wxLogError(_("Cannot parse dimension from '%s'."), s
.mb_str());
716 if (m_InstanceAsWindow
)
717 return wxDLG_UNIT(m_InstanceAsWindow
, wxSize(sx
, 0)).x
;
718 else if (m_ParentAsWindow
)
719 return wxDLG_UNIT(m_ParentAsWindow
, wxSize(sx
, 0)).x
;
722 wxLogError(_("Cannot convert dialog units: dialog unknown."));
731 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
733 wxXmlNode
*font_node
= GetParamNode(param
);
734 if (font_node
== NULL
)
736 wxLogError(_("Cannot find font node '%s'."), param
.mb_str());
740 wxXmlNode
*oldnode
= m_Node
;
743 long size
= GetLong(_T("size"), 12);
745 wxString style
= GetParamValue(_T("style"));
746 wxString weight
= GetParamValue(_T("weight"));
747 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
748 if (style
== _T("italic")) istyle
= wxITALIC
;
749 else if (style
== _T("slant")) istyle
= wxSLANT
;
750 if (weight
== _T("bold")) iweight
= wxBOLD
;
751 else if (weight
== _T("light")) iweight
= wxLIGHT
;
753 wxString family
= GetParamValue(_T("family"));
754 int ifamily
= wxDEFAULT
;
755 if (family
== _T("decorative")) ifamily
= wxDECORATIVE
;
756 else if (family
== _T("roman")) ifamily
= wxROMAN
;
757 else if (family
== _T("script")) ifamily
= wxSCRIPT
;
758 else if (family
== _T("swiss")) ifamily
= wxSWISS
;
759 else if (family
== _T("modern")) ifamily
= wxMODERN
;
761 bool underlined
= GetBool(_T("underlined"), FALSE
);
763 wxString encoding
= GetParamValue(_T("encoding"));
765 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
766 if (!encoding
.IsEmpty()) enc
= mapper
.CharsetToEncoding(encoding
);
767 if (enc
== wxFONTENCODING_SYSTEM
) enc
= wxFONTENCODING_SYSTEM
;
769 wxString faces
= GetParamValue(_T("face"));
770 wxString facename
= wxEmptyString
;
771 wxFontEnumerator enu
;
772 enu
.EnumerateFacenames();
773 wxStringTokenizer
tk(faces
, _T(","));
774 while (tk
.HasMoreTokens())
776 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
777 if (index
!= wxNOT_FOUND
)
779 facename
= (*enu
.GetFacenames())[index
];
786 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
791 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
795 if (HasParam(_T("exstyle")))
796 wnd
->SetExtraStyle(GetStyle(_T("exstyle")));
797 if (HasParam(_T("bg")))
798 wnd
->SetBackgroundColour(GetColour(_T("bg")));
799 if (HasParam(_T("fg")))
800 wnd
->SetForegroundColour(GetColour(_T("fg")));
801 if (GetBool(_T("enabled"), 1) == 0)
803 if (GetBool(_T("focused"), 0) == 1)
805 if (GetBool(_T("hidden"), 0) == 1)
808 if (HasParam(_T("tooltip")))
809 wnd
->SetToolTip(GetText(_T("tooltip")));
811 if (HasParam(_T("font")))
812 wnd
->SetFont(GetFont());
816 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
,
817 bool only_this_handler
, wxXmlNode
*children_node
)
819 if (children_node
== NULL
) children_node
= GetParamNode(_T("children"));
820 if (children_node
== NULL
) return;
822 wxXmlNode
*n
= children_node
->GetChildren();
826 if (n
->GetType() == wxXML_ELEMENT_NODE
)
828 if (only_this_handler
)
831 CreateResource(n
, parent
, NULL
);
834 m_Resource
->CreateResFromNode(n
, parent
, NULL
);
848 // --------------- XMLID implementation -----------------------------
850 #define XMLID_TABLE_SIZE 1024
860 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
861 static int XMLID_LastID
= wxID_HIGHEST
;
863 /*static*/ int wxXmlResource::GetXMLID(const char *str_id
)
867 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
868 index
%= XMLID_TABLE_SIZE
;
870 XMLID_record
*oldrec
= NULL
;
872 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
874 if (strcmp(rec
->key
, str_id
) == 0)
882 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
883 &XMLID_Records
[index
] : &oldrec
->next
;
884 *rec_var
= new XMLID_record
;
885 (*rec_var
)->id
= ++XMLID_LastID
;
886 (*rec_var
)->key
= strdup(str_id
);
887 (*rec_var
)->next
= NULL
;
889 return (*rec_var
)->id
;
893 static void CleanXMLID_Record(XMLID_record
*rec
)
897 CleanXMLID_Record(rec
->next
);
903 static void CleanXMLID_Records()
905 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
906 CleanXMLID_Record(XMLID_Records
[i
]);
916 // --------------- module and globals -----------------------------
919 static wxXmlResource gs_XmlResource
;
921 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
924 class wxXmlResourceModule
: public wxModule
926 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
928 wxXmlResourceModule() {}
929 bool OnInit() {return TRUE
;}
932 wxTheXmlResource
->ClearHandlers();
933 CleanXMLID_Records();
937 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)