Add wxWithImages helper mix-in with {Set,Get,Assign}ImageList() methods.

Avoid defining SetImageList() in several different places in wx API as not
only this resulted in (trivial) code duplication but this method also had
different semantics before: it didn't take ownership of the pointer passed to
it in wxTreeCtrl, wxListCtrl and wxBookCtrl and derived classes but did take
its ownership in wxDataViewTreeCtrl and wxRichTextFormattingDialog.

Harmonize this for all the classes now: SetImageList() never takes ownership
while AssignImageList() (which is now available in all classes having
SetImageList()) always does.

Also add convenience wxWithImages::GetImage() helper to avoid (more) code
duplication in wxDataViewTreeCtrl code.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68809 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-08-21 14:08:43 +00:00
parent 36a96421b3
commit abfdefede3
17 changed files with 141 additions and 144 deletions

View File

@@ -22,6 +22,7 @@
#include "wx/control.h"
#include "wx/dynarray.h"
#include "wx/withimages.h"
WX_DEFINE_EXPORTED_ARRAY_PTR(wxWindow *, wxArrayPages);
@@ -54,7 +55,8 @@ enum
// wxBookCtrlBase
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxBookCtrlBase : public wxControl
class WXDLLIMPEXP_CORE wxBookCtrlBase : public wxControl,
public wxWithImages
{
public:
// construction
@@ -85,9 +87,6 @@ public:
long style = 0,
const wxString& name = wxEmptyString);
// dtor
virtual ~wxBookCtrlBase();
// accessors
// ---------
@@ -117,15 +116,6 @@ public:
// images belong to the same image list)
// ---------------------------------------------------------------------
// sets the image list to use, it is *not* deleted by the control
virtual void SetImageList(wxImageList *imageList);
// as SetImageList() but we will delete the image list ourselves
void AssignImageList(wxImageList *imageList);
// get pointer (may be NULL) to the associated image list
wxImageList* GetImageList() const { return m_imageList; }
// sets/returns item's image index in the current image list
virtual int GetPageImage(size_t n) const = 0;
virtual bool SetPageImage(size_t n, int imageId) = 0;
@@ -320,12 +310,6 @@ protected:
// the array of all pages of this control
wxArrayPages m_pages;
// the associated image list or NULL
wxImageList *m_imageList;
// true if we must delete m_imageList
bool m_ownsImageList;
// get the page area
virtual wxRect GetPageRect() const;

View File

