operator ED_OT_undo_push, needed for editmode undo/redo glitch fix, (coming next).
[blender-staging.git] / source / blender / editors / util / undo.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2004 Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_object_types.h"
38
39 #include "BLI_blenlib.h"
40 #include "BLI_editVert.h"
41 #include "BLI_dynstr.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_blender.h"
45 #include "BKE_context.h"
46 #include "BKE_global.h"
47 #include "BKE_screen.h"
48
49 #include "ED_armature.h"
50 #include "ED_particle.h"
51 #include "ED_curve.h"
52 #include "ED_mball.h"
53 #include "ED_mesh.h"
54 #include "ED_object.h"
55 #include "ED_screen.h"
56 #include "ED_sculpt.h"
57 #include "ED_util.h"
58 #include "ED_text.h"
59
60 #include "WM_api.h"
61 #include "WM_types.h"
62
63 #include "RNA_access.h"
64 #include "RNA_define.h"
65
66 #include "util_intern.h"
67
68 #define MAXUNDONAME 64 /* XXX, make common define */
69
70 /* ***************** generic undo system ********************* */
71
72 void ED_undo_push(bContext *C, const char *str)
73 {
74         wmWindowManager *wm= CTX_wm_manager(C);
75         Object *obedit= CTX_data_edit_object(C);
76         Object *obact= CTX_data_active_object(C);
77
78         if(obedit) {
79                 if (U.undosteps == 0) return;
80                 
81                 if(obedit->type==OB_MESH)
82                         undo_push_mesh(C, str);
83                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
84                         undo_push_curve(C, str);
85                 else if (obedit->type==OB_FONT)
86                         undo_push_font(C, str);
87                 else if (obedit->type==OB_MBALL)
88                         undo_push_mball(C, str);
89                 else if (obedit->type==OB_LATTICE)
90                         undo_push_lattice(C, str);
91                 else if (obedit->type==OB_ARMATURE)
92                         undo_push_armature(C, str);
93         }
94         else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
95                 if (U.undosteps == 0) return;
96                 
97                 PE_undo_push(CTX_data_scene(C), str);
98         }
99         else {
100                 if(U.uiflag & USER_GLOBALUNDO) 
101                         BKE_write_undo(C, str);
102         }
103         
104         if(wm->file_saved) {
105                 wm->file_saved= 0;
106                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
107         }
108 }
109
110 static int ed_undo_step(bContext *C, int step, const char *undoname)
111 {       
112         Object *obedit= CTX_data_edit_object(C);
113         Object *obact= CTX_data_active_object(C);
114         ScrArea *sa= CTX_wm_area(C);
115
116         if(sa && sa->spacetype==SPACE_IMAGE) {
117                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
118                 
119                 if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
120                         if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
121                                 if(U.uiflag & USER_GLOBALUNDO)
122                                         BKE_undo_name(C, undoname);
123
124                         WM_event_add_notifier(C, NC_WINDOW, NULL);
125                         return OPERATOR_FINISHED;
126                 }
127         }
128
129         if(sa && sa->spacetype==SPACE_TEXT) {
130                 ED_text_undo_step(C, step);
131         }
132         else if(obedit) {
133                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
134                         if(undoname)
135                                 undo_editmode_name(C, undoname);
136                         else
137                                 undo_editmode_step(C, step);
138
139                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, NULL);
140                 }
141         }
142         else {
143                 int do_glob_undo= 0;
144                 
145                 if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
146                         if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
147                                 do_glob_undo= 1;
148                 }
149                 else if(obact && obact->mode & OB_MODE_SCULPT) {
150                         if(!ED_undo_paint_step(C, UNDO_PAINT_MESH, step, undoname) && undoname)
151                                 do_glob_undo= 1;
152                 }
153                 else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
154                         if(step==1)
155                                 PE_undo(CTX_data_scene(C));
156                         else
157                                 PE_redo(CTX_data_scene(C));
158                 }
159                 else {
160                         do_glob_undo= 1;
161                 }
162                 
163                 if(do_glob_undo) {
164                         if(U.uiflag & USER_GLOBALUNDO) {
165                                 // note python defines not valid here anymore.
166                                 //#ifdef WITH_PYTHON
167                                 // XXX          BPY_scripts_clear_pyobjects();
168                                 //#endif
169                                 if(undoname)
170                                         BKE_undo_name(C, undoname);
171                                 else
172                                         BKE_undo_step(C, step);
173
174                                 WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, CTX_data_scene(C));
175                         }
176                         
177                 }
178         }
179         
180         WM_event_add_notifier(C, NC_WINDOW, NULL);
181         
182         return OPERATOR_FINISHED;
183 }
184
185 void ED_undo_pop(bContext *C)
186 {
187         ed_undo_step(C, 1, NULL);
188 }
189 void ED_undo_redo(bContext *C)
190 {
191         ed_undo_step(C, -1, NULL);
192 }
193
194 void ED_undo_push_op(bContext *C, wmOperator *op)
195 {
196         /* in future, get undo string info? */
197         ED_undo_push(C, op->type->name);
198 }
199
200 void ED_undo_pop_op(bContext *C, wmOperator *op)
201 {
202         /* search back a couple of undo's, in case something else added pushes */
203         ed_undo_step(C, 0, op->type->name);
204 }
205
206 /* name optionally, function used to check for operator redo panel */
207 int ED_undo_valid(const bContext *C, const char *undoname)
208 {
209         Object *obedit= CTX_data_edit_object(C);
210         Object *obact= CTX_data_active_object(C);
211         ScrArea *sa= CTX_wm_area(C);
212         
213         if(sa && sa->spacetype==SPACE_IMAGE) {
214                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
215                 
216                 if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
217                         return 1;
218                 }
219         }
220         
221         if(sa && sa->spacetype==SPACE_TEXT) {
222                 return 1;
223         }
224         else if(obedit) {
225                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
226                         return undo_editmode_valid(undoname);
227                 }
228         }
229         else {
230                 
231                 /* if below tests fail, global undo gets executed */
232                 
233                 if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
234                         if( ED_undo_paint_valid(UNDO_PAINT_IMAGE, undoname) )
235                                 return 1;
236                 }
237                 else if(obact && obact->mode & OB_MODE_SCULPT) {
238                         if( ED_undo_paint_valid(UNDO_PAINT_MESH, undoname) )
239                                 return 1;
240                 }
241                 else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
242                         return PE_undo_valid(CTX_data_scene(C));
243                 }
244                 
245                 if(U.uiflag & USER_GLOBALUNDO) {
246                         return BKE_undo_valid(undoname);
247                 }
248         }
249         return 0;
250 }
251
252 static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op))
253 {
254         /* "last operator" should disappear, later we can tie ths with undo stack nicer */
255         WM_operator_stack_clear(C);
256         return ed_undo_step(C, 1, NULL);
257 }
258
259 static int ed_undo_push_exec(bContext *C, wmOperator *op)
260 {
261         char str[MAXUNDONAME];
262         RNA_string_get(op->ptr, "message", str);
263         ED_undo_push(C, str);
264         return OPERATOR_FINISHED;
265 }
266
267 static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op))
268 {
269         return ed_undo_step(C, -1, NULL);
270 }
271
272 #if 0 /* UNUSED */
273 void ED_undo_menu(bContext *C)
274 {
275         Object *obedit= CTX_data_edit_object(C);
276         Object *obact= CTX_data_active_object(C);
277         
278         if(obedit) {
279                 //if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
280                 //      undo_editmode_menu();
281         }
282         else {
283                 if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
284                         PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
285                 else if(U.uiflag & USER_GLOBALUNDO) {
286                         char *menu= BKE_undo_menu_string();
287                         if(menu) {
288                                 short event= 0; // XXX pupmenu_col(menu, 20);
289                                 MEM_freeN(menu);
290                                 if(event>0) {
291                                         BKE_undo_number(C, event);
292                                 }
293                         }
294                 }
295         }
296 }
297 #endif
298
299 /* ********************** */
300
301 void ED_OT_undo(wmOperatorType *ot)
302 {
303         /* identifiers */
304         ot->name= "Undo";
305         ot->description= "Undo previous action";
306         ot->idname= "ED_OT_undo";
307         
308         /* api callbacks */
309         ot->exec= ed_undo_exec;
310         ot->poll= ED_operator_screenactive;
311 }
312
313 void ED_OT_undo_push(wmOperatorType *ot)
314 {
315         /* identifiers */
316         ot->name= "Undo Push";
317         ot->description= "Add an undo state (internal use only)";
318         ot->idname= "ED_OT_undo_push";
319         
320         /* api callbacks */
321         ot->exec= ed_undo_push_exec;
322
323         RNA_def_string(ot->srna, "message", "Add an undo step *function may be moved*", MAXUNDONAME, "Undo Message", "");
324 }
325
326 void ED_OT_redo(wmOperatorType *ot)
327 {
328         /* identifiers */
329         ot->name= "Redo";
330         ot->description= "Redo previous action";
331         ot->idname= "ED_OT_redo";
332         
333         /* api callbacks */
334         ot->exec= ed_redo_exec;
335         ot->poll= ED_operator_screenactive;
336 }
337
338
339 /* ui callbacks should call this rather then calling WM_operator_repeat() themselves */
340 int ED_undo_operator_repeat(bContext *C, struct wmOperator *op)
341 {
342         int ret= 0;
343
344         if(op) {
345                 ARegion *ar= CTX_wm_region(C);
346                 ARegion *ar1= BKE_area_find_region_type(CTX_wm_area(C), RGN_TYPE_WINDOW);
347
348                 if(ar1)
349                         CTX_wm_region_set(C, ar1);
350
351                 if(WM_operator_repeat_check(C, op) && WM_operator_poll(C, op->type)) {
352                         int retval;
353
354                         if (G.f & G_DEBUG)
355                                 printf("redo_cb: operator redo %s\n", op->type->name);
356                         ED_undo_pop_op(C, op);
357                         retval= WM_operator_repeat(C, op);
358                         if((retval & OPERATOR_FINISHED)==0) {
359                                 if (G.f & G_DEBUG)
360                                         printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval);
361                                 ED_undo_redo(C);
362                         }
363                         else {
364                                 ret= 1;
365                         }
366                 }
367
368                 /* set region back */
369                 CTX_wm_region_set(C, ar);
370         }
371         else {
372                 if (G.f & G_DEBUG) {
373                         printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name);
374                 }
375         }
376
377         return ret;
378 }
379
380 void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused))
381 {
382         ED_undo_operator_repeat(C, (wmOperator *)arg_op);
383 }
384
385 void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event))
386 {
387         ED_undo_operator_repeat(C, (wmOperator *)arg_op);
388 }