Add wxCursor ctor from XPM data to all ports

This just uses the existing wxImage ctor from XPM data and wxCursor ctor
from wxImage, but will allow the code creating cursors from XPM to still
work even when wxImage ctor from XPM is made explicit.

Add a trivial test just to check that the new ctor can be used.
This commit is contained in:
Vadim Zeitlin
2021-04-17 19:32:54 +01:00
parent 9c6e67cac0
commit 72500faf7c
16 changed files with 122 additions and 1 deletions

View File

@@ -11,6 +11,7 @@
#define _WX_DFB_CURSOR_H_ #define _WX_DFB_CURSOR_H_
class WXDLLIMPEXP_FWD_CORE wxBitmap; class WXDLLIMPEXP_FWD_CORE wxBitmap;
class WXDLLIMPEXP_FWD_CORE wxImage;
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxCursor // wxCursor
@@ -24,6 +25,10 @@ public:
#if WXWIN_COMPATIBILITY_2_8 #if WXWIN_COMPATIBILITY_2_8
wxCursor(int id) { InitFromStock((wxStockCursor)id); } wxCursor(int id) { InitFromStock((wxStockCursor)id); }
#endif #endif
#if wxUSE_IMAGE
wxCursor(const wxImage& image);
wxCursor(const char* const* xpmData);
#endif // wxUSE_IMAGE
wxCursor(const wxString& name, wxCursor(const wxString& name,
wxBitmapType type = wxCURSOR_DEFAULT_TYPE, wxBitmapType type = wxCURSOR_DEFAULT_TYPE,
int hotSpotX = 0, int hotSpotY = 0); int hotSpotX = 0, int hotSpotY = 0);

View File

