60f526b940ad296d1c08966986a9ea82b7093c40
[blender.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
64 #include "util_intern.h"
65
66 /* ***************** generic undo system ********************* */
67
68 void ED_undo_push(bContext *C, const char *str)
69 {
70         wmWindowManager *wm= CTX_wm_manager(C);
71         Object *obedit= CTX_data_edit_object(C);
72         Object *obact= CTX_data_active_object(C);
73
74         if(obedit) {
75                 if (U.undosteps == 0) return;
76                 
77                 if(obedit->type==OB_MESH)
78                         undo_push_mesh(C, str);
79                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
80                         undo_push_curve(C, str);
81                 else if (obedit->type==OB_FONT)
82                         undo_push_font(C, str);
83                 else if (obedit->type==OB_MBALL)
84                         undo_push_mball(C, str);
85                 else if (obedit->type==OB_LATTICE)
86                         undo_push_lattice(C, str);
87                 else if (obedit->type==OB_ARMATURE)
88                         undo_push_armature(C, str);
89         }
90         else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
91                 if (U.undosteps == 0) return;
92                 
93                 PE_undo_push(CTX_data_scene(C), str);
94         }
95         else {
96                 if(U.uiflag & USER_GLOBALUNDO) 
97                         BKE_write_undo(C, str);
98         }
99         
100         if(wm->file_saved) {
101                 wm->file_saved= 0;
102                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
103         }
104 }
105
106 static int ed_undo_step(bContext *C, int step, const char *undoname)
107 {       
108         Object *obedit= CTX_data_edit_object(C);
109         Object *obact= CTX_data_active_object(C);
110         ScrArea *sa= CTX_wm_area(C);
111
112         if(sa && sa->spacetype==SPACE_IMAGE) {
113                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
114                 
115                 if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
116                         if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
117                                 if(U.uiflag & USER_GLOBALUNDO)
118                                         BKE_undo_name(C, undoname);
119
120                         WM_event_add_notifier(C, NC_WINDOW, NULL);
121                         return OPERATOR_FINISHED;
122                 }
123         }
124
125         if(sa && sa->spacetype==SPACE_TEXT) {
126                 ED_text_undo_step(C, step);
127         }
128         else if(obedit) {
129                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
130                         if(undoname)
131                                 undo_editmode_name(C, undoname);
132                         else
133                                 undo_editmode_step(C, step);
134
135                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, NULL);
136                 }
137         }
138         else {
139                 int do_glob_undo= 0;
140                 
141                 if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
142                         if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
143                                 do_glob_undo= 1;
144                 }
145                 else if(obact && obact->mode & OB_MODE_SCULPT) {
146                         if(!ED_undo_paint_step(C, UNDO_PAINT_MESH, step, undoname) && undoname)
147                                 do_glob_undo= 1;
148                 }
149                 else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
150                         if(step==1)
151                                 PE_undo(CTX_data_scene(C));
152                         else
153                                 PE_redo(CTX_data_scene(C));
154                 }
155                 else {
156                         do_glob_undo= 1;
157                 }
158                 
159                 if(do_glob_undo) {
160                         if(U.uiflag & USER_GLOBALUNDO) {
161                                 // note python defines not valid here anymore.
162                                 //#ifdef WITH_PYTHON
163                                 // XXX          BPY_scripts_clear_pyobjects();
164                                 //#endif
165                                 if(undoname)
166                                         BKE_undo_name(C, undoname);
167                                 else
168                                         BKE_undo_step(C, step);
169
170                                 WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, CTX_data_scene(C));
171                         }
172                         
173                 }
174         }
175         
176         WM_event_add_notifier(C, NC_WINDOW, NULL);
177         
178         return OPERATOR_FINISHED;
179 }
180
181 void ED_undo_pop(bContext *C)
182 {
183         ed_undo_step(C, 1, NULL);
184 }
185 void ED_undo_redo(bContext *C)
186 {
187         ed_undo_step(C, -1, NULL);
188 }
189
190 void ED_undo_push_op(bContext *C, wmOperator *op)
191 {
192         /* in future, get undo string info? */
193         ED_undo_push(C, op->type->name);
194 }
195
196 void ED_undo_pop_op(bContext *C, wmOperator *op)
197 {
198         /* search back a couple of undo's, in case something else added pushes */
199         ed_undo_step(C, 0, op->type->name);
200 }
201
202 static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op))
203 {
204         /* "last operator" should disappear, later we can tie ths with undo stack nicer */
205         WM_operator_stack_clear(C);
206         return ed_undo_step(C, 1, NULL);
207 }
208
209 static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op))
210 {
211         return ed_undo_step(C, -1, NULL);
212 }
213
214 void ED_undo_menu(bContext *C)
215 {
216         Object *obedit= CTX_data_edit_object(C);
217         Object *obact= CTX_data_active_object(C);
218         
219         if(obedit) {
220                 //if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
221                 //      undo_editmode_menu();
222         }
223         else {
224                 if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
225                         PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
226                 else if(U.uiflag & USER_GLOBALUNDO) {
227                         char *menu= BKE_undo_menu_string();
228                         if(menu) {
229                                 short event= 0; // XXX pupmenu_col(menu, 20);
230                                 MEM_freeN(menu);
231                                 if(event>0) {
232                                         BKE_undo_number(C, event);
233                                 }
234                         }
235                 }
236         }
237 }
238
239 /* ********************** */
240
241 void ED_OT_undo(wmOperatorType *ot)
242 {
243         /* identifiers */
244         ot->name= "Undo";
245         ot->description= "Undo previous action";
246         ot->idname= "ED_OT_undo";
247         
248         /* api callbacks */
249         ot->exec= ed_undo_exec;
250         ot->poll= ED_operator_screenactive;
251 }
252
253 void ED_OT_redo(wmOperatorType *ot)
254 {
255         /* identifiers */
256         ot->name= "Redo";
257         ot->description= "Redo previous action";
258         ot->idname= "ED_OT_redo";
259         
260         /* api callbacks */
261         ot->exec= ed_redo_exec;
262         ot->poll= ED_operator_screenactive;
263 }
264
265
266 /* ui callbacks should call this rather then calling WM_operator_repeat() themselves */
267 int ED_undo_operator_repeat(bContext *C, struct wmOperator *op)
268 {
269         int ret= 0;
270
271         if(op) {
272                 ARegion *ar= CTX_wm_region(C);
273                 ARegion *ar1= BKE_area_find_region_type(CTX_wm_area(C), RGN_TYPE_WINDOW);
274
275                 if(ar1)
276                         CTX_wm_region_set(C, ar1);
277
278                 if(WM_operator_repeat_check(C, op) && WM_operator_poll(C, op->type)) {
279                         int retval;
280
281                         if (G.f & G_DEBUG)
282                                 printf("redo_cb: operator redo %s\n", op->type->name);
283                         ED_undo_pop_op(C, op);
284                         retval= WM_operator_repeat(C, op);
285                         if((retval & OPERATOR_FINISHED)==0) {
286                                 if (G.f & G_DEBUG)
287                                         printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval);
288                                 ED_undo_redo(C);
289                         }
290                         else {
291                                 ret= 1;
292                         }
293                 }
294
295                 /* set region back */
296                 CTX_wm_region_set(C, ar);
297         }
298         else {
299                 if (G.f & G_DEBUG) {
300                         printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name);
301                 }
302         }
303
304         return ret;
305 }
306
307 void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused))
308 {
309         ED_undo_operator_repeat(C, (wmOperator *)arg_op);
310 }
311
312 void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event))
313 {
314         ED_undo_operator_repeat(C, (wmOperator *)arg_op);
315 }