Merge branch 'shared-client-data'

Allow sharing client data in wxGrid-related classes.

See #22369.
This commit is contained in:
Vadim Zeitlin
2022-05-01 02:55:36 +02:00
11 changed files with 402 additions and 160 deletions

View File

@@ -14,6 +14,7 @@
#include "wx/defs.h"
#include "wx/string.h"
#include "wx/hashmap.h"
#include "wx/object.h"
typedef int (*wxShadowObjectMethod)(void*, void*);
WX_DECLARE_STRING_HASH_MAP_WITH_DECL(
@@ -158,5 +159,47 @@ protected:
};
// This class is a replacement for wxClientDataContainer, and unlike
// wxClientDataContainer the wxSharedClientDataContainer client data is
// copiable, so it can be copied when objects containing it are cloned.
// Like wxClientDataContainer, wxSharedClientDataContainer is a mixin
// that provides storage and management of "client data.". The client data
// is reference counted and managed by the container.
//
// NOTE: If your class has a clone function and needs to store client data,
// use wxSharedClientDataContainer and not wxClientDataContainer!
class WXDLLIMPEXP_BASE wxSharedClientDataContainer
{
public:
// Provide the same functions as in wxClientDataContainer, so that objects
// using it and this class could be used in exactly the same way.
void SetClientObject(wxClientData *data);
wxClientData *GetClientObject() const;
void SetClientData(void *data);
void *GetClientData() const;
protected:
bool HasClientDataContainer() const { return m_data.get() != NULL; }
void CopyClientDataContainer(const wxSharedClientDataContainer& other)
{
m_data = other.m_data;
}
private:
class wxRefCountedClientDataContainer : public wxClientDataContainer,
public wxRefCounter
{
};
// Helper function that will create m_data if it is currently NULL
wxClientDataContainer *GetValidClientData();
// m_data is shared, not deep copied, when cloned. If you make changes to
// the data in one instance of your class, you change it for all cloned
// instances!
wxObjectDataPtr<wxRefCountedClientDataContainer> m_data;
};
#endif // _WX_CLNTDATAH__

View File

