Fix editor sizing issue introduced in 4dc65e66
[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 "ardour/lmath.h"
21 #include "selection_memento.h"
22 #include "editing.h"
23 #include "public_editor.h"
24
25 #include "pbd/i18n.h"
26
27 SelectionMemento::SelectionMemento ()
28 {
29 }
30
31 SelectionMemento::~SelectionMemento ()
32 {
33 }
34
35 XMLNode&
36 SelectionMemento::get_state () {
37
38         XMLNode* node = new XMLNode ("SelectionMemento");
39         PublicEditor& editor = PublicEditor::instance();
40
41         node->set_property ("mouse-mode", enum2str(editor.current_mouse_mode()));
42         node->set_property ("zoom", editor.get_current_zoom());
43         node->set_property ("left-frame", editor.leftmost_sample());
44         node->set_property ("y-origin", editor.get_y_origin());
45
46         node->add_child_nocopy (editor.get_selection().get_state());
47         return *node;
48 }
49
50 int
51 SelectionMemento::set_state (const XMLNode& node, int /*version*/) {
52
53         PublicEditor& editor = PublicEditor::instance();
54         if (node.name() != X_("SelectionMemento")) {
55                 return -1;
56         }
57
58         std::string str;
59         if (node.get_property ("mouse-mode", str)) {
60                 Editing::MouseMode m = Editing::str2mousemode (str);
61                 editor.set_mouse_mode (m, true);
62         }
63
64         float zoom;
65         if (node.get_property ("zoom", zoom)) {
66                 /* older versions of ardour used floating point samples_per_pixel */
67                 editor.reset_zoom (llrintf (zoom));
68         }
69
70         samplepos_t pos;
71         if (node.get_property ("left-frame", pos)) {
72                 if (pos < 0) {
73                         pos = 0;
74                 }
75                 editor.reset_x_origin (pos);
76         }
77
78         double y_origin;
79         if (node.get_property ("y-origin", y_origin)) {
80                 editor.reset_y_origin (y_origin);
81         }
82
83         XMLNodeList children = node.children ();
84         for (XMLNodeList::const_iterator i = children.begin(); i != children.end(); ++i) {
85                 editor.get_selection().set_state (**i, Stateful::current_state_version);
86         }
87
88         return 0;
89 }