Don't default to grab mode when undoing selection.
[ardour.git] / gtk2_ardour / selection_memento.cc
1 /*
2     Copyright (C) 2014 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "selection_memento.h"
21 #include "editing.h"
22 #include "public_editor.h"
23
24 #include "i18n.h"
25
26 SelectionMemento::SelectionMemento ()
27 {
28 }
29
30 SelectionMemento::~SelectionMemento ()
31 {
32 }
33
34 XMLNode&
35 SelectionMemento::get_state () {
36
37         XMLNode* node = new XMLNode ("SelectionMemento");
38         char buf[32];
39         PublicEditor& editor = PublicEditor::instance();
40
41         node->add_property ("mouse-mode", enum2str(editor.current_mouse_mode()));
42         snprintf (buf, sizeof(buf), "%" PRId64, editor.get_current_zoom());
43         node->add_property ("zoom", buf);
44         snprintf (buf, sizeof (buf), "%" PRIi64, editor.leftmost_sample());
45         node->add_property ("left-frame", buf);
46         snprintf (buf, sizeof (buf), "%f", editor.get_y_origin());
47         node->add_property ("y-origin", buf);
48
49         node->add_child_nocopy (editor.get_selection().get_state());
50         return *node;
51 }
52
53 int
54 SelectionMemento::set_state (const XMLNode& node, int /*version*/) {
55
56         const XMLProperty* prop;
57         PublicEditor& editor = PublicEditor::instance();
58         if (node.name() != X_("SelectionMemento")) {
59                 return -1;
60         }
61
62         if ((prop = node.property ("mouse-mode"))) {
63                 Editing::MouseMode m = Editing::str2mousemode(prop->value());
64                 editor.set_mouse_mode (m, true);
65         }
66
67         if ((prop = node.property ("zoom"))) {
68                 /* older versions of ardour used floating point samples_per_pixel */
69                 double f = PBD::atof (prop->value());
70                 editor.reset_zoom (llrintf (f));
71         }
72
73         if ((prop = node.property ("left-frame")) != 0) {
74                 framepos_t pos;
75                 if (sscanf (prop->value().c_str(), "%" PRId64, &pos) == 1) {
76                         if (pos < 0) {
77                                 pos = 0;
78                         }
79                         editor.reset_x_origin (pos);
80                 }
81         }
82
83         if ((prop = node.property ("y-origin")) != 0) {
84                 editor.reset_y_origin (atof (prop->value ().c_str()));
85         }
86
87         XMLNodeList children = node.children ();
88         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
89                 editor.get_selection().set_state (**i, Stateful::current_state_version);
90         }
91
92         return 0;
93 }