2.5
[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_util.h"
58 #include "ED_mesh.h"
59
60 #include "UI_interface.h"
61 #include "UI_resources.h"
62
63 /* ***************** generic undo system ********************* */
64
65 /* ********* XXX **************** */
66 static void undo_push_curve() {}
67 static void undo_push_font() {}
68 static void undo_push_mball() {}
69 static void undo_push_lattice() {}
70 static void undo_push_armature() {}
71 static void PE_undo_push() {}
72 static void PE_undo() {}
73 static void PE_redo() {}
74 static void PE_undo_menu() {}
75 static void undo_imagepaint_step() {}
76 static void sound_initialize_sounds() {}
77 /* ********* XXX **************** */
78
79 void ED_undo_push(bContext *C, char *str)
80 {
81         if(G.obedit) {
82                 if (U.undosteps == 0) return;
83                 
84                 if(G.obedit->type==OB_MESH)
85                         undo_push_mesh(str);
86                 else if ELEM(G.obedit->type, OB_CURVE, OB_SURF)
87                         undo_push_curve(str);
88                 else if (G.obedit->type==OB_FONT)
89                         undo_push_font(str);
90                 else if (G.obedit->type==OB_MBALL)
91                         undo_push_mball(str);
92                 else if (G.obedit->type==OB_LATTICE)
93                         undo_push_lattice(str);
94                 else if (G.obedit->type==OB_ARMATURE)
95                         undo_push_armature(str);
96         }
97         else if(G.f & G_PARTICLEEDIT) {
98                 if (U.undosteps == 0) return;
99                 
100                 PE_undo_push(str);
101         }
102         else {
103                 if(U.uiflag & USER_GLOBALUNDO) 
104                         BKE_write_undo(C, str);
105         }
106 }
107
108 static void undo_do(bContext *C)
109 {
110         if(U.uiflag & USER_GLOBALUNDO) {
111 #ifndef DISABLE_PYTHON
112 // XXX          BPY_scripts_clear_pyobjects();
113 #endif
114                 BKE_undo_step(C, 1);
115                 sound_initialize_sounds();
116         }
117         
118 }
119
120 void ED_undo(bContext *C)
121 {       
122         ScrArea *sa= CTX_wm_area(C);
123         
124         if(G.obedit) {
125                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
126                         undo_editmode_step(1);
127         }
128         else {
129                 if(G.f & G_TEXTUREPAINT)
130                         undo_imagepaint_step(1);
131                 else if(sa->spacetype==SPACE_IMAGE) {
132                         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
133                         if(sima->flag & SI_DRAWTOOL)
134                                 undo_imagepaint_step(1);
135                         else
136                                 undo_do(C);
137                 }
138                 else if(G.f & G_PARTICLEEDIT)
139                         PE_undo();
140                 else {
141                         undo_do(C);
142                 }
143         }
144 }
145
146 void ED_redo(bContext *C)
147 {
148         ScrArea *sa= CTX_wm_area(C);
149         
150         if(G.obedit) {
151                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
152                         undo_editmode_step(-1);
153         }
154         else {
155                 if(G.f & G_TEXTUREPAINT)
156                         undo_imagepaint_step(-1);
157                 else if(sa->spacetype==SPACE_IMAGE) {
158                         SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
159                         if(sima->flag & SI_DRAWTOOL)
160                                 undo_imagepaint_step(-1);
161                         else {
162                                 BKE_undo_step(C, -1);
163                                 sound_initialize_sounds();
164                         }
165                 }
166                 else if(G.f & G_PARTICLEEDIT)
167                         PE_redo();
168                 else {
169                         /* includes faceselect now */
170                         if(U.uiflag & USER_GLOBALUNDO) {
171                                 BKE_undo_step(C, -1);
172                                 sound_initialize_sounds();
173                         }
174                 }
175         }
176 }
177
178 void ED_undo_menu(bContext *C)
179 {
180         if(G.obedit) {
181                 if ELEM7(G.obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
182                         undo_editmode_menu();
183         }
184         else {
185                 if(G.f & G_PARTICLEEDIT)
186                         PE_undo_menu();
187                 else if(U.uiflag & USER_GLOBALUNDO) {
188                         char *menu= BKE_undo_menu_string();
189                         if(menu) {
190                                 short event= 0; // XXX pupmenu_col(menu, 20);
191                                 MEM_freeN(menu);
192                                 if(event>0) {
193                                         BKE_undo_number(C, event);
194                                         sound_initialize_sounds();
195                                 }
196                         }
197                 }
198         }
199 }
200