@@ -24,6 +24,7 @@
#include "wx/weakref.h"
#include "wx/vector.h"
#include "wx/dataobj.h"
#include "wx/withimages.h"
class WXDLLIMPEXP_FWD_CORE wxImageList;
@@ -1230,10 +1231,11 @@ public:
//-----------------------------------------------------------------------------
class WXDLLIMPEXP_ADV wxDataViewTreeCtrl: public wxDataViewCtrl
class WXDLLIMPEXP_ADV wxDataViewTreeCtrl: public wxDataViewCtrl,
public wxWithImages
{
public:
wxDataViewTreeCtrl() { Init(); }
wxDataViewTreeCtrl() { }
wxDataViewTreeCtrl(wxWindow *parent,
wxWindowID id,
const wxPoint& pos = wxDefaultPosition,
@@ -1241,13 +1243,9 @@ public:
long style = wxDV_NO_HEADER | wxDV_ROW_LINES,
const wxValidator& validator = wxDefaultValidator)
{
Init();
Create(parent, id, pos, size, style, validator);
}
virtual ~wxDataViewTreeCtrl();
bool Create(wxWindow *parent,
wxWindowID id,
const wxPoint& pos = wxDefaultPosition,
@@ -1263,9 +1261,6 @@ public:
bool IsContainer( const wxDataViewItem& item ) const
{ return GetStore()->IsContainer(item); }
void SetImageList( wxImageList *imagelist );
wxImageList* GetImageList() { return m_imageList; }
wxDataViewItem AppendItem( const wxDataViewItem& parent,
const wxString &text, int icon = -1, wxClientData *data = NULL );
wxDataViewItem PrependItem( const wxDataViewItem& parent,
@@ -1310,14 +1305,6 @@ public:
void OnCollapsed( wxDataViewEvent &event );
void OnSize( wxSizeEvent &event );
private:
void Init()
{
m_imageList = NULL;
}
wxImageList *m_imageList;
private:
DECLARE_EVENT_TABLE()
DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewTreeCtrl)

View File

@@ -22,6 +22,7 @@
#include "wx/propdlg.h"
#include "wx/bookctrl.h"
#include "wx/withimages.h"
#if wxUSE_HTML
#include "wx/htmllbox.h"
@@ -32,7 +33,6 @@
#include "wx/richtext/richtextuicustomization.h"
class WXDLLIMPEXP_FWD_RICHTEXT wxRichTextFormattingDialog;
class WXDLLIMPEXP_FWD_CORE wxImageList;
class WXDLLIMPEXP_FWD_CORE wxComboBox;
class WXDLLIMPEXP_FWD_CORE wxCheckBox;
@@ -119,7 +119,8 @@ public:
* Formatting dialog for a wxRichTextCtrl
*/
class WXDLLIMPEXP_RICHTEXT wxRichTextFormattingDialog: public wxPropertySheetDialog
class WXDLLIMPEXP_RICHTEXT wxRichTextFormattingDialog: public wxPropertySheetDialog,
public wxWithImages
{
DECLARE_CLASS(wxRichTextFormattingDialog)
DECLARE_HELP_PROVISION()
@@ -194,10 +195,6 @@ public:
void OnHelp(wxCommandEvent& event);
void OnUpdateHelp(wxUpdateUIEvent& event);
/// Set/get image list
void SetImageList(wxImageList* imageList) { m_imageList = imageList; }
wxImageList* GetImageList() const { return m_imageList; }
/// Get/set formatting factory object
static void SetFormattingDialogFactory(wxRichTextFormattingDialogFactory* factory);
static wxRichTextFormattingDialogFactory* GetFormattingDialogFactory() { return ms_FormattingDialogFactory; }
@@ -237,7 +234,6 @@ public:
protected:
wxImageList* m_imageList;
wxRichTextAttr m_attributes;
//wxRichTextAttr m_resetAttributes;
wxRichTextStyleDefinition* m_styleDefinition;

91
include/wx/withimages.h Normal file
View File

@@ -0,0 +1,91 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/withimages.h
// Purpose: Declaration of a simple wxWithImages class.
// Author: Vadim Zeitlin
// Created: 2011-08-17
// RCS-ID: $Id: wxhead.h,v 1.12 2010-04-22 12:44:51 zeitlin Exp $
// Copyright: (c) 2011 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_WITHIMAGES_H_
#define _WX_WITHIMAGES_H_
#include "wx/defs.h"
#include "wx/imaglist.h"
// ----------------------------------------------------------------------------
// wxWithImages: mix-in class providing access to wxImageList.
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxWithImages
{
public:
wxWithImages()
{
m_imageList = NULL;
m_ownsImageList = false;
}
virtual ~wxWithImages()
{
FreeIfNeeded();
}
// Sets the image list to use, it is *not* deleted by the control.
virtual void SetImageList(wxImageList* imageList)
{
FreeIfNeeded();
m_imageList = imageList;
}
// As SetImageList() but we will delete the image list ourselves.
void AssignImageList(wxImageList* imageList)
{
SetImageList(imageList);
m_ownsImageList = true;
}
// Get pointer (may be NULL) to the associated image list.
wxImageList* GetImageList() const { return m_imageList; }
protected:
// Return true if we have a valid image list.
bool HasImageList() const { return m_imageList != NULL; }
// Return the image with the given index from the image list.
//
// If there is no image list or if index == -1 (which traditionally means
// that no image should be used for the given item), silently returns
// wxNullIcon.
wxIcon GetImage(int iconIndex) const
{
return m_imageList && iconIndex != -1 ? m_imageList->GetIcon(iconIndex)
: wxNullIcon;
}
private:
// Free the image list if necessary, i.e. if we own it.
void FreeIfNeeded()
{
if ( m_ownsImageList )
{
delete m_imageList;
m_imageList = NULL;
// We don't own it any more.
m_ownsImageList = false;
}
}
// The associated image list or NULL.
wxImageList* m_imageList;
// False by default, if true then we delete m_imageList.
bool m_ownsImageList;
wxDECLARE_NO_COPY_CLASS(wxWithImages);
};
#endif // _WX_WITHIMAGES_H_