Welcome to mirror list, hosted at ThFree Co, Russian Federation.

undo.c « util « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9ac44f482a1fc5de98db8319c51360757b3cf7fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2004 Blender Foundation
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/editors/util/undo.c
 *  \ingroup edutil
 */



#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "MEM_guardedalloc.h"

#include "DNA_object_types.h"

#include "BLI_blenlib.h"
#include "BLI_editVert.h"
#include "BLI_dynstr.h"
#include "BLI_utildefines.h"

#include "BKE_blender.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_screen.h"

#include "ED_armature.h"
#include "ED_particle.h"
#include "ED_curve.h"
#include "ED_mball.h"
#include "ED_mesh.h"
#include "ED_object.h"
#include "ED_screen.h"
#include "ED_sculpt.h"
#include "ED_util.h"
#include "ED_text.h"

#include "WM_api.h"
#include "WM_types.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "util_intern.h"

#define MAXUNDONAME 64 /* XXX, make common define */

/* ***************** generic undo system ********************* */

void ED_undo_push(bContext *C, const char *str)
{
	wmWindowManager *wm= CTX_wm_manager(C);
	Object *obedit= CTX_data_edit_object(C);
	Object *obact= CTX_data_active_object(C);

	if (G.f & G_DEBUG)
		printf("undo push %s\n", str);
	
	if(obedit) {
		if (U.undosteps == 0) return;
		
		if(obedit->type==OB_MESH)
			undo_push_mesh(C, str);
		else if ELEM(obedit->type, OB_CURVE, OB_SURF)
			undo_push_curve(C, str);
		else if (obedit->type==OB_FONT)
			undo_push_font(C, str);
		else if (obedit->type==OB_MBALL)
			undo_push_mball(C, str);
		else if (obedit->type==OB_LATTICE)
			undo_push_lattice(C, str);
		else if (obedit->type==OB_ARMATURE)
			undo_push_armature(C, str);
	}
	else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
		if (U.undosteps == 0) return;
		
		PE_undo_push(CTX_data_scene(C), str);
	}
	else {
		if(U.uiflag & USER_GLOBALUNDO) 
			BKE_write_undo(C, str);
	}
	
	if(wm->file_saved) {
		wm->file_saved= 0;
		WM_event_add_notifier(C, NC_WM|ND_DATACHANGED, NULL);
	}
}

static int ed_undo_step(bContext *C, int step, const char *undoname)
{	
	Object *obedit= CTX_data_edit_object(C);
	Object *obact= CTX_data_active_object(C);
	ScrArea *sa= CTX_wm_area(C);

	if(sa && sa->spacetype==SPACE_IMAGE) {
		SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
		
		if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
			if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
				if(U.uiflag & USER_GLOBALUNDO)
					BKE_undo_name(C, undoname);

			WM_event_add_notifier(C, NC_WINDOW, NULL);
			return OPERATOR_FINISHED;
		}
	}

	if(sa && sa->spacetype==SPACE_TEXT) {
		ED_text_undo_step(C, step);
	}
	else if(obedit) {
		if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
			if(undoname)
				undo_editmode_name(C, undoname);
			else
				undo_editmode_step(C, step);

			WM_event_add_notifier(C, NC_GEOM|ND_DATA, NULL);
		}
	}
	else {
		int do_glob_undo= 0;
		
		if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
			if(!ED_undo_paint_step(C, UNDO_PAINT_IMAGE, step, undoname) && undoname)
				do_glob_undo= 1;
		}
		else if(obact && obact->mode & OB_MODE_SCULPT) {
			if(!ED_undo_paint_step(C, UNDO_PAINT_MESH, step, undoname) && undoname)
				do_glob_undo= 1;
		}
		else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
			if(step==1)
				PE_undo(CTX_data_scene(C));
			else
				PE_redo(CTX_data_scene(C));
		}
		else {
			do_glob_undo= 1;
		}
		
		if(do_glob_undo) {
			if(U.uiflag & USER_GLOBALUNDO) {
				// note python defines not valid here anymore.
				//#ifdef WITH_PYTHON
				// XXX		BPY_scripts_clear_pyobjects();
				//#endif
				if(undoname)
					BKE_undo_name(C, undoname);
				else
					BKE_undo_step(C, step);

				WM_event_add_notifier(C, NC_SCENE|ND_LAYER_CONTENT, CTX_data_scene(C));
			}
			
		}
	}
	
	WM_event_add_notifier(C, NC_WINDOW, NULL);
	
	return OPERATOR_FINISHED;
}

void ED_undo_pop(bContext *C)
{
	ed_undo_step(C, 1, NULL);
}
void ED_undo_redo(bContext *C)
{
	ed_undo_step(C, -1, NULL);
}

void ED_undo_push_op(bContext *C, wmOperator *op)
{
	/* in future, get undo string info? */
	ED_undo_push(C, op->type->name);
}

void ED_undo_pop_op(bContext *C, wmOperator *op)
{
	/* search back a couple of undo's, in case something else added pushes */
	ed_undo_step(C, 0, op->type->name);
}

