2 * ***** BEGIN GPL LICENSE BLOCK *****
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * The Original Code is Copyright (C) 2004 Blender Foundation
19 * All rights reserved.
21 * The Original Code is: all of this file.
23 * Contributor(s): none yet.
25 * ***** END GPL LICENSE BLOCK *****
28 /** \file blender/editors/util/undo.c
36 #include "MEM_guardedalloc.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
41 #include "BLI_utildefines.h"
43 #include "BLT_translation.h"
45 #include "BKE_blender_undo.h"
46 #include "BKE_context.h"
47 #include "BKE_global.h"
49 #include "BKE_screen.h"
51 #include "ED_armature.h"
52 #include "ED_particle.h"
54 #include "ED_gpencil.h"
57 #include "ED_object.h"
58 #include "ED_render.h"
59 #include "ED_screen.h"
67 #include "RNA_access.h"
68 #include "RNA_define.h"
70 #include "UI_interface.h"
71 #include "UI_resources.h"
73 #include "util_intern.h"
75 /* ***************** generic undo system ********************* */
77 void ED_undo_push(bContext *C, const char *str)
79 Object *obedit = CTX_data_edit_object(C);
80 Object *obact = CTX_data_active_object(C);
82 if (G.debug & G_DEBUG)
83 printf("%s: %s\n", __func__, str);
86 if (U.undosteps == 0) return;
88 if (obedit->type == OB_MESH)
89 undo_push_mesh(C, str);
90 else if (ELEM(obedit->type, OB_CURVE, OB_SURF))
91 undo_push_curve(C, str);
92 else if (obedit->type == OB_FONT)
93 undo_push_font(C, str);
94 else if (obedit->type == OB_MBALL)
95 undo_push_mball(C, str);
96 else if (obedit->type == OB_LATTICE)
97 undo_push_lattice(C, str);
98 else if (obedit->type == OB_ARMATURE)
99 undo_push_armature(C, str);
101 else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
102 if (U.undosteps == 0) return;
104 PE_undo_push(CTX_data_scene(C), str);
106 else if (obact && obact->mode & OB_MODE_SCULPT) {
107 /* do nothing for now */
110 BKE_undo_write(C, str);
113 WM_file_tag_modified(C);
116 /* note: also check undo_history_exec() in bottom if you change notifiers */
117 static int ed_undo_step(bContext *C, int step, const char *undoname)
119 wmWindowManager *wm = CTX_wm_manager(C);
120 wmWindow *win = CTX_wm_window(C);
121 Main *bmain = CTX_data_main(C);
122 Scene *scene = CTX_data_scene(C);
123 Object *obedit = CTX_data_edit_object(C);
124 Object *obact = CTX_data_active_object(C);
125 ScrArea *sa = CTX_wm_area(C);
127 /* undo during jobs are running can easily lead to freeing data using by jobs,
128 * or they can just lead to freezing job in some other cases */
129 if (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY)) {
130 return OPERATOR_CANCELLED;
133 /* grease pencil can be can be used in plenty of spaces, so check it first */
134 if (ED_gpencil_session_active()) {
135 return ED_undo_gpencil_step(C, step, undoname);
138 if (sa && (sa->spacetype == SPACE_IMAGE)) {
139 SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
141 if ((obact && (obact->mode & OB_MODE_TEXTURE_PAINT)) || (sima->mode == SI_MODE_PAINT)) {
142 if (!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname) {
143 if (U.uiflag & USER_GLOBALUNDO) {
144 ED_viewport_render_kill_jobs(wm, bmain, true);
145 BKE_undo_name(C, undoname);
149 WM_event_add_notifier(C, NC_WINDOW, NULL);
150 return OPERATOR_FINISHED;
154 if (sa && (sa->spacetype == SPACE_TEXT)) {
155 ED_text_undo_step(C, step);
158 if (OB_TYPE_SUPPORT_EDITMODE(obedit->type)) {
160 undo_editmode_name(C, undoname);
162 undo_editmode_step(C, step);
164 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
168 /* Note: we used to do a fall-through here where if the
169 * mode-specific undo system had no more steps to undo (or
170 * redo), the global undo would run.
172 * That was inconsistent with editmode, and also makes for
173 * unecessarily tricky interaction with the other undo
175 if (obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
176 ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname);
178 else if (obact && obact->mode & OB_MODE_SCULPT) {
179 ED_undo_paint_step(C, UNDO_PAINT_MESH, step, undoname);
181 else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
187 else if (U.uiflag & USER_GLOBALUNDO) {
188 // note python defines not valid here anymore.
190 // XXX BPY_scripts_clear_pyobjects();
193 /* for global undo/redo we should just clear the editmode stack */
194 /* for example, texface stores image pointers */
195 undo_editmode_clear();
197 ED_viewport_render_kill_jobs(wm, bmain, true);
200 BKE_undo_name(C, undoname);
202 BKE_undo_step(C, step);
204 scene = CTX_data_scene(C);
206 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, scene);
210 WM_event_add_notifier(C, NC_WINDOW, NULL);
211 WM_event_add_notifier(C, NC_WM | ND_UNDO, NULL);
214 win->addmousemove = true;
217 return OPERATOR_FINISHED;
220 void ED_undo_grouped_push(bContext *C, const char *str)
222 /* do nothing if previous undo task is the same as this one (or from the same undo group) */
223 const char *last_undo = BKE_undo_get_name_last();
225 if (last_undo && STREQ(str, last_undo)) {
230 ED_undo_push(C, str);
233 void ED_undo_pop(bContext *C)
235 ed_undo_step(C, 1, NULL);
237 void ED_undo_redo(bContext *C)
239 ed_undo_step(C, -1, NULL);
242 void ED_undo_push_op(bContext *C, wmOperator *op)
244 /* in future, get undo string info? */
245 ED_undo_push(C, op->type->name);
248 void ED_undo_grouped_push_op(bContext *C, wmOperator *op)
250 if (op->type->undo_group[0] != '\0') {
251 ED_undo_grouped_push(C, op->type->undo_group);
254 ED_undo_grouped_push(C, op->type->name);
258 void ED_undo_pop_op(bContext *C, wmOperator *op)
260 /* search back a couple of undo's, in case something else added pushes */
261 ed_undo_step(C, 0, op->type->name);
264 /* name optionally, function used to check for operator redo panel */
265 bool ED_undo_is_valid(const bContext *C, const char *undoname)
267 Object *obedit = CTX_data_edit_object(C);
268 Object *obact = CTX_data_active_object(C);
269 ScrArea *sa = CTX_wm_area(C);
271 if (sa && sa->spacetype == SPACE_IMAGE) {
272 SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
274 if ((obact && (obact->mode & OB_MODE_TEXTURE_PAINT)) || (sima->mode == SI_MODE_PAINT)) {
279 if (sa && (sa->spacetype == SPACE_TEXT)) {
283 if (OB_TYPE_SUPPORT_EDITMODE(obedit->type)) {
284 return undo_editmode_is_valid(undoname);
289 /* if below tests fail, global undo gets executed */
291 if (obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
292 if (ED_undo_paint_is_valid(UNDO_PAINT_IMAGE, undoname))
295 else if (obact && obact->mode & OB_MODE_SCULPT) {
296 if (ED_undo_paint_is_valid(UNDO_PAINT_MESH, undoname))
299 else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
300 return PE_undo_is_valid(CTX_data_scene(C));
303 if (U.uiflag & USER_GLOBALUNDO) {
304 return BKE_undo_is_valid(undoname);
310 static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op))
312 /* "last operator" should disappear, later we can tie this with undo stack nicer */
313 WM_operator_stack_clear(CTX_wm_manager(C));
314 return ed_undo_step(C, 1, NULL);
317 static int ed_undo_push_exec(bContext *C, wmOperator *op)
319 char str[BKE_UNDO_STR_MAX];
320 RNA_string_get(op->ptr, "message", str);
321 ED_undo_push(C, str);
322 return OPERATOR_FINISHED;
325 static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op))
327 return ed_undo_step(C, -1, NULL);
330 static int ed_undo_redo_exec(bContext *C, wmOperator *UNUSED(op))
332 wmOperator *last_op = WM_operator_last_redo(C);
333 const int ret = ED_undo_operator_repeat(C, last_op);
334 return ret ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
337 static int ed_undo_redo_poll(bContext *C)
339 wmOperator *last_op = WM_operator_last_redo(C);
340 return last_op && ED_operator_screenactive(C) &&
341 WM_operator_check_ui_enabled(C, last_op->type->name);
344 /* ********************** */
346 void ED_OT_undo(wmOperatorType *ot)
350 ot->description = "Undo previous action";
351 ot->idname = "ED_OT_undo";
354 ot->exec = ed_undo_exec;
355 ot->poll = ED_operator_screenactive;
358 void ED_OT_undo_push(wmOperatorType *ot)
361 ot->name = "Undo Push";
362 ot->description = "Add an undo state (internal use only)";
363 ot->idname = "ED_OT_undo_push";
366 ot->exec = ed_undo_push_exec;
368 ot->flag = OPTYPE_INTERNAL;
370 RNA_def_string(ot->srna, "message", "Add an undo step *function may be moved*", BKE_UNDO_STR_MAX, "Undo Message", "");
373 void ED_OT_redo(wmOperatorType *ot)
377 ot->description = "Redo previous action";
378 ot->idname = "ED_OT_redo";
381 ot->exec = ed_redo_exec;
382 ot->poll = ED_operator_screenactive;
385 void ED_OT_undo_redo(wmOperatorType *ot)
388 ot->name = "Undo and Redo";
389 ot->description = "Undo and redo previous action";
390 ot->idname = "ED_OT_undo_redo";
393 ot->exec = ed_undo_redo_exec;
394 ot->poll = ed_undo_redo_poll;
397 /* ui callbacks should call this rather than calling WM_operator_repeat() themselves */
398 int ED_undo_operator_repeat(bContext *C, struct wmOperator *op)
403 wmWindowManager *wm = CTX_wm_manager(C);
404 struct Scene *scene = CTX_data_scene(C);
406 /* keep in sync with logic in view3d_panel_operator_redo() */
407 ARegion *ar = CTX_wm_region(C);
408 ARegion *ar1 = BKE_area_find_region_active_win(CTX_wm_area(C));
411 CTX_wm_region_set(C, ar1);
413 if ((WM_operator_repeat_check(C, op)) &&
414 (WM_operator_poll(C, op->type)) &&
415 /* note, undo/redo cant run if there are jobs active,
416 * check for screen jobs only so jobs like material/texture/world preview
417 * (which copy their data), wont stop redo, see [#29579]],
419 * note, - WM_operator_check_ui_enabled() jobs test _must_ stay in sync with this */
420 (WM_jobs_test(wm, scene, WM_JOB_TYPE_ANY) == 0))
424 ED_viewport_render_kill_jobs(wm, CTX_data_main(C), true);
426 if (G.debug & G_DEBUG)
427 printf("redo_cb: operator redo %s\n", op->type->name);
429 WM_operator_free_all_after(wm, op);
431 ED_undo_pop_op(C, op);
433 if (op->type->check) {
434 if (op->type->check(C, op)) {
435 /* check for popup and re-layout buttons */
436 ARegion *ar_menu = CTX_wm_menu(C);
438 ED_region_tag_refresh_ui(ar_menu);
443 retval = WM_operator_repeat(C, op);
444 if ((retval & OPERATOR_FINISHED) == 0) {
445 if (G.debug & G_DEBUG)
446 printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval);
454 if (G.debug & G_DEBUG) {
455 printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name);
459 /* set region back */
460 CTX_wm_region_set(C, ar);
463 if (G.debug & G_DEBUG) {
464 printf("redo_cb: ED_undo_operator_repeat called with NULL 'op'\n");
472 void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused))
474 ED_undo_operator_repeat(C, (wmOperator *)arg_op);
477 void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event))
479 ED_undo_operator_repeat(C, (wmOperator *)arg_op);
483 /* ************************** */
486 UNDOSYSTEM_GLOBAL = 1,
487 UNDOSYSTEM_EDITMODE = 2,
488 UNDOSYSTEM_PARTICLE = 3,
489 UNDOSYSTEM_IMAPAINT = 4,
490 UNDOSYSTEM_SCULPT = 5,
493 static int get_undo_system(bContext *C)
495 Object *obact = CTX_data_active_object(C);
496 Object *obedit = CTX_data_edit_object(C);
497 ScrArea *sa = CTX_wm_area(C);
499 /* first check for editor undo */
500 if (sa && (sa->spacetype == SPACE_IMAGE)) {
501 SpaceImage *sima = (SpaceImage *)sa->spacedata.first;
503 if ((obact && (obact->mode & OB_MODE_TEXTURE_PAINT)) || (sima->mode == SI_MODE_PAINT)) {
504 if (!ED_undo_paint_empty(UNDO_PAINT_IMAGE))
505 return UNDOSYSTEM_IMAPAINT;
508 /* find out which undo system */
510 if (OB_TYPE_SUPPORT_EDITMODE(obedit->type)) {
511 return UNDOSYSTEM_EDITMODE;
516 if (obact->mode & OB_MODE_PARTICLE_EDIT)
517 return UNDOSYSTEM_PARTICLE;
518 else if (obact->mode & OB_MODE_TEXTURE_PAINT) {
519 if (!ED_undo_paint_empty(UNDO_PAINT_IMAGE))
520 return UNDOSYSTEM_IMAPAINT;
522 else if (obact->mode & OB_MODE_SCULPT) {
523 if (!ED_undo_paint_empty(UNDO_PAINT_MESH))
524 return UNDOSYSTEM_SCULPT;
527 if (U.uiflag & USER_GLOBALUNDO)
528 return UNDOSYSTEM_GLOBAL;
534 /* create enum based on undo items */
535 static const EnumPropertyItem *rna_undo_itemf(bContext *C, int undosys, int *totitem)
537 EnumPropertyItem item_tmp = {0}, *item = NULL;
542 const char *name = NULL;
544 if (undosys == UNDOSYSTEM_PARTICLE) {
545 name = PE_undo_get_name(CTX_data_scene(C), i, &active);
547 else if (undosys == UNDOSYSTEM_EDITMODE) {
548 name = undo_editmode_get_name(C, i, &active);
550 else if (undosys == UNDOSYSTEM_IMAPAINT) {
551 name = ED_undo_paint_get_name(C, UNDO_PAINT_IMAGE, i, &active);
553 else if (undosys == UNDOSYSTEM_SCULPT) {
554 name = ED_undo_paint_get_name(C, UNDO_PAINT_MESH, i, &active);
557 name = BKE_undo_get_name(i, &active);
561 item_tmp.identifier = name;
562 /* XXX This won't work with non-default contexts (e.g. operators) :/ */
563 item_tmp.name = IFACE_(name);
565 item_tmp.icon = ICON_RESTRICT_VIEW_OFF;
567 item_tmp.icon = ICON_NONE;
568 item_tmp.value = i++;
569 RNA_enum_item_add(&item, totitem, &item_tmp);
575 RNA_enum_item_end(&item, totitem);
581 static int undo_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
583 int undosys, totitem = 0;
585 undosys = get_undo_system(C);
588 const EnumPropertyItem *item = rna_undo_itemf(C, undosys, &totitem);
591 uiPopupMenu *pup = UI_popup_menu_begin(C, RNA_struct_ui_name(op->type->srna), ICON_NONE);
592 uiLayout *layout = UI_popup_menu_layout(pup);
593 uiLayout *split = uiLayoutSplit(layout, 0.0f, false);
594 uiLayout *column = NULL;
595 const int col_size = 20 + totitem / 12;
599 for (c = 0, i = totitem; i--;) {
600 if (add_col && !(c % col_size)) {
601 column = uiLayoutColumn(split, false);
604 if (item[i].identifier) {
605 uiItemIntO(column, item[i].name, item[i].icon, op->type->idname, "item", item[i].value);
611 MEM_freeN((void *)item);
613 UI_popup_menu_end(C, pup);
617 return OPERATOR_CANCELLED;
620 /* note: also check ed_undo_step() in top if you change notifiers */
621 static int undo_history_exec(bContext *C, wmOperator *op)
623 if (RNA_struct_property_is_set(op->ptr, "item")) {
624 int undosys = get_undo_system(C);
625 int item = RNA_int_get(op->ptr, "item");
627 if (undosys == UNDOSYSTEM_PARTICLE) {
628 PE_undo_number(CTX_data_scene(C), item);
630 else if (undosys == UNDOSYSTEM_EDITMODE) {
631 undo_editmode_number(C, item + 1);
632 WM_event_add_notifier(C, NC_GEOM | ND_DATA, NULL);
634 else if (undosys == UNDOSYSTEM_IMAPAINT) {
635 ED_undo_paint_step_num(C, UNDO_PAINT_IMAGE, item);
637 else if (undosys == UNDOSYSTEM_SCULPT) {
638 ED_undo_paint_step_num(C, UNDO_PAINT_MESH, item);
641 ED_viewport_render_kill_jobs(CTX_wm_manager(C), CTX_data_main(C), true);
642 BKE_undo_number(C, item);
643 WM_event_add_notifier(C, NC_SCENE | ND_LAYER_CONTENT, CTX_data_scene(C));
645 WM_event_add_notifier(C, NC_WINDOW, NULL);
647 return OPERATOR_FINISHED;
649 return OPERATOR_CANCELLED;
652 void ED_OT_undo_history(wmOperatorType *ot)
655 ot->name = "Undo History";
656 ot->description = "Redo specific action in history";
657 ot->idname = "ED_OT_undo_history";
660 ot->invoke = undo_history_invoke;
661 ot->exec = undo_history_exec;
662 ot->poll = ED_operator_screenactive;
664 RNA_def_int(ot->srna, "item", 0, 0, INT_MAX, "Item", "", 0, INT_MAX);