529c0b565da3e1e22d815ff37f81703e6feb7dc8
[ardour.git] / gtk2_ardour / redirect_automation_time_axis.cc
1 /*
2     Copyright (C) 2003 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     $Id$
19 */
20
21 #include <ardour/redirect.h>
22 #include <ardour/session.h>
23 #include <cstdlib>
24 #include <pbd/memento_command.h>
25
26 #include "redirect_automation_time_axis.h"
27 #include "automation_line.h"
28 #include "canvas_impl.h"
29
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34 using namespace Gtk;
35
36 RedirectAutomationTimeAxisView::RedirectAutomationTimeAxisView (Session& s, boost::shared_ptr<Route> r, 
37                                                                 PublicEditor& e, TimeAxisView& parent, Canvas& canvas, std::string n,
38                                                                 uint32_t prt, Redirect& rd, string state_name)
39
40         : AxisView (s),
41           AutomationTimeAxisView (s, r, e, parent, canvas, n, state_name, rd.name()),
42           redirect (rd),
43           port (prt)
44         
45 {
46         char buf[32];
47         xml_node = 0;
48         _marked_for_display = false;
49         
50         ensure_xml_node ();
51
52         XMLNodeList kids;
53         XMLNodeConstIterator iter;
54
55         kids = xml_node->children ();
56
57         snprintf (buf, sizeof(buf), "Port_%" PRIu32, port);
58                 
59         for (iter = kids.begin(); iter != kids.end(); ++iter) {
60                 if ((*iter)->name() == buf) {
61                 
62                         XMLProperty *shown = (*iter)->property("shown_editor");
63                         
64                         if (shown && shown->value() == "yes") {
65                                 _marked_for_display = true;
66                         }
67                         break;
68                 }
69         }
70 }
71
72 RedirectAutomationTimeAxisView::~RedirectAutomationTimeAxisView ()
73 {
74 }
75
76 void
77 RedirectAutomationTimeAxisView::add_automation_event (ArdourCanvas::Item* item, GdkEvent* event, nframes_t when, double y)
78 {
79         double x = 0;
80
81         canvas_display->w2i (x, y);
82
83         /* compute vertical fractional position */
84
85         if (y < 0)
86                 y = 0;
87         else if (y > height)
88                 y = height;
89         
90         y = 1.0 - (y / height);
91
92         /* map to model space */
93
94         if (!lines.empty()) {
95                 AutomationList& alist (redirect.automation_list(port));
96                 string description = _("add automation event to ");
97                 description += redirect.describe_parameter (port);
98
99                 lines.front()->view_to_model_y (y);
100                 
101                 _session.begin_reversible_command (description);
102                 XMLNode &before = alist.get_state();
103                 alist.add (when, y);
104                 XMLNode &after = alist.get_state();
105                 _session.add_command(new MementoCommand<AutomationList>(alist, &before, &after));
106                 _session.commit_reversible_command ();
107                 _session.set_dirty ();
108         }
109 }
110
111 void
112 RedirectAutomationTimeAxisView::ensure_xml_node ()
113 {
114         if (xml_node == 0) {
115                 if ((xml_node = redirect.extra_xml ("GUI")) == 0) {
116                         xml_node = new XMLNode ("GUI");
117                         redirect.add_extra_xml (*xml_node);
118                 }
119         }
120 }
121
122 guint32
123 RedirectAutomationTimeAxisView::show_at (double y, int& nth, Gtk::VBox *parent)
124 {
125         ensure_xml_node ();
126         update_extra_xml_shown (true);
127         
128         return TimeAxisView::show_at (y, nth, parent);
129 }
130
131 void
132 RedirectAutomationTimeAxisView::hide ()
133 {
134         ensure_xml_node ();
135         update_extra_xml_shown (false);
136
137         TimeAxisView::hide ();
138 }
139
140
141 void
142 RedirectAutomationTimeAxisView::update_extra_xml_shown (bool editor_shown)
143 {
144         char buf[32];
145
146         XMLNodeList nlist = xml_node->children ();
147         XMLNodeConstIterator i;
148         XMLNode * port_node = 0;
149
150         snprintf (buf, sizeof(buf), "Port_%" PRIu32, port);
151
152         for (i = nlist.begin(); i != nlist.end(); ++i) {
153                 if ((*i)->name() == buf) {
154                         port_node = (*i);
155                         break;
156                 }
157         }
158
159         if (!port_node) {
160                 port_node = new XMLNode(buf);
161                 xml_node->add_child_nocopy(*port_node);
162         }
163         
164         port_node->add_property ("shown_editor", editor_shown ? "yes": "no");
165         
166 }
167
168 void
169 RedirectAutomationTimeAxisView::set_automation_state (AutoState state)
170 {
171         if (!ignore_state_request) {
172                 redirect.automation_list (port).set_automation_state (state);
173         }
174 }