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