X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/1df61962e86618c393188fce545a9d8ffbf8c425..3b96fc2f1b64f78bba7c755a5c14f618962f696b:/src/xrc/xmlres.cpp diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index 8f36455918..37855b675c 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -8,10 +8,6 @@ // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifdef __GNUG__ -#pragma implementation "xmlres.h" -#endif - // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" @@ -21,7 +17,9 @@ #if wxUSE_XRC +#ifndef __WXWINCE__ #include +#endif #include "wx/dialog.h" #include "wx/panel.h" @@ -38,6 +36,7 @@ #include "wx/image.h" #include "wx/fontmap.h" #include "wx/artprov.h" +#include "wx/settings.h" #include "wx/xml/xml.h" #include "wx/xrc/xmlres.h" @@ -81,6 +80,46 @@ wxXmlResource::~wxXmlResource() } +/* static */ +wxString wxXmlResource::ConvertFileNameToURL(const wxString& filename) +{ + wxString fnd(filename); + + // NB: as Load() and Unload() accept both filenames and URLs (should + // probably be changed to filenames only, but embedded resources + // currently rely on its ability to handle URLs - FIXME) we need to + // determine whether found name is filename and not URL and this is the + // fastest/simplest way to do it + if (wxFileName::FileExists(fnd)) + { + // Make the name absolute filename, because the app may + // change working directory later: + wxFileName fn(fnd); + if (fn.IsRelative()) + { + fn.MakeAbsolute(); + fnd = fn.GetFullPath(); + } +#if wxUSE_FILESYSTEM + fnd = wxFileSystem::FileNameToURL(fnd); +#endif + } + + return fnd; +} + +#if wxUSE_FILESYSTEM + +/* static */ +bool wxXmlResource::IsArchive(const wxString& filename) +{ + const wxString fnd = filename.Lower(); + + return fnd.Matches(wxT("*.zip")) || fnd.Matches(wxT("*.xrs")); +} + +#endif // wxUSE_FILESYSTEM + bool wxXmlResource::Load(const wxString& filemask) { wxString fnd; @@ -100,36 +139,17 @@ bool wxXmlResource::Load(const wxString& filemask) fnd = wxXmlFindFirst; else fnd = filemask; - while (!!fnd) + while (!fnd.empty()) { - // 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.MakeAbsolute(); - fnd = fn.GetFullPath(); - } -#if wxUSE_FILESYSTEM - fnd = wxFileSystem::FileNameToURL(fnd); -#endif - } + fnd = ConvertFileNameToURL(fnd); #if wxUSE_FILESYSTEM - if (fnd.Lower().Matches(wxT("*.zip")) || - fnd.Lower().Matches(wxT("*.xrs"))) + if ( IsArchive(fnd) ) { rt = rt && Load(fnd + wxT("#zip:*.xrc")); } - else -#endif + else // a single resource URL +#endif // wxUSE_FILESYSTEM { drec = new wxXmlResourceDataRecord; drec->File = fnd; @@ -146,6 +166,46 @@ bool wxXmlResource::Load(const wxString& filemask) return rt && UpdateResources(); } +bool wxXmlResource::Unload(const wxString& filename) +{ + wxASSERT_MSG( !wxIsWild(filename), + _T("wildcards not supported by wxXmlResource::Unload()") ); + + wxString fnd = ConvertFileNameToURL(filename); +#if wxUSE_FILESYSTEM + const bool isArchive = IsArchive(fnd); + if ( isArchive ) + fnd += _T("#zip:"); +#endif // wxUSE_FILESYSTEM + + bool unloaded = false; + const size_t count = m_data.GetCount(); + for ( size_t i = 0; i < count; i++ ) + { +#if wxUSE_FILESYSTEM + if ( isArchive ) + { + if ( m_data[i].File.StartsWith(fnd) ) + unloaded = true; + // don't break from the loop, we can have other matching files + } + else // a single resource URL +#endif // wxUSE_FILESYSTEM + { + if ( m_data[i].File == fnd ) + { + m_data.RemoveAt(i); + unloaded = true; + + // no sense in continuing, there is only one file with this URL + break; + } + } + } + + return unloaded; +} + IMPLEMENT_ABSTRACT_CLASS(wxXmlResourceHandler, wxObject) @@ -724,9 +784,11 @@ void wxXmlResourceHandler::AddWindowStyles() XRC_ADD_STYLE(wxNO_BORDER); XRC_ADD_STYLE(wxTRANSPARENT_WINDOW); XRC_ADD_STYLE(wxWANTS_CHARS); + XRC_ADD_STYLE(wxTAB_TRAVERSAL); XRC_ADD_STYLE(wxNO_FULL_REPAINT_ON_RESIZE); XRC_ADD_STYLE(wxFULL_REPAINT_ON_RESIZE); XRC_ADD_STYLE(wxWS_EX_BLOCK_EVENTS); + XRC_ADD_STYLE(wxWS_EX_VALIDATE_RECURSIVELY); } @@ -795,11 +857,11 @@ wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate) case wxT('n'): str2 << wxT('\n'); break; - + case wxT('t'): str2 << wxT('\t'); break; - + case wxT('r'): str2 << wxT('\r'); break; @@ -812,7 +874,7 @@ wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate) break; } // else fall-through to default: branch below - + default: str2 << wxT('\\') << *dt; break; @@ -838,13 +900,11 @@ wxString wxXmlResourceHandler::GetText(const wxString& param, bool translate) #endif } } - else - { - // If wxXRC_USE_LOCALE is not set, then the string is already in - // system's default encoding in ANSI build, so we don't have to - // do anything special here. - return str2; - } + + // If wxXRC_USE_LOCALE is not set, then the string is already in + // system's default encoding in ANSI build, so we don't have to + // do anything special here. + return str2; } @@ -859,18 +919,22 @@ long wxXmlResourceHandler::GetLong(const wxString& param, long defaultv) return value; } - + float wxXmlResourceHandler::GetFloat(const wxString& param, float defaultv) { double value; wxString str1 = GetParamValue(param); +#ifndef __WXWINCE__ const char *prevlocale = setlocale(LC_NUMERIC, "C"); - +#endif + if (!str1.ToDouble(&value)) value = defaultv; +#ifndef __WXWINCE__ setlocale(LC_NUMERIC, prevlocale); +#endif return value; } @@ -895,7 +959,8 @@ bool wxXmlResourceHandler::GetBool(const wxString& param, bool defaultv) wxString v = GetParamValue(param); v.MakeLower(); if (!v) return defaultv; - else return (v == wxT("1")); + + return (v == wxT("1")); } @@ -963,7 +1028,7 @@ wxColour wxXmlResourceHandler::GetColour(const wxString& param) wxColour clr = GetSystemColour(v); if (clr.Ok()) return clr; - + wxLogError(_("XRC resource: Incorrect colour specification '%s' for property '%s'."), v.c_str(), param.c_str()); return wxNullColour; @@ -992,7 +1057,7 @@ wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, scl = defaultArtClient; else scl = wxART_MAKE_CLIENT_ID_FROM_STR(scl); - + wxBitmap stockArt = wxArtProvider::GetBitmap(wxART_MAKE_ART_ID_FROM_STR(sid), scl, size); @@ -1003,7 +1068,7 @@ wxBitmap wxXmlResourceHandler::GetBitmap(const wxString& param, /* ...or load the bitmap from file: */ wxString name = GetParamValue(param); - if (name.IsEmpty()) return wxNullBitmap; + if (name.empty()) return wxNullBitmap; #if wxUSE_FILESYSTEM wxFSFile *fsfile = GetCurFileSystem().OpenFile(name); if (fsfile == NULL) @@ -1078,7 +1143,7 @@ wxString wxXmlResourceHandler::GetNodeContent(wxXmlNode *node) wxString wxXmlResourceHandler::GetParamValue(const wxString& param) { - if (param.IsEmpty()) + if (param.empty()) return GetNodeContent(m_node); else return GetNodeContent(GetParamNode(param)); @@ -1086,10 +1151,11 @@ wxString wxXmlResourceHandler::GetParamValue(const wxString& param) -wxSize wxXmlResourceHandler::GetSize(const wxString& param) +wxSize wxXmlResourceHandler::GetSize(const wxString& param, + wxWindow *windowToUse) { wxString s = GetParamValue(param); - if (s.IsEmpty()) s = wxT("-1,-1"); + if (s.empty()) s = wxT("-1,-1"); bool is_dlg; long sx, sy = 0; @@ -1105,15 +1171,22 @@ wxSize wxXmlResourceHandler::GetSize(const wxString& param) if (is_dlg) { - if (m_parentAsWindow) + if (windowToUse) + { + return wxDLG_UNIT(windowToUse, wxSize(sx, sy)); + } + else if (m_parentAsWindow) + { return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, sy)); + } else { wxLogError(_("Cannot convert dialog units: dialog unknown.")); return wxDefaultSize; } } - else return wxSize(sx, sy); + + return wxSize(sx, sy); } @@ -1126,10 +1199,12 @@ wxPoint wxXmlResourceHandler::GetPosition(const wxString& param) -wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaultv) +wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, + wxCoord defaultv, + wxWindow *windowToUse) { wxString s = GetParamValue(param); - if (s.IsEmpty()) return defaultv; + if (s.empty()) return defaultv; bool is_dlg; long sx; @@ -1144,15 +1219,22 @@ wxCoord wxXmlResourceHandler::GetDimension(const wxString& param, wxCoord defaul if (is_dlg) { - if (m_parentAsWindow) + if (windowToUse) + { + return wxDLG_UNIT(windowToUse, wxSize(sx, 0)).x; + } + else if (m_parentAsWindow) + { return wxDLG_UNIT(m_parentAsWindow, wxSize(sx, 0)).x; + } else { wxLogError(_("Cannot convert dialog units: dialog unknown.")); return defaultv; } } - else return sx; + + return sx; } @@ -1194,7 +1276,7 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) // size int isize = wxDEFAULT; bool hasSize = HasParam(wxT("size")); - if (hasSize) + if (hasSize) isize = GetLong(wxT("size"), wxDEFAULT); // style @@ -1203,9 +1285,9 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) if (hasStyle) { wxString style = GetParamValue(wxT("style")); - if (style == wxT("italic")) + if (style == wxT("italic")) istyle = wxITALIC; - else if (style == wxT("slant")) + else if (style == wxT("slant")) istyle = wxSLANT; } @@ -1215,12 +1297,12 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) if (hasWeight) { wxString weight = GetParamValue(wxT("weight")); - if (weight == wxT("bold")) + if (weight == wxT("bold")) iweight = wxBOLD; - else if (weight == wxT("light")) + else if (weight == wxT("light")) iweight = wxLIGHT; } - + // underline bool hasUnderlined = HasParam(wxT("underlined")); bool underlined = hasUnderlined ? GetBool(wxT("underlined"), false) : false; @@ -1238,8 +1320,8 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) else if (family == wxT("modern")) ifamily = wxMODERN; else if (family == wxT("teletype")) ifamily = wxTELETYPE; } - - + + wxString facename; bool hasFacename = HasParam(wxT("face")); if (hasFacename) @@ -1266,7 +1348,7 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) { wxString encoding = GetParamValue(wxT("encoding")); wxFontMapper mapper; - if (!encoding.IsEmpty()) + if (!encoding.empty()) enc = mapper.CharsetToEncoding(encoding); if (enc == wxFONTENCODING_SYSTEM) enc = wxFONTENCODING_DEFAULT; @@ -1274,7 +1356,7 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) // is this font based on a system font? wxFont sysfont = GetSystemFont(GetParamValue(wxT("sysfont"))); - + if (sysfont.Ok()) { if (hasSize) @@ -1282,7 +1364,7 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) else if (HasParam(wxT("relativesize"))) sysfont.SetPointSize(int(sysfont.GetPointSize() * GetFloat(wxT("relativesize")))); - + if (hasStyle) sysfont.SetStyle(istyle); if (hasWeight) @@ -1299,12 +1381,10 @@ wxFont wxXmlResourceHandler::GetFont(const wxString& param) m_node = oldnode; return sysfont; } - else - { - m_node = oldnode; - return wxFont(isize, ifamily, istyle, iweight, - underlined, facename, enc); - } + + m_node = oldnode; + return wxFont(isize, ifamily, istyle, iweight, + underlined, facename, enc); } @@ -1333,6 +1413,8 @@ void wxXmlResourceHandler::SetupWindow(wxWindow *wnd) #endif if (HasParam(wxT("font"))) wnd->SetFont(GetFont()); + if (HasParam(wxT("help"))) + wnd->SetHelpText(GetText(wxT("help"))); } @@ -1432,8 +1514,18 @@ static int XRCID_Lookup(const wxChar *str_id, int value_if_not_found = -2) return (*rec_var)->id; } +static void AddStdXRCID_Records(); + /*static*/ int wxXmlResource::GetXRCID(const wxChar *str_id) { + static bool s_stdIDsAdded = false; + + if ( !s_stdIDsAdded ) + { + s_stdIDsAdded = true; + AddStdXRCID_Records(); + } + return XRCID_Lookup(str_id); } @@ -1464,7 +1556,7 @@ static void AddStdXRCID_Records() stdID(wxID_ANY); stdID(wxID_SEPARATOR); - + stdID(wxID_OPEN); stdID(wxID_CLOSE); stdID(wxID_NEW); @@ -1578,7 +1670,6 @@ public: wxXmlResourceModule() {} bool OnInit() { - AddStdXRCID_Records(); wxXmlResource::AddSubclassFactory(new wxXmlSubclassFactoryCXX); return true; }