2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License
4 * as published by the Free Software Foundation; either version 2
5 * of the License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software Foundation,
14 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 /** \file blender/editors/space_text/text_undo.c
24 #include "MEM_guardedalloc.h"
26 #include "DNA_text_types.h"
28 #include "BLI_array_utils.h"
30 #include "BLT_translation.h"
34 #include "BKE_context.h"
35 #include "BKE_report.h"
37 #include "BKE_undo_system.h"
44 #include "ED_screen.h"
47 #include "UI_interface.h"
48 #include "UI_resources.h"
50 #include "RNA_access.h"
51 #include "RNA_define.h"
53 #include "text_intern.h"
54 #include "text_format.h"
56 /* TODO(campbell): undo_system: move text undo out of text block. */
58 /* -------------------------------------------------------------------- */
59 /** \name Implements ED Undo System
62 typedef struct TextUndoStep {
64 UndoRefID_Text text_ref;
68 static bool text_undosys_poll(bContext *C)
70 Text *text = CTX_data_edit_text(C);
74 if (ID_IS_LINKED(text)) {
80 static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
82 TextUndoStep *us = (TextUndoStep *)us_p;
83 BLI_assert(BLI_array_is_zeroed(&us->data, 1));
86 /* XXX, use to set the undo type only. */
93 static bool text_undosys_step_encode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p)
95 TextUndoStep *us = (TextUndoStep *)us_p;
97 Text *text = CTX_data_edit_text(C);
99 /* No undo data was generated. Hint, use global undo here. */
100 if ((us->data.pos == -1) || (us->data.buf == NULL)) {
104 us_p->is_applied = true;
106 us->text_ref.ptr = text;
108 us->step.data_size = us->data.len;
114 static void text_undosys_step_decode_undo_impl(Text *text, TextUndoStep *us)
116 BLI_assert(us->step.is_applied == true);
117 TextUndoBuf data = us->data;
118 while (data.pos > -1) {
119 txt_do_undo(text, &data);
121 BLI_assert(data.pos == -1);
122 us->step.is_applied = false;
125 static void text_undosys_step_decode_redo_impl(Text *text, TextUndoStep *us)
127 BLI_assert(us->step.is_applied == false);
128 TextUndoBuf data = us->data;
130 while (data.pos < us->data.pos) {
131 txt_do_redo(text, &data);
133 BLI_assert(data.pos == us->data.pos);
134 us->step.is_applied = true;
137 static void text_undosys_step_decode_undo(Text *text, TextUndoStep *us)
139 TextUndoStep *us_iter = us;
140 while (us_iter->step.next && (us_iter->step.next->type == us_iter->step.type)) {
141 if (us_iter->step.next->is_applied == false) {
144 us_iter = (TextUndoStep *)us_iter->step.next;
146 while (us_iter != us) {
147 text_undosys_step_decode_undo_impl(text, us_iter);
148 us_iter = (TextUndoStep *)us_iter->step.prev;
152 static void text_undosys_step_decode_redo(Text *text, TextUndoStep *us)
154 TextUndoStep *us_iter = us;
155 while (us_iter->step.prev && (us_iter->step.prev->type == us_iter->step.type)) {
156 if (us_iter->step.prev->is_applied == true) {
159 us_iter = (TextUndoStep *)us_iter->step.prev;
161 while (us_iter && (us_iter->step.is_applied == false)) {
162 text_undosys_step_decode_redo_impl(text, us_iter);
166 us_iter = (TextUndoStep *)us_iter->step.next;
170 static void text_undosys_step_decode(struct bContext *C, struct Main *UNUSED(bmain), UndoStep *us_p, int dir)
172 TextUndoStep *us = (TextUndoStep *)us_p;
173 Text *text = us->text_ref.ptr;
176 text_undosys_step_decode_undo(text, us);
179 text_undosys_step_decode_redo(text, us);
182 SpaceText *st = CTX_wm_space_text(C);
184 /* Not essential, always show text being undo where possible. */
187 text_update_edited(text);
188 text_update_cursor_moved(C);
189 text_drawcache_tag_update(st, 1);
190 WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
193 static void text_undosys_step_free(UndoStep *us_p)
195 TextUndoStep *us = (TextUndoStep *)us_p;
196 MEM_SAFE_FREE(us->data.buf);
199 static void text_undosys_foreach_ID_ref(
200 UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
202 TextUndoStep *us = (TextUndoStep *)us_p;
203 foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->text_ref));
206 /* Export for ED_undo_sys. */
208 void ED_text_undosys_type(UndoType *ut)
211 ut->poll = text_undosys_poll;
212 ut->step_encode_init = text_undosys_step_encode_init;
213 ut->step_encode = text_undosys_step_encode;
214 ut->step_decode = text_undosys_step_decode;
215 ut->step_free = text_undosys_step_free;
217 ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref;
219 ut->use_context = false;
221 ut->step_size = sizeof(TextUndoStep);
226 /* -------------------------------------------------------------------- */
230 /* Use operator system to finish the undo step. */
231 TextUndoBuf *ED_text_undo_push_init(bContext *C)
233 UndoStack *ustack = ED_undo_stack_get();
234 UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT);
235 TextUndoStep *us = (TextUndoStep *)us_p;