@@ -25,6 +25,7 @@ public:
#endif #endif
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor( const wxImage & image ); wxCursor( const wxImage & image );
wxCursor(const char* const* xpmData);
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxCursor(const wxString& name, wxCursor(const wxString& name,
wxBitmapType type = wxCURSOR_DEFAULT_TYPE, wxBitmapType type = wxCURSOR_DEFAULT_TYPE,

View File

@@ -32,6 +32,7 @@ public:
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor(const wxImage& image); wxCursor(const wxImage& image);
wxCursor(const char* const* xpmData);
#endif #endif
wxCursor(wxStockCursor id) { InitFromStock(id); } wxCursor(wxStockCursor id) { InitFromStock(id); }
@@ -52,6 +53,10 @@ protected:
private: private:
void InitFromStock(wxStockCursor); void InitFromStock(wxStockCursor);
#if wxUSE_IMAGE
void InitFromImage(const wxImage& image);
#endif
void Create(const char bits[], int width, int height, void Create(const char bits[], int width, int height,
int hotSpotX = -1, int hotSpotY = -1, int hotSpotX = -1, int hotSpotY = -1,
const char maskBits[] = NULL); const char maskBits[] = NULL);

View File

@@ -21,6 +21,7 @@ public:
wxCursor(); wxCursor();
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor(const wxImage& image); wxCursor(const wxImage& image);
wxCursor(const char* const* xpmData);
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxCursor(const wxString& name, wxCursor(const wxString& name,
wxBitmapType type = wxCURSOR_DEFAULT_TYPE, wxBitmapType type = wxCURSOR_DEFAULT_TYPE,
@@ -44,6 +45,10 @@ protected:
virtual wxGDIImageRefData *CreateData() const wxOVERRIDE; virtual wxGDIImageRefData *CreateData() const wxOVERRIDE;
private: private:
#if wxUSE_IMAGE
void InitFromImage(const wxImage& image);
#endif // wxUSE_IMAGE
wxDECLARE_DYNAMIC_CLASS(wxCursor); wxDECLARE_DYNAMIC_CLASS(wxCursor);
}; };

View File

@@ -21,6 +21,7 @@ public:
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor(const wxImage & image) ; wxCursor(const wxImage & image) ;
wxCursor(const char* const* xpmData);
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxCursor(const wxString& name, wxCursor(const wxString& name,
wxBitmapType type = wxCURSOR_DEFAULT_TYPE, wxBitmapType type = wxCURSOR_DEFAULT_TYPE,

View File

@@ -22,6 +22,7 @@ public:
#endif #endif
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor( const wxImage & image ); wxCursor( const wxImage & image );
wxCursor(const char* const* xpmData);
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxCursor(const wxString& name, wxCursor(const wxString& name,
wxBitmapType type = wxCURSOR_DEFAULT_TYPE, wxBitmapType type = wxCURSOR_DEFAULT_TYPE,

View File

@@ -29,6 +29,7 @@ public:
#endif #endif
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor( const wxImage & image ); wxCursor( const wxImage & image );
wxCursor(const char* const* xpmData);
#endif #endif
wxCursor(const wxString& name, wxCursor(const wxString& name,

View File

@@ -190,6 +190,17 @@ public:
*/ */
wxCursor(const wxImage& image); wxCursor(const wxImage& image);
/**
Constructs a cursor from XPM data.
In versions of wxWidgets until 3.1.6 constructing wxCursor from XPM
data implicitly used wxImage constructor from XPM data and wxCursor
constructor from wxImage. Since 3.1.6 this constructor overload is
available to allow constructing wxCursor from XPM to still work, even
though wxImage constructor from XPM is now @c explicit.
*/
wxCursor(const char* const* xpmData);
/** /**
Copy constructor, uses @ref overview_refcount "reference counting". Copy constructor, uses @ref overview_refcount "reference counting".

View File

@@ -53,6 +53,20 @@ void wxCursor::InitFromStock(wxStockCursor cursorId)
#warning "FIXME -- implement the cursor as bitmaps (that's what DFB uses)" #warning "FIXME -- implement the cursor as bitmaps (that's what DFB uses)"
} }
#if wxUSE_IMAGE
wxCursor::wxCursor(const wxImage& image)
{
#warning "FIXME"
}
wxCursor::wxCursor(const char* const* xpmData)
{
#warning "FIXME"
}
#endif // wxUSE_IMAGE
wxCursor::wxCursor(const wxString& cursor_file, wxCursor::wxCursor(const wxString& cursor_file,
wxBitmapType type, wxBitmapType type,
int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY)) int WXUNUSED(hotSpotX), int WXUNUSED(hotSpotY))

View File

@@ -76,6 +76,7 @@ wxCursor::wxCursor(const wxString& cursor_file,
wxBitmapType type, wxBitmapType type,
int hotSpotX, int hotSpotY) int hotSpotX, int hotSpotY)
{ {
#if wxUSE_IMAGE
wxImage img; wxImage img;
if (!img.LoadFile(cursor_file, type)) if (!img.LoadFile(cursor_file, type))
return; return;
@@ -95,6 +96,11 @@ wxCursor::wxCursor(const wxImage& img)
{ {
InitFromImage(img); InitFromImage(img);
} }
wxCursor::wxCursor(const char* const* xpmData)
{
InitFromImage(wxImage(xpmData));
}
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxCursor::wxCursor(const char bits[], int width, int height, wxCursor::wxCursor(const char bits[], int width, int height,

View File

@@ -93,7 +93,7 @@ wxCursor::wxCursor()
} }
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor::wxCursor(const wxImage & image) void wxCursor::InitFromImage(const wxImage & image)
{ {
unsigned char * rgbBits = image.GetData(); unsigned char * rgbBits = image.GetData();
int w = image.GetWidth() ; int w = image.GetWidth() ;
@@ -174,6 +174,16 @@ wxCursor::wxCursor(const wxImage & image)
delete[] bits; delete[] bits;
delete[] maskBits; delete[] maskBits;
} }
wxCursor::wxCursor(const wxImage& image)
{
InitFromImage(image);
}
wxCursor::wxCursor(const char* const* xpmData)
{
InitFromImage(wxImage(xpmData));
}
#endif #endif
void wxCursor::Create(const char bits[], int width, int height, void wxCursor::Create(const char bits[], int width, int height,

View File

@@ -153,6 +153,16 @@ wxCursor::wxCursor()
#if wxUSE_IMAGE #if wxUSE_IMAGE
wxCursor::wxCursor(const wxImage& image) wxCursor::wxCursor(const wxImage& image)
{
InitFromImage(image);
}
wxCursor::wxCursor(const char* const* xpmData)
{
InitFromImage(wxImage(xpmData));
}
void wxCursor::InitFromImage(const wxImage& image)
{ {
// image has to be of the standard cursor size, otherwise we won't be able // image has to be of the standard cursor size, otherwise we won't be able
// to create it // to create it

View File

@@ -230,6 +230,11 @@ wxCursor::wxCursor( const wxImage &image )
{ {
InitFromImage( image ) ; InitFromImage( image ) ;
} }
wxCursor::wxCursor(const char* const* xpmData)
{
InitFromImage( wxImage(xpmData) ) ;
}
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxGDIRefData *wxCursor::CreateGDIRefData() const wxGDIRefData *wxCursor::CreateGDIRefData() const

View File

@@ -83,6 +83,11 @@ wxCursor::wxCursor(const wxImage& img)
{ {
InitFromImage(img); InitFromImage(img);
} }
wxCursor::wxCursor(const char* const* xpmData)
{
InitFromImage(wxImage(xpmData));
}
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE
wxPoint wxCursor::GetHotSpot() const wxPoint wxCursor::GetHotSpot() const

View File

@@ -140,6 +140,11 @@ wxCursor::wxCursor( const wxImage & WXUNUSED(image) )
{ {
wxFAIL_MSG( wxT("wxCursor creation from wxImage not yet implemented") ); wxFAIL_MSG( wxT("wxCursor creation from wxImage not yet implemented") );
} }
wxCursor::wxCursor(const char* const* WXUNUSED(xpmData))
{
wxFAIL_MSG( wxT("wxCursor creation from XPM not yet implemented") );
}
#endif #endif
wxCursor::~wxCursor() wxCursor::~wxCursor()

View File

@@ -21,6 +21,8 @@
#include "wx/anidecod.h" // wxImageArray #include "wx/anidecod.h" // wxImageArray
#include "wx/bitmap.h" #include "wx/bitmap.h"
#include "wx/cursor.h"
#include "wx/icon.h"
#include "wx/palette.h" #include "wx/palette.h"
#include "wx/url.h" #include "wx/url.h"
#include "wx/log.h" #include "wx/log.h"
@@ -2164,6 +2166,40 @@ TEST_CASE("wxImage::InitAlpha", "[image][initalpha]")
} }
} }
TEST_CASE("wxImage::XPM", "[image][xpm]")
{
static const char * dummy_xpm[] = {
"16 16 2 1",
"@ c Black",
" c None",
"@ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @ ",
" @"
};
wxImage image(dummy_xpm);
CHECK( image.IsOk() );
// The goal here is mostly just to check that this code compiles, i.e. that
// creating all these classes from XPM works.
CHECK( wxBitmap(dummy_xpm).IsOk() );
CHECK( wxCursor(dummy_xpm).IsOk() );
CHECK( wxIcon(dummy_xpm).IsOk() );
}
/* /*
TODO: add lots of more tests to wxImage functions TODO: add lots of more tests to wxImage functions
*/ */