2.5: Particle Edit, work in progress commit.
[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
51 #include "BLI_blenlib.h"
52 #include "BLI_editVert.h"
53 #include "BLI_dynstr.h"
54
55 #include "BKE_utildefines.h"
56
57 #include "ED_armature.h"
58 #include "ED_particle.h"
59 #include "ED_curve.h"
60 #include "ED_mesh.h"
61 #include "ED_object.h"
62 #include "ED_screen.h"
63 #include "ED_sculpt.h"
64 #include "ED_util.h"
65
66 #include "WM_api.h"
67 #include "WM_types.h"
68
69 #include "UI_interface.h"
70 #include "UI_resources.h"
71
72 /* ***************** generic undo system ********************* */
73
74 /* ********* XXX **************** */
75 static void undo_push_mball() {}
76 static void sound_initialize_sounds() {}
77 /* ********* XXX **************** */
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         
84         if(obedit) {
85                 if (U.undosteps == 0) return;
86                 
87                 if(obedit->type==OB_MESH)
88                         undo_push_mesh(C, str);
89                 else if ELEM(obedit->type, OB_CURVE, OB_SURF)
90                         undo_push_curve(C, str);
91                 else if (obedit->type==OB_FONT)
92                         undo_push_font(C, str);
93                 else if (obedit->type==OB_MBALL)
94                         undo_push_mball(str);
95                 else if (obedit->type==OB_LATTICE)
96                         undo_push_lattice(C, str);
97                 else if (obedit->type==OB_ARMATURE)
98                         undo_push_armature(C, str);
99         }
100         else if(G.f & G_PARTICLEEDIT) {
101                 if (U.undosteps == 0) return;
102                 
103                 PE_undo_push(CTX_data_scene(C), str);
104         }
105         else {
106                 if(U.uiflag & USER_GLOBALUNDO) 
107                         BKE_write_undo(C, str);
108         }
109         
110         if(wm->file_saved) {
111                 wm->file_saved= 0;
112                 WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
113         }
114 }
115
116 void ED_undo_push_op(bContext *C, wmOperator *op)
117 {
118         /* in future, get undo string info? */
119         ED_undo_push(C, op->type->name);
120 }
121
122 static int ed_undo_step(bContext *C, int step)
123 {       
124         Object *obedit= CTX_data_edit_object(C);
125         ScrArea *sa= CTX_wm_area(C);
126         
127         if(obedit) {
128                 if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
129                         undo_editmode_step(C, step);
130         }
131         else {
132                 int do_glob_undo= 0;
133                 
134                 if(G.f & G_TEXTUREPAINT)
135                         undo_imagepaint_step(step);
136                 else if(sa->spacetype==SPACE_IMAGE) {
137                         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
138                         if(sima->flag & SI_DRAWTOOL)
139                                 undo_imagepaint_step(step);
140                         else
141                                 do_glob_undo= 1;
142                 }
143                 else if(G.f & G_PARTICLEEDIT) {
144                         if(step==1)
145                                 PE_undo(CTX_data_scene(C));
146                         else
147                                 PE_redo(CTX_data_scene(C));
148                 }
149                 else {
150                         do_glob_undo= 1;
151                 }
152                 
153                 if(do_glob_undo) {
154                         if(U.uiflag & USER_GLOBALUNDO) {
155 #ifndef DISABLE_PYTHON
156                                 // XXX          BPY_scripts_clear_pyobjects();
157 #endif
158                                 BKE_undo_step(C, step);
159                                 sound_initialize_sounds();
160                         }
161                         
162                 }
163         }
164         
165         WM_event_add_notifier(C, NC_WINDOW, NULL);
166         
167         return OPERATOR_FINISHED;
168 }
169
170 void ED_undo_pop(bContext *C)
171 {
172         ed_undo_step(C, 1);
173 }
174
175 static int ed_undo_exec(bContext *C, wmOperator *op)
176 {
177         return ed_undo_step(C, 1);
178 }
179 static int ed_redo_exec(bContext *C, wmOperator *op)
180 {
181         return ed_undo_step(C, -1);
182 }
183
184 void ED_undo_menu(bContext *C)
185 {
186         Object *obedit= CTX_data_edit_object(C);
187         
188         if(obedit) {
189                 //if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
190                 //      undo_editmode_menu();
191         }
192         else {
193                 if(G.f & G_PARTICLEEDIT)
194                         PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
195                 else if(U.uiflag & USER_GLOBALUNDO) {
196                         char *menu= BKE_undo_menu_string();
197                         if(menu) {
198                                 short event= 0; // XXX pupmenu_col(menu, 20);
199                                 MEM_freeN(menu);
200                                 if(event>0) {
201                                         BKE_undo_number(C, event);
202                                         sound_initialize_sounds();
203                                 }
204                         }
205                 }
206         }
207 }
208
209 /* ********************** */
210
211 void ED_OT_undo(wmOperatorType *ot)
212 {
213         /* identifiers */
214         ot->name= "Undo";
215         ot->idname= "ED_OT_undo";
216         
217         /* api callbacks */
218         ot->exec= ed_undo_exec;
219         ot->poll= ED_operator_screenactive;
220 }
221
222 void ED_OT_redo(wmOperatorType *ot)
223 {
224         /* identifiers */
225         ot->name= "Redo";
226         ot->idname= "ED_OT_redo";
227         
228         /* api callbacks */
229         ot->exec= ed_redo_exec;
230         ot->poll= ED_operator_screenactive;
231 }
232
233