Refactor wxImageList classes to use wxImageListBase

This ensures that the native MSW and generic implementations provide the
same API by forcing them to implement the same pure virtual functions.
This commit is contained in:
Vadim Zeitlin 2023-11-03 01:49:34 +01:00
parent 3f971f52c1
commit 0d4792cc2d
3 changed files with 87 additions and 45 deletions

View file

@ -10,42 +10,38 @@
#ifndef _WX_IMAGLISTG_H_
#define _WX_IMAGLISTG_H_
#include "wx/bitmap.h"
#include "wx/gdicmn.h"
#include "wx/vector.h"
class WXDLLIMPEXP_FWD_CORE wxDC;
class WXDLLIMPEXP_FWD_CORE wxIcon;
class WXDLLIMPEXP_FWD_CORE wxColour;
class WXDLLIMPEXP_CORE wxGenericImageList: public wxObject
class WXDLLIMPEXP_CORE wxGenericImageList : public wxImageListBase
{
public:
wxGenericImageList();
wxGenericImageList( int width, int height, bool mask = true, int initialCount = 1 );
virtual ~wxGenericImageList();
bool Create( int width, int height, bool mask = true, int initialCount = 1 );
void Destroy();
virtual void Destroy() override;
virtual int GetImageCount() const;
virtual bool GetSize( int index, int &width, int &height ) const;
virtual wxSize GetSize() const { return m_size; }
virtual int GetImageCount() const override;
virtual bool GetSize( int index, int &width, int &height ) const override;
int Add( const wxBitmap& bitmap );
int Add( const wxBitmap& bitmap, const wxBitmap& mask );
int Add( const wxBitmap& bitmap, const wxColour& maskColour );
wxBitmap GetBitmap(int index) const;
wxIcon GetIcon(int index) const;
bool Replace( int index,
using wxImageListBase::GetSize;
virtual int Add( const wxBitmap& bitmap ) override;
virtual int Add( const wxBitmap& bitmap, const wxBitmap& mask ) override;
virtual int Add( const wxBitmap& bitmap, const wxColour& maskColour ) override;
virtual wxBitmap GetBitmap(int index) const override;
virtual wxIcon GetIcon(int index) const override;
virtual bool Replace( int index,
const wxBitmap& bitmap,
const wxBitmap& mask = wxNullBitmap );
bool Remove( int index );
bool RemoveAll();
const wxBitmap& mask = wxNullBitmap ) override;
virtual bool Remove( int index ) override;
virtual bool RemoveAll() override;
virtual bool Draw(int index, wxDC& dc, int x, int y,
int flags = wxIMAGELIST_DRAW_NORMAL,
bool solidBackground = false);
bool solidBackground = false) override;
#if WXWIN_COMPATIBILITY_3_0
wxDEPRECATED_MSG("Don't use this overload: it's not portable and does nothing")
@ -61,10 +57,6 @@ private:
wxBitmap GetImageListBitmap(const wxBitmap& bitmap) const;
wxVector<wxBitmap> m_images;
bool m_useMask;
// Size of a single bitmap in the list.
wxSize m_size;
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxGenericImageList);
};

View file

@ -12,6 +12,13 @@
#include "wx/defs.h"
#include "wx/bitmap.h"
class WXDLLIMPEXP_FWD_CORE wxDC;
class WXDLLIMPEXP_FWD_CORE wxIcon;
class WXDLLIMPEXP_FWD_CORE wxColour;
/*
* wxImageList is used for wxListCtrl, wxTreeCtrl. These controls refer to
* images for their items by an index into an image list.
@ -40,6 +47,51 @@ enum
#define wxIMAGELIST_DRAW_SELECTED 0x0004
#define wxIMAGELIST_DRAW_FOCUSED 0x0008
// Define the interface of platform-specific wxImageList class.
class wxImageListBase : public wxObject
{
public:
/*
This class should provide default ctor as well as the following ctor:
wxImageList(int width, int height, bool mask = true, int initialCount = 1)
and Create() member function taking the same parameters and returning
bool.
*/
virtual void Destroy() = 0;
// Returns the size the image list was created with.
wxSize GetSize() const { return m_size; }
virtual int GetImageCount() const = 0;
virtual bool GetSize(int index, int &width, int &height) const = 0;
virtual int Add(const wxBitmap& bitmap) = 0;
virtual int Add(const wxBitmap& bitmap, const wxBitmap& mask) = 0;
virtual int Add(const wxBitmap& bitmap, const wxColour& maskColour) = 0;
virtual bool Replace(int index,
const wxBitmap& bitmap,
const wxBitmap& mask = wxNullBitmap) = 0;
virtual bool Remove(int index) = 0;
virtual bool RemoveAll() = 0;
virtual bool Draw(int index, wxDC& dc, int x, int y,
int flags = wxIMAGELIST_DRAW_NORMAL,
bool solidBackground = false) = 0;
virtual wxBitmap GetBitmap(int index) const = 0;
virtual wxIcon GetIcon(int index) const = 0;
protected:
// Size of a single bitmap in the list in logical pixels.
wxSize m_size;
bool m_useMask = false;
};
#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
#include "wx/msw/imaglist.h"
#define wxHAS_NATIVE_IMAGELIST

