1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: XRC 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 static void 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"))
249 ProcessPlatformProperty(c
);
254 node
->RemoveChild(c
);
255 wxXmlNode
*c2
= c
->GetNext();
264 void wxXmlResource::UpdateResources()
267 # if wxUSE_FILESYSTEM
268 wxFSFile
*file
= NULL
;
272 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
274 modif
= (m_data
[i
].Doc
== NULL
);
278 # if wxUSE_FILESYSTEM
279 file
= fsys
.OpenFile(m_data
[i
].File
);
280 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
282 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
285 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
291 wxInputStream
*stream
= NULL
;
293 # if wxUSE_FILESYSTEM
294 file
= fsys
.OpenFile(m_data
[i
].File
);
296 stream
= file
->GetStream();
298 stream
= new wxFileInputStream(m_data
[i
].File
);
303 delete m_data
[i
].Doc
;
304 m_data
[i
].Doc
= new wxXmlDocument
;
306 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
))
308 wxLogError(_("Cannot load resources from file '%s'."), m_data
[i
].File
.c_str());
309 wxDELETE(m_data
[i
].Doc
);
311 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
313 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
314 wxDELETE(m_data
[i
].Doc
);
320 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
321 wxT("version"), wxT("0.0.0.0"));
322 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
323 &v1
, &v2
, &v3
, &v4
) == 4)
324 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
329 if (m_version
!= version
)
330 wxLogError(_("Resource files must have same version number!"));
332 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
333 m_data
[i
].Time
= file
->GetModificationTime();
336 # if wxUSE_FILESYSTEM
347 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
, const wxString
& classname
)
349 UpdateResources(); //ensure everything is up-to-date
352 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
354 if (m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
) continue;
355 for (wxXmlNode
*node
= m_data
[f
].Doc
->GetRoot()->GetChildren();
356 node
; node
= node
->GetNext())
357 if (node
->GetType() == wxXML_ELEMENT_NODE
&&
359 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
360 node
->GetName() == wxT("object") &&
361 node
->GetPropVal(wxT("name"), &dummy
) &&
365 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
371 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
372 name
.c_str(), classname
.c_str());
378 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
380 if (node
== NULL
) return NULL
;
382 wxXmlResourceHandler
*handler
;
384 wxNode
* ND
= m_handlers
.GetFirst();
387 handler
= (wxXmlResourceHandler
*)ND
->GetData();
388 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
390 ret
= handler
->CreateResource(node
, parent
, instance
);
396 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
397 node
->GetName().c_str(),
398 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
410 wxXmlResourceHandler::wxXmlResourceHandler()
411 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
412 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
417 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
419 wxXmlNode
*myNode
= m_node
;
420 wxString myClass
= m_class
;
421 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
422 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
425 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
427 m_instance
= instance
;
428 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
429 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
431 wxObject
*returned
= DoCreateResource();
435 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
436 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
442 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
444 m_styleNames
.Add(name
);
445 m_styleValues
.Add(value
);
450 void wxXmlResourceHandler::AddWindowStyles()
452 ADD_STYLE(wxSIMPLE_BORDER
);
453 ADD_STYLE(wxSUNKEN_BORDER
);
454 ADD_STYLE(wxDOUBLE_BORDER
);
455 ADD_STYLE(wxRAISED_BORDER
);
456 ADD_STYLE(wxSTATIC_BORDER
);
457 ADD_STYLE(wxNO_BORDER
);
458 ADD_STYLE(wxTRANSPARENT_WINDOW
);
459 ADD_STYLE(wxWANTS_CHARS
);
460 ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
465 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
467 return (GetParamNode(param
) != NULL
);
471 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
473 wxString s
= GetParamValue(param
);
475 if (!s
) return defaults
;
477 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
481 while (tkn
.HasMoreTokens())
483 fl
= tkn
.GetNextToken();
484 index
= m_styleNames
.Index(fl
);
485 if (index
!= wxNOT_FOUND
)
486 style
|= m_styleValues
[index
];
488 wxLogError(_("Unknown style flag ") + fl
);
495 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
497 wxString str1
= GetParamValue(param
);
502 // VS: First version of XRC resources used $ instead of & (which is illegal in XML),
503 // but later I realized that '_' fits this purpose much better (because
504 // &File means "File with F underlined").
505 if (m_resource
->CompareVersion(2,3,0,1) < 0)
510 for (dt
= str1
.c_str(); *dt
; dt
++)
512 // Remap amp_char to &, map double amp_char to amp_char (for things
513 // like "&File..." -- this is illegal in XML, so we use "_File..."):
516 if ( *(++dt
) == amp_char
)
519 str2
<< wxT('&') << *dt
;
521 // Remap \n to CR, \r to LF, \t to TAB:
522 else if (*dt
== wxT('\\'))
525 case wxT('n') : str2
<< wxT('\n'); break;
526 case wxT('t') : str2
<< wxT('\t'); break;
527 case wxT('r') : str2
<< wxT('\r'); break;
528 default : str2
<< wxT('\\') << *dt
; break;
533 if (m_resource
->GetUseLocale())
534 return wxGetTranslation(str2
);
541 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
544 wxString str1
= GetParamValue(param
);
546 if (!str1
.ToLong(&value
))
553 int wxXmlResourceHandler::GetID()
555 wxString sid
= GetName();
558 if (sid
== wxT("-1")) return -1;
559 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
560 #define stdID(id) else if (sid == wxT(#id)) return id
561 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
562 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
563 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
564 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
565 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
566 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
567 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
568 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
569 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
570 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
571 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
572 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
573 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
575 else return wxXmlResource::GetXMLID(sid
);
579 wxString
wxXmlResourceHandler::GetName()
581 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
586 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
588 wxString v
= GetParamValue(param
);
590 if (!v
) return defaultv
;
591 else return (v
== wxT("1"));
596 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
598 wxString v
= GetParamValue(param
);
599 unsigned long tmp
= 0;
601 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
602 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
604 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
605 v
.c_str(), param
.c_str());
609 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
610 (unsigned char) ((tmp
& 0x00FF00) >> 8),
611 (unsigned char) ((tmp
& 0x0000FF)));
616 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
618 wxString name
= GetParamValue(param
);
619 if (name
.IsEmpty()) return wxNullBitmap
;
621 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
624 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
627 wxImage
img(*(fsfile
->GetStream()));
630 wxImage
img(GetParamValue(wxT("bitmap")));
634 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
637 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
638 return img
.ConvertToBitmap();
643 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
645 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
647 icon
.CopyFromBitmap(GetBitmap(param
, size
));
650 wxBitmap bmppt
= GetBitmap(param
, size
);
651 iconpt
= (wxIcon
*)(&bmppt
);
652 wxIcon
icon(*iconpt
);
659 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
661 wxXmlNode
*n
= m_node
->GetChildren();
665 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
673 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
676 if (n
== NULL
) return wxEmptyString
;
677 n
= n
->GetChildren();
681 if (n
->GetType() == wxXML_TEXT_NODE
||
682 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
683 return n
->GetContent();
686 return wxEmptyString
;
691 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
694 return GetNodeContent(m_node
);
696 return GetNodeContent(GetParamNode(param
));
701 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
703 wxString s
= GetParamValue(param
);
704 if (s
.IsEmpty()) s
= wxT("-1,-1");
708 is_dlg
= s
[s
.Length()-1] == wxT('d');
709 if (is_dlg
) s
.RemoveLast();
711 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
712 !s
.AfterLast(wxT(',')).ToLong(&sy
))
714 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
715 return wxDefaultSize
;
720 if (m_instanceAsWindow
)
721 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
722 else if (m_parentAsWindow
)
723 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
726 wxLogError(_("Cannot convert dialog units: dialog unknown."));
727 return wxDefaultSize
;
730 else return wxSize(sx
, sy
);
735 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
737 wxSize sz
= GetSize(param
);
738 return wxPoint(sz
.x
, sz
.y
);
743 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
745 wxString s
= GetParamValue(param
);
746 if (s
.IsEmpty()) return defaultv
;
750 is_dlg
= s
[s
.Length()-1] == wxT('d');
751 if (is_dlg
) s
.RemoveLast();
755 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
761 if (m_instanceAsWindow
)
762 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
763 else if (m_parentAsWindow
)
764 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
767 wxLogError(_("Cannot convert dialog units: dialog unknown."));
776 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
778 wxXmlNode
*font_node
= GetParamNode(param
);
779 if (font_node
== NULL
)
781 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
785 wxXmlNode
*oldnode
= m_node
;
788 long size
= GetLong(wxT("size"), 12);
790 wxString style
= GetParamValue(wxT("style"));
791 wxString weight
= GetParamValue(wxT("weight"));
792 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
793 if (style
== wxT("italic")) istyle
= wxITALIC
;
794 else if (style
== wxT("slant")) istyle
= wxSLANT
;
795 if (weight
== wxT("bold")) iweight
= wxBOLD
;
796 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
798 wxString family
= GetParamValue(wxT("family"));
799 int ifamily
= wxDEFAULT
;
800 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
801 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
802 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
803 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
804 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
806 bool underlined
= GetBool(wxT("underlined"), FALSE
);
808 wxString encoding
= GetParamValue(wxT("encoding"));
810 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
811 if (!encoding
.IsEmpty())
812 enc
= mapper
.CharsetToEncoding(encoding
);
813 if (enc
== wxFONTENCODING_SYSTEM
)
814 enc
= wxFONTENCODING_DEFAULT
;
816 wxString faces
= GetParamValue(wxT("face"));
817 wxString facename
= wxEmptyString
;
818 wxFontEnumerator enu
;
819 enu
.EnumerateFacenames();
820 wxStringTokenizer
tk(faces
, wxT(","));
821 while (tk
.HasMoreTokens())
823 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
824 if (index
!= wxNOT_FOUND
)
826 facename
= (*enu
.GetFacenames())[index
];
833 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
838 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
842 if (HasParam(wxT("exstyle")))
843 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
844 if (HasParam(wxT("bg")))
845 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
846 if (HasParam(wxT("fg")))
847 wnd
->SetForegroundColour(GetColour(wxT("fg")));
848 if (GetBool(wxT("enabled"), 1) == 0)
850 if (GetBool(wxT("focused"), 0) == 1)
852 if (GetBool(wxT("hidden"), 0) == 1)
855 if (HasParam(wxT("tooltip")))
856 wnd
->SetToolTip(GetText(wxT("tooltip")));
858 if (HasParam(wxT("font")))
859 wnd
->SetFont(GetFont());
863 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
865 wxXmlNode
*n
= m_node
->GetChildren();
869 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
870 n
->GetName() == wxT("object"))
872 if (this_hnd_only
&& CanHandle(n
))
873 CreateResource(n
, parent
, NULL
);
875 m_resource
->CreateResFromNode(n
, parent
, NULL
);
882 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
885 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
886 wxXmlNode
*n
= root
->GetChildren();
890 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
892 CreateResource(n
, parent
, NULL
);
904 // --------------- XMLID implementation -----------------------------
906 #define XMLID_TABLE_SIZE 1024
916 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
918 /*static*/ int wxXmlResource::GetXMLID(const wxChar
*str_id
)
920 static int XMLID_LastID
= wxID_HIGHEST
;
924 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
925 index
%= XMLID_TABLE_SIZE
;
927 XMLID_record
*oldrec
= NULL
;
929 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
931 if (wxStrcmp(rec
->key
, str_id
) == 0)
939 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
940 &XMLID_Records
[index
] : &oldrec
->next
;
941 *rec_var
= new XMLID_record
;
942 (*rec_var
)->id
= ++XMLID_LastID
;
943 (*rec_var
)->key
= wxStrdup(str_id
);
944 (*rec_var
)->next
= NULL
;
946 return (*rec_var
)->id
;
950 static void CleanXMLID_Record(XMLID_record
*rec
)
954 CleanXMLID_Record(rec
->next
);
960 static void CleanXMLID_Records()
962 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
963 CleanXMLID_Record(XMLID_Records
[i
]);
973 // --------------- module and globals -----------------------------
976 static wxXmlResource gs_XmlResource
;
978 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
981 class wxXmlResourceModule
: public wxModule
983 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
985 wxXmlResourceModule() {}
986 bool OnInit() {return TRUE
;}
989 wxTheXmlResource
->ClearHandlers();
990 CleanXMLID_Records();
994 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
997 // When wxXml is loaded dynamically after the application is already running
998 // then the built-in module system won't pick this one up. Add it manually.
999 void wxXmlInitResourceModule()
1001 wxModule
* module = new wxXmlResourceModule
;
1003 wxModule::RegisterModule(module);