Refactor: replace wxTreeItemId and wxDataViewItem with new wxItemId<>.

Add wxItemId<> template which can be used to identify items in different
{tree,list}-like controls, including wxDataViewCtrl (where it replaces, in
backwards compatible way, wxDataViewItem), wxTreeCtrl (where it replaces
wxTreeItemId) and the upcoming wxTreeListCtrl.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@68812 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2011-08-21 14:08:56 +00:00
parent 64ee1d68b1
commit 87df1c87e2
9 changed files with 92 additions and 67 deletions

View File

@@ -3905,6 +3905,7 @@ COND_USE_GUI_1_ALL_GUI_HEADERS = \
wx/helphtml.h \ wx/helphtml.h \
wx/icon.h \ wx/icon.h \
wx/infobar.h \ wx/infobar.h \
wx/itemid.h \
wx/layout.h \ wx/layout.h \
wx/listbox.h \ wx/listbox.h \
wx/mdi.h \ wx/mdi.h \

View File

@@ -879,6 +879,7 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file!
wx/helphtml.h wx/helphtml.h
wx/icon.h wx/icon.h
wx/infobar.h wx/infobar.h
wx/itemid.h
wx/layout.h wx/layout.h
wx/listbox.h wx/listbox.h
wx/mdi.h wx/mdi.h

View File

@@ -6444,6 +6444,10 @@ SOURCE=..\..\include\wx\infobar.h
# End Source File # End Source File
# Begin Source File # Begin Source File
SOURCE=..\..\include\wx\itemid.h
# End Source File
# Begin Source File
SOURCE=..\..\include\wx\joystick.h SOURCE=..\..\include\wx\joystick.h
# End Source File # End Source File
# Begin Source File # Begin Source File

View File

@@ -5409,6 +5409,9 @@
RelativePath="..\..\include\wx\infobar.h"> RelativePath="..\..\include\wx\infobar.h">
</File> </File>
<File <File
RelativePath="..\..\include\wx\itemid.h">
</File>
<File
RelativePath="..\..\include\wx\joystick.h"> RelativePath="..\..\include\wx\joystick.h">
</File> </File>
<File <File

View File

@@ -7232,6 +7232,10 @@
> >
</File> </File>
<File <File
RelativePath="..\..\include\wx\itemid.h"
>
</File>
<File
RelativePath="..\..\include\wx\joystick.h" RelativePath="..\..\include\wx\joystick.h"
> >
</File> </File>

View File

@@ -7228,6 +7228,10 @@
> >
</File> </File>
<File <File
RelativePath="..\..\include\wx\itemid.h"
>
</File>
<File
RelativePath="..\..\include\wx\joystick.h" RelativePath="..\..\include\wx\joystick.h"
> >
</File> </File>

View File

@@ -21,6 +21,7 @@
#include "wx/variant.h" #include "wx/variant.h"
#include "wx/dynarray.h" #include "wx/dynarray.h"
#include "wx/icon.h" #include "wx/icon.h"
#include "wx/itemid.h"
#include "wx/weakref.h" #include "wx/weakref.h"
#include "wx/vector.h" #include "wx/vector.h"
#include "wx/dataobj.h" #include "wx/dataobj.h"
@@ -45,7 +46,6 @@ class WXDLLIMPEXP_FWD_CORE wxImageList;
// wxDataViewCtrl globals // wxDataViewCtrl globals
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
class WXDLLIMPEXP_FWD_ADV wxDataViewItem;
class WXDLLIMPEXP_FWD_ADV wxDataViewModel; class WXDLLIMPEXP_FWD_ADV wxDataViewModel;
class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl; class WXDLLIMPEXP_FWD_ADV wxDataViewCtrl;
class WXDLLIMPEXP_FWD_ADV wxDataViewColumn; class WXDLLIMPEXP_FWD_ADV wxDataViewColumn;
@@ -79,34 +79,14 @@ extern WXDLLIMPEXP_DATA_ADV(const char) wxDataViewCtrlNameStr[];
// wxDataViewItem // wxDataViewItem
// --------------------------------------------------------- // ---------------------------------------------------------
class WXDLLIMPEXP_ADV wxDataViewItem // Make it a class and not a typedef to allow forward declaring it.
class wxDataViewItem : public wxItemId<void*>
{ {
public: public:
wxDataViewItem() : m_id(NULL) {} wxDataViewItem() : wxItemId<void*>() { }
wxDataViewItem(const wxDataViewItem &item) : m_id(item.m_id) {} wxEXPLICIT wxDataViewItem(void* pItem) : wxItemId<void*>(pItem) { }
wxEXPLICIT wxDataViewItem(void* id) : m_id(id) {}
bool IsOk() const { return m_id != NULL; }
void* GetID() const { return m_id; }
operator const void* () const { return m_id; }
private:
void* m_id;
}; };
inline
bool operator==(const wxDataViewItem& left, const wxDataViewItem& right)
{
return left.GetID() == right.GetID();
}
inline
bool operator!=(const wxDataViewItem& left, const wxDataViewItem& right)
{
return !(left == right);
}
WX_DEFINE_ARRAY(wxDataViewItem, wxDataViewItemArray); WX_DEFINE_ARRAY(wxDataViewItem, wxDataViewItemArray);
// --------------------------------------------------------- // ---------------------------------------------------------

