Welcome to mirror list, hosted at ThFree Co, Russian Federation.

text_undo.c « space_text « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b427ce43dbbc9b89ed7d637b7c4d7013ca52ea93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/space_text/text_undo.c
 *  \ingroup sptext
 */

#include <string.h>
#include <errno.h>

#include "MEM_guardedalloc.h"

#include "DNA_text_types.h"

#include "BLI_listbase.h"
#include "BLI_array_utils.h"

#include "BLT_translation.h"

#include "PIL_time.h"

#include "BKE_context.h"
#include "BKE_report.h"
#include "BKE_text.h"
#include "BKE_undo_system.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_text.h"
#include "ED_curve.h"
#include "ED_screen.h"
#include "ED_undo.h"

#include "UI_interface.h"
#include "UI_resources.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "text_intern.h"
#include "text_format.h"

/* TODO(campbell): undo_system: move text undo out of text block. */

/* -------------------------------------------------------------------- */
/** \name Implements ED Undo System
 * \{ */

typedef struct TextUndoStep {
	UndoStep step;
	UndoRefID_Text text_ref;
	TextUndoBuf data;
} TextUndoStep;

static bool text_undosys_poll(bContext *C)
{
	Text *text = CTX_data_edit_text(C);
	if (text == NULL) {
		return false;
	}
	if (ID_IS_LINKED(text)) {
		return false;
	}
	return true;
}

static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
{
	TextUndoStep *us = (TextUndoStep *)us_p;
	BLI_assert(BLI_array_is_zeroed(&us->data, 1));

	UNUSED_VARS(C);
	/* XXX, use to set the undo type only. */

	us->data.buf = NULL;
	us->data.len = 0;
	us->data.pos = -1;
}

static bool text_undosys_step_encode(struct bContext *C, UndoStep *us_p)
{
	TextUndoStep *us = (TextUndoStep *)us_p;

	Text *text = CTX_data_edit_text(C);

	/* No undo data was generated. Hint, use global undo here. */
	if ((us->data.pos == -1) || (us->data.buf == NULL)) {
		return false;
	}

	us->text_ref.ptr = text;

	us->step.data_size = us->data.len;

	return true;
}

static void text_undosys_step_decode(struct bContext *C, UndoStep *us_p, int dir)
{
	TextUndoStep *us = (TextUndoStep *)us_p;
	Text *text = us->text_ref.ptr;

	if (dir < 0) {
		TextUndoBuf data = us->data;
		while (data.pos > -1) {
			txt_do_undo(text, &data);
		}
		BLI_assert(data.pos == -1);
	}
	else {
		TextUndoBuf data = us->data;
		data.pos = -1;
		while (data.pos < us->data.pos) {
			txt_do_redo(text, &data);
		}
		BLI_assert(data.pos == us->data.pos);
	}

	SpaceText *st = CTX_wm_space_text(C);
	if (st) {
		/* Not essential, always show text being undo where possible. */
		st->text = text;
	}
	text_update_edited(text);
	text_update_cursor_moved(C);
	text_drawcache_tag_update(st, 1);
	WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
}

static void text_undosys_step_free(UndoStep *us_p)
{
	TextUndoStep *us = (TextUndoStep *)us_p;
	MEM_SAFE_FREE(us->data.buf);
}

static void text_undosys_foreach_ID_ref(
        UndoStep *us_p, UndoTypeForEachIDRefFn foreach_ID_ref_fn, void *user_data)
{
	TextUndoStep *us = (TextUndoStep *)us_p;
	foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->text_ref));
}

/* Export for ED_undo_sys. */

void ED_text_undosys_type(UndoType *ut)
{
	ut->name = "Text";
	ut->poll = text_undosys_poll;
	ut->step_encode_init = text_undosys_step_encode_init;
	ut->step_encode = text_undosys_step_encode;
	ut->step_decode = text_undosys_step_decode;
	ut->step_free = text_undosys_step_free;

	ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref;

	ut->mode = BKE_UNDOTYPE_MODE_ACCUMULATE;
	ut->use_context = false;

	ut->step_size = sizeof(TextUndoStep);
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Utilities
 * \{ */

/* Use operator system to finish the undo step. */
TextUndoBuf *ED_text_undo_push_init(bContext *C)
{
	UndoStack *ustack = ED_undo_stack_get();
	UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT);
	TextUndoStep *us = (TextUndoStep *)us_p;
	return &us->data;
}

/** \} */