enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / automation_time_axis.cc
1 /*
2     Copyright (C) 2000-2007 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 */
19
20 #include <utility>
21 #include <gtkmm2ext/barcontroller.h>
22 #include <gtkmm2ext/utils.h>
23 #include <boost/algorithm/string.hpp>
24 #include <boost/lexical_cast.hpp>
25
26 #include "pbd/error.h"
27 #include "pbd/memento_command.h"
28 #include "pbd/stacktrace.h"
29
30 #include "ardour/automation_control.h"
31 #include "ardour/event_type_map.h"
32 #include "ardour/parameter_types.h"
33 #include "ardour/profile.h"
34 #include "ardour/route.h"
35 #include "ardour/session.h"
36
37 #include "canvas/debug.h"
38
39 #include "automation_time_axis.h"
40 #include "automation_streamview.h"
41 #include "gui_thread.h"
42 #include "route_time_axis.h"
43 #include "automation_line.h"
44 #include "paste_context.h"
45 #include "public_editor.h"
46 #include "selection.h"
47 #include "tooltips.h"
48 #include "rgb_macros.h"
49 #include "point_selection.h"
50 #include "control_point.h"
51 #include "utils.h"
52 #include "item_counts.h"
53 #include "ui_config.h"
54
55 #include "pbd/i18n.h"
56
57 using namespace std;
58 using namespace ARDOUR;
59 using namespace ARDOUR_UI_UTILS;
60 using namespace PBD;
61 using namespace Gtk;
62 using namespace Gtkmm2ext;
63 using namespace Editing;
64
65 Pango::FontDescription AutomationTimeAxisView::name_font;
66 bool AutomationTimeAxisView::have_name_font = false;
67
68
69 /** \a a the automatable object this time axis is to display data for.
70  * For route/track automation (e.g. gain) pass the route for both \r and \a.
71  * For route child (e.g. plugin) automation, pass the child for \a.
72  * For region automation (e.g. MIDI CC), pass null for \a.
73  */
74 AutomationTimeAxisView::AutomationTimeAxisView (
75         Session* s,
76         boost::shared_ptr<Route> r,
77         boost::shared_ptr<Automatable> a,
78         boost::shared_ptr<AutomationControl> c,
79         Evoral::Parameter p,
80         PublicEditor& e,
81         TimeAxisView& parent,
82         bool show_regions,
83         ArdourCanvas::Canvas& canvas,
84         const string & nom,
85         const string & nomparent
86         )
87         : SessionHandlePtr (s)
88         , TimeAxisView (s, e, &parent, canvas)
89         , _route (r)
90         , _control (c)
91         , _automatable (a)
92         , _parameter (p)
93         , _base_rect (new ArdourCanvas::Rectangle (_canvas_display))
94         , _view (show_regions ? new AutomationStreamView (*this) : 0)
95         , auto_button (X_("")) /* force addition of a label */
96         , _show_regions (show_regions)
97 {
98         //concatenate plugin name and param name into the tooltip
99         string tipname = nomparent;
100         if (!tipname.empty()) {
101                 tipname += ": ";
102         }
103         tipname += nom;
104         set_tooltip(controls_ebox, tipname);
105
106         //plugin name and param name appear on 2 separate lines in the track header
107         tipname = nomparent;
108         if (!tipname.empty()) {
109                 tipname += "\n";
110         }
111         tipname += nom;
112         _name = tipname;
113
114         CANVAS_DEBUG_NAME (_canvas_display, string_compose ("main for auto %2/%1", _name, r->name()));
115         CANVAS_DEBUG_NAME (selection_group, string_compose ("selections for auto %2/%1", _name, r->name()));
116         CANVAS_DEBUG_NAME (_ghost_group, string_compose ("ghosts for auto %2/%1", _name, r->name()));
117
118         if (!have_name_font) {
119                 name_font = get_font_for_style (X_("AutomationTrackName"));
120                 have_name_font = true;
121         }
122
123         if (_automatable && _control) {
124                 _controller = AutomationController::create (_automatable, _control->parameter(), _control->desc(), _control);
125         }
126
127         const std::string fill_color_name = (dynamic_cast<MidiTimeAxisView*>(&parent)
128                                              ? "midi automation track fill"
129                                              : "audio automation track fill");
130
131         automation_menu = 0;
132         auto_off_item = 0;
133         auto_touch_item = 0;
134         auto_write_item = 0;
135         auto_play_item = 0;
136         mode_discrete_item = 0;
137         mode_line_item = 0;
138
139         ignore_state_request = false;
140         first_call_to_set_height = true;
141
142         CANVAS_DEBUG_NAME (_base_rect, string_compose ("base rect for %1", _name));
143         _base_rect->set_x1 (ArdourCanvas::COORD_MAX);
144         _base_rect->set_outline (false);
145         _base_rect->set_fill_color (UIConfiguration::instance().color_mod (fill_color_name, "automation track fill"));
146         _base_rect->set_data ("trackview", this);
147         _base_rect->Event.connect (sigc::bind (sigc::mem_fun (_editor, &PublicEditor::canvas_automation_track_event), _base_rect, this));
148         if (!a) {
149                 _base_rect->lower_to_bottom();
150         }
151
152         hide_button.set_icon (ArdourIcon::CloseCross);
153         hide_button.set_tweaks(ArdourButton::TrackHeader);
154
155         auto_button.set_name ("route button");
156         hide_button.set_name ("route button");
157
158         auto_button.unset_flags (Gtk::CAN_FOCUS);
159         hide_button.unset_flags (Gtk::CAN_FOCUS);
160
161         controls_table.set_no_show_all();
162
163         set_tooltip(auto_button, _("automation state"));
164         set_tooltip(hide_button, _("hide track"));
165
166         const string str = gui_property ("height");
167         if (!str.empty()) {
168                 set_height (atoi (str));
169         } else {
170                 set_height (preset_height (HeightNormal));
171         }
172
173         //name label isn't editable on an automation track; remove the tooltip
174         set_tooltip (name_label, X_(""));
175
176         /* repack the name label, which TimeAxisView has already attached to
177          * the controls_table
178          */
179
180         if (name_label.get_parent()) {
181                 name_label.get_parent()->remove (name_label);
182         }
183
184         name_label.set_text (_name);
185         name_label.set_alignment (Gtk::ALIGN_LEFT, Gtk::ALIGN_CENTER);
186         name_label.set_name (X_("TrackParameterName"));
187         name_label.set_ellipsize (Pango::ELLIPSIZE_END);
188
189         /* add the buttons */
190         controls_table.set_border_width (1);
191         controls_table.attach (hide_button, 1, 2, 0, 1, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
192         controls_table.attach (name_label,  2, 3, 1, 3, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 2, 0);
193         controls_table.attach (auto_button, 3, 4, 2, 3, Gtk::SHRINK, Gtk::SHRINK, 0, 0);
194
195         Gtk::DrawingArea *blank0 = manage (new Gtk::DrawingArea());
196         Gtk::DrawingArea *blank1 = manage (new Gtk::DrawingArea());
197
198         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(&parent);
199         // TODO use rtv->controls_base_unselected_name
200         // subscribe to route_active_changed, ...
201         if (rtv && rtv->is_audio_track()) {
202                 blank0->set_name ("AudioTrackControlsBaseUnselected");
203         } else if (rtv && rtv->is_midi_track()) {
204                 blank0->set_name ("MidiTrackControlsBaseUnselected");
205         } else if (rtv) {
206                 blank0->set_name ("AudioBusControlsBaseUnselected");
207         } else {
208                 blank0->set_name ("UnknownControlsBaseUnselected");
209         }
210         blank0->set_size_request (-1, -1);
211         blank1->set_size_request (1, 0);
212         VSeparator* separator = manage (new VSeparator());
213         separator->set_name("TrackSeparator");
214         separator->set_size_request (1, -1);
215
216         controls_button_size_group->add_widget(hide_button);
217         controls_button_size_group->add_widget(*blank0);
218
219         time_axis_hbox.pack_start (*blank0, false, false);
220         time_axis_hbox.pack_start (*separator, false, false);
221         time_axis_hbox.reorder_child (*blank0, 0);
222         time_axis_hbox.reorder_child (*separator, 1);
223         time_axis_hbox.reorder_child (time_axis_vbox, 2);
224
225         if (!ARDOUR::Profile->get_mixbus() ) {
226                 time_axis_hbox.pack_start (*blank1, false, false);
227         }
228
229         blank0->show();
230         separator->show();
231         name_label.show ();
232         hide_button.show ();
233
234         if (_controller) {
235                 _controller->disable_vertical_scroll ();
236                 controls_table.attach (*_controller.get(), 2, 4, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND, 0, 0);
237         }
238
239         controls_table.show_all ();
240
241         hide_button.signal_clicked.connect (sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked));
242         auto_button.signal_clicked.connect (sigc::mem_fun(*this, &AutomationTimeAxisView::auto_clicked));
243
244         controls_base_selected_name = X_("AutomationTrackControlsBaseSelected");
245         controls_base_unselected_name = X_("AutomationTrackControlsBase");
246
247         controls_ebox.set_name (controls_base_unselected_name);
248         time_axis_frame.set_name (controls_base_unselected_name);
249
250         /* ask for notifications of any new RegionViews */
251         if (show_regions) {
252
253                 if (_view) {
254                         _view->attach ();
255                 }
256
257         } else {
258                 /* no regions, just a single line for the entire track (e.g. bus gain) */
259
260                 assert (_control);
261
262                 boost::shared_ptr<AutomationLine> line (
263                         new AutomationLine (
264                                 ARDOUR::EventTypeMap::instance().to_symbol(_parameter),
265                                 *this,
266                                 *_canvas_display,
267                                 _control->alist(),
268                                 _control->desc()
269                                 )
270                         );
271
272                 line->set_line_color (UIConfiguration::instance().color ("processor automation line"));
273                 line->set_fill (true);
274                 line->queue_reset ();
275                 add_line (line);
276         }
277
278         /* make sure labels etc. are correct */
279
280         automation_state_changed ();
281         UIConfiguration::instance().ColorsChanged.connect (sigc::mem_fun (*this, &AutomationTimeAxisView::color_handler));
282
283         _route->DropReferences.connect (
284                 _route_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::route_going_away, this), gui_context ()
285                 );
286 }
287
288 AutomationTimeAxisView::~AutomationTimeAxisView ()
289 {
290         cleanup_gui_properties ();
291         delete _view;
292 }
293
294 void
295 AutomationTimeAxisView::route_going_away ()
296 {
297         _route.reset ();
298 }
299
300 void
301 AutomationTimeAxisView::auto_clicked ()
302 {
303         using namespace Menu_Helpers;
304
305         if (automation_menu == 0) {
306                 automation_menu = manage (new Menu);
307                 automation_menu->set_name ("ArdourContextMenu");
308                 MenuList& items (automation_menu->items());
309
310                 items.push_back (MenuElem (S_("Automation|Manual"), sigc::bind (sigc::mem_fun(*this,
311                                                                                               &AutomationTimeAxisView::set_automation_state), (AutoState) ARDOUR::Off)));
312                 items.push_back (MenuElem (_("Play"), sigc::bind (sigc::mem_fun(*this,
313                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Play)));
314                 items.push_back (MenuElem (_("Write"), sigc::bind (sigc::mem_fun(*this,
315                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Write)));
316                 items.push_back (MenuElem (_("Touch"), sigc::bind (sigc::mem_fun(*this,
317                                 &AutomationTimeAxisView::set_automation_state), (AutoState) Touch)));
318         }
319
320         automation_menu->popup (1, gtk_get_current_event_time());
321 }
322
323 void
324 AutomationTimeAxisView::set_automation_state (AutoState state)
325 {
326         if (ignore_state_request) {
327                 return;
328         }
329
330         if (_automatable) {
331                 _automatable->set_parameter_automation_state (_parameter, state);
332         }
333
334         if (_view) {
335                 _view->set_automation_state (state);
336
337                 /* AutomationStreamViews don't signal when their automation state changes, so handle
338                    our updates `manually'.
339                 */
340                 automation_state_changed ();
341         }
342 }
343
344 void
345 AutomationTimeAxisView::automation_state_changed ()
346 {
347         AutoState state;
348
349         /* update button label */
350
351         if (_view) {
352                 state = _view->automation_state ();
353         } else if (_line) {
354                 assert (_control);
355                 state = _control->alist()->automation_state ();
356         } else {
357                 state = ARDOUR::Off;
358         }
359
360         switch (state & (ARDOUR::Off|Play|Touch|Write)) {
361         case ARDOUR::Off:
362                 auto_button.set_text (S_("Automation|Manual"));
363                 if (auto_off_item) {
364                         ignore_state_request = true;
365                         auto_off_item->set_active (true);
366                         auto_play_item->set_active (false);
367                         auto_touch_item->set_active (false);
368                         auto_write_item->set_active (false);
369                         ignore_state_request = false;
370                 }
371                 break;
372         case Play:
373                 auto_button.set_text (_("Play"));
374                 if (auto_play_item) {
375                         ignore_state_request = true;
376                         auto_play_item->set_active (true);
377                         auto_off_item->set_active (false);
378                         auto_touch_item->set_active (false);
379                         auto_write_item->set_active (false);
380                         ignore_state_request = false;
381                 }
382                 break;
383         case Write:
384                 auto_button.set_text (_("Write"));
385                 if (auto_write_item) {
386                         ignore_state_request = true;
387                         auto_write_item->set_active (true);
388                         auto_off_item->set_active (false);
389                         auto_play_item->set_active (false);
390                         auto_touch_item->set_active (false);
391                         ignore_state_request = false;
392                 }
393                 break;
394         case Touch:
395                 auto_button.set_text (_("Touch"));
396                 if (auto_touch_item) {
397                         ignore_state_request = true;
398                         auto_touch_item->set_active (true);
399                         auto_off_item->set_active (false);
400                         auto_play_item->set_active (false);
401                         auto_write_item->set_active (false);
402                         ignore_state_request = false;
403                 }
404                 break;
405         default:
406                 auto_button.set_text (_("???"));
407                 break;
408         }
409 }
410
411 /** The interpolation style of our AutomationList has changed, so update */
412 void
413 AutomationTimeAxisView::interpolation_changed (AutomationList::InterpolationStyle s)
414 {
415         if (mode_line_item && mode_discrete_item) {
416                 if (s == AutomationList::Discrete) {
417                         mode_discrete_item->set_active(true);
418                         mode_line_item->set_active(false);
419                 } else {
420                         mode_line_item->set_active(true);
421                         mode_discrete_item->set_active(false);
422                 }
423         }
424 }
425
426 /** A menu item has been selected to change our interpolation mode */
427 void
428 AutomationTimeAxisView::set_interpolation (AutomationList::InterpolationStyle style)
429 {
430         /* Tell our view's list, if we have one, otherwise tell our own.
431          * Everything else will be signalled back from that.
432          */
433
434         if (_view) {
435                 _view->set_interpolation (style);
436         } else {
437                 assert (_control);
438                 _control->list()->set_interpolation (style);
439         }
440 }
441
442 void
443 AutomationTimeAxisView::clear_clicked ()
444 {
445         assert (_line || _view);
446
447         _editor.begin_reversible_command (_("clear automation"));
448
449         if (_line) {
450                 _line->clear ();
451         } else if (_view) {
452                 _view->clear ();
453         }
454         set_automation_state ((AutoState) ARDOUR::Off);
455         _editor.commit_reversible_command ();
456         _session->set_dirty ();
457 }
458
459 void
460 AutomationTimeAxisView::set_height (uint32_t h, TrackHeightMode m)
461 {
462         bool const changed = (height != (uint32_t) h) || first_call_to_set_height;
463         uint32_t const normal = preset_height (HeightNormal);
464         bool const changed_between_small_and_normal = ( (height < normal && h >= normal) || (height >= normal || h < normal) );
465
466         TimeAxisView::set_height (h, m);
467
468         _base_rect->set_y1 (h);
469
470         if (_line) {
471                 _line->set_height(h - 2.5);
472         }
473
474         if (_view) {
475                 _view->set_height(h);
476                 _view->update_contents_height();
477         }
478
479         if (changed_between_small_and_normal || first_call_to_set_height) {
480
481                 first_call_to_set_height = false;
482
483                 if (h >= preset_height (HeightNormal)) {
484                         if (!(_parameter.type() >= MidiCCAutomation &&
485                               _parameter.type() <= MidiChannelPressureAutomation)) {
486                                 auto_button.show();
487                         }
488                         name_label.show();
489                         hide_button.show();
490
491                 } else if (h >= preset_height (HeightSmall)) {
492                         controls_table.hide_all ();
493                         auto_button.hide();
494                         name_label.hide();
495                 }
496         }
497
498         if (changed) {
499                 if (_canvas_display->visible() && _route) {
500                         /* only emit the signal if the height really changed and we were visible */
501                         _route->gui_changed ("visible_tracks", (void *) 0); /* EMIT_SIGNAL */
502                 }
503         }
504 }
505
506 void
507 AutomationTimeAxisView::set_samples_per_pixel (double fpp)
508 {
509         TimeAxisView::set_samples_per_pixel (fpp);
510
511         if (_line) {
512                 _line->reset ();
513         }
514
515         if (_view) {
516                 _view->set_samples_per_pixel (fpp);
517         }
518 }
519
520 void
521 AutomationTimeAxisView::hide_clicked ()
522 {
523         hide_button.set_sensitive(false);
524         set_marked_for_display (false);
525         RouteTimeAxisView* rtv = dynamic_cast<RouteTimeAxisView*>(parent);
526         if (rtv) {
527                 rtv->request_redraw ();
528         }
529         hide_button.set_sensitive(true);
530 }
531
532 void
533 AutomationTimeAxisView::build_display_menu ()
534 {
535         using namespace Menu_Helpers;
536
537         /* prepare it */
538
539         TimeAxisView::build_display_menu ();
540
541         /* now fill it with our stuff */
542
543         MenuList& items = display_menu->items();
544
545         items.push_back (MenuElem (_("Hide"), sigc::mem_fun(*this, &AutomationTimeAxisView::hide_clicked)));
546         items.push_back (SeparatorElem());
547         items.push_back (MenuElem (_("Clear"), sigc::mem_fun(*this, &AutomationTimeAxisView::clear_clicked)));
548         items.push_back (SeparatorElem());
549
550         /* state menu */
551
552         Menu* auto_state_menu = manage (new Menu);
553         auto_state_menu->set_name ("ArdourContextMenu");
554         MenuList& as_items = auto_state_menu->items();
555
556         as_items.push_back (CheckMenuElem (S_("Automation|Manual"), sigc::bind (
557                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
558                         (AutoState) ARDOUR::Off)));
559         auto_off_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
560
561         as_items.push_back (CheckMenuElem (_("Play"), sigc::bind (
562                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
563                         (AutoState) Play)));
564         auto_play_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
565
566         as_items.push_back (CheckMenuElem (_("Write"), sigc::bind (
567                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
568                         (AutoState) Write)));
569         auto_write_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
570
571         as_items.push_back (CheckMenuElem (_("Touch"), sigc::bind (
572                         sigc::mem_fun(*this, &AutomationTimeAxisView::set_automation_state),
573                         (AutoState) Touch)));
574         auto_touch_item = dynamic_cast<Gtk::CheckMenuItem*>(&as_items.back());
575
576         if (!(_parameter.type() >= MidiCCAutomation &&
577               _parameter.type() <= MidiChannelPressureAutomation)) {
578                 items.push_back (MenuElem (_("State"), *auto_state_menu));
579         }
580
581         /* mode menu */
582
583         /* current interpolation state */
584         AutomationList::InterpolationStyle const s = _view ? _view->interpolation() : _control->list()->interpolation ();
585
586         if (ARDOUR::parameter_is_midi((AutomationType)_parameter.type())) {
587
588                 Menu* auto_mode_menu = manage (new Menu);
589                 auto_mode_menu->set_name ("ArdourContextMenu");
590                 MenuList& am_items = auto_mode_menu->items();
591
592                 RadioMenuItem::Group group;
593
594                 am_items.push_back (RadioMenuElem (group, _("Discrete"), sigc::bind (
595                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
596                                 AutomationList::Discrete)));
597                 mode_discrete_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
598                 mode_discrete_item->set_active (s == AutomationList::Discrete);
599
600                 am_items.push_back (RadioMenuElem (group, _("Linear"), sigc::bind (
601                                 sigc::mem_fun(*this, &AutomationTimeAxisView::set_interpolation),
602                                 AutomationList::Linear)));
603                 mode_line_item = dynamic_cast<Gtk::CheckMenuItem*>(&am_items.back());
604                 mode_line_item->set_active (s == AutomationList::Linear);
605
606                 items.push_back (MenuElem (_("Mode"), *auto_mode_menu));
607         }
608
609         /* make sure the automation menu state is correct */
610
611         automation_state_changed ();
612         interpolation_changed (s);
613 }
614
615 void
616 AutomationTimeAxisView::add_automation_event (GdkEvent* event, framepos_t when, double y, bool with_guard_points)
617 {
618         if (!_line) {
619                 return;
620         }
621
622         boost::shared_ptr<AutomationList> list = _line->the_list ();
623
624         if (list->in_write_pass()) {
625                 /* do not allow the GUI to add automation events during an
626                    automation write pass.
627                 */
628                 return;
629         }
630
631         double x = 0;
632
633         _line->grab_item().canvas_to_item (x, y);
634
635         /* compute vertical fractional position */
636
637         y = 1.0 - (y / _line->height());
638
639         /* map using line */
640
641         _line->view_to_model_coord (x, y);
642
643         _editor.snap_to_with_modifier (when, event);
644
645         XMLNode& before = list->get_state();
646         std::list<Selectable*> results;
647         if (list->editor_add (when, y, with_guard_points)) {
648                 XMLNode& after = list->get_state();
649                 _editor.begin_reversible_command (_("add automation event"));
650                 _session->add_command (new MementoCommand<ARDOUR::AutomationList> (*list.get (), &before, &after));
651
652                 _line->get_selectables (when, when, 0.0, 1.0, results);
653                 _editor.get_selection ().set (results);
654
655                 _editor.commit_reversible_command ();
656                 _session->set_dirty ();
657         }
658 }
659
660 bool
661 AutomationTimeAxisView::paste (framepos_t pos, const Selection& selection, PasteContext& ctx, const int32_t sub_num)
662 {
663         if (_line) {
664                 return paste_one (pos, ctx.count, ctx.times, selection, ctx.counts, ctx.greedy);
665         } else if (_view) {
666                 AutomationSelection::const_iterator l = selection.lines.get_nth(_parameter, ctx.counts.n_lines(_parameter));
667                 if (l == selection.lines.end()) {
668                         if (ctx.greedy && selection.lines.size() == 1) {
669                                 l = selection.lines.begin();
670                         }
671                 }
672                 if (l != selection.lines.end() && _view->paste (pos, ctx.count, ctx.times, *l)) {
673                         ctx.counts.increase_n_lines(_parameter);
674                         return true;
675                 }
676         }
677
678         return false;
679 }
680
681 bool
682 AutomationTimeAxisView::paste_one (framepos_t pos, unsigned paste_count, float times, const Selection& selection, ItemCounts& counts, bool greedy)
683 {
684         boost::shared_ptr<AutomationList> alist(_line->the_list());
685
686         if (_session->transport_rolling() && alist->automation_write()) {
687                 /* do not paste if this control is in write mode and we're rolling */
688                 return false;
689         }
690
691         /* Get appropriate list from selection. */
692         AutomationSelection::const_iterator p = selection.lines.get_nth(_parameter, counts.n_lines(_parameter));
693         if (p == selection.lines.end()) {
694                 if (greedy && selection.lines.size() == 1) {
695                         p = selection.lines.begin();
696                 } else {
697                         return false;
698                 }
699         }
700         counts.increase_n_lines(_parameter);
701
702         /* add multi-paste offset if applicable */
703         pos += _editor.get_paste_offset(pos, paste_count, (*p)->length());
704
705         double const model_pos = _line->time_converter().from (pos - _line->time_converter().origin_b ());
706
707         XMLNode &before = alist->get_state();
708         alist->paste (**p, model_pos, times);
709         _session->add_command (new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
710
711         return true;
712 }
713
714 void
715 AutomationTimeAxisView::get_selectables (framepos_t start, framepos_t end, double top, double bot, list<Selectable*>& results, bool /*within*/)
716 {
717         if (!_line && !_view) {
718                 return;
719         }
720
721         if (touched (top, bot)) {
722
723                 /* remember: this is X Window - coordinate space starts in upper left and moves down.
724                    _y_position is the "origin" or "top" of the track.
725                 */
726
727                 /* bottom of our track */
728                 double const mybot = _y_position + height;
729
730                 double topfrac;
731                 double botfrac;
732
733                 if (_y_position >= top && mybot <= bot) {
734
735                         /* _y_position is below top, mybot is above bot, so we're fully
736                            covered vertically.
737                         */
738
739                         topfrac = 1.0;
740                         botfrac = 0.0;
741
742                 } else {
743
744                         /* top and bot are within _y_position .. mybot */
745
746                         topfrac = 1.0 - ((top - _y_position) / height);
747                         botfrac = 1.0 - ((bot - _y_position) / height);
748
749                 }
750
751                 if (_line) {
752                         _line->get_selectables (start, end, botfrac, topfrac, results);
753                 } else if (_view) {
754                         _view->get_selectables (start, end, botfrac, topfrac, results);
755                 }
756         }
757 }
758
759 void
760 AutomationTimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
761 {
762         if (_line) {
763                 _line->get_inverted_selectables (sel, result);
764         }
765 }
766
767 void
768 AutomationTimeAxisView::set_selected_points (PointSelection& points)
769 {
770         if (_line) {
771                 _line->set_selected_points (points);
772         } else if (_view) {
773                 _view->set_selected_points (points);
774         }
775 }
776
777 void
778 AutomationTimeAxisView::clear_lines ()
779 {
780         _line.reset();
781         _list_connections.drop_connections ();
782 }
783
784 void
785 AutomationTimeAxisView::add_line (boost::shared_ptr<AutomationLine> line)
786 {
787         if (_control && line) {
788                 assert(line->the_list() == _control->list());
789
790                 _control->alist()->automation_state_changed.connect (
791                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::automation_state_changed, this), gui_context()
792                         );
793
794                 _control->alist()->InterpolationChanged.connect (
795                         _list_connections, invalidator (*this), boost::bind (&AutomationTimeAxisView::interpolation_changed, this, _1), gui_context()
796                         );
797         }
798
799         _line = line;
800
801         line->set_height (height);
802
803         /* pick up the current state */
804         automation_state_changed ();
805
806         line->add_visibility (AutomationLine::Line);
807 }
808
809 void
810 AutomationTimeAxisView::entered()
811 {
812         if (_line) {
813                 _line->track_entered();
814         }
815 }
816
817 void
818 AutomationTimeAxisView::exited ()
819 {
820         if (_line) {
821                 _line->track_exited();
822         }
823 }
824
825 void
826 AutomationTimeAxisView::color_handler ()
827 {
828         if (_line) {
829                 _line->set_colors();
830         }
831 }
832
833 int
834 AutomationTimeAxisView::set_state_2X (const XMLNode& node, int /*version*/)
835 {
836         if (node.name() == X_("gain") && _parameter == Evoral::Parameter (GainAutomation)) {
837                 XMLProperty const * shown = node.property (X_("shown"));
838                 if (shown) {
839                         bool yn = string_is_affirmative (shown->value ());
840                         if (yn) {
841                                 _canvas_display->show (); /* FIXME: necessary? show_at? */
842                         }
843                         set_gui_property ("visible", yn);
844                 } else {
845                         set_gui_property ("visible", false);
846                 }
847         }
848
849         return 0;
850 }
851
852 int
853 AutomationTimeAxisView::set_state (const XMLNode&, int /*version*/)
854 {
855         return 0;
856 }
857
858 void
859 AutomationTimeAxisView::what_has_visible_automation (const boost::shared_ptr<Automatable>& automatable, set<Evoral::Parameter>& visible)
860 {
861         /* this keeps "knowledge" of how we store visibility information
862            in XML private to this class.
863         */
864
865         assert (automatable);
866
867         Automatable::Controls& controls (automatable->controls());
868
869         for (Automatable::Controls::iterator i = controls.begin(); i != controls.end(); ++i) {
870
871                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl> (i->second);
872
873                 if (ac && ac->alist()) {
874
875                         const XMLNode* gui_node = ac->extra_xml ("GUI");
876
877                         if (gui_node) {
878                                 XMLProperty const * prop = gui_node->property ("shown");
879                                 if (prop) {
880                                         if (string_is_affirmative (prop->value())) {
881                                                 visible.insert (i->first);
882                                         }
883                                 }
884                         }
885                 }
886         }
887 }
888
889
890 /** @return true if this view has any automation data to display */
891 bool
892 AutomationTimeAxisView::has_automation () const
893 {
894         return ( (_line && _line->npoints() > 0) || (_view && _view->has_automation()) );
895 }
896
897 list<boost::shared_ptr<AutomationLine> >
898 AutomationTimeAxisView::lines () const
899 {
900         list<boost::shared_ptr<AutomationLine> > lines;
901
902         if (_line) {
903                 lines.push_back (_line);
904         } else if (_view) {
905                 lines = _view->get_lines ();
906         }
907
908         return lines;
909 }
910
911 string
912 AutomationTimeAxisView::state_id() const
913 {
914         if (_automatable != _route && _control) {
915                 return string_compose ("automation %1", _control->id().to_s());
916         } else if (_parameter) {
917                 return string_compose ("automation %1 %2/%3/%4",
918                                        _route->id(),
919                                        _parameter.type(),
920                                        _parameter.id(),
921                                        (int) _parameter.channel());
922         } else {
923                 error << "Automation time axis has no state ID" << endmsg;
924                 return "";
925         }
926 }
927
928 /** Given a state id string, see if it is one generated by
929  *  this class.  If so, parse it into its components.
930  *  @param state_id State ID string to parse.
931  *  @param route_id Filled in with the route's ID if the state ID string is parsed.
932  *  @param has_parameter Filled in with true if the state ID has a parameter, otherwise false.
933  *  @param parameter Filled in with the state ID's parameter, if it has one.
934  *  @return true if this is a state ID generated by this class, otherwise false.
935  */
936
937 bool
938 AutomationTimeAxisView::parse_state_id (
939         string const & state_id,
940         PBD::ID & route_id,
941         bool & has_parameter,
942         Evoral::Parameter & parameter)
943 {
944         stringstream s;
945         s << state_id;
946
947         string a, b, c;
948         s >> a >> b >> c;
949
950         if (a != X_("automation")) {
951                 return false;
952         }
953
954         route_id = PBD::ID (b);
955
956         if (c.empty ()) {
957                 has_parameter = false;
958                 return true;
959         }
960
961         has_parameter = true;
962
963         vector<string> p;
964         boost::split (p, c, boost::is_any_of ("/"));
965
966         assert (p.size() == 3);
967
968         parameter = Evoral::Parameter (
969                 boost::lexical_cast<int> (p[0]),
970                 boost::lexical_cast<int> (p[2]),
971                 boost::lexical_cast<int> (p[1])
972                 );
973
974         return true;
975 }
976
977 void
978 AutomationTimeAxisView::cut_copy_clear (Selection& selection, CutCopyOp op)
979 {
980         list<boost::shared_ptr<AutomationLine> > lines;
981         if (_line) {
982                 lines.push_back (_line);
983         } else if (_view) {
984                 lines = _view->get_lines ();
985         }
986
987         for (list<boost::shared_ptr<AutomationLine> >::iterator i = lines.begin(); i != lines.end(); ++i) {
988                 cut_copy_clear_one (**i, selection, op);
989         }
990 }
991
992 void
993 AutomationTimeAxisView::cut_copy_clear_one (AutomationLine& line, Selection& selection, CutCopyOp op)
994 {
995         boost::shared_ptr<Evoral::ControlList> what_we_got;
996         boost::shared_ptr<AutomationList> alist (line.the_list());
997
998         XMLNode &before = alist->get_state();
999
1000         /* convert time selection to automation list model coordinates */
1001         const Evoral::TimeConverter<double, ARDOUR::framepos_t>& tc = line.time_converter ();
1002         double const start = tc.from (selection.time.front().start - tc.origin_b ());
1003         double const end = tc.from (selection.time.front().end - tc.origin_b ());
1004
1005         switch (op) {
1006         case Delete:
1007                 if (alist->cut (start, end) != 0) {
1008                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1009                 }
1010                 break;
1011
1012         case Cut:
1013
1014                 if ((what_we_got = alist->cut (start, end)) != 0) {
1015                         _editor.get_cut_buffer().add (what_we_got);
1016                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1017                 }
1018                 break;
1019         case Copy:
1020                 if ((what_we_got = alist->copy (start, end)) != 0) {
1021                         _editor.get_cut_buffer().add (what_we_got);
1022                 }
1023                 break;
1024
1025         case Clear:
1026                 if ((what_we_got = alist->cut (start, end)) != 0) {
1027                         _session->add_command(new MementoCommand<AutomationList>(*alist.get(), &before, &alist->get_state()));
1028                 }
1029                 break;
1030         }
1031
1032         if (what_we_got) {
1033                 for (AutomationList::iterator x = what_we_got->begin(); x != what_we_got->end(); ++x) {
1034                         double when = (*x)->when;
1035                         double val  = (*x)->value;
1036                         line.model_to_view_coord (when, val);
1037                         (*x)->when = when;
1038                         (*x)->value = val;
1039                 }
1040         }
1041 }
1042
1043 PresentationInfo const &
1044 AutomationTimeAxisView::presentation_info () const
1045 {
1046         return _route->presentation_info();
1047 }
1048
1049 boost::shared_ptr<Stripable>
1050 AutomationTimeAxisView::stripable () const
1051 {
1052         return _route;
1053 }
1054
1055 Gdk::Color
1056 AutomationTimeAxisView::color () const
1057 {
1058         return gdk_color_from_rgb (_route->presentation_info().color());
1059 }