1 #define __SP_DOCUMENT_UNDO_C__
3 /** \file
4 * Undo/Redo stack implementation
5 *
6 * Authors:
7 * Lauris Kaplinski <lauris@kaplinski.com>
8 * MenTaLguY <mental@rydia.net>
9 *
10 * Copyright (C) 1999-2003 authors
11 * Copyright (C) 2001-2002 Ximian, Inc.
12 *
13 * Released under GNU GPL, read the file 'COPYING' for more information
14 *
15 * Using the split document model gives sodipodi a very simple and clean
16 * undo implementation. Whenever mutation occurs in the XML tree,
17 * SPObject invokes one of the five corresponding handlers of its
18 * container document. This writes down a generic description of the
19 * given action, and appends it to the recent action list, kept by the
20 * document. There will be as many action records as there are mutation
21 * events, which are all kept and processed together in the undo
22 * stack. Two methods exist to indicate that the given action is completed:
23 *
24 * \verbatim
25 void sp_document_done (SPDocument *document);
26 void sp_document_maybe_done (SPDocument *document, const unsigned char *key) \endverbatim
27 *
28 * Both move the recent action list into the undo stack and clear the
29 * list afterwards. While the first method does an unconditional push,
30 * the second one first checks the key of the most recent stack entry. If
31 * the keys are identical, the current action list is appended to the
32 * existing stack entry, instead of pushing it onto its own. This
33 * behaviour can be used to collect multi-step actions (like winding the
34 * Gtk spinbutton) from the UI into a single undoable step.
35 *
36 * For controls implemented by Sodipodi itself, implementing undo as a
37 * single step is usually done in a more efficent way. Most controls have
38 * the abstract model of grab, drag, release, and change user
39 * action. During the grab phase, all modifications are done to the
40 * SPObject directly - i.e. they do not change XML tree, and thus do not
41 * generate undo actions either. Only at the release phase (normally
42 * associated with releasing the mousebutton), changes are written back
43 * to the XML tree, thus generating only a single set of undo actions.
44 * (Lauris Kaplinski)
45 */
47 #ifdef HAVE_CONFIG_H
48 # include "config.h"
49 #endif
53 #if HAVE_STRING_H
54 #endif
57 #if HAVE_STDLIB_H
58 #endif
60 #include "xml/repr.h"
61 #include "document-private.h"
62 #include "inkscape.h"
63 #include "debug/event-tracker.h"
64 #include "debug/simple-event.h"
65 #include "event.h"
68 /*
69 * Undo & redo
70 */
71 /**
72 * Set undo sensitivity.
73 *
74 * \note
75 * Since undo sensitivity needs to be nested, setting undo sensitivity
76 * should be done like this:
77 *\verbatim
78 bool saved = sp_document_get_undo_sensitive(document);
79 sp_document_set_undo_sensitive(document, false);
80 ... do stuff ...
81 sp_document_set_undo_sensitive(document, saved); \endverbatim
82 */
83 void
84 sp_document_set_undo_sensitive (SPDocument *doc, bool sensitive)
85 {
86 g_assert (doc != NULL);
87 g_assert (doc->priv != NULL);
89 if ( sensitive == doc->priv->sensitive )
90 return;
92 if (sensitive) {
93 sp_repr_begin_transaction (doc->rdoc);
94 } else {
95 doc->priv->partial = sp_repr_coalesce_log (
96 doc->priv->partial,
97 sp_repr_commit_undoable (doc->rdoc)
98 );
99 }
101 doc->priv->sensitive = sensitive;
102 }
104 /*TODO: Throughout the inkscape code tree set/get_undo_sensitive are used for
105 * as is shown above. Perhaps it makes sense to create new functions,
106 * undo_ignore, and undo_recall to replace the start and end parts of the above.
107 * The main complexity with this is that they have to nest, so you have to store
108 * the saved bools in a stack. Perhaps this is why the above solution is better.
109 */
111 bool sp_document_get_undo_sensitive(SPDocument const *document) {
112 g_assert(document != NULL);
113 g_assert(document->priv != NULL);
115 return document->priv->sensitive;
116 }
118 void
119 sp_document_done (SPDocument *doc, const unsigned int event_type, Glib::ustring event_description)
120 {
121 sp_document_maybe_done (doc, NULL, event_type, event_description);
122 }
124 void
125 sp_document_reset_key (Inkscape::Application *inkscape, SPDesktop *desktop, GtkObject *base)
126 {
127 SPDocument *doc = (SPDocument *) base;
128 doc->actionkey = NULL;
129 }
131 void
132 sp_document_maybe_done (SPDocument *doc, const gchar *key, const unsigned int event_type,
133 Glib::ustring event_description)
134 {
135 g_assert (doc != NULL);
136 g_assert (doc->priv != NULL);
137 g_assert (doc->priv->sensitive);
139 doc->collectOrphans();
141 sp_document_ensure_up_to_date (doc);
143 sp_document_clear_redo (doc);
145 Inkscape::XML::Event *log = sp_repr_coalesce_log (doc->priv->partial, sp_repr_commit_undoable (doc->rdoc));
146 doc->priv->partial = NULL;
148 if (!log) {
149 sp_repr_begin_transaction (doc->rdoc);
150 return;
151 }
153 if (key && doc->actionkey && !strcmp (key, doc->actionkey) && doc->priv->undo) {
154 doc->priv->undo->data =
155 new Inkscape::Event(sp_repr_coalesce_log (((Inkscape::Event *)
156 doc->priv->undo->data)->event, log));
157 } else {
158 Inkscape::Event *event = new Inkscape::Event(log, event_type, event_description);
159 doc->priv->undo = g_slist_prepend (doc->priv->undo, event);
160 doc->priv->history_size++;
161 doc->priv->undoStackObservers.notifyUndoCommitEvent(event);
162 }
164 doc->actionkey = key;
166 doc->virgin = FALSE;
167 if (!doc->rroot->attribute("sodipodi:modified")) {
168 doc->rroot->setAttribute("sodipodi:modified", "true");
169 }
171 sp_repr_begin_transaction (doc->rdoc);
173 doc->priv->commit_signal.emit();
174 }
176 void
177 sp_document_cancel (SPDocument *doc)
178 {
179 g_assert (doc != NULL);
180 g_assert (doc->priv != NULL);
181 g_assert (doc->priv->sensitive);
183 sp_repr_rollback (doc->rdoc);
185 if (doc->priv->partial) {
186 sp_repr_undo_log (doc->priv->partial);
187 sp_repr_free_log (doc->priv->partial);
188 doc->priv->partial = NULL;
189 }
191 sp_repr_begin_transaction (doc->rdoc);
192 }
194 namespace {
196 void finish_incomplete_transaction(SPDocument &doc) {
197 SPDocumentPrivate &priv=*doc.priv;
198 Inkscape::XML::Event *log=sp_repr_commit_undoable(doc.rdoc);
199 if (log || priv.partial) {
200 g_warning ("Incomplete undo transaction:");
201 priv.partial = sp_repr_coalesce_log(priv.partial, log);
202 sp_repr_debug_print_log(priv.partial);
203 priv.undo = g_slist_prepend(priv.undo, priv.partial);
204 priv.partial = NULL;
205 }
206 }
208 }
210 gboolean
211 sp_document_undo (SPDocument *doc)
212 {
213 using Inkscape::Debug::EventTracker;
214 using Inkscape::Debug::SimpleEvent;
216 gboolean ret;
218 EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("undo");
220 g_assert (doc != NULL);
221 g_assert (doc->priv != NULL);
222 g_assert (doc->priv->sensitive);
224 doc->priv->sensitive = FALSE;
226 doc->actionkey = NULL;
228 finish_incomplete_transaction(*doc);
230 if (doc->priv->undo) {
231 Inkscape::Event *log=(Inkscape::Event *)doc->priv->undo->data;
232 doc->priv->undo = g_slist_remove (doc->priv->undo, log);
233 sp_repr_undo_log (log->event);
234 doc->priv->redo = g_slist_prepend (doc->priv->redo, log);
236 doc->rroot->setAttribute("sodipodi:modified", "true");
237 doc->priv->undoStackObservers.notifyUndoEvent(log);
239 ret = TRUE;
240 } else {
241 ret = FALSE;
242 }
244 sp_repr_begin_transaction (doc->rdoc);
246 doc->priv->sensitive = TRUE;
248 if (ret)
249 inkscape_external_change();
251 return ret;
252 }
254 gboolean
255 sp_document_redo (SPDocument *doc)
256 {
257 using Inkscape::Debug::EventTracker;
258 using Inkscape::Debug::SimpleEvent;
260 gboolean ret;
262 EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("redo");
264 g_assert (doc != NULL);
265 g_assert (doc->priv != NULL);
266 g_assert (doc->priv->sensitive);
268 doc->priv->sensitive = FALSE;
270 doc->actionkey = NULL;
272 finish_incomplete_transaction(*doc);
274 if (doc->priv->redo) {
275 Inkscape::Event *log=(Inkscape::Event *)doc->priv->redo->data;
276 doc->priv->redo = g_slist_remove (doc->priv->redo, log);
277 sp_repr_replay_log (log->event);
278 doc->priv->undo = g_slist_prepend (doc->priv->undo, log);
280 doc->rroot->setAttribute("sodipodi:modified", "true");
281 doc->priv->undoStackObservers.notifyRedoEvent(log);
283 ret = TRUE;
284 } else {
285 ret = FALSE;
286 }
288 sp_repr_begin_transaction (doc->rdoc);
290 doc->priv->sensitive = TRUE;
292 if (ret)
293 inkscape_external_change();
295 return ret;
296 }
298 void
299 sp_document_clear_undo (SPDocument *doc)
300 {
301 while (doc->priv->undo) {
302 GSList *current;
304 current = doc->priv->undo;
305 doc->priv->undo = current->next;
306 doc->priv->history_size--;
308 delete ((Inkscape::Event *) current->data);
309 g_slist_free_1 (current);
310 }
311 }
313 void
314 sp_document_clear_redo (SPDocument *doc)
315 {
316 while (doc->priv->redo) {
317 GSList *current;
319 current = doc->priv->redo;
320 doc->priv->redo = current->next;
321 doc->priv->history_size--;
323 delete ((Inkscape::Event *) current->data);
324 g_slist_free_1 (current);
325 }
326 }
327 /*
328 Local Variables:
329 mode:c++
330 c-file-style:"stroustrup"
331 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
332 indent-tabs-mode:nil
333 fill-column:99
334 End:
335 */
336 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :