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