wxMaxima
Loading...
Searching...
No Matches
TreeUndoManager.h
Go to the documentation of this file.
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2026 Gunter Königsmann <wxMaxima@physikbuch.de>
4//
5// This program is free software; you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation; either version 2 of the License, or
8// (at your option) any later version.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15//
16// You should have received a copy of the GNU General Public License
17// along with this program; if not, write to the Free Software
18// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19//
20// SPDX-License-Identifier: GPL-2.0+
21
34#ifndef TREEUNDOMANAGER_H
35#define TREEUNDOMANAGER_H
36
37#include "TreeUndoAction.h"
38
51{
52public:
54 UndoActions &UndoStack() { return m_undoActions; }
55 const UndoActions &UndoStack() const { return m_undoActions; }
56
58 UndoActions &RedoStack() { return m_redoActions; }
59 const UndoActions &RedoStack() const { return m_redoActions; }
60
67 static void DiscardAction(UndoActions *actionList)
68 {
69 if (actionList->empty())
70 return;
71 do {
72 actionList->pop_back();
73 } while (!actionList->empty() && actionList->back().m_partOfAtomicAction);
74 }
75
77 static void AppendAction(UndoActions *actionList)
78 {
79 if (!actionList->empty())
80 actionList->front().m_partOfAtomicAction = true;
81 }
82
84 void AppendAction() { AppendAction(&m_undoActions); }
85
88 {
89 while (!m_redoActions.empty())
90 DiscardAction(&m_redoActions);
91 }
92
95 {
96 while (!m_undoActions.empty())
97 DiscardAction(&m_undoActions);
98 }
99
102 {
105 m_activeCell = nullptr;
106 }
107
115 void CellEntered(GroupCell *group, const wxString &text,
116 long long selStart, long long selEnd)
117 {
118 m_activeCell = group;
119 m_activeCellOldText = text;
120 m_activeCellOldSelStart = selStart;
121 m_activeCellOldSelEnd = selEnd;
122 }
123
134 bool CellLeft(GroupCell *activeCell, const wxString &currentText)
135 {
136 if (m_activeCell) //-V1051
137 wxASSERT_MSG(m_activeCell == activeCell,
138 _("Bug: Cell left but not entered."));
139
140 // We only can undo a text change if the text has actually changed.
141 if ((m_activeCellOldText.Length() > 1) &&
142 (m_activeCellOldText != currentText) &&
143 (m_activeCellOldText + wxS(";") != currentText)) {
144 m_undoActions.emplace_front(activeCell, m_activeCellOldText,
145 m_activeCellOldSelStart,
146 m_activeCellOldSelEnd);
148 return true;
149 }
150 return false;
151 }
152
154 void ForgetActiveCell() { m_activeCell = nullptr; }
155
157 GroupCell *ActiveCell() const { return m_activeCell; }
158
159private:
161 UndoActions m_undoActions;
162
164 UndoActions m_redoActions;
165
172 CellPtr<GroupCell> m_activeCell;
173
175 wxString m_activeCellOldText;
176
178 long long m_activeCellOldSelStart = -1;
179 long long m_activeCellOldSelEnd = -1;
180};
181
182#endif // TREEUNDOMANAGER_H
Declares TreeUndoAction, the immutable record of one cell-tree undo/redo step, and the UndoActions li...
std::list< TreeUndoAction > UndoActions
The type of the list of tree actions that can be undone.
Definition: TreeUndoAction.h:120
A weak non-owning pointer that becomes null whenever the observed object is destroyed.
Definition: CellPtr.h:511
A cell grouping input (and, if there is one, also the output) cell to a foldable item.
Definition: GroupCell.h:88
Owns the cell-tree undo/redo stacks and the active-cell snapshot.
Definition: TreeUndoManager.h:51
GroupCell * ActiveCell() const
The cell the cursor is in, as recorded by CellEntered().
Definition: TreeUndoManager.h:157
bool CellLeft(GroupCell *activeCell, const wxString &currentText)
The cursor is leaving a cell: record a text-change undo action if needed.
Definition: TreeUndoManager.h:134
void CellEntered(GroupCell *group, const wxString &text, long long selStart, long long selEnd)
The cursor has entered a cell: snapshot its contents.
Definition: TreeUndoManager.h:115
void ClearUndoActionList()
Clear the list of actions that can be undone.
Definition: TreeUndoManager.h:94
void ClearBuffers()
Clear both action lists and forget the active-cell snapshot.
Definition: TreeUndoManager.h:101
UndoActions & UndoStack()
The list of tree actions that can be undone.
Definition: TreeUndoManager.h:54
void ForgetActiveCell()
Forget the active-cell snapshot (e.g. the next change must not be undoable).
Definition: TreeUndoManager.h:154
static void DiscardAction(UndoActions *actionList)
Remove one action from an action list.
Definition: TreeUndoManager.h:67
UndoActions & RedoStack()
The list of undone tree actions that can be redone.
Definition: TreeUndoManager.h:58
void ClearRedoActionList()
Clear the list of actions that can be redone.
Definition: TreeUndoManager.h:87
void AppendAction()
Mark the newest undo action as part of the following atomic action.
Definition: TreeUndoManager.h:84
static void AppendAction(UndoActions *actionList)
Mark the newest action of the list as part of the following atomic action.
Definition: TreeUndoManager.h:77