wxMaxima
Loading...
Searching...
No Matches
CellPointers.h
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2004-2015 Andrej Vodopivec <andrej.vodopivec@gmail.com>
4// Copyright (C) 2014-2018 Gunter Königsmann <wxMaxima@physikbuch.de>
5// Copyright (C) 2020 Kuba Ober <kuba@bertec.com>
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17//
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21//
22// SPDX-License-Identifier: GPL-2.0+
23
24#ifndef WXMAXIMA_CELLPOINTERS_H
25#define WXMAXIMA_CELLPOINTERS_H
26
27#include "cells/Cell.h"
28#include "cells/MatrixBlock.h"
29#include <cstddef>
30#include <optional>
31#include <utility>
32#include <wx/string.h>
33#include <vector>
34
35class wxWindow;
36template<class T> class wxScrolled;
38
39class EditorCell;
40class TextCell;
41
54{
55public:
58 {
59 public:
60 ErrorList() = default;
62 bool Empty() const { return m_errors.empty(); }
64 void Remove(GroupCell * cell);
66 bool Contains(GroupCell * cell) const;
68 void Add(GroupCell * cell);
70 GroupCell *FirstError() const;
72 GroupCell *LastError() const;
74 void Clear() { m_errors.clear(); }
75 private:
77 std::vector<CellPtr<GroupCell>> m_errors;
78 };
79
81
85 GroupCell *GetWorkingGroup(bool resortToLast = false) const;
89 GroupCell *GetLastWorkingGroup() const { return m_lastWorkingGroup; }
90
92 bool HasCellsSelected() const { return m_selectionStart && m_selectionEnd; }
93
97 const CellPtr<Cell> &GetSelectionStart() const { return m_selectionStart; }
99 const CellPtr<Cell> &GetSelectionEnd() const { return m_selectionEnd; }
103 m_selectionStart = cell; m_blockMatrix = {}; m_textFirst = {};
104 }
107 void SetSelectionEnd(Cell *cell) {
108 m_selectionEnd = cell; m_blockMatrix = {}; m_textFirst = {};
109 }
110
119 {
120 Cell *first = nullptr;
121 std::size_t from = 0;
122 Cell *last = nullptr;
123 std::size_t to = 0;
124 bool operator==(const TextRange &) const = default;
125 };
133 void SetSelectedText(const TextRange &range) {
134 m_selectionStart = range.first;
135 m_selectionEnd = range.last;
136 m_blockMatrix = {};
137 m_textFirst = range.first;
138 m_textLast = range.last;
139 m_textFrom = range.from;
140 m_textTo = range.to;
141 }
149 std::optional<TextRange> GetSelectedText() const {
150 if (m_textFirst && m_textLast && (m_selectionStart == m_textFirst) &&
151 (m_selectionEnd == m_textLast))
152 return TextRange{m_textFirst.get(), m_textFrom, m_textLast.get(), m_textTo};
153 return std::nullopt;
154 }
155
170 void SetSelectedMatrixBlock(Cell *matrix, const MatrixEntry &anchor,
171 const MatrixEntry &corner,
172 bool wholeMatrix = false) {
173 m_selectionStart = matrix;
174 m_selectionEnd = matrix;
175 m_blockMatrix = matrix;
176 m_textFirst = {};
177 m_blockAnchor = anchor;
178 m_blockCorner = corner;
179 m_blockIsWholeMatrix = wholeMatrix;
180 }
188 std::optional<MatrixBlock> GetSelectedMatrixBlock() const {
189 if (IsBlockMatrixSelected() && !m_blockIsWholeMatrix)
190 return MatrixBlock::Spanning(m_blockAnchor, m_blockCorner);
191 return std::nullopt;
192 }
195 {
196 MatrixEntry anchor;
197 MatrixEntry corner;
198 };
205 std::optional<BlockCorners> GetSelectedMatrixBlockCorners() const {
206 if (IsBlockMatrixSelected())
207 return BlockCorners{m_blockAnchor, m_blockCorner};
208 return std::nullopt;
209 }
210
211 // The out-of-line definitions convert a CellPtr to (or a raw pointer from)
212 // EditorCell*/TextCell*, which needs the complete type this header only
213 // forward-declares.
215 EditorCell *GetAnswerCell() const;
217 void SetAnswerCell(EditorCell *cell);
219 void ClearAnswerCell() { m_answerCell = {}; }
223
227 void SetCurrentTextCell(TextCell *cell);
228
230 EditorCell *GetActiveCell() const;
232 void SetActiveCell(EditorCell *cell);
234 void ClearActiveCell() { m_activeCell = {}; }
235
237 const wxString &GetSelectionString() const { return m_selectionString; }
239 void SetSelectionString(const wxString &str) { m_selectionString = str; }
241 void ClearSelectionString() { m_selectionString.Clear(); }
242
244 ErrorList &GetErrorList() { return m_errorList; }
246 const ErrorList &GetErrorList() const { return m_errorList; }
247
256 void SetEvaluatingStatement(EditorCell *editor, std::size_t start,
257 std::size_t end, const wxString &text);
263 std::optional<std::pair<std::size_t, std::size_t>>
264 GetEvaluatingStatement(const EditorCell *editor, const wxString &text) const;
265
266private:
271 CellPtr<GroupCell> m_workingGroup;
273 CellPtr<GroupCell> m_lastWorkingGroup;
275 CellPtr<EditorCell> m_answerCell;
277 CellPtr<TextCell> m_currentTextCell;
279 CellPtr<EditorCell> m_activeCell;
286 CellPtr<Cell> m_selectionStart;
293 CellPtr<Cell> m_selectionEnd;
295 CellPtr<Cell> m_blockMatrix;
297 MatrixEntry m_blockAnchor;
299 MatrixEntry m_blockCorner;
301 CellPtr<Cell> m_textFirst;
303 CellPtr<Cell> m_textLast;
305 std::size_t m_textFrom = 0;
307 std::size_t m_textTo = 0;
309 bool m_blockIsWholeMatrix = false;
311 bool IsBlockMatrixSelected() const {
312 return m_blockMatrix && (m_selectionStart == m_blockMatrix) &&
313 (m_selectionEnd == m_blockMatrix);
314 }
320 wxString m_selectionString;
322 ErrorList m_errorList;
324 CellPtr<EditorCell> m_evaluatingEditor;
325 std::size_t m_evaluatingStart = 0;
326 std::size_t m_evaluatingEnd = 0;
327 wxString m_evaluatingText;
328};
329
341{
342public:
343 explicit ViewCellPointers(wxScrolledCanvas *worksheet) : m_worksheet(worksheet) {}
344
345 void ScrollToCell(Cell *cell) { m_cellToScrollTo = cell; }
346 Cell *CellToScrollTo() { return m_cellToScrollTo; }
347
349 bool ScrollToCellScheduled() const { return m_scrollToCell; }
351 void SetScrollToCellScheduled(bool scheduled) { m_scrollToCell = scheduled; }
352
354 const CellPtr<EditorCell> &SearchStart() const { return m_cellSearchStartedIn; }
356 int IndexSearchStartedAt() const { return m_indexSearchStartedAt; }
359 void SetSearchStart(EditorCell *cell, int index);
362 {
363 m_cellSearchStartedIn = {};
364 m_indexSearchStartedAt = -1;
365 }
366
369 { return m_cellMouseSelectionStartedIn; }
374 { m_cellMouseSelectionStartedIn = {}; }
375
378 { return m_cellKeyboardSelectionStartedIn; }
383 { m_cellKeyboardSelectionStartedIn = {}; }
384
385 void SetTimerIdForCell(Cell *cell, int timerId);
386 int GetTimerIdForCell(Cell *cell) const;
387 Cell *GetCellForTimerId(int timerId) const;
388 void RemoveTimerIdForCell(const Cell *const cell);
389
395 Cell *GetCellUnderPointer() const { return m_cellUnderPointer; }
397 void SetCellUnderPointer(Cell *cell) { m_cellUnderPointer = cell; }
399 void ClearCellUnderPointer() { m_cellUnderPointer = {}; }
400
401 void WXMXResetCounter() { m_wxmxImgCounter = 0; }
402 wxString WXMXGetNewFileName();
403 std::size_t WXMXImageCount() const { return m_wxmxImgCounter; }
404
405 wxScrolledCanvas *GetWorksheet() { return m_worksheet; }
406
407private:
409 CellPtr<GroupCell> m_groupCellUnderPointer;
411 CellPtr<Cell> m_cellUnderPointer;
413 CellPtr<EditorCell> m_cellMouseSelectionStartedIn;
415 CellPtr<EditorCell> m_cellKeyboardSelectionStartedIn;
417 CellPtr<EditorCell> m_cellSearchStartedIn;
419 int m_indexSearchStartedAt = -1;
421 bool m_scrollToCell = false;
422 struct CellTimerId {
423 // A CellPtr, not a raw pointer: animation timers fire asynchronously, so a
424 // timer entry can outlive its cell. Auto-nulling means GetCellForTimerId()
425 // returns nullptr for a destroyed cell instead of a dangling pointer, even
426 // if RemoveTimerIdForCell() was somehow not reached (see the "Long-lived
427 // cell references" rule in CellPtr.h).
428 CellPtr<Cell> cell;
429 int timerId = -1;
430 CellTimerId() = default;
431 CellTimerId(Cell *cell, int timerId) : cell(cell), timerId(timerId) {}
432 };
434 std::vector<CellTimerId> m_timerIds;
436 CellPtr<Cell> m_cellToScrollTo;
438 wxScrolledCanvas *const m_worksheet;
440 std::size_t m_wxmxImgCounter = 0;
441};
442
443#endif
The definition of the base class of all cells the worksheet consists of.
int group
Neighbouring sections of the same group aren't separated.
Definition: ToolBarSections.cpp:45
A weak non-owning pointer that becomes null whenever the observed object is destroyed.
Definition: CellPtr.h:539
The base class all cell types the worksheet can consist of are derived from.
Definition: Cell.h:148
A list of editor cells containing error messages.
Definition: CellPointers.h:58
void Clear()
Empty the list of GroupCells with errors.
Definition: CellPointers.h:74
void Remove(GroupCell *cell)
Remove one specific GroupCell from the list of errors.
Definition: CellPointers.cpp:104
bool Empty() const
Is the list of errors empty?
Definition: CellPointers.h:62
bool Contains(GroupCell *cell) const
Does the list of GroupCell with errors contain cell?
Definition: CellPointers.cpp:108
void Add(GroupCell *cell)
Mark this GroupCell as containing errors.
Definition: CellPointers.cpp:112
GroupCell * FirstError() const
The first GroupCell with error that is still in the list.
Definition: CellPointers.cpp:116
GroupCell * LastError() const
The last GroupCell with errors in the list.
Definition: CellPointers.cpp:120
The document-model half of the cell-pointer registry.
Definition: CellPointers.h:54
void SetSelectionEnd(Cell *cell)
Set the last cell of the selected range (may be null). Forgets any selected matrix block,...
Definition: CellPointers.h:107
TextCell * GetCurrentTextCell() const
The text cell the text maxima is currently sending us is being appended to.
Definition: CellPointers.cpp:133
const CellPtr< Cell > & GetSelectionStart() const
The first cell of the currently selected range of cells, or null. Returned by reference so callers ke...
Definition: CellPointers.h:97
void SetAnswerCell(EditorCell *cell)
Set the editor cell maxima's current question is answered in.
Definition: CellPointers.cpp:126
void ClearAnswerCell()
Forget the editor cell that held maxima's current question.
Definition: CellPointers.h:219
const CellPtr< Cell > & GetSelectionEnd() const
The last cell of the currently selected range of cells, or null.
Definition: CellPointers.h:99
void ClearActiveCell()
Forget the editor cell the text cursor was in.
Definition: CellPointers.h:234
void SetCurrentTextCell(TextCell *cell)
Set the text cell maxima's incoming text is being appended to (may be null).
Definition: CellPointers.cpp:137
void ClearSelectionString()
Forget the currently selected string.
Definition: CellPointers.h:241
GroupCell * GetLastWorkingGroup() const
The last group cell maxima was working on (regardless of the current one).
Definition: CellPointers.h:89
void ClearAnswerCellIfInGroup(GroupCell *group)
Forget the question editor if it belongs to group, e.g. because that group's output is being reset or...
Definition: CellPointers.cpp:128
EditorCell * GetAnswerCell() const
The editor cell maxima's current question is being answered in, or null.
Definition: CellPointers.cpp:124
std::optional< MatrixBlock > GetSelectedMatrixBlock() const
The selected block of a matrix's entries, if only a block is selected.
Definition: CellPointers.h:188
void SetSelectionString(const wxString &str)
Set the currently selected string.
Definition: CellPointers.h:239
void SetSelectedMatrixBlock(Cell *matrix, const MatrixEntry &anchor, const MatrixEntry &corner, bool wholeMatrix=false)
Narrow the selection to a block of one matrix's entries (GH #2345)
Definition: CellPointers.h:170
GroupCell * GetWorkingGroup(bool resortToLast=false) const
Returns the cell maxima currently works on. nullptr if there isn't such a cell.
Definition: CellPointers.cpp:151
void SetSelectionStart(Cell *cell)
Set the first cell of the selected range (may be null). Forgets any selected matrix block,...
Definition: CellPointers.h:102
bool HasCellsSelected() const
Are any whole cells (as opposed to text inside an editor) selected?
Definition: CellPointers.h:92
void SetEvaluatingStatement(EditorCell *editor, std::size_t start, std::size_t end, const wxString &text)
The statement of the working group's cell that Maxima is evaluating now.
Definition: CellPointers.cpp:158
void SetWorkingGroup(GroupCell *group)
Sets the cell maxima currently works on. nullptr if there isn't such a cell.
Definition: CellPointers.cpp:145
ErrorList & GetErrorList()
The list of cells maxima has complained about errors in.
Definition: CellPointers.h:244
void SetActiveCell(EditorCell *cell)
Set the editor cell the blinking text cursor is in (may be null).
Definition: CellPointers.cpp:143
const wxString & GetSelectionString() const
The currently selected string, or empty.
Definition: CellPointers.h:237
std::optional< TextRange > GetSelectedText() const
The characters selected in a run of text cells, if only some are.
Definition: CellPointers.h:149
void SetSelectedText(const TextRange &range)
Select only some of the characters of a run of text cells (GH #825)
Definition: CellPointers.h:133
std::optional< BlockCorners > GetSelectedMatrixBlockCorners() const
The corners of the selected block, even if it has grown to the whole matrix.
Definition: CellPointers.h:205
const ErrorList & GetErrorList() const
The list of cells maxima has complained about errors in (read-only view).
Definition: CellPointers.h:246
std::optional< std::pair< std::size_t, std::size_t > > GetEvaluatingStatement(const EditorCell *editor, const wxString &text) const
The statement range SetEvaluatingStatement() set for editor.
Definition: CellPointers.cpp:169
EditorCell * GetActiveCell() const
The editor cell the blinking text cursor is in, or null.
Definition: CellPointers.cpp:141
This class defines what the user sees as input cell.
Definition: EditorCell.h:67
A cell grouping input (and, if there is one, also the output) cell to a foldable item.
Definition: GroupCell.h:88
A Text cell.
Definition: TextCell.h:40
The view/interaction half of the cell-pointer registry.
Definition: CellPointers.h:341
void SetSearchStart(EditorCell *cell, int index)
Record where an incremental search started (its cell and cursor index). Out-of-line: assigning the ra...
Definition: CellPointers.cpp:79
void SetGroupCellUnderPointer(GroupCell *cell)
Set the GroupCell under the mouse pointer (may be null).
Definition: CellPointers.cpp:96
void ResetKeyboardSelectionStart()
Forget where the keyboard selection was started.
Definition: CellPointers.h:382
void ClearCellUnderPointer()
Forget which cell was under the mouse pointer.
Definition: CellPointers.h:399
void SetMouseSelectionStart(EditorCell *cell)
Record the EditorCell a mouse selection started in (out-of-line: see above).
Definition: CellPointers.cpp:84
const CellPtr< EditorCell > & MouseSelectionStart() const
The EditorCell a mouse selection was started in, or null.
Definition: CellPointers.h:368
void ResetSearchStart()
Forget where the search was started.
Definition: CellPointers.h:361
void ResetMouseSelectionStart()
Forget where the mouse selection was started.
Definition: CellPointers.h:373
int IndexSearchStartedAt() const
The cursor position an incremental search was started at (-1 = none).
Definition: CellPointers.h:356
GroupCell * GetGroupCellUnderPointer() const
The GroupCell currently under the mouse pointer, or null.
Definition: CellPointers.cpp:92
bool ScrollToCellScheduled() const
Is a scroll to CellToScrollTo() currently scheduled?
Definition: CellPointers.h:349
const CellPtr< EditorCell > & KeyboardSelectionStart() const
The EditorCell a keyboard selection was started in, or null.
Definition: CellPointers.h:377
void SetCellUnderPointer(Cell *cell)
Set the cell under the mouse pointer (may be null).
Definition: CellPointers.h:397
void SetKeyboardSelectionStart(EditorCell *cell)
Record the EditorCell a keyboard selection started in (out-of-line: see above).
Definition: CellPointers.cpp:88
Cell * GetCellUnderPointer() const
The cell currently under the mouse pointer, or null.
Definition: CellPointers.h:395
const CellPtr< EditorCell > & SearchStart() const
The EditorCell an incremental search was started in, or null.
Definition: CellPointers.h:354
void SetScrollToCellScheduled(bool scheduled)
Schedule (or cancel) scrolling to CellToScrollTo().
Definition: CellPointers.h:351
Definition: CellPointers.h:36
The two corners of a selected block: the anchor first, then the moving one.
Definition: CellPointers.h:195
Characters selected in a run of text cells (GH #825)
Definition: CellPointers.h:119
static MatrixBlock Spanning(const MatrixEntry &a, const MatrixEntry &b)
The smallest block holding both entries: the rectangle they are corners of.
Definition: MatrixBlock.h:62
One entry of a matrix, by its zero-based row and column.
Definition: MatrixBlock.h:30