Commit Graph

349 Commits

Author SHA1 Message Date
Vadim Zeitlin
317ad65079 Move SetTextColoursAndFont() to wxGridCellRenderer
Move this function from wxGridCellStringRenderer to its base class to
allow using it even in the classes not drawing any strings.

Also document it.
2021-04-06 22:51:08 +02:00
Paul Cornett
30637ad3b6 Make wxGridEvent methods GetRow() and GetCol() const and not virtual
See #19085
2021-03-05 07:38:32 -08:00
Paul Cornett
4388b9bd6b Make some member functions const 2021-03-04 11:49:33 -08:00
Gary Allen
095d1e317c Add wxGridSelectNone selection mode
In this mode the user can't select any cells at all in wxGrid.
2020-10-31 17:01:36 +01:00
Vadim Zeitlin
b9f946fcd3 Merge branch 'check-headers-max-warn'
Check compilation of all wx headers with all gcc warnings enabled.

This should make it impossible to introduce problems that only appear
when -Wpedantic or -Wany-other-not-completely-unreasonable-warning is
enabled when building user code including wx headers again.

See https://github.com/wxWidgets/wxWidgets/pull/2033
2020-08-31 15:10:16 +02:00
Vadim Zeitlin
be2a61519b Merge branch 'grid-selected'
Split the wxGrid RANGE_SELECT event into separate SELECTING and SELECTED
events.

