Code

62259fa1947f7c63fcf63c1d5cd0aa67b55482b7
[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 <string>
62 #include <cstring>
63 #include "xml/repr.h"
64 #include "document-private.h"
65 #include "inkscape.h"
66 #include "debug/event-tracker.h"
67 #include "debug/simple-event.h"
68 #include "debug/timestamp.h"
69 #include "event.h"
72 /*
73  * Undo & redo
74  */
75 /**
76  * Set undo sensitivity.
77  *
78  * \note
79  *   Since undo sensitivity needs to be nested, setting undo sensitivity
80  *   should be done like this:
81  *\verbatim
82         bool saved = sp_document_get_undo_sensitive(document);
83         sp_document_set_undo_sensitive(document, false);
84         ... do stuff ...
85         sp_document_set_undo_sensitive(document, saved);  \endverbatim
86  */
87 void
88 sp_document_set_undo_sensitive (SPDocument *doc, bool sensitive)
89 {
90         g_assert (doc != NULL);
91         g_assert (doc->priv != NULL);
93         if ( sensitive == doc->priv->sensitive )
94                 return;
96         if (sensitive) {
97                 sp_repr_begin_transaction (doc->rdoc);
98         } else {
99                 doc->priv->partial = sp_repr_coalesce_log (
100                         doc->priv->partial,
101                         sp_repr_commit_undoable (doc->rdoc)
102                 );
103         }
105         doc->priv->sensitive = sensitive;
108 /*TODO: Throughout the inkscape code tree set/get_undo_sensitive are used for
109  * as is shown above.  Perhaps it makes sense to create new functions,
110  * undo_ignore, and undo_recall to replace the start and end parts of the above.
111  * The main complexity with this is that they have to nest, so you have to store
112  * the saved bools in a stack.  Perhaps this is why the above solution is better.
113  */
115 bool sp_document_get_undo_sensitive(SPDocument const *document) {
116         g_assert(document != NULL);
117         g_assert(document->priv != NULL);
119         return document->priv->sensitive;
122 void
123 sp_document_done (SPDocument *doc, const unsigned int event_type, Glib::ustring event_description)
125         sp_document_maybe_done (doc, NULL, event_type, event_description);
128 void
129 sp_document_reset_key (Inkscape::Application */*inkscape*/, SPDesktop */*desktop*/, GtkObject *base)
131     SPDocument *doc = (SPDocument *) base;
132     doc->actionkey = NULL;
135 namespace {
137 using Inkscape::Debug::Event;
138 using Inkscape::Debug::SimpleEvent;
139 using Inkscape::Util::share_static_string;
140 using Inkscape::Debug::timestamp;
141 using Inkscape::Verb;
143 typedef SimpleEvent<Event::INTERACTION> InteractionEvent;
145 class CommitEvent : public InteractionEvent {
146 public:
148     CommitEvent(SPDocument *doc, const gchar *key, const unsigned int type)
149     : InteractionEvent(share_static_string("commit"))
150     {
151         _addProperty(share_static_string("timestamp"), timestamp());
152         gchar *serial = g_strdup_printf("%lu", doc->serial());
153         _addProperty(share_static_string("document"), serial);
154         g_free(serial);
155         Verb *verb = Verb::get(type);
156         if (verb) {
157             _addProperty(share_static_string("context"), verb->get_id());
158         }
159         if (key) {
160             _addProperty(share_static_string("merge-key"), key);
161         }
162     }
163 };
167 void
168 sp_document_maybe_done (SPDocument *doc, const gchar *key, const unsigned int event_type,
169                         Glib::ustring event_description)
171         g_assert (doc != NULL);
172         g_assert (doc->priv != NULL);
173         g_assert (doc->priv->sensitive);
175         Inkscape::Debug::EventTracker<CommitEvent> tracker(doc, key, event_type);
177         doc->collectOrphans();
179         sp_document_ensure_up_to_date (doc);
181         sp_document_clear_redo (doc);
183         Inkscape::XML::Event *log = sp_repr_coalesce_log (doc->priv->partial, sp_repr_commit_undoable (doc->rdoc));
184         doc->priv->partial = NULL;
186         if (!log) {
187                 sp_repr_begin_transaction (doc->rdoc);
188                 return;
189         }
191         if (key && doc->actionkey && !strcmp (key, doc->actionkey) && doc->priv->undo) {
192                 ((Inkscape::Event *)doc->priv->undo->data)->event =
193                     sp_repr_coalesce_log (((Inkscape::Event *)doc->priv->undo->data)->event, log);
194         } else {
195                 Inkscape::Event *event = new Inkscape::Event(log, event_type, event_description);
196                 doc->priv->undo = g_slist_prepend (doc->priv->undo, event);
197                 doc->priv->history_size++;
198                 doc->priv->undoStackObservers.notifyUndoCommitEvent(event);
199         }
201         if (doc->actionkey)
202             g_free(doc->actionkey);
203         doc->actionkey = key ? g_strdup(key) : NULL;
205         doc->virgin = FALSE;
206         doc->setModifiedSinceSave();
208         sp_repr_begin_transaction (doc->rdoc);
210   doc->priv->commit_signal.emit();
213 void
214 sp_document_cancel (SPDocument *doc)
216         g_assert (doc != NULL);
217         g_assert (doc->priv != NULL);
218         g_assert (doc->priv->sensitive);
220         sp_repr_rollback (doc->rdoc);
222         if (doc->priv->partial) {
223                 sp_repr_undo_log (doc->priv->partial);
224                 sp_repr_free_log (doc->priv->partial);
225                 doc->priv->partial = NULL;
226         }
228         sp_repr_begin_transaction (doc->rdoc);
231 static 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         }
245 gboolean
246 sp_document_undo (SPDocument *doc)
248         using Inkscape::Debug::EventTracker;
249         using Inkscape::Debug::SimpleEvent;
251         gboolean ret;
253         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("undo");
255         g_assert (doc != NULL);
256         g_assert (doc->priv != NULL);
257         g_assert (doc->priv->sensitive);
259         doc->priv->sensitive = FALSE;
260         doc->priv->seeking = true;
262         doc->actionkey = NULL;
264         finish_incomplete_transaction(*doc);
266         if (doc->priv->undo) {
267                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->undo->data;
268                 doc->priv->undo = g_slist_remove (doc->priv->undo, log);
269                 sp_repr_undo_log (log->event);
270                 doc->priv->redo = g_slist_prepend (doc->priv->redo, log);
272                 doc->setModifiedSinceSave();
273                 doc->priv->undoStackObservers.notifyUndoEvent(log);
275                 ret = TRUE;
276         } else {
277                 ret = FALSE;
278         }
280         sp_repr_begin_transaction (doc->rdoc);
282         doc->priv->sensitive = TRUE;
283         doc->priv->seeking = false;
285         if (ret)
286                 inkscape_external_change();
288         return ret;
291 gboolean
292 sp_document_redo (SPDocument *doc)
294         using Inkscape::Debug::EventTracker;
295         using Inkscape::Debug::SimpleEvent;
297         gboolean ret;
299         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("redo");
301         g_assert (doc != NULL);
302         g_assert (doc->priv != NULL);
303         g_assert (doc->priv->sensitive);
305         doc->priv->sensitive = FALSE;
306         doc->priv->seeking = true;
308         doc->actionkey = NULL;
310         finish_incomplete_transaction(*doc);
312         if (doc->priv->redo) {
313                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->redo->data;
314                 doc->priv->redo = g_slist_remove (doc->priv->redo, log);
315                 sp_repr_replay_log (log->event);
316                 doc->priv->undo = g_slist_prepend (doc->priv->undo, log);
318                 doc->setModifiedSinceSave();
319                 doc->priv->undoStackObservers.notifyRedoEvent(log);
321                 ret = TRUE;
322         } else {
323                 ret = FALSE;
324         }
326         sp_repr_begin_transaction (doc->rdoc);
328         doc->priv->sensitive = TRUE;
329         doc->priv->seeking = false;
331         if (ret)
332                 inkscape_external_change();
334         return ret;
337 void
338 sp_document_clear_undo (SPDocument *doc)
340         if (doc->priv->undo)
341                 doc->priv->undoStackObservers.notifyClearUndoEvent();
343         while (doc->priv->undo) {
344                 GSList *current;
346                 current = doc->priv->undo;
347                 doc->priv->undo = current->next;
348                 doc->priv->history_size--;
350                 delete ((Inkscape::Event *) current->data);
351                 g_slist_free_1 (current);
352         }
355 void
356 sp_document_clear_redo (SPDocument *doc)
358         if (doc->priv->redo)
359                 doc->priv->undoStackObservers.notifyClearRedoEvent();
361         while (doc->priv->redo) {
362                 GSList *current;
364                 current = doc->priv->redo;
365                 doc->priv->redo = current->next;
366                 doc->priv->history_size--;
368                 delete ((Inkscape::Event *) current->data);
369                 g_slist_free_1 (current);
370         }
372 /*
373   Local Variables:
374   mode:c++
375   c-file-style:"stroustrup"
376   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
377   indent-tabs-mode:nil
378   fill-column:99
379   End:
380 */
381 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :