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
347 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
348 const wxString
& name
,
349 const wxString
& classname
,
355 // first search for match at the top-level nodes (as this is
356 // where the resource is most commonly looked for):
357 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
359 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
360 (node
->GetName() == wxT("object") ||
361 node
->GetName() == wxT("object_ref")) &&
363 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
364 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
369 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
371 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
372 (node
->GetName() == wxT("object") ||
373 node
->GetName() == wxT("object_ref")) )
375 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
384 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
385 const wxString
& classname
,
388 UpdateResources(); //ensure everything is up-to-date
391 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
393 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
396 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
397 name
, classname
, recursive
);
401 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
407 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
408 name
.c_str(), classname
.c_str());
412 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
415 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
417 wxXmlProperty
*dprop
;
418 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
421 if ( dprop
->GetName() == prop
->GetName() )
423 dprop
->SetValue(prop
->GetValue());
429 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
432 // Merge child nodes:
433 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
435 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
438 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
440 if ( dnode
->GetName() == node
->GetName() &&
441 dnode
->GetPropVal("name", wxEmptyString
) == name
&&
442 dnode
->GetType() == node
->GetType() )
444 MergeNodes(*dnode
, *node
);
450 dest
.AddChild(new wxXmlNode(*node
));
453 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
454 dest
.SetContent(with
.GetContent());
457 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
459 if (node
== NULL
) return NULL
;
461 // handling of referenced resource
462 if ( node
->GetName() == wxT("object_ref") )
464 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
465 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
469 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
474 wxXmlNode
copy(*refNode
);
475 MergeNodes(copy
, *node
);
477 return CreateResFromNode(©
, parent
, instance
);
480 wxXmlResourceHandler
*handler
;
482 wxNode
* ND
= m_handlers
.GetFirst();
485 handler
= (wxXmlResourceHandler
*)ND
->GetData();
486 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
488 ret
= handler
->CreateResource(node
, parent
, instance
);
494 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
495 node
->GetName().c_str(),
496 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
503 wxXmlResourceHandler::wxXmlResourceHandler()
504 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
505 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
510 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
512 wxXmlNode
*myNode
= m_node
;
513 wxString myClass
= m_class
;
514 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
515 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
517 m_instance
= instance
;
518 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
519 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
521 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
522 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
525 m_instance
= classInfo
->CreateObject();
529 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
530 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
533 m_instance
= classInfo
->CreateObject();
537 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
539 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
540 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
542 wxObject
*returned
= DoCreateResource();
546 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
547 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
553 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
555 m_styleNames
.Add(name
);
556 m_styleValues
.Add(value
);
561 void wxXmlResourceHandler::AddWindowStyles()
563 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
564 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
565 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
566 XRC_ADD_STYLE(wxRAISED_BORDER
);
567 XRC_ADD_STYLE(wxSTATIC_BORDER
);
568 XRC_ADD_STYLE(wxNO_BORDER
);
569 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
570 XRC_ADD_STYLE(wxWANTS_CHARS
);
571 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
576 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
578 return (GetParamNode(param
) != NULL
);
582 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
584 wxString s
= GetParamValue(param
);
586 if (!s
) return defaults
;
588 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
592 while (tkn
.HasMoreTokens())
594 fl
= tkn
.GetNextToken();
595 index
= m_styleNames
.Index(fl
);
596 if (index
!= wxNOT_FOUND
)
597 style
|= m_styleValues
[index
];
599 wxLogError(_("Unknown style flag ") + fl
);
606 wxString
wxXmlResourceHandler::GetText(const wxString
& param
)
613 if (m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
614 str1
= wxGetTranslation(GetParamValue(param
));
616 str1
= GetParamValue(param
);
618 // VS: First version of XRC resources used $ instead of & (which is illegal in XML),
619 // but later I realized that '_' fits this purpose much better (because
620 // &File means "File with F underlined").
621 if (m_resource
->CompareVersion(2,3,0,1) < 0)
626 for (dt
= str1
.c_str(); *dt
; dt
++)
628 // Remap amp_char to &, map double amp_char to amp_char (for things
629 // like "&File..." -- this is illegal in XML, so we use "_File..."):
632 if ( *(++dt
) == amp_char
)
635 str2
<< wxT('&') << *dt
;
637 // Remap \n to CR, \r to LF, \t to TAB:
638 else if (*dt
== wxT('\\'))
641 case wxT('n') : str2
<< wxT('\n'); break;
642 case wxT('t') : str2
<< wxT('\t'); break;
643 case wxT('r') : str2
<< wxT('\r'); break;
644 default : str2
<< wxT('\\') << *dt
; break;
654 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
657 wxString str1
= GetParamValue(param
);
659 if (!str1
.ToLong(&value
))
666 int wxXmlResourceHandler::GetID()
668 wxString sid
= GetName();
671 if (sid
== wxT("-1")) return -1;
672 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
673 #define stdID(id) else if (sid == wxT(#id)) return id
674 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
675 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
676 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
677 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
678 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
679 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
680 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
681 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
682 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
683 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
684 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
685 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
686 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
688 else return wxXmlResource::GetXMLID(sid
);
692 wxString
wxXmlResourceHandler::GetName()
694 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
699 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
701 wxString v
= GetParamValue(param
);
703 if (!v
) return defaultv
;
704 else return (v
== wxT("1"));
709 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
711 wxString v
= GetParamValue(param
);
712 unsigned long tmp
= 0;
714 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
715 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
717 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
718 v
.c_str(), param
.c_str());
722 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
723 (unsigned char) ((tmp
& 0x00FF00) >> 8),
724 (unsigned char) ((tmp
& 0x0000FF)));
729 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
731 wxString name
= GetParamValue(param
);
732 if (name
.IsEmpty()) return wxNullBitmap
;
734 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
737 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
740 wxImage
img(*(fsfile
->GetStream()));
743 wxImage
img(GetParamValue(wxT("bitmap")));
747 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
750 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
751 return img
.ConvertToBitmap();
756 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
758 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
760 icon
.CopyFromBitmap(GetBitmap(param
, size
));
763 wxBitmap bmppt
= GetBitmap(param
, size
);
764 iconpt
= (wxIcon
*)(&bmppt
);
765 wxIcon
icon(*iconpt
);
772 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
774 wxXmlNode
*n
= m_node
->GetChildren();
778 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
786 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
789 if (n
== NULL
) return wxEmptyString
;
790 n
= n
->GetChildren();
794 if (n
->GetType() == wxXML_TEXT_NODE
||
795 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
796 return n
->GetContent();
799 return wxEmptyString
;
804 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
807 return GetNodeContent(m_node
);
809 return GetNodeContent(GetParamNode(param
));
814 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
816 wxString s
= GetParamValue(param
);
817 if (s
.IsEmpty()) s
= wxT("-1,-1");
821 is_dlg
= s
[s
.Length()-1] == wxT('d');
822 if (is_dlg
) s
.RemoveLast();
824 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
825 !s
.AfterLast(wxT(',')).ToLong(&sy
))
827 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
828 return wxDefaultSize
;
833 if (m_instanceAsWindow
)
834 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
835 else if (m_parentAsWindow
)
836 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
839 wxLogError(_("Cannot convert dialog units: dialog unknown."));
840 return wxDefaultSize
;
843 else return wxSize(sx
, sy
);
848 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
850 wxSize sz
= GetSize(param
);
851 return wxPoint(sz
.x
, sz
.y
);
856 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
858 wxString s
= GetParamValue(param
);
859 if (s
.IsEmpty()) return defaultv
;
863 is_dlg
= s
[s
.Length()-1] == wxT('d');
864 if (is_dlg
) s
.RemoveLast();
868 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
874 if (m_instanceAsWindow
)
875 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
876 else if (m_parentAsWindow
)
877 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
880 wxLogError(_("Cannot convert dialog units: dialog unknown."));
889 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
891 wxXmlNode
*font_node
= GetParamNode(param
);
892 if (font_node
== NULL
)
894 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
898 wxXmlNode
*oldnode
= m_node
;
901 long size
= GetLong(wxT("size"), 12);
903 wxString style
= GetParamValue(wxT("style"));
904 wxString weight
= GetParamValue(wxT("weight"));
905 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
906 if (style
== wxT("italic")) istyle
= wxITALIC
;
907 else if (style
== wxT("slant")) istyle
= wxSLANT
;
908 if (weight
== wxT("bold")) iweight
= wxBOLD
;
909 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
911 wxString family
= GetParamValue(wxT("family"));
912 int ifamily
= wxDEFAULT
;
913 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
914 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
915 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
916 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
917 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
919 bool underlined
= GetBool(wxT("underlined"), FALSE
);
921 wxString encoding
= GetParamValue(wxT("encoding"));
923 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
924 if (!encoding
.IsEmpty())
925 enc
= mapper
.CharsetToEncoding(encoding
);
926 if (enc
== wxFONTENCODING_SYSTEM
)
927 enc
= wxFONTENCODING_DEFAULT
;
929 wxString faces
= GetParamValue(wxT("face"));
930 wxString facename
= wxEmptyString
;
931 wxFontEnumerator enu
;
932 enu
.EnumerateFacenames();
933 wxStringTokenizer
tk(faces
, wxT(","));
934 while (tk
.HasMoreTokens())
936 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
937 if (index
!= wxNOT_FOUND
)
939 facename
= (*enu
.GetFacenames())[index
];
946 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
951 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
955 if (HasParam(wxT("exstyle")))
956 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
957 if (HasParam(wxT("bg")))
958 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
959 if (HasParam(wxT("fg")))
960 wnd
->SetForegroundColour(GetColour(wxT("fg")));
961 if (GetBool(wxT("enabled"), 1) == 0)
963 if (GetBool(wxT("focused"), 0) == 1)
965 if (GetBool(wxT("hidden"), 0) == 1)
968 if (HasParam(wxT("tooltip")))
969 wnd
->SetToolTip(GetText(wxT("tooltip")));
971 if (HasParam(wxT("font")))
972 wnd
->SetFont(GetFont());
976 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
978 wxXmlNode
*n
= m_node
->GetChildren();
982 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
983 n
->GetName() == wxT("object"))
985 if (this_hnd_only
&& CanHandle(n
))
986 CreateResource(n
, parent
, NULL
);
988 m_resource
->CreateResFromNode(n
, parent
, NULL
);
995 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
998 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
999 wxXmlNode
*n
= root
->GetChildren();
1003 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1005 CreateResource(n
, parent
, NULL
);
1017 // --------------- XMLID implementation -----------------------------
1019 #define XMLID_TABLE_SIZE 1024
1029 static XMLID_record
*XMLID_Records
[XMLID_TABLE_SIZE
] = {NULL
};
1031 /*static*/ int wxXmlResource::GetXMLID(const wxChar
*str_id
)
1033 static int XMLID_LastID
= wxID_HIGHEST
;
1037 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1038 index
%= XMLID_TABLE_SIZE
;
1040 XMLID_record
*oldrec
= NULL
;
1042 for (XMLID_record
*rec
= XMLID_Records
[index
]; rec
; rec
= rec
->next
)
1044 if (wxStrcmp(rec
->key
, str_id
) == 0)
1052 XMLID_record
**rec_var
= (oldrec
== NULL
) ?
1053 &XMLID_Records
[index
] : &oldrec
->next
;
1054 *rec_var
= new XMLID_record
;
1055 (*rec_var
)->id
= ++XMLID_LastID
;
1056 (*rec_var
)->key
= wxStrdup(str_id
);
1057 (*rec_var
)->next
= NULL
;
1059 return (*rec_var
)->id
;
1063 static void CleanXMLID_Record(XMLID_record
*rec
)
1067 CleanXMLID_Record(rec
->next
);
1073 static void CleanXMLID_Records()
1075 for (int i
= 0; i
< XMLID_TABLE_SIZE
; i
++)
1076 CleanXMLID_Record(XMLID_Records
[i
]);
1086 // --------------- module and globals -----------------------------
1089 static wxXmlResource gs_XmlResource
;
1091 wxXmlResource
*wxTheXmlResource
= &gs_XmlResource
;
1094 class wxXmlResourceModule
: public wxModule
1096 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1098 wxXmlResourceModule() {}
1099 bool OnInit() {return TRUE
;}
1102 wxTheXmlResource
->ClearHandlers();
1103 CleanXMLID_Records();
1107 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1110 // When wxXml is loaded dynamically after the application is already running
1111 // then the built-in module system won't pick this one up. Add it manually.
1112 void wxXmlInitResourceModule()
1114 wxModule
* module = new wxXmlResourceModule
;
1116 wxModule::RegisterModule(module);