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