@@ -138,11 +138,14 @@ class wxGridDirectionOperations;
// class is not documented and is not public at all
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxGridCellWorker : public wxClientDataContainer, public wxRefCounter
class WXDLLIMPEXP_CORE wxGridCellWorker : public wxSharedClientDataContainer,
public wxRefCounter
{
public:
wxGridCellWorker() { }
wxGridCellWorker(const wxGridCellWorker& other);
// interpret renderer parameters: arbitrary string whose interpretation is
// left to the derived classes
virtual void SetParameters(const wxString& params);
@@ -169,6 +172,16 @@ private:
class WXDLLIMPEXP_CORE wxGridCellRenderer : public wxGridCellWorker
{
public:
wxGridCellRenderer()
: wxGridCellWorker()
{
}
wxGridCellRenderer(const wxGridCellRenderer& other)
: wxGridCellWorker(other)
{
}
// draw the given cell on the provided DC inside the given rectangle
// using the style specified by the attribute and the default or selected
// state corresponding to the isSelected value.
@@ -376,7 +389,14 @@ private:
class WXDLLIMPEXP_CORE wxGridCellEditor : public wxGridCellWorker
{
public:
wxGridCellEditor();
wxGridCellEditor()
: wxGridCellWorker(),
m_control(NULL),
m_attr(NULL)
{
}
wxGridCellEditor(const wxGridCellEditor& other);
bool IsCreated() const { return m_control != NULL; }
@@ -524,8 +544,6 @@ protected:
// suppress the stupid gcc warning about the class having private dtor and
// no friends
friend class wxGridCellEditorDummyFriend;
wxDECLARE_NO_COPY_CLASS(wxGridCellEditor);
};
// Smart pointer to wxGridCellEditor, calling DecRef() on it automatically.
@@ -535,6 +553,16 @@ typedef wxObjectDataPtr<wxGridCellEditor> wxGridCellEditorPtr;
class wxGridCellActivatableEditor : public wxGridCellEditor
{
public:
wxGridCellActivatableEditor()
: wxGridCellEditor()
{
}
wxGridCellActivatableEditor(const wxGridCellActivatableEditor& other)
: wxGridCellEditor(other)
{
}
// In this class these methods must be overridden.
virtual wxGridActivationResult
TryActivate(int row, int col, wxGrid* grid,
@@ -705,7 +733,8 @@ private:
// class may be returned by wxGridTable::GetAttr().
// ----------------------------------------------------------------------------
class WXDLLIMPEXP_CORE wxGridCellAttr : public wxClientDataContainer, public wxRefCounter
class WXDLLIMPEXP_CORE wxGridCellAttr : public wxSharedClientDataContainer,
public wxRefCounter
{
public:
enum wxAttrKind

View File

@@ -23,6 +23,16 @@
class WXDLLIMPEXP_ADV wxGridCellStringRenderer : public wxGridCellRenderer
{
public:
wxGridCellStringRenderer()
: wxGridCellRenderer()
{
}
wxGridCellStringRenderer(const wxGridCellStringRenderer& other)
: wxGridCellRenderer(other)
{
}
// draw the string
virtual void Draw(wxGrid& grid,
wxGridCellAttr& attr,
@@ -38,7 +48,7 @@ public:
int row, int col) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellStringRenderer; }
{ return new wxGridCellStringRenderer(*this); }
protected:
// calc the string extent for given string/font
@@ -53,11 +63,19 @@ class WXDLLIMPEXP_ADV wxGridCellNumberRenderer : public wxGridCellStringRenderer
public:
explicit wxGridCellNumberRenderer(long minValue = LONG_MIN,
long maxValue = LONG_MAX)
: m_minValue(minValue),
: wxGridCellStringRenderer(),
m_minValue(minValue),
m_maxValue(maxValue)
{
}
wxGridCellNumberRenderer(const wxGridCellNumberRenderer& other)
: wxGridCellStringRenderer(other),
m_minValue(other.m_minValue),
m_maxValue(other.m_maxValue)
{
}
// draw the string right aligned
virtual void Draw(wxGrid& grid,
wxGridCellAttr& attr,
@@ -79,7 +97,7 @@ public:
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellNumberRenderer(m_minValue, m_maxValue); }
{ return new wxGridCellNumberRenderer(*this); }
protected:
wxString GetString(const wxGrid& grid, int row, int col);
@@ -95,6 +113,15 @@ public:
int precision = -1,
int format = wxGRID_FLOAT_FORMAT_DEFAULT);
wxGridCellFloatRenderer(const wxGridCellFloatRenderer& other)
: wxGridCellStringRenderer(other),
m_width(other.m_width),
m_precision(other.m_precision),
m_style(other.m_style),
m_format(other.m_format)
{
}
// get/change formatting parameters
int GetWidth() const { return m_width; }
void SetWidth(int width) { m_width = width; m_format.clear(); }
@@ -120,7 +147,8 @@ public:
// with format being one of f|e|g|E|F|G
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellFloatRenderer(*this); }
protected:
wxString GetString(const wxGrid& grid, int row, int col);
@@ -138,6 +166,16 @@ private:
class WXDLLIMPEXP_ADV wxGridCellBoolRenderer : public wxGridCellRenderer
{
public:
wxGridCellBoolRenderer()
: wxGridCellRenderer()
{
}
wxGridCellBoolRenderer(const wxGridCellBoolRenderer& other)
: wxGridCellRenderer(other)
{
}
// draw a check mark or nothing
virtual void Draw(wxGrid& grid,
wxGridCellAttr& attr,
@@ -157,7 +195,7 @@ public:
wxDC& dc) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellBoolRenderer; }
{ return new wxGridCellBoolRenderer(*this); }
};
@@ -174,7 +212,8 @@ public:
explicit wxGridCellDateRenderer(const wxString& outformat = wxString());
wxGridCellDateRenderer(const wxGridCellDateRenderer& other)
: m_oformat(other.m_oformat),
: wxGridCellStringRenderer(other),
m_oformat(other.m_oformat),
m_tz(other.m_tz)
{
}
@@ -196,7 +235,8 @@ public:
wxGridCellAttr& attr,
wxDC& dc) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellDateRenderer(*this); }
// output strptime()-like format string
virtual void SetParameters(const wxString& params) wxOVERRIDE;
@@ -226,7 +266,8 @@ public:
{
}
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellDateTimeRenderer(*this); }
protected:
virtual void
@@ -242,7 +283,9 @@ protected:
class WXDLLIMPEXP_ADV wxGridCellChoiceRenderer : public wxGridCellStringRenderer
{
public:
wxGridCellChoiceRenderer() { }
explicit wxGridCellChoiceRenderer(const wxString& choices = wxString());
wxGridCellChoiceRenderer(const wxGridCellChoiceRenderer& other);
virtual wxSize GetMaxBestSize(wxGrid& grid,
wxGridCellAttr& attr,
@@ -257,10 +300,6 @@ public:
}
protected:
wxGridCellChoiceRenderer(const wxGridCellChoiceRenderer& other)
: m_choices(other.m_choices)
{
}
wxArrayString m_choices;
};
@@ -270,7 +309,15 @@ protected:
class WXDLLIMPEXP_ADV wxGridCellEnumRenderer : public wxGridCellChoiceRenderer
{
public:
wxGridCellEnumRenderer( const wxString& choices = wxEmptyString );
explicit wxGridCellEnumRenderer(const wxString& choices = wxString())
: wxGridCellChoiceRenderer(choices)
{
}
wxGridCellEnumRenderer(const wxGridCellEnumRenderer& other)
: wxGridCellChoiceRenderer(other)
{
}
// draw the string right aligned
virtual void Draw(wxGrid& grid,
@@ -285,7 +332,8 @@ public:
wxDC& dc,
int row, int col) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellEnumRenderer(*this); }
protected:
wxString GetString(const wxGrid& grid, int row, int col);
@@ -295,7 +343,15 @@ protected:
class WXDLLIMPEXP_ADV wxGridCellAutoWrapStringRenderer : public wxGridCellStringRenderer
{
public:
wxGridCellAutoWrapStringRenderer() : wxGridCellStringRenderer() { }
wxGridCellAutoWrapStringRenderer()
: wxGridCellStringRenderer()
{
}
wxGridCellAutoWrapStringRenderer(const wxGridCellAutoWrapStringRenderer& other)
: wxGridCellStringRenderer(other)
{
}
virtual void Draw(wxGrid& grid,
wxGridCellAttr& attr,
@@ -322,7 +378,7 @@ public:
int height) wxOVERRIDE;
virtual wxGridCellRenderer *Clone() const wxOVERRIDE
{ return new wxGridCellAutoWrapStringRenderer; }
{ return new wxGridCellAutoWrapStringRenderer(*this); }
private:
wxArrayString GetTextLines( wxGrid& grid,

View File

@@ -55,7 +55,13 @@ private:
class WXDLLIMPEXP_ADV wxGridCellTextEditor : public wxGridCellEditor
{
public:
explicit wxGridCellTextEditor(size_t maxChars = 0);
explicit wxGridCellTextEditor(size_t maxChars = 0)
: wxGridCellEditor(),
m_maxChars(maxChars)
{
}
wxGridCellTextEditor(const wxGridCellTextEditor& other);
virtual void Create(wxWindow* parent,
wxWindowID id,
@@ -78,7 +84,8 @@ public:
virtual void SetValidator(const wxValidator& validator);
#endif
virtual wxGridCellEditor *Clone() const wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellTextEditor(*this); }
// added GetValue so we can get the value which is in the control
virtual wxString GetValue() const wxOVERRIDE;
@@ -98,8 +105,6 @@ private:
wxScopedPtr<wxValidator> m_validator;
#endif
wxString m_value;
wxDECLARE_NO_COPY_CLASS(wxGridCellTextEditor);
};
// the editor for numeric (long) data
@@ -108,7 +113,20 @@ class WXDLLIMPEXP_ADV wxGridCellNumberEditor : public wxGridCellTextEditor
public:
// allows to specify the range - if min == max == -1, no range checking is
// done
wxGridCellNumberEditor(int min = -1, int max = -1);
explicit wxGridCellNumberEditor(int min = -1, int max = -1)
: wxGridCellTextEditor(),
m_min(min),
m_max(max)
{
}
wxGridCellNumberEditor(const wxGridCellNumberEditor& other)
: wxGridCellTextEditor(other),
m_min(other.m_min),
m_max(other.m_max),
m_value(other.m_value)
{
}
virtual void Create(wxWindow* parent,
wxWindowID id,
@@ -129,7 +147,7 @@ public:
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellNumberEditor(m_min, m_max); }
{ return new wxGridCellNumberEditor(*this); }
// added GetValue so we can get the value which is in the control
virtual wxString GetValue() const wxOVERRIDE;
@@ -158,8 +176,6 @@ private:
m_max;
long m_value;
wxDECLARE_NO_COPY_CLASS(wxGridCellNumberEditor);
};
@@ -191,9 +207,25 @@ enum wxGridCellFloatFormat
class WXDLLIMPEXP_ADV wxGridCellFloatEditor : public wxGridCellTextEditor
{
public:
wxGridCellFloatEditor(int width = -1,
int precision = -1,
int format = wxGRID_FLOAT_FORMAT_DEFAULT);
explicit wxGridCellFloatEditor(int width = -1,
int precision = -1,
int format = wxGRID_FLOAT_FORMAT_DEFAULT)
: wxGridCellTextEditor(),
m_width(width),
m_precision(precision),
m_style(format)
{
}
wxGridCellFloatEditor(const wxGridCellFloatEditor& other)
: wxGridCellTextEditor(other),
m_width(other.m_width),
m_precision(other.m_precision),
m_value(other.m_value),
m_style(other.m_style),
m_format(other.m_format)
{
}
virtual void Create(wxWindow* parent,
wxWindowID id,
@@ -209,7 +241,7 @@ public:
virtual void StartingKey(wxKeyEvent& event) wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellFloatEditor(m_width, m_precision); }
{ return new wxGridCellFloatEditor(*this); }
// parameters string format is "width[,precision[,format]]"
// format to choose between f|e|g|E|G (f is used by default)
@@ -226,8 +258,6 @@ private:
int m_style;
wxString m_format;
wxDECLARE_NO_COPY_CLASS(wxGridCellFloatEditor);
};
#endif // wxUSE_TEXTCTRL
@@ -238,7 +268,16 @@ private:
class WXDLLIMPEXP_ADV wxGridCellBoolEditor : public wxGridCellEditor
{
public:
wxGridCellBoolEditor() { }
wxGridCellBoolEditor()
: wxGridCellEditor()
{
}
wxGridCellBoolEditor(const wxGridCellBoolEditor& other)
: wxGridCellEditor(other),
m_value(other.m_value)
{
}
virtual wxGridActivationResult
TryActivate(int row, int col, wxGrid* grid,
@@ -263,7 +302,7 @@ public:
virtual void StartingKey(wxKeyEvent& event) wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellBoolEditor; }
{ return new wxGridCellBoolEditor(*this); }
// added GetValue so we can get the value which is in the control, see
// also UseStringValues()
@@ -272,7 +311,7 @@ public:
// set the string values returned by GetValue() for the true and false
// states, respectively
static void UseStringValues(const wxString& valueTrue = wxT("1"),
const wxString& valueFalse = wxEmptyString);
const wxString& valueFalse = wxString());
// return true if the given string is equal to the string representation of
// true value which we currently use
@@ -294,8 +333,6 @@ private:
bool m_value;
static wxString ms_stringValues[2];
wxDECLARE_NO_COPY_CLASS(wxGridCellBoolEditor);
};
#endif // wxUSE_CHECKBOX
@@ -307,11 +344,24 @@ class WXDLLIMPEXP_ADV wxGridCellChoiceEditor : public wxGridCellEditor
{
public:
// if !allowOthers, user can't type a string not in choices array
wxGridCellChoiceEditor(size_t count = 0,
const wxString choices[] = NULL,
bool allowOthers = false);
wxGridCellChoiceEditor(const wxArrayString& choices,
bool allowOthers = false);
explicit wxGridCellChoiceEditor(size_t count = 0,
const wxString choices[] = NULL,
bool allowOthers = false);
explicit wxGridCellChoiceEditor(const wxArrayString& choices,
bool allowOthers = false)
: wxGridCellEditor(),
m_choices(choices),
m_allowOthers(allowOthers)
{
}
wxGridCellChoiceEditor(const wxGridCellChoiceEditor& other)
: wxGridCellEditor(other),
m_value(other.m_value),
m_choices(other.m_choices),
m_allowOthers(other.m_allowOthers)
{
}
virtual void Create(wxWindow* parent,
wxWindowID id,
@@ -329,7 +379,8 @@ public:
// parameters string format is "item1[,item2[...,itemN]]"
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellChoiceEditor(*this); }
// added GetValue so we can get the value which is in the control
virtual wxString GetValue() const wxOVERRIDE;
@@ -342,8 +393,6 @@ protected:
wxString m_value;
wxArrayString m_choices;
bool m_allowOthers;
wxDECLARE_NO_COPY_CLASS(wxGridCellChoiceEditor);
};
#endif // wxUSE_COMBOBOX
@@ -353,10 +402,18 @@ protected:
class WXDLLIMPEXP_ADV wxGridCellEnumEditor : public wxGridCellChoiceEditor
{
public:
wxGridCellEnumEditor( const wxString& choices = wxEmptyString );
explicit wxGridCellEnumEditor(const wxString& choices = wxString());
wxGridCellEnumEditor(const wxGridCellEnumEditor& other)
: wxGridCellChoiceEditor(other),
m_index(other.m_index)
{
}
virtual ~wxGridCellEnumEditor() {}
virtual wxGridCellEditor* Clone() const wxOVERRIDE;
virtual wxGridCellEditor* Clone() const wxOVERRIDE
{ return new wxGridCellEnumEditor(*this); }
virtual void BeginEdit(int row, int col, wxGrid* grid) wxOVERRIDE;
virtual bool EndEdit(int row, int col, const wxGrid* grid,
@@ -365,8 +422,6 @@ public:
private:
long m_index;
wxDECLARE_NO_COPY_CLASS(wxGridCellEnumEditor);
};
#endif // wxUSE_COMBOBOX
@@ -374,15 +429,22 @@ private:
class WXDLLIMPEXP_ADV wxGridCellAutoWrapStringEditor : public wxGridCellTextEditor
{
public:
wxGridCellAutoWrapStringEditor() : wxGridCellTextEditor() { }
wxGridCellAutoWrapStringEditor()
: wxGridCellTextEditor()
{
}
wxGridCellAutoWrapStringEditor(const wxGridCellAutoWrapStringEditor& other)
: wxGridCellTextEditor(other)
{
}
virtual void Create(wxWindow* parent,
wxWindowID id,
wxEvtHandler* evtHandler) wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellAutoWrapStringEditor; }
wxDECLARE_NO_COPY_CLASS(wxGridCellAutoWrapStringEditor);
{ return new wxGridCellAutoWrapStringEditor(*this); }
};
#if wxUSE_DATEPICKCTRL
@@ -392,6 +454,13 @@ class WXDLLIMPEXP_ADV wxGridCellDateEditor : public wxGridCellEditor
public:
explicit wxGridCellDateEditor(const wxString& format = wxString());
wxGridCellDateEditor(const wxGridCellDateEditor& other)
: wxGridCellEditor(other),
m_value(other.m_value),
m_format(other.m_format)
{
}
virtual void SetParameters(const wxString& params) wxOVERRIDE;
virtual void Create(wxWindow* parent,
@@ -407,7 +476,8 @@ public:
virtual void Reset() wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE;
virtual wxGridCellEditor *Clone() const wxOVERRIDE
{ return new wxGridCellDateEditor(*this); }
virtual wxString GetValue() const wxOVERRIDE;
@@ -417,8 +487,6 @@ protected:
private:
wxDateTime m_value;
wxString m_format;
wxDECLARE_NO_COPY_CLASS(wxGridCellDateEditor);
};
#endif // wxUSE_DATEPICKCTRL