From 4ffa05c30b10eb8a07dd0a9b43466f6d11bc1364 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Mon, 2 Apr 2018 15:02:08 +0200 Subject: Cleanup: move undo into it's own directory Split out undo API from ED_util.h into ED_undo.h --- source/blender/editors/CMakeLists.txt | 1 + source/blender/editors/animation/fmodifier_ui.c | 2 +- .../editors/armature/editarmature_retarget.c | 2 +- source/blender/editors/curve/editcurve_add.c | 2 +- source/blender/editors/include/ED_undo.h | 62 +++ source/blender/editors/include/ED_util.h | 34 +- .../blender/editors/interface/interface_handlers.c | 2 +- .../editors/interface/interface_templates.c | 2 +- source/blender/editors/object/object_edit.c | 2 +- source/blender/editors/render/render_internal.c | 1 + source/blender/editors/screen/screen_ops.c | 1 + source/blender/editors/space_buttons/buttons_ops.c | 2 +- source/blender/editors/space_clip/clip_toolbar.c | 2 +- source/blender/editors/space_graph/graph_buttons.c | 2 +- source/blender/editors/space_logic/logic_window.c | 2 +- source/blender/editors/space_node/node_templates.c | 2 +- .../editors/space_outliner/outliner_select.c | 2 +- .../editors/space_outliner/outliner_tools.c | 2 +- .../blender/editors/space_view3d/view3d_header.c | 2 +- .../blender/editors/space_view3d/view3d_toolbar.c | 2 +- source/blender/editors/undo/CMakeLists.txt | 45 ++ source/blender/editors/undo/ed_undo.c | 479 +++++++++++++++++++++ source/blender/editors/undo/memfile_undo.c | 149 +++++++ source/blender/editors/undo/undo_intern.h | 35 ++ source/blender/editors/undo/undo_system_types.c | 74 ++++ source/blender/editors/util/CMakeLists.txt | 4 - source/blender/editors/util/memfile_undo.c | 149 ------- source/blender/editors/util/undo.c | 478 -------------------- source/blender/editors/util/undo_system_types.c | 74 ---- source/blender/editors/util/util_intern.h | 44 -- .../blender/windowmanager/intern/wm_event_system.c | 1 + source/blender/windowmanager/intern/wm_files.c | 1 + source/blender/windowmanager/intern/wm_init_exit.c | 1 + source/blender/windowmanager/intern/wm_operators.c | 2 +- 34 files changed, 867 insertions(+), 798 deletions(-) create mode 100644 source/blender/editors/include/ED_undo.h create mode 100644 source/blender/editors/undo/CMakeLists.txt create mode 100644 source/blender/editors/undo/ed_undo.c create mode 100644 source/blender/editors/undo/memfile_undo.c create mode 100644 source/blender/editors/undo/undo_intern.h create mode 100644 source/blender/editors/undo/undo_system_types.c delete mode 100644 source/blender/editors/util/memfile_undo.c delete mode 100644 source/blender/editors/util/undo.c delete mode 100644 source/blender/editors/util/undo_system_types.c delete mode 100644 source/blender/editors/util/util_intern.h (limited to 'source/blender') diff --git a/source/blender/editors/CMakeLists.txt b/source/blender/editors/CMakeLists.txt index be8829b0418..17a8eef1bdb 100644 --- a/source/blender/editors/CMakeLists.txt +++ b/source/blender/editors/CMakeLists.txt @@ -59,6 +59,7 @@ if(WITH_BLENDER) add_subdirectory(space_userpref) add_subdirectory(space_view3d) add_subdirectory(transform) + add_subdirectory(undo) add_subdirectory(util) add_subdirectory(uvedit) endif() diff --git a/source/blender/editors/animation/fmodifier_ui.c b/source/blender/editors/animation/fmodifier_ui.c index 8d77460e197..8106be79521 100644 --- a/source/blender/editors/animation/fmodifier_ui.c +++ b/source/blender/editors/animation/fmodifier_ui.c @@ -62,7 +62,7 @@ #include "UI_resources.h" #include "ED_anim_api.h" -#include "ED_util.h" +#include "ED_undo.h" /* ********************************************** */ /* UI STUFF */ diff --git a/source/blender/editors/armature/editarmature_retarget.c b/source/blender/editors/armature/editarmature_retarget.c index 2fb216c2ef8..3dd41f25e09 100644 --- a/source/blender/editors/armature/editarmature_retarget.c +++ b/source/blender/editors/armature/editarmature_retarget.c @@ -42,7 +42,7 @@ #include "BKE_context.h" #include "ED_armature.h" -#include "ED_util.h" +#include "ED_undo.h" #include "BIF_retarget.h" diff --git a/source/blender/editors/curve/editcurve_add.c b/source/blender/editors/curve/editcurve_add.c index cc8e272d4f7..b23f0f967ec 100644 --- a/source/blender/editors/curve/editcurve_add.c +++ b/source/blender/editors/curve/editcurve_add.c @@ -52,7 +52,7 @@ #include "ED_object.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "ED_view3d.h" #include "ED_curve.h" diff --git a/source/blender/editors/include/ED_undo.h b/source/blender/editors/include/ED_undo.h new file mode 100644 index 00000000000..044d69cfced --- /dev/null +++ b/source/blender/editors/include/ED_undo.h @@ -0,0 +1,62 @@ +/* + * ***** 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 ED_undo.h + * \ingroup editors + */ + +#ifndef __ED_UNDO_H__ +#define __ED_UNDO_H__ + +struct bContext; +struct wmOperator; +struct wmOperatorType; +struct UndoStack; + +/* undo.c */ +void ED_undo_push(struct bContext *C, const char *str); +void ED_undo_push_op(struct bContext *C, struct wmOperator *op); +void ED_undo_grouped_push(struct bContext *C, const char *str); +void ED_undo_grouped_push_op(struct bContext *C, struct wmOperator *op); +void ED_undo_pop_op(struct bContext *C, struct wmOperator *op); +void ED_undo_pop(struct bContext *C); +void ED_undo_redo(struct bContext *C); +void ED_OT_undo(struct wmOperatorType *ot); +void ED_OT_undo_push(struct wmOperatorType *ot); +void ED_OT_redo(struct wmOperatorType *ot); +void ED_OT_undo_redo(struct wmOperatorType *ot); +void ED_OT_undo_history(struct wmOperatorType *ot); + +int ED_undo_operator_repeat(struct bContext *C, struct wmOperator *op); +/* convenience since UI callbacks use this mostly*/ +void ED_undo_operator_repeat_cb(struct bContext *C, void *arg_op, void *arg_unused); +void ED_undo_operator_repeat_cb_evt(struct bContext *C, void *arg_op, int arg_unused); + +bool ED_undo_is_valid(const struct bContext *C, const char *undoname); + +/* undo_system_types.c */ +void ED_undosys_type_init(void); +void ED_undosys_type_free(void); + +/* memfile_undo.c */ +struct MemFile *ED_undosys_stack_memfile_get_active(struct UndoStack *ustack); + +#endif /* __ED_UNDO_H__ */ + diff --git a/source/blender/editors/include/ED_util.h b/source/blender/editors/include/ED_util.h index 5a373cebac1..2653585dacc 100644 --- a/source/blender/editors/include/ED_util.h +++ b/source/blender/editors/include/ED_util.h @@ -32,11 +32,9 @@ #define __ED_UTIL_H__ struct bContext; -struct SpaceLink; -struct wmOperator; struct wmOperatorType; -struct UndoStack; struct ScrArea; +struct SpaceLink; struct PackedFile; /* ed_util.c */ @@ -50,36 +48,6 @@ void ED_spacedata_id_remap(struct ScrArea *sa, struct SpaceLink *sl, struct I void ED_OT_flush_edits(struct wmOperatorType *ot); -/* ************** Undo ************************ */ - -/* undo.c */ -void ED_undo_push(struct bContext *C, const char *str); -void ED_undo_push_op(struct bContext *C, struct wmOperator *op); -void ED_undo_grouped_push(struct bContext *C, const char *str); -void ED_undo_grouped_push_op(struct bContext *C, struct wmOperator *op); -void ED_undo_pop_op(struct bContext *C, struct wmOperator *op); -void ED_undo_pop(struct bContext *C); -void ED_undo_redo(struct bContext *C); -void ED_OT_undo(struct wmOperatorType *ot); -void ED_OT_undo_push(struct wmOperatorType *ot); -void ED_OT_redo(struct wmOperatorType *ot); -void ED_OT_undo_redo(struct wmOperatorType *ot); -void ED_OT_undo_history(struct wmOperatorType *ot); - -int ED_undo_operator_repeat(struct bContext *C, struct wmOperator *op); -/* convenience since UI callbacks use this mostly*/ -void ED_undo_operator_repeat_cb(struct bContext *C, void *arg_op, void *arg_unused); -void ED_undo_operator_repeat_cb_evt(struct bContext *C, void *arg_op, int arg_unused); - -bool ED_undo_is_valid(const struct bContext *C, const char *undoname); - -/* undo_system_types.c */ -void ED_undosys_type_init(void); -void ED_undosys_type_free(void); - -/* memfile_undo.c */ -struct MemFile *ED_undosys_stack_memfile_get_active(struct UndoStack *ustack); - /* ************** XXX OLD CRUFT WARNING ************* */ void apply_keyb_grid(int shift, int ctrl, float *val, float fac1, float fac2, float fac3, int invert); diff --git a/source/blender/editors/interface/interface_handlers.c b/source/blender/editors/interface/interface_handlers.c index bf39a8d353f..9f7cd62227c 100644 --- a/source/blender/editors/interface/interface_handlers.c +++ b/source/blender/editors/interface/interface_handlers.c @@ -74,7 +74,7 @@ #include "BKE_paint.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "ED_keyframing.h" #include "UI_interface.h" diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c index 82a502db0ad..b92216138fd 100644 --- a/source/blender/editors/interface/interface_templates.c +++ b/source/blender/editors/interface/interface_templates.c @@ -75,7 +75,7 @@ #include "ED_screen.h" #include "ED_object.h" #include "ED_render.h" -#include "ED_util.h" +#include "ED_undo.h" #include "RNA_access.h" diff --git a/source/blender/editors/object/object_edit.c b/source/blender/editors/object/object_edit.c index cbdc9685f2c..c83e25fa9c2 100644 --- a/source/blender/editors/object/object_edit.c +++ b/source/blender/editors/object/object_edit.c @@ -94,7 +94,7 @@ #include "ED_lattice.h" #include "ED_object.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "ED_image.h" #include "RNA_access.h" diff --git a/source/blender/editors/render/render_internal.c b/source/blender/editors/render/render_internal.c index 92fb140243f..d81b85f1414 100644 --- a/source/blender/editors/render/render_internal.c +++ b/source/blender/editors/render/render_internal.c @@ -74,6 +74,7 @@ #include "ED_render.h" #include "ED_screen.h" #include "ED_util.h" +#include "ED_undo.h" #include "ED_view3d.h" #include "RE_pipeline.h" diff --git a/source/blender/editors/screen/screen_ops.c b/source/blender/editors/screen/screen_ops.c index 2bf2c532ded..62113cafebb 100644 --- a/source/blender/editors/screen/screen_ops.c +++ b/source/blender/editors/screen/screen_ops.c @@ -74,6 +74,7 @@ #include "ED_screen_types.h" #include "ED_sequencer.h" #include "ED_util.h" +#include "ED_undo.h" #include "ED_view3d.h" #include "RNA_access.h" diff --git a/source/blender/editors/space_buttons/buttons_ops.c b/source/blender/editors/space_buttons/buttons_ops.c index fc1b6877f5e..2a703ebb46c 100644 --- a/source/blender/editors/space_buttons/buttons_ops.c +++ b/source/blender/editors/space_buttons/buttons_ops.c @@ -51,7 +51,7 @@ #include "WM_types.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "RNA_access.h" diff --git a/source/blender/editors/space_clip/clip_toolbar.c b/source/blender/editors/space_clip/clip_toolbar.c index 1504ce1a7ba..d2a7244eded 100644 --- a/source/blender/editors/space_clip/clip_toolbar.c +++ b/source/blender/editors/space_clip/clip_toolbar.c @@ -49,7 +49,7 @@ #include "WM_api.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "UI_interface.h" #include "UI_resources.h" diff --git a/source/blender/editors/space_graph/graph_buttons.c b/source/blender/editors/space_graph/graph_buttons.c index df6bd01144d..1ada5538fe3 100644 --- a/source/blender/editors/space_graph/graph_buttons.c +++ b/source/blender/editors/space_graph/graph_buttons.c @@ -64,7 +64,7 @@ #include "ED_anim_api.h" #include "ED_keyframing.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "UI_interface.h" #include "UI_resources.h" diff --git a/source/blender/editors/space_logic/logic_window.c b/source/blender/editors/space_logic/logic_window.c index e1a31be4afb..4dd3f369204 100644 --- a/source/blender/editors/space_logic/logic_window.c +++ b/source/blender/editors/space_logic/logic_window.c @@ -56,7 +56,7 @@ #include "BKE_main.h" #include "BKE_sca.h" -#include "ED_util.h" +#include "ED_undo.h" #include "BLT_translation.h" diff --git a/source/blender/editors/space_node/node_templates.c b/source/blender/editors/space_node/node_templates.c index 508a16926e6..173c919e38c 100644 --- a/source/blender/editors/space_node/node_templates.c +++ b/source/blender/editors/space_node/node_templates.c @@ -53,7 +53,7 @@ #include "ED_node.h" /* own include */ -#include "ED_util.h" +#include "ED_undo.h" /************************* Node Socket Manipulation **************************/ diff --git a/source/blender/editors/space_outliner/outliner_select.c b/source/blender/editors/space_outliner/outliner_select.c index 12b9d273a47..7166e870f6e 100644 --- a/source/blender/editors/space_outliner/outliner_select.c +++ b/source/blender/editors/space_outliner/outliner_select.c @@ -54,7 +54,7 @@ #include "ED_object.h" #include "ED_screen.h" #include "ED_sequencer.h" -#include "ED_util.h" +#include "ED_undo.h" #include "WM_api.h" #include "WM_types.h" diff --git a/source/blender/editors/space_outliner/outliner_tools.c b/source/blender/editors/space_outliner/outliner_tools.c index cf9deeaedf8..84711bf7c3c 100644 --- a/source/blender/editors/space_outliner/outliner_tools.c +++ b/source/blender/editors/space_outliner/outliner_tools.c @@ -68,7 +68,7 @@ #include "ED_object.h" #include "ED_screen.h" #include "ED_sequencer.h" -#include "ED_util.h" +#include "ED_undo.h" #include "WM_api.h" #include "WM_types.h" diff --git a/source/blender/editors/space_view3d/view3d_header.c b/source/blender/editors/space_view3d/view3d_header.c index 11dc4d10f2a..d80ef486663 100644 --- a/source/blender/editors/space_view3d/view3d_header.c +++ b/source/blender/editors/space_view3d/view3d_header.c @@ -54,7 +54,7 @@ #include "WM_types.h" #include "ED_mesh.h" -#include "ED_util.h" +#include "ED_undo.h" #include "ED_screen.h" #include "UI_interface.h" diff --git a/source/blender/editors/space_view3d/view3d_toolbar.c b/source/blender/editors/space_view3d/view3d_toolbar.c index 5e3c783c1b6..dfa64bd2015 100644 --- a/source/blender/editors/space_view3d/view3d_toolbar.c +++ b/source/blender/editors/space_view3d/view3d_toolbar.c @@ -55,7 +55,7 @@ #include "RNA_access.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "UI_interface.h" #include "UI_resources.h" diff --git a/source/blender/editors/undo/CMakeLists.txt b/source/blender/editors/undo/CMakeLists.txt new file mode 100644 index 00000000000..89832604ed8 --- /dev/null +++ b/source/blender/editors/undo/CMakeLists.txt @@ -0,0 +1,45 @@ +# ***** 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. +# +# Contributor(s): Jacques Beaurain. +# +# ***** END GPL LICENSE BLOCK ***** + +set(INC + ../include + ../../blenkernel + ../../blenlib + ../../blentranslation + ../../makesdna + ../../makesrna + ../../windowmanager + ../../../../intern/guardedalloc + ../../../../intern/clog +) + +set(SRC + ed_undo.c + memfile_undo.c + undo_system_types.c + + undo_intern.h +) + +if(WITH_INTERNATIONAL) + add_definitions(-DWITH_INTERNATIONAL) +endif() + +blender_add_lib(bf_editor_undo "${SRC}" "${INC}" "${INC_SYS}") diff --git a/source/blender/editors/undo/ed_undo.c b/source/blender/editors/undo/ed_undo.c new file mode 100644 index 00000000000..691ba066bf9 --- /dev/null +++ b/source/blender/editors/undo/ed_undo.c @@ -0,0 +1,479 @@ +/* + * ***** 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. + * + * The Original Code is Copyright (C) 2004 Blender Foundation + * All rights reserved. + * + * The Original Code is: all of this file. + * + * Contributor(s): none yet. + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/editors/util/undo.c + * \ingroup edundo + */ + +#include + +#include "MEM_guardedalloc.h" + +#include "DNA_scene_types.h" + +#include "BLI_utildefines.h" + +#include "BLT_translation.h" + +#include "BKE_blender_undo.h" +#include "BKE_context.h" +#include "BKE_global.h" +#include "BKE_screen.h" +#include "BKE_undo_system.h" + +#include "ED_gpencil.h" +#include "ED_render.h" +#include "ED_screen.h" +#include "ED_undo.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "RNA_access.h" +#include "RNA_define.h" + +#include "UI_interface.h" +#include "UI_resources.h" + +/* -------------------------------------------------------------------- */ +/** \name Generic Undo System Access + * + * Non-operator undo editor functions. + * \{ */ + +void ED_undo_push(bContext *C, const char *str) +{ + if (G.debug & G_DEBUG) { + printf("%s: %s\n", __func__, str); + } + const int steps = U.undosteps; + + if (steps <= 0) { + return; + } + + wmWindowManager *wm = CTX_wm_manager(C); + + /* Only apply limit if this is the last undo step. */ + if (wm->undo_stack->step_active && (wm->undo_stack->step_active->next == NULL)) { + BKE_undosys_stack_limit_steps_and_memory(wm->undo_stack, steps - 1, 0); + } + + BKE_undosys_step_push(wm->undo_stack, C, str); + + if (U.undomemory != 0) { + const size_t memory_limit = (size_t)U.undomemory * 1024 * 1024; + BKE_undosys_stack_limit_steps_and_memory(wm->undo_stack, 0, memory_limit); + } + + WM_file_tag_modified(); +} + +/* note: also check undo_history_exec() in bottom if you change notifiers */ +static int ed_undo_step(bContext *C, int step, const char *undoname) +{ + wmWindowManager *wm = CTX_wm_manager(C); + wmWindow *win = CTX_wm_window(C); + // Main *bmain = CTX_data_main(C); + Scene *scene = CTX_data_scene(C); + + /* undo during jobs are running can easily lead to freeing data using by jobs, + * or they can just lead to freezing job in some other cases */ + if (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY)) { + return OPERATOR_CANCELLED; + } + + /* TODO(campbell): undo_system: use undo system */ + /* grease pencil can be can be used in plenty of spaces, so check it first */ + if (ED_gpencil_session_active()) { + return ED_undo_gpencil_step(C, step, undoname); + } + + /* Undo System */ + { + if (undoname) { + UndoStep *step_data = BKE_undosys_step_find_by_name(wm->undo_stack, undoname); + BKE_undosys_step_undo_with_data(wm->undo_stack, C, step_data); + } + else { + BKE_undosys_step_undo_compat_only(wm->undo_stack, C, step); + } + } + + WM_event_add_notifier(C, NC_WINDOW, NULL); + WM_event_add_notifier(C, NC_WM | ND_UNDO, NULL); + + if (win) { + win->addmousemove = true; + } + + return OPERATOR_FINISHED; +} + +void ED_undo_grouped_push(bContext *C, const char *str) +{ + /* do nothing if previous undo task is the same as this one (or from the same undo group) */ + { + wmWindowManager *wm = CTX_wm_manager(C); + if (wm->undo_stack->steps.last) { + const UndoStep *us = wm->undo_stack->steps.last; + if (STREQ(str, us->name)) { + return; + } + } + + } + + /* push as usual */ + ED_undo_push(C, str); +} + +void ED_undo_pop(bContext *C) +{ + ed_undo_step(C, 1, NULL); +} +void ED_undo_redo(bContext *C) +{ + ed_undo_step(C, -1, NULL); +} + +void ED_undo_push_op(bContext *C, wmOperator *op) +{ + /* in future, get undo string info? */ + ED_undo_push(C, op->type->name); +} + +void ED_undo_grouped_push_op(bContext *C, wmOperator *op) +{ + if (op->type->undo_group[0] != '\0') { + ED_undo_grouped_push(C, op->type->undo_group); + } + else { + ED_undo_grouped_push(C, op->type->name); + } +} + +void ED_undo_pop_op(bContext *C, wmOperator *op) +{ + /* search back a couple of undo's, in case something else added pushes */ + ed_undo_step(C, 0, op->type->name); +} + +/* name optionally, function used to check for operator redo panel */ +bool ED_undo_is_valid(const bContext *C, const char *undoname) +{ + wmWindowManager *wm = CTX_wm_manager(C); + return BKE_undosys_stack_has_undo(wm->undo_stack, undoname); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Undo, Undo Push & Redo Operators + * \{ */ + +static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op)) +{ + /* "last operator" should disappear, later we can tie this with undo stack nicer */ + WM_operator_stack_clear(CTX_wm_manager(C)); + return ed_undo_step(C, 1, NULL); +} + +static int ed_undo_push_exec(bContext *C, wmOperator *op) +{ + char str[BKE_UNDO_STR_MAX]; + RNA_string_get(op->ptr, "message", str); + ED_undo_push(C, str); + return OPERATOR_FINISHED; +} + +static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op)) +{ + return ed_undo_step(C, -1, NULL); +} + +static int ed_undo_redo_exec(bContext *C, wmOperator *UNUSED(op)) +{ + wmOperator *last_op = WM_operator_last_redo(C); + const int ret = ED_undo_operator_repeat(C, last_op); + return ret ? OPERATOR_FINISHED : OPERATOR_CANCELLED; +} + +static int ed_undo_redo_poll(bContext *C) +{ + wmOperator *last_op = WM_operator_last_redo(C); + return last_op && ED_operator_screenactive(C) && + WM_operator_check_ui_enabled(C, last_op->type->name); +} + +void ED_OT_undo(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Undo"; + ot->description = "Undo previous action"; + ot->idname = "ED_OT_undo"; + + /* api callbacks */ + ot->exec = ed_undo_exec; + ot->poll = ED_operator_screenactive; +} + +void ED_OT_undo_push(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Undo Push"; + ot->description = "Add an undo state (internal use only)"; + ot->idname = "ED_OT_undo_push"; + + /* api callbacks */ + ot->exec = ed_undo_push_exec; + + ot->flag = OPTYPE_INTERNAL; + + RNA_def_string(ot->srna, "message", "Add an undo step *function may be moved*", BKE_UNDO_STR_MAX, "Undo Message", ""); +} + +void ED_OT_redo(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Redo"; + ot->description = "Redo previous action"; + ot->idname = "ED_OT_redo"; + + /* api callbacks */ + ot->exec = ed_redo_exec; + ot->poll = ED_operator_screenactive; +} + +void ED_OT_undo_redo(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Undo and Redo"; + ot->description = "Undo and redo previous action"; + ot->idname = "ED_OT_undo_redo"; + + /* api callbacks */ + ot->exec = ed_undo_redo_exec; + ot->poll = ed_undo_redo_poll; +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Operator Repeat + * \{ */ + +/* ui callbacks should call this rather than calling WM_operator_repeat() themselves */ +int ED_undo_operator_repeat(bContext *C, struct wmOperator *op) +{ + int ret = 0; + + if (op) { + wmWindowManager *wm = CTX_wm_manager(C); + struct Scene *scene = CTX_data_scene(C); + + /* keep in sync with logic in view3d_panel_operator_redo() */ + ARegion *ar = CTX_wm_region(C); + ARegion *ar1 = BKE_area_find_region_active_win(CTX_wm_area(C)); + + if (ar1) + CTX_wm_region_set(C, ar1); + + if ((WM_operator_repeat_check(C, op)) && + (WM_operator_poll(C, op->type)) && + /* note, undo/redo cant run if there are jobs active, + * check for screen jobs only so jobs like material/texture/world preview + * (which copy their data), wont stop redo, see [#29579]], + * + * note, - WM_operator_check_ui_enabled() jobs test _must_ stay in sync with this */ + (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY) == 0)) + { + int retval; + + ED_viewport_render_kill_jobs(wm, CTX_data_main(C), true); + + if (G.debug & G_DEBUG) + printf("redo_cb: operator redo %s\n", op->type->name); + + WM_operator_free_all_after(wm, op); + + ED_undo_pop_op(C, op); + + if (op->type->check) { + if (op->type->check(C, op)) { + /* check for popup and re-layout buttons */ + ARegion *ar_menu = CTX_wm_menu(C); + if (ar_menu) { + ED_region_tag_refresh_ui(ar_menu); + } + } + } + + retval = WM_operator_repeat(C, op); + if ((retval & OPERATOR_FINISHED) == 0) { + if (G.debug & G_DEBUG) + printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval); + ED_undo_redo(C); + } + else { + ret = 1; + } + } + else { + if (G.debug & G_DEBUG) { + printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name); + } + } + + /* set region back */ + CTX_wm_region_set(C, ar); + } + else { + if (G.debug & G_DEBUG) { + printf("redo_cb: ED_undo_operator_repeat called with NULL 'op'\n"); + } + } + + return ret; +} + + +void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused)) +{ + ED_undo_operator_repeat(C, (wmOperator *)arg_op); +} + +void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event)) +{ + ED_undo_operator_repeat(C, (wmOperator *)arg_op); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Undo History Operator + * \{ */ + +/* create enum based on undo items */ +static const EnumPropertyItem *rna_undo_itemf(bContext *C, int *totitem) +{ + EnumPropertyItem item_tmp = {0}, *item = NULL; + int i = 0; + + wmWindowManager *wm = CTX_wm_manager(C); + if (wm->undo_stack == NULL) { + return NULL; + } + + for (UndoStep *us = wm->undo_stack->steps.first; us; us = us->next, i++) { + if (us->skip == false) { + item_tmp.identifier = us->name; + item_tmp.name = IFACE_(us->name); + if (us == wm->undo_stack->step_active) { + item_tmp.icon = ICON_RESTRICT_VIEW_OFF; + } + else { + item_tmp.icon = ICON_NONE; + } + item_tmp.value = i; + RNA_enum_item_add(&item, totitem, &item_tmp); + } + } + RNA_enum_item_end(&item, totitem); + + return item; +} + + +static int undo_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) +{ + int totitem = 0; + + { + const EnumPropertyItem *item = rna_undo_itemf(C, &totitem); + + if (totitem > 0) { + uiPopupMenu *pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE); + uiLayout *layout = UI_popup_menu_layout(pup); + uiLayout *split = uiLayoutSplit(layout, 0.0f, false); + uiLayout *column = NULL; + const int col_size = 20 + totitem / 12; + int i, c; + bool add_col = true; + + for (c = 0, i = totitem; i--;) { + if (add_col && !(c % col_size)) { + column = uiLayoutColumn(split, false); + add_col = false; + } + if (item[i].identifier) { + uiItemIntO(column, item[i].name, item[i].icon, op->type->idname, "item", item[i].value); + ++c; + add_col = true; + } + } + + MEM_freeN((void *)item); + + UI_popup_menu_end(C, pup); + } + + } + return OPERATOR_CANCELLED; +} + +/* note: also check ed_undo_step() in top if you change notifiers */ +static int undo_history_exec(bContext *C, wmOperator *op) +{ + PropertyRNA *prop = RNA_struct_find_property(op->ptr, "item"); + if (RNA_property_is_set(op->ptr, prop)) { + int item = RNA_property_int_get(op->ptr, prop); + wmWindowManager *wm = CTX_wm_manager(C); + BKE_undosys_step_undo_from_index(wm->undo_stack, C, item); + WM_event_add_notifier(C, NC_WINDOW, NULL); + return OPERATOR_FINISHED; + } + return OPERATOR_CANCELLED; +} + +void ED_OT_undo_history(wmOperatorType *ot) +{ + /* identifiers */ + ot->name = "Undo History"; + ot->description = "Redo specific action in history"; + ot->idname = "ED_OT_undo_history"; + + /* api callbacks */ + ot->invoke = undo_history_invoke; + ot->exec = undo_history_exec; + ot->poll = ED_operator_screenactive; + + RNA_def_int(ot->srna, "item", 0, 0, INT_MAX, "Item", "", 0, INT_MAX); + +} + +/** \} */ diff --git a/source/blender/editors/undo/memfile_undo.c b/source/blender/editors/undo/memfile_undo.c new file mode 100644 index 00000000000..c336069bff8 --- /dev/null +++ b/source/blender/editors/undo/memfile_undo.c @@ -0,0 +1,149 @@ +/* + * ***** 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/util/memfile_undo.c + * \ingroup edundo + * + * Wrapper between 'ED_undo.h' and 'BKE_undo_system.h' API's. + */ + +#include "BLI_utildefines.h" +#include "BLI_sys_types.h" + +#include "DNA_object_enums.h" + +#include "BKE_blender_undo.h" +#include "BKE_context.h" +#include "BKE_undo_system.h" + +#include "WM_api.h" +#include "WM_types.h" + +#include "ED_object.h" +#include "ED_undo.h" +#include "ED_render.h" + + +#include "../blenloader/BLO_undofile.h" + +#include "undo_intern.h" + +/* -------------------------------------------------------------------- */ +/** \name Implements ED Undo System + * \{ */ + +typedef struct MemFileUndoStep { + UndoStep step; + MemFileUndoData *data; +} MemFileUndoStep; + +static bool memfile_undosys_poll(bContext *UNUSED(C)) +{ + /* other poll functions must run first, this is a catch-all. */ + + if ((U.uiflag & USER_GLOBALUNDO) == 0) { + return false; + } + return true; +} + +static bool memfile_undosys_step_encode(struct bContext *C, UndoStep *us_p) +{ + MemFileUndoStep *us = (MemFileUndoStep *)us_p; + + /* Important we only use 'main' from the context (see: BKE_undosys_stack_init_from_main). */ + struct Main *bmain = CTX_data_main(C); + + /* can be NULL, use when set. */ + MemFileUndoStep *us_prev = (MemFileUndoStep *)BKE_undosys_step_same_type_prev(us_p); + us->data = BKE_memfile_undo_encode(bmain, us_prev ? us_prev->data : NULL); + us->step.data_size = us->data->undo_size; + return true; +} + +static void memfile_undosys_step_decode(struct bContext *C, UndoStep *us_p, int UNUSED(dir)) +{ + /* Loading the content will correctly switch into compatible non-object modes. */ + ED_object_mode_set(C, OB_MODE_OBJECT); + + /* This is needed so undoing/redoing doesn't crash with threaded previews going */ + ED_viewport_render_kill_jobs(CTX_wm_manager(C), CTX_data_main(C), true); + MemFileUndoStep *us = (MemFileUndoStep *)us_p; + BKE_memfile_undo_decode(us->data, C); + + WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, CTX_data_scene(C)); +} + +static void memfile_undosys_step_free(UndoStep *us_p) +{ + /* To avoid unnecessary slow down, free backwards (so we don't need to merge when clearing all). */ + MemFileUndoStep *us = (MemFileUndoStep *)us_p; + if (us_p->next != NULL) { + UndoStep *us_next_p = BKE_undosys_step_same_type_next(us_p); + if (us_next_p != NULL) { + MemFileUndoStep *us_next = (MemFileUndoStep *)us_next_p; + BLO_memfile_merge(&us->data->memfile, &us_next->data->memfile); + } + } + + BKE_memfile_undo_free(us->data); +} + +/* Export for ED_undo_sys. */ +void ED_memfile_undosys_type(UndoType *ut) +{ + ut->name = "Global Undo"; + ut->poll = memfile_undosys_poll; + ut->step_encode = memfile_undosys_step_encode; + ut->step_decode = memfile_undosys_step_decode; + ut->step_free = memfile_undosys_step_free; + + ut->mode = BKE_UNDOTYPE_MODE_STORE; + ut->use_context = true; + + ut->step_size = sizeof(MemFileUndoStep); +} + +/** \} */ + +/* -------------------------------------------------------------------- */ +/** \name Utilities + * \{ */ + +/** + * Ideally we wouldn't need to export global undo internals, there are some cases where it's needed though. + */ +static struct MemFile *ed_undosys_step_get_memfile(UndoStep *us_p) +{ + MemFileUndoStep *us = (MemFileUndoStep *)us_p; + return &us->data->memfile; +} + +struct MemFile *ED_undosys_stack_memfile_get_active(UndoStack *ustack) +{ + UndoStep *us = BKE_undosys_stack_active_with_type(ustack, BKE_UNDOSYS_TYPE_MEMFILE); + if (us) { + return ed_undosys_step_get_memfile(us); + } + return NULL; +} + + +/** \} */ diff --git a/source/blender/editors/undo/undo_intern.h b/source/blender/editors/undo/undo_intern.h new file mode 100644 index 00000000000..671f9637d65 --- /dev/null +++ b/source/blender/editors/undo/undo_intern.h @@ -0,0 +1,35 @@ +/* + * ***** 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/undo/undo_intern.h + * \ingroup edundo + */ + +#ifndef __UNDO_INTERN_H__ +#define __UNDO_INTERN_H__ + +/* internal exports only */ + +struct UndoType; + +/* memfile_undo.c */ +void ED_memfile_undosys_type(struct UndoType *ut); + +#endif /* __UNDO_INTERN_H__ */ diff --git a/source/blender/editors/undo/undo_system_types.c b/source/blender/editors/undo/undo_system_types.c new file mode 100644 index 00000000000..f1ef444337c --- /dev/null +++ b/source/blender/editors/undo/undo_system_types.c @@ -0,0 +1,74 @@ +/* + * ***** 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/util/undo_system_types.c + * \ingroup edundo + */ + +#include + +#include "BLI_utildefines.h" + + +#include "ED_armature.h" +#include "ED_curve.h" +#include "ED_lattice.h" +#include "ED_mball.h" +#include "ED_mesh.h" +#include "ED_paint.h" +#include "ED_particle.h" +#include "ED_sculpt.h" +#include "ED_text.h" +#include "ED_undo.h" +#include "undo_intern.h" + +/* Keep last */ +#include "BKE_undo_system.h" + +void ED_undosys_type_init(void) +{ + /* Edit Modes */ + BKE_undosys_type_append(ED_armature_undosys_type); + BKE_undosys_type_append(ED_curve_undosys_type); + BKE_undosys_type_append(ED_font_undosys_type); + BKE_undosys_type_append(ED_lattice_undosys_type); + BKE_undosys_type_append(ED_mball_undosys_type); + BKE_undosys_type_append(ED_mesh_undosys_type); + + /* Paint Modes */ + BKE_UNDOSYS_TYPE_IMAGE = BKE_undosys_type_append(ED_image_undosys_type); + + BKE_UNDOSYS_TYPE_SCULPT = BKE_undosys_type_append(ED_sculpt_undosys_type); + + BKE_UNDOSYS_TYPE_PARTICLE = BKE_undosys_type_append(ED_particle_undosys_type); + + BKE_UNDOSYS_TYPE_PAINTCURVE = BKE_undosys_type_append(ED_paintcurve_undosys_type); + + /* Text editor */ + BKE_undosys_type_append(ED_text_undosys_type); + + /* Keep global undo last (as a fallback). */ + BKE_UNDOSYS_TYPE_MEMFILE = BKE_undosys_type_append(ED_memfile_undosys_type); +} + +void ED_undosys_type_free(void) +{ + BKE_undosys_type_free_all(); +} diff --git a/source/blender/editors/util/CMakeLists.txt b/source/blender/editors/util/CMakeLists.txt index b2c0f6ad309..8657c876d47 100644 --- a/source/blender/editors/util/CMakeLists.txt +++ b/source/blender/editors/util/CMakeLists.txt @@ -41,12 +41,8 @@ set(INC_SYS set(SRC ed_transverts.c ed_util.c - memfile_undo.c numinput.c - undo.c - undo_system_types.c - util_intern.h # general includes ../include/BIF_gl.h ../include/BIF_glutil.h diff --git a/source/blender/editors/util/memfile_undo.c b/source/blender/editors/util/memfile_undo.c deleted file mode 100644 index 95af0c48147..00000000000 --- a/source/blender/editors/util/memfile_undo.c +++ /dev/null @@ -1,149 +0,0 @@ -/* - * ***** 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/util/memfile_undo.c - * \ingroup edutil - * - * Wrapper between 'BKE_undo.h' and 'BKE_undo_system.h' - */ - -#include "BLI_utildefines.h" -#include "BLI_sys_types.h" - -#include "DNA_object_enums.h" - -#include "BKE_blender_undo.h" -#include "BKE_context.h" -#include "BKE_undo_system.h" - -#include "WM_api.h" -#include "WM_types.h" - -#include "ED_object.h" -#include "ED_util.h" -#include "ED_render.h" - - -#include "../blenloader/BLO_undofile.h" - -#include "util_intern.h" - -/* -------------------------------------------------------------------- */ -/** \name Implements ED Undo System - * \{ */ - -typedef struct MemFileUndoStep { - UndoStep step; - MemFileUndoData *data; -} MemFileUndoStep; - -static bool memfile_undosys_poll(bContext *UNUSED(C)) -{ - /* other poll functions must run first, this is a catch-all. */ - - if ((U.uiflag & USER_GLOBALUNDO) == 0) { - return false; - } - return true; -} - -static bool memfile_undosys_step_encode(struct bContext *C, UndoStep *us_p) -{ - MemFileUndoStep *us = (MemFileUndoStep *)us_p; - - /* Important we only use 'main' from the context (see: BKE_undosys_stack_init_from_main). */ - struct Main *bmain = CTX_data_main(C); - - /* can be NULL, use when set. */ - MemFileUndoStep *us_prev = (MemFileUndoStep *)BKE_undosys_step_same_type_prev(us_p); - us->data = BKE_memfile_undo_encode(bmain, us_prev ? us_prev->data : NULL); - us->step.data_size = us->data->undo_size; - return true; -} - -static void memfile_undosys_step_decode(struct bContext *C, UndoStep *us_p, int UNUSED(dir)) -{ - /* Loading the content will correctly switch into compatible non-object modes. */ - ED_object_mode_set(C, OB_MODE_OBJECT); - - /* This is needed so undoing/redoing doesn't crash with threaded previews going */ - ED_viewport_render_kill_jobs(CTX_wm_manager(C), CTX_data_main(C), true); - MemFileUndoStep *us = (MemFileUndoStep *)us_p; - BKE_memfile_undo_decode(us->data, C); - - WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, CTX_data_scene(C)); -} - -static void memfile_undosys_step_free(UndoStep *us_p) -{ - /* To avoid unnecessary slow down, free backwards (so we don't need to merge when clearing all). */ - MemFileUndoStep *us = (MemFileUndoStep *)us_p; - if (us_p->next != NULL) { - UndoStep *us_next_p = BKE_undosys_step_same_type_next(us_p); - if (us_next_p != NULL) { - MemFileUndoStep *us_next = (MemFileUndoStep *)us_next_p; - BLO_memfile_merge(&us->data->memfile, &us_next->data->memfile); - } - } - - BKE_memfile_undo_free(us->data); -} - -/* Export for ED_undo_sys. */ -void ED_memfile_undosys_type(UndoType *ut) -{ - ut->name = "Global Undo"; - ut->poll = memfile_undosys_poll; - ut->step_encode = memfile_undosys_step_encode; - ut->step_decode = memfile_undosys_step_decode; - ut->step_free = memfile_undosys_step_free; - - ut->mode = BKE_UNDOTYPE_MODE_STORE; - ut->use_context = true; - - ut->step_size = sizeof(MemFileUndoStep); -} - -/** \} */ - -/* -------------------------------------------------------------------- */ -/** \name Utilities - * \{ */ - -/** - * Ideally we wouldn't need to export global undo internals, there are some cases where it's needed though. - */ -static struct MemFile *ed_undosys_step_get_memfile(UndoStep *us_p) -{ - MemFileUndoStep *us = (MemFileUndoStep *)us_p; - return &us->data->memfile; -} - -struct MemFile *ED_undosys_stack_memfile_get_active(UndoStack *ustack) -{ - UndoStep *us = BKE_undosys_stack_active_with_type(ustack, BKE_UNDOSYS_TYPE_MEMFILE); - if (us) { - return ed_undosys_step_get_memfile(us); - } - return NULL; -} - - -/** \} */ diff --git a/source/blender/editors/util/undo.c b/source/blender/editors/util/undo.c deleted file mode 100644 index fea3360a39c..00000000000 --- a/source/blender/editors/util/undo.c +++ /dev/null @@ -1,478 +0,0 @@ -/* - * ***** 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. - * - * The Original Code is Copyright (C) 2004 Blender Foundation - * All rights reserved. - * - * The Original Code is: all of this file. - * - * Contributor(s): none yet. - * - * ***** END GPL LICENSE BLOCK ***** - */ - -/** \file blender/editors/util/undo.c - * \ingroup edutil - */ - -#include -#include -#include - -#include "MEM_guardedalloc.h" - -#include "DNA_object_types.h" -#include "DNA_scene_types.h" - -#include "BLI_utildefines.h" - -#include "BLT_translation.h" - -#include "BKE_blender_undo.h" -#include "BKE_context.h" -#include "BKE_global.h" -#include "BKE_main.h" -#include "BKE_screen.h" -#include "BKE_undo_system.h" - -#include "ED_armature.h" -#include "ED_particle.h" -#include "ED_curve.h" -#include "ED_gpencil.h" -#include "ED_lattice.h" -#include "ED_mball.h" -#include "ED_mesh.h" -#include "ED_object.h" -#include "ED_render.h" -#include "ED_screen.h" -#include "ED_paint.h" -#include "ED_util.h" -#include "ED_text.h" - -#include "WM_api.h" -#include "WM_types.h" - -#include "RNA_access.h" -#include "RNA_define.h" - -#include "UI_interface.h" -#include "UI_resources.h" - -#include "util_intern.h" - -/* ***************** generic undo system ********************* */ - -void ED_undo_push(bContext *C, const char *str) -{ - if (G.debug & G_DEBUG) { - printf("%s: %s\n", __func__, str); - } - const int steps = U.undosteps; - - if (steps <= 0) { - return; - } - - wmWindowManager *wm = CTX_wm_manager(C); - - /* Only apply limit if this is the last undo step. */ - if (wm->undo_stack->step_active && (wm->undo_stack->step_active->next == NULL)) { - BKE_undosys_stack_limit_steps_and_memory(wm->undo_stack, steps - 1, 0); - } - - BKE_undosys_step_push(wm->undo_stack, C, str); - - if (U.undomemory != 0) { - const size_t memory_limit = (size_t)U.undomemory * 1024 * 1024; - BKE_undosys_stack_limit_steps_and_memory(wm->undo_stack, 0, memory_limit); - } - - WM_file_tag_modified(); -} - -/* note: also check undo_history_exec() in bottom if you change notifiers */ -static int ed_undo_step(bContext *C, int step, const char *undoname) -{ - wmWindowManager *wm = CTX_wm_manager(C); - wmWindow *win = CTX_wm_window(C); - // Main *bmain = CTX_data_main(C); - Scene *scene = CTX_data_scene(C); - - /* undo during jobs are running can easily lead to freeing data using by jobs, - * or they can just lead to freezing job in some other cases */ - if (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY)) { - return OPERATOR_CANCELLED; - } - - /* TODO(campbell): undo_system: use undo system */ - /* grease pencil can be can be used in plenty of spaces, so check it first */ - if (ED_gpencil_session_active()) { - return ED_undo_gpencil_step(C, step, undoname); - } - - /* Undo System */ - { - if (undoname) { - UndoStep *step_data = BKE_undosys_step_find_by_name(wm->undo_stack, undoname); - BKE_undosys_step_undo_with_data(wm->undo_stack, C, step_data); - } - else { - BKE_undosys_step_undo_compat_only(wm->undo_stack, C, step); - } - } - - WM_event_add_notifier(C, NC_WINDOW, NULL); - WM_event_add_notifier(C, NC_WM | ND_UNDO, NULL); - - if (win) { - win->addmousemove = true; - } - - return OPERATOR_FINISHED; -} - -void ED_undo_grouped_push(bContext *C, const char *str) -{ - /* do nothing if previous undo task is the same as this one (or from the same undo group) */ - { - wmWindowManager *wm = CTX_wm_manager(C); - if (wm->undo_stack->steps.last) { - const UndoStep *us = wm->undo_stack->steps.last; - if (STREQ(str, us->name)) { - return; - } - } - - } - - /* push as usual */ - ED_undo_push(C, str); -} - -void ED_undo_pop(bContext *C) -{ - ed_undo_step(C, 1, NULL); -} -void ED_undo_redo(bContext *C) -{ - ed_undo_step(C, -1, NULL); -} - -void ED_undo_push_op(bContext *C, wmOperator *op) -{ - /* in future, get undo string info? */ - ED_undo_push(C, op->type->name); -} - -void ED_undo_grouped_push_op(bContext *C, wmOperator *op) -{ - if (op->type->undo_group[0] != '\0') { - ED_undo_grouped_push(C, op->type->undo_group); - } - else { - ED_undo_grouped_push(C, op->type->name); - } -} - -void ED_undo_pop_op(bContext *C, wmOperator *op) -{ - /* search back a couple of undo's, in case something else added pushes */ - ed_undo_step(C, 0, op->type->name); -} - -/* name optionally, function used to check for operator redo panel */ -bool ED_undo_is_valid(const bContext *C, const char *undoname) -{ - wmWindowManager *wm = CTX_wm_manager(C); - return BKE_undosys_stack_has_undo(wm->undo_stack, undoname); -} - -static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op)) -{ - /* "last operator" should disappear, later we can tie this with undo stack nicer */ - WM_operator_stack_clear(CTX_wm_manager(C)); - return ed_undo_step(C, 1, NULL); -} - -static int ed_undo_push_exec(bContext *C, wmOperator *op) -{ - char str[BKE_UNDO_STR_MAX]; - RNA_string_get(op->ptr, "message", str); - ED_undo_push(C, str); - return OPERATOR_FINISHED; -} - -static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op)) -{ - return ed_undo_step(C, -1, NULL); -} - -static int ed_undo_redo_exec(bContext *C, wmOperator *UNUSED(op)) -{ - wmOperator *last_op = WM_operator_last_redo(C); - const int ret = ED_undo_operator_repeat(C, last_op); - return ret ? OPERATOR_FINISHED : OPERATOR_CANCELLED; -} - -static int ed_undo_redo_poll(bContext *C) -{ - wmOperator *last_op = WM_operator_last_redo(C); - return last_op && ED_operator_screenactive(C) && - WM_operator_check_ui_enabled(C, last_op->type->name); -} - -/* ********************** */ - -void ED_OT_undo(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Undo"; - ot->description = "Undo previous action"; - ot->idname = "ED_OT_undo"; - - /* api callbacks */ - ot->exec = ed_undo_exec; - ot->poll = ED_operator_screenactive; -} - -void ED_OT_undo_push(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Undo Push"; - ot->description = "Add an undo state (internal use only)"; - ot->idname = "ED_OT_undo_push"; - - /* api callbacks */ - ot->exec = ed_undo_push_exec; - - ot->flag = OPTYPE_INTERNAL; - - RNA_def_string(ot->srna, "message", "Add an undo step *function may be moved*", BKE_UNDO_STR_MAX, "Undo Message", ""); -} - -void ED_OT_redo(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Redo"; - ot->description = "Redo previous action"; - ot->idname = "ED_OT_redo"; - - /* api callbacks */ - ot->exec = ed_redo_exec; - ot->poll = ED_operator_screenactive; -} - -void ED_OT_undo_redo(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Undo and Redo"; - ot->description = "Undo and redo previous action"; - ot->idname = "ED_OT_undo_redo"; - - /* api callbacks */ - ot->exec = ed_undo_redo_exec; - ot->poll = ed_undo_redo_poll; -} - -/* ui callbacks should call this rather than calling WM_operator_repeat() themselves */ -int ED_undo_operator_repeat(bContext *C, struct wmOperator *op) -{ - int ret = 0; - - if (op) { - wmWindowManager *wm = CTX_wm_manager(C); - struct Scene *scene = CTX_data_scene(C); - - /* keep in sync with logic in view3d_panel_operator_redo() */ - ARegion *ar = CTX_wm_region(C); - ARegion *ar1 = BKE_area_find_region_active_win(CTX_wm_area(C)); - - if (ar1) - CTX_wm_region_set(C, ar1); - - if ((WM_operator_repeat_check(C, op)) && - (WM_operator_poll(C, op->type)) && - /* note, undo/redo cant run if there are jobs active, - * check for screen jobs only so jobs like material/texture/world preview - * (which copy their data), wont stop redo, see [#29579]], - * - * note, - WM_operator_check_ui_enabled() jobs test _must_ stay in sync with this */ - (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY) == 0)) - { - int retval; - - ED_viewport_render_kill_jobs(wm, CTX_data_main(C), true); - - if (G.debug & G_DEBUG) - printf("redo_cb: operator redo %s\n", op->type->name); - - WM_operator_free_all_after(wm, op); - - ED_undo_pop_op(C, op); - - if (op->type->check) { - if (op->type->check(C, op)) { - /* check for popup and re-layout buttons */ - ARegion *ar_menu = CTX_wm_menu(C); - if (ar_menu) { - ED_region_tag_refresh_ui(ar_menu); - } - } - } - - retval = WM_operator_repeat(C, op); - if ((retval & OPERATOR_FINISHED) == 0) { - if (G.debug & G_DEBUG) - printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval); - ED_undo_redo(C); - } - else { - ret = 1; - } - } - else { - if (G.debug & G_DEBUG) { - printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name); - } - } - - /* set region back */ - CTX_wm_region_set(C, ar); - } - else { - if (G.debug & G_DEBUG) { - printf("redo_cb: ED_undo_operator_repeat called with NULL 'op'\n"); - } - } - - return ret; -} - - -void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused)) -{ - ED_undo_operator_repeat(C, (wmOperator *)arg_op); -} - -void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event)) -{ - ED_undo_operator_repeat(C, (wmOperator *)arg_op); -} - - -/* ************************** */ - -/* create enum based on undo items */ -static const EnumPropertyItem *rna_undo_itemf(bContext *C, int *totitem) -{ - EnumPropertyItem item_tmp = {0}, *item = NULL; - int i = 0; - - wmWindowManager *wm = CTX_wm_manager(C); - if (wm->undo_stack == NULL) { - return NULL; - } - - for (UndoStep *us = wm->undo_stack->steps.first; us; us = us->next, i++) { - if (us->skip == false) { - item_tmp.identifier = us->name; - /* XXX This won't work with non-default contexts (e.g. operators) :/ */ - item_tmp.name = IFACE_(us->name); - if (us == wm->undo_stack->step_active) { - item_tmp.icon = ICON_RESTRICT_VIEW_OFF; - } - else { - item_tmp.icon = ICON_NONE; - } - item_tmp.value = i; - RNA_enum_item_add(&item, totitem, &item_tmp); - } - } - RNA_enum_item_end(&item, totitem); - - return item; -} - - -static int undo_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event)) -{ - int totitem = 0; - - { - const EnumPropertyItem *item = rna_undo_itemf(C, &totitem); - - if (totitem > 0) { - uiPopupMenu *pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE); - uiLayout *layout = UI_popup_menu_layout(pup); - uiLayout *split = uiLayoutSplit(layout, 0.0f, false); - uiLayout *column = NULL; - const int col_size = 20 + totitem / 12; - int i, c; - bool add_col = true; - - for (c = 0, i = totitem; i--;) { - if (add_col && !(c % col_size)) { - column = uiLayoutColumn(split, false); - add_col = false; - } - if (item[i].identifier) { - uiItemIntO(column, item[i].name, item[i].icon, op->type->idname, "item", item[i].value); - ++c; - add_col = true; - } - } - - MEM_freeN((void *)item); - - UI_popup_menu_end(C, pup); - } - - } - return OPERATOR_CANCELLED; -} - -/* note: also check ed_undo_step() in top if you change notifiers */ -static int undo_history_exec(bContext *C, wmOperator *op) -{ - PropertyRNA *prop = RNA_struct_find_property(op->ptr, "item"); - if (RNA_property_is_set(op->ptr, prop)) { - int item = RNA_property_int_get(op->ptr, prop); - wmWindowManager *wm = CTX_wm_manager(C); - BKE_undosys_step_undo_from_index(wm->undo_stack, C, item); - WM_event_add_notifier(C, NC_WINDOW, NULL); - return OPERATOR_FINISHED; - } - return OPERATOR_CANCELLED; -} - -void ED_OT_undo_history(wmOperatorType *ot) -{ - /* identifiers */ - ot->name = "Undo History"; - ot->description = "Redo specific action in history"; - ot->idname = "ED_OT_undo_history"; - - /* api callbacks */ - ot->invoke = undo_history_invoke; - ot->exec = undo_history_exec; - ot->poll = ED_operator_screenactive; - - RNA_def_int(ot->srna, "item", 0, 0, INT_MAX, "Item", "", 0, INT_MAX); - -} - - diff --git a/source/blender/editors/util/undo_system_types.c b/source/blender/editors/util/undo_system_types.c deleted file mode 100644 index a326d9eb859..00000000000 --- a/source/blender/editors/util/undo_system_types.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * ***** 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/util/undo_system_types.c - * \ingroup edutil - */ - -#include - -#include "BLI_utildefines.h" - - -#include "ED_armature.h" -#include "ED_curve.h" -#include "ED_lattice.h" -#include "ED_mball.h" -#include "ED_mesh.h" -#include "ED_paint.h" -#include "ED_particle.h" -#include "ED_sculpt.h" -#include "ED_text.h" -#include "ED_util.h" -#include "util_intern.h" - -/* Keep last */ -#include "BKE_undo_system.h" - -void ED_undosys_type_init(void) -{ - /* Edit Modes */ - BKE_undosys_type_append(ED_armature_undosys_type); - BKE_undosys_type_append(ED_curve_undosys_type); - BKE_undosys_type_append(ED_font_undosys_type); - BKE_undosys_type_append(ED_lattice_undosys_type); - BKE_undosys_type_append(ED_mball_undosys_type); - BKE_undosys_type_append(ED_mesh_undosys_type); - - /* Paint Modes */ - BKE_UNDOSYS_TYPE_IMAGE = BKE_undosys_type_append(ED_image_undosys_type); - - BKE_UNDOSYS_TYPE_SCULPT = BKE_undosys_type_append(ED_sculpt_undosys_type); - - BKE_UNDOSYS_TYPE_PARTICLE = BKE_undosys_type_append(ED_particle_undosys_type); - - BKE_UNDOSYS_TYPE_PAINTCURVE = BKE_undosys_type_append(ED_paintcurve_undosys_type); - - /* Text editor */ - BKE_undosys_type_append(ED_text_undosys_type); - - /* Keep global undo last (as a fallback). */ - BKE_UNDOSYS_TYPE_MEMFILE = BKE_undosys_type_append(ED_memfile_undosys_type); -} - -void ED_undosys_type_free(void) -{ - BKE_undosys_type_free_all(); -} diff --git a/source/blender/editors/util/util_intern.h b/source/blender/editors/util/util_intern.h deleted file mode 100644 index 6eda3900e91..00000000000 --- a/source/blender/editors/util/util_intern.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * ***** 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. - * - * The Original Code is Copyright (C) 2008 Blender Foundation. - * All rights reserved. - * - * - * Contributor(s): Blender Foundation - * - * ***** END GPL LICENSE BLOCK ***** - */ - -/** \file blender/editors/util/util_intern.h - * \ingroup edutil - */ - - -#ifndef __UTIL_INTERN_H__ -#define __UTIL_INTERN_H__ - -/* internal exports only */ - -struct UndoType; -struct Main; -struct Scene; - -/* memfile_undo.c */ -void ED_memfile_undosys_type(struct UndoType *ut); - -#endif /* __UTIL_INTERN_H__ */ diff --git a/source/blender/windowmanager/intern/wm_event_system.c b/source/blender/windowmanager/intern/wm_event_system.c index b2df53321c0..4f441c293c4 100644 --- a/source/blender/windowmanager/intern/wm_event_system.c +++ b/source/blender/windowmanager/intern/wm_event_system.c @@ -67,6 +67,7 @@ #include "ED_screen.h" #include "ED_view3d.h" #include "ED_util.h" +#include "ED_undo.h" #include "RNA_access.h" diff --git a/source/blender/windowmanager/intern/wm_files.c b/source/blender/windowmanager/intern/wm_files.c index 1d96d291dc4..da5ebd7abcd 100644 --- a/source/blender/windowmanager/intern/wm_files.c +++ b/source/blender/windowmanager/intern/wm_files.c @@ -105,6 +105,7 @@ #include "ED_screen.h" #include "ED_view3d.h" #include "ED_util.h" +#include "ED_undo.h" #include "GHOST_C-api.h" #include "GHOST_Path-api.h" diff --git a/source/blender/windowmanager/intern/wm_init_exit.c b/source/blender/windowmanager/intern/wm_init_exit.c index b9ac05e0c57..52682523212 100644 --- a/source/blender/windowmanager/intern/wm_init_exit.c +++ b/source/blender/windowmanager/intern/wm_init_exit.c @@ -113,6 +113,7 @@ #include "ED_space_api.h" #include "ED_screen.h" #include "ED_util.h" +#include "ED_undo.h" #include "UI_interface.h" #include "BLF_api.h" diff --git a/source/blender/windowmanager/intern/wm_operators.c b/source/blender/windowmanager/intern/wm_operators.c index b05b2596719..f46358f83cf 100644 --- a/source/blender/windowmanager/intern/wm_operators.c +++ b/source/blender/windowmanager/intern/wm_operators.c @@ -96,7 +96,7 @@ #include "ED_numinput.h" #include "ED_screen.h" -#include "ED_util.h" +#include "ED_undo.h" #include "ED_view3d.h" #include "GPU_basic_shader.h" -- cgit v1.2.3