File: pps-undo-context.c

package info (click to toggle)
papers 49.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 21,044 kB
  • sloc: ansic: 37,728; sh: 197; xml: 127; makefile: 113
file content (287 lines) | stat: -rw-r--r-- 8,929 bytes parent folder | download | duplicates (2)
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
/* pps-undo-context.c
 *  this file is part of papers, a gnome document viewer
 *
 * Copyright (C) 2024 Lucas Baudin
 *
 * Papers 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.
 *
 * Papers 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.
 */

#include "pps-undo-context.h"

/**
 * PpsUndoContext:
 *
 * This context singleton manages two stacks of #PpsUndoAction
 * that contain actions that may be undone or redone.
 *
 * These actions may be added by a number of objects that implement
 * PpsUndoHandler (e.g. the annotations context). Each handler provides a pointer
 * to an opaque structure that contains data to undo the action.
 *
 * The switch between the undo/redo stacks is done transparently: actions are added
 * to the redo stack if and only if another action is being undone (i.e.
 * `pps_undo_context_undo` is being called).
 */

#ifdef G_LOG_DOMAIN
#undef G_LOG_DOMAIN
#endif
#define G_LOG_DOMAIN "PpsUndoCtx"

typedef enum {
	PPS_UNDO_CONTEXT_STATE_NORMAL,
	PPS_UNDO_CONTEXT_STATE_UNDOING,
	PPS_UNDO_CONTEXT_STATE_REDOING
} PpsUndoContextState;

struct _PpsUndoContext {
	GObject parent_instance;
	GQueue *undo_stack;
	GQueue *redo_stack;
	/* This tracks whether the user asked to undo/redo an action
	or is doing some other normal thing. This is used to add new undoable actions
	to the correct stack */
	PpsUndoContextState undo_state;
	PpsDocumentModel *document_model;
};

typedef struct {
	PpsUndoHandler *handler;
	gpointer data;
} PpsUndoAction;

enum {
	PROP_0,
	PROP_DOCUMENT_MODEL,
	NUM_PROPERTIES
};
G_DEFINE_TYPE (PpsUndoContext, pps_undo_context, G_TYPE_OBJECT)

static void
pps_undo_action_free (PpsUndoAction *action)
{
	pps_undo_handler_free_action (action->handler, action->data);
	g_object_unref (action->handler);
	g_free (action);
}

static void
pps_undo_context_document_changed (GObject *gobject, GParamSpec *pspec, gpointer user_data)
{
	PpsUndoContext *context = PPS_UNDO_CONTEXT (user_data);
	g_queue_clear_full (context->undo_stack, (GDestroyNotify) pps_undo_action_free);
	g_queue_clear_full (context->redo_stack, (GDestroyNotify) pps_undo_action_free);
}

static void
pps_undo_context_finalize (GObject *object)
{
	PpsUndoContext *context = PPS_UNDO_CONTEXT (object);
	g_queue_free_full (context->undo_stack, (GDestroyNotify) pps_undo_action_free);
	g_queue_free_full (context->redo_stack, (GDestroyNotify) pps_undo_action_free);
	g_object_unref (context->document_model);
	G_OBJECT_CLASS (pps_undo_context_parent_class)->finalize (object);
}