/* name optionally, function used to check for operator redo panel */
int ED_undo_valid(const bContext *C, const char *undoname)
{
	Object *obedit= CTX_data_edit_object(C);
	Object *obact= CTX_data_active_object(C);
	ScrArea *sa= CTX_wm_area(C);
	
	if(sa && sa->spacetype==SPACE_IMAGE) {
		SpaceImage *sima= (SpaceImage *)sa->spacedata.first;
		
		if((obact && obact->mode & OB_MODE_TEXTURE_PAINT) || sima->flag & SI_DRAWTOOL) {
			return 1;
		}
	}
	
	if(sa && sa->spacetype==SPACE_TEXT) {
		return 1;
	}
	else if(obedit) {
		if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE) {
			return undo_editmode_valid(undoname);
		}
	}
	else {
		
		/* if below tests fail, global undo gets executed */
		
		if(obact && obact->mode & OB_MODE_TEXTURE_PAINT) {
			if( ED_undo_paint_valid(UNDO_PAINT_IMAGE, undoname) )
				return 1;
		}
		else if(obact && obact->mode & OB_MODE_SCULPT) {
			if( ED_undo_paint_valid(UNDO_PAINT_MESH, undoname) )
				return 1;
		}
		else if(obact && obact->mode & OB_MODE_PARTICLE_EDIT) {
			return PE_undo_valid(CTX_data_scene(C));
		}
		
		if(U.uiflag & USER_GLOBALUNDO) {
			return BKE_undo_valid(undoname);
		}
	}
	return 0;
}

static int ed_undo_exec(bContext *C, wmOperator *UNUSED(op))
{
	/* "last operator" should disappear, later we can tie ths with undo stack nicer */
	WM_operator_stack_clear(CTX_wm_manager(C));
	return ed_undo_step(C, 1, NULL);
}

static int ed_undo_push_exec(bContext *C, wmOperator *op)
{
	char str[MAXUNDONAME];
	RNA_string_get(op->ptr, "message", str);
	ED_undo_push(C, str);
	return OPERATOR_FINISHED;
}

static int ed_redo_exec(bContext *C, wmOperator *UNUSED(op))
{
	return ed_undo_step(C, -1, NULL);
}

#if 0 /* UNUSED */
void ED_undo_menu(bContext *C)
{
	Object *obedit= CTX_data_edit_object(C);
	Object *obact= CTX_data_active_object(C);
	
	if(obedit) {
		//if ELEM7(obedit->type, OB_MESH, OB_FONT, OB_CURVE, OB_SURF, OB_MBALL, OB_LATTICE, OB_ARMATURE)
		//	undo_editmode_menu();
	}
	else {
		if(obact && obact->mode & OB_MODE_PARTICLE_EDIT)
			PE_undo_menu(CTX_data_scene(C), CTX_data_active_object(C));
		else if(U.uiflag & USER_GLOBALUNDO) {
			char *menu= BKE_undo_menu_string();
			if(menu) {
				short event= 0; // XXX pupmenu_col(menu, 20);
				MEM_freeN(menu);
				if(event>0) {
					BKE_undo_number(C, event);
				}
			}
		}
	}
}
#endif

/* ********************** */

void ED_OT_undo(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Undo";
	ot->description= "Undo previous action";
	ot->idname= "ED_OT_undo";
	
	/* api callbacks */
	ot->exec= ed_undo_exec;
	ot->poll= ED_operator_screenactive;
}

void ED_OT_undo_push(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Undo Push";
	ot->description= "Add an undo state (internal use only)";
	ot->idname= "ED_OT_undo_push";
	
	/* api callbacks */
	ot->exec= ed_undo_push_exec;

	RNA_def_string(ot->srna, "message", "Add an undo step *function may be moved*", MAXUNDONAME, "Undo Message", "");
}

void ED_OT_redo(wmOperatorType *ot)
{
	/* identifiers */
	ot->name= "Redo";
	ot->description= "Redo previous action";
	ot->idname= "ED_OT_redo";
	
	/* api callbacks */
	ot->exec= ed_redo_exec;
	ot->poll= ED_operator_screenactive;
}


/* ui callbacks should call this rather then calling WM_operator_repeat() themselves */
int ED_undo_operator_repeat(bContext *C, struct wmOperator *op)
{
	int ret= 0;

	if(op) {
		ARegion *ar= CTX_wm_region(C);
		ARegion *ar1= BKE_area_find_region_type(CTX_wm_area(C), RGN_TYPE_WINDOW);

		if(ar1)
			CTX_wm_region_set(C, ar1);

		if(WM_operator_repeat_check(C, op) && WM_operator_poll(C, op->type)) {
			int retval;

			if (G.f & G_DEBUG)
				printf("redo_cb: operator redo %s\n", op->type->name);
			ED_undo_pop_op(C, op);

			if(op->type->check) {
				op->type->check(C, op); /* ignore return value since its running again anyway */
			}

			retval= WM_operator_repeat(C, op);
			if((retval & OPERATOR_FINISHED)==0) {
				if (G.f & G_DEBUG)
					printf("redo_cb: operator redo failed: %s, return %d\n", op->type->name, retval);
				ED_undo_redo(C);
			}
			else {
				ret= 1;
			}
		}

		/* set region back */
		CTX_wm_region_set(C, ar);
	}
	else {
		if (G.f & G_DEBUG) {
			printf("redo_cb: WM_operator_repeat_check returned false %s\n", op->type->name);
		}
	}

	return ret;
}

void ED_undo_operator_repeat_cb(bContext *C, void *arg_op, void *UNUSED(arg_unused))
{
	ED_undo_operator_repeat(C, (wmOperator *)arg_op);
}

void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_event))
{
	ED_undo_operator_repeat(C, (wmOperator *)arg_op);
}