View file

@ -10,9 +10,7 @@
#ifndef _WX_IMAGLIST_H_
#define _WX_IMAGLIST_H_
#include "wx/bitmap.h"
class WXDLLIMPEXP_CORE wxImageList : public wxObject
class WXDLLIMPEXP_CORE wxImageList : public wxImageListBase
{
public:
/*
@ -36,13 +34,12 @@ public:
////////////////////////////////////////////////////////////////////////////
// Returns the number of images in the image list.
int GetImageCount() const;
virtual int GetImageCount() const override;
// Returns the size (same for all images) of the images in the list
bool GetSize(int index, int &width, int &height) const;
virtual bool GetSize(int index, int &width, int &height) const override;
// Returns the overall size
wxSize GetSize() const { return m_size; }
using wxImageListBase::GetSize;
// Operations
////////////////////////////////////////////////////////////////////////////
@ -54,17 +51,21 @@ public:
bool Create(int width, int height, bool mask = true, int initialNumber = 1);
// Destroys the image list, Create() may then be called again later.
void Destroy();
virtual void Destroy() override;
// Adds a bitmap, and optionally a mask bitmap.
// Note that wxImageList creates *new* bitmaps, so you may delete
// 'bitmap' and 'mask' after calling Add.
int Add(const wxBitmap& bitmap, const wxBitmap& mask = wxNullBitmap);
virtual int Add(const wxBitmap& bitmap, const wxBitmap& mask) override;
virtual int Add(const wxBitmap& bitmap) override
{
return Add(bitmap, wxNullBitmap);
}
// Adds a bitmap, using the specified colour to create the mask bitmap
// Note that wxImageList creates *new* bitmaps, so you may delete
// 'bitmap' after calling Add.
int Add(const wxBitmap& bitmap, const wxColour& maskColour);
virtual int Add(const wxBitmap& bitmap, const wxColour& maskColour) override;
// Adds a bitmap and mask from an icon.
int Add(const wxIcon& icon);
@ -72,31 +73,31 @@ public:
// Replaces a bitmap, optionally passing a mask bitmap.
// Note that wxImageList creates new bitmaps, so you may delete
// 'bitmap' and 'mask' after calling Replace.
bool Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask = wxNullBitmap);
virtual bool Replace(int index, const wxBitmap& bitmap, const wxBitmap& mask = wxNullBitmap) override;
// Replaces a bitmap and mask from an icon.
// You can delete 'icon' after calling Replace.
bool Replace(int index, const wxIcon& icon);
// Removes the image at the given index.
bool Remove(int index);
virtual bool Remove(int index) override;
// Remove all images
bool RemoveAll();
virtual bool RemoveAll() override;
// Draws the given image on a dc at the specified position.
// If 'solidBackground' is true, Draw sets the image list background
// colour to the background colour of the wxDC, to speed up
// drawing by eliminating masked drawing where possible.
bool Draw(int index, wxDC& dc, int x, int y,
virtual bool Draw(int index, wxDC& dc, int x, int y,
int flags = wxIMAGELIST_DRAW_NORMAL,
bool solidBackground = false);
bool solidBackground = false) override;
// Get a bitmap
wxBitmap GetBitmap(int index) const;
virtual wxBitmap GetBitmap(int index) const override;
// Get an icon
wxIcon GetIcon(int index) const;
virtual wxIcon GetIcon(int index) const override;
// TODO: miscellaneous functionality
/*
@ -191,7 +192,6 @@ public:
protected:
WXHIMAGELIST m_hImageList = nullptr;
wxSize m_size;
private:
// Private helper used by GetImageListBitmaps().
@ -202,8 +202,6 @@ private:
void GetImageListBitmaps(wxMSWBitmaps& bitmaps,
const wxBitmap& bitmap, const wxBitmap& mask);
bool m_useMask = false;
wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxImageList);
};