static void
pps_undo_context_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
	PpsUndoContext *context = PPS_UNDO_CONTEXT (object);

	switch (property_id) {
	case PROP_DOCUMENT_MODEL:
		context->document_model = g_value_dup_object (value);
		g_signal_connect_object (context->document_model, "notify::document",
		                         G_CALLBACK (pps_undo_context_document_changed),
		                         context, G_CONNECT_DEFAULT);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
pps_undo_context_get_property (GObject *object, guint property_id, GValue *value, GParamSpec *pspec)
{
	PpsUndoContext *context = PPS_UNDO_CONTEXT (object);

	switch (property_id) {
	case PROP_DOCUMENT_MODEL:
		g_value_set_object (value, context->document_model);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
pps_undo_context_class_init (PpsUndoContextClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);

	object_class->finalize = pps_undo_context_finalize;
	object_class->set_property = pps_undo_context_set_property;
	object_class->get_property = pps_undo_context_get_property;

	g_object_class_install_property (object_class,
	                                 PROP_DOCUMENT_MODEL,
	                                 g_param_spec_object ("document-model",
	                                                      "Document Model",
	                                                      "The document model associated with this undo context",
	                                                      PPS_TYPE_DOCUMENT_MODEL,
	                                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
}

static void
pps_undo_context_init (PpsUndoContext *context)
{
	context->undo_stack = g_queue_new ();
	context->redo_stack = g_queue_new ();
	context->undo_state = PPS_UNDO_CONTEXT_STATE_NORMAL;
}

PpsUndoContext *
pps_undo_context_new (PpsDocumentModel *document_model)
{
	return g_object_new (PPS_TYPE_UNDO_CONTEXT, "document-model", document_model, NULL);
}

/**
 * pps_undo_context_add_action:
 * @context: a #PpsUndoContext
 * @handler: an object implementing #PpsUndoHandler
 * @data: a pointer to a struct that @handler will use to undo an action
 *
 * This method adds a new action to the undo (or redo if it is used while
 * undoing) stack. If it is undone in the future, `pps_undo_handler_undo`
 * will be called on @handler with @data.
 */
void
pps_undo_context_add_action (PpsUndoContext *context, PpsUndoHandler *handler, gpointer data)
{
	g_return_if_fail (PPS_IS_UNDO_CONTEXT (context));
	g_return_if_fail (PPS_IS_UNDO_HANDLER (handler));

	PpsUndoAction *action = g_new (PpsUndoAction, 1);
	GQueue *queue;

	if (context->undo_state == PPS_UNDO_CONTEXT_STATE_UNDOING) {
		queue = context->redo_stack;
	} else {
		queue = context->undo_stack;
	}
	if (context->undo_state == PPS_UNDO_CONTEXT_STATE_NORMAL) {
		g_queue_clear_full (context->redo_stack, (GDestroyNotify) pps_undo_action_free);
		g_debug ("Clearing the redo stack");
	}
	action->handler = g_object_ref (handler);
	action->data = data;

	g_debug ("Adding action to the undo/redo stack");
	g_queue_push_head (queue, action);
}

/**
 * pps_undo_context_undo:
 * @context: a #PpsUndoContext
 *
 * This pops the last action on the undo stack and undoes it by calling the
 * undo interface method of the `PpsUndoHandler` associated to the action.
 * While undoing, the `PpsUndoHandler` should add a new action with
 * `pps_undo_context_add_action` to redo what has just been undone, it will be
 * added to the redo stack by the undo context.
 */
void
pps_undo_context_undo (PpsUndoContext *context)
{
	g_return_if_fail (PPS_IS_UNDO_CONTEXT (context));

	PpsUndoAction *action = g_queue_pop_head (context->undo_stack);
	if (action) {
		context->undo_state = PPS_UNDO_CONTEXT_STATE_UNDOING;
		pps_undo_handler_undo (action->handler, action->data);
		pps_undo_handler_free_action (action->handler, action->data);
		g_free (action);
		context->undo_state = PPS_UNDO_CONTEXT_STATE_NORMAL;
	} else {
		g_debug ("Undo stack empty");
	}
}

/**
 * pps_undo_context_redo:
 * @context: a #PpsUndoContext
 *
 * Similar to the undo method, but taking an action from the redo stack.
 */
void
pps_undo_context_redo (PpsUndoContext *context)
{
	g_return_if_fail (PPS_IS_UNDO_CONTEXT (context));

	PpsUndoAction *action = g_queue_pop_head (context->redo_stack);
	if (action) {
		context->undo_state = PPS_UNDO_CONTEXT_STATE_REDOING;
		pps_undo_handler_undo (action->handler, action->data);
		pps_undo_handler_free_action (action->handler, action->data);
		g_free (action);
		context->undo_state = PPS_UNDO_CONTEXT_STATE_NORMAL;
	} else {
		g_debug ("Redo stack empty");
	}
}

/* The next two functions may be used to "squash" actions on the last
one registered by the context. This is used by the annotations context
but should be avoided in newly written code, i.e. an action should always
represent a complete user action. */

/**
 * pps_undo_context_get_last_handler:
 * @context: #PpsUndoContext instance
 *
 * Returns: (transfer none): The last handler
 */
PpsUndoHandler *
pps_undo_context_get_last_handler (PpsUndoContext *context)
{
	PpsUndoAction *last_action;

	if (g_queue_is_empty (context->undo_stack))
		return NULL;

	last_action = g_queue_peek_head (context->undo_stack);
	return last_action->handler;
}

gpointer
pps_undo_context_get_last_action (PpsUndoContext *context)
{
	PpsUndoAction *last_action;
	GQueue *queue;
	if (context->undo_state == PPS_UNDO_CONTEXT_STATE_UNDOING) {
		queue = context->redo_stack;
	} else {
		queue = context->undo_stack;
	}

	if (g_queue_is_empty (queue))
		return NULL;

	last_action = g_queue_peek_head (queue);
	return last_action->data;
}