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"
25 #include "wx/wfstream.h"
26 #include "wx/filesys.h"
29 #include "wx/tokenzr.h"
30 #include "wx/fontenum.h"
31 #include "wx/module.h"
32 #include "wx/bitmap.h"
34 #include "wx/fontmap.h"
36 #include "wx/xrc/xml.h"
37 #include "wx/xrc/xmlres.h"
39 #include "wx/arrimpl.cpp"
40 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
43 wxXmlResource::wxXmlResource(bool use_locale
)
45 m_handlers
.DeleteContents(TRUE
);
46 m_useLocale
= use_locale
;
50 wxXmlResource::wxXmlResource(const wxString
& filemask
, bool use_locale
)
52 m_useLocale
= use_locale
;
54 m_handlers
.DeleteContents(TRUE
);
58 wxXmlResource::~wxXmlResource()
64 bool wxXmlResource::Load(const wxString
& filemask
)
67 wxXmlResourceDataRecord
*drec
;
68 bool iswild
= wxIsWild(filemask
);
73 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
74 # define wxXmlFindNext fsys.FindNext()
76 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
77 # define wxXmlFindNext wxFindNextFile()
86 if (filemask
.Lower().Matches(wxT("*.zip")) ||
87 filemask
.Lower().Matches(wxT("*.rsc")))
89 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xmb"));
90 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
95 drec
= new wxXmlResourceDataRecord
;
105 # undef wxXmlFindFirst
106 # undef wxXmlFindNext
112 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
114 m_handlers
.Append(handler
);
115 handler
->SetParentResource(this);
120 void wxXmlResource::ClearHandlers()
127 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
129 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
134 wxMenuBar
*wxXmlResource::LoadMenuBar(const wxString
& name
)
136 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), NULL
, NULL
);
141 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
143 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
148 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
150 wxDialog
*dialog
= new wxDialog
;
151 if (!LoadDialog(dialog
, parent
, name
))
152 { delete dialog
; return NULL
; }
156 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
158 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
163 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
165 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
168 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
170 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
173 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
175 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
178 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
180 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
181 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
184 if (bmp
) { rt
= *bmp
; delete bmp
; }
188 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
190 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
191 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
194 if (icon
) { rt
= *icon
; delete icon
; }
198 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
199 wxWindow
*control
, wxWindow
*parent
)
202 parent
= control
->GetParent();
203 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
206 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
209 return control
->Reparent(container
);
213 void wxXmlResource::ProcessPlatformProperty(wxXmlNode
*node
)
218 wxXmlNode
*c
= node
->GetChildren();
222 if (!c
->GetPropVal(wxT("platform"), &s
))
226 wxStringTokenizer
tkn(s
, " |");
228 while (tkn
.HasMoreTokens())
230 s
= tkn
.GetNextToken();
233 s
== wxString(wxT("win"))
234 #elif defined(__UNIX__)
235 s
== wxString(wxT("unix"))
236 #elif defined(__MAC__)
237 s
== wxString(wxT("mac"))
238 #elif defined(__OS2__)
239 s
== wxString(wxT("os2"))
248 ProcessPlatformProperty(c
);
251 node
->RemoveChild(c
);
261 void wxXmlResource::UpdateResources()
264 # if wxUSE_FILESYSTEM
265 wxFSFile
*file
= NULL
;
269 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
271 modif
= (m_data
[i
].Doc
== NULL
);
275 # if wxUSE_FILESYSTEM
276 file
= fsys
.OpenFile(m_data
[i
].File
);
277 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
279 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
282 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
288 wxInputStream
*stream
= NULL
;
290 # if wxUSE_FILESYSTEM
291 file
= fsys
.OpenFile(m_data
[i
].File
);
293 stream
= file
->GetStream();
295 stream
= new wxFileInputStream(m_data
[i
].File
);
300 delete m_data
[i
].Doc
;
301 m_data
[i
].Doc
= new wxXmlDocument
;
303 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
))
305 wxLogError(_("Cannot load resources from file '%s'."), m_data
[i
].File
.c_str());
306 wxDELETE(m_data
[i
].Doc
);
308 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
310 wxLogError(_("Invalid XML resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
311 wxDELETE(m_data
[i
].Doc
);
317 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
318 wxT("version"), wxT("0.0.0.0"));
319 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
320 &v1
, &v2
, &v3
, &v4
) == 4)
321 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
326 if (m_version
!= version
)
327 wxLogError(_("Resource files must have same version number!"));
329 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
330 m_data
[i
].Time
= file
->GetModificationTime();
333 # if wxUSE_FILESYSTEM
344 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
, const wxString
& classname
)
346 UpdateResources(); //ensure everything is up-to-date
349 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
351 if (m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
) continue;
352 for (wxXmlNode
*node
= m_data
[f
].Doc
->GetRoot()->GetChildren();
353 node
; node
= node
->GetNext())
354 if (node
->GetType() == wxXML_ELEMENT_NODE
&&
356 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
357 node
->GetName() == wxT("object") &&
358 node
->GetPropVal(wxT("name"), &dummy
) &&
362 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
368 wxLogError(_("XML resource '%s' (class '%s') not found!"),
369 name
.c_str(), classname
.c_str());
375 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
377 if (node
== NULL
) return NULL
;
379 wxXmlResourceHandler
*handler
;
381 wxNode
* ND
= m_handlers
.GetFirst();
384 handler
= (wxXmlResourceHandler
*)ND
->GetData();
385 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
387 ret
= handler
->CreateResource(node
, parent
, instance
);
393 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
394 node
->GetName().c_str(),
395 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
407 wxXmlResourceHandler::wxXmlResourceHandler()
408 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
409 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
414 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
416 wxXmlNode
*myNode
= m_node
;
417 wxString myClass
= m_class
;
418 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
419 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
422 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
424 m_instance
= instance
;
425 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
426 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
428 wxObject
*returned
= DoCreateResource();
432 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
433 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
439 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
441 m_styleNames
.Add(name
);
442 m_styleValues
.Add(value
);
447 void wxXmlResourceHandler::AddWindowStyles()
449 ADD_STYLE(wxSIMPLE_BORDER
);
450 ADD_STYLE(wxSUNKEN_BORDER
);
451 ADD_STYLE(wxDOUBLE_BORDER
);
452 ADD_STYLE(wxRAISED_BORDER
);
453 ADD_STYLE(wxSTATIC_BORDER
);
454 ADD_STYLE(wxNO_BORDER
);
455 ADD_STYLE(wxTRANSPARENT_WINDOW
);
456 ADD_STYLE(wxWANTS_CHARS
);
457 ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
462 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
464 return (GetParamNode(param
) != NULL
);
468 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
470 wxString s
= GetParamValue(param
);
472 if (!s
) return defaults
;
474 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
478 while (tkn
.HasMoreTokens())
480 fl
= tkn
.GetNextToken();
481 index
= m_styleNames
.Index(fl
);
482 if (index
!= wxNOT_FOUND
)
483 style
|= m_styleValues
[index
];
485 wxLogError(_("Unknown style flag ") + fl
);
492 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
494 wxString str1
= GetParamValue(param
);
499 // VS: First version of XML resources used $ instead of & (which is illegal in XML),
500 // but later I realized that '_' fits this purpose much better (because
501 // &File means "File with F underlined").
502 if (m_resource
->CompareVersion(2,3,0,1) < 0)
507 for (dt
= str1
.c_str(); *dt
; dt
++)
509 // Remap amp_char to &, map double amp_char to amp_char (for things
510 // like "&File..." -- this is illegal in XML, so we use "_File..."):
513 if ( *(++dt
) == amp_char
)
516 str2
<< wxT('&') << *dt
;
518 // Remap \n to CR, \r to LF, \t to TAB:
519 else if (*dt
== wxT('\\'))
522 case wxT('n') : str2
<< wxT('\n'); break;
523 case wxT('t') : str2
<< wxT('\t'); break;
524 case wxT('r') : str2
<< wxT('\r'); break;
525 default : str2
<< wxT('\\') << *dt
; break;
530 if (m_resource
->GetUseLocale())
531 return wxGetTranslation(str2
);
538 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
541 wxString str1
= GetParamValue(param
);
543 if (!str1
.ToLong(&value
))
550 int wxXmlResourceHandler::GetID()
552 wxString sid
= GetName();
555 if (sid
== wxT("-1")) return -1;
556 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
557 #define stdID(id) else if (sid == wxT(#id)) return id
558 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
559 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
560 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
561 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
562 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
563 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
564 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
565 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
566 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
567 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
568 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
569 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
570 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
572 else return wxXmlResource::GetXMLID(sid
);
576 wxString
wxXmlResourceHandler::GetName()
578 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
583 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
585 wxString v
= GetParamValue(param
);
587 if (!v
) return defaultv
;
588 else return (v
== wxT("1"));
593 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
595 wxString v
= GetParamValue(param
);
596 unsigned long tmp
= 0;
598 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
599 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
601 wxLogError(_("XML resource: Incorrect colour specification '%s' for property '%s'."),
602 v
.c_str(), param
.c_str());
606 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
607 (unsigned char) ((tmp
& 0x00FF00) >> 8),
608 (unsigned char) ((tmp
& 0x0000FF)));
613 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
615 wxString name
= GetParamValue(param
);
616 if (name
.IsEmpty()) return wxNullBitmap
;
618 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
621 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.c_str());
624 wxImage
img(*(fsfile
->GetStream()));
627 wxImage
img(GetParamValue(wxT("bitmap")));
631 wxLogError(_("XML resource: Cannot create bitmap from '%s'."), param
.c_str());
634 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
635 return img
.ConvertToBitmap();
640 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
642 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
644 icon
.CopyFromBitmap(GetBitmap(param
, size
));
647 wxBitmap bmppt
= GetBitmap(param
, size
);
648 iconpt
= (wxIcon
*)(&bmppt
);
649 wxIcon
icon(*iconpt
);
656 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
658 wxXmlNode
*n
= m_node
->GetChildren();
662 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
670 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
673 if (n
== NULL
) return wxEmptyString
;
674 n
= n
->GetChildren();
678 if (n
->GetType() == wxXML_TEXT_NODE
||
679 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
680 return n
->GetContent();
683 return wxEmptyString
;
688 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
691 return GetNodeContent(m_node
);
693 return GetNodeContent(GetParamNode(param
));
698 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
700 wxString s
= GetParamValue(param
);
701 if (s
.IsEmpty()) s
= wxT("-1,-1");
705 is_dlg
= s
[s
.Length()-1] == wxT('d');
706 if (is_dlg
) s
.RemoveLast();
708 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
709 !s
.AfterLast(wxT(',')).ToLong(&sy
))
711 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
712 return wxDefaultSize
;
717 if (m_instanceAsWindow
)
718 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
719 else if (m_parentAsWindow
)
720 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
723 wxLogError(_("Cannot convert dialog units: dialog unknown."));
724 return wxDefaultSize
;
727 else return wxSize(sx
, sy
);
732 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
734 wxSize sz
= GetSize(param
);
735 return wxPoint(sz
.x
, sz
.y
);
740 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
742 wxString s
= GetParamValue(param
);
743 if (s
.IsEmpty()) return defaultv
;
747 is_dlg
= s
[s
.Length()-1] == wxT('d');
748 if (is_dlg
) s
.RemoveLast();
752 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
758 if (m_instanceAsWindow
)
759 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
760 else if (m_parentAsWindow
)
761 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
764 wxLogError(_("Cannot convert dialog units: dialog unknown."));
773 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
775 wxXmlNode
*font_node
= GetParamNode(param
);
776 if (font_node
== NULL
)
778 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
782 wxXmlNode
*oldnode
= m_node
;
785 long size
= GetLong(wxT("size"), 12);
787 wxString style
= GetParamValue(wxT("style"));
788 wxString weight
= GetParamValue(wxT("weight"));
789 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
790 if (style
== wxT("italic")) istyle
= wxITALIC
;
791 else if (style
== wxT("slant")) istyle
= wxSLANT
;
792 if (weight
== wxT("bold")) iweight
= wxBOLD
;
793 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
795 wxString family
= GetParamValue(wxT("family"));
796 int ifamily
= wxDEFAULT
;
797 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
798 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
799 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
800 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
801 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
803 bool underlined
= GetBool(wxT("underlined"), FALSE
);
805 wxString encoding
= GetParamValue(wxT("encoding"));
807 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
808 if (!encoding
.IsEmpty()) enc
= mapper
.CharsetToEncoding(encoding
);
809 if (enc
== wxFONTENCODING_SYSTEM
) enc
= wxFONTENCODING_SYSTEM
;
811 wxString faces
= GetParamValue(wxT("face"));
812 wxString facename
= wxEmptyString
;
813 wxFontEnumerator enu
;
814 enu
.EnumerateFacenames();
815 wxStringTokenizer
tk(faces
, wxT(","));
816 while (tk
.HasMoreTokens())
818 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
819 if (index
!= wxNOT_FOUND
)
821 facename
= (*enu
.GetFacenames())[index
];
828 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
833 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
837 if (HasParam(wxT("exstyle")))
838 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
839 if (HasParam(wxT("bg")))
840 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
841 if (HasParam(wxT("fg")))
842 wnd
->SetForegroundColour(GetColour(wxT("fg")));
843 if (GetBool(wxT("enabled"), 1) == 0)
845 if (GetBool(wxT("focused"), 0) == 1)
847 if (GetBool(wxT("hidden"), 0) == 1)
850 if (HasParam(wxT("tooltip")))
851 wnd
->SetToolTip(GetText(wxT("tooltip")));
853 if (HasParam(wxT("font")))
854 wnd
->SetFont(GetFont());
858 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
860 wxXmlNode
*n
= m_node
->GetChildren();
864 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
865 n
->GetName() == wxT("object"))
867 if (this_hnd_only
&& CanHandle(n
))
868 CreateResource(n
, parent
, NULL
);
870 m_resource
->CreateResFromNode(n
, parent
, NULL
);
877 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
880 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
881 wxXmlNode
*n
= root
->GetChildren();
885 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
887 CreateResource(n
, parent
, NULL
);
899 // --------------- XMLID implementation -----------------------------
901 #define XMLID_TABLE_SIZE 1024
911 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
913 /*static*/ int wxXmlResource::GetXMLID(const wxChar
*str_id
)
915 static int XMLID_LastID
= wxID_HIGHEST
;
919 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
920 index
%= XMLID_TABLE_SIZE
;
922 XMLID_record
*oldrec
= NULL
;
924 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
926 if (wxStrcmp(rec
->key
, str_id
) == 0)
934 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
935 &XMLID_Records
[index
] : &oldrec
->next
;
936 *rec_var
= new XMLID_record
;
937 (*rec_var
)->id
= ++XMLID_LastID
;
938 (*rec_var
)->key
= wxStrdup(str_id
);
939 (*rec_var
)->next
= NULL
;
941 return (*rec_var
)->id
;
945 static void CleanXMLID_Record(XMLID_record
*rec
)
949 CleanXMLID_Record(rec
->next
);
955 static void CleanXMLID_Records()
957 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
958 CleanXMLID_Record(XMLID_Records
[i
]);
968 // --------------- module and globals -----------------------------
971 static wxXmlResource gs_XmlResource
;
973 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
976 class wxXmlResourceModule
: public wxModule
978 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
980 wxXmlResourceModule() {}
981 bool OnInit() {return TRUE
;}
984 wxTheXmlResource
->ClearHandlers();
985 CleanXMLID_Records();
989 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
992 // When wxXml is loaded dynamically after the application is already running
993 // then the built-in module system won't pick this one up. Add it manually.
994 void wxXmlInitResourceModule()
996 wxModule
* module = new wxXmlResourceModule
;
998 wxModule::RegisterModule(module);