Skip to content

Commit

Permalink
Refactor wxDataViewEvent constructors
Browse files Browse the repository at this point in the history
Take care of all the common stuff such as setting the event object and the
model, which is used for all events, in the ctor. Also set both the column
pointer and the column index at once instead of having two separate setters
for them which could result in inconsistent event objects (and did, as
sometimes only one or only the other field was set).

This makes the code shorter (we save 160 lines) and more clear and ensures
that everything is always initialized.

Closes #12649.
  • Loading branch information
vadz committed Mar 18, 2016
1 parent e2e7d3d commit 9829446
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 325 deletions.
70 changes: 45 additions & 25 deletions include/wx/dataview.h
Expand Up @@ -769,25 +769,32 @@ class WXDLLIMPEXP_ADV wxDataViewCtrlBase: public wxSystemThemedControl<wxControl
class WXDLLIMPEXP_ADV wxDataViewEvent : public wxNotifyEvent
{
public:
wxDataViewEvent(wxEventType commandType = wxEVT_NULL, int winid = 0)
: wxNotifyEvent(commandType, winid),
m_item(0),
m_col(-1),
m_model(NULL),
m_value(wxNullVariant),
m_column(NULL),
m_pos(-1,-1),
m_cacheFrom(0),
m_cacheTo(0),
m_editCancelled(false)
#if wxUSE_DRAG_AND_DROP
, m_dataObject(NULL),
m_dataBuffer(NULL),
m_dataSize(0),
m_dragFlags(0),
m_dropEffect(wxDragNone)
#endif
{ }
// Default ctor, normally shouldn't be used and mostly exists only for
// backwards compatibility.
wxDataViewEvent()
: wxNotifyEvent()
{
Init(NULL, NULL, wxDataViewItem());
}

// Constructor for the events affecting columns (and possibly also items).
wxDataViewEvent(wxEventType evtType,
wxDataViewCtrlBase* dvc,
wxDataViewColumn* column,
const wxDataViewItem& item = wxDataViewItem())
: wxNotifyEvent(evtType, dvc->GetId())
{
Init(dvc, column, item);
}

// Constructor for the events affecting only the items.
wxDataViewEvent(wxEventType evtType,
wxDataViewCtrlBase* dvc,
const wxDataViewItem& item)
: wxNotifyEvent(evtType, dvc->GetId())
{
Init(dvc, NULL, item);
}

wxDataViewEvent(const wxDataViewEvent& event)
: wxNotifyEvent(event),
Expand All @@ -811,13 +818,8 @@ class WXDLLIMPEXP_ADV wxDataViewEvent : public wxNotifyEvent
{ }

wxDataViewItem GetItem() const { return m_item; }
void SetItem( const wxDataViewItem &item ) { m_item = item; }

int GetColumn() const { return m_col; }
void SetColumn( int col ) { m_col = col; }

wxDataViewModel* GetModel() const { return m_model; }
void SetModel( wxDataViewModel *model ) { m_model = model; }

const wxVariant &GetValue() const { return m_value; }
void SetValue( const wxVariant &value ) { m_value = value; }
Expand All @@ -827,7 +829,6 @@ class WXDLLIMPEXP_ADV wxDataViewEvent : public wxNotifyEvent
void SetEditCanceled(bool editCancelled) { m_editCancelled = editCancelled; }

// for wxEVT_DATAVIEW_COLUMN_HEADER_CLICKED only
void SetDataViewColumn( wxDataViewColumn *col ) { m_column = col; }
wxDataViewColumn *GetDataViewColumn() const { return m_column; }

// for wxEVT_DATAVIEW_CONTEXT_MENU only
Expand Down Expand Up @@ -860,6 +861,20 @@ class WXDLLIMPEXP_ADV wxDataViewEvent : public wxNotifyEvent

virtual wxEvent *Clone() const wxOVERRIDE { return new wxDataViewEvent(*this); }

// These methods shouldn't be used outside of wxWidgets and wxWidgets
// itself doesn't use them any longer neither as it constructs the events
// with the appropriate ctors directly.
#if WXWIN_COMPATIBILITY_3_0
wxDEPRECATED_MSG("Pass the argument to the ctor instead")
void SetModel( wxDataViewModel *model ) { m_model = model; }
wxDEPRECATED_MSG("Pass the argument to the ctor instead")
void SetDataViewColumn( wxDataViewColumn *col ) { m_column = col; }
wxDEPRECATED_MSG("Pass the argument to the ctor instead")
void SetColumn( int col ) { m_col = col; }
wxDEPRECATED_MSG("Pass the argument to the ctor instead")
void SetItem( const wxDataViewItem &item ) { m_item = item; }
#endif // WXWIN_COMPATIBILITY_3_0

protected:
wxDataViewItem m_item;
int m_col;
Expand All @@ -883,6 +898,11 @@ class WXDLLIMPEXP_ADV wxDataViewEvent : public wxNotifyEvent
#endif // wxUSE_DRAG_AND_DROP

private:
// Common part of non-copy ctors.
void Init(wxDataViewCtrlBase* dvc,
wxDataViewColumn* column,
const wxDataViewItem& item);

wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxDataViewEvent);
};

