Code

add document serial numbers
[inkscape.git] / src / document-undo.cpp
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) 2007  MenTaLguY <mental@rydia.net>
11  * Copyright (C) 1999-2003 authors
12  * Copyright (C) 2001-2002 Ximian, Inc.
13  *
14  * Released under GNU GPL, read the file 'COPYING' for more information
15  *
16  * Using the split document model gives sodipodi a very simple and clean
17  * undo implementation. Whenever mutation occurs in the XML tree,
18  * SPObject invokes one of the five corresponding handlers of its
19  * container document. This writes down a generic description of the
20  * given action, and appends it to the recent action list, kept by the
21  * document. There will be as many action records as there are mutation
22  * events, which are all kept and processed together in the undo
23  * stack. Two methods exist to indicate that the given action is completed:
24  *
25  * \verbatim
26    void sp_document_done (SPDocument *document);
27    void sp_document_maybe_done (SPDocument *document, const unsigned char *key) \endverbatim
28  *
29  * Both move the recent action list into the undo stack and clear the
30  * list afterwards.  While the first method does an unconditional push,
31  * the second one first checks the key of the most recent stack entry. If
32  * the keys are identical, the current action list is appended to the
33  * existing stack entry, instead of pushing it onto its own.  This
34  * behaviour can be used to collect multi-step actions (like winding the
35  * Gtk spinbutton) from the UI into a single undoable step.
36  *
37  * For controls implemented by Sodipodi itself, implementing undo as a
38  * single step is usually done in a more efficent way. Most controls have
39  * the abstract model of grab, drag, release, and change user
40  * action. During the grab phase, all modifications are done to the
41  * SPObject directly - i.e. they do not change XML tree, and thus do not
42  * generate undo actions either.  Only at the release phase (normally
43  * associated with releasing the mousebutton), changes are written back
44  * to the XML tree, thus generating only a single set of undo actions.
45  * (Lauris Kaplinski)
46  */
48 #ifdef HAVE_CONFIG_H
49 # include "config.h"
50 #endif
54 #if HAVE_STRING_H
55 #endif
58 #if HAVE_STDLIB_H
59 #endif
61 #include "xml/repr.h"
62 #include "document-private.h"
63 #include "inkscape.h"
64 #include "debug/event-tracker.h"
65 #include "debug/simple-event.h"
66 #include "debug/timestamp.h"
67 #include "event.h"
70 /*
71  * Undo & redo
72  */
73 /**
74  * Set undo sensitivity.
75  *
76  * \note
77  *   Since undo sensitivity needs to be nested, setting undo sensitivity
78  *   should be done like this:
79  *\verbatim
80         bool saved = sp_document_get_undo_sensitive(document);
81         sp_document_set_undo_sensitive(document, false);
82         ... do stuff ...
83         sp_document_set_undo_sensitive(document, saved);  \endverbatim
84  */
85 void
86 sp_document_set_undo_sensitive (SPDocument *doc, bool sensitive)
87 {
88         g_assert (doc != NULL);
89         g_assert (doc->priv != NULL);
91         if ( sensitive == doc->priv->sensitive )
92                 return;
94         if (sensitive) {
95                 sp_repr_begin_transaction (doc->rdoc);
96         } else {
97                 doc->priv->partial = sp_repr_coalesce_log (
98                         doc->priv->partial,
99                         sp_repr_commit_undoable (doc->rdoc)
100                 );
101         }
103         doc->priv->sensitive = sensitive;
106 /*TODO: Throughout the inkscape code tree set/get_undo_sensitive are used for
107  * as is shown above.  Perhaps it makes sense to create new functions,
108  * undo_ignore, and undo_recall to replace the start and end parts of the above.
109  * The main complexity with this is that they have to nest, so you have to store
110  * the saved bools in a stack.  Perhaps this is why the above solution is better.
111  */
113 bool sp_document_get_undo_sensitive(SPDocument const *document) {
114         g_assert(document != NULL);
115         g_assert(document->priv != NULL);
117         return document->priv->sensitive;
120 void
121 sp_document_done (SPDocument *doc, const unsigned int event_type, Glib::ustring event_description)
123         sp_document_maybe_done (doc, NULL, event_type, event_description);
126 void
127 sp_document_reset_key (Inkscape::Application *inkscape, SPDesktop *desktop, GtkObject *base)
129         SPDocument *doc = (SPDocument *) base;
130         doc->actionkey = NULL;
133 namespace {
135 using Inkscape::Debug::Event;
136 using Inkscape::Debug::SimpleEvent;
137 using Inkscape::Util::share_static_string;
138 using Inkscape::Debug::timestamp;
139 using Inkscape::Verb;
141 typedef SimpleEvent<Event::INTERACTION> InteractionEvent;
143 class CommitEvent : public InteractionEvent {
144 public:
146     CommitEvent(SPDocument *doc, const gchar *key, const unsigned int type)
147     : InteractionEvent(share_static_string("commit"))
148     {
149         _addProperty(share_static_string("timestamp"), timestamp()); 
150         gchar *serial = g_strdup_printf("%ul", doc->serial());
151         _addProperty(share_static_string("document"), serial);
152         g_free(serial);
153         Verb *verb = Verb::get(type);
154         if (verb) {
155             _addProperty(share_static_string("context"), verb->get_id());
156         }
157         if (key) {
158             _addProperty(share_static_string("merge-key"), key);
159         }
160     }
161 };
165 void
166 sp_document_maybe_done (SPDocument *doc, const gchar *key, const unsigned int event_type,
167                         Glib::ustring event_description)
169         g_assert (doc != NULL);
170         g_assert (doc->priv != NULL);
171         g_assert (doc->priv->sensitive);
173         Inkscape::Debug::EventTracker<CommitEvent> tracker(doc, key, event_type);
175         doc->collectOrphans();
177         sp_document_ensure_up_to_date (doc);
179         sp_document_clear_redo (doc);
181         Inkscape::XML::Event *log = sp_repr_coalesce_log (doc->priv->partial, sp_repr_commit_undoable (doc->rdoc));
182         doc->priv->partial = NULL;
184         if (!log) {
185                 sp_repr_begin_transaction (doc->rdoc);
186                 return;
187         }
189         if (key && doc->actionkey && !strcmp (key, doc->actionkey) && doc->priv->undo) {
190                 ((Inkscape::Event *)doc->priv->undo->data)->event =
191                     sp_repr_coalesce_log (((Inkscape::Event *)doc->priv->undo->data)->event, log);
192         } else {
193                 Inkscape::Event *event = new Inkscape::Event(log, event_type, event_description);
194                 doc->priv->undo = g_slist_prepend (doc->priv->undo, event);
195                 doc->priv->history_size++;
196                 doc->priv->undoStackObservers.notifyUndoCommitEvent(event);
197         }
199         doc->actionkey = key;
201         doc->virgin = FALSE;
202         if (!doc->rroot->attribute("sodipodi:modified")) {
203                 doc->rroot->setAttribute("sodipodi:modified", "true");
204         }
206         sp_repr_begin_transaction (doc->rdoc);
208   doc->priv->commit_signal.emit();
211 void
212 sp_document_cancel (SPDocument *doc)
214         g_assert (doc != NULL);
215         g_assert (doc->priv != NULL);
216         g_assert (doc->priv->sensitive);
218         sp_repr_rollback (doc->rdoc);
220         if (doc->priv->partial) {
221                 sp_repr_undo_log (doc->priv->partial);
222                 sp_repr_free_log (doc->priv->partial);
223                 doc->priv->partial = NULL;
224         }
226         sp_repr_begin_transaction (doc->rdoc);
229 namespace {
231 void finish_incomplete_transaction(SPDocument &doc) {
232         SPDocumentPrivate &priv=*doc.priv;
233         Inkscape::XML::Event *log=sp_repr_commit_undoable(doc.rdoc);
234         if (log || priv.partial) {
235                 g_warning ("Incomplete undo transaction:");
236                 priv.partial = sp_repr_coalesce_log(priv.partial, log);
237                 sp_repr_debug_print_log(priv.partial);
238                 Inkscape::Event *event = new Inkscape::Event(priv.partial);
239                 priv.undo = g_slist_prepend(priv.undo, event);
240                 priv.undoStackObservers.notifyUndoCommitEvent(event);
241                 priv.partial = NULL;
242         }
247 gboolean
248 sp_document_undo (SPDocument *doc)
250         using Inkscape::Debug::EventTracker;
251         using Inkscape::Debug::SimpleEvent;
253         gboolean ret;
255         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("undo");
257         g_assert (doc != NULL);
258         g_assert (doc->priv != NULL);
259         g_assert (doc->priv->sensitive);
261         doc->priv->sensitive = FALSE;
262         doc->priv->seeking = true;
264         doc->actionkey = NULL;
266         finish_incomplete_transaction(*doc);
268         if (doc->priv->undo) {
269                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->undo->data;
270                 doc->priv->undo = g_slist_remove (doc->priv->undo, log);
271                 sp_repr_undo_log (log->event);
272                 doc->priv->redo = g_slist_prepend (doc->priv->redo, log);
274                 doc->rroot->setAttribute("sodipodi:modified", "true");
275                 doc->priv->undoStackObservers.notifyUndoEvent(log);
277                 ret = TRUE;
278         } else {
279                 ret = FALSE;
280         }
282         sp_repr_begin_transaction (doc->rdoc);
284         doc->priv->sensitive = TRUE;
285         doc->priv->seeking = false;
287         if (ret)
288                 inkscape_external_change();
290         return ret;
293 gboolean
294 sp_document_redo (SPDocument *doc)
296         using Inkscape::Debug::EventTracker;
297         using Inkscape::Debug::SimpleEvent;
299         gboolean ret;
301         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("redo");
303         g_assert (doc != NULL);
304         g_assert (doc->priv != NULL);
305         g_assert (doc->priv->sensitive);
307         doc->priv->sensitive = FALSE;
308         doc->priv->seeking = true;
310         doc->actionkey = NULL;
312         finish_incomplete_transaction(*doc);
314         if (doc->priv->redo) {
315                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->redo->data;
316                 doc->priv->redo = g_slist_remove (doc->priv->redo, log);
317                 sp_repr_replay_log (log->event);
318                 doc->priv->undo = g_slist_prepend (doc->priv->undo, log);
320                 doc->rroot->setAttribute("sodipodi:modified", "true");
321                 doc->priv->undoStackObservers.notifyRedoEvent(log);
323                 ret = TRUE;
324         } else {
325                 ret = FALSE;
326         }
328         sp_repr_begin_transaction (doc->rdoc);
330         doc->priv->sensitive = TRUE;
331         doc->priv->seeking = false;
333         if (ret)
334                 inkscape_external_change();
336         return ret;
339 void
340 sp_document_clear_undo (SPDocument *doc)
342         if (doc->priv->undo)
343                 doc->priv->undoStackObservers.notifyClearUndoEvent();
345         while (doc->priv->undo) {
346                 GSList *current;
348                 current = doc->priv->undo;
349                 doc->priv->undo = current->next;
350                 doc->priv->history_size--;
352                 delete ((Inkscape::Event *) current->data);
353                 g_slist_free_1 (current);
354         }
357 void
358 sp_document_clear_redo (SPDocument *doc)
360         if (doc->priv->redo)
361                 doc->priv->undoStackObservers.notifyClearRedoEvent();
363         while (doc->priv->redo) {
364                 GSList *current;
366                 current = doc->priv->redo;
367                 doc->priv->redo = current->next;
368                 doc->priv->history_size--;
370                 delete ((Inkscape::Event *) current->data);
371                 g_slist_free_1 (current);
372         }
374 /*
375   Local Variables:
376   mode:c++
377   c-file-style:"stroustrup"
378   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
379   indent-tabs-mode:nil
380   fill-column:99
381   End:
382 */
383 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :