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

@ -3946,6 +3946,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \
wx/valnum.h \
wx/window.h \
wx/windowid.h \
wx/withimages.h \
wx/wrapsizer.h \
wx/wupdlock.h \
wx/accel.h \

View File

@ -920,6 +920,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
wx/valnum.h
wx/window.h
wx/windowid.h
wx/withimages.h
wx/wrapsizer.h
wx/wupdlock.h

View File

@ -6960,6 +6960,10 @@ SOURCE=..\..\include\wx\windowid.h
# End Source File
# Begin Source File
SOURCE=..\..\include\wx\withimages.h
# End Source File
# Begin Source File
SOURCE=..\..\include\wx\wizard.h
# End Source File
# Begin Source File

View File

@ -5796,6 +5796,9 @@
RelativePath="..\..\include\wx\windowid.h">
</File>
<File
RelativePath="..\..\include\wx\withimages.h">
</File>
<File
RelativePath="..\..\include\wx\wizard.h">
</File>
<File

View File

@ -7748,6 +7748,10 @@
>
</File>
<File
RelativePath="..\..\include\wx\withimages.h"
>
</File>
<File
RelativePath="..\..\include\wx\wizard.h"
>
</File>

View File

@ -7744,6 +7744,10 @@
>
</File>
<File
RelativePath="..\..\include\wx\withimages.h"
>
</File>
<File
RelativePath="..\..\include\wx\wizard.h"
>
</File>

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_

View File

