style cleanup: follow style guide for formatting of if/for/while loops, and else...
[blender.git] / source / blender / editors / util / undo.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
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.
8  *
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.
13  *
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.
17  *
18  * The Original Code is Copyright (C) 2004 Blender Foundation
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/util/undo.c
29  *  \ingroup edutil
30  */
31
32
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <math.h>
37
38 #include "MEM_guardedalloc.h"
39
40 #include "DNA_mesh_types.h"
41 #include "DNA_object_types.h"
42
43 #include "BLI_blenlib.h"
44 #include "BLI_dynstr.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_blender.h"
48 #include "BKE_context.h"
49 #include "BKE_global.h"
50 #include "BKE_screen.h"
51 #include "BKE_tessmesh.h" /* BMESH_EM_UNDO_RECALC_TESSFACE_WORKAROUND */
52
53
54 #include "ED_armature.h"
55 #include "ED_particle.h"
56 #include "ED_curve.h"
57 #include "ED_gpencil.h"
58 #include "ED_mball.h"
59 #include "ED_mesh.h"
60 #include "ED_object.h"
61 #include "ED_screen.h"
62 #include "ED_sculpt.h"
63 #include "ED_util.h"
64 #include "ED_text.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "RNA_access.h"
70 #include "RNA_define.h"
71
72 #include "UI_interface.h"
73 #include "UI_resources.h"
74
75 #include "util_intern.h"
76
77 #define MAXUNDONAME 64 /* XXX, make common define */
78
79 /* ***************** generic undo system ********************* */
80
81 void ED_undo_push(bContext *C, const char *str)
82 {
83         wmWindowManager *wm= CTX_wm_manager(C);
84         Object *obedit= CTX_data_edit_object(C);
85         Object *obact= CTX_data_active_object(C);
86
87         if (G.f & G_DEBUG)
88                 printf("undo push %s\n", str);
89         
90         if (obedit) {
91
92 #ifdef BMESH_EM_UNDO_RECALC_TESSFACE_WORKAROUND
93                 /* undo is causing tessface recalc, so without we need to do explicitly */
94
95                 if (U.undosteps == 0) {
96                         if (obedit->type == OB_MESH) {
97                                 Mesh *me= obedit->data;
98                                 BMEdit_RecalcTessellation(me->edit_btmesh);
99                         }
100                 }
101
102 #endif /* BMESH_EM_UNDO_RECALC_TESSFACE_WORKAROUND */
103
104                 if (U.undosteps == 0) return;
105                 
106                 if (obedit->type==OB_MESH)
107                         undo_push_mesh(C, str);
108                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
109                         undo_push_curve(C, str);
110                 else if (obedit->type==OB_FONT)
111                         undo_push_font(C, str);
112                 else if (obedit->type==OB_MBALL)
113                         undo_push_mball(C, str);
114                 else if (obedit->type==OB_LATTICE)
115                         undo_push_lattice(C, str);
116                 else if (obedit->type==OB_ARMATURE)
117                         undo_push_armature(C, str);
118         }
119         else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
120                 if (U.undosteps == 0) return;
121                 
122                 PE_undo_push(CTX_data_scene(C), str);
123         }
124         else {
125                 if (U.uiflag & USER_GLOBALUNDO) 
126                         BKE_write_undo(C, str);
127         }
128         
129         if (wm->file_saved) {
130                 wm->file_saved= 0;
131                 /* notifier that data changed, for save-over warning or header */
132                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
133         }
134 }
135
136 /* note: also check undo_history_exec() in bottom if you change notifiers */
137 static int ed_undo_step(bContext *C, int step, const char *undoname)
138 {       
139         Object *obedit= CTX_data_edit_object(C);
140         Object *obact= CTX_data_active_object(C);
141         ScrArea *sa= CTX_wm_area(C);
142
143         /* undo during jobs are running can easily lead to freeing data using by jobs,
144          * or they can just lead to freezing job in some other cases */
145         if (WM_jobs_test(CTX_wm_manager(C), CTX_data_scene(C))) {
146                 return OPERATOR_CANCELLED;
147         }
148
149         /* grease pencil can be can be used in plenty of spaces, so check it first */
150         if (ED_gpencil_session_active()) {
151                 return ED_undo_gpencil_step(C, step, undoname);
152         }
153
154         if (sa && sa->spacetype==SPACE_IMAGE) {
155                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
156                 
157                 if ((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
158                         if (!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
159                                 if (U.uiflag & USER_GLOBALUNDO)
160                                         BKE_undo_name(C, undoname);
161
162                         WM_event_add_notifier(C, NC_WINDOW, NULL);
163                         return OPERATOR_FINISHED;
164                 }
165         }
166
167         if (sa && sa->spacetype==SPACE_TEXT) {
168                 ED_text_undo_step(C, step);
169         }
170         else if (obedit) {
171                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
172                         if (undoname)
173                                 undo_editmode_name(C, undoname);
174                         else
175                                 undo_editmode_step(C, step);
176
177                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, NULL);
178                 }
179         }
180         else {
181                 int do_glob_undo= 0;
182                 
183                 if (obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
184                         if (!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname))
185                                 do_glob_undo= 1;
186                 }
187                 else if (obact && obact->mode & OB_MODE_SCULPT) {
188                         if (!ED_undo_paint_step(C, UNDO_PAINT_MESH, step, undoname))
189                                 do_glob_undo= 1;
190                 }
191                 else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
192                         if (step==1)
193                                 PE_undo(CTX_data_scene(C));
194                         else
195                                 PE_redo(CTX_data_scene(C));
196                 }
197                 else {
198                         do_glob_undo= 1;
199                 }
200                 
201                 if (do_glob_undo) {
202                         if (U.uiflag & USER_GLOBALUNDO) {
203                                 // note python defines not valid here anymore.
204                                 //#ifdef WITH_PYTHON
205                                 // XXX          BPY_scripts_clear_pyobjects();
206                                 //#endif
207                                 if (undoname)
208                                         BKE_undo_name(C, undoname);
209                                 else
210                                         BKE_undo_step(C, step);
211
212                                 WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, CTX_data_scene(C));
213                         }
214                         
215                 }
216         }
217         
218         WM_event_add_notifier(C, NC_WINDOW, NULL);
219         
220         return OPERATOR_FINISHED;
221 }
222
223 void ED_undo_pop(bContext *C)
224 {
225         ed_undo_step(C, 1, NULL);
226 }
227 void ED_undo_redo(bContext *C)
228 {
229         ed_undo_step(C, -1, NULL);
230 }
231
232 void ED_undo_push_op(bContext *C, wmOperator *op)
233 {
234         /* in future, get undo string info? */
235         ED_undo_push(C, op->type->name);
236 }
237
238 void ED_undo_pop_op(bContext *C, wmOperator *op)
239 {
240         /* search back a couple of undo's, in case something else added pushes */
241         ed_undo_step(C, 0, op->type->name);
242 }
243
244 /* name optionally, function used to check for operator redo panel */
245 int ED_undo_valid(const bContext *C, const char *undoname)
246 {
247         Object *obedit= CTX_data_edit_object(C);
248         Object *obact= CTX_data_active_object(C);
249         ScrArea *sa= CTX_wm_area(C);
250         
251         if (sa && sa->spacetype==SPACE_IMAGE) {
252                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
253                 
254                 if ((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
255                         return 1;
256                 }
257         }
258         
259         if (sa && sa->spacetype==SPACE_TEXT) {
260                 return 1;
261         }
262         else if (obedit) {
263                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
264                         return undo_editmode_valid(undoname);
265                 }
266         }
267         else {
268                 
269                 /* if below tests fail, global undo gets executed */
270                 
271                 if (obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
272                         if ( ED_undo_paint_valid(UNDO_PAINT_IMAGE, undoname) )
273                                 return 1;
274                 }
275                 else if (obact && obact->mode & OB_MODE_SCULPT) {
276                         if ( ED_undo_paint_valid(UNDO_PAINT_MESH, undoname) )
277                                 return 1;
278                 }
279                 else if (obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
280                         return PE_undo_valid(CTX_data_scene(C));
281                 }
282                 
283                 if (U.uiflag & USER_GLOBALUNDO) {
284                         return BKE_undo_valid(undoname);
285                 }
286         }
287         return 0;
288 }
289
290 static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op))
291 {
292         /* "last operator" should disappear, later we can tie ths with undo stack nicer */
293         WM_operator_stack_clear(CTX_wm_manager(C));
294         return ed_undo_step(C, 1, NULL);
295 }
296
297 static int ed_undo_push_exec(bContext *C, wmOperator *op)
298 {
299         char str[MAXUNDONAME];
300         RNA_string_get(op->ptr, "message", str);
301         ED_undo_push(C, str);
302         return OPERATOR_FINISHED;
303 }
304
305 static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op))
306 {
307         return ed_undo_step(C, -1, NULL);
308 }
309
310
311 /* ********************** */
312
313 void ED_OT_undo(wmOperatorType *ot)
314 {
315         /* identifiers */
316         ot->name = "Undo";
317         ot->description = "Undo previous action";
318         ot->idname = "ED_OT_undo";
319         
320         /* api callbacks */
321         ot->exec = ed_undo_exec;
322         ot->poll = ED_operator_screenactive;
323 }
324
325 void ED_OT_undo_push(wmOperatorType *ot)
326 {
327         /* identifiers */
328         ot->name = "Undo Push";
329         ot->description = "Add an undo state (internal use only)";
330         ot->idname = "ED_OT_undo_push";
331         
332         /* api callbacks */
333         ot->exec = ed_undo_push_exec;
334
335         ot->flag = OPTYPE_INTERNAL;
336
337         RNA_def_string(ot->srna, "message", "Add an undo step *function may be moved*", MAXUNDONAME, "Undo Message", "");
338 }
339
340 void ED_OT_redo(wmOperatorType *ot)
341 {
342         /* identifiers */
343         ot->name = "Redo";
344         ot->description = "Redo previous action";
345         ot->idname = "ED_OT_redo";
346         
347         /* api callbacks */
348         ot->exec = ed_redo_exec;
349         ot->poll = ED_operator_screenactive;
350 }
351
352
353 /* ui callbacks should call this rather than calling WM_operator_repeat() themselves */
354 int ED_undo_operator_repeat(bContext *C, struct wmOperator *op)
355 {
356         int ret= 0;
357
358         if (op) {
359                 wmWindowManager *wm= CTX_wm_manager(C);
360                 struct Scene *scene= CTX_data_scene(C);
361
362                 ARegion *ar= CTX_wm_region(C);
363                 ARegion *ar1= BKE_area_find_region_type(CTX_wm_area(C), RGN_TYPE_WINDOW);
364
365                 if (ar1)
366                         CTX_wm_region_set(C, ar1);
367
368                 if ( (WM_operator_repeat_check(C, op)) &&
369                      (WM_operator_poll(C, op->type)) &&
370                      /* note, undo/redo cant run if there are jobs active,
371                       * check for screen jobs only so jobs like material/texture/world preview
372                       * (which copy their data), wont stop redo, see [#29579]],
373                       *
374                       * note, - WM_operator_check_ui_enabled() jobs test _must_ stay in sync with this */
375                      (WM_jobs_test(wm, scene) == 0))
376                 {
377                         int retval;
378
379                         if (G.f & G_DEBUG)
380                                 printf("redo_cb: operator redo %s\n", op->type->name);
381                         ED_undo_pop_op(C, op);
382
383                         if (op->type->check) {
384                                 op->type->check(C, op); /* ignore return value since its running again anyway */
385                         }
386
387                         retval= WM_operator_repeat(C, op);
388                         if ((retval & OPERATOR_FINISHED)==0) {
389                                 if (G.f & G_DEBUG)
390                                         printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval);
391                                 ED_undo_redo(C);
392                         }
393                         else {
394                                 ret= 1;
395                         }
396                 }
397                 else {
398                         if (G.f & G_DEBUG) {
399                                 printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name);
400                         }
401                 }
402
403                 /* set region back */
404                 CTX_wm_region_set(C, ar);
405         }
406         else {
407                 if (G.f & G_DEBUG) {
408                         printf("redo_cb: ED_undo_operator_repeat called with NULL 'op'\n");
409                 }
410         }
411
412         return ret;
413 }
414
415
416 void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused))
417 {
418         ED_undo_operator_repeat(C, (wmOperator *)arg_op);
419 }
420
421 void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event))
422 {
423         ED_undo_operator_repeat(C, (wmOperator *)arg_op);
424 }
425
426
427 /* ************************** */
428
429 #define UNDOSYSTEM_GLOBAL       1
430 #define UNDOSYSTEM_EDITMODE     2
431 #define UNDOSYSTEM_PARTICLE     3
432
433 static int get_undo_system(bContext *C)
434 {
435         Object *obedit= CTX_data_edit_object(C);
436         
437         /* find out which undo system */
438         if (obedit) {
439                 if (ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE))
440                         return UNDOSYSTEM_EDITMODE;
441         }
442         else {
443                 Object *obact= CTX_data_active_object(C);
444                 
445                 if (obact && obact->mode & OB_MODE_PARTICLE_EDIT)
446                         return UNDOSYSTEM_PARTICLE;
447                 else if (U.uiflag & USER_GLOBALUNDO)
448                         return UNDOSYSTEM_GLOBAL;
449         }
450         
451         return 0;
452 }
453
454 /* create enum based on undo items */
455 static EnumPropertyItem *rna_undo_itemf(bContext *C, int undosys, int *totitem)
456 {
457         EnumPropertyItem item_tmp= {0}, *item= NULL;
458         int active, i= 0;
459         
460         while (TRUE) {
461                 const char *name= NULL;
462                 
463                 if (undosys==UNDOSYSTEM_PARTICLE) {
464                         name= PE_undo_get_name(CTX_data_scene(C), i, &active);
465                 }
466                 else if (undosys==UNDOSYSTEM_EDITMODE) {
467                         name= undo_editmode_get_name(C, i, &active);
468                 }
469                 else {
470                         name= BKE_undo_get_name(i, &active);
471                 }
472                 
473                 if (name) {
474                         item_tmp.identifier= item_tmp.name= name;
475                         if (active)
476                                 item_tmp.icon= ICON_RESTRICT_VIEW_OFF;
477                         else 
478                                 item_tmp.icon= ICON_NONE;
479                         item_tmp.value= i++;
480                         RNA_enum_item_add(&item, totitem, &item_tmp);
481                 }
482                 else
483                         break;
484         }
485         
486         RNA_enum_item_end(&item, totitem);
487         
488         return item;
489 }
490
491
492 static int undo_history_invoke(bContext *C, wmOperator *op, wmEvent *UNUSED(event))
493 {
494         int undosys, totitem= 0;
495         
496         undosys= get_undo_system(C);
497         
498         if (undosys) {
499                 EnumPropertyItem *item= rna_undo_itemf(C, undosys, &totitem);
500                 
501                 if (totitem > 0) {
502                         uiPopupMenu *pup= uiPupMenuBegin(C, op->type->name, ICON_NONE);
503                         uiLayout *layout= uiPupMenuLayout(pup);
504                         uiLayout *split= uiLayoutSplit(layout, 0, 0), *column = NULL;
505                         int i, c;
506                         
507                         for (c=0, i=totitem-1; i >= 0; i--, c++) {
508                                 if ( (c % 20)==0 )
509                                         column= uiLayoutColumn(split, 0);
510                                 if (item[i].identifier)
511                                         uiItemIntO(column, item[i].name, item[i].icon, op->type->idname, "item", item[i].value);
512                                 
513                         }
514                         
515                         MEM_freeN(item);
516                         
517                         uiPupMenuEnd(C, pup);
518                 }               
519                 
520         }
521         return OPERATOR_CANCELLED;
522 }
523
524 /* note: also check ed_undo_step() in top if you change notifiers */
525 static int undo_history_exec(bContext *C, wmOperator *op)
526 {
527         if (RNA_struct_property_is_set(op->ptr, "item")) {
528                 int undosys= get_undo_system(C);
529                 int item= RNA_int_get(op->ptr, "item");
530                 
531                 if (undosys==UNDOSYSTEM_PARTICLE) {
532                         PE_undo_number(CTX_data_scene(C), item);
533                 }
534                 else if (undosys==UNDOSYSTEM_EDITMODE) {
535                         undo_editmode_number(C, item+1);
536                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, NULL);
537                 }
538                 else {
539                         BKE_undo_number(C, item);
540                         WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, CTX_data_scene(C));
541                 }
542                 WM_event_add_notifier(C, NC_WINDOW, NULL);
543                 
544                 return OPERATOR_FINISHED;
545         }
546         return OPERATOR_CANCELLED;
547 }
548
549 void ED_OT_undo_history(wmOperatorType *ot)
550 {
551         /* identifiers */
552         ot->name = "Undo History";
553         ot->description = "Redo specific action in history";
554         ot->idname = "ED_OT_undo_history";
555         
556         /* api callbacks */
557         ot->invoke = undo_history_invoke;
558         ot->exec = undo_history_exec;
559         ot->poll = ED_operator_screenactive;
560         
561         RNA_def_int(ot->srna, "item", 0, 0, INT_MAX, "Item", "", 0, INT_MAX);
562
563 }
564
565