X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/acd32ffcdb319f162633c20e0202db3f8542998a..36adcfc9135110ee27c6e4247e5d29175159694f:/src/xrc/xmlres.cpp diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index b8d8b35601..075489d9e7 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -44,7 +44,7 @@ #include "wx/fontenum.h" #include "wx/fontmap.h" #include "wx/artprov.h" - +#include "wx/dir.h" #include "wx/xml/xml.h" @@ -178,6 +178,31 @@ bool wxXmlResource::IsArchive(const wxString& filename) #endif // wxUSE_FILESYSTEM +bool wxXmlResource::LoadFile(const wxFileName& file) +{ +#if wxUSE_FILESYSTEM + return Load(wxFileSystem::FileNameToURL(file)); +#else + return Load(file.GetFullPath()); +#endif +} + +bool wxXmlResource::LoadAllFiles(const wxString& dirname) +{ + bool ok = true; + wxArrayString files; + + wxDir::GetAllFiles(dirname, &files, "*.xrc"); + + for ( wxArrayString::const_iterator i = files.begin(); i != files.end(); ++i ) + { + if ( !LoadFile(*i) ) + ok = false; + } + + return ok; +} + bool wxXmlResource::Load(const wxString& filemask_) { wxString filemask = ConvertFileNameToURL(filemask_); @@ -382,7 +407,7 @@ bool wxXmlResource::AttachUnknownControl(const wxString& name, wxWindow *container = parent->FindWindow(name + wxT("_container")); if (!container) { - wxLogError(_("Cannot find container for unknown control '%s'."), name.c_str()); + wxLogError("Cannot find container for unknown control '%s'.", name); return false; } return control->Reparent(container); @@ -522,7 +547,11 @@ bool wxXmlResource::UpdateResources() } else if (rec->Doc->GetRoot()->GetName() != wxT("resource")) { - wxLogError(_("Invalid XRC resource '%s': doesn't have root node 'resource'."), rec->File); + ReportError + ( + rec->Doc->GetRoot(), + "invalid XRC resource, doesn't have root node " + ); wxDELETE(rec->Doc); rt = false; } @@ -541,7 +570,7 @@ bool wxXmlResource::UpdateResources() m_version = version; if (m_version != version) { - wxLogError(_("Resource files must have same version number!")); + wxLogError("Resource files must have same version number."); rt = false; } @@ -630,8 +659,15 @@ wxXmlNode *wxXmlResource::FindResource(const wxString& name, if ( !node ) { - wxLogError(_("XRC resource '%s' (class '%s') not found!"), - name, classname); + ReportError + ( + NULL, + wxString::Format + ( + "XRC resource \"%s\" (class \"%s\") not found", + name, classname + ) + ); } #if wxUSE_FILESYSTEM else // node was found @@ -749,8 +785,15 @@ wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, if ( !refNode ) { - wxLogError(_("Referenced object node with ref=\"%s\" not found!"), - refName.c_str()); + ReportError + ( + node, + wxString::Format + ( + "referenced object node with ref=\"%s\" not found", + refName + ) + ); return NULL; } @@ -778,9 +821,16 @@ wxObject *wxXmlResource::CreateResFromNode(wxXmlNode *node, wxObject *parent, } } - wxLogError(_("No handler found for XML node '%s', class '%s'!"), - node->GetName().c_str(), - node->GetAttribute(wxT("class"), wxEmptyString).c_str()); + ReportError + ( + node, + wxString::Format + ( + "no handler found for XML node \"%s\" (class \"%s\")", + node->GetName(), + node->GetAttribute("class", wxEmptyString) + ) + ); return NULL; } @@ -852,8 +902,15 @@ wxObject *wxXmlResourceHandler::CreateResource(wxXmlNode *node, wxObject *parent if (!m_instance) { wxString name = node->GetAttribute(wxT("name"), wxEmptyString); - wxLogError(_("Subclass '%s' not found for resource '%s', not subclassing!"), - subclass.c_str(), name.c_str()); + ReportError + ( + node, + wxString::Format + ( + "subclass \"%s\" not found for resource \"%s\", not subclassing", + subclass, name + ) + ); } } } @@ -927,9 +984,17 @@ int wxXmlResourceHandler::GetStyle(const wxString& param, int defaults) fl = tkn.GetNextToken(); index = m_styleNames.Index(fl); if (index != wxNOT_FOUND) + { style |= m_styleValues[index]; + } else - wxLogError(_("Unknown style flag ") + fl); + { + ReportParamError + ( + param, + wxString::Format("unknown style flag \"%s\"", fl) + ); + } } return style; } @@ -1154,8 +1219,11 @@ wxColour wxXmlResourceHandler::GetColour(const wxString& param, const wxColour& if (clr.Ok()) return clr; - wxLogError(_("XRC resource: Incorrect colour specification '%s' for attribute '%s'."), - v.c_str(), param.c_str()); + ReportParamError + ( + param, + wxString::Format("incorrect colour specification \"%s\"", v) + ); return wxNullColour; } @@ -1196,8 +1264,11 @@ wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, wxFSFile *fsfile = GetCurFileSystem().OpenFile(name, wxFS_READ | wxFS_SEEKABLE); if (fsfile == NULL) { - wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), - name.c_str()); + ReportParamError + ( + param, + wxString::Format("cannot open bitmap resource \"%s\"", name) + ); return wxNullBitmap; } wxImage img(*(fsfile->GetStream())); @@ -1208,8 +1279,11 @@ wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, if (!img.Ok()) { - wxLogError(_("XRC resource: Cannot create bitmap from '%s'."), - name.c_str()); + ReportParamError + ( + param, + wxString::Format("cannot create bitmap from \"%s\"", name) + ); return wxNullBitmap; } if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y); @@ -1300,7 +1374,11 @@ wxSize wxXmlResourceHandler::GetSize(const wxString& param, if (!s.BeforeFirst(wxT(',')).ToLong(&sx) || !s.AfterLast(wxT(',')).ToLong(&sy)) { - wxLogError(_("Cannot parse coordinates from '%s'."), s.c_str()); + ReportParamError + ( + param, + wxString::Format("cannot parse coordinates value \"%s\"", s) + ); return wxDefaultSize; } @@ -1316,7 +1394,11 @@ wxSize wxXmlResourceHandler::GetSize(const wxString& param, } else { - wxLogError(_("Cannot convert dialog units: dialog unknown.")); + ReportParamError + ( + param, + "cannot convert dialog units: dialog unknown" + ); return wxDefaultSize; } } @@ -1348,7 +1430,11 @@ wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, if (!s.ToLong(&sx)) { - wxLogError(_("Cannot parse dimension from '%s'."), s.c_str()); + ReportParamError + ( + param, + wxString::Format("cannot parse dimension value \"%s\"", s) + ); return defaultv; } @@ -1364,7 +1450,11 @@ wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, } else { - wxLogError(_("Cannot convert dialog units: dialog unknown.")); + ReportParamError + ( + param, + "cannot convert dialog units: dialog unknown" + ); return defaultv; } } @@ -1385,7 +1475,6 @@ static wxFont GetSystemFont(const wxString& name) SYSFNT(wxSYS_ANSI_VAR_FONT) SYSFNT(wxSYS_SYSTEM_FONT) SYSFNT(wxSYS_DEVICE_DEFAULT_FONT) - SYSFNT(wxSYS_DEFAULT_PALETTE) SYSFNT(wxSYS_SYSTEM_FIXED_FONT) SYSFNT(wxSYS_DEFAULT_GUI_FONT) #undef SYSFNT @@ -1399,7 +1488,8 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) wxXmlNode *font_node = GetParamNode(param); if (font_node == NULL) { - wxLogError(_("Cannot find font node '%s'."), param.c_str()); + ReportError( + wxString::Format("cannot find font node \"%s\"", param)); return wxNullFont; } @@ -1590,12 +1680,87 @@ void wxXmlResourceHandler::CreateChildrenPrivately(wxObject *parent, wxXmlNode * } +//----------------------------------------------------------------------------- +// errors reporting +//----------------------------------------------------------------------------- +void wxXmlResourceHandler::ReportError(const wxString& message) +{ + m_resource->ReportError(m_node, message); +} + +void wxXmlResourceHandler::ReportError(wxXmlNode *context, + const wxString& message) +{ + m_resource->ReportError(context ? context : m_node, message); +} +void wxXmlResourceHandler::ReportParamError(const wxString& param, + const wxString& message) +{ + m_resource->ReportError(GetParamNode(param), message); +} +namespace +{ + +wxString +GetFileNameFromNode(wxXmlNode *node, const wxXmlResourceDataRecords& files) +{ + wxXmlNode *root = node; + while ( root->GetParent() ) + root = root->GetParent(); + + for ( wxXmlResourceDataRecords::const_iterator i = files.begin(); + i != files.end(); ++i ) + { + if ( (*i)->Doc->GetRoot() == root ) + { + return (*i)->File; + } + } + + return wxEmptyString; // not found +} + +} // anonymous namespace + +void wxXmlResource::ReportError(wxXmlNode *context, const wxString& message) +{ + if ( !context ) + { + DoReportError("", NULL, message); + return; + } + + // We need to find out the file that 'context' is part of. Performance of + // this code is not critical, so we simply find the root XML node and + // compare it with all loaded XRC files. + const wxString filename = GetFileNameFromNode(context, Data()); + + DoReportError(filename, context, message); +} + +void wxXmlResource::DoReportError(const wxString& xrcFile, wxXmlNode *position, + const wxString& message) +{ + const int line = position ? position->GetLineNumber() : -1; + + wxString loc; + if ( !xrcFile.empty() ) + loc = xrcFile + ':'; + if ( line != -1 ) + loc += wxString::Format("%d:", line); + if ( !loc.empty() ) + loc += ' '; + + wxLogError("XRC error: %s%s", loc, message); +} -// --------------- XRCID implementation ----------------------------- +//----------------------------------------------------------------------------- +// XRCID implementation +//----------------------------------------------------------------------------- #define XRCID_TABLE_SIZE 1024 @@ -1838,7 +2003,9 @@ static void AddStdXRCID_Records() -// --------------- module and globals ----------------------------- +//----------------------------------------------------------------------------- +// module and globals +//----------------------------------------------------------------------------- // normally we would do the cleanup from wxXmlResourceModule::OnExit() but it // can happen that some XRC records have been created because of the use of