Expand Down
52 changes: 32 additions & 20 deletions src/common/datavcmn.cpp
Expand Up @@ -669,16 +669,13 @@ wxDataViewCtrl* wxDataViewRendererBase::GetView() const

bool wxDataViewRendererBase::StartEditing( const wxDataViewItem &item, wxRect labelRect )
{
wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
wxDataViewColumn* const column = GetOwner();
wxDataViewCtrl* const dv_ctrl = column->GetOwner();

// Before doing anything we send an event asking if editing of this item is really wanted.
wxDataViewEvent start_event( wxEVT_DATAVIEW_ITEM_START_EDITING, dv_ctrl->GetId() );
start_event.SetDataViewColumn( GetOwner() );
start_event.SetModel( dv_ctrl->GetModel() );
start_event.SetItem( item );
start_event.SetEventObject( dv_ctrl );
dv_ctrl->GetEventHandler()->ProcessEvent( start_event );
if( !start_event.IsAllowed() )
wxDataViewEvent event(wxEVT_DATAVIEW_ITEM_START_EDITING, dv_ctrl, column, item);
dv_ctrl->GetEventHandler()->ProcessEvent( event );
if( !event.IsAllowed() )
return false;

unsigned int col = GetOwner()->GetModelColumn();
Expand Down Expand Up @@ -712,11 +709,7 @@ void wxDataViewRendererBase::NotifyEditingStarted(const wxDataViewItem& item)
wxDataViewColumn* const column = GetOwner();
wxDataViewCtrl* const dv_ctrl = column->GetOwner();

wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_EDITING_STARTED, dv_ctrl->GetId() );
event.SetDataViewColumn( column );
event.SetModel( dv_ctrl->GetModel() );
event.SetItem( item );
event.SetEventObject( dv_ctrl );
wxDataViewEvent event(wxEVT_DATAVIEW_ITEM_EDITING_STARTED, dv_ctrl, column, item);
dv_ctrl->GetEventHandler()->ProcessEvent( event );
}

Expand Down Expand Up @@ -756,7 +749,8 @@ bool wxDataViewRendererBase::FinishEditing()
wxVariant value;
const bool gotValue = GetValueFromEditorCtrl(m_editorCtrl, value);

wxDataViewCtrl* dv_ctrl = GetOwner()->GetOwner();
wxDataViewColumn* const column = GetOwner();
wxDataViewCtrl* const dv_ctrl = column->GetOwner();

DestroyEditControl();

Expand All @@ -769,14 +763,9 @@ bool wxDataViewRendererBase::FinishEditing()
unsigned int col = GetOwner()->GetModelColumn();

// Now we should send Editing Done event
wxDataViewEvent event( wxEVT_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl->GetId() );
event.SetDataViewColumn( GetOwner() );
event.SetModel( dv_ctrl->GetModel() );
event.SetItem( m_item );
wxDataViewEvent event(wxEVT_DATAVIEW_ITEM_EDITING_DONE, dv_ctrl, column, m_item);
event.SetValue( value );
event.SetColumn( col );
event.SetEditCanceled( !isValid );
event.SetEventObject( dv_ctrl );
dv_ctrl->GetEventHandler()->ProcessEvent( event );

bool accepted = false;
Expand Down Expand Up @@ -1591,6 +1580,29 @@ wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_BEGIN_DRAG, wxDataViewEvent );
wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_DROP_POSSIBLE, wxDataViewEvent );
wxDEFINE_EVENT( wxEVT_DATAVIEW_ITEM_DROP, wxDataViewEvent );

// Common part of non-copy ctors.
void wxDataViewEvent::Init(wxDataViewCtrlBase* dvc,
wxDataViewColumn* column,
const wxDataViewItem& item)
{
m_item = item;
m_col = column ? column->GetModelColumn() : -1;
m_model = dvc->GetModel();
m_column = column;
m_pos = wxDefaultPosition;
m_cacheFrom = 0;
m_cacheTo = 0;
m_editCancelled = false;
#if wxUSE_DRAG_AND_DROP
m_dataObject = NULL;
m_dataBuffer = NULL;
m_dataSize = 0;
m_dragFlags = 0;
m_dropEffect = wxDragNone;
#endif // wxUSE_DRAG_AND_DROP

SetEventObject(dvc);
}

#if wxUSE_SPINCTRL

Expand Down

0 comments on commit 9829446

Please sign in to comment.