See https://github.com/wxWidgets/wxWidgets/pull/2028
2020-08-31 14:55:56 +02:00
Vadim Zeitlin
2f605badf3 Fix g++ 4.8 -Wshadow for parameter/function conflicts
Unlike the later versions, g++ 4.8 produces a -Wshadow when the name of
a parameter is the same of the name of a method, so rename the (private)
parameters to avoid this.
2020-08-31 01:25:07 +02:00
Ilya Sinitsyn
5d3d376766 Remove unused wxGrid function 2020-08-24 02:09:01 +02:00
Daniel Kulp
415f080c80 Split wxGrid RANGE_SELECT event into SELECTING and SELECTED
This will allow the applications that are only interested in the final
selection to ignore the intermediate SELECTING events, which are now
sent as soon as the selection changes while dragging the mouse, and only
handle the final SELECTED ones, when the drag is over.
2020-08-20 16:56:03 +02:00
Arrigo Marchiori
c86bcf962d Use wxASCII_STR() on string literals
Fix the build with wxNO_IMPLICIT_WXSTRING_ENCODING.
2020-07-17 17:52:16 +02:00
Ian McInerney
b256aa6956 Replace wxEVT_GRID_HIDE_EDITOR usage with CallAfter
This simplifies the code by removing the need for a special event,
and also means the combox popup handler is no longer needed to reset
the inSetFocus flag for the closing handler.
2020-07-14 12:28:13 +01:00
Vadim Zeitlin
17b3a9fa96 Add missing wxOVERRIDE to wxGridCellActivatableEditor
Fix -Winconsistent-missing-override due to the changes of ca84560350
(Add wxGridCellActivatableEditor and show it in the grid sample,
2020-06-30).
2020-07-04 13:36:44 +02:00
Vadim Zeitlin
8445d1993e Fix bug in workaround for event handlers deleting wxGrid cells
The changes of e6e6dbe077 (Fix problems due to deleting grid cells in
the event handlers, 2020-05-02) fixed the bug (see #18731), but
introduced another one in place: if wxEVT_GRID_CELL_CHANGED handler
deleted any cells, wxGrid code could mistakenly restore the "old" cell
value (actually it restored the value for a different cell, as the
current cell coordinates necessarily changed too in this case).

This bug became more visible after the addition of activatable editors
as the new code asserts, instead of trying to restore the old value, if
wxEVT_GRID_CELL_CHANGED is vetoed, which also resulted in asserts if the
handler deleted the current cell instead.

Fix this by separating the cases of event being really vetoed and of the
event handler deleting the cell for which the event was generated and
handle the latter separately from the former where it matters.

This commit is best viewed ignoring whitespace changes.
2020-06-30 19:04:52 +02:00
Vadim Zeitlin
f877106663 Use EventResult enum instead of magic numbers in wxGrid code
This is more verbose, but also much more clear (and so allows to remove
some previously necessary comments).

No real changes, even keep the same values for the enum elements as were
previously used: this is probably unnecessary but do it just to minimize
changes.
2020-06-30 18:51:13 +02:00
Vadim Zeitlin
ca84560350 Add wxGridCellActivatableEditor and show it in the grid sample
Add a helper class making it easier to define activation-only editors
and use it to implement MyGridStarEditor in the sample, showing a
typical example of using such editor.
2020-06-30 02:30:36 +02:00
Vadim Zeitlin
3cc3236f10 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.
2020-06-30 02:25:14 +02:00
Vadim Zeitlin
d14a33bf37 Merge branch 'grid-streamline'
Various simplifications and bug fixes in wxGrid code, mostly related to
handling in-place cell editing.

See https://github.com/wxWidgets/wxWidgets/pull/1910
2020-06-30 00:25:46 +02:00
Vadim Zeitlin
3747169660 Remove apparently unnecessary wxGrid::m_inOnKeyDown
Although this variable, and a check for it in OnKeyDown(), was present
since the first version of this code added back in f85afd4e46 (Added new
wxGrid classes[...], 1999-10-06), there doesn't seem to be any
indication that it has ever been needed, so remove it to simplify the
code and make it possible to add early returns to this function easily.

No real changes yet.
2020-06-28 22:53:10 +02:00
Vadim Zeitlin
a400a380f2 Add wxGrid::DoEnableCellEditControl() with bool return value
Checking the new function return value is simpler than checking the
value of m_cellEditCtrlEnabled after calling EnableCellEditControl().

Do this now also when starting editing using the mouse, as it was simply
forgotten before and so StartingClick() was still called even if editing
was vetoed.

Also add DoDisableCellEditControl(), but this one exists purely for the
symmetry.
2020-06-28 01:47:06 +02:00
Vadim Zeitlin
2d9112bd9b Add DoHideCellEditControl() for symmetry
This also allows to reset m_cellEditCtrlEnabled earlier, as we don't
have to keep it true for the duration of HideCellEditControl()
execution.

No real changes, as with the previous commit, this one is best viewed
ignoring whitespace changes.
2020-06-28 00:40:09 +02:00
Vadim Zeitlin
c73634c520 Add DoShowCellEditControl() showing the editor unconditionally
It doesn't make sense to perform the checks in ShowCellEditControl()
when it's called from EnableCellEditControl() and this makes the code
unnecessarily fragile as m_cellEditCtrlEnabled needs to be set at just
the right moment for it to work correctly.

Call the new DoShowCellEditControl() instead and perform the checks only
in the public function, for compatibility.

Also note in a comment and the documentation that ShowCellEditControl()
is not very useful anyhow and that EnableCellEditControl() should most
often be used instead.

No real changes (the commit is best viewed ignoring whitespace changes).
2020-06-28 00:30:57 +02:00
Vadim Zeitlin
45bc2e648b Remove check for read only cells from IsCellEditControlEnabled()
This check was introduced back in 283b7808d8 (added support for readonly
cells and 3d border drawing, 2000-02-16), but was wrong even then and
remained wrong ever since: we must not set m_cellEditCtrlEnabled to true
when the current cell is read-only, so there is no need to check for the
latter condition if m_cellEditCtrlEnabled is indeed true.

Ensure that we really never erroneously set m_cellEditCtrlEnabled for
the read-only cells by replacing an wxASSERT_MSG checking for this in
EnableCellEditControl() with wxCHECK_RET().

Also explicitly document this function precondition, also added back in
b54ba67107 ([...] added CanEnableCellControl() and use it before calling
EnableEC, 2000-02-17) but never documented so far.
2020-06-28 00:02:29 +02:00
Vadim Zeitlin
d7f19ee610 Fix spelling in comments and documentation using codespell
Apply the utility from https://github.com/codespell-project/codespell/
to fix spelling issues in the headers under both include and interface
directories and add a file with a couple of exceptions.

The exact command line used was:

    $ codespell -w -I misc/scripts/codespell.ignore -i 3 in*
2020-06-27 22:56:22 +02:00
Vadim Zeitlin
4b1f057d74 Factor out DoAcceptCellEditControl() into a separate function
This allows to fold the last DoSaveEditControlValue() call into this
function, so that it's only called from DoAcceptCellEditControl() or
from SaveEditControlValue() (which is public, and hence can't be
changed, even if its behaviour doesn't make much sense).

This commit means that m_cellEditCtrlEnabled is now reset to false when
AcceptCellEditControlIfShown() is called, which was not the case before,
but this seems to make sense, as we shouldn't be just hiding the editor
while leaving it enabled, and, also, doesn't really seem to change
anything as hiding the editor indirectly results in a call to
DisableCellEditControl(), via wxGrid::OnHideEditor(), and so it was
actually already reset before -- but now this happens slightly earlier
and more explicitly.
2020-06-27 17:13:04 +02:00
Vadim Zeitlin
3a874471c3 Add helper AcceptCellEditControlIfShown() function
This is just another refactoring in order to avoid duplicating calls to
HideCellEditControl() and SaveEditControlValue() in several different
places.

Also call DoSaveEditControlValue() because if the editor is shown, it is
also necessarily enabled and there is no need to check for this.
2020-06-27 16:57:40 +02:00
Vadim Zeitlin
ed0f58c2dd Add helper wxGrid::GetCurrentCellEditorPtr() and use it
No real changes, just simplify the code by using a single function to
retrieve the editor to use for the current cell.

This also allows to get rid of a few temporary variables, further
amplifying the simplification.
2020-06-27 16:00:02 +02:00
Vadim Zeitlin
f5647828d0 Respect cell alignment when positioning the editors
Define the common logic for positioning editors not taking the entire
cell area (i.e. basically anything other than wxGridCellTextEditor) in
the new DoPositionEditor() function.

Also use the cell and editor alignment to decide where to position the
control if it's smaller than the cell, as it looks better if e.g.
wxGridCellDateEditor appears near the place where the date is displayed
instead of being centered in the middle of a wide column.
2020-06-26 22:19:49 +02:00
Vadim Zeitlin
867cc2a3eb Handle clicks on grid edges normally when not using drag-resizing
Clicking on (or near) the grid column or row edges was handled specially
to allow dragging them in order to resize the column or row, but this
doesn't need to be done if drag-resizing the columns or rows is not
allowed in the first place and resulted in surprising user-visible
behaviour: e.g. when using row selection, clicking mostly anywhere in
the row selected it, except if the click happened to be between the two
columns, in which case it didn't.

Fix this and always select the row in such scenario now.

Unfortunately, doing this required adding yet more CanDragXXX()
functions in addition to the already impressive panoply of them in
wxGrid, but we need CanDragGridColEdges() as none of the existing
functions checked for m_useNativeHeader (there was instead an ad hoc
check for it directly in the mouse handling code) and the row version
had to be added for symmetry.
2020-06-21 19:29:37 +02:00
Vadim Zeitlin
71d42a8290 Add wxGridCellRenderer::GetMaxBestSize()
This is another optimization, useful for the renderers that are used
with the values of a fixed form or part of a limited set, as it is much
faster to compute the best size for all values of the set rather than
computing them for all the cells in the column.
2020-06-13 15:51:20 +02:00
Vadim Zeitlin
249db04dd3 Add wxGridTableBase::CanMeasureColUsingSameAttr()
This allows to optimize AutoSizeColumns() in the common case when all
cells in the same column can be measured using the same attribute.
2020-06-11 10:03:13 +02:00
Vadim Zeitlin
123e21c181 Refactor wxGrid::SetOrCalcColumnSizes() and SetOrCalcRowSizes()
Get rid of the unnecessarily complicated functions doing two quite
different things depending on whether their first boolean parameter was
true of false.

Instead, split their body between AutoSize{Columns,Rows}() (which used
to call them) and DoGetBestSize(), keeping just the part needed in each
case.

This is much simpler and even more efficient, as it avoids a completely
unnecessary call to CalcDimensions() and Refresh() from DoGetBestSize(),
which doesn't change the current size at all and so doesn't need to
refresh anything, but previously did it and not only once, but twice,
because both of SetOrCalc{Column,Row}Sizes() did it.
2020-05-31 17:51:12 +02:00
Vadim Zeitlin
3307000baa Add wxGrid::GetSelectedRowBlocks() and GetSelectedColBlocks()
These functions are much simpler to use in the application code using
wxGrid in row- or column-only selection mode than GetSelectedBlocks()
itself because they take care of deduplicating, ordering and squashing
together the adjacent ranges, so that the application can use their
results directly, unlike with GetSelectedBlocks().
2020-05-27 03:19:34 +02:00
Vadim Zeitlin
7466681607 Merge branch 'grid-event-delete'
Handle deleting grid rows/columns in event handlers gracefully.

See https://github.com/wxWidgets/wxWidgets/pull/1834
2020-05-06 18:45:16 +02:00
Kvaz1r
63e1697dda Add wxGridBlocks::iterator::operator->()
An iterator should have this operator defined too, and not just
operator*() as it used to.
2020-05-02 18:50:03 +02:00
Vadim Zeitlin
e6e6dbe077 Fix problems due to deleting grid cells in the event handlers
Deleting last grid rows or column in a few event handlers could result
in asserts/crashes in wxGrid code if the event handler also called
event.Skip(), as wxGrid still tried to perform the default action using
the deleted cell, when these events happened in the last row or column.

It's not totally clear whether calling event.Skip() after performing an
action modifying the grid should be allowed at all, but, in doubt, at
least avoid crashing if it does happen, by considering the event as
being handled (and even vetoed) if its handler deleted the cell in which
it was generated.

Closes #18731.
2020-05-02 00:53:59 +02:00
Vadim Zeitlin
5cdcfddc61 Refactor event sending code in wxGrid to use even more functions
Move the logic determining the return value of SendEvent() into its own
function instead of repeating it twice.

No real changes, this is a pure refactoring.
2020-05-02 00:49:08 +02:00
Vadim Zeitlin
e5d03323f9 Rename wxGridBlockCoords::ContainsCell/Block() to just Contains()
These methods do the same thing, so it seems better to use the same name
for them.

This is not really a backwards-incompatible change, as these methods
were just added in the parent commit, so nobody is using them yet.
2020-04-15 18:37:06 +02:00
Vadim Zeitlin
b8c3c60316 Allow extending selection using Shift-Page Up/Down keys
Also make Page Up/Down themselves work consistently with the other
cursor movement keys and clear current selection if they move the
cursor.

Even though DoMoveCursorByPage() is simpler than DoMoveCursorByBlock(),
still factor out AdvanceByPage() for consistency with AdvanceByBlock()
and because it still makes the code more clear.
2020-04-13 01:23:25 +02:00
Vadim Zeitlin
bc3c6fea70 Fix Shift-Ctrl-arrows handling
Extending the selection with Ctrl-arrows is different from all the other
cases, as we need to combine both the selection anchor and the current
cell coordinates when doing it.

This means that we can't reuse the same PrepareForSelectionExpansion()
helper for this case, so this function is not useful finally and this
commit removes it entirely. It also replaces GetCurrentBlockCornerRow()
and GetCurrentBlockCornerCol() functions with GetExtensionAnchor() which
combines both of them.

Finally, it adds wxGridDirectionOperations::TryToAdvance() helper to
avoid repeating the IsAtBoundary() check which was previously part of
PrepareForSelectionExpansion() in multiple places.

And because the "extending" and normal parts of DoMoveCursorByBlock()
are so different now, it also factors out AdvanceByBlock() helper which
can be used to keep these parts well separate from each other instead of
intermixing them together.

With all these preparatory changes, it's finally possible to implement
the "extending selection by block" logic relatively easily, with the
bulk of this branch actually taken by comments explaining why do we have
to do what we do.

Add unit tests verifying that the functions used by Shift-Ctrl-arrow
work as expected.
2020-04-13 00:49:22 +02:00
Vadim Zeitlin
30eaa28de5 Rename wxGrid::GetSelectionRange() to GetSelectedBlocks()
This seems to be more consistent with the existing functions and doesn't
create ambiguity with a grid range.

Also rename wxGridSelectionRange to just wxGridBlocks as, in principle,
this class could be used for iterating over any blocks, not just the
selected ones.

No changes in functionality, this is just a renaming.
2020-04-12 02:38:37 +02:00
Vadim Zeitlin
69a05b0340 Use the same selection expansion logic for Shift-Ctrl-cursor
Expanding the selection from keyboard with Ctrl pressed should move in
the same way Ctrl-cursor does, but use the same selection anchor as
Shift-cursor does instead of always using the current cell.

This makes the expansion work much more intuitively in the grid, e.g.
pressing Shift-Ctrl-Down in

    1 2
    3 4

grid when 1 and 2 are selected now selects all the cells instead of
selecting 1 and 3 as it did before.
2020-04-06 02:39:21 +02:00
Vadim Zeitlin
1d43ae7dc6 Refactor selection expansion code to use actual wxKeyboardState
Switch from using just "bool expandSelection" in the grid functions
(possibly) extending the current selection to using the full
wxKeyboardState.

This allows to pass it to ExtendOrCreateCurrentBlock() and slightly
simplify the code by using DoMoveCursorFromKeyboard().
2020-04-05 15:56:45 +02:00
Vadim Zeitlin
791a9e68ae Rename and simplify wxGridBlockCoords::ContainsBlock()
Change the return type of this function to a simple and clear bool
instead of 3-valued int requiring a special explanation. This is simpler
and not any less efficient as checking for whether one block contains
another or the other one contains this one are separate operations
anyhow.

Rename the function to a more grammatically correct name.

Also move it inline as it's now trivial enough for this to be worth it.
2020-04-05 01:37:56 +02:00
Vadim Zeitlin
a5952ee087 Rename wxGridBlockCoords::ContainsCell() and move it inline
Make the function name more grammatically correct.

No real changes.
2020-04-04 19:45:19 +02:00
Vadim Zeitlin
0a5a904d8d Simplify wxGridSelectionRange to provide only iterators
This class was a strange hybrid of a container/view/range and iterator,
as it both provided begin()/end() container-like methods and
iterator-like methods for dereferencing/advancing.

Simplify this by removing the latter part and making this class really
just a range, with its own iterator class in order to avoid leaking the
exact type of the iterator used in the API.

Note that while it's now completely trivial, it is still useful as it
isolates the application code from the vector used to store the selected
blocks currently and will allow to change internal representation in the
future without breaking the existing code.
2020-04-04 19:37:23 +02:00
Vadim Zeitlin
0f8e985252 Define wxGridSelectionRange::iterator in terms of vector iterator
This fixes compilation when using std::vector<> implementation for
wxVector (i.e. with wxUSE_STD_CONTAINERS==1).
2020-04-04 19:04:17 +02:00
Ilya Sinitsyn
f8015b13b1 Implement wxGrid selection blocks iterating interface 2020-04-04 18:50:37 +02:00
Ilya Sinitsyn
0920a1646b Make wxGrid row selecting more user friendly 2020-04-04 18:50:37 +02:00
Ilya Sinitsyn
89dd47edee Make wxGrid column selecting more user friendly 2020-04-04 18:50:37 +02:00
Ilya Sinitsyn
e1b9ece9a4 Edit the current wxGrid selection block
Really edit the current selection block instead of storing the temporary
information about the current selection and applying it on releasing Shift
key or LKM.
2020-04-04 18:50:37 +02:00