From abfdefede3209ac777bdac8a65bae8bf008f327b Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 21 Aug 2011 14:08:43 +0000 Subject: [PATCH] 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 --- Makefile.in | 1 + build/bakefiles/files.bkl | 1 + build/msw/wx_core.dsp | 4 ++ build/msw/wx_vc7_core.vcproj | 3 + build/msw/wx_vc8_core.vcproj | 4 ++ build/msw/wx_vc9_core.vcproj | 4 ++ include/wx/bookctrl.h | 22 +----- include/wx/dataview.h | 21 ++---- include/wx/richtext/richtextformatdlg.h | 10 +-- include/wx/withimages.h | 91 +++++++++++++++++++++++++ samples/dataview/dataview.cpp | 2 +- src/common/bookctrl.cpp | 35 ---------- src/common/datavcmn.cpp | 73 +++++--------------- src/gtk/notebook.cpp | 8 +-- src/msw/notebook.cpp | 2 - src/osx/notebook_osx.cpp | 2 +- src/richtext/richtextformatdlg.cpp | 2 - 17 files changed, 141 insertions(+), 144 deletions(-) create mode 100644 include/wx/withimages.h diff --git a/Makefile.in b/Makefile.in index e156c7e867..5484f93194 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 \ diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index 02dbf51f09..b4434de656 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -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 diff --git a/build/msw/wx_core.dsp b/build/msw/wx_core.dsp index 5b6ecf620f..b29e9c7732 100644 --- a/build/msw/wx_core.dsp +++ b/build/msw/wx_core.dsp @@ -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 diff --git a/build/msw/wx_vc7_core.vcproj b/build/msw/wx_vc7_core.vcproj index 94a0489a2c..30aa6ce196 100644 --- a/build/msw/wx_vc7_core.vcproj +++ b/build/msw/wx_vc7_core.vcproj @@ -5796,6 +5796,9 @@ RelativePath="..\..\include\wx\windowid.h"> + + + + diff --git a/build/msw/wx_vc9_core.vcproj b/build/msw/wx_vc9_core.vcproj index 7dbebad098..06b39192c0 100644 --- a/build/msw/wx_vc9_core.vcproj +++ b/build/msw/wx_vc9_core.vcproj @@ -7744,6 +7744,10 @@ > + + diff --git a/include/wx/bookctrl.h b/include/wx/bookctrl.h index 86a89acfd1..bc2b89d8b3 100644 --- a/include/wx/bookctrl.h +++ b/include/wx/bookctrl.h @@ -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; diff --git a/include/wx/dataview.h b/include/wx/dataview.h index 87bfcf12e2..ff6e3b31e3 100644 --- a/include/wx/dataview.h +++ b/include/wx/dataview.h @@ -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) diff --git a/include/wx/richtext/richtextformatdlg.h b/include/wx/richtext/richtextformatdlg.h index 9b0898e491..53abf8ac7d 100644 --- a/include/wx/richtext/richtextformatdlg.h +++ b/include/wx/richtext/richtextformatdlg.h @@ -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; diff --git a/include/wx/withimages.h b/include/wx/withimages.h new file mode 100644 index 0000000000..6ca0c902e9 --- /dev/null +++ b/include/wx/withimages.h @@ -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 +// 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_ diff --git a/samples/dataview/dataview.cpp b/samples/dataview/dataview.cpp index f3c8bb2074..5fb2a2683d 100644 --- a/samples/dataview/dataview.cpp +++ b/samples/dataview/dataview.cpp @@ -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 ); diff --git a/src/common/bookctrl.cpp b/src/common/bookctrl.cpp index 5392388681..cc2141b793 100644 --- a/src/common/bookctrl.cpp +++ b/src/common/bookctrl.cpp @@ -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 // ---------------------------------------------------------------------------- diff --git a/src/common/datavcmn.cpp b/src/common/datavcmn.cpp index c5540283fc..7c2f06f457 100644 --- a/src/common/datavcmn.cpp +++ b/src/common/datavcmn.cpp @@ -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; diff --git a/src/gtk/notebook.cpp b/src/gtk/notebook.cpp index f418384a36..9cf75ee56b 100644 --- a/src/gtk/notebook.cpp +++ b/src/gtk/notebook.cpp @@ -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); diff --git a/src/msw/notebook.cpp b/src/msw/notebook.cpp index 1879bb2ba3..e69fd60c43 100644 --- a/src/msw/notebook.cpp +++ b/src/msw/notebook.cpp @@ -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 diff --git a/src/osx/notebook_osx.cpp b/src/osx/notebook_osx.cpp index 0f0ea5f37b..68ff2095b5 100644 --- a/src/osx/notebook_osx.cpp +++ b/src/osx/notebook_osx.cpp @@ -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] ) diff --git a/src/richtext/richtextformatdlg.cpp b/src/richtext/richtextformatdlg.cpp index 768ef405b0..032268117c 100644 --- a/src/richtext/richtextformatdlg.cpp +++ b/src/richtext/richtextformatdlg.cpp @@ -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; } -- 2.47.2