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 <vz-github@zeitlins.org>
This commit is contained in:
Stefan Csomor
2021-10-26 22:27:36 +02:00
committed by GitHub
parent 5f8e9c14ae
commit a7f5e3c5c3
4 changed files with 87 additions and 17 deletions

View File

@@ -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);

View File

@@ -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.

View File

@@ -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<wxBitmap> 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 )

View File

@@ -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,32 +186,29 @@ 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<wxBitmap> resources;
wxVector<wxBitmap> 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<CFURLRef> 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<CFURLRef> imageURL(wxOSXCreateURLFromFileSystemPath(fn.GetFullPath()));
// Create the data provider object
wxCFRef<CGDataProviderRef> provider(CGDataProviderCreateWithURL(imageURL));
CGImageRef image = NULL;
@@ -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)