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