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"
30 #include "wx/bitmap.h"
33 #include "wx/xml/xml.h"
34 #include "wx/xml/xmlres.h"
36 #include "wx/arrimpl.cpp"
37 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
40 wxXmlResource::wxXmlResource()
42 m_Handlers
.DeleteContents(TRUE
);
45 wxXmlResource::wxXmlResource(const wxString
& filemask
)
47 m_Handlers
.DeleteContents(TRUE
);
51 wxXmlResource::~wxXmlResource()
57 bool wxXmlResource::Load(const wxString
& filemask
)
60 wxXmlResourceDataRecord
*drec
;
61 bool iswild
= wxIsWild(filemask
);
65 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
66 # define wxXmlFindNext fsys.FindNext()
68 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
69 # define wxXmlFindNext wxFindNextFile()
78 if (filemask
.Lower().Matches("*.zip") ||
79 filemask
.Lower().Matches("*.rsc"))
84 fnd2
= fs2
.FindFirst(fnd
+ wxT("#zip:*.xmb"), wxFILE
);
87 drec
= new wxXmlResourceDataRecord
;
90 fnd2
= fs2
.FindNext();
96 drec
= new wxXmlResourceDataRecord
;
106 # undef wxXmlFindFirst
107 # undef wxXmlFindNext
113 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
115 m_Handlers
.Append(handler
);
116 handler
->SetParentResource(this);
121 void wxXmlResource::ClearHandlers()
128 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
130 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("menu")), NULL
, NULL
);
135 wxMenuBar
*wxXmlResource::LoadMenuBar(const wxString
& name
)
137 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("menubar")), NULL
, NULL
);
142 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
144 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("toolbar")), parent
, NULL
);
149 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
151 wxDialog
*dialog
= new wxDialog
;
152 if (!LoadDialog(dialog
, parent
, name
))
153 { delete dialog
; return NULL
; }
157 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
159 return CreateResFromNode(FindResource(name
, wxT("dialog")), parent
, dlg
) != NULL
;
164 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
166 wxPanel
*panel
= new wxPanel
;
167 if (!LoadPanel(panel
, parent
, name
))
168 { delete panel
; return NULL
; }
172 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
174 return CreateResFromNode(FindResource(name
, wxT("panel")), parent
, panel
) != NULL
;
179 void wxXmlResource::ProcessPlatformProperty(wxXmlNode
*node
)
184 wxXmlNode
*c
= node
->GetChildren();
188 if (!c
->GetPropVal(_T("platform"), &s
))
192 wxStringTokenizer
tkn(s
, " |");
194 while (tkn
.HasMoreTokens())
196 s
= tkn
.GetNextToken();
199 s
== wxString(_T("win"))
200 #elif defined(__UNIX__)
201 s
== wxString(_T("unix"))
202 #elif defined(__MAC__)
203 s
== wxString(_T("mac"))
204 #elif defined(__OS2__)
205 s
== wxString(_T("os2"))
214 ProcessPlatformProperty(c
);
217 node
->RemoveChild(c
);
227 void wxXmlResource::UpdateResources()
230 # if wxUSE_FILESYSTEM
235 for (size_t i
= 0; i
< m_Data
.GetCount(); i
++)
237 modif
= (m_Data
[i
].Doc
== NULL
);
241 # if wxUSE_FILESYSTEM
242 file
= fsys
.OpenFile(m_Data
[i
].File
);
243 modif
= file
&& file
->GetModificationTime() > m_Data
[i
].Time
;
245 wxLogError(_("Cannot open file '%s'."), m_Data
[i
].File
.c_str());
248 modif
= wxDateTime(wxFileModificationTime(m_Data
[i
].File
)) > m_Data
[i
].Time
;
254 wxInputStream
*stream
;
256 # if wxUSE_FILESYSTEM
257 file
= fsys
.OpenFile(m_Data
[i
].File
);
258 stream
= file
->GetStream();
260 stream
= new wxFileInputStream(m_Data
[i
].File
);
265 delete m_Data
[i
].Doc
;
266 m_Data
[i
].Doc
= new wxXmlDocument
;
268 if (!stream
|| !m_Data
[i
].Doc
->Load(*stream
))
270 wxLogError(_("Cannot load resources from file '%s'."), m_Data
[i
].File
.c_str());
271 delete m_Data
[i
].Doc
;
272 m_Data
[i
].Doc
= NULL
;
274 else if (m_Data
[i
].Doc
->GetRoot()->GetName() != _T("resource"))
276 wxLogError(_("Invalid XML resource '%s': doesn't have root node 'resource'."), m_Data
[i
].File
.c_str());
277 delete m_Data
[i
].Doc
;
278 m_Data
[i
].Doc
= NULL
;
281 ProcessPlatformProperty(m_Data
[i
].Doc
->GetRoot());
283 # if wxUSE_FILESYSTEM
294 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
, const wxString
& type
)
296 UpdateResources(); //ensure everything is up-to-date
299 for (size_t f
= 0; f
< m_Data
.GetCount(); f
++)
301 if (m_Data
[f
].Doc
== NULL
|| m_Data
[f
].Doc
->GetRoot() == NULL
) continue;
302 for (wxXmlNode
*node
= m_Data
[f
].Doc
->GetRoot()->GetChildren();
303 node
; node
= node
->GetNext())
304 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
305 (!type
|| node
->GetName() == type
) &&
306 node
->GetPropVal(wxT("name"), &dummy
) &&
310 m_CurFileSystem
.ChangePathTo(m_Data
[f
].File
);
316 wxLogError(_("XML resource '%s' (type '%s') not found!"),
317 name
.c_str(), type
.c_str());
323 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
325 if (node
== NULL
) return NULL
;
327 wxXmlResourceHandler
*handler
;
329 wxNode
* ND
= m_Handlers
.GetFirst();
332 handler
= (wxXmlResourceHandler
*)ND
->GetData();
333 if (handler
->CanHandle(node
))
335 ret
= handler
->CreateResource(node
, parent
, instance
);
341 wxLogError(_("No handler found for XML node '%s'!"), node
->GetName().c_str());
353 wxXmlResourceHandler::wxXmlResourceHandler()
354 : m_Node(NULL
), m_Parent(NULL
), m_Instance(NULL
),
355 m_ParentAsWindow(NULL
), m_InstanceAsWindow(NULL
)
360 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
362 wxXmlNode
*myNode
= m_Node
;
363 wxObject
*myParent
= m_Parent
, *myInstance
= m_Instance
;
364 wxWindow
*myParentAW
= m_ParentAsWindow
, *myInstanceAW
= m_InstanceAsWindow
;
368 m_Instance
= instance
;
369 m_ParentAsWindow
= wxDynamicCast(m_Parent
, wxWindow
);
370 m_InstanceAsWindow
= wxDynamicCast(m_Instance
, wxWindow
);
372 wxObject
*returned
= DoCreateResource();
375 m_Parent
= myParent
; m_ParentAsWindow
= myParentAW
;
376 m_Instance
= myInstance
; m_InstanceAsWindow
= myInstanceAW
;
382 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
384 m_StyleNames
.Add(name
);
385 m_StyleValues
.Add(value
);
390 void wxXmlResourceHandler::AddWindowStyles()
392 ADD_STYLE(wxSIMPLE_BORDER
);
393 ADD_STYLE(wxSUNKEN_BORDER
);
394 ADD_STYLE(wxDOUBLE_BORDER
);
395 ADD_STYLE(wxRAISED_BORDER
);
396 ADD_STYLE(wxSTATIC_BORDER
);
397 ADD_STYLE(wxTRANSPARENT_WINDOW
);
398 ADD_STYLE(wxWANTS_CHARS
);
399 ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
404 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
406 return (GetParamNode(param
) != NULL
);
410 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
412 wxString s
= GetParamValue(param
);
414 if (!s
) return defaults
;
416 wxStringTokenizer
tkn(s
, _T("| "), wxTOKEN_STRTOK
);
420 while (tkn
.HasMoreTokens())
422 fl
= tkn
.GetNextToken();
423 index
= m_StyleNames
.Index(fl
);
424 if (index
!= wxNOT_FOUND
)
425 style
|= m_StyleValues
[index
];
427 wxLogError(_("Unknown style flag ") + fl
);
434 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
436 wxString str1
= GetParamValue(param
);
440 for (dt
= str1
.c_str(); *dt
; dt
++)
442 // Remap $ to &, map $$ to $ (for things like "&File..." --
443 // this is illegal in XML, so we use "$File..."):
447 case '$' : str2
<< '$'; break;
448 default : str2
<< '&' << *dt
; break;
450 // Remap \n to CR, \r LF, \t to TAB:
451 else if (*dt
== '\\')
454 case 'n' : str2
<< '\n'; break;
455 case 't' : str2
<< '\t'; break;
456 case 'r' : str2
<< '\r'; break;
457 default : str2
<< '\\' << *dt
; break;
466 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
469 wxString str1
= GetParamValue(param
);
471 if (!str1
.ToLong(&value
))
478 int wxXmlResourceHandler::GetID()
480 wxString sid
= GetName();
483 if (sid
== _T("-1")) return -1;
484 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
485 #define stdID(id) else if (sid == _T(#id)) return id
486 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
487 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
488 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
489 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
490 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
491 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
492 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
493 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
494 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
495 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
496 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
497 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
498 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
500 else return XMLID(sid
.c_str());
504 wxString
wxXmlResourceHandler::GetName()
506 return m_Node
->GetPropVal(_T("name"), _T("-1"));
511 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
513 wxString v
= GetParamValue(param
);
515 if (!v
) return defaultv
;
516 else return (v
== _T("1"));
521 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
523 wxString v
= GetParamValue(param
);
524 unsigned long tmp
= 0;
526 if (v
.Length() != 7 || v
[0] != _T('#') ||
527 wxSscanf(v
.c_str(), _T("#%lX"), &tmp
) != 1)
529 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
530 v
.c_str(), param
.c_str());
534 return wxColour((tmp
& 0xFF0000) >> 16 ,
535 (tmp
& 0x00FF00) >> 8,
541 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
543 wxString name
= GetParamValue(param
);
544 if (name
.IsEmpty()) return wxNullBitmap
;
546 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
549 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.mb_str());
552 wxImage
img(*(fsfile
->GetStream()));
555 wxImage
img(GetParamValue(_T("bitmap")));
559 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.mb_str());
562 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
563 return img
.ConvertToBitmap();
568 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
570 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
572 icon
.CopyFromBitmap(GetBitmap(param
, size
));
575 wxBitmap bmppt
= GetBitmap(param
, size
);
576 iconpt
= (wxIcon
*)(&bmppt
);
577 wxIcon
icon(*iconpt
);
584 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
586 wxXmlNode
*n
= m_Node
->GetChildren();
590 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
598 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
601 if (n
== NULL
) return wxEmptyString
;
602 n
= n
->GetChildren();
606 if (n
->GetType() == wxXML_TEXT_NODE
||
607 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
608 return n
->GetContent();
611 return wxEmptyString
;
616 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
618 return GetNodeContent(GetParamNode(param
));
623 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
625 wxString s
= GetParamValue(param
);
626 if (s
.IsEmpty()) s
= _T("-1,-1");
630 is_dlg
= s
[s
.Length()-1] == _T('d');
631 if (is_dlg
) s
.RemoveLast();
633 if (!s
.BeforeFirst(_T(',')).ToLong(&sx
) ||
634 !s
.AfterLast(_T(',')).ToLong(&sy
))
636 wxLogError(_("Cannot parse coordinates from '%s'."), s
.mb_str());
637 return wxDefaultSize
;
642 if (m_InstanceAsWindow
)
643 return wxDLG_UNIT(m_InstanceAsWindow
, wxSize(sx
, sy
));
644 else if (m_ParentAsWindow
)
645 return wxDLG_UNIT(m_ParentAsWindow
, wxSize(sx
, sy
));
648 wxLogError(_("Cannot convert dialog units: dialog unknown."));
649 return wxDefaultSize
;
652 else return wxSize(sx
, sy
);
657 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
659 wxSize sz
= GetSize(param
);
660 return wxPoint(sz
.x
, sz
.y
);
665 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
667 wxString s
= GetParamValue(param
);
668 if (s
.IsEmpty()) return defaultv
;
672 is_dlg
= s
[s
.Length()-1] == _T('d');
673 if (is_dlg
) s
.RemoveLast();
677 wxLogError(_("Cannot parse dimension from '%s'."), s
.mb_str());
683 if (m_InstanceAsWindow
)
684 return wxDLG_UNIT(m_InstanceAsWindow
, wxSize(sx
, 0)).x
;
685 else if (m_ParentAsWindow
)
686 return wxDLG_UNIT(m_ParentAsWindow
, wxSize(sx
, 0)).x
;
689 wxLogError(_("Cannot convert dialog units: dialog unknown."));
698 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
700 //FIXME : add font, cursor
702 if (HasParam(_T("exstyle")))
703 wnd
->SetExtraStyle(GetStyle(_T("exstyle")));
704 if (HasParam(_T("bg")))
705 wnd
->SetBackgroundColour(GetColour(_T("bg")));
706 if (HasParam(_T("fg")))
707 wnd
->SetForegroundColour(GetColour(_T("fg")));
708 if (GetBool(_T("enabled"), 1) == 0)
710 if (GetBool(_T("focused"), 0) == 1)
712 if (GetBool(_T("hidden"), 0) == 1)
715 if (HasParam(_T("tooltip")))
716 wnd
->SetToolTip(GetText(_T("tooltip")));
721 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
,
722 bool only_this_handler
, wxXmlNode
*children_node
)
724 if (children_node
== NULL
) children_node
= GetParamNode(_T("children"));
725 if (children_node
== NULL
) return;
727 wxXmlNode
*n
= children_node
->GetChildren();
731 if (n
->GetType() == wxXML_ELEMENT_NODE
)
733 if (only_this_handler
)
736 CreateResource(n
, parent
, NULL
);
739 m_Resource
->CreateResFromNode(n
, parent
, NULL
);
753 // --------------- XMLID implementation -----------------------------
755 #define XMLID_TABLE_SIZE 1024
765 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
766 static int XMLID_LastID
= wxID_HIGHEST
;
768 /*static*/ int wxXmlResource::GetXMLID(const char *str_id
)
772 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
773 index
%= XMLID_TABLE_SIZE
;
775 XMLID_record
*oldrec
= NULL
;
777 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
779 if (strcmp(rec
->key
, str_id
) == 0)
787 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
788 &XMLID_Records
[index
] : &oldrec
->next
;
789 *rec_var
= new XMLID_record
;
790 (*rec_var
)->id
= ++XMLID_LastID
;
791 (*rec_var
)->key
= strdup(str_id
);
792 (*rec_var
)->next
= NULL
;
794 return (*rec_var
)->id
;
798 static void CleanXMLID_Record(XMLID_record
*rec
)
802 CleanXMLID_Record(rec
->next
);
808 static void CleanXMLID_Records()
810 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
811 CleanXMLID_Record(XMLID_Records
[i
]);
821 // --------------- module and globals -----------------------------
824 static wxXmlResource gs_XmlResource
;
826 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
829 class wxXmlResourceModule
: public wxModule
831 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
833 wxXmlResourceModule() {}
834 bool OnInit() {return TRUE
;}
837 wxTheXmlResource
->ClearHandlers();
838 CleanXMLID_Records();
842 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)