fix for error when changing DISBALE_PYTHON -> WITH_PYTHON,
[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 "BKE_blender.h"
40 #include "BKE_context.h"
41
42 #include "BLI_blenlib.h"
43 #include "BLI_editVert.h"
44 #include "BLI_dynstr.h"
45
46
47 #include "ED_armature.h"
48 #include "ED_particle.h"
49 #include "ED_curve.h"
50 #include "ED_mball.h"
51 #include "ED_mesh.h"
52 #include "ED_object.h"
53 #include "ED_screen.h"
54 #include "ED_sculpt.h"
55 #include "ED_util.h"
56 #include "ED_text.h"
57
58 #include "WM_api.h"
59 #include "WM_types.h"
60
61
62 #include "util_intern.h"
63
64 /* ***************** generic undo system ********************* */
65
66 void ED_undo_push(bContext *C, const char *str)
67 {
68         wmWindowManager *wm= CTX_wm_manager(C);
69         Object *obedit= CTX_data_edit_object(C);
70         Object *obact= CTX_data_active_object(C);
71
72         if(obedit) {
73                 if (U.undosteps == 0) return;
74                 
75                 if(obedit->type==OB_MESH)
76                         undo_push_mesh(C, str);
77                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
78                         undo_push_curve(C, str);
79                 else if (obedit->type==OB_FONT)
80                         undo_push_font(C, str);
81                 else if (obedit->type==OB_MBALL)
82                         undo_push_mball(C, str);
83                 else if (obedit->type==OB_LATTICE)
84                         undo_push_lattice(C, str);
85                 else if (obedit->type==OB_ARMATURE)
86                         undo_push_armature(C, str);
87         }
88         else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
89                 if (U.undosteps == 0) return;
90                 
91                 PE_undo_push(CTX_data_scene(C), str);
92         }
93         else {
94                 if(U.uiflag & USER_GLOBALUNDO) 
95                         BKE_write_undo(C, str);
96         }
97         
98         if(wm->file_saved) {
99                 wm->file_saved= 0;
100                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
101         }
102 }
103
104 static int ed_undo_step(bContext *C, int step, const char *undoname)
105 {       
106         Object *obedit= CTX_data_edit_object(C);
107         Object *obact= CTX_data_active_object(C);
108         ScrArea *sa= CTX_wm_area(C);
109
110         if(sa && sa->spacetype==SPACE_IMAGE) {
111                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
112                 
113                 if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
114                         if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
115                                 if(U.uiflag & USER_GLOBALUNDO)
116                                         BKE_undo_name(C, undoname);
117
118                         WM_event_add_notifier(C, NC_WINDOW, NULL);
119                         return OPERATOR_FINISHED;
120                 }
121         }
122
123         if(sa && sa->spacetype==SPACE_TEXT) {
124                 ED_text_undo_step(C, step);
125         }
126         else if(obedit) {
127                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
128                         if(undoname)
129                                 undo_editmode_name(C, undoname);
130                         else
131                                 undo_editmode_step(C, step);
132
133                         WM_event_add_notifier(C, NC_GEOM|ND_DATA, NULL);
134                 }
135         }
136         else {
137                 int do_glob_undo= 0;
138                 
139                 if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
140                         if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
141                                 do_glob_undo= 1;
142                 }
143                 else if(obact && obact->mode & OB_MODE_SCULPT) {
144                         if(!ED_undo_paint_step(C, UNDO_PAINT_MESH, step, undoname) && undoname)
145                                 do_glob_undo= 1;
146                 }
147                 else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
148                         if(step==1)
149                                 PE_undo(CTX_data_scene(C));
150                         else
151                                 PE_redo(CTX_data_scene(C));
152                 }
153                 else {
154                         do_glob_undo= 1;
155                 }
156                 
157                 if(do_glob_undo) {
158                         if(U.uiflag & USER_GLOBALUNDO) {
159                                 // note python defines not valid here anymore.
160                                 //#ifdef WITH_PYTHON
161                                 // XXX          BPY_scripts_clear_pyobjects();
162                                 //#endif
163                                 if(undoname)
164                                         BKE_undo_name(C, undoname);
165                                 else
166                                         BKE_undo_step(C, step);
167
168                                 WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, CTX_data_scene(C));
169                         }
170                         
171                 }
172         }
173         
174         WM_event_add_notifier(C, NC_WINDOW, NULL);
175         
176         return OPERATOR_FINISHED;
177 }
178
179 void ED_undo_pop(bContext *C)
180 {
181         ed_undo_step(C, 1, NULL);
182 }
183 void ED_undo_redo(bContext *C)
184 {
185         ed_undo_step(C, -1, NULL);
186 }
187
188 void ED_undo_push_op(bContext *C, wmOperator *op)
189 {
190         /* in future, get undo string info? */
191         ED_undo_push(C, op->type->name);
192 }
193
194 void ED_undo_pop_op(bContext *C, wmOperator *op)
195 {
196         /* search back a couple of undo's, in case something else added pushes */
197         ed_undo_step(C, 0, op->type->name);
198 }
199
200 static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op))
201 {
202         /* "last operator" should disappear, later we can tie ths with undo stack nicer */
203         WM_operator_stack_clear(C);
204         return ed_undo_step(C, 1, NULL);
205 }
206
207 static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op))
208 {
209         return ed_undo_step(C, -1, NULL);
210 }
211
212 void ED_undo_menu(bContext *C)
213 {
214         Object *obedit= CTX_data_edit_object(C);
215         Object *obact= CTX_data_active_object(C);
216         
217         if(obedit) {
218                 //if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
219                 //      undo_editmode_menu();
220         }
221         else {
222                 if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
223                         PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
224                 else if(U.uiflag & USER_GLOBALUNDO) {
225                         char *menu= BKE_undo_menu_string();
226                         if(menu) {
227                                 short event= 0; // XXX pupmenu_col(menu, 20);
228                                 MEM_freeN(menu);
229                                 if(event>0) {
230                                         BKE_undo_number(C, event);
231                                 }
232                         }
233                 }
234         }
235 }
236
237 /* ********************** */
238
239 void ED_OT_undo(wmOperatorType *ot)
240 {
241         /* identifiers */
242         ot->name= "Undo";
243         ot->description= "Undo previous action";
244         ot->idname= "ED_OT_undo";
245         
246         /* api callbacks */
247         ot->exec= ed_undo_exec;
248         ot->poll= ED_operator_screenactive;
249 }
250
251 void ED_OT_redo(wmOperatorType *ot)
252 {
253         /* identifiers */
254         ot->name= "Redo";
255         ot->description= "Redo previous action";
256         ot->idname= "ED_OT_redo";
257         
258         /* api callbacks */
259         ot->exec= ed_redo_exec;
260         ot->poll= ED_operator_screenactive;
261 }
262
263