59
include/wx/itemid.h Normal file
View File

@@ -0,0 +1,59 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/itemid.h
// Purpose: wxItemId class declaration.
// 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_ITEMID_H_
#define _WX_ITEMID_H_
// ----------------------------------------------------------------------------
// wxItemId: an opaque item identifier used with wx{Tree,TreeList,DataView}Ctrl.
// ----------------------------------------------------------------------------
// The template argument T is typically a pointer to some opaque type. While
// wxTreeItemId and wxDataViewItem use a pointer to void, this is dangerous and
// not recommended for the new item id classes.
template <typename T>
class wxItemId
{
public:
typedef T Type;
// This ctor is implicit which is fine for non-void* types, but if you use
// this class with void* you're strongly advised to make the derived class
// ctor explicit as implicitly converting from any pointer is simply too
// dangerous.
wxItemId(Type item = NULL) : m_pItem(item) { }
// Default copy ctor, assignment operator and dtor are ok.
bool IsOk() const { return m_pItem != NULL; }
Type GetID() const { return m_pItem; }
operator const Type() const { return m_pItem; }
void Unset() { m_pItem = NULL; }
// This field is public *only* for compatibility with the old wxTreeItemId
// implementation and must not be used in any new code.
//private:
Type m_pItem;
};
template <typename T>
bool operator==(const wxItemId<T>& left, const wxItemId<T>& right)
{
return left.GetID() == right.GetID();
}
template <typename T>
bool operator!=(const wxItemId<T>& left, const wxItemId<T>& right)
{
return !(left == right);
}
#endif // _WX_ITEMID_H_

View File

@@ -23,6 +23,7 @@
#include "wx/window.h" // for wxClientData #include "wx/window.h" // for wxClientData
#include "wx/event.h" #include "wx/event.h"
#include "wx/dynarray.h" #include "wx/dynarray.h"
#include "wx/itemid.h"
#if WXWIN_COMPATIBILITY_2_6 #if WXWIN_COMPATIBILITY_2_6
@@ -38,53 +39,19 @@ enum
#endif // WXWIN_COMPATIBILITY_2_6 #endif // WXWIN_COMPATIBILITY_2_6
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxTreeItemId identifies an element of the tree. In this implementation, it's // wxTreeItemId identifies an element of the tree. It's opaque for the
// just a trivial wrapper around Win32 HTREEITEM or a pointer to some private // application and the only method which can be used by user code is IsOk().
// data structure in the generic version. It's opaque for the application and
// the only method which can be used by user code is IsOk().
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Using this typedef removes an ambiguity when calling Remove() // This is a class and not a typedef because existing code may forward declare
typedef void *wxTreeItemIdValue; // wxTreeItemId as a class and we don't want to break it without good reason.
class wxTreeItemId : public wxItemId<void*>
class WXDLLIMPEXP_CORE wxTreeItemId
{ {
friend bool operator==(const wxTreeItemId&, const wxTreeItemId&);
public: public:
// ctors wxTreeItemId() : wxItemId<void*>() { }
// 0 is invalid value for HTREEITEM wxTreeItemId(void* pItem) : wxItemId<void*>(pItem) { }
wxTreeItemId() { m_pItem = 0; }
// construct wxTreeItemId from the native item id
wxTreeItemId(void *pItem) { m_pItem = pItem; }
// default copy ctor/assignment operator are ok for us
// accessors
// is this a valid tree item?
bool IsOk() const { return m_pItem != 0; }
// return true if this item is not valid
bool operator!() const { return !IsOk(); }
// operations
// invalidate the item
void Unset() { m_pItem = 0; }
operator bool() const { return IsOk(); }
wxTreeItemIdValue m_pItem;
}; };
inline bool operator==(const wxTreeItemId& i1, const wxTreeItemId& i2)
{
return i1.m_pItem == i2.m_pItem;
}
inline bool operator!=(const wxTreeItemId& i1, const wxTreeItemId& i2)
{
return i1.m_pItem != i2.m_pItem;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// wxTreeItemData is some (arbitrary) user class associated with some item. The // wxTreeItemData is some (arbitrary) user class associated with some item. The
// main advantage of having this class (compared to old untyped interface) is // main advantage of having this class (compared to old untyped interface) is
@@ -119,10 +86,12 @@ protected:
wxTreeItemId m_pItem; wxTreeItemId m_pItem;
}; };
typedef void *wxTreeItemIdValue;
WX_DEFINE_EXPORTED_ARRAY_PTR(wxTreeItemIdValue, wxArrayTreeItemIdsBase); WX_DEFINE_EXPORTED_ARRAY_PTR(wxTreeItemIdValue, wxArrayTreeItemIdsBase);
// this is a wrapper around the array class defined above which allow to wok // this is a wrapper around the array class defined above which allow to wok
// with vaue of natural wxTreeItemId type instead of using wxTreeItemIdValue // with values of natural wxTreeItemId type instead of using wxTreeItemIdValue
// and does it without any loss of efficiency // and does it without any loss of efficiency
class WXDLLIMPEXP_CORE wxArrayTreeItemIds : public wxArrayTreeItemIdsBase class WXDLLIMPEXP_CORE wxArrayTreeItemIds : public wxArrayTreeItemIdsBase
{ {