Replace wxTreeItemAttr and wxListItemAttr with wxItemAttr

The two existing structs were completely identical, just replace them with a
single wxItemAttr.

Notice that wxDataViewItemAttr is not quite the same, although pretty similar,
so it remains separate for now. It would be nice to combine it with this one
too in the future, e.g. to make it simpler to make items bold in a wxListCtrl.
This commit is contained in:
Vadim Zeitlin
2016-04-16 17:44:19 +02:00
parent c592363e95
commit 246ae58c19
24 changed files with 232 additions and 221 deletions

View File

@@ -85,8 +85,8 @@ public:
void GetItem( wxListItem &info ) const;
void SetAttr(wxListItemAttr *attr) { m_attr = attr; }
wxListItemAttr *GetAttr() const { return m_attr; }
void SetAttr(wxItemAttr *attr) { m_attr = attr; }
wxItemAttr *GetAttr() const { return m_attr; }
public:
// the item image or -1
@@ -103,7 +103,7 @@ public:
wxListMainWindow *m_owner;
// custom attributes or NULL
wxListItemAttr *m_attr;
wxItemAttr *m_attr;
protected:
// common part of all ctors
@@ -263,8 +263,8 @@ public:
wxString GetText(int index) const;
void SetText( int index, const wxString& s );
wxListItemAttr *GetAttr() const;
void SetAttr(wxListItemAttr *attr);
wxItemAttr *GetAttr() const;
void SetAttr(wxItemAttr *attr);
// return true if the highlighting really changed
bool Highlight( bool on );

65
include/wx/itemattr.h Normal file
View File

@@ -0,0 +1,65 @@
///////////////////////////////////////////////////////////////////////////////
// Name: wx/itemattr.h
// Purpose: wxItemAttr class declaration
// Author: Vadim Zeitlin
// Created: 2016-04-16 (extracted from wx/listctrl.h)
// Copyright: (c) 2016 Vadim Zeitlin <vadim@wxwidgets.org>
// Licence: wxWindows licence
///////////////////////////////////////////////////////////////////////////////
#ifndef _WX_ITEMATTR_H_
#define _WX_ITEMATTR_H_
// ----------------------------------------------------------------------------
// wxItemAttr: a structure containing the visual attributes of an item
// ----------------------------------------------------------------------------
class wxItemAttr
{
public:
// ctors
wxItemAttr() { }
wxItemAttr(const wxColour& colText,
const wxColour& colBack,
const wxFont& font)
: m_colText(colText), m_colBack(colBack), m_font(font)
{
}
// default copy ctor, assignment operator and dtor are ok
// setters
void SetTextColour(const wxColour& colText) { m_colText = colText; }
void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
void SetFont(const wxFont& font) { m_font = font; }
// accessors
bool HasTextColour() const { return m_colText.IsOk(); }
bool HasBackgroundColour() const { return m_colBack.IsOk(); }
bool HasFont() const { return m_font.IsOk(); }
const wxColour& GetTextColour() const { return m_colText; }
const wxColour& GetBackgroundColour() const { return m_colBack; }
const wxFont& GetFont() const { return m_font; }
// this is almost like assignment operator except it doesn't overwrite the
// fields unset in the source attribute
void AssignFrom(const wxItemAttr& source)
{
if ( source.HasTextColour() )
SetTextColour(source.GetTextColour());
if ( source.HasBackgroundColour() )
SetBackgroundColour(source.GetBackgroundColour());
if ( source.HasFont() )
SetFont(source.GetFont());
}
private:
wxColour m_colText,
m_colBack;
wxFont m_font;
};
#endif // _WX_ITEMATTR_H_

View File

