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
);
389 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
391 return (GetParamNode(param
) != NULL
);
395 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
397 wxString s
= GetParamValue(param
);
399 if (!s
) return defaults
;
401 wxStringTokenizer
tkn(s
, _T("| "), wxTOKEN_STRTOK
);
405 while (tkn
.HasMoreTokens())
407 fl
= tkn
.GetNextToken();
408 index
= m_StyleNames
.Index(fl
);
409 if (index
!= wxNOT_FOUND
)
410 style
|= m_StyleValues
[index
];
412 wxLogError(_("Unknown style flag ") + fl
);
419 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
421 wxString str1
= GetParamValue(param
);
425 for (dt
= str1
.c_str(); *dt
; dt
++)
427 // Remap $ to &, map $$ to $ (for things like "&File..." --
428 // this is illegal in XML, so we use "$File..."):
432 case '$' : str2
<< '$'; break;
433 default : str2
<< '&' << *dt
; break;
435 // Remap \n to CR, \r LF, \t to TAB:
436 else if (*dt
== '\\')
439 case 'n' : str2
<< '\n'; break;
440 case 't' : str2
<< '\t'; break;
441 case 'r' : str2
<< '\r'; break;
442 default : str2
<< '\\' << *dt
; break;
451 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
454 wxString str1
= GetParamValue(param
);
456 if (!str1
.ToLong(&value
))
463 int wxXmlResourceHandler::GetID()
465 wxString sid
= GetName();
468 if (sid
== _T("-1")) return -1;
469 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
470 #define stdID(id) else if (sid == _T(#id)) return id
471 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
472 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
473 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
474 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
475 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
476 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
477 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
478 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
479 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
480 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
481 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
482 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
483 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
485 else return XMLID(sid
.c_str());
489 wxString
wxXmlResourceHandler::GetName()
491 return m_Node
->GetPropVal(_T("name"), _T("-1"));
496 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
498 wxString v
= GetParamValue(param
);
500 if (!v
) return defaultv
;
501 else return (v
== _T("1"));
506 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
508 wxString v
= GetParamValue(param
);
509 unsigned long tmp
= 0;
511 if (v
.Length() != 7 || v
[0] != _T('#') ||
512 wxSscanf(v
.c_str(), _T("#%lX"), &tmp
) != 1)
514 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
515 v
.c_str(), param
.c_str());
519 return wxColour((tmp
& 0xFF0000) >> 16 ,
520 (tmp
& 0x00FF00) >> 8,
526 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
528 wxString name
= GetParamValue(param
);
529 if (name
.IsEmpty()) return wxNullBitmap
;
531 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
534 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.mb_str());
537 wxImage
img(*(fsfile
->GetStream()));
540 wxImage
img(GetParamValue(_T("bitmap")));
544 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.mb_str());
547 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
548 return img
.ConvertToBitmap();
553 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
557 icon
.CopyFromBitmap(GetBitmap(param
, size
));
560 wxBitmap bmppt
= GetBitmap(param
, size
);
561 iconpt
= (wxIcon
*)(&bmppt
);
562 wxIcon
icon(*iconpt
);
569 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
571 wxXmlNode
*n
= m_Node
->GetChildren();
575 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
583 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
586 if (n
== NULL
) return wxEmptyString
;
587 n
= n
->GetChildren();
591 if (n
->GetType() == wxXML_TEXT_NODE
||
592 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
593 return n
->GetContent();
596 return wxEmptyString
;
601 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
603 return GetNodeContent(GetParamNode(param
));
608 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
610 wxString s
= GetParamValue(param
);
611 if (s
.IsEmpty()) s
= _T("-1,-1");
615 is_dlg
= s
[s
.Length()-1] == _T('d');
616 if (is_dlg
) s
.RemoveLast();
618 if (!s
.BeforeFirst(_T(',')).ToLong(&sx
) ||
619 !s
.AfterLast(_T(',')).ToLong(&sy
))
621 wxLogError(_("Cannot parse coordinates from '%s'."), s
.mb_str());
622 return wxDefaultSize
;
627 if (m_InstanceAsWindow
)
628 return wxDLG_UNIT(m_InstanceAsWindow
, wxSize(sx
, sy
));
629 else if (m_ParentAsWindow
)
630 return wxDLG_UNIT(m_ParentAsWindow
, wxSize(sx
, sy
));
633 wxLogError(_("Cannot convert dialog units: dialog unknown."));
634 return wxDefaultSize
;
637 else return wxSize(sx
, sy
);
642 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
644 wxSize sz
= GetSize(param
);
645 return wxPoint(sz
.x
, sz
.y
);
650 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
652 //FIXME : add font, cursor
654 if (HasParam(_T("exstyle")))
655 wnd
->SetExtraStyle(GetStyle(_T("exstyle")));
656 if (HasParam(_T("bg")))
657 wnd
->SetBackgroundColour(GetColour(_T("bg")));
658 if (HasParam(_T("fg")))
659 wnd
->SetForegroundColour(GetColour(_T("fg")));
660 if (GetBool(_T("enabled"), 1) == 0)
662 if (GetBool(_T("focused"), 0) == 1)
664 if (GetBool(_T("hidden"), 0) == 1)
667 if (HasParam(_T("tooltip")))
668 wnd
->SetToolTip(GetText(_T("tooltip")));
673 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
,
674 bool only_this_handler
, wxXmlNode
*children_node
)
676 if (children_node
== NULL
) children_node
= GetParamNode(_T("children"));
677 if (children_node
== NULL
) return;
679 wxXmlNode
*n
= children_node
->GetChildren();
683 if (n
->GetType() == wxXML_ELEMENT_NODE
)
685 if (only_this_handler
)
688 CreateResource(n
, parent
, NULL
);
691 m_Resource
->CreateResFromNode(n
, parent
, NULL
);
705 // --------------- XMLID implementation -----------------------------
707 #define XMLID_TABLE_SIZE 1024
717 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
718 static int XMLID_LastID
= wxID_HIGHEST
;
720 /*static*/ int wxXmlResource::GetXMLID(const char *str_id
)
724 for (const char *c
= str_id
; *c
!= '\0'; c
++) index
+= (int)*c
;
725 index
%= XMLID_TABLE_SIZE
;
727 XMLID_record
*oldrec
= NULL
;
729 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
731 if (strcmp(rec
->key
, str_id
) == 0)
733 #ifdef DEBUG_XMLID_HASH
734 printf("XMLID: matched '%s' (%ith item)\n", rec
->key
, matchcnt
);
742 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
743 &XMLID_Records
[index
] : &oldrec
->next
;
744 *rec_var
= new XMLID_record
;
745 (*rec_var
)->id
= ++XMLID_LastID
;
746 (*rec_var
)->key
= strdup(str_id
);
747 (*rec_var
)->next
= NULL
;
748 #ifdef DEBUG_XMLID_HASH
749 printf("XMLID: new key for '%s': %i at %i (%ith item)\n",
750 (*rec_var
)->key
, (*rec_var
)->id
, index
, matchcnt
);
753 return (*rec_var
)->id
;
757 static void CleanXMLID_Record(XMLID_record
*rec
)
761 #ifdef DEBUG_XMLID_HASH
762 printf("XMLID: clearing '%s'\n", rec
->key
);
764 CleanXMLID_Record(rec
->next
);
770 static void CleanXMLID_Records()
772 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
773 CleanXMLID_Record(XMLID_Records
[i
]);
783 // --------------- module and globals -----------------------------
786 static wxXmlResource gs_XmlResource
;
788 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
791 class wxXmlResourceModule
: public wxModule
793 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
795 wxXmlResourceModule() {}
796 bool OnInit() {return TRUE
;}
799 wxTheXmlResource
->ClearHandlers();
800 CleanXMLID_Records();
804 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)