2 Copyright (C) 2000-2007 Paul Davis
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.
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.
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.
21 #include <gtkmm2ext/barcontroller.h>
22 #include <gtkmm2ext/utils.h>
23 #include <boost/algorithm/string.hpp>
24 #include <boost/lexical_cast.hpp>
26 #include "pbd/memento_command.h"
27 #include "pbd/stacktrace.h"
29 #include "ardour/automation_control.h"
30 #include "ardour/event_type_map.h"
31 #include "ardour/route.h"
32 #include "ardour/session.h"
34 #include "ardour_ui.h"
35 #include "automation_time_axis.h"
36 #include "automation_streamview.h"
37 #include "global_signals.h"
38 #include "gui_thread.h"
39 #include "route_time_axis.h"
40 #include "automation_line.h"
41 #include "public_editor.h"
42 #include "simplerect.h"
43 #include "selection.h"
44 #include "rgb_macros.h"
45 #include "point_selection.h"
46 #include "canvas_impl.h"
47 #include "control_point.h"
53 using namespace ARDOUR;
56 using namespace Gtkmm2ext;
57 using namespace Editing;
59 Pango::FontDescription AutomationTimeAxisView::name_font;
60 bool AutomationTimeAxisView::have_name_font = false;
63 /** \a a the automatable object this time axis is to display data for.
64 * For route/track automation (e.g. gain) pass the route for both \r and \a.
65 * For route child (e.g. plugin) automation, pass the child for \a.
66 * For region automation (e.g. MIDI CC), pass null for \a.
68 AutomationTimeAxisView::AutomationTimeAxisView (
70 boost::shared_ptr<Route> r,
71 boost::shared_ptr<Automatable> a,
72 boost::shared_ptr<AutomationControl> c,
77 ArdourCanvas::Canvas& canvas,
79 const string & nomparent
82 , TimeAxisView (s, e, &parent, canvas)
88 , _view (show_regions ? new AutomationStreamView (*this) : 0)
90 , auto_button (X_("")) /* force addition of a label */
91 , _show_regions (show_regions)
93 if (!have_name_font) {
94 name_font = get_font_for_style (X_("AutomationTrackName"));
95 have_name_font = true;
98 if (_automatable && _control) {
99 _controller = AutomationController::create (_automatable, _control->parameter(), _control);
107 mode_discrete_item = 0;
110 ignore_state_request = false;
111 first_call_to_set_height = true;
113 _base_rect = new SimpleRect(*_canvas_display);
114 _base_rect->property_x1() = 0.0;
115 _base_rect->property_y1() = 0.0;
116 /** gnomecanvas sometimes converts this value to int or adds 2 to it, so it must be
117 set correctly to avoid overflow.
119 _base_rect->property_x2() = INT_MAX - 2;
120 _base_rect->property_outline_color_rgba() = ARDOUR_UI::config()->canvasvar_AutomationTrackOutline.get();
122 /* outline ends and bottom */
123 _base_rect->property_outline_what() = (guint32) (0x1|0x2|0x8);
124 _base_rect->property_fill_color_rgba() = ARDOUR_UI::config()->canvasvar_AutomationTrackFill.get();
126 _base_rect->set_data ("trackview", this);
128 _base_rect->signal_event().connect (sigc::bind (
129 sigc::mem_fun (_editor, &PublicEditor::canvas_automation_track_event),
133 _base_rect->lower_to_bottom();
136 hide_button.add (*(manage (new Gtk::Image (::get_icon("hide")))));
138 auto_button.set_name ("TrackVisualButton");
139 hide_button.set_name ("TrackRemoveButton");
141 auto_button.unset_flags (Gtk::CAN_FOCUS);
142 hide_button.unset_flags (Gtk::CAN_FOCUS);
144 controls_table.set_no_show_all();
146 ARDOUR_UI::instance()->set_tip(auto_button, _("automation state"));
147 ARDOUR_UI::instance()->set_tip(hide_button, _("hide track"));
149 const string str = gui_property ("height");
151 set_height (atoi (str));
153 set_height (preset_height (HeightNormal));
156 /* repack the name label */
158 if (name_label.get_parent()) {
159 name_label.get_parent()->remove (name_label);
162 name_label.set_text (_name);
163 name_label.set_alignment (Gtk::ALIGN_CENTER, Gtk::ALIGN_CENTER);
164 name_label.set_name (X_("TrackParameterName"));
165 name_label.set_ellipsize (Pango::ELLIPSIZE_END);
167 string tipname = nomparent;
168 if (!tipname.empty()) {
172 ARDOUR_UI::instance()->set_tip(controls_ebox, tipname);
174 /* add the buttons */
175 controls_table.attach (hide_button, 0, 1, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
176 controls_table.attach (name_label, 0, 6, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
177 controls_table.attach (auto_button, 6, 8, 1, 2, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
182 _controller.get()->set_size_request(-1, 24);
183 /* add bar controller */
184 controls_table.attach (*_controller.get(), 1, 8, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
185 /* note that this handler connects *before* the default handler */
186 _controller->event_widget().signal_scroll_event().connect (mem_fun (*this, &AutomationTimeAxisView::controls_ebox_scroll), false);
189 controls_table.show_all ();
191 hide_button.signal_clicked().connect (sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked));
192 auto_button.signal_clicked().connect (sigc::mem_fun(*this, &AutomationTimeAxisView::auto_clicked));
194 controls_base_selected_name = X_("AutomationTrackControlsBaseSelected");
195 controls_base_unselected_name = X_("AutomationTrackControlsBase");
196 controls_ebox.set_name (controls_base_unselected_name);
198 /* ask for notifications of any new RegionViews */
206 /* no regions, just a single line for the entire track (e.g. bus gain) */
210 boost::shared_ptr<AutomationLine> line (
212 ARDOUR::EventTypeMap::instance().to_symbol(_parameter),
219 line->set_line_color (ARDOUR_UI::config()->canvasvar_ProcessorAutomationLine.get());
220 line->queue_reset ();
224 /* make sure labels etc. are correct */
226 automation_state_changed ();
227 ColorsChanged.connect (sigc::mem_fun (*this, &AutomationTimeAxisView::color_handler));
229 _route->DropReferences.connect (
230 _route_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::route_going_away, this), gui_context ()
234 AutomationTimeAxisView::~AutomationTimeAxisView ()
240 AutomationTimeAxisView::route_going_away ()
246 AutomationTimeAxisView::auto_clicked ()
248 using namespace Menu_Helpers;
250 if (automation_menu == 0) {
251 automation_menu = manage (new Menu);
252 automation_menu->set_name ("ArdourContextMenu");
253 MenuList& items (automation_menu->items());
255 items.push_back (MenuElem (S_("Automation|Manual"), sigc::bind (sigc::mem_fun(*this,
256 &AutomationTimeAxisView::set_automation_state), (AutoState) ARDOUR::Off)));
257 items.push_back (MenuElem (_("Play"), sigc::bind (sigc::mem_fun(*this,
258 &AutomationTimeAxisView::set_automation_state), (AutoState) Play)));
259 items.push_back (MenuElem (_("Write"), sigc::bind (sigc::mem_fun(*this,
260 &AutomationTimeAxisView::set_automation_state), (AutoState) Write)));
261 items.push_back (MenuElem (_("Touch"), sigc::bind (sigc::mem_fun(*this,
262 &AutomationTimeAxisView::set_automation_state), (AutoState) Touch)));
265 automation_menu->popup (1, gtk_get_current_event_time());
269 AutomationTimeAxisView::set_automation_state (AutoState state)
271 if (ignore_state_request) {
276 _automatable->set_parameter_automation_state (_parameter, state);
280 _view->set_automation_state (state);
282 /* AutomationStreamViews don't signal when their automation state changes, so handle
283 our updates `manually'.
285 automation_state_changed ();
290 AutomationTimeAxisView::automation_state_changed ()
294 /* update button label */
297 state = _view->automation_state ();
300 state = _control->alist()->automation_state ();
305 switch (state & (ARDOUR::Off|Play|Touch|Write)) {
307 auto_button.set_label (S_("Automation|Manual"));
309 ignore_state_request = true;
310 auto_off_item->set_active (true);
311 auto_play_item->set_active (false);
312 auto_touch_item->set_active (false);
313 auto_write_item->set_active (false);
314 ignore_state_request = false;
318 auto_button.set_label (_("Play"));
319 if (auto_play_item) {
320 ignore_state_request = true;
321 auto_play_item->set_active (true);
322 auto_off_item->set_active (false);
323 auto_touch_item->set_active (false);
324 auto_write_item->set_active (false);
325 ignore_state_request = false;
329 auto_button.set_label (_("Write"));
330 if (auto_write_item) {
331 ignore_state_request = true;
332 auto_write_item->set_active (true);
333 auto_off_item->set_active (false);
334 auto_play_item->set_active (false);
335 auto_touch_item->set_active (false);
336 ignore_state_request = false;
340 auto_button.set_label (_("Touch"));
341 if (auto_touch_item) {
342 ignore_state_request = true;
343 auto_touch_item->set_active (true);
344 auto_off_item->set_active (false);
345 auto_play_item->set_active (false);
346 auto_write_item->set_active (false);
347 ignore_state_request = false;
351 auto_button.set_label (_("???"));
356 /** The interpolation style of our AutomationList has changed, so update */
358 AutomationTimeAxisView::interpolation_changed (AutomationList::InterpolationStyle s)
360 if (mode_line_item && mode_discrete_item) {
361 if (s == AutomationList::Discrete) {
362 mode_discrete_item->set_active(true);
363 mode_line_item->set_active(false);
365 mode_line_item->set_active(true);
366 mode_discrete_item->set_active(false);
371 /** A menu item has been selected to change our interpolation mode */
373 AutomationTimeAxisView::set_interpolation (AutomationList::InterpolationStyle style)
375 /* Tell our view's list, if we have one, otherwise tell our own.
376 * Everything else will be signalled back from that.
380 _view->set_interpolation (style);
383 _control->list()->set_interpolation (style);
388 AutomationTimeAxisView::clear_clicked ()
390 assert (_line || _view);
392 _session->begin_reversible_command (_("clear automation"));
400 _session->commit_reversible_command ();
401 _session->set_dirty ();
405 AutomationTimeAxisView::set_height (uint32_t h)
407 bool const changed = (height != (uint32_t) h) || first_call_to_set_height;
408 uint32_t const normal = preset_height (HeightNormal);
409 bool const changed_between_small_and_normal = ( (height < normal && h >= normal) || (height >= normal || h < normal) );
411 TimeAxisView::set_height (h);
413 _base_rect->property_y2() = h;
416 _line->set_height(h);
420 _view->set_height(h);
421 _view->update_contents_height();
424 if (changed_between_small_and_normal || first_call_to_set_height) {
426 first_call_to_set_height = false;
428 if (h >= preset_height (HeightNormal)) {
430 hide_button.show_all();
432 } else if (h >= preset_height (HeightSmall)) {
433 controls_table.hide_all ();
439 if (canvas_item_visible (_canvas_display) && _route) {
440 /* only emit the signal if the height really changed and we were visible */
441 _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
447 AutomationTimeAxisView::set_samples_per_unit (double spu)
449 TimeAxisView::set_samples_per_unit (spu);
456 _view->set_samples_per_unit (spu);
461 AutomationTimeAxisView::hide_clicked ()
463 hide_button.set_sensitive(false);
464 set_marked_for_display (false);
465 RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(parent);
467 rtv->request_redraw ();
469 hide_button.set_sensitive(true);
473 AutomationTimeAxisView::build_display_menu ()
475 using namespace Menu_Helpers;
479 TimeAxisView::build_display_menu ();
481 /* now fill it with our stuff */
483 MenuList& items = display_menu->items();
485 items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked)));
486 items.push_back (SeparatorElem());
487 items.push_back (MenuElem (_("Clear"), sigc::mem_fun(*this, &AutomationTimeAxisView::clear_clicked)));
488 items.push_back (SeparatorElem());
492 Menu* auto_state_menu = manage (new Menu);
493 auto_state_menu->set_name ("ArdourContextMenu");
494 MenuList& as_items = auto_state_menu->items();
496 as_items.push_back (CheckMenuElem (S_("Automation|Manual"), sigc::bind (
497 sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
498 (AutoState) ARDOUR::Off)));
499 auto_off_item = dynamic_cast<CheckMenuItem*>(&as_items.back());
501 as_items.push_back (CheckMenuElem (_("Play"), sigc::bind (
502 sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
504 auto_play_item = dynamic_cast<CheckMenuItem*>(&as_items.back());
506 as_items.push_back (CheckMenuElem (_("Write"), sigc::bind (
507 sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
508 (AutoState) Write)));
509 auto_write_item = dynamic_cast<CheckMenuItem*>(&as_items.back());
511 as_items.push_back (CheckMenuElem (_("Touch"), sigc::bind (
512 sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
513 (AutoState) Touch)));
514 auto_touch_item = dynamic_cast<CheckMenuItem*>(&as_items.back());
516 items.push_back (MenuElem (_("State"), *auto_state_menu));
520 /* current interpolation state */
521 AutomationList::InterpolationStyle const s = _view ? _view->interpolation() : _control->list()->interpolation ();
523 if (EventTypeMap::instance().is_midi_parameter(_parameter)) {
525 Menu* auto_mode_menu = manage (new Menu);
526 auto_mode_menu->set_name ("ArdourContextMenu");
527 MenuList& am_items = auto_mode_menu->items();
529 RadioMenuItem::Group group;
531 am_items.push_back (RadioMenuElem (group, _("Discrete"), sigc::bind (
532 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
533 AutomationList::Discrete)));
534 mode_discrete_item = dynamic_cast<CheckMenuItem*>(&am_items.back());
535 mode_discrete_item->set_active (s == AutomationList::Discrete);
537 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
538 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
539 AutomationList::Linear)));
540 mode_line_item = dynamic_cast<CheckMenuItem*>(&am_items.back());
541 mode_line_item->set_active (s == AutomationList::Linear);
543 items.push_back (MenuElem (_("Mode"), *auto_mode_menu));
546 /* make sure the automation menu state is correct */
548 automation_state_changed ();
549 interpolation_changed (s);
553 AutomationTimeAxisView::add_automation_event (GdkEvent* event, framepos_t when, double y)
559 boost::shared_ptr<AutomationList> list = _line->the_list ();
561 if (list->in_write_pass()) {
562 /* do not allow the GUI to add automation events during an
563 automation write pass.
570 _canvas_display->w2i (x, y);
572 /* compute vertical fractional position */
574 y = 1.0 - (y / height);
578 _line->view_to_model_coord (x, y);
581 _editor.snap_to_with_modifier (when, event);
583 _session->begin_reversible_command (_("add automation event"));
584 XMLNode& before = list->get_state();
588 XMLNode& after = list->get_state();
589 _session->commit_reversible_command (new MementoCommand<ARDOUR::AutomationList> (*list, &before, &after));
590 _session->set_dirty ();
593 /** Paste a selection.
594 * @param pos Position to paste to (session frames).
595 * @param times Number of times to paste.
596 * @param selection Selection to paste.
597 * @param nth Index of the AutomationList within the selection to paste from.
600 AutomationTimeAxisView::paste (framepos_t pos, float times, Selection& selection, size_t nth)
602 boost::shared_ptr<AutomationLine> line;
607 line = _view->paste_line (pos);
614 return paste_one (*line, pos, times, selection, nth);
618 AutomationTimeAxisView::paste_one (AutomationLine& line, framepos_t pos, float times, Selection& selection, size_t nth)
620 AutomationSelection::iterator p;
621 boost::shared_ptr<AutomationList> alist(line.the_list());
623 if (_session->transport_rolling() && alist->automation_write()) {
624 /* do not paste if this control is in write mode and we're rolling */
628 for (p = selection.lines.begin(); p != selection.lines.end() && nth; ++p, --nth) {}
630 if (p == selection.lines.end()) {
634 double const model_pos = line.time_converter().from (pos - line.time_converter().origin_b ());
636 XMLNode &before = alist->get_state();
637 alist->paste (**p, model_pos, times);
638 _session->add_command (new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
644 AutomationTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results)
646 if (!_line && !_view) {
650 if (touched (top, bot)) {
652 /* remember: this is X Window - coordinate space starts in upper left and moves down.
653 _y_position is the "origin" or "top" of the track.
656 /* bottom of our track */
657 double const mybot = _y_position + height;
662 if (_y_position >= top && mybot <= bot) {
664 /* _y_position is below top, mybot is above bot, so we're fully
673 /* top and bot are within _y_position .. mybot */
675 topfrac = 1.0 - ((top - _y_position) / height);
676 botfrac = 1.0 - ((bot - _y_position) / height);
681 _line->get_selectables (start, end, botfrac, topfrac, results);
683 _view->get_selectables (start, end, botfrac, topfrac, results);
689 AutomationTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
692 _line->get_inverted_selectables (sel, result);
697 AutomationTimeAxisView::set_selected_points (PointSelection& points)
700 _line->set_selected_points (points);
702 _view->set_selected_points (points);
707 AutomationTimeAxisView::clear_lines ()
710 _list_connections.drop_connections ();
714 AutomationTimeAxisView::add_line (boost::shared_ptr<AutomationLine> line)
716 if (_control && line) {
717 assert(line->the_list() == _control->list());
719 _control->alist()->automation_state_changed.connect (
720 _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::automation_state_changed, this), gui_context()
723 _control->alist()->InterpolationChanged.connect (
724 _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::interpolation_changed, this, _1), gui_context()
730 line->set_height (height);
732 /* pick up the current state */
733 automation_state_changed ();
735 line->add_visibility (AutomationLine::Line);
739 AutomationTimeAxisView::entered()
742 _line->track_entered();
747 AutomationTimeAxisView::exited ()
750 _line->track_exited();
755 AutomationTimeAxisView::color_handler ()
763 AutomationTimeAxisView::set_state_2X (const XMLNode& node, int /*version*/)
765 if (node.name() == X_("gain") && _parameter == Evoral::Parameter (GainAutomation)) {
766 XMLProperty const * shown = node.property (X_("shown"));
768 bool yn = string_is_affirmative (shown->value ());
770 _canvas_display->show (); /* FIXME: necessary? show_at? */
772 set_gui_property ("visible", yn);
774 set_gui_property ("visible", false);
782 AutomationTimeAxisView::set_state (const XMLNode&, int /*version*/)
788 AutomationTimeAxisView::what_has_visible_automation (const boost::shared_ptr<Automatable>& automatable, set<Evoral::Parameter>& visible)
790 /* this keeps "knowledge" of how we store visibility information
791 in XML private to this class.
794 assert (automatable);
796 Automatable::Controls& controls (automatable->controls());
798 for (Automatable::Controls::iterator i = controls.begin(); i != controls.end(); ++i) {
800 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
804 const XMLNode* gui_node = ac->extra_xml ("GUI");
807 const XMLProperty* prop = gui_node->property ("shown");
809 if (string_is_affirmative (prop->value())) {
810 visible.insert (i->first);
819 /** @return true if this view has any automation data to display */
821 AutomationTimeAxisView::has_automation () const
823 return ( (_line && _line->npoints() > 0) || (_view && _view->has_automation()) );
826 list<boost::shared_ptr<AutomationLine> >
827 AutomationTimeAxisView::lines () const
829 list<boost::shared_ptr<AutomationLine> > lines;
832 lines.push_back (_line);
834 lines = _view->get_lines ();
841 AutomationTimeAxisView::state_id() const
844 return string_compose ("automation %1", _control->id().to_s());
847 return string_compose ("automation %1 %2/%3/%4",
851 (int) _parameter.channel());
855 /** Given a state id string, see if it is one generated by
856 * this class. If so, parse it into its components.
857 * @param state_id State ID string to parse.
858 * @param route_id Filled in with the route's ID if the state ID string is parsed.
859 * @param has_parameter Filled in with true if the state ID has a parameter, otherwise false.
860 * @param parameter Filled in with the state ID's parameter, if it has one.
861 * @return true if this is a state ID generated by this class, otherwise false.
865 AutomationTimeAxisView::parse_state_id (
866 string const & state_id,
868 bool & has_parameter,
869 Evoral::Parameter & parameter)
877 if (a != X_("automation")) {
881 route_id = PBD::ID (b);
884 has_parameter = false;
888 has_parameter = true;
891 boost::split (p, c, boost::is_any_of ("/"));
893 assert (p.size() == 3);
895 parameter = Evoral::Parameter (
896 boost::lexical_cast<int> (p[0]),
897 boost::lexical_cast<int> (p[2]),
898 boost::lexical_cast<int> (p[1])
905 AutomationTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
907 list<boost::shared_ptr<AutomationLine> > lines;
909 lines.push_back (_line);
911 lines = _view->get_lines ();
914 for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
915 cut_copy_clear_one (**i, selection, op);
920 AutomationTimeAxisView::cut_copy_clear_one (AutomationLine& line, Selection& selection, CutCopyOp op)
922 boost::shared_ptr<Evoral::ControlList> what_we_got;
923 boost::shared_ptr<AutomationList> alist (line.the_list());
925 XMLNode &before = alist->get_state();
927 /* convert time selection to automation list model coordinates */
928 const Evoral::TimeConverter<double, ARDOUR::framepos_t>& tc = line.time_converter ();
929 double const start = tc.from (selection.time.front().start - tc.origin_b ());
930 double const end = tc.from (selection.time.front().end - tc.origin_b ());
934 if (alist->cut (start, end) != 0) {
935 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
941 if ((what_we_got = alist->cut (start, end)) != 0) {
942 _editor.get_cut_buffer().add (what_we_got);
943 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
947 if ((what_we_got = alist->copy (start, end)) != 0) {
948 _editor.get_cut_buffer().add (what_we_got);
953 if ((what_we_got = alist->cut (start, end)) != 0) {
954 _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
960 for (AutomationList::iterator x = what_we_got->begin(); x != what_we_got->end(); ++x) {
961 double when = (*x)->when;
962 double val = (*x)->value;
963 line.model_to_view_coord (when, val);