@@ -16,6 +16,7 @@
#include "wx/gdicmn.h"
#include "wx/event.h"
#include "wx/control.h"
#include "wx/itemattr.h"
#include "wx/systhemectrl.h"
class WXDLLIMPEXP_FWD_CORE wxImageList;
@@ -158,60 +159,10 @@ enum
wxLIST_FIND_RIGHT
};
// ----------------------------------------------------------------------------
// wxListItemAttr: a structure containing the visual attributes of an item
// ----------------------------------------------------------------------------
// TODO: this should be renamed to wxItemAttr or something general like this
// and used as base class for wxTextAttr which duplicates this class
// entirely currently
class WXDLLIMPEXP_CORE wxListItemAttr
{
public:
// ctors
wxListItemAttr() { }
wxListItemAttr(const wxColour& colText,
const wxColour& colBack,
const wxFont& font)
: m_colText(colText), m_colBack(colBack), m_font(font)
{
}
// default copy ctor, assignment operator and dtor are ok
// setters
void SetTextColour(const wxColour& colText) { m_colText = colText; }
void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
void SetFont(const wxFont& font) { m_font = font; }
// accessors
bool HasTextColour() const { return m_colText.IsOk(); }
bool HasBackgroundColour() const { return m_colBack.IsOk(); }
bool HasFont() const { return m_font.IsOk(); }
const wxColour& GetTextColour() const { return m_colText; }
const wxColour& GetBackgroundColour() const { return m_colBack; }
const wxFont& GetFont() const { return m_font; }
// this is almost like assignment operator except it doesn't overwrite the
// fields unset in the source attribute
void AssignFrom(const wxListItemAttr& source)
{
if ( source.HasTextColour() )
SetTextColour(source.GetTextColour());
if ( source.HasBackgroundColour() )
SetBackgroundColour(source.GetBackgroundColour());
if ( source.HasFont() )
SetFont(source.GetFont());
}
private:
wxColour m_colText,
m_colBack;
wxFont m_font;
};
// For compatibility, define the old name for this class. There is no need to
// deprecate it as it doesn't cost us anything to keep this typedef, but the
// new code should prefer to use the new wxItemAttr name.
typedef wxItemAttr wxListItemAttr;
// ----------------------------------------------------------------------------
// wxListItem: the item or column info, used to exchange data with wxListCtrl
@@ -237,7 +188,7 @@ public:
{
// copy list item attributes
if ( item.HasAttributes() )
m_attr = new wxListItemAttr(*item.GetAttributes());
m_attr = new wxItemAttr(*item.GetAttributes());
}
wxListItem& operator=(const wxListItem& item)
@@ -254,7 +205,7 @@ public:
m_data = item.m_data;
m_format = item.m_format;
m_width = item.m_width;
m_attr = item.m_attr ? new wxListItemAttr(*item.m_attr) : NULL;
m_attr = item.m_attr ? new wxItemAttr(*item.m_attr) : NULL;
}
return *this;
@@ -310,7 +261,7 @@ public:
int GetWidth() const { return m_width; }
wxListColumnFormat GetAlign() const { return (wxListColumnFormat)m_format; }
wxListItemAttr *GetAttributes() const { return m_attr; }
wxItemAttr *GetAttributes() const { return m_attr; }
bool HasAttributes() const { return m_attr != NULL; }
wxColour GetTextColour() const
@@ -342,10 +293,10 @@ public:
protected:
// creates m_attr if we don't have it yet
wxListItemAttr& Attributes()
wxItemAttr& Attributes()
{
if ( !m_attr )
m_attr = new wxListItemAttr;
m_attr = new wxItemAttr;
return *m_attr;
}
@@ -364,7 +315,7 @@ protected:
m_width = 0;
}
wxListItemAttr *m_attr; // optional pointer to the items style
wxItemAttr *m_attr; // optional pointer to the items style
private:
wxDECLARE_DYNAMIC_CLASS(wxListItem);
@@ -443,7 +394,7 @@ public:
virtual bool SetColumnWidth(int col, int width) = 0;
// return the attribute for the item (may return NULL if none)
virtual wxListItemAttr *OnGetItemAttr(long item) const;
virtual wxItemAttr *OnGetItemAttr(long item) const;
// Other miscellaneous accessors.
// ------------------------------
@@ -475,7 +426,7 @@ protected:
private:
// user defined color to draw row lines, may be invalid
wxListItemAttr m_alternateRowColour;
wxItemAttr m_alternateRowColour;
};
// ----------------------------------------------------------------------------

View File

@@ -402,7 +402,7 @@ protected:
// get the item attribute, either by quering it for virtual control, or by
// returning the one previously set using setter methods for a normal one
wxListItemAttr *DoGetItemColumnAttr(long item, long column) const;
wxItemAttr *DoGetItemColumnAttr(long item, long column) const;
wxTextCtrl* m_textCtrl; // The control used for editing a label
@@ -437,7 +437,7 @@ protected:
virtual int OnGetItemColumnImage(long item, long column) const;
// return the attribute for the given item and column (may return NULL if none)
virtual wxListItemAttr *OnGetItemColumnAttr(long item, long WXUNUSED(column)) const
virtual wxItemAttr *OnGetItemColumnAttr(long item, long WXUNUSED(column)) const
{
return OnGetItemAttr(item);
}

View File

@@ -34,7 +34,8 @@ class WXDLLIMPEXP_FWD_CORE wxDragImage;
struct WXDLLIMPEXP_FWD_CORE wxTreeViewItem;
// hash storing attributes for our items
WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxTreeItemAttr *, wxMapTreeAttr);
class wxItemAttr;
WX_DECLARE_EXPORTED_VOIDPTR_HASH_MAP(wxItemAttr *, wxMapTreeAttr);
// ----------------------------------------------------------------------------
// wxTreeCtrl

View File

@@ -279,7 +279,7 @@ public:
virtual int OnGetItemColumnImage(long item, long column) const;
// return the attribute for the given item and column (may return NULL if none)
virtual wxListItemAttr *OnGetItemColumnAttr(long item, long WXUNUSED(column)) const
virtual wxItemAttr *OnGetItemColumnAttr(long item, long WXUNUSED(column)) const
{
return OnGetItemAttr(item);
}

View File

@@ -180,40 +180,6 @@ static const int wxTREE_HITTEST_ONITEM = wxTREE_HITTEST_ONITEMICON |
// tree ctrl default name
extern WXDLLIMPEXP_DATA_CORE(const char) wxTreeCtrlNameStr[];
// ----------------------------------------------------------------------------
// wxTreeItemAttr: a structure containing the visual attributes of an item
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxTreeItemAttr
{
public:
// ctors
wxTreeItemAttr() { }
wxTreeItemAttr(const wxColour& colText,
const wxColour& colBack,
const wxFont& font)
: m_colText(colText), m_colBack(colBack), m_font(font) { }
// setters
void SetTextColour(const wxColour& colText) { m_colText = colText; }
void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
void SetFont(const wxFont& font) { m_font = font; }
// accessors
bool HasTextColour() const { return m_colText.IsOk(); }
bool HasBackgroundColour() const { return m_colBack.IsOk(); }
bool HasFont() const { return m_font.IsOk(); }
const wxColour& GetTextColour() const { return m_colText; }
const wxColour& GetBackgroundColour() const { return m_colBack; }
const wxFont& GetFont() const { return m_font; }
private:
wxColour m_colText,
m_colBack;
wxFont m_font;
};
// ----------------------------------------------------------------------------
// wxTreeEvent is a special class for all events associated with tree controls
//