2.5 audio cleanup:
[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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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_mesh.h"
62 #include "ED_object.h"
63 #include "ED_screen.h"
64 #include "ED_sculpt.h"
65 #include "ED_util.h"
66 #include "ED_text.h"
67
68 #include "WM_api.h"
69 #include "WM_types.h"
70
71 #include "UI_interface.h"
72 #include "UI_resources.h"
73
74 #include "util_intern.h"
75
76 /* ***************** generic undo system ********************* */
77
78 void ED_undo_push(bContext *C, char *str)
79 {
80         wmWindowManager *wm= CTX_wm_manager(C);
81         Object *obedit= CTX_data_edit_object(C);
82
83         if(obedit) {
84                 if (U.undosteps == 0) return;
85                 
86                 if(obedit->type==OB_MESH)
87                         undo_push_mesh(C, str);
88                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
89                         undo_push_curve(C, str);
90                 else if (obedit->type==OB_FONT)
91                         undo_push_font(C, str);
92                 else if (obedit->type==OB_MBALL)
93                         undo_push_mball(C, str);
94                 else if (obedit->type==OB_LATTICE)
95                         undo_push_lattice(C, str);
96                 else if (obedit->type==OB_ARMATURE)
97                         undo_push_armature(C, str);
98         }
99         else if(G.f & G_PARTICLEEDIT) {
100                 if (U.undosteps == 0) return;
101                 
102                 PE_undo_push(CTX_data_scene(C), str);
103         }
104         else {
105                 if(U.uiflag & USER_GLOBALUNDO) 
106                         BKE_write_undo(C, str);
107         }
108         
109         if(wm->file_saved) {
110                 wm->file_saved= 0;
111                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
112         }
113 }
114
115 static int ed_undo_step(bContext *C, int step, const char *undoname)
116 {       
117         Object *obedit= CTX_data_edit_object(C);
118         ScrArea *sa= CTX_wm_area(C);
119
120         if(sa && sa->spacetype==SPACE_IMAGE) {
121                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
122                 
123                 if(G.f & G_TEXTUREPAINT || sima->flag & SI_DRAWTOOL) {
124                         undo_imagepaint_step(step);
125
126                         WM_event_add_notifier(C, NC_WINDOW, NULL);
127                         return OPERATOR_FINISHED;
128                 }
129         }
130
131         if(sa && sa->spacetype==SPACE_TEXT) {
132                 ED_text_undo_step(C, step);
133         }
134         else if(obedit) {
135                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
136                         if(undoname)
137                                 undo_editmode_name(C, undoname);
138                         else
139                                 undo_editmode_step(C, step);
140                 }
141         }
142         else {
143                 int do_glob_undo= 0;
144                 
145                 if(G.f & G_TEXTUREPAINT)
146                         undo_imagepaint_step(step);
147                 else if(G.f & G_PARTICLEEDIT) {
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 #ifndef DISABLE_PYTHON
160                                 // XXX          BPY_scripts_clear_pyobjects();
161 #endif
162                                 if(undoname)
163                                         BKE_undo_name(C, undoname);
164                                 else
165                                         BKE_undo_step(C, step);
166                         }
167                         
168                 }
169         }
170         
171         WM_event_add_notifier(C, NC_WINDOW, NULL);
172         
173         return OPERATOR_FINISHED;
174 }
175
176 void ED_undo_pop(bContext *C)
177 {
178         ed_undo_step(C, 1, NULL);
179 }
180 void ED_undo_redo(bContext *C)
181 {
182         ed_undo_step(C, -1, NULL);
183 }
184
185 void ED_undo_push_op(bContext *C, wmOperator *op)
186 {
187         /* in future, get undo string info? */
188         ED_undo_push(C, op->type->name);
189 }
190
191 void ED_undo_pop_op(bContext *C, wmOperator *op)
192 {
193         /* search back a couple of undo's, in case something else added pushes */
194         ed_undo_step(C, 0, op->type->name);
195 }
196
197 static int ed_undo_exec(bContext *C, wmOperator *op)
198 {
199         /* "last operator" should disappear, later we can tie ths with undo stack nicer */
200         WM_operator_stack_clear(C);
201         return ed_undo_step(C, 1, NULL);
202 }
203
204 static int ed_redo_exec(bContext *C, wmOperator *op)
205 {
206         return ed_undo_step(C, -1, NULL);
207 }
208
209 void ED_undo_menu(bContext *C)
210 {
211         Object *obedit= CTX_data_edit_object(C);
212         
213         if(obedit) {
214                 //if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
215                 //      undo_editmode_menu();
216         }
217         else {
218                 if(G.f & G_PARTICLEEDIT)
219                         PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
220                 else if(U.uiflag & USER_GLOBALUNDO) {
221                         char *menu= BKE_undo_menu_string();
222                         if(menu) {
223                                 short event= 0; // XXX pupmenu_col(menu, 20);
224                                 MEM_freeN(menu);
225                                 if(event>0) {
226                                         BKE_undo_number(C, event);
227                                 }
228                         }
229                 }
230         }
231 }
232
233 /* ********************** */
234
235 void ED_OT_undo(wmOperatorType *ot)
236 {
237         /* identifiers */
238         ot->name= "Undo";
239         ot->idname= "ED_OT_undo";
240         
241         /* api callbacks */
242         ot->exec= ed_undo_exec;
243         ot->poll= ED_operator_screenactive;
244 }
245
246 void ED_OT_redo(wmOperatorType *ot)
247 {
248         /* identifiers */
249         ot->name= "Redo";
250         ot->idname= "ED_OT_redo";
251         
252         /* api callbacks */
253         ot->exec= ed_redo_exec;
254         ot->poll= ED_operator_screenactive;
255 }
256
257