From a7f5e3c5c3260f8c66be50af03e4a1d5dc6b8b38 Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Tue, 26 Oct 2021 22:27:36 +0200 Subject: [PATCH] OSX wxBitmapBundle::FromFiles implementation for macOS (#2561) * adding macOS wxBitmapBundle::FromFiles implementation * adding FromFiles overload, generic implementation * Update interface/wx/bmpbndl.h * Removing outdated comment Co-authored-by: VZ --- include/wx/bmpbndl.h | 5 +++++ interface/wx/bmpbndl.h | 15 ++++++++++++++ src/common/bmpbndl.cpp | 46 +++++++++++++++++++++++++++++++++++++++++ src/osx/core/bmpbndl.mm | 38 +++++++++++++++++++--------------- 4 files changed, 87 insertions(+), 17 deletions(-) diff --git a/include/wx/bmpbndl.h b/include/wx/bmpbndl.h index c0813b1cc4..5a13937a01 100644 --- a/include/wx/bmpbndl.h +++ b/include/wx/bmpbndl.h @@ -87,6 +87,11 @@ public: // form name_2x or name@2x (and also using other factors) will be used. static wxBitmapBundle FromResources(const wxString& name); + // Create from files: all existing versions of the bitmap of the + // form filename_2x or name@2x (and also using other factors) will be used. + static wxBitmapBundle FromFiles(const wxString& fullpathname); + static wxBitmapBundle FromFiles(const wxString& path, const wxString& filename, const wxString& extension = wxASCII_STR("png")); + // Create from existing implementation static wxBitmapBundle FromImpl(wxBitmapBundleImpl* impl); diff --git a/interface/wx/bmpbndl.h b/interface/wx/bmpbndl.h index 49cd69f233..400b2471c5 100644 --- a/interface/wx/bmpbndl.h +++ b/interface/wx/bmpbndl.h @@ -198,6 +198,21 @@ public: */ static wxBitmapBundle FromResources(const wxString& name); + /** + Create a bundle from bitmaps stored as files. + + Looking in @a path for files using @a filename as prefix and potentionally a + suffix with scale, e.g. "_2x" or "@2x" + + @param path Path of the directory containing the files + @param filename Bitmap's filename without any scale suffix + @param extension File extension, without leading dot (`png` by default) + */ + static wxBitmapBundle FromFiles(const wxString& path, const wxString& filename, const wxString& extension = "png"); + + /// @overload + static wxBitmapBundle FromFiles(const wxString& fullpathname); + /** Create a bundle from the SVG image. diff --git a/src/common/bmpbndl.cpp b/src/common/bmpbndl.cpp index c5e269e102..37040f634b 100644 --- a/src/common/bmpbndl.cpp +++ b/src/common/bmpbndl.cpp @@ -24,6 +24,7 @@ #include "wx/bmpbndl.h" #include "wx/icon.h" #include "wx/window.h" +#include "wx/filename.h" #include "wx/private/bmpbndl.h" @@ -374,6 +375,51 @@ wxBitmapBundle wxBitmapBundle::FromResources(const wxString& WXUNUSED(name)) #endif // !__WXMSW__ && !__WXOSX__ +wxBitmapBundle wxBitmapBundle::FromFiles(const wxString& filename) +{ + wxFileName fn(filename); + return FromFiles(fn.GetPath(wxPATH_GET_VOLUME), fn.GetName(), fn.GetExt()); +} + +#if !defined( __WXOSX__ ) + +/* static */ +wxBitmapBundle wxBitmapBundle::FromFiles(const wxString& path, const wxString& filename, const wxString& extension) +{ + wxVector bitmaps; + + wxFileName fn(path, filename, extension); + wxString ext = extension.Lower(); + + for ( int dpiFactor = 1 ; dpiFactor <= 2 ; ++dpiFactor) + { + if ( dpiFactor == 1 ) + fn.SetName(filename); + else + fn.SetName(wxString::Format("%s@%dx", filename, dpiFactor)); + + if ( !fn.FileExists() && dpiFactor != 1 ) + { + // try alternate naming scheme + fn.SetName(wxString::Format("%s_%dx", filename, dpiFactor)); + } + + if ( fn.FileExists() ) + { + wxBitmap bmp(fn.GetFullPath(), wxBITMAP_TYPE_ANY); + + if ( bmp.IsOk() ) + { + bitmaps.push_back(bmp); + } + } + } + + return wxBitmapBundle::FromBitmaps(bitmaps); +} + +#endif + wxSize wxBitmapBundle::GetDefaultSize() const { if ( !m_impl ) diff --git a/src/osx/core/bmpbndl.mm b/src/osx/core/bmpbndl.mm index 70f3483d83..590ce233fd 100644 --- a/src/osx/core/bmpbndl.mm +++ b/src/osx/core/bmpbndl.mm @@ -22,6 +22,8 @@ #endif // WX_PRECOMP #include "wx/bmpbndl.h" +#include "wx/filename.h" +#include "wx/stdpaths.h" #include "wx/private/bmpbndl.h" @@ -184,34 +186,31 @@ void wxOSXAddBitmapToImage( WXImage image, const wxBitmap& bmp) } #endif -wxBitmapBundle wxBitmapBundle::FromResources(const wxString& name) +wxBitmapBundle wxBitmapBundle::FromFiles(const wxString& path, const wxString& filename, const wxString& extension) { - wxVector resources; + wxVector bitmaps; - wxString ext = "png"; // TODO adapt as soon as we get res param - wxCFStringRef restype(ext); + wxFileName fn(path, filename, extension); + wxString ext = extension.Lower(); for ( int dpiFactor = 1 ; dpiFactor <= 2 ; ++dpiFactor) { - wxString filename; if ( dpiFactor == 1 ) - filename = name; + fn.SetName(filename); else - filename = wxString::Format("%s@%dx", name, dpiFactor); + fn.SetName(wxString::Format("%s@%dx", filename, dpiFactor)); - wxCFStringRef resname(filename); - wxCFRef imageURL(CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL)); - if ( !imageURL.get() && dpiFactor != 1 ) + if ( !fn.FileExists() && dpiFactor != 1 ) { // try alternate naming scheme - filename = wxString::Format("%s_%dx", name, dpiFactor); - resname = wxCFStringRef(filename); - imageURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), resname, restype, NULL); + fn.SetName(wxString::Format("%s_%dx", filename, dpiFactor)); } - if ( imageURL.get() ) + + if ( fn.FileExists() ) { + wxCFRef imageURL(wxOSXCreateURLFromFileSystemPath(fn.GetFullPath())); // Create the data provider object - wxCFRef provider(CGDataProviderCreateWithURL (imageURL) ); + wxCFRef provider(CGDataProviderCreateWithURL(imageURL)); CGImageRef image = NULL; if ( ext == "jpeg" ) @@ -224,12 +223,17 @@ wxBitmapBundle wxBitmapBundle::FromResources(const wxString& name) { wxBitmap bmp(image, dpiFactor); CGImageRelease(image); - resources.push_back(bmp); + bitmaps.push_back(bmp); } } } - return wxBitmapBundle::FromBitmaps(resources); + return wxBitmapBundle::FromBitmaps(bitmaps); +} + +wxBitmapBundle wxBitmapBundle::FromResources(const wxString& name) +{ + return wxBitmapBundle::FromFiles(wxStandardPaths::Get().GetResourcesDir(), name, "png"); } WXImage wxOSXGetImageFromBundle(const wxBitmapBundle& bundle)