Add support for "activatable" editors to wxGrid

This is useful for editors which don't really need to show any control
as they can change their value directly and will be used to reimplement
wxGridCellBoolEditor in the upcoming commits.
This commit is contained in:
Vadim Zeitlin
2020-06-30 02:25:14 +02:00
parent 73532c4d95
commit 3cc3236f10
3 changed files with 347 additions and 13 deletions

View File

@@ -233,6 +233,130 @@ public:
// Smart pointer to wxGridCellRenderer, calling DecRef() on it automatically.
typedef wxObjectDataPtr<wxGridCellRenderer> wxGridCellRendererPtr;
// ----------------------------------------------------------------------------
// Helper classes used by wxGridCellEditor::TryActivate() and DoActivate().
// ----------------------------------------------------------------------------
// This class represents a source of cell activation, which may be either a
// user event (mouse or keyboard) or the program itself.
//
// Note that objects of this class are supposed to be ephemeral and so store
// pointers to the events specified when creating them, which are supposed to
// have life-time greater than that of the objects of this class.
class wxGridActivationSource
{
public:
enum Origin
{
Program,
Key,
Mouse
};
// Factory functions, only used by the library itself.
static wxGridActivationSource FromProgram()
{
return wxGridActivationSource(Program, NULL);
}
static wxGridActivationSource From(const wxKeyEvent& event)
{
return wxGridActivationSource(Key, &event);
}
static wxGridActivationSource From(const wxMouseEvent& event)
{
return wxGridActivationSource(Mouse, &event);
}
// Accessors allowing to retrieve information about the source.
// Can be called for any object.
Origin GetOrigin() const { return m_origin; }
// Can be called for objects with Key origin only.
const wxKeyEvent& GetKeyEvent() const
{
wxASSERT( m_origin == Key );
return *static_cast<const wxKeyEvent*>(m_event);
}
// Can be called for objects with Mouse origin only.
const wxMouseEvent& GetMouseEvent() const
{
wxASSERT( m_origin == Mouse );
return *static_cast<const wxMouseEvent*>(m_event);
}
private:
wxGridActivationSource(Origin origin, const wxEvent* event)
: m_origin(origin),
m_event(event)
{
}
const Origin m_origin;
const wxEvent* const m_event;
};
// This class represents the result of TryActivate(), which may be either
// absence of any action (if activating wouldn't change the value anyhow),
// attempt to change the value to the specified one or just start normal
// editing, which is the default for the editors not supporting activation.
class wxGridActivationResult
{
public:
enum Action
{
Ignore,
Change,
ShowEditor
};
// Factory functions, only used by the library itself.
static wxGridActivationResult DoNothing()
{
return wxGridActivationResult(Ignore);
}
static wxGridActivationResult DoChange(const wxString& newval)
{
return wxGridActivationResult(Change, newval);
}
static wxGridActivationResult DoEdit()
{
return wxGridActivationResult(ShowEditor);
}
// Accessors allowing to retrieve information about the result.
// Can be called for any object.
Action GetAction() const { return m_action; }
// Can be called for objects with Change action type only.
const wxString& GetNewValue() const
{
wxASSERT( m_action == Change );
return m_newval;
}
private:
explicit
wxGridActivationResult(Action action, const wxString& newval = wxString())
: m_action(action),
m_newval(newval)
{
}
const Action m_action;
const wxString m_newval;
};
// ----------------------------------------------------------------------------
// wxGridCellEditor: This class is responsible for providing and manipulating
// the in-place edit controls for the grid. Instances of wxGridCellEditor
@@ -335,6 +459,32 @@ public:
wxControl* GetControl() { return wxDynamicCast(m_control, wxControl); }
void SetControl(wxControl* control) { m_control = control; }
// Support for "activatable" editors: those change the value of the cell
// immediately, instead of creating an editor control and waiting for user
// input.
//
// See wxGridCellBoolEditor for an example of such editor.
// Override this function to return "Change" activation result from it to
// show that the editor supports activation. DoActivate() will be called if
// the cell changing event is not vetoed.
virtual
wxGridActivationResult
TryActivate(int WXUNUSED(row), int WXUNUSED(col),
wxGrid* WXUNUSED(grid),
const wxGridActivationSource& WXUNUSED(actSource))
{
return wxGridActivationResult::DoEdit();
}
virtual
void
DoActivate(int WXUNUSED(row), int WXUNUSED(col), wxGrid* WXUNUSED(grid))
{
wxFAIL_MSG( "Must be overridden if TryActivate() is overridden" );
}
protected:
// the dtor is private because only DecRef() can delete us
virtual ~wxGridCellEditor();
@@ -2908,13 +3058,17 @@ private:
}
// Show/hide the cell editor for the current cell unconditionally.
void DoShowCellEditControl();
// Return false if the editor was activated instead of being shown and also
// sets m_cellEditCtrlEnabled to true when it returns true as a side effect.
bool DoShowCellEditControl(const wxGridActivationSource& actSource);
void DoHideCellEditControl();
// Unconditionally try showing the editor for the current cell.
//
// Returns false if the user code vetoed wxEVT_GRID_EDITOR_SHOWN.
bool DoEnableCellEditControl();
// Returns false if the user code vetoed wxEVT_GRID_EDITOR_SHOWN or if the
// editor was simply activated and won't be permanently shown.
bool DoEnableCellEditControl(const wxGridActivationSource& actSource);
// Unconditionally disable (accepting the changes) the editor.
void DoDisableCellEditControl();