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(int flags
)
45 m_handlers
.DeleteContents(TRUE
);
50 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
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(wxWindow
*parent
, const wxString
& name
)
136 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
142 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
144 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), 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("wxDialog")), parent
, dlg
) != NULL
;
164 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
166 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
169 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
171 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
174 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
176 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
179 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
181 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
182 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
185 if (bmp
) { rt
= *bmp
; delete bmp
; }
189 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
191 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
192 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
195 if (icon
) { rt
= *icon
; delete icon
; }
199 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
200 wxWindow
*control
, wxWindow
*parent
)
203 parent
= control
->GetParent();
204 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
207 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
210 return control
->Reparent(container
);
214 static void ProcessPlatformProperty(wxXmlNode
*node
)
219 wxXmlNode
*c
= node
->GetChildren();
223 if (!c
->GetPropVal(wxT("platform"), &s
))
227 wxStringTokenizer
tkn(s
, " |");
229 while (tkn
.HasMoreTokens())
231 s
= tkn
.GetNextToken();
234 s
== wxString(wxT("win"))
235 #elif defined(__UNIX__)
236 s
== wxString(wxT("unix"))
237 #elif defined(__MAC__)
238 s
== wxString(wxT("mac"))
239 #elif defined(__OS2__)
240 s
== wxString(wxT("os2"))
250 ProcessPlatformProperty(c
);
255 node
->RemoveChild(c
);
256 wxXmlNode
*c2
= c
->GetNext();
265 void wxXmlResource::UpdateResources()
268 # if wxUSE_FILESYSTEM
269 wxFSFile
*file
= NULL
;
273 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
275 modif
= (m_data
[i
].Doc
== NULL
);
279 # if wxUSE_FILESYSTEM
280 file
= fsys
.OpenFile(m_data
[i
].File
);
281 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
283 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
286 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
292 wxInputStream
*stream
= NULL
;
294 # if wxUSE_FILESYSTEM
295 file
= fsys
.OpenFile(m_data
[i
].File
);
297 stream
= file
->GetStream();
299 stream
= new wxFileInputStream(m_data
[i
].File
);
304 delete m_data
[i
].Doc
;
305 m_data
[i
].Doc
= new wxXmlDocument
;
307 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
))
309 wxLogError(_("Cannot load resources from file '%s'."), m_data
[i
].File
.c_str());
310 wxDELETE(m_data
[i
].Doc
);
312 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
314 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
315 wxDELETE(m_data
[i
].Doc
);
321 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
322 wxT("version"), wxT("0.0.0.0"));
323 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
324 &v1
, &v2
, &v3
, &v4
) == 4)
325 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
330 if (m_version
!= version
)
331 wxLogError(_("Resource files must have same version number!"));
333 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
334 m_data
[i
].Time
= file
->GetModificationTime();
337 # if wxUSE_FILESYSTEM
348 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
, const wxString
& classname
)
350 UpdateResources(); //ensure everything is up-to-date
353 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
355 if (m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
) continue;
356 for (wxXmlNode
*node
= m_data
[f
].Doc
->GetRoot()->GetChildren();
357 node
; node
= node
->GetNext())
358 if (node
->GetType() == wxXML_ELEMENT_NODE
&&
360 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
361 node
->GetName() == wxT("object") &&
362 node
->GetPropVal(wxT("name"), &dummy
) &&
366 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
372 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
373 name
.c_str(), classname
.c_str());
379 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
381 if (node
== NULL
) return NULL
;
383 wxXmlResourceHandler
*handler
;
385 wxNode
* ND
= m_handlers
.GetFirst();
388 handler
= (wxXmlResourceHandler
*)ND
->GetData();
389 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
391 ret
= handler
->CreateResource(node
, parent
, instance
);
397 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
398 node
->GetName().c_str(),
399 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
411 wxXmlResourceHandler::wxXmlResourceHandler()
412 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
413 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
418 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
420 wxXmlNode
*myNode
= m_node
;
421 wxString myClass
= m_class
;
422 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
423 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
425 m_instance
= instance
;
426 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
427 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
429 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
430 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
433 m_instance
= classInfo
->CreateObject();
437 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
438 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
441 m_instance
= classInfo
->CreateObject();
445 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
447 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
448 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
450 wxObject
*returned
= DoCreateResource();
454 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
455 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
461 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
463 m_styleNames
.Add(name
);
464 m_styleValues
.Add(value
);
469 void wxXmlResourceHandler::AddWindowStyles()
471 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
472 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
473 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
474 XRC_ADD_STYLE(wxRAISED_BORDER
);
475 XRC_ADD_STYLE(wxSTATIC_BORDER
);
476 XRC_ADD_STYLE(wxNO_BORDER
);
477 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
478 XRC_ADD_STYLE(wxWANTS_CHARS
);
479 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
484 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
486 return (GetParamNode(param
) != NULL
);
490 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
492 wxString s
= GetParamValue(param
);
494 if (!s
) return defaults
;
496 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
500 while (tkn
.HasMoreTokens())
502 fl
= tkn
.GetNextToken();
503 index
= m_styleNames
.Index(fl
);
504 if (index
!= wxNOT_FOUND
)
505 style
|= m_styleValues
[index
];
507 wxLogError(_("Unknown style flag ") + fl
);
514 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
521 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
522 str1
= wxGetTranslation(GetParamValue(param
));
524 str1
= GetParamValue(param
);
526 // VS: First version of XRC resources used $ instead of & (which is illegal in XML),
527 // but later I realized that '_' fits this purpose much better (because
528 // &File means "File with F underlined").
529 if (m_resource
->CompareVersion(2,3,0,1) < 0)
534 for (dt
= str1
.c_str(); *dt
; dt
++)
536 // Remap amp_char to &, map double amp_char to amp_char (for things
537 // like "&File..." -- this is illegal in XML, so we use "_File..."):
540 if ( *(++dt
) == amp_char
)
543 str2
<< wxT('&') << *dt
;
545 // Remap \n to CR, \r to LF, \t to TAB:
546 else if (*dt
== wxT('\\'))
549 case wxT('n') : str2
<< wxT('\n'); break;
550 case wxT('t') : str2
<< wxT('\t'); break;
551 case wxT('r') : str2
<< wxT('\r'); break;
552 default : str2
<< wxT('\\') << *dt
; break;
562 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
565 wxString str1
= GetParamValue(param
);
567 if (!str1
.ToLong(&value
))
574 int wxXmlResourceHandler::GetID()
576 wxString sid
= GetName();
579 if (sid
== wxT("-1")) return -1;
580 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
581 #define stdID(id) else if (sid == wxT(#id)) return id
582 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
583 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
584 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
585 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
586 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
587 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
588 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
589 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
590 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
591 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
592 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
593 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
594 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
596 else return wxXmlResource::GetXMLID(sid
);
600 wxString
wxXmlResourceHandler::GetName()
602 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
607 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
609 wxString v
= GetParamValue(param
);
611 if (!v
) return defaultv
;
612 else return (v
== wxT("1"));
617 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
619 wxString v
= GetParamValue(param
);
620 unsigned long tmp
= 0;
622 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
623 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
625 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
626 v
.c_str(), param
.c_str());
630 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
631 (unsigned char) ((tmp
& 0x00FF00) >> 8),
632 (unsigned char) ((tmp
& 0x0000FF)));
637 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
639 wxString name
= GetParamValue(param
);
640 if (name
.IsEmpty()) return wxNullBitmap
;
642 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
645 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
648 wxImage
img(*(fsfile
->GetStream()));
651 wxImage
img(GetParamValue(wxT("bitmap")));
655 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
658 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
659 return img
.ConvertToBitmap();
664 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
666 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
668 icon
.CopyFromBitmap(GetBitmap(param
, size
));
671 wxBitmap bmppt
= GetBitmap(param
, size
);
672 iconpt
= (wxIcon
*)(&bmppt
);
673 wxIcon
icon(*iconpt
);
680 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
682 wxXmlNode
*n
= m_node
->GetChildren();
686 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
694 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
697 if (n
== NULL
) return wxEmptyString
;
698 n
= n
->GetChildren();
702 if (n
->GetType() == wxXML_TEXT_NODE
||
703 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
704 return n
->GetContent();
707 return wxEmptyString
;
712 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
715 return GetNodeContent(m_node
);
717 return GetNodeContent(GetParamNode(param
));
722 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
724 wxString s
= GetParamValue(param
);
725 if (s
.IsEmpty()) s
= wxT("-1,-1");
729 is_dlg
= s
[s
.Length()-1] == wxT('d');
730 if (is_dlg
) s
.RemoveLast();
732 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
733 !s
.AfterLast(wxT(',')).ToLong(&sy
))
735 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
736 return wxDefaultSize
;
741 if (m_instanceAsWindow
)
742 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
743 else if (m_parentAsWindow
)
744 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
747 wxLogError(_("Cannot convert dialog units: dialog unknown."));
748 return wxDefaultSize
;
751 else return wxSize(sx
, sy
);
756 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
758 wxSize sz
= GetSize(param
);
759 return wxPoint(sz
.x
, sz
.y
);
764 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
766 wxString s
= GetParamValue(param
);
767 if (s
.IsEmpty()) return defaultv
;
771 is_dlg
= s
[s
.Length()-1] == wxT('d');
772 if (is_dlg
) s
.RemoveLast();
776 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
782 if (m_instanceAsWindow
)
783 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
784 else if (m_parentAsWindow
)
785 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
788 wxLogError(_("Cannot convert dialog units: dialog unknown."));
797 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
799 wxXmlNode
*font_node
= GetParamNode(param
);
800 if (font_node
== NULL
)
802 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
806 wxXmlNode
*oldnode
= m_node
;
809 long size
= GetLong(wxT("size"), 12);
811 wxString style
= GetParamValue(wxT("style"));
812 wxString weight
= GetParamValue(wxT("weight"));
813 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
814 if (style
== wxT("italic")) istyle
= wxITALIC
;
815 else if (style
== wxT("slant")) istyle
= wxSLANT
;
816 if (weight
== wxT("bold")) iweight
= wxBOLD
;
817 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
819 wxString family
= GetParamValue(wxT("family"));
820 int ifamily
= wxDEFAULT
;
821 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
822 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
823 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
824 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
825 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
827 bool underlined
= GetBool(wxT("underlined"), FALSE
);
829 wxString encoding
= GetParamValue(wxT("encoding"));
831 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
832 if (!encoding
.IsEmpty())
833 enc
= mapper
.CharsetToEncoding(encoding
);
834 if (enc
== wxFONTENCODING_SYSTEM
)
835 enc
= wxFONTENCODING_DEFAULT
;
837 wxString faces
= GetParamValue(wxT("face"));
838 wxString facename
= wxEmptyString
;
839 wxFontEnumerator enu
;
840 enu
.EnumerateFacenames();
841 wxStringTokenizer
tk(faces
, wxT(","));
842 while (tk
.HasMoreTokens())
844 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
845 if (index
!= wxNOT_FOUND
)
847 facename
= (*enu
.GetFacenames())[index
];
854 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
859 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
863 if (HasParam(wxT("exstyle")))
864 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
865 if (HasParam(wxT("bg")))
866 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
867 if (HasParam(wxT("fg")))
868 wnd
->SetForegroundColour(GetColour(wxT("fg")));
869 if (GetBool(wxT("enabled"), 1) == 0)
871 if (GetBool(wxT("focused"), 0) == 1)
873 if (GetBool(wxT("hidden"), 0) == 1)
876 if (HasParam(wxT("tooltip")))
877 wnd
->SetToolTip(GetText(wxT("tooltip")));
879 if (HasParam(wxT("font")))
880 wnd
->SetFont(GetFont());
884 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
886 wxXmlNode
*n
= m_node
->GetChildren();
890 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
891 n
->GetName() == wxT("object"))
893 if (this_hnd_only
&& CanHandle(n
))
894 CreateResource(n
, parent
, NULL
);
896 m_resource
->CreateResFromNode(n
, parent
, NULL
);
903 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
906 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
907 wxXmlNode
*n
= root
->GetChildren();
911 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
913 CreateResource(n
, parent
, NULL
);
925 // --------------- XMLID implementation -----------------------------
927 #define XMLID_TABLE_SIZE 1024
937 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
939 /*static*/ int wxXmlResource::GetXMLID(const wxChar
*str_id
)
941 static int XMLID_LastID
= wxID_HIGHEST
;
945 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
946 index
%= XMLID_TABLE_SIZE
;
948 XMLID_record
*oldrec
= NULL
;
950 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
952 if (wxStrcmp(rec
->key
, str_id
) == 0)
960 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
961 &XMLID_Records
[index
] : &oldrec
->next
;
962 *rec_var
= new XMLID_record
;
963 (*rec_var
)->id
= ++XMLID_LastID
;
964 (*rec_var
)->key
= wxStrdup(str_id
);
965 (*rec_var
)->next
= NULL
;
967 return (*rec_var
)->id
;
971 static void CleanXMLID_Record(XMLID_record
*rec
)
975 CleanXMLID_Record(rec
->next
);
981 static void CleanXMLID_Records()
983 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
984 CleanXMLID_Record(XMLID_Records
[i
]);
994 // --------------- module and globals -----------------------------
997 static wxXmlResource gs_XmlResource
;
999 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
1002 class wxXmlResourceModule
: public wxModule
1004 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1006 wxXmlResourceModule() {}
1007 bool OnInit() {return TRUE
;}
1010 wxTheXmlResource
->ClearHandlers();
1011 CleanXMLID_Records();
1015 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1018 // When wxXml is loaded dynamically after the application is already running
1019 // then the built-in module system won't pick this one up. Add it manually.
1020 void wxXmlInitResourceModule()
1022 wxModule
* module = new wxXmlResourceModule
;
1024 wxModule::RegisterModule(module);