@ -684,7 +684,7 @@ void MyFrame::BuildDataViewCtrl(wxPanel* parent, unsigned int nPanel, unsigned l
wxImageList *ilist = new wxImageList( 16, 16 );
ilist->Add( wxIcon(wx_small_xpm) );
tc->SetImageList( ilist );
tc->AssignImageList( ilist );
wxDataViewItem parent =
tc->AppendContainer( wxDataViewItem(0), "The Root", 0 );

View File

@ -55,8 +55,6 @@ void wxBookCtrlBase::Init()
{
m_selection = wxNOT_FOUND;
m_bookctrl = NULL;
m_imageList = NULL;
m_ownsImageList = false;
m_fitToCurrentPage = false;
#if defined(__WXWINCE__)
@ -89,39 +87,6 @@ wxBookCtrlBase::Create(wxWindow *parent,
);
}
wxBookCtrlBase::~wxBookCtrlBase()
{
if ( m_ownsImageList )
{
// may be NULL, ok
delete m_imageList;
}
}
// ----------------------------------------------------------------------------
// image list
// ----------------------------------------------------------------------------
void wxBookCtrlBase::SetImageList(wxImageList *imageList)
{
if ( m_ownsImageList )
{
// may be NULL, ok
delete m_imageList;
m_ownsImageList = false;
}
m_imageList = imageList;
}
void wxBookCtrlBase::AssignImageList(wxImageList* imageList)
{
SetImageList(imageList);
m_ownsImageList = true;
}
// ----------------------------------------------------------------------------
// geometry
// ----------------------------------------------------------------------------

View File

@ -2248,11 +2248,6 @@ BEGIN_EVENT_TABLE(wxDataViewTreeCtrl,wxDataViewCtrl)
EVT_SIZE( wxDataViewTreeCtrl::OnSize )
END_EVENT_TABLE()
wxDataViewTreeCtrl::~wxDataViewTreeCtrl()
{
delete m_imageList;
}
bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
const wxPoint& pos, const wxSize& size, long style, const wxValidator& validator )
{
@ -2277,21 +2272,11 @@ bool wxDataViewTreeCtrl::Create( wxWindow *parent, wxWindowID id,
return true;
}
void wxDataViewTreeCtrl::SetImageList( wxImageList *imagelist )
{
delete m_imageList;
m_imageList = imagelist;
}
wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
const wxString &text, int iconIndex, wxClientData *data )
{
wxIcon icon = wxNullIcon;
if (m_imageList && (iconIndex != -1))
icon = m_imageList->GetIcon( iconIndex );
wxDataViewItem res = GetStore()->AppendItem( parent, text, icon, data );
wxDataViewItem res = GetStore()->
AppendItem( parent, text, GetImage(iconIndex), data );
GetStore()->ItemAdded( parent, res );
@ -2301,11 +2286,8 @@ wxDataViewItem wxDataViewTreeCtrl::AppendItem( const wxDataViewItem& parent,
wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
const wxString &text, int iconIndex, wxClientData *data )
{
wxIcon icon = wxNullIcon;
if (m_imageList && (iconIndex != -1))
icon = m_imageList->GetIcon( iconIndex );
wxDataViewItem res = GetStore()->PrependItem( parent, text, icon, data );
wxDataViewItem res = GetStore()->
PrependItem( parent, text, GetImage(iconIndex), data );
GetStore()->ItemAdded( parent, res );
@ -2315,11 +2297,8 @@ wxDataViewItem wxDataViewTreeCtrl::PrependItem( const wxDataViewItem& parent,
wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, const wxDataViewItem& previous,
const wxString &text, int iconIndex, wxClientData *data )
{
wxIcon icon = wxNullIcon;
if (m_imageList && (iconIndex != -1))
icon = m_imageList->GetIcon( iconIndex );
wxDataViewItem res = GetStore()->InsertItem( parent, previous, text, icon, data );
wxDataViewItem res = GetStore()->
InsertItem( parent, previous, text, GetImage(iconIndex), data );
GetStore()->ItemAdded( parent, res );
@ -2329,15 +2308,9 @@ wxDataViewItem wxDataViewTreeCtrl::InsertItem( const wxDataViewItem& parent, con
wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& parent,
const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
{
wxIcon icon = wxNullIcon;
if (m_imageList && (iconIndex != -1))
icon = m_imageList->GetIcon( iconIndex );
wxIcon expanded = wxNullIcon;
if (m_imageList && (expandedIndex != -1))
expanded = m_imageList->GetIcon( expandedIndex );
wxDataViewItem res = GetStore()->PrependContainer( parent, text, icon, expanded, data );
wxDataViewItem res = GetStore()->
PrependContainer( parent, text,
GetImage(iconIndex), GetImage(expandedIndex), data );
GetStore()->ItemAdded( parent, res );
@ -2347,15 +2320,9 @@ wxDataViewItem wxDataViewTreeCtrl::PrependContainer( const wxDataViewItem& paren
wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent,
const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
{
wxIcon icon = wxNullIcon;
if (m_imageList && (iconIndex != -1))
icon = m_imageList->GetIcon( iconIndex );
wxIcon expanded = wxNullIcon;
if (m_imageList && (expandedIndex != -1))
expanded = m_imageList->GetIcon( expandedIndex );
wxDataViewItem res = GetStore()->AppendContainer( parent, text, icon, expanded, data );
wxDataViewItem res = GetStore()->
AppendContainer( parent, text,
GetImage(iconIndex), GetImage(expandedIndex), data );
GetStore()->ItemAdded( parent, res );
@ -2365,15 +2332,9 @@ wxDataViewItem wxDataViewTreeCtrl::AppendContainer( const wxDataViewItem& parent
wxDataViewItem wxDataViewTreeCtrl::InsertContainer( const wxDataViewItem& parent, const wxDataViewItem& previous,
const wxString &text, int iconIndex, int expandedIndex, wxClientData *data )
{
wxIcon icon = wxNullIcon;
if (m_imageList && (iconIndex != -1))
icon = m_imageList->GetIcon( iconIndex );
wxIcon expanded = wxNullIcon;
if (m_imageList && (expandedIndex != -1))
expanded = m_imageList->GetIcon( expandedIndex );
wxDataViewItem res = GetStore()->InsertContainer( parent, previous, text, icon, expanded, data );
wxDataViewItem res = GetStore()->
InsertContainer( parent, previous, text,
GetImage(iconIndex), GetImage(expandedIndex), data );
GetStore()->ItemAdded( parent, res );
@ -2442,7 +2403,7 @@ void wxDataViewTreeCtrl::DeleteAllItems()
void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
{
if (m_imageList) return;
if (HasImageList()) return;
wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
if (!container) return;
@ -2454,7 +2415,7 @@ void wxDataViewTreeCtrl::OnExpanded( wxDataViewEvent &event )
void wxDataViewTreeCtrl::OnCollapsed( wxDataViewEvent &event )
{
if (m_imageList) return;
if (HasImageList()) return;
wxDataViewTreeStoreContainerNode* container = GetStore()->FindContainerNode( event.GetItem() );
if (!container) return;

View File

@ -277,8 +277,8 @@ bool wxNotebook::SetPageImage( size_t page, int image )
wxGtkNotebookPage* pageData = GetNotebookPage(page);
if (image >= 0)
{
wxCHECK_MSG(m_imageList, false, "invalid notebook imagelist");
const wxBitmap* bitmap = m_imageList->GetBitmapPtr(image);
wxCHECK_MSG(HasImageList(), false, "invalid notebook imagelist");
const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(image);
if (bitmap == NULL)
return false;
if (pageData->m_image)
@ -410,9 +410,9 @@ bool wxNotebook::InsertPage( size_t position,
pageData->m_image = NULL;
if (imageId != -1)
{
if (m_imageList)
if (HasImageList())
{
const wxBitmap* bitmap = m_imageList->GetBitmapPtr(imageId);
const wxBitmap* bitmap = GetImageList()->GetBitmapPtr(imageId);
pageData->m_image = gtk_image_new_from_pixbuf(bitmap->GetPixbuf());
gtk_box_pack_start(GTK_BOX(pageData->m_box),
pageData->m_image, false, false, m_padding);

View File

@ -138,8 +138,6 @@ END_EVENT_TABLE()
// common part of all ctors
void wxNotebook::Init()
{
m_imageList = NULL;
#if wxUSE_UXTHEME
m_hbrBackground = NULL;
#endif // wxUSE_UXTHEME

View File

@ -145,7 +145,7 @@ bool wxNotebook::SetPageImage(size_t nPage, int nImage)
{
wxCHECK_MSG( IS_VALID_PAGE(nPage), false,
wxT("SetPageImage: invalid notebook page") );
wxCHECK_MSG( m_imageList && nImage < m_imageList->GetImageCount(), false,
wxCHECK_MSG( HasImageList() && nImage < GetImageList()->GetImageCount(), false,
wxT("SetPageImage: invalid image index") );
if ( nImage != m_images[nPage] )

View File

@ -94,7 +94,6 @@ wxRichTextFormattingDialogFactory* wxRichTextFormattingDialog::ms_FormattingDial
void wxRichTextFormattingDialog::Init()
{
m_imageList = NULL;
m_styleDefinition = NULL;
m_styleSheet = NULL;
m_object = NULL;
@ -102,7 +101,6 @@ void wxRichTextFormattingDialog::Init()
wxRichTextFormattingDialog::~wxRichTextFormattingDialog()
{
delete m_imageList;
delete m_styleDefinition;
}