Code

CodingStyle: Use static for file-local function as per wiki CodingStyle document...
[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         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 static void finish_incomplete_transaction(SPDocument &doc) {
230         SPDocumentPrivate &priv=*doc.priv;
231         Inkscape::XML::Event *log=sp_repr_commit_undoable(doc.rdoc);
232         if (log || priv.partial) {
233                 g_warning ("Incomplete undo transaction:");
234                 priv.partial = sp_repr_coalesce_log(priv.partial, log);
235                 sp_repr_debug_print_log(priv.partial);
236                 Inkscape::Event *event = new Inkscape::Event(priv.partial);
237                 priv.undo = g_slist_prepend(priv.undo, event);
238                 priv.undoStackObservers.notifyUndoCommitEvent(event);
239                 priv.partial = NULL;
240         }
243 gboolean
244 sp_document_undo (SPDocument *doc)
246         using Inkscape::Debug::EventTracker;
247         using Inkscape::Debug::SimpleEvent;
249         gboolean ret;
251         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("undo");
253         g_assert (doc != NULL);
254         g_assert (doc->priv != NULL);
255         g_assert (doc->priv->sensitive);
257         doc->priv->sensitive = FALSE;
258         doc->priv->seeking = true;
260         doc->actionkey = NULL;
262         finish_incomplete_transaction(*doc);
264         if (doc->priv->undo) {
265                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->undo->data;
266                 doc->priv->undo = g_slist_remove (doc->priv->undo, log);
267                 sp_repr_undo_log (log->event);
268                 doc->priv->redo = g_slist_prepend (doc->priv->redo, log);
270                 doc->rroot->setAttribute("sodipodi:modified", "true");
271                 doc->priv->undoStackObservers.notifyUndoEvent(log);
273                 ret = TRUE;
274         } else {
275                 ret = FALSE;
276         }
278         sp_repr_begin_transaction (doc->rdoc);
280         doc->priv->sensitive = TRUE;
281         doc->priv->seeking = false;
283         if (ret)
284                 inkscape_external_change();
286         return ret;
289 gboolean
290 sp_document_redo (SPDocument *doc)
292         using Inkscape::Debug::EventTracker;
293         using Inkscape::Debug::SimpleEvent;
295         gboolean ret;
297         EventTracker<SimpleEvent<Inkscape::Debug::Event::DOCUMENT> > tracker("redo");
299         g_assert (doc != NULL);
300         g_assert (doc->priv != NULL);
301         g_assert (doc->priv->sensitive);
303         doc->priv->sensitive = FALSE;
304         doc->priv->seeking = true;
306         doc->actionkey = NULL;
308         finish_incomplete_transaction(*doc);
310         if (doc->priv->redo) {
311                 Inkscape::Event *log=(Inkscape::Event *)doc->priv->redo->data;
312                 doc->priv->redo = g_slist_remove (doc->priv->redo, log);
313                 sp_repr_replay_log (log->event);
314                 doc->priv->undo = g_slist_prepend (doc->priv->undo, log);
316                 doc->rroot->setAttribute("sodipodi:modified", "true");
317                 doc->priv->undoStackObservers.notifyRedoEvent(log);
319                 ret = TRUE;
320         } else {
321                 ret = FALSE;
322         }
324         sp_repr_begin_transaction (doc->rdoc);
326         doc->priv->sensitive = TRUE;
327         doc->priv->seeking = false;
329         if (ret)
330                 inkscape_external_change();
332         return ret;
335 void
336 sp_document_clear_undo (SPDocument *doc)
338         if (doc->priv->undo)
339                 doc->priv->undoStackObservers.notifyClearUndoEvent();
341         while (doc->priv->undo) {
342                 GSList *current;
344                 current = doc->priv->undo;
345                 doc->priv->undo = current->next;
346                 doc->priv->history_size--;
348                 delete ((Inkscape::Event *) current->data);
349                 g_slist_free_1 (current);
350         }
353 void
354 sp_document_clear_redo (SPDocument *doc)
356         if (doc->priv->redo)
357                 doc->priv->undoStackObservers.notifyClearRedoEvent();
359         while (doc->priv->redo) {
360                 GSList *current;
362                 current = doc->priv->redo;
363                 doc->priv->redo = current->next;
364                 doc->priv->history_size--;
366                 delete ((Inkscape::Event *) current->data);
367                 g_slist_free_1 (current);
368         }
370 /*
371   Local Variables:
372   mode:c++
373   c-file-style:"stroustrup"
374   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
375   indent-tabs-mode:nil
376   fill-column:99
377   End:
378 */
379 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :