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);
137 void wxXmlResource::ClearHandlers()
144 wxMenu
*wxXmlResource::LoadMenu(const wxString
& name
)
146 return (wxMenu
*)CreateResFromNode(FindResource(name
, wxT("wxMenu")), NULL
, NULL
);
151 wxMenuBar
*wxXmlResource::LoadMenuBar(wxWindow
*parent
, const wxString
& name
)
153 return (wxMenuBar
*)CreateResFromNode(FindResource(name
, wxT("wxMenuBar")), parent
, NULL
);
159 wxToolBar
*wxXmlResource::LoadToolBar(wxWindow
*parent
, const wxString
& name
)
161 return (wxToolBar
*)CreateResFromNode(FindResource(name
, wxT("wxToolBar")), parent
, NULL
);
166 wxDialog
*wxXmlResource::LoadDialog(wxWindow
*parent
, const wxString
& name
)
168 wxDialog
*dialog
= new wxDialog
;
169 if (!LoadDialog(dialog
, parent
, name
))
170 { delete dialog
; return NULL
; }
174 bool wxXmlResource::LoadDialog(wxDialog
*dlg
, wxWindow
*parent
, const wxString
& name
)
176 return CreateResFromNode(FindResource(name
, wxT("wxDialog")), parent
, dlg
) != NULL
;
181 wxPanel
*wxXmlResource::LoadPanel(wxWindow
*parent
, const wxString
& name
)
183 return (wxPanel
*)CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, NULL
);
186 bool wxXmlResource::LoadPanel(wxPanel
*panel
, wxWindow
*parent
, const wxString
& name
)
188 return CreateResFromNode(FindResource(name
, wxT("wxPanel")), parent
, panel
) != NULL
;
191 bool wxXmlResource::LoadFrame(wxFrame
* frame
, wxWindow
*parent
, const wxString
& name
)
193 return CreateResFromNode(FindResource(name
, wxT("wxFrame")), parent
, frame
) != NULL
;
196 wxBitmap
wxXmlResource::LoadBitmap(const wxString
& name
)
198 wxBitmap
*bmp
= (wxBitmap
*)CreateResFromNode(
199 FindResource(name
, wxT("wxBitmap")), NULL
, NULL
);
202 if (bmp
) { rt
= *bmp
; delete bmp
; }
206 wxIcon
wxXmlResource::LoadIcon(const wxString
& name
)
208 wxIcon
*icon
= (wxIcon
*)CreateResFromNode(
209 FindResource(name
, wxT("wxIcon")), NULL
, NULL
);
212 if (icon
) { rt
= *icon
; delete icon
; }
216 bool wxXmlResource::AttachUnknownControl(const wxString
& name
,
217 wxWindow
*control
, wxWindow
*parent
)
220 parent
= control
->GetParent();
221 wxWindow
*container
= parent
->FindWindow(name
+ wxT("_container"));
224 wxLogError(_("Cannot find container for unknown control '%s'."), name
.c_str());
227 return control
->Reparent(container
);
231 static void ProcessPlatformProperty(wxXmlNode
*node
)
236 wxXmlNode
*c
= node
->GetChildren();
240 if (!c
->GetPropVal(wxT("platform"), &s
))
244 wxStringTokenizer
tkn(s
, " |");
246 while (tkn
.HasMoreTokens())
248 s
= tkn
.GetNextToken();
251 s
== wxString(wxT("win"))
252 #elif defined(__UNIX__)
253 s
== wxString(wxT("unix"))
254 #elif defined(__MAC__)
255 s
== wxString(wxT("mac"))
256 #elif defined(__OS2__)
257 s
== wxString(wxT("os2"))
267 ProcessPlatformProperty(c
);
272 node
->RemoveChild(c
);
273 wxXmlNode
*c2
= c
->GetNext();
282 void wxXmlResource::UpdateResources()
285 # if wxUSE_FILESYSTEM
286 wxFSFile
*file
= NULL
;
290 wxString
encoding(wxT("UTF-8"));
291 #if !wxUSE_UNICODE && wxUSE_INTL
292 if ( (GetFlags() & wxXRC_USE_LOCALE
) == 0 )
294 // In case we are not using wxLocale to translate strings, convert the strings
295 // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because
296 // it could break wxGetTranslation lookup.
297 encoding
= wxLocale::GetSystemEncodingName();
301 for (size_t i
= 0; i
< m_data
.GetCount(); i
++)
303 modif
= (m_data
[i
].Doc
== NULL
);
307 # if wxUSE_FILESYSTEM
308 file
= fsys
.OpenFile(m_data
[i
].File
);
309 modif
= file
&& file
->GetModificationTime() > m_data
[i
].Time
;
311 wxLogError(_("Cannot open file '%s'."), m_data
[i
].File
.c_str());
314 modif
= wxDateTime(wxFileModificationTime(m_data
[i
].File
)) > m_data
[i
].Time
;
320 wxInputStream
*stream
= NULL
;
322 # if wxUSE_FILESYSTEM
323 file
= fsys
.OpenFile(m_data
[i
].File
);
325 stream
= file
->GetStream();
327 stream
= new wxFileInputStream(m_data
[i
].File
);
332 delete m_data
[i
].Doc
;
333 m_data
[i
].Doc
= new wxXmlDocument
;
335 if (!stream
|| !m_data
[i
].Doc
->Load(*stream
, encoding
))
337 wxLogError(_("Cannot load resources from file '%s'."),
338 m_data
[i
].File
.c_str());
339 wxDELETE(m_data
[i
].Doc
);
341 else if (m_data
[i
].Doc
->GetRoot()->GetName() != wxT("resource"))
343 wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data
[i
].File
.c_str());
344 wxDELETE(m_data
[i
].Doc
);
350 wxString verstr
= m_data
[i
].Doc
->GetRoot()->GetPropVal(
351 wxT("version"), wxT("0.0.0.0"));
352 if (wxSscanf(verstr
.c_str(), wxT("%i.%i.%i.%i"),
353 &v1
, &v2
, &v3
, &v4
) == 4)
354 version
= v1
*256*256*256+v2
*256*256+v3
*256+v4
;
359 if (m_version
!= version
)
360 wxLogError(_("Resource files must have same version number!"));
362 ProcessPlatformProperty(m_data
[i
].Doc
->GetRoot());
363 m_data
[i
].Time
= file
->GetModificationTime();
366 # if wxUSE_FILESYSTEM
376 wxXmlNode
*wxXmlResource::DoFindResource(wxXmlNode
*parent
,
377 const wxString
& name
,
378 const wxString
& classname
,
384 // first search for match at the top-level nodes (as this is
385 // where the resource is most commonly looked for):
386 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
388 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
389 (node
->GetName() == wxT("object") ||
390 node
->GetName() == wxT("object_ref")) &&
392 node
->GetPropVal(wxT("class"), wxEmptyString
) == classname
) &&
393 node
->GetPropVal(wxT("name"), &dummy
) && dummy
== name
)
398 for (node
= parent
->GetChildren(); node
; node
= node
->GetNext())
400 if ( node
->GetType() == wxXML_ELEMENT_NODE
&&
401 (node
->GetName() == wxT("object") ||
402 node
->GetName() == wxT("object_ref")) )
404 wxXmlNode
* found
= DoFindResource(node
, name
, classname
, TRUE
);
413 wxXmlNode
*wxXmlResource::FindResource(const wxString
& name
,
414 const wxString
& classname
,
417 UpdateResources(); //ensure everything is up-to-date
420 for (size_t f
= 0; f
< m_data
.GetCount(); f
++)
422 if ( m_data
[f
].Doc
== NULL
|| m_data
[f
].Doc
->GetRoot() == NULL
)
425 wxXmlNode
* found
= DoFindResource(m_data
[f
].Doc
->GetRoot(),
426 name
, classname
, recursive
);
430 m_curFileSystem
.ChangePathTo(m_data
[f
].File
);
436 wxLogError(_("XRC resource '%s' (class '%s') not found!"),
437 name
.c_str(), classname
.c_str());
441 static void MergeNodes(wxXmlNode
& dest
, wxXmlNode
& with
)
444 for (wxXmlProperty
*prop
= with
.GetProperties(); prop
; prop
= prop
->GetNext())
446 wxXmlProperty
*dprop
;
447 for (dprop
= dest
.GetProperties(); dprop
; dprop
= dprop
->GetNext())
450 if ( dprop
->GetName() == prop
->GetName() )
452 dprop
->SetValue(prop
->GetValue());
458 dest
.AddProperty(prop
->GetName(), prop
->GetValue());
461 // Merge child nodes:
462 for (wxXmlNode
* node
= with
.GetChildren(); node
; node
= node
->GetNext())
464 wxString name
= node
->GetPropVal(wxT("name"), wxEmptyString
);
467 for (dnode
= dest
.GetChildren(); dnode
; dnode
= dnode
->GetNext() )
469 if ( dnode
->GetName() == node
->GetName() &&
470 dnode
->GetPropVal("name", wxEmptyString
) == name
&&
471 dnode
->GetType() == node
->GetType() )
473 MergeNodes(*dnode
, *node
);
479 dest
.AddChild(new wxXmlNode(*node
));
482 if ( dest
.GetType() == wxXML_TEXT_NODE
&& with
.GetContent().Length() )
483 dest
.SetContent(with
.GetContent());
486 wxObject
*wxXmlResource::CreateResFromNode(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
488 if (node
== NULL
) return NULL
;
490 // handling of referenced resource
491 if ( node
->GetName() == wxT("object_ref") )
493 wxString refName
= node
->GetPropVal(wxT("ref"), wxEmptyString
);
494 wxXmlNode
* refNode
= FindResource(refName
, wxEmptyString
, TRUE
);
498 wxLogError(_("Referenced object node with ref=\"%s\" not found!"),
503 wxXmlNode
copy(*refNode
);
504 MergeNodes(copy
, *node
);
506 return CreateResFromNode(©
, parent
, instance
);
509 wxXmlResourceHandler
*handler
;
511 wxNode
* ND
= m_handlers
.GetFirst();
514 handler
= (wxXmlResourceHandler
*)ND
->GetData();
515 if (node
->GetName() == wxT("object") && handler
->CanHandle(node
))
517 ret
= handler
->CreateResource(node
, parent
, instance
);
523 wxLogError(_("No handler found for XML node '%s', class '%s'!"),
524 node
->GetName().c_str(),
525 node
->GetPropVal(wxT("class"), wxEmptyString
).c_str());
532 wxXmlResourceHandler::wxXmlResourceHandler()
533 : m_node(NULL
), m_parent(NULL
), m_instance(NULL
),
534 m_parentAsWindow(NULL
), m_instanceAsWindow(NULL
)
539 wxObject
*wxXmlResourceHandler::CreateResource(wxXmlNode
*node
, wxObject
*parent
, wxObject
*instance
)
541 wxXmlNode
*myNode
= m_node
;
542 wxString myClass
= m_class
;
543 wxObject
*myParent
= m_parent
, *myInstance
= m_instance
;
544 wxWindow
*myParentAW
= m_parentAsWindow
, *myInstanceAW
= m_instanceAsWindow
;
546 m_instance
= instance
;
547 if (!m_instance
&& node
->HasProp(wxT("subclass")) &&
548 !(m_resource
->GetFlags() & wxXRC_NO_SUBCLASSING
))
550 wxString subclass
= node
->GetPropVal(wxT("subclass"), wxEmptyString
);
551 wxClassInfo
* classInfo
= wxClassInfo::FindClass(subclass
);
554 m_instance
= classInfo
->CreateObject();
558 wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"),
559 subclass
.c_str(), node
->GetPropVal(wxT("name"), wxEmptyString
).c_str());
562 m_instance
= classInfo
->CreateObject();
566 m_class
= node
->GetPropVal(wxT("class"), wxEmptyString
);
568 m_parentAsWindow
= wxDynamicCast(m_parent
, wxWindow
);
569 m_instanceAsWindow
= wxDynamicCast(m_instance
, wxWindow
);
571 wxObject
*returned
= DoCreateResource();
575 m_parent
= myParent
; m_parentAsWindow
= myParentAW
;
576 m_instance
= myInstance
; m_instanceAsWindow
= myInstanceAW
;
582 void wxXmlResourceHandler::AddStyle(const wxString
& name
, int value
)
584 m_styleNames
.Add(name
);
585 m_styleValues
.Add(value
);
590 void wxXmlResourceHandler::AddWindowStyles()
592 XRC_ADD_STYLE(wxSIMPLE_BORDER
);
593 XRC_ADD_STYLE(wxSUNKEN_BORDER
);
594 XRC_ADD_STYLE(wxDOUBLE_BORDER
);
595 XRC_ADD_STYLE(wxRAISED_BORDER
);
596 XRC_ADD_STYLE(wxSTATIC_BORDER
);
597 XRC_ADD_STYLE(wxNO_BORDER
);
598 XRC_ADD_STYLE(wxTRANSPARENT_WINDOW
);
599 XRC_ADD_STYLE(wxWANTS_CHARS
);
600 XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE
);
605 bool wxXmlResourceHandler::HasParam(const wxString
& param
)
607 return (GetParamNode(param
) != NULL
);
611 int wxXmlResourceHandler::GetStyle(const wxString
& param
, int defaults
)
613 wxString s
= GetParamValue(param
);
615 if (!s
) return defaults
;
617 wxStringTokenizer
tkn(s
, wxT("| "), wxTOKEN_STRTOK
);
621 while (tkn
.HasMoreTokens())
623 fl
= tkn
.GetNextToken();
624 index
= m_styleNames
.Index(fl
);
625 if (index
!= wxNOT_FOUND
)
626 style
|= m_styleValues
[index
];
628 wxLogError(_("Unknown style flag ") + fl
);
635 wxString
wxXmlResourceHandler::GetText(const wxString
& param
, bool translate
)
637 wxString
str1(GetParamValue(param
));
642 // VS: First version of XRC resources used $ instead of & (which is
643 // illegal in XML), but later I realized that '_' fits this purpose
644 // much better (because &File means "File with F underlined").
645 if (m_resource
->CompareVersion(2,3,0,1) < 0)
650 for (dt
= str1
.c_str(); *dt
; dt
++)
652 // Remap amp_char to &, map double amp_char to amp_char (for things
653 // like "&File..." -- this is illegal in XML, so we use "_File..."):
656 if ( *(++dt
) == amp_char
)
659 str2
<< wxT('&') << *dt
;
661 // Remap \n to CR, \r to LF, \t to TAB:
662 else if (*dt
== wxT('\\'))
665 case wxT('n') : str2
<< wxT('\n'); break;
666 case wxT('t') : str2
<< wxT('\t'); break;
667 case wxT('r') : str2
<< wxT('\r'); break;
668 default : str2
<< wxT('\\') << *dt
; break;
673 if (translate
&& m_resource
->GetFlags() & wxXRC_USE_LOCALE
)
674 return wxGetTranslation(str2
);
682 long wxXmlResourceHandler::GetLong(const wxString
& param
, long defaultv
)
685 wxString str1
= GetParamValue(param
);
687 if (!str1
.ToLong(&value
))
695 int wxXmlResourceHandler::GetID()
697 wxString sid
= GetName();
700 if (sid
== wxT("-1")) return -1;
701 else if (sid
.IsNumber() && sid
.ToLong(&num
)) return num
;
702 #define stdID(id) else if (sid == wxT(#id)) return id
703 stdID(wxID_OPEN
); stdID(wxID_CLOSE
); stdID(wxID_NEW
);
704 stdID(wxID_SAVE
); stdID(wxID_SAVEAS
); stdID(wxID_REVERT
);
705 stdID(wxID_EXIT
); stdID(wxID_UNDO
); stdID(wxID_REDO
);
706 stdID(wxID_HELP
); stdID(wxID_PRINT
); stdID(wxID_PRINT_SETUP
);
707 stdID(wxID_PREVIEW
); stdID(wxID_ABOUT
); stdID(wxID_HELP_CONTENTS
);
708 stdID(wxID_HELP_COMMANDS
); stdID(wxID_HELP_PROCEDURES
);
709 stdID(wxID_CUT
); stdID(wxID_COPY
); stdID(wxID_PASTE
);
710 stdID(wxID_CLEAR
); stdID(wxID_FIND
); stdID(wxID_DUPLICATE
);
711 stdID(wxID_SELECTALL
); stdID(wxID_OK
); stdID(wxID_CANCEL
);
712 stdID(wxID_APPLY
); stdID(wxID_YES
); stdID(wxID_NO
);
713 stdID(wxID_STATIC
); stdID(wxID_FORWARD
); stdID(wxID_BACKWARD
);
714 stdID(wxID_DEFAULT
); stdID(wxID_MORE
); stdID(wxID_SETUP
);
715 stdID(wxID_RESET
); stdID(wxID_HELP_CONTEXT
);
717 else return wxXmlResource::GetXRCID(sid
);
722 wxArtID
wxXmlResourceHandler::GetStockID(const wxString
& param
)
724 wxXmlNode
*stockIDNode
= GetParamNode(param
);
725 if (stockIDNode
== NULL
)
726 return wxEmptyString
;
728 wxXmlNode
*oldnode
= m_node
;
729 m_node
= stockIDNode
;
731 wxArtID sid
= m_node
->GetPropVal(wxT("stock_id"), wxT(""));
735 if (sid
.IsEmpty()) return wxEmptyString
;
736 #define stdID(id) else if (sid == wxT(#id)) return id
737 stdID(wxART_ADD_BOOKMARK
); stdID(wxART_DEL_BOOKMARK
);
738 stdID(wxART_HELP_SIDE_PANEL
); stdID(wxART_HELP_SETTINGS
);
739 stdID(wxART_HELP_BOOK
); stdID(wxART_HELP_FOLDER
);
740 stdID(wxART_HELP_PAGE
); stdID(wxART_GO_BACK
);
741 stdID(wxART_GO_FORWARD
); stdID(wxART_GO_UP
);
742 stdID(wxART_GO_DOWN
); stdID(wxART_GO_TO_PARENT
);
743 stdID(wxART_GO_HOME
); stdID(wxART_FILE_OPEN
);
744 stdID(wxART_PRINT
); stdID(wxART_HELP
); stdID(wxART_TIP
);
745 stdID(wxART_REPORT_VIEW
); stdID(wxART_LIST_VIEW
);
746 stdID(wxART_NEW_DIR
); stdID(wxART_FOLDER
);
747 stdID(wxART_GO_DIR_UP
); stdID(wxART_EXECUTABLE_FILE
);
748 stdID(wxART_NORMAL_FILE
); stdID(wxART_TICK_MARK
);
749 stdID(wxART_CROSS_MARK
); stdID(wxART_ERROR
);
750 stdID(wxART_QUESTION
); stdID(wxART_WARNING
);
751 stdID(wxART_INFORMATION
);
758 wxArtClient
wxXmlResourceHandler::GetStockClient(const wxString
& param
)
760 wxXmlNode
*stockClientNode
= GetParamNode(param
);
761 if (stockClientNode
== NULL
)
762 return wxEmptyString
;
764 wxXmlNode
*oldnode
= m_node
;
765 m_node
= stockClientNode
;
767 wxArtClient sid
= m_node
->GetPropVal(wxT("stock_client"), wxT("wxART_OTHER"));
771 if (sid
.IsEmpty()) return wxEmptyString
;
772 #define stdID(id) else if (sid == wxT(#id)) return id
773 stdID(wxART_TOOLBAR
); stdID(wxART_MENU
);
774 stdID(wxART_FRAME_ICON
); stdID(wxART_CMN_DIALOG
);
775 stdID(wxART_HELP_BROWSER
); stdID(wxART_MESSAGE_BOX
);
783 wxString
wxXmlResourceHandler::GetName()
785 return m_node
->GetPropVal(wxT("name"), wxT("-1"));
790 bool wxXmlResourceHandler::GetBool(const wxString
& param
, bool defaultv
)
792 wxString v
= GetParamValue(param
);
794 if (!v
) return defaultv
;
795 else return (v
== wxT("1"));
800 wxColour
wxXmlResourceHandler::GetColour(const wxString
& param
)
802 wxString v
= GetParamValue(param
);
803 unsigned long tmp
= 0;
805 if (v
.Length() != 7 || v
[0u] != wxT('#') ||
806 wxSscanf(v
.c_str(), wxT("#%lX"), &tmp
) != 1)
808 wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."),
809 v
.c_str(), param
.c_str());
813 return wxColour((unsigned char) ((tmp
& 0xFF0000) >> 16) ,
814 (unsigned char) ((tmp
& 0x00FF00) >> 8),
815 (unsigned char) ((tmp
& 0x0000FF)));
820 wxBitmap
wxXmlResourceHandler::GetBitmap(const wxString
& param
, wxSize size
)
822 wxString stk
= param
;
823 if (!GetStockID(stk
).IsEmpty())
825 wxBitmap stockArt
= wxArtProvider::GetBitmap(GetStockID(stk
),
826 GetStockClient(stk
), size
);
827 if (stockArt
!= wxNullBitmap
)
831 wxString name
= GetParamValue(param
);
832 if (name
.IsEmpty()) return wxNullBitmap
;
834 wxFSFile
*fsfile
= GetCurFileSystem().OpenFile(name
);
837 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
840 wxImage
img(*(fsfile
->GetStream()));
843 wxImage
img(GetParamValue(wxT("bitmap")));
848 wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), param
.c_str());
851 if (!(size
== wxDefaultSize
)) img
.Rescale(size
.x
, size
.y
);
852 return wxBitmap(img
);
858 wxIcon
wxXmlResourceHandler::GetIcon(const wxString
& param
, wxSize size
)
860 #if wxCHECK_VERSION(2,3,0) || defined(__WXMSW__)
862 icon
.CopyFromBitmap(GetBitmap(param
, size
));
865 wxBitmap bmppt
= GetBitmap(param
, size
);
866 iconpt
= (wxIcon
*)(&bmppt
);
867 wxIcon
icon(*iconpt
);
874 wxXmlNode
*wxXmlResourceHandler::GetParamNode(const wxString
& param
)
876 wxXmlNode
*n
= m_node
->GetChildren();
880 if (n
->GetType() == wxXML_ELEMENT_NODE
&& n
->GetName() == param
)
888 wxString
wxXmlResourceHandler::GetNodeContent(wxXmlNode
*node
)
891 if (n
== NULL
) return wxEmptyString
;
892 n
= n
->GetChildren();
896 if (n
->GetType() == wxXML_TEXT_NODE
||
897 n
->GetType() == wxXML_CDATA_SECTION_NODE
)
898 return n
->GetContent();
901 return wxEmptyString
;
906 wxString
wxXmlResourceHandler::GetParamValue(const wxString
& param
)
909 return GetNodeContent(m_node
);
911 return GetNodeContent(GetParamNode(param
));
916 wxSize
wxXmlResourceHandler::GetSize(const wxString
& param
)
918 wxString s
= GetParamValue(param
);
919 if (s
.IsEmpty()) s
= wxT("-1,-1");
923 is_dlg
= s
[s
.Length()-1] == wxT('d');
924 if (is_dlg
) s
.RemoveLast();
926 if (!s
.BeforeFirst(wxT(',')).ToLong(&sx
) ||
927 !s
.AfterLast(wxT(',')).ToLong(&sy
))
929 wxLogError(_("Cannot parse coordinates from '%s'."), s
.c_str());
930 return wxDefaultSize
;
935 if (m_instanceAsWindow
)
936 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, sy
));
937 else if (m_parentAsWindow
)
938 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, sy
));
941 wxLogError(_("Cannot convert dialog units: dialog unknown."));
942 return wxDefaultSize
;
945 else return wxSize(sx
, sy
);
950 wxPoint
wxXmlResourceHandler::GetPosition(const wxString
& param
)
952 wxSize sz
= GetSize(param
);
953 return wxPoint(sz
.x
, sz
.y
);
958 wxCoord
wxXmlResourceHandler::GetDimension(const wxString
& param
, wxCoord defaultv
)
960 wxString s
= GetParamValue(param
);
961 if (s
.IsEmpty()) return defaultv
;
965 is_dlg
= s
[s
.Length()-1] == wxT('d');
966 if (is_dlg
) s
.RemoveLast();
970 wxLogError(_("Cannot parse dimension from '%s'."), s
.c_str());
976 if (m_instanceAsWindow
)
977 return wxDLG_UNIT(m_instanceAsWindow
, wxSize(sx
, 0)).x
;
978 else if (m_parentAsWindow
)
979 return wxDLG_UNIT(m_parentAsWindow
, wxSize(sx
, 0)).x
;
982 wxLogError(_("Cannot convert dialog units: dialog unknown."));
991 wxFont
wxXmlResourceHandler::GetFont(const wxString
& param
)
993 wxXmlNode
*font_node
= GetParamNode(param
);
994 if (font_node
== NULL
)
996 wxLogError(_("Cannot find font node '%s'."), param
.c_str());
1000 wxXmlNode
*oldnode
= m_node
;
1003 long size
= GetLong(wxT("size"), 12);
1005 wxString style
= GetParamValue(wxT("style"));
1006 wxString weight
= GetParamValue(wxT("weight"));
1007 int istyle
= wxNORMAL
, iweight
= wxNORMAL
;
1008 if (style
== wxT("italic")) istyle
= wxITALIC
;
1009 else if (style
== wxT("slant")) istyle
= wxSLANT
;
1010 if (weight
== wxT("bold")) iweight
= wxBOLD
;
1011 else if (weight
== wxT("light")) iweight
= wxLIGHT
;
1013 wxString family
= GetParamValue(wxT("family"));
1014 int ifamily
= wxDEFAULT
;
1015 if (family
== wxT("decorative")) ifamily
= wxDECORATIVE
;
1016 else if (family
== wxT("roman")) ifamily
= wxROMAN
;
1017 else if (family
== wxT("script")) ifamily
= wxSCRIPT
;
1018 else if (family
== wxT("swiss")) ifamily
= wxSWISS
;
1019 else if (family
== wxT("modern")) ifamily
= wxMODERN
;
1021 bool underlined
= GetBool(wxT("underlined"), FALSE
);
1023 wxString encoding
= GetParamValue(wxT("encoding"));
1024 wxFontMapper mapper
;
1025 wxFontEncoding enc
= wxFONTENCODING_DEFAULT
;
1026 if (!encoding
.IsEmpty())
1027 enc
= mapper
.CharsetToEncoding(encoding
);
1028 if (enc
== wxFONTENCODING_SYSTEM
)
1029 enc
= wxFONTENCODING_DEFAULT
;
1031 wxString faces
= GetParamValue(wxT("face"));
1032 wxString facename
= wxEmptyString
;
1033 wxFontEnumerator enu
;
1034 enu
.EnumerateFacenames();
1035 wxStringTokenizer
tk(faces
, wxT(","));
1036 while (tk
.HasMoreTokens())
1038 int index
= enu
.GetFacenames()->Index(tk
.GetNextToken(), FALSE
);
1039 if (index
!= wxNOT_FOUND
)
1041 facename
= (*enu
.GetFacenames())[index
];
1048 wxFont
font(size
, ifamily
, istyle
, iweight
, underlined
, facename
, enc
);
1053 void wxXmlResourceHandler::SetupWindow(wxWindow
*wnd
)
1055 //FIXME : add cursor
1057 if (HasParam(wxT("exstyle")))
1058 wnd
->SetExtraStyle(GetStyle(wxT("exstyle")));
1059 if (HasParam(wxT("bg")))
1060 wnd
->SetBackgroundColour(GetColour(wxT("bg")));
1061 if (HasParam(wxT("fg")))
1062 wnd
->SetForegroundColour(GetColour(wxT("fg")));
1063 if (GetBool(wxT("enabled"), 1) == 0)
1065 if (GetBool(wxT("focused"), 0) == 1)
1067 if (GetBool(wxT("hidden"), 0) == 1)
1070 if (HasParam(wxT("tooltip")))
1071 wnd
->SetToolTip(GetText(wxT("tooltip")));
1073 if (HasParam(wxT("font")))
1074 wnd
->SetFont(GetFont());
1078 void wxXmlResourceHandler::CreateChildren(wxObject
*parent
, bool this_hnd_only
)
1080 wxXmlNode
*n
= m_node
->GetChildren();
1084 if (n
->GetType() == wxXML_ELEMENT_NODE
&&
1085 n
->GetName() == wxT("object"))
1087 if (this_hnd_only
&& CanHandle(n
))
1088 CreateResource(n
, parent
, NULL
);
1090 m_resource
->CreateResFromNode(n
, parent
, NULL
);
1097 void wxXmlResourceHandler::CreateChildrenPrivately(wxObject
*parent
, wxXmlNode
*rootnode
)
1100 if (rootnode
== NULL
) root
= m_node
; else root
= rootnode
;
1101 wxXmlNode
*n
= root
->GetChildren();
1105 if (n
->GetType() == wxXML_ELEMENT_NODE
&& CanHandle(n
))
1107 CreateResource(n
, parent
, NULL
);
1119 // --------------- XRCID implementation -----------------------------
1121 #define XRCID_TABLE_SIZE 1024
1131 static XRCID_record
*XRCID_Records
[XRCID_TABLE_SIZE
] = {NULL
};
1133 /*static*/ int wxXmlResource::GetXRCID(const wxChar
*str_id
)
1135 static int XRCID_LastID
= wxID_HIGHEST
;
1139 for (const wxChar
*c
= str_id
; *c
!= wxT('\0'); c
++) index
+= (int)*c
;
1140 index
%= XRCID_TABLE_SIZE
;
1142 XRCID_record
*oldrec
= NULL
;
1144 for (XRCID_record
*rec
= XRCID_Records
[index
]; rec
; rec
= rec
->next
)
1146 if (wxStrcmp(rec
->key
, str_id
) == 0)
1154 XRCID_record
**rec_var
= (oldrec
== NULL
) ?
1155 &XRCID_Records
[index
] : &oldrec
->next
;
1156 *rec_var
= new XRCID_record
;
1157 (*rec_var
)->id
= ++XRCID_LastID
;
1158 (*rec_var
)->key
= wxStrdup(str_id
);
1159 (*rec_var
)->next
= NULL
;
1161 return (*rec_var
)->id
;
1165 static void CleanXRCID_Record(XRCID_record
*rec
)
1169 CleanXRCID_Record(rec
->next
);
1175 static void CleanXRCID_Records()
1177 for (int i
= 0; i
< XRCID_TABLE_SIZE
; i
++)
1178 CleanXRCID_Record(XRCID_Records
[i
]);
1188 // --------------- module and globals -----------------------------
1190 class wxXmlResourceModule
: public wxModule
1192 DECLARE_DYNAMIC_CLASS(wxXmlResourceModule
)
1194 wxXmlResourceModule() {}
1201 delete wxXmlResource::Set(NULL
);
1202 CleanXRCID_Records();
1206 IMPLEMENT_DYNAMIC_CLASS(wxXmlResourceModule
, wxModule
)
1209 // When wxXml is loaded dynamically after the application is already running
1210 // then the built-in module system won't pick this one up. Add it manually.
1211 void wxXmlInitResourceModule()
1213 wxModule
* module = new wxXmlResourceModule
;
1215 wxModule::RegisterModule(module);