Code

Connector tool: make connectors avoid the convex hull of shapes.
[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         doc->actionkey = key;
203         doc->virgin = FALSE;
204         doc->setModifiedSinceSave();
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->setModifiedSinceSave();
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->setModifiedSinceSave();
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 :