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 return (wxDialog
*)CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, NULL
);
177 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
179 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
184 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
186 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
189 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
191 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
194 wxFrame
*wxXmlResource::LoadFrame(wxWindow
* parent
, const wxString
& name
)
196 return (wxFrame
*)CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, NULL
);
199 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
201 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
204 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
206 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
207 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
210 if (bmp
) { rt
= *bmp
; delete bmp
; }
214 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
216 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
217 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
220 if (icon
) { rt
= *icon
; delete icon
; }
225 wxObject
*wxXmlResource::LoadObject(wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
227 return CreateResFromNode(FindResource(name
, classname
), parent
, NULL
);
230 bool wxXmlResource::LoadObject(wxObject
*instance
, wxWindow
*parent
, const wxString
& name
, const wxString
& classname
)
232 return CreateResFromNode(FindResource(name
, classname
), parent
, instance
) != NULL
;
236 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
237 wxWindow
*control
, wxWindow
*parent
)
240 parent
= control
->GetParent();
241 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
244 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
247 return control
->Reparent(container
);
251 static void ProcessPlatformProperty(wxXmlNode
*node
)
256 wxXmlNode
*c
= node
->GetChildren();
260 if (!c
->GetPropVal(wxT("platform"), &s
))
264 wxStringTokenizer
tkn(s
, " |");
266 while (tkn
.HasMoreTokens())
268 s
= tkn
.GetNextToken();
271 s
== wxString(wxT("win"))
272 #elif defined(__UNIX__)
273 s
== wxString(wxT("unix"))
274 #elif defined(__MAC__)
275 s
== wxString(wxT("mac"))
276 #elif defined(__OS2__)
277 s
== wxString(wxT("os2"))
287 ProcessPlatformProperty(c
);
292 wxXmlNode
*c2
= c
->GetNext();
293 node
->RemoveChild(c
);
302 void wxXmlResource::UpdateResources()
305 # if wxUSE_FILESYSTEM
306 wxFSFile
*file
= NULL
;
310 wxString
encoding(wxT("UTF-8"));
311 #if !wxUSE_UNICODE && wxUSE_INTL
312 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
314 // In case we are not using wxLocale to translate strings, convert the strings
315 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
316 // it could break wxGetTranslation lookup.
317 encoding
= wxLocale::GetSystemEncodingName();
321 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
323 modif
= (m_data
[i
].Doc
== NULL
);
327 # if wxUSE_FILESYSTEM
328 file
= fsys
.OpenFile(m_data
[i
].File
);
329 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
331 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
334 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
340 wxInputStream
*stream
= NULL
;
342 # if wxUSE_FILESYSTEM
343 file
= fsys
.OpenFile(m_data
[i
].File
);
345 stream
= file
->GetStream();
347 stream
= new wxFileInputStream(m_data
[i
].File
);
352 delete m_data
[i
].Doc
;
353 m_data
[i
].Doc
= new wxXmlDocument
;
355 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
357 wxLogError(_("Cannot load resources from file '%s'."),
358 m_data
[i
].File
.c_str());
359 wxDELETE(m_data
[i
].Doc
);
361 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
363 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
364 wxDELETE(m_data
[i
].Doc
);
370 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
371 wxT("version"), wxT("0.0.0.0"));
372 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
373 &v1
, &v2
, &v3
, &v4
) == 4)
374 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
379 if (m_version
!= version
)
380 wxLogError(_("Resource files must have same version number!"));
382 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
383 m_data
[i
].Time
= file
->GetModificationTime();
386 # if wxUSE_FILESYSTEM
396 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
397 const wxString
& name
,
398 const wxString
& classname
,
404 // first search for match at the top-level nodes (as this is
405 // where the resource is most commonly looked for):
406 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
408 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
409 (node
->GetName() == wxT("object") ||
410 node
->GetName() == wxT("object_ref")) &&
412 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
413 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
418 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
420 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
421 (node
->GetName() == wxT("object") ||
422 node
->GetName() == wxT("object_ref")) )
424 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
433 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
434 const wxString
& classname
,
437 UpdateResources(); //ensure everything is up-to-date
440 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
442 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
445 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
446 name
, classname
, recursive
);
450 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
456 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
457 name
.c_str(), classname
.c_str());
461 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
464 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
466 wxXmlProperty
*dprop
;
467 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
470 if ( dprop
->GetName() == prop
->GetName() )
472 dprop
->SetValue(prop
->GetValue());
478 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
481 // Merge child nodes:
482 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
484 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
487 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
489 if ( dnode
->GetName() == node
->GetName() &&
490 dnode
->GetPropVal("name", wxEmptyString
) == name
&&
491 dnode
->GetType() == node
->GetType() )
493 MergeNodes(*dnode
, *node
);
499 dest
.AddChild(new wxXmlNode(*node
));
502 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
503 dest
.SetContent(with
.GetContent());
506 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
508 if (node
== NULL
) return NULL
;
510 // handling of referenced resource
511 if ( node
->GetName() == wxT("object_ref") )
513 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
514 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
518 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
523 wxXmlNode
copy(*refNode
);
524 MergeNodes(copy
, *node
);
526 return CreateResFromNode(©
, parent
, instance
);
529 wxXmlResourceHandler
*handler
;
531 wxNode
* ND
= m_handlers
.GetFirst();
534 handler
= (wxXmlResourceHandler
*)ND
->GetData();
535 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
537 ret
= handler
->CreateResource(node
, parent
, instance
);
543 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
544 node
->GetName().c_str(),
545 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
552 wxXmlResourceHandler::wxXmlResourceHandler()
553 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
554 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
559 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
561 wxXmlNode
*myNode
= m_node
;
562 wxString myClass
= m_class
;
563 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
564 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
566 m_instance
= instance
;
567 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
568 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
570 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
571 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
574 m_instance
= classInfo
->CreateObject();
578 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
579 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
582 m_instance
= classInfo
->CreateObject();
586 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
588 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
589 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
591 wxObject
*returned
= DoCreateResource();
595 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
596 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
602 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
604 m_styleNames
.Add(name
);
605 m_styleValues
.Add(value
);
610 void wxXmlResourceHandler::AddWindowStyles()
612 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
613 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
614 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
615 XRC_ADD_STYLE(wxRAISED_BORDER
);
616 XRC_ADD_STYLE(wxSTATIC_BORDER
);
617 XRC_ADD_STYLE(wxNO_BORDER
);
618 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
619 XRC_ADD_STYLE(wxWANTS_CHARS
);
620 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
625 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
627 return (GetParamNode(param
) != NULL
);
631 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
633 wxString s
= GetParamValue(param
);
635 if (!s
) return defaults
;
637 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
641 while (tkn
.HasMoreTokens())
643 fl
= tkn
.GetNextToken();
644 index
= m_styleNames
.Index(fl
);
645 if (index
!= wxNOT_FOUND
)
646 style
|= m_styleValues
[index
];
648 wxLogError(_("Unknown style flag ") + fl
);
655 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
657 wxString
str1(GetParamValue(param
));
662 // VS: First version of XRC resources used $ instead of & (which is
663 // illegal in XML), but later I realized that '_' fits this purpose
664 // much better (because &File means "File with F underlined").
665 if (m_resource
->CompareVersion(2,3,0,1) < 0)
670 for (dt
= str1
.c_str(); *dt
; dt
++)
672 // Remap amp_char to &, map double amp_char to amp_char (for things
673 // like "&File..." -- this is illegal in XML, so we use "_File..."):
676 if ( *(++dt
) == amp_char
)
679 str2
<< wxT('&') << *dt
;
681 // Remap \n to CR, \r to LF, \t to TAB:
682 else if (*dt
== wxT('\\'))
685 case wxT('n') : str2
<< wxT('\n'); break;
686 case wxT('t') : str2
<< wxT('\t'); break;
687 case wxT('r') : str2
<< wxT('\r'); break;
688 default : str2
<< wxT('\\') << *dt
; break;
693 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
694 return wxGetTranslation(str2
);
702 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
705 wxString str1
= GetParamValue(param
);
707 if (!str1
.ToLong(&value
))
715 int wxXmlResourceHandler::GetID()
717 wxString sid
= GetName();
720 if (sid
== wxT("-1")) return -1;
721 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
722 #define stdID(id) else if (sid == wxT(#id)) return id
723 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
724 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
725 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
726 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
727 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
728 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
729 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
730 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
731 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
732 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
733 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
734 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
735 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
737 else return wxXmlResource::GetXRCID(sid
);
742 wxString
wxXmlResourceHandler::GetName()
744 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
749 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
751 wxString v
= GetParamValue(param
);
753 if (!v
) return defaultv
;
754 else return (v
== wxT("1"));
759 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
761 wxString v
= GetParamValue(param
);
762 unsigned long tmp
= 0;
764 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
765 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
767 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
768 v
.c_str(), param
.c_str());
772 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
773 (unsigned char) ((tmp
& 0x00FF00) >> 8),
774 (unsigned char) ((tmp
& 0x0000FF)));
779 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
,
780 const wxArtClient
& defaultArtClient
,
783 /* If the bitmap is specified as stock item, query wxArtProvider for it: */
784 wxXmlNode
*bmpNode
= GetParamNode(param
);
787 wxString sid
= bmpNode
->GetPropVal(wxT("stock_id"), wxEmptyString
);
790 wxString scl
= bmpNode
->GetPropVal(wxT("stock_client"), defaultArtClient
);
792 wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid
),
793 wxART_MAKE_CLIENT_ID_FROM_STR(scl
),
800 /* ...or load the bitmap from file: */
801 wxString name
= GetParamValue(param
);
802 if (name
.IsEmpty()) return wxNullBitmap
;
804 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
807 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
810 wxImage
img(*(fsfile
->GetStream()));
813 wxImage
img(GetParamValue(wxT("bitmap")));
818 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
821 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
822 return wxBitmap(img
);
828 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
,
829 const wxArtClient
& defaultArtClient
,
832 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
834 icon
.CopyFromBitmap(GetBitmap(param
, defaultArtClient
, size
));
837 wxBitmap bmppt
= GetBitmap(param
, size
);
838 iconpt
= (wxIcon
*)(&bmppt
);
839 wxIcon
icon(*iconpt
);
846 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
848 wxXmlNode
*n
= m_node
->GetChildren();
852 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
860 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
863 if (n
== NULL
) return wxEmptyString
;
864 n
= n
->GetChildren();
868 if (n
->GetType() == wxXML_TEXT_NODE
||
869 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
870 return n
->GetContent();
873 return wxEmptyString
;
878 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
881 return GetNodeContent(m_node
);
883 return GetNodeContent(GetParamNode(param
));
888 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
890 wxString s
= GetParamValue(param
);
891 if (s
.IsEmpty()) s
= wxT("-1,-1");
895 is_dlg
= s
[s
.Length()-1] == wxT('d');
896 if (is_dlg
) s
.RemoveLast();
898 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
899 !s
.AfterLast(wxT(',')).ToLong(&sy
))
901 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
902 return wxDefaultSize
;
907 if (m_instanceAsWindow
)
908 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
909 else if (m_parentAsWindow
)
910 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
913 wxLogError(_("Cannot convert dialog units: dialog unknown."));
914 return wxDefaultSize
;
917 else return wxSize(sx
, sy
);
922 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
924 wxSize sz
= GetSize(param
);
925 return wxPoint(sz
.x
, sz
.y
);
930 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
932 wxString s
= GetParamValue(param
);
933 if (s
.IsEmpty()) return defaultv
;
937 is_dlg
= s
[s
.Length()-1] == wxT('d');
938 if (is_dlg
) s
.RemoveLast();
942 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
948 if (m_instanceAsWindow
)
949 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
950 else if (m_parentAsWindow
)
951 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
954 wxLogError(_("Cannot convert dialog units: dialog unknown."));
963 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
965 wxXmlNode
*font_node
= GetParamNode(param
);
966 if (font_node
== NULL
)
968 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
972 wxXmlNode
*oldnode
= m_node
;
975 long size
= GetLong(wxT("size"), 12);
977 wxString style
= GetParamValue(wxT("style"));
978 wxString weight
= GetParamValue(wxT("weight"));
979 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
980 if (style
== wxT("italic")) istyle
= wxITALIC
;
981 else if (style
== wxT("slant")) istyle
= wxSLANT
;
982 if (weight
== wxT("bold")) iweight
= wxBOLD
;
983 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
985 wxString family
= GetParamValue(wxT("family"));
986 int ifamily
= wxDEFAULT
;
987 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
988 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
989 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
990 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
991 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
993 bool underlined
= GetBool(wxT("underlined"), FALSE
);
995 wxString encoding
= GetParamValue(wxT("encoding"));
997 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
998 if (!encoding
.IsEmpty())
999 enc
= mapper
.CharsetToEncoding(encoding
);
1000 if (enc
== wxFONTENCODING_SYSTEM
)
1001 enc
= wxFONTENCODING_DEFAULT
;
1003 wxString faces
= GetParamValue(wxT("face"));
1004 wxString facename
= wxEmptyString
;
1005 wxFontEnumerator enu
;
1006 enu
.EnumerateFacenames();
1007 wxStringTokenizer
tk(faces
, wxT(","));
1008 while (tk
.HasMoreTokens())
1010 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1011 if (index
!= wxNOT_FOUND
)
1013 facename
= (*enu
.GetFacenames())[index
];
1020 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1025 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1027 //FIXME : add cursor
1029 if (HasParam(wxT("exstyle")))
1030 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1031 if (HasParam(wxT("bg")))
1032 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1033 if (HasParam(wxT("fg")))
1034 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1035 if (GetBool(wxT("enabled"), 1) == 0)
1037 if (GetBool(wxT("focused"), 0) == 1)
1039 if (GetBool(wxT("hidden"), 0) == 1)
1042 if (HasParam(wxT("tooltip")))
1043 wnd
->SetToolTip(GetText(wxT("tooltip")));
1045 if (HasParam(wxT("font")))
1046 wnd
->SetFont(GetFont());
1050 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1052 wxXmlNode
*n
= m_node
->GetChildren();
1056 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1057 n
->GetName() == wxT("object"))
1059 if (this_hnd_only
&& CanHandle(n
))
1060 CreateResource(n
, parent
, NULL
);
1062 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1069 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1072 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1073 wxXmlNode
*n
= root
->GetChildren();
1077 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1079 CreateResource(n
, parent
, NULL
);
1091 // --------------- XRCID implementation -----------------------------
1093 #define XRCID_TABLE_SIZE 1024
1103 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1105 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1107 static int XRCID_LastID
= wxID_HIGHEST
;
1111 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1112 index
%= XRCID_TABLE_SIZE
;
1114 XRCID_record
*oldrec
= NULL
;
1116 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1118 if (wxStrcmp(rec
->key
, str_id
) == 0)
1126 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1127 &XRCID_Records
[index
] : &oldrec
->next
;
1128 *rec_var
= new XRCID_record
;
1129 (*rec_var
)->id
= ++XRCID_LastID
;
1130 (*rec_var
)->key
= wxStrdup(str_id
);
1131 (*rec_var
)->next
= NULL
;
1133 return (*rec_var
)->id
;
1137 static void CleanXRCID_Record(XRCID_record
*rec
)
1141 CleanXRCID_Record(rec
->next
);
1147 static void CleanXRCID_Records()
1149 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1150 CleanXRCID_Record(XRCID_Records
[i
]);
1160 // --------------- module and globals -----------------------------
1162 class wxXmlResourceModule
: public wxModule
1164 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1166 wxXmlResourceModule() {}
1173 delete wxXmlResource::Set(NULL
);
1174 CleanXRCID_Records();
1178 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1181 // When wxXml is loaded dynamically after the application is already running
1182 // then the built-in module system won't pick this one up. Add it manually.
1183 void wxXmlInitResourceModule()
1185 wxModule
* module = new wxXmlResourceModule
;
1187 wxModule::RegisterModule(module);