svn merge -r 21372:21508 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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 /* ***************** generic undo system ********************* */
75
76 /* ********* XXX **************** */
77 static void undo_push_mball() {}
78 static void sound_initialize_sounds() {}
79 /* ********* XXX **************** */
80
81 void ED_undo_push(bContext *C, char *str)
82 {
83         wmWindowManager *wm= CTX_wm_manager(C);
84         Object *obedit= CTX_data_edit_object(C);
85         
86         if(obedit) {
87                 if (U.undosteps == 0) return;
88                 
89                 if(obedit->type==OB_MESH)
90                         undo_push_mesh(C, str);
91                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
92                         undo_push_curve(C, str);
93                 else if (obedit->type==OB_FONT)
94                         undo_push_font(C, str);
95                 else if (obedit->type==OB_MBALL)
96                         undo_push_mball(str);
97                 else if (obedit->type==OB_LATTICE)
98                         undo_push_lattice(C, str);
99                 else if (obedit->type==OB_ARMATURE)
100                         undo_push_armature(C, str);
101         }
102         else if(G.f & G_PARTICLEEDIT) {
103                 if (U.undosteps == 0) return;
104                 
105                 PE_undo_push(CTX_data_scene(C), str);
106         }
107         else {
108                 if(U.uiflag & USER_GLOBALUNDO) 
109                         BKE_write_undo(C, str);
110         }
111         
112         if(wm->file_saved) {
113                 wm->file_saved= 0;
114                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
115         }
116 }
117
118 void ED_undo_push_op(bContext *C, wmOperator *op)
119 {
120         /* in future, get undo string info? */
121         ED_undo_push(C, op->type->name);
122 }
123
124 static int ed_undo_step(bContext *C, int step)
125 {       
126         Object *obedit= CTX_data_edit_object(C);
127         ScrArea *sa= CTX_wm_area(C);
128
129         if(sa && sa->spacetype==SPACE_IMAGE) {
130                 SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
131                 
132                 if(G.f & G_TEXTUREPAINT || sima->flag & SI_DRAWTOOL) {
133                         undo_imagepaint_step(step);
134
135                         WM_event_add_notifier(C, NC_WINDOW, NULL);
136                         return OPERATOR_FINISHED;
137                 }
138         }
139
140         if(sa && sa->spacetype==SPACE_TEXT) {
141                 ED_text_undo_step(C, step);
142         }
143         else if(obedit) {
144                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
145                         undo_editmode_step(C, step);
146         }
147         else {
148                 int do_glob_undo= 0;
149                 
150                 if(G.f & G_TEXTUREPAINT)
151                         undo_imagepaint_step(step);
152                 else if(G.f & G_PARTICLEEDIT) {
153                         if(step==1)
154                                 PE_undo(CTX_data_scene(C));
155                         else
156                                 PE_redo(CTX_data_scene(C));
157                 }
158                 else {
159                         do_glob_undo= 1;
160                 }
161                 
162                 if(do_glob_undo) {
163                         if(U.uiflag & USER_GLOBALUNDO) {
164 #ifndef DISABLE_PYTHON
165                                 // XXX          BPY_scripts_clear_pyobjects();
166 #endif
167                                 BKE_undo_step(C, step);
168                                 sound_initialize_sounds();
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);
182 }
183 void ED_undo_redo(bContext *C)
184 {
185         ed_undo_step(C, -1);
186 }
187
188 static int ed_undo_exec(bContext *C, wmOperator *op)
189 {
190         /* "last operator" should disappear, later we can tie ths with undo stack nicer */
191         WM_operator_stack_clear(C);
192         return ed_undo_step(C, 1);
193 }
194 static int ed_redo_exec(bContext *C, wmOperator *op)
195 {
196         return ed_undo_step(C, -1);
197 }
198
199 void ED_undo_menu(bContext *C)
200 {
201         Object *obedit= CTX_data_edit_object(C);
202         
203         if(obedit) {
204                 //if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
205                 //      undo_editmode_menu();
206         }
207         else {
208                 if(G.f & G_PARTICLEEDIT)
209                         PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
210                 else if(U.uiflag & USER_GLOBALUNDO) {
211                         char *menu= BKE_undo_menu_string();
212                         if(menu) {
213                                 short event= 0; // XXX pupmenu_col(menu, 20);
214                                 MEM_freeN(menu);
215                                 if(event>0) {
216                                         BKE_undo_number(C, event);
217                                         sound_initialize_sounds();
218                                 }
219                         }
220                 }
221         }
222 }
223
224 /* ********************** */
225
226 void ED_OT_undo(wmOperatorType *ot)
227 {
228         /* identifiers */
229         ot->name= "Undo";
230         ot->idname= "ED_OT_undo";
231         
232         /* api callbacks */
233         ot->exec= ed_undo_exec;
234         ot->poll= ED_operator_screenactive;
235 }
236
237 void ED_OT_redo(wmOperatorType *ot)
238 {
239         /* identifiers */
240         ot->name= "Redo";
241         ot->idname= "ED_OT_redo";
242         
243         /* api callbacks */
244         ot->exec= ed_redo_exec;
245         ot->poll= ED_operator_screenactive;
246 }
247
248