2018-03-19 14:17:59 +01:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup sptext
|
2018-03-19 14:17:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include "MEM_guardedalloc.h"
|
|
|
|
|
|
|
|
#include "DNA_text_types.h"
|
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
#include "BLI_array_store.h"
|
2018-03-19 14:17:59 +01:00
|
|
|
#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"
|
2019-07-11 15:25:52 +10:00
|
|
|
#include "BKE_main.h"
|
2018-03-19 14:17:59 +01:00
|
|
|
|
|
|
|
#include "WM_api.h"
|
|
|
|
#include "WM_types.h"
|
|
|
|
|
|
|
|
#include "ED_text.h"
|
|
|
|
#include "ED_curve.h"
|
|
|
|
#include "ED_screen.h"
|
2018-04-05 15:22:33 +02:00
|
|
|
#include "ED_undo.h"
|
2018-03-19 14:17:59 +01:00
|
|
|
|
|
|
|
#include "UI_interface.h"
|
|
|
|
#include "UI_resources.h"
|
|
|
|
|
|
|
|
#include "RNA_access.h"
|
|
|
|
#include "RNA_define.h"
|
|
|
|
|
|
|
|
#include "text_intern.h"
|
|
|
|
#include "text_format.h"
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Implements ED Undo System
|
|
|
|
* \{ */
|
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
#define ARRAY_CHUNK_SIZE 128
|
|
|
|
|
2018-03-19 14:17:59 +01:00
|
|
|
typedef struct TextUndoStep {
|
2019-04-17 06:17:24 +02:00
|
|
|
UndoStep step;
|
|
|
|
UndoRefID_Text text_ref;
|
2019-07-11 15:25:52 +10:00
|
|
|
struct {
|
|
|
|
BArrayState *state;
|
|
|
|
int buf_len;
|
|
|
|
} data;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
int line, line_select;
|
|
|
|
int column, column_select;
|
|
|
|
} cursor;
|
|
|
|
|
2018-03-19 14:17:59 +01:00
|
|
|
} TextUndoStep;
|
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
static struct {
|
|
|
|
BArrayStore *buffer_store;
|
|
|
|
int users;
|
|
|
|
} g_text_buffers = {NULL};
|
|
|
|
|
2019-02-06 12:55:26 +11:00
|
|
|
static bool text_undosys_poll(bContext *UNUSED(C))
|
2018-03-19 14:17:59 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
/* Only use when operators initialized. */
|
|
|
|
UndoStack *ustack = ED_undo_stack_get();
|
|
|
|
return (ustack->step_init && (ustack->step_init->type == BKE_UNDOSYS_TYPE_TEXT));
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 15:22:33 +02:00
|
|
|
static void text_undosys_step_encode_init(struct bContext *C, UndoStep *us_p)
|
2018-03-19 14:17:59 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextUndoStep *us = (TextUndoStep *)us_p;
|
|
|
|
BLI_assert(BLI_array_is_zeroed(&us->data, 1));
|
2018-04-05 09:59:21 +02:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
UNUSED_VARS(C, us);
|
2019-04-17 06:17:24 +02:00
|
|
|
/* XXX, use to set the undo type only. */
|
2018-04-05 15:22:33 +02:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static bool text_undosys_step_encode(struct bContext *C,
|
|
|
|
struct Main *UNUSED(bmain),
|
|
|
|
UndoStep *us_p)
|
2018-04-05 15:22:33 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextUndoStep *us = (TextUndoStep *)us_p;
|
2018-04-05 15:22:33 +02:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
Text *text = CTX_data_edit_text(C);
|
2018-03-19 14:17:59 +01:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
int buf_len = 0;
|
|
|
|
|
|
|
|
uchar *buf = (uchar *)txt_to_buf_for_undo(text, &buf_len);
|
|
|
|
if (g_text_buffers.buffer_store == NULL) {
|
|
|
|
g_text_buffers.buffer_store = BLI_array_store_create(1, ARRAY_CHUNK_SIZE);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-07-11 15:25:52 +10:00
|
|
|
g_text_buffers.users += 1;
|
|
|
|
const size_t total_size_prev = BLI_array_store_calc_size_compacted_get(
|
|
|
|
g_text_buffers.buffer_store);
|
2018-04-05 09:59:21 +02:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
us->data.state = BLI_array_store_state_add(g_text_buffers.buffer_store, buf, buf_len, NULL);
|
|
|
|
MEM_freeN(buf);
|
2019-02-05 14:24:11 +11:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
us->cursor.line = txt_get_span(text->lines.first, text->curl);
|
|
|
|
us->cursor.column = text->curc;
|
2018-03-19 14:17:59 +01:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
if (txt_has_sel(text)) {
|
|
|
|
us->cursor.line_select = (text->curl == text->sell) ?
|
|
|
|
us->cursor.line :
|
|
|
|
txt_get_span(text->lines.first, text->sell);
|
|
|
|
us->cursor.column_select = text->selc;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-07-11 15:25:52 +10:00
|
|
|
else {
|
|
|
|
us->cursor.line_select = us->cursor.line;
|
|
|
|
us->cursor.column_select = us->cursor.column;
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-02-05 14:24:11 +11:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
us_p->is_applied = true;
|
2019-02-05 14:24:11 +11:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
us->text_ref.ptr = text;
|
|
|
|
|
|
|
|
us->step.data_size = BLI_array_store_calc_size_compacted_get(g_text_buffers.buffer_store) -
|
|
|
|
total_size_prev;
|
|
|
|
|
|
|
|
return true;
|
2019-02-05 14:24:11 +11:00
|
|
|
}
|
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
static void text_undosys_step_decode(struct bContext *C,
|
|
|
|
struct Main *UNUSED(bmain),
|
|
|
|
UndoStep *us_p,
|
|
|
|
int UNUSED(dir),
|
|
|
|
bool UNUSED(is_final))
|
2018-03-19 14:17:59 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextUndoStep *us = (TextUndoStep *)us_p;
|
2019-07-11 15:25:52 +10:00
|
|
|
Text *text = us->text_ref.ptr;
|
|
|
|
size_t buf_len;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-07-11 15:25:52 +10:00
|
|
|
{
|
|
|
|
const uchar *buf = BLI_array_store_state_data_get_alloc(us->data.state, &buf_len);
|
|
|
|
txt_from_buf_for_undo(text, (const char *)buf, buf_len);
|
|
|
|
MEM_freeN((void *)buf);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-07-11 15:25:52 +10:00
|
|
|
|
|
|
|
const bool has_select = ((us->cursor.line != us->cursor.line_select) ||
|
|
|
|
(us->cursor.column != us->cursor.column_select));
|
|
|
|
if (has_select) {
|
|
|
|
txt_move_to(text, us->cursor.line_select, us->cursor.column_select, false);
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2019-07-11 15:25:52 +10:00
|
|
|
txt_move_to(text, us->cursor.line, us->cursor.column, has_select);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
|
|
|
SpaceText *st = CTX_wm_space_text(C);
|
|
|
|
if (st) {
|
|
|
|
/* Not essential, always show text being undo where possible. */
|
|
|
|
st->text = text;
|
|
|
|
}
|
|
|
|
text_update_cursor_moved(C);
|
|
|
|
text_drawcache_tag_update(st, 1);
|
|
|
|
WM_event_add_notifier(C, NC_TEXT | NA_EDITED, text);
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void text_undosys_step_free(UndoStep *us_p)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextUndoStep *us = (TextUndoStep *)us_p;
|
2019-07-11 15:25:52 +10:00
|
|
|
|
|
|
|
BLI_array_store_state_remove(g_text_buffers.buffer_store, us->data.state);
|
|
|
|
|
|
|
|
g_text_buffers.users -= 1;
|
|
|
|
if (g_text_buffers.users == 0) {
|
|
|
|
BLI_array_store_destroy(g_text_buffers.buffer_store);
|
|
|
|
g_text_buffers.buffer_store = NULL;
|
|
|
|
}
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
static void text_undosys_foreach_ID_ref(UndoStep *us_p,
|
|
|
|
UndoTypeForEachIDRefFn foreach_ID_ref_fn,
|
|
|
|
void *user_data)
|
2018-03-19 14:17:59 +01:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
TextUndoStep *us = (TextUndoStep *)us_p;
|
|
|
|
foreach_ID_ref_fn(user_data, ((UndoRefID *)&us->text_ref));
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Export for ED_undo_sys. */
|
|
|
|
|
|
|
|
void ED_text_undosys_type(UndoType *ut)
|
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
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;
|
2018-03-19 14:17:59 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
ut->step_foreach_ID_ref = text_undosys_foreach_ID_ref;
|
2018-03-19 14:17:59 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
ut->use_context = false;
|
2018-03-19 14:17:59 +01:00
|
|
|
|
2019-04-17 06:17:24 +02:00
|
|
|
ut->step_size = sizeof(TextUndoStep);
|
2018-03-19 14:17:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|
2018-04-05 15:22:33 +02:00
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Utilities
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
/* Use operator system to finish the undo step. */
|
2019-07-11 15:25:52 +10:00
|
|
|
UndoStep *ED_text_undo_push_init(bContext *C)
|
2018-04-05 15:22:33 +02:00
|
|
|
{
|
2019-04-17 06:17:24 +02:00
|
|
|
UndoStack *ustack = ED_undo_stack_get();
|
2019-07-11 15:25:52 +10:00
|
|
|
Main *bmain = CTX_data_main(C);
|
|
|
|
wmWindowManager *wm = bmain->wm.first;
|
|
|
|
if (wm->op_undo_depth <= 1) {
|
|
|
|
UndoStep *us_p = BKE_undosys_step_push_init_with_type(ustack, C, NULL, BKE_UNDOSYS_TYPE_TEXT);
|
|
|
|
return us_p;
|
|
|
|
}
|
|
|
|
return NULL;
|
2018-04-05 15:22:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** \} */
|