📄 xmlres.cpp
字号:
/////////////////////////////////////////////////////////////////////////////// Name: xmlres.cpp// Purpose: XRC resources// Author: Vaclav Slavik// Created: 2000/03/05// RCS-ID: $Id: xmlres.cpp,v 1.2 2004/10/30 21:07:19 HopeSeekr Exp $// Copyright: (c) 2000 Vaclav Slavik// Licence: wxWindows licence/////////////////////////////////////////////////////////////////////////////#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)#pragma implementation "xmlres.h"#endif// For compilers that support precompilation, includes "wx.h".#include <wx/wxprec.h>#ifdef __BORLANDC__ #pragma hdrstop#endif#include <wx/artprov.h>#include <wx/bitmap.h>#include <wx/dialog.h>#include <wx/filename.h>#include <wx/filesys.h>#include <wx/fontenum.h>#include <wx/fontmap.h>#include <wx/frame.h>#include <wx/image.h>#include <wx/intl.h>#include <wx/list.h>#include <wx/listimpl.cpp>#include <wx/log.h>#include <wx/module.h>#include <wx/panel.h>#include <wx/tokenzr.h>#include <wx/wfstream.h>#include <wx/xrc/xml.h>#include <wx/xrc/xmlres.h>wxXmlResource *wxXmlResource::ms_instance = NULL;/*static*/ wxXmlResource *wxXmlResource::Get(){ if ( !ms_instance ) ms_instance = new wxXmlResource; return ms_instance;}/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res){ wxXmlResource *old = ms_instance; ms_instance = res; return old;}wxXmlResource::wxXmlResource(){ m_flags = wxXRC_USE_LOCALE; m_version = -1;}wxXmlResource::wxXmlResource(int flags){ m_flags = flags; m_version = -1;}wxXmlResource::wxXmlResource(const wxString& filemask, int flags){ m_flags = flags; m_version = -1; Load(filemask);}wxXmlResource::~wxXmlResource(){ ClearHandlers(); std::list<wxXmlSubclassFactory *>::iterator it; for (it = ms_subclassFactories->begin(); it != ms_subclassFactories->end(); ++it) { delete *it; } ms_subclassFactories->clear(); delete ms_subclassFactories;}bool wxXmlResource::Load(const wxString& filemask){ wxString fnd; wxXmlResourceDataRecord *drec; bool iswild = wxIsWild(filemask); bool rt = TRUE;#if wxUSE_FILESYSTEM wxFileSystem fsys;# define wxXmlFindFirst fsys.FindFirst(filemask, wxFILE)# define wxXmlFindNext fsys.FindNext()#else# define wxXmlFindFirst wxFindFirstFile(filemask, wxFILE)# define wxXmlFindNext wxFindNextFile()#endif if (iswild) fnd = wxXmlFindFirst; else fnd = filemask; while (!!fnd) { // NB: Load() accepts both filenames and URLs (should probably be // changed to filenames only, but embedded resources currently // rely on its ability to handle URLs - FIXME). This check // serves as a quick way to determine whether found name is // filename and not URL: if (wxFileName::FileExists(fnd)) { // Make the name absolute filename, because the app may // change working directory later: wxFileName fn(fnd); if (fn.IsRelative()) { fn.Normalize(wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE); fnd = fn.GetFullPath(); } } #if wxUSE_FILESYSTEM if (fnd.Lower().Matches(wxT("*.zip")) || fnd.Lower().Matches(wxT("*.xrs"))) { wxString url(wxFileSystem::FileNameToURL(fnd)); rt = rt && Load(url + wxT("#zip:*.xrc")); } else#endif { drec = new wxXmlResourceDataRecord; drec->File = fnd; m_data.push_back(drec); } if (iswild) fnd = wxXmlFindNext; else fnd = wxEmptyString; }# undef wxXmlFindFirst# undef wxXmlFindNext return rt;}void wxXmlResource::AddHandler(wxXmlResourceHandler* handler){ m_handlers.push_back(handler); handler->SetParentResource(this);}void wxXmlResource::InsertHandler(wxXmlResourceHandler *handler){ m_handlers.push_front(handler); handler->SetParentResource(this);}void wxXmlResource::ClearHandlers(){ // The equivalent of wxList::DeleteContents() std::list<wxXmlResourceHandler *>::iterator it; for (it = m_handlers.begin(); it != m_handlers.end(); ++it) { delete *it; } m_handlers.clear();}wxMenu *wxXmlResource::LoadMenu(const wxString& name){ return (wxMenu*)CreateResFromNode(FindResource(name, wxT("wxMenu")), NULL, NULL);}wxMenuBar *wxXmlResource::LoadMenuBar(wxWindow *parent, const wxString& name){ return (wxMenuBar*)CreateResFromNode(FindResource(name, wxT("wxMenuBar")), parent, NULL);}#if wxUSE_TOOLBARwxToolBar *wxXmlResource::LoadToolBar(wxWindow *parent, const wxString& name){ return (wxToolBar*)CreateResFromNode(FindResource(name, wxT("wxToolBar")), parent, NULL);}#endifwxDialog *wxXmlResource::LoadDialog(wxWindow *parent, const wxString& name){ return (wxDialog*)CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, NULL);}bool wxXmlResource::LoadDialog(wxDialog *dlg, wxWindow *parent, const wxString& name){ return CreateResFromNode(FindResource(name, wxT("wxDialog")), parent, dlg) != NULL;}wxPanel *wxXmlResource::LoadPanel(wxWindow *parent, const wxString& name){ return (wxPanel*)CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, NULL);}bool wxXmlResource::LoadPanel(wxPanel *panel, wxWindow *parent, const wxString& name){ return CreateResFromNode(FindResource(name, wxT("wxPanel")), parent, panel) != NULL;}wxFrame *wxXmlResource::LoadFrame(wxWindow* parent, const wxString& name){ return (wxFrame*)CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, NULL);}bool wxXmlResource::LoadFrame(wxFrame* frame, wxWindow *parent, const wxString& name){ return CreateResFromNode(FindResource(name, wxT("wxFrame")), parent, frame) != NULL;}wxBitmap wxXmlResource::LoadBitmap(const wxString& name){ wxBitmap *bmp = (wxBitmap*)CreateResFromNode( FindResource(name, wxT("wxBitmap")), NULL, NULL); wxBitmap rt; if (bmp) { rt = *bmp; delete bmp; } return rt;}wxIcon wxXmlResource::LoadIcon(const wxString& name){ wxIcon *icon = (wxIcon*)CreateResFromNode( FindResource(name, wxT("wxIcon")), NULL, NULL); wxIcon rt; if (icon) { rt = *icon; delete icon; } return rt;}wxObject *wxXmlResource::LoadObject(wxWindow *parent, const wxString& name, const wxString& classname){ return CreateResFromNode(FindResource(name, classname), parent, NULL);}bool wxXmlResource::LoadObject(wxObject *instance, wxWindow *parent, const wxString& name, const wxString& classname){ return CreateResFromNode(FindResource(name, classname), parent, instance) != NULL;}bool wxXmlResource::AttachUnknownControl(const wxString& name, wxWindow *control, wxWindow *parent){ if (parent == NULL) parent = control->GetParent(); wxWindow *container = parent->FindWindow(name + wxT("_container")); if (!container) { wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str()); return FALSE; } return control->Reparent(container);}static void ProcessPlatformProperty(wxXmlNode *node){ wxString s; bool isok; wxXmlNode *c = node->GetChildren(); while (c) { isok = FALSE; if (!c->GetPropVal(wxT("platform"), &s)) isok = TRUE; else { wxStringTokenizer tkn(s, wxT(" |")); while (tkn.HasMoreTokens()) { s = tkn.GetNextToken(); if (#ifdef __WXMSW__ s == wxString(wxT("win"))#elif defined(__UNIX__) s == wxString(wxT("unix"))#elif defined(__MAC__) s == wxString(wxT("mac"))#elif defined(__OS2__) s == wxString(wxT("os2"))#else FALSE#endif ) isok = TRUE; } } if (isok) { ProcessPlatformProperty(c); c = c->GetNext(); } else { wxXmlNode *c2 = c->GetNext(); node->RemoveChild(c); delete c; c = c2; } }}void wxXmlResource::UpdateResources(){ bool modif;# if wxUSE_FILESYSTEM wxFSFile *file = NULL; wxFileSystem fsys;# endif wxString encoding(wxT("UTF-8"));#if !wxUSE_UNICODE && wxUSE_INTL if ( (GetFlags() & wxXRC_USE_LOCALE) == 0 ) { // In case we are not using wxLocale to translate strings, convert the strings // GUI's charset. This must not be done when wxXRC_USE_LOCALE is on, because // it could break wxGetTranslation lookup. encoding = wxLocale::GetSystemEncodingName(); }#endif for (size_t i = 0; i < m_data.size(); i++) { modif = (m_data[i]->Doc == NULL); if (!modif) {# if wxUSE_FILESYSTEM file = fsys.OpenFile(m_data[i]->File); modif = file && file->GetModificationTime() > m_data[i]->Time; if (!file) wxLogError(_("Cannot open file '%s'."), m_data[i]->File.c_str()); wxDELETE(file);# else modif = wxDateTime(wxFileModificationTime(m_data[i]->File)) > m_data[i]->Time;# endif } if (modif) { wxInputStream *stream = NULL;# if wxUSE_FILESYSTEM file = fsys.OpenFile(m_data[i]->File); if (file) stream = file->GetStream();# else stream = new wxFileInputStream(m_data[i]->File);# endif if (stream) { delete m_data[i]->Doc; m_data[i]->Doc = new wxXmlDocument; } if (!stream || !m_data[i]->Doc->Load(*stream, encoding)) { wxLogError(_("Cannot load resources from file '%s'."), m_data[i]->File.c_str()); wxDELETE(m_data[i]->Doc); } else if (m_data[i]->Doc->GetRoot()->GetName() != wxT("resource")) { wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), m_data[i]->File.c_str()); wxDELETE(m_data[i]->Doc); } else { long version; int v1, v2, v3, v4; wxString verstr = m_data[i]->Doc->GetRoot()->GetPropVal( wxT("version"), wxT("0.0.0.0")); if (wxSscanf(verstr.c_str(), wxT("%i.%i.%i.%i"), &v1, &v2, &v3, &v4) == 4) version = v1*256*256*256+v2*256*256+v3*256+v4; else version = 0; if (m_version == -1) m_version = version; if (m_version != version) wxLogError(_("Resource files must have same version number!")); ProcessPlatformProperty(m_data[i]->Doc->GetRoot()); m_data[i]->Time = file->GetModificationTime(); }# if wxUSE_FILESYSTEM wxDELETE(file);# else wxDELETE(stream);# endif } }}wxXmlNode *wxXmlResource::DoFindResource(wxXmlNode *parent, const wxString& name, const wxString& classname, bool recursive){ wxString dummy;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -