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