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 <optional>
30#include <utility>
31#include <wx/string.h>
32#include <vector>
33
34class wxWindow;
35template<class T> class wxScrolled;
37
38class EditorCell;
39class TextCell;
40
53{
54public:
57 {
58 public:
59 ErrorList() = default;
61 bool Empty() const { return m_errors.empty(); }
63 void Remove(GroupCell * cell);
65 bool Contains(GroupCell * cell) const;
67 void Add(GroupCell * cell);
69 GroupCell *FirstError() const;
71 GroupCell *LastError() const;
73 void Clear() { m_errors.clear(); }
74 private:
76 std::vector<CellPtr<GroupCell>> m_errors;
77 };
78
80
84 GroupCell *GetWorkingGroup(bool resortToLast = false) const;
88 GroupCell *GetLastWorkingGroup() const { return m_lastWorkingGroup; }
89
91 bool HasCellsSelected() const { return m_selectionStart && m_selectionEnd; }
92
96 const CellPtr<Cell> &GetSelectionStart() const { return m_selectionStart; }
98 const CellPtr<Cell> &GetSelectionEnd() const { return m_selectionEnd; }
101 void SetSelectionStart(Cell *cell) { m_selectionStart = cell; m_blockMatrix = {}; }
104 void SetSelectionEnd(Cell *cell) { m_selectionEnd = cell; m_blockMatrix = {}; }
105
120 void SetSelectedMatrixBlock(Cell *matrix, const MatrixEntry &anchor,
121 const MatrixEntry &corner,
122 bool wholeMatrix = false) {
123 m_selectionStart = matrix;
124 m_selectionEnd = matrix;
125 m_blockMatrix = matrix;
126 m_blockAnchor = anchor;
127 m_blockCorner = corner;
128 m_blockIsWholeMatrix = wholeMatrix;
129 }
137 std::optional<MatrixBlock> GetSelectedMatrixBlock() const {
138 if (IsBlockMatrixSelected() && !m_blockIsWholeMatrix)
139 return MatrixBlock::Spanning(m_blockAnchor, m_blockCorner);
140 return std::nullopt;
141 }
144 {
145 MatrixEntry anchor;
146 MatrixEntry corner;
147 };
154 std::optional<BlockCorners> GetSelectedMatrixBlockCorners() const {
155 if (IsBlockMatrixSelected())
156 return BlockCorners{m_blockAnchor, m_blockCorner};
157 return std::nullopt;
158 }
159
160 // The out-of-line definitions convert a CellPtr to (or a raw pointer from)
161 // EditorCell*/TextCell*, which needs the complete type this header only
162 // forward-declares.
164 EditorCell *GetAnswerCell() const;
166 void SetAnswerCell(EditorCell *cell);
168 void ClearAnswerCell() { m_answerCell = {}; }
172
176 void SetCurrentTextCell(TextCell *cell);
177
179 EditorCell *GetActiveCell() const;
181 void SetActiveCell(EditorCell *cell);
183 void ClearActiveCell() { m_activeCell = {}; }
184
186 const wxString &GetSelectionString() const { return m_selectionString; }
188 void SetSelectionString(const wxString &str) { m_selectionString = str; }
190 void ClearSelectionString() { m_selectionString.Clear(); }
191
193 ErrorList &GetErrorList() { return m_errorList; }
195 const ErrorList &GetErrorList() const { return m_errorList; }
196
205 void SetEvaluatingStatement(EditorCell *editor, std::size_t start,
206 std::size_t end, const wxString &text);
212 std::optional<std::pair<std::size_t, std::size_t>>
213 GetEvaluatingStatement(const EditorCell *editor, const wxString &text) const;
214
215private:
220 CellPtr<GroupCell> m_workingGroup;
222 CellPtr<GroupCell> m_lastWorkingGroup;
224 CellPtr<EditorCell> m_answerCell;
226 CellPtr<TextCell> m_currentTextCell;
228 CellPtr<EditorCell> m_activeCell;
235 CellPtr<Cell> m_selectionStart;
242 CellPtr<Cell> m_selectionEnd;
244 CellPtr<Cell> m_blockMatrix;
246 MatrixEntry m_blockAnchor;
248 MatrixEntry m_blockCorner;
250 bool m_blockIsWholeMatrix = false;
252 bool IsBlockMatrixSelected() const {
253 return m_blockMatrix && (m_selectionStart == m_blockMatrix) &&
254 (m_selectionEnd == m_blockMatrix);
255 }
261 wxString m_selectionString;
263 ErrorList m_errorList;
265 CellPtr<EditorCell> m_evaluatingEditor;
266 std::size_t m_evaluatingStart = 0;
267 std::size_t m_evaluatingEnd = 0;
268 wxString m_evaluatingText;
269};
270
282{
283public:
284 explicit ViewCellPointers(wxScrolledCanvas *worksheet) : m_worksheet(worksheet) {}
285
286 void ScrollToCell(Cell *cell) { m_cellToScrollTo = cell; }
287 Cell *CellToScrollTo() { return m_cellToScrollTo; }
288
290 bool ScrollToCellScheduled() const { return m_scrollToCell; }
292 void SetScrollToCellScheduled(bool scheduled) { m_scrollToCell = scheduled; }
293
295 const CellPtr<EditorCell> &SearchStart() const { return m_cellSearchStartedIn; }
297 int IndexSearchStartedAt() const { return m_indexSearchStartedAt; }
300 void SetSearchStart(EditorCell *cell, int index);
303 {
304 m_cellSearchStartedIn = {};
305 m_indexSearchStartedAt = -1;
306 }
307
310 { return m_cellMouseSelectionStartedIn; }
315 { m_cellMouseSelectionStartedIn = {}; }
316
319 { return m_cellKeyboardSelectionStartedIn; }
324 { m_cellKeyboardSelectionStartedIn = {}; }
325
326 void SetTimerIdForCell(Cell *cell, int timerId);
327 int GetTimerIdForCell(Cell *cell) const;
328 Cell *GetCellForTimerId(int timerId) const;
329 void RemoveTimerIdForCell(const Cell *const cell);
330
336 Cell *GetCellUnderPointer() const { return m_cellUnderPointer; }
338 void SetCellUnderPointer(Cell *cell) { m_cellUnderPointer = cell; }
340 void ClearCellUnderPointer() { m_cellUnderPointer = {}; }
341
342 void WXMXResetCounter() { m_wxmxImgCounter = 0; }
343 wxString WXMXGetNewFileName();
344 std::size_t WXMXImageCount() const { return m_wxmxImgCounter; }
345
346 wxScrolledCanvas *GetWorksheet() { return m_worksheet; }
347
348private:
350 CellPtr<GroupCell> m_groupCellUnderPointer;
352 CellPtr<Cell> m_cellUnderPointer;
354 CellPtr<EditorCell> m_cellMouseSelectionStartedIn;
356 CellPtr<EditorCell> m_cellKeyboardSelectionStartedIn;
358 CellPtr<EditorCell> m_cellSearchStartedIn;
360 int m_indexSearchStartedAt = -1;
362 bool m_scrollToCell = false;
363 struct CellTimerId {
364 // A CellPtr, not a raw pointer: animation timers fire asynchronously, so a
365 // timer entry can outlive its cell. Auto-nulling means GetCellForTimerId()
366 // returns nullptr for a destroyed cell instead of a dangling pointer, even
367 // if RemoveTimerIdForCell() was somehow not reached (see the "Long-lived
368 // cell references" rule in CellPtr.h).
369 CellPtr<Cell> cell;
370 int timerId = -1;
371 CellTimerId() = default;
372 CellTimerId(Cell *cell, int timerId) : cell(cell), timerId(timerId) {}
373 };
375 std::vector<CellTimerId> m_timerIds;
377 CellPtr<Cell> m_cellToScrollTo;
379 wxScrolledCanvas *const m_worksheet;
381 std::size_t m_wxmxImgCounter = 0;
382};
383
384#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:57
void Clear()
Empty the list of GroupCells with errors.
Definition: CellPointers.h:73
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:61
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:53
void SetSelectionEnd(Cell *cell)
Set the last cell of the selected range (may be null). Forgets any selected matrix block,...
Definition: CellPointers.h:104
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:96
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:168
const CellPtr< Cell > & GetSelectionEnd() const
The last cell of the currently selected range of cells, or null.
Definition: CellPointers.h:98
void ClearActiveCell()
Forget the editor cell the text cursor was in.
Definition: CellPointers.h:183
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:190
GroupCell * GetLastWorkingGroup() const
The last group cell maxima was working on (regardless of the current one).
Definition: CellPointers.h:88
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:137
void SetSelectionString(const wxString &str)
Set the currently selected string.
Definition: CellPointers.h:188
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:120
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:101
bool HasCellsSelected() const
Are any whole cells (as opposed to text inside an editor) selected?
Definition: CellPointers.h:91
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:193
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:186
std::optional< BlockCorners > GetSelectedMatrixBlockCorners() const
The corners of the selected block, even if it has grown to the whole matrix.
Definition: CellPointers.h:154
const ErrorList & GetErrorList() const
The list of cells maxima has complained about errors in (read-only view).
Definition: CellPointers.h:195
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:282
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:323
void ClearCellUnderPointer()
Forget which cell was under the mouse pointer.
Definition: CellPointers.h:340
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:309
void ResetSearchStart()
Forget where the search was started.
Definition: CellPointers.h:302
void ResetMouseSelectionStart()
Forget where the mouse selection was started.
Definition: CellPointers.h:314
int IndexSearchStartedAt() const
The cursor position an incremental search was started at (-1 = none).
Definition: CellPointers.h:297
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:290
const CellPtr< EditorCell > & KeyboardSelectionStart() const
The EditorCell a keyboard selection was started in, or null.
Definition: CellPointers.h:318
void SetCellUnderPointer(Cell *cell)
Set the cell under the mouse pointer (may be null).
Definition: CellPointers.h:338
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:336
const CellPtr< EditorCell > & SearchStart() const
The EditorCell an incremental search was started in, or null.
Definition: CellPointers.h:295
void SetScrollToCellScheduled(bool scheduled)
Schedule (or cancel) scrolling to CellToScrollTo().
Definition: CellPointers.h:292
Definition: CellPointers.h:35
The two corners of a selected block: the anchor first, then the moving one.
Definition: CellPointers.h:144
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