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