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"
35 #include "wx/artprov.h"
37 #include "wx/xrc/xml.h"
38 #include "wx/xrc/xmlres.h"
40 #include "wx/arrimpl.cpp"
41 WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords
);
44 wxXmlResource
*wxXmlResource::ms_instance
= NULL
;
46 /*static*/ wxXmlResource
*wxXmlResource::Get()
49 ms_instance
= new wxXmlResource
;
53 /*static*/ wxXmlResource
*wxXmlResource::Set(wxXmlResource
*res
)
55 wxXmlResource
*old
= ms_instance
;
60 wxXmlResource::wxXmlResource(int flags
)
62 m_handlers
.DeleteContents(TRUE
);
67 wxXmlResource::wxXmlResource(const wxString
& filemask
, int flags
)
71 m_handlers
.DeleteContents(TRUE
);
75 wxXmlResource::~wxXmlResource()
81 bool wxXmlResource::Load(const wxString
& filemask
)
84 wxXmlResourceDataRecord
*drec
;
85 bool iswild
= wxIsWild(filemask
);
90 # define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)
91 # define wxXmlFindNext fsys.FindNext()
93 # define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)
94 # define wxXmlFindNext wxFindNextFile()
103 if (filemask
.Lower().Matches(wxT("*.zip")) ||
104 filemask
.Lower().Matches(wxT("*.xrs")))
106 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xmlbin"));
107 rt
= rt
&& Load(fnd
+ wxT("#zip:*.xrc"));
112 drec
= new wxXmlResourceDataRecord
;
122 # undef wxXmlFindFirst
123 # undef wxXmlFindNext
129 void wxXmlResource::AddHandler(wxXmlResourceHandler
*handler
)
131 m_handlers
.Append(handler
);
132 handler
->SetParentResource(this);
135 void wxXmlResource::InsertHandler(wxXmlResourceHandler
*handler
)
137 m_handlers
.Insert(handler
);
138 handler
->SetParentResource(this);
143 void wxXmlResource::ClearHandlers()
150 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
152 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
157 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
159 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
165 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
167 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
172 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
174 wxDialog
*dialog
= new wxDialog
;
175 if (!LoadDialog(dialog
, parent
, name
))
176 { delete dialog
; return NULL
; }
180 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
182 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
187 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
189 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
192 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
194 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
197 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
199 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
202 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
204 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
207 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
209 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
210 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
213 if (bmp
) { rt
= *bmp
; delete bmp
; }
217 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
219 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
220 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
223 if (icon
) { rt
= *icon
; delete icon
; }
228 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
230 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
233 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
235 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
239 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
240 wxWindow
*control
, wxWindow
*parent
)
243 parent
= control
->GetParent();
244 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
247 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
250 return control
->Reparent(container
);
254 static void ProcessPlatformProperty(wxXmlNode
*node
)
259 wxXmlNode
*c
= node
->GetChildren();
263 if (!c
->GetPropVal(wxT("platform"), &s
))
267 wxStringTokenizer
tkn(s
, " |");
269 while (tkn
.HasMoreTokens())
271 s
= tkn
.GetNextToken();
274 s
== wxString(wxT("win"))
275 #elif defined(__UNIX__)
276 s
== wxString(wxT("unix"))
277 #elif defined(__MAC__)
278 s
== wxString(wxT("mac"))
279 #elif defined(__OS2__)
280 s
== wxString(wxT("os2"))
290 ProcessPlatformProperty(c
);
295 wxXmlNode
*c2
= c
->GetNext();
296 node
->RemoveChild(c
);
305 void wxXmlResource::UpdateResources()
308 # if wxUSE_FILESYSTEM
309 wxFSFile
*file
= NULL
;
313 wxString
encoding(wxT("UTF-8"));
314 #if !wxUSE_UNICODE && wxUSE_INTL
315 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
317 // In case we are not using wxLocale to translate strings, convert the strings
318 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
319 // it could break wxGetTranslation lookup.
320 encoding
= wxLocale::GetSystemEncodingName();
324 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
326 modif
= (m_data
[i
].Doc
== NULL
);
330 # if wxUSE_FILESYSTEM
331 file
= fsys
.OpenFile(m_data
[i
].File
);
332 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
334 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
337 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
343 wxInputStream
*stream
= NULL
;
345 # if wxUSE_FILESYSTEM
346 file
= fsys
.OpenFile(m_data
[i
].File
);
348 stream
= file
->GetStream();
350 stream
= new wxFileInputStream(m_data
[i
].File
);
355 delete m_data
[i
].Doc
;
356 m_data
[i
].Doc
= new wxXmlDocument
;
358 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
360 wxLogError(_("Cannot load resources from file '%s'."),
361 m_data
[i
].File
.c_str());
362 wxDELETE(m_data
[i
].Doc
);
364 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
366 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
367 wxDELETE(m_data
[i
].Doc
);
373 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
374 wxT("version"), wxT("0.0.0.0"));
375 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
376 &v1
, &v2
, &v3
, &v4
) == 4)
377 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
382 if (m_version
!= version
)
383 wxLogError(_("Resource files must have same version number!"));
385 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
386 m_data
[i
].Time
= file
->GetModificationTime();
389 # if wxUSE_FILESYSTEM
399 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
400 const wxString
& name
,
401 const wxString
& classname
,
407 // first search for match at the top-level nodes (as this is
408 // where the resource is most commonly looked for):
409 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
411 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
412 (node
->GetName() == wxT("object") ||
413 node
->GetName() == wxT("object_ref")) &&
415 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
416 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
421 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
423 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
424 (node
->GetName() == wxT("object") ||
425 node
->GetName() == wxT("object_ref")) )
427 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
436 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
437 const wxString
& classname
,
440 UpdateResources(); //ensure everything is up-to-date
443 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
445 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
448 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
449 name
, classname
, recursive
);
453 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
459 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
460 name
.c_str(), classname
.c_str());
464 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
467 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
469 wxXmlProperty
*dprop
;
470 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
473 if ( dprop
->GetName() == prop
->GetName() )
475 dprop
->SetValue(prop
->GetValue());
481 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
484 // Merge child nodes:
485 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
487 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
490 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
492 if ( dnode
->GetName() == node
->GetName() &&
493 dnode
->GetPropVal("name", wxEmptyString
) == name
&&
494 dnode
->GetType() == node
->GetType() )
496 MergeNodes(*dnode
, *node
);
502 dest
.AddChild(new wxXmlNode(*node
));
505 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
506 dest
.SetContent(with
.GetContent());
509 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
511 if (node
== NULL
) return NULL
;
513 // handling of referenced resource
514 if ( node
->GetName() == wxT("object_ref") )
516 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
517 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
521 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
526 wxXmlNode
copy(*refNode
);
527 MergeNodes(copy
, *node
);
529 return CreateResFromNode(©
, parent
, instance
);
532 wxXmlResourceHandler
*handler
;
534 wxNode
* ND
= m_handlers
.GetFirst();
537 handler
= (wxXmlResourceHandler
*)ND
->GetData();
538 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
540 ret
= handler
->CreateResource(node
, parent
, instance
);
546 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
547 node
->GetName().c_str(),
548 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
555 wxXmlResourceHandler::wxXmlResourceHandler()
556 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
557 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
562 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
564 wxXmlNode
*myNode
= m_node
;
565 wxString myClass
= m_class
;
566 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
567 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
569 m_instance
= instance
;
570 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
571 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
573 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
574 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
577 m_instance
= classInfo
->CreateObject();
581 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
582 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
585 m_instance
= classInfo
->CreateObject();
589 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
591 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
592 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
594 wxObject
*returned
= DoCreateResource();
598 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
599 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
605 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
607 m_styleNames
.Add(name
);
608 m_styleValues
.Add(value
);
613 void wxXmlResourceHandler::AddWindowStyles()
615 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
616 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
617 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
618 XRC_ADD_STYLE(wxRAISED_BORDER
);
619 XRC_ADD_STYLE(wxSTATIC_BORDER
);
620 XRC_ADD_STYLE(wxNO_BORDER
);
621 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
622 XRC_ADD_STYLE(wxWANTS_CHARS
);
623 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
628 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
630 return (GetParamNode(param
) != NULL
);
634 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
636 wxString s
= GetParamValue(param
);
638 if (!s
) return defaults
;
640 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
644 while (tkn
.HasMoreTokens())
646 fl
= tkn
.GetNextToken();
647 index
= m_styleNames
.Index(fl
);
648 if (index
!= wxNOT_FOUND
)
649 style
|= m_styleValues
[index
];
651 wxLogError(_("Unknown style flag ") + fl
);
658 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
660 wxString
str1(GetParamValue(param
));
665 // VS: First version of XRC resources used $ instead of & (which is
666 // illegal in XML), but later I realized that '_' fits this purpose
667 // much better (because &File means "File with F underlined").
668 if (m_resource
->CompareVersion(2,3,0,1) < 0)
673 for (dt
= str1
.c_str(); *dt
; dt
++)
675 // Remap amp_char to &, map double amp_char to amp_char (for things
676 // like "&File..." -- this is illegal in XML, so we use "_File..."):
679 if ( *(++dt
) == amp_char
)
682 str2
<< wxT('&') << *dt
;
684 // Remap \n to CR, \r to LF, \t to TAB:
685 else if (*dt
== wxT('\\'))
688 case wxT('n') : str2
<< wxT('\n'); break;
689 case wxT('t') : str2
<< wxT('\t'); break;
690 case wxT('r') : str2
<< wxT('\r'); break;
691 default : str2
<< wxT('\\') << *dt
; break;
696 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
697 return wxGetTranslation(str2
);
705 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
708 wxString str1
= GetParamValue(param
);
710 if (!str1
.ToLong(&value
))
718 int wxXmlResourceHandler::GetID()
720 wxString sid
= GetName();
723 if (sid
== wxT("-1")) return -1;
724 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
725 #define stdID(id) else if (sid == wxT(#id)) return id
726 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
727 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
728 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
729 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
730 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
731 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
732 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
733 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
734 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
735 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
736 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
737 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
738 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
740 else return wxXmlResource::GetXRCID(sid
);
745 wxString
wxXmlResourceHandler::GetName()
747 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
752 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
754 wxString v
= GetParamValue(param
);
756 if (!v
) return defaultv
;
757 else return (v
== wxT("1"));
762 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
764 wxString v
= GetParamValue(param
);
765 unsigned long tmp
= 0;
767 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
768 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
770 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
771 v
.c_str(), param
.c_str());
775 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
776 (unsigned char) ((tmp
& 0x00FF00) >> 8),
777 (unsigned char) ((tmp
& 0x0000FF)));
782 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
783 const wxArtClient
& defaultArtClient
,
786 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
787 wxXmlNode
*bmpNode
= GetParamNode(param
);
790 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
793 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
795 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
796 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
803 /* ...or load the bitmap from file: */
804 wxString name
= GetParamValue(param
);
805 if (name
.IsEmpty()) return wxNullBitmap
;
807 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
810 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
813 wxImage
img(*(fsfile
->GetStream()));
816 wxImage
img(GetParamValue(wxT("bitmap")));
821 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
824 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
825 return wxBitmap(img
);
831 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
832 const wxArtClient
& defaultArtClient
,
835 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
837 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
840 wxBitmap bmppt
= GetBitmap(param
, size
);
841 iconpt
= (wxIcon
*)(&bmppt
);
842 wxIcon
icon(*iconpt
);
849 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
851 wxXmlNode
*n
= m_node
->GetChildren();
855 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
863 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
866 if (n
== NULL
) return wxEmptyString
;
867 n
= n
->GetChildren();
871 if (n
->GetType() == wxXML_TEXT_NODE
||
872 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
873 return n
->GetContent();
876 return wxEmptyString
;
881 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
884 return GetNodeContent(m_node
);
886 return GetNodeContent(GetParamNode(param
));
891 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
893 wxString s
= GetParamValue(param
);
894 if (s
.IsEmpty()) s
= wxT("-1,-1");
898 is_dlg
= s
[s
.Length()-1] == wxT('d');
899 if (is_dlg
) s
.RemoveLast();
901 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
902 !s
.AfterLast(wxT(',')).ToLong(&sy
))
904 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
905 return wxDefaultSize
;
910 if (m_instanceAsWindow
)
911 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
912 else if (m_parentAsWindow
)
913 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
916 wxLogError(_("Cannot convert dialog units: dialog unknown."));
917 return wxDefaultSize
;
920 else return wxSize(sx
, sy
);
925 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
927 wxSize sz
= GetSize(param
);
928 return wxPoint(sz
.x
, sz
.y
);
933 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
935 wxString s
= GetParamValue(param
);
936 if (s
.IsEmpty()) return defaultv
;
940 is_dlg
= s
[s
.Length()-1] == wxT('d');
941 if (is_dlg
) s
.RemoveLast();
945 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
951 if (m_instanceAsWindow
)
952 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
953 else if (m_parentAsWindow
)
954 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
957 wxLogError(_("Cannot convert dialog units: dialog unknown."));
966 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
968 wxXmlNode
*font_node
= GetParamNode(param
);
969 if (font_node
== NULL
)
971 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
975 wxXmlNode
*oldnode
= m_node
;
978 long size
= GetLong(wxT("size"), 12);
980 wxString style
= GetParamValue(wxT("style"));
981 wxString weight
= GetParamValue(wxT("weight"));
982 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
983 if (style
== wxT("italic")) istyle
= wxITALIC
;
984 else if (style
== wxT("slant")) istyle
= wxSLANT
;
985 if (weight
== wxT("bold")) iweight
= wxBOLD
;
986 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
988 wxString family
= GetParamValue(wxT("family"));
989 int ifamily
= wxDEFAULT
;
990 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
991 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
992 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
993 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
994 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
996 bool underlined
= GetBool(wxT("underlined"), FALSE
);
998 wxString encoding
= GetParamValue(wxT("encoding"));
1000 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1001 if (!encoding
.IsEmpty())
1002 enc
= mapper
.CharsetToEncoding(encoding
);
1003 if (enc
== wxFONTENCODING_SYSTEM
)
1004 enc
= wxFONTENCODING_DEFAULT
;
1006 wxString faces
= GetParamValue(wxT("face"));
1007 wxString facename
= wxEmptyString
;
1008 wxFontEnumerator enu
;
1009 enu
.EnumerateFacenames();
1010 wxStringTokenizer
tk(faces
, wxT(","));
1011 while (tk
.HasMoreTokens())
1013 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1014 if (index
!= wxNOT_FOUND
)
1016 facename
= (*enu
.GetFacenames())[index
];
1023 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1028 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1030 //FIXME : add cursor
1032 if (HasParam(wxT("exstyle")))
1033 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1034 if (HasParam(wxT("bg")))
1035 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1036 if (HasParam(wxT("fg")))
1037 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1038 if (GetBool(wxT("enabled"), 1) == 0)
1040 if (GetBool(wxT("focused"), 0) == 1)
1042 if (GetBool(wxT("hidden"), 0) == 1)
1045 if (HasParam(wxT("tooltip")))
1046 wnd
->SetToolTip(GetText(wxT("tooltip")));
1048 if (HasParam(wxT("font")))
1049 wnd
->SetFont(GetFont());
1053 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1055 wxXmlNode
*n
= m_node
->GetChildren();
1059 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1060 n
->GetName() == wxT("object"))
1062 if (this_hnd_only
&& CanHandle(n
))
1063 CreateResource(n
, parent
, NULL
);
1065 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1072 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1075 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1076 wxXmlNode
*n
= root
->GetChildren();
1080 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1082 CreateResource(n
, parent
, NULL
);
1094 // --------------- XRCID implementation -----------------------------
1096 #define XRCID_TABLE_SIZE 1024
1106 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1108 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1110 static int XRCID_LastID
= wxID_HIGHEST
;
1114 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1115 index
%= XRCID_TABLE_SIZE
;
1117 XRCID_record
*oldrec
= NULL
;
1119 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1121 if (wxStrcmp(rec
->key
, str_id
) == 0)
1129 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1130 &XRCID_Records
[index
] : &oldrec
->next
;
1131 *rec_var
= new XRCID_record
;
1132 (*rec_var
)->id
= ++XRCID_LastID
;
1133 (*rec_var
)->key
= wxStrdup(str_id
);
1134 (*rec_var
)->next
= NULL
;
1136 return (*rec_var
)->id
;
1140 static void CleanXRCID_Record(XRCID_record
*rec
)
1144 CleanXRCID_Record(rec
->next
);
1150 static void CleanXRCID_Records()
1152 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1153 CleanXRCID_Record(XRCID_Records
[i
]);
1163 // --------------- module and globals -----------------------------
1165 class wxXmlResourceModule
: public wxModule
1167 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1169 wxXmlResourceModule() {}
1176 delete wxXmlResource::Set(NULL
);
1177 CleanXRCID_Records();
1181 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1184 // When wxXml is loaded dynamically after the application is already running
1185 // then the built-in module system won't pick this one up. Add it manually.
1186 void wxXmlInitResourceModule()
1188 wxModule
* module = new wxXmlResourceModule
;
1190 wxModule::RegisterModule(module);