Code

stop toggling insensitive state for modification flag updates (fixes critical bug...
[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("%lu", 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         doc->setModifiedSinceSave();
204         sp_repr_begin_transaction (doc->rdoc);
206   doc->priv->commit_signal.emit();
209 void
210 sp_document_cancel (SPDocument *doc)
212         g_assert (doc != NULL);
213         g_assert (doc->priv != NULL);
214         g_assert (doc->priv->sensitive);
216         sp_repr_rollback (doc->rdoc);
218         if (doc->priv->partial) {
219                 sp_repr_undo_log (doc->priv->partial);
220                 sp_repr_free_log (doc->priv->partial);
221                 doc->priv->partial = NULL;
222         }
224         sp_repr_begin_transaction (doc->rdoc);
227 static void finish_incomplete_transaction(SPDocument &doc) {
228         SPDocumentPrivate &priv=*doc.priv;
229         Inkscape::XML::Event *log=sp_repr_commit_undoable(doc.rdoc);
230         if (log || priv.partial) {
231                 g_warning ("Incomplete undo transaction:");
232                 priv.partial = sp_repr_coalesce_log(priv.partial, log);
233                 sp_repr_debug_print_log(priv.partial);
234                 Inkscape::Event *event = new Inkscape::Event(priv.partial);
235                 priv.undo = g_slist_prepend(priv.undo, event);
236                 priv.undoStackObservers.notifyUndoCommitEvent(event);
237                 priv.partial = NULL;
238         }
241 gboolean
242 sp_document_undo (SPDocument *doc)
244         using Inkscape::Debug::EventTracker;
245         using Inkscape::Debug::SimpleEvent;
247         gboolean ret;
249         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("undo");
251         g_assert (doc != NULL);
252         g_assert (doc->priv != NULL);
253         g_assert (doc->priv->sensitive);
255         doc->priv->sensitive = FALSE;
256         doc->priv->seeking = true;
258         doc->actionkey = NULL;
260         finish_incomplete_transaction(*doc);
262         if (doc->priv->undo) {
263                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->undo->data;
264                 doc->priv->undo = g_slist_remove (doc->priv->undo, log);
265                 sp_repr_undo_log (log->event);
266                 doc->priv->redo = g_slist_prepend (doc->priv->redo, log);
268                 doc->setModifiedSinceSave();
269                 doc->priv->undoStackObservers.notifyUndoEvent(log);
271                 ret = TRUE;
272         } else {
273                 ret = FALSE;
274         }
276         sp_repr_begin_transaction (doc->rdoc);
278         doc->priv->sensitive = TRUE;
279         doc->priv->seeking = false;
281         if (ret)
282                 inkscape_external_change();
284         return ret;
287 gboolean
288 sp_document_redo (SPDocument *doc)
290         using Inkscape::Debug::EventTracker;
291         using Inkscape::Debug::SimpleEvent;
293         gboolean ret;
295         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("redo");
297         g_assert (doc != NULL);
298         g_assert (doc->priv != NULL);
299         g_assert (doc->priv->sensitive);
301         doc->priv->sensitive = FALSE;
302         doc->priv->seeking = true;
304         doc->actionkey = NULL;
306         finish_incomplete_transaction(*doc);
308         if (doc->priv->redo) {
309                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->redo->data;
310                 doc->priv->redo = g_slist_remove (doc->priv->redo, log);
311                 sp_repr_replay_log (log->event);
312                 doc->priv->undo = g_slist_prepend (doc->priv->undo, log);
314                 doc->setModifiedSinceSave();
315                 doc->priv->undoStackObservers.notifyRedoEvent(log);
317                 ret = TRUE;
318         } else {
319                 ret = FALSE;
320         }
322         sp_repr_begin_transaction (doc->rdoc);
324         doc->priv->sensitive = TRUE;
325         doc->priv->seeking = false;
327         if (ret)
328                 inkscape_external_change();
330         return ret;
333 void
334 sp_document_clear_undo (SPDocument *doc)
336         if (doc->priv->undo)
337                 doc->priv->undoStackObservers.notifyClearUndoEvent();
339         while (doc->priv->undo) {
340                 GSList *current;
342                 current = doc->priv->undo;
343                 doc->priv->undo = current->next;
344                 doc->priv->history_size--;
346                 delete ((Inkscape::Event *) current->data);
347                 g_slist_free_1 (current);
348         }
351 void
352 sp_document_clear_redo (SPDocument *doc)
354         if (doc->priv->redo)
355                 doc->priv->undoStackObservers.notifyClearRedoEvent();
357         while (doc->priv->redo) {
358                 GSList *current;
360                 current = doc->priv->redo;
361                 doc->priv->redo = current->next;
362                 doc->priv->history_size--;
364                 delete ((Inkscape::Event *) current->data);
365                 g_slist_free_1 (current);
366         }
368 /*
369   Local Variables:
370   mode:c++
371   c-file-style:"stroustrup"
372   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
373   indent-tabs-mode:nil
374   fill-column:99
375   End:
376 */
377 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :