mega commit to remove gtk_object cruft, and much other stuff
[ardour.git] / gtk2_ardour / time_axis_view.cc
1 /*
2     Copyright (C) 2000 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 <cstdlib>
22 #include <cmath>
23 #include <algorithm>
24 #include <string>
25 #include <list>
26
27 #include <libgnomecanvasmm/libgnomecanvasmm.h>
28 #include <libgnomecanvasmm/canvas.h>
29 #include <libgnomecanvasmm/item.h>
30
31 #include <pbd/error.h>
32
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/selector.h>
35 #include <gtkmm2ext/stop_signal.h>
36
37 #include <ardour/session.h>
38 #include <ardour/utils.h>
39 #include <ardour/ladspa_plugin.h>
40 #include <ardour/insert.h>
41 #include <ardour/location.h>
42
43 #include "ardour_ui.h"
44 #include "public_editor.h"
45 #include "time_axis_view.h"
46 #include "simplerect.h"
47 #include "selection.h"
48 #include "keyboard.h"
49 #include "rgb_macros.h"
50 #include "utils.h"
51
52 #include "i18n.h"
53
54 using namespace Gtk;
55 using namespace sigc; 
56 using namespace ARDOUR;
57 using namespace Editing;
58 using namespace ArdourCanvas;
59
60 const double trim_handle_size = 6.0; /* pixels */
61
62 TimeAxisView::TimeAxisView (ARDOUR::Session& sess, PublicEditor& ed, TimeAxisView* rent, Canvas& canvas) 
63         : AxisView (sess), 
64           editor (ed),
65           controls_table (2, 9)
66 {
67         canvas_display = new Group (*canvas.root(), 0.0, 0.0);
68         
69         selection_group = new Group (*canvas_display);
70         selection_group->hide();
71         
72         control_parent = 0;
73         display_menu = 0;
74         size_menu = 0;
75         _marked_for_display = false;
76         _hidden = false;
77         height = 0;
78         effective_height = 0;
79         parent = rent;
80         _has_state = false;
81
82         /*
83           Create the standard LHS Controls
84           We create the top-level container and name add the name label here,
85           subclasses can add to the layout as required
86         */
87
88         name_entry.set_name ("EditorTrackNameDisplay");
89         name_entry.signal_button_release_event().connect (mem_fun (*this, &TimeAxisView::name_entry_button_release));
90         name_entry.signal_button_press_event().connect (mem_fun (*this, &TimeAxisView::name_entry_button_press));
91         
92         Gtkmm2ext::set_size_request_to_display_given_text (name_entry, N_("gTortnam"), 10, 10); // just represents a short name
93
94         name_label.set_name ("TrackLabel");
95         name_label.set_alignment (0.0, 0.5);
96
97         // name_hbox.set_border_width (2);
98         // name_hbox.set_spacing (5);
99
100         /* typically, either name_label OR name_entry are visible,
101            but not both. its up to derived classes to show/hide them as they
102            wish.
103         */
104
105         name_hbox.pack_start (name_label, true, true);
106         name_hbox.pack_start (name_entry, true, true);
107         name_hbox.show ();
108
109         controls_table.set_border_width (2);
110         controls_table.set_row_spacings (0);
111         controls_table.set_col_spacings (0);
112         controls_table.set_homogeneous (true);
113         controls_table.show ();
114
115         controls_table.attach (name_hbox, 0, 5, 0, 1, Gtk::FILL|Gtk::EXPAND, Gtk::FILL|Gtk::EXPAND);
116
117         controls_table.show ();
118
119         controls_vbox.pack_start (controls_table, false, false);
120         controls_vbox.show ();
121
122         controls_ebox.set_name ("TimeAxisViewControlsBaseUnselected");
123         controls_ebox.add (controls_vbox);
124         controls_ebox.set_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
125         controls_ebox.set_flags (Gtk::CAN_FOCUS);
126
127         controls_ebox.signal_button_release_event().connect (mem_fun (*this, &TimeAxisView::controls_ebox_button_release));
128         
129         controls_lhs_pad.set_name ("TimeAxisViewControlsPadding");
130         controls_hbox.pack_start (controls_lhs_pad,false,false);
131         controls_hbox.pack_start (controls_ebox,true,true);
132         controls_hbox.show ();
133
134         controls_frame.add (controls_hbox);
135         controls_frame.set_name ("TimeAxisViewControlsBaseUnselected");
136         controls_frame.set_shadow_type (Gtk::SHADOW_OUT);
137 }
138
139 TimeAxisView::~TimeAxisView()
140 {
141         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
142                 delete *i;
143         }
144
145         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
146                 delete (*i)->rect;
147                 delete (*i)->start_trim;
148                 delete (*i)->end_trim;
149
150         }
151
152         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
153                 delete (*i)->rect;
154                 delete (*i)->start_trim;
155                 delete (*i)->end_trim;
156         }
157
158         if (selection_group) {
159                 delete selection_group;
160                 selection_group = 0;
161         }
162
163         if (canvas_display) {
164                 delete canvas_display;
165                 canvas_display = 0;
166         }
167
168         if (display_menu) {
169                 delete display_menu;
170                 display_menu = 0;
171         }
172
173         if (size_menu) {
174                 delete size_menu;
175                 size_menu = 0;
176         }
177 }
178
179 guint32
180 TimeAxisView::show_at (double y, int& nth, VBox *parent)
181 {
182         gdouble ix1, ix2, iy1, iy2;
183         effective_height = 0;
184
185         if (control_parent) {
186                 control_parent->reorder_child (controls_frame, nth);
187         } else {
188                 control_parent = parent;
189                 parent->pack_start (controls_frame, false, false);
190                 parent->reorder_child (controls_frame, nth);
191         }
192
193         controls_frame.show ();
194         controls_ebox.show ();
195
196         /* the coordinates used here are in the system of the
197            item's parent ...
198         */
199
200         canvas_display->get_bounds (ix1, iy1, ix2, iy2);
201         Group* pg = canvas_display->property_parent();
202         pg->i2w (ix1, iy1);
203
204         if (iy1 < 0) {
205                 iy1 = 0;
206         }
207
208         canvas_display->move (0.0, y - iy1);
209         canvas_display->show();/* XXX not necessary */
210         y_position = y;
211         order = nth;
212         _hidden = false;
213         
214         effective_height = height;
215
216         /* now show children */
217         
218         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
219                 
220                 if ((*i)->marked_for_display()) {
221                         (*i)->canvas_display->show();
222                 }
223                 
224                 if (canvas_item_visible ((*i)->canvas_display)) {
225                         ++nth;
226                         effective_height += (*i)->show_at (y + effective_height, nth, parent);
227                 }
228         }
229
230         return effective_height;
231 }
232
233 gint
234 TimeAxisView::controls_ebox_button_release (GdkEventButton* ev)
235 {
236         switch (ev->button) {
237         case 1:
238                 selection_click (ev);
239                 break;
240
241         case 3:
242                 popup_display_menu (ev->time);
243                 break;
244
245         case 4:
246                 if (Keyboard::no_modifier_keys_pressed (ev)) {
247                         editor.scroll_tracks_up_line ();
248                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
249                         step_height (true);
250                 }
251                 break;
252
253         case 5:
254                 if (Keyboard::no_modifier_keys_pressed (ev)) {
255                         editor.scroll_tracks_down_line ();
256                 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
257                         step_height (false);
258                 }
259                 break;
260                 
261
262         }
263
264         return TRUE;
265 }
266
267 void
268 TimeAxisView::selection_click (GdkEventButton* ev)
269 {
270         if (Keyboard::modifier_state_contains (ev->state, Keyboard::Shift)) {
271
272                 if (editor.get_selection().selected (this)) {
273                         editor.get_selection().remove (this);
274                 } else {
275                         editor.get_selection().add (this);
276                 }
277
278         } else {
279
280                 editor.get_selection().set (this);
281         }
282 }
283
284 void
285 TimeAxisView::hide ()
286 {
287         if (_hidden) {
288                 return;
289         }
290
291         canvas_display->hide();
292         controls_frame.hide ();
293
294         if (control_parent) {
295                 control_parent->remove (controls_frame);
296                 control_parent = 0;
297         }
298
299         y_position = -1;
300         _hidden = true;
301         
302         /* now hide children */
303         
304         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
305                 (*i)->hide ();
306         }
307         
308         Hiding ();
309 }
310
311 void
312 TimeAxisView::step_height (bool bigger)
313 {
314         switch (height) {
315         case Largest:
316                 if (!bigger) set_height (Large);
317                 break;
318         case Large:
319                 if (bigger) set_height (Largest);
320                 else set_height (Larger);
321                 break;
322         case Larger:
323                 if (bigger) set_height (Large);
324                 else set_height (Normal);
325                 break;
326         case Normal:
327                 if (bigger) set_height (Larger);
328                 else set_height (Smaller);
329                 break;
330         case Smaller:
331                 if (bigger) set_height (Normal);
332                 else set_height (Small);
333                 break;
334         case Small:
335                 if (bigger) set_height (Smaller);
336                 break;
337         }
338 }
339
340
341 void
342 TimeAxisView::set_height (TrackHeight h)
343 {
344         height = (guint32) h;
345         controls_frame.set_size_request (-1, height);
346
347         if (canvas_item_visible (selection_group)) {
348                 /* resize the selection rect */
349                 show_selection (editor.get_selection().time);
350         }
351
352 //      for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
353 //              (*i)->set_height (h);
354 //      }
355
356 }
357
358 bool
359 TimeAxisView::name_entry_button_press (GdkEventButton *ev)
360 {
361         if (ev->button == 3) {
362                 return true;
363         }
364         return false;
365 }
366
367 bool
368 TimeAxisView::name_entry_button_release (GdkEventButton *ev)
369 {
370         if (ev->button == 3) {
371                 popup_display_menu (ev->time);
372                 return true;
373         }
374         return false;
375 }
376
377 void
378 TimeAxisView::popup_display_menu (guint32 when)
379 {
380         if (display_menu == 0) {
381                 build_display_menu ();
382         }
383         display_menu->popup (1, when);  
384 }
385
386 gint
387 TimeAxisView::size_click (GdkEventButton *ev)
388 {
389         popup_size_menu (ev->time);
390         return TRUE;
391 }
392
393 void
394 TimeAxisView::popup_size_menu (guint32 when)
395 {
396         if (size_menu == 0) {
397                 build_size_menu ();
398         }
399         size_menu->popup (1, when);
400 }
401
402 void
403 TimeAxisView::set_selected (bool yn)
404 {
405         AxisView::set_selected (yn);
406
407         if (_selected) {
408                 controls_ebox.set_name (controls_base_selected_name);
409                 controls_frame.set_name (controls_base_selected_name);
410
411                 /* propagate any existing selection, if the mode is right */
412
413                 if (editor.current_mouse_mode() == Editing::MouseRange && !editor.get_selection().time.empty()) {
414                         show_selection (editor.get_selection().time);
415                 }
416
417         } else {
418                 controls_ebox.set_name (controls_base_unselected_name);
419                 controls_frame.set_name (controls_base_unselected_name);
420
421                 hide_selection ();
422
423                 /* children will be set for the yn=true case. but when deselecting
424                    the editor only has a list of top-level trackviews, so we
425                    have to do this here.
426                 */
427
428                 for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
429                         (*i)->set_selected (false);
430                 }
431
432                 
433         }
434 }
435
436 void
437 TimeAxisView::build_size_menu ()
438 {
439         using namespace Menu_Helpers;
440
441         size_menu = new Menu;
442         size_menu->set_name ("ArdourContextMenu");
443         MenuList& items = size_menu->items();
444         
445         items.push_back (MenuElem (_("Largest"), bind (mem_fun (*this, &TimeAxisView::set_height), Largest)));
446         items.push_back (MenuElem (_("Large"), bind (mem_fun (*this, &TimeAxisView::set_height), Large)));
447         items.push_back (MenuElem (_("Larger"), bind (mem_fun (*this, &TimeAxisView::set_height), Larger)));
448         items.push_back (MenuElem (_("Normal"), bind (mem_fun (*this, &TimeAxisView::set_height), Normal)));
449         items.push_back (MenuElem (_("Smaller"), bind (mem_fun (*this, &TimeAxisView::set_height), Smaller)));
450         items.push_back (MenuElem (_("Small"), bind (mem_fun (*this, &TimeAxisView::set_height), Small)));
451 }
452
453 void
454 TimeAxisView::build_display_menu ()
455 {
456         using namespace Menu_Helpers;
457
458         display_menu = new Menu;
459         display_menu->set_name ("ArdourContextMenu");
460
461         // Just let implementing classes define what goes into the manu
462 }
463
464 void
465 TimeAxisView::set_samples_per_unit (double spu)
466 {
467         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
468                 (*i)->set_samples_per_unit (spu);
469         }
470 }
471
472 void
473 TimeAxisView::show_timestretch (jack_nframes_t start, jack_nframes_t end)
474 {
475         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
476                 (*i)->show_timestretch (start, end);
477         }
478 }
479
480 void
481 TimeAxisView::hide_timestretch ()
482 {
483         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
484                 (*i)->hide_timestretch ();
485         }
486 }
487
488 void
489 TimeAxisView::show_selection (TimeSelection& ts)
490 {
491         double x1;
492         double x2;
493         double y2;
494         SelectionRect *rect;
495
496         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
497                 (*i)->show_selection (ts);
498         }
499
500         if (canvas_item_visible (selection_group)) {
501                 while (!used_selection_rects.empty()) {
502                         free_selection_rects.push_front (used_selection_rects.front());
503                         used_selection_rects.pop_front();
504                         free_selection_rects.front()->rect->hide();
505                         free_selection_rects.front()->start_trim->hide();
506                         free_selection_rects.front()->end_trim->hide();
507                 }
508                 selection_group->hide();
509         }
510
511         selection_group->show();
512         selection_group->raise_to_top();
513         
514         for (list<AudioRange>::iterator i = ts.begin(); i != ts.end(); ++i) {
515                 jack_nframes_t start, end, cnt;
516
517                 start = (*i).start;
518                 end = (*i).end;
519                 cnt = end - start + 1;
520
521                 rect = get_selection_rect ((*i).id);
522                 
523                 x1 = start / editor.get_current_zoom();
524                 x2 = (start + cnt - 1) / editor.get_current_zoom();
525                 y2 = height;
526
527                 rect->rect->property_x1() = x1;
528                 rect->rect->property_y1() = 1.0;
529                 rect->rect->property_x2() = x2;
530                 rect->rect->property_y2() = y2;
531                 
532                 // trim boxes are at the top for selections
533                 
534                 if (x2 > x1) {
535                         rect->start_trim->property_x1() = x1;
536                         rect->start_trim->property_y1() = 1.0;
537                         rect->start_trim->property_x2() = x1 + trim_handle_size;
538                         rect->start_trim->property_y2() = 1.0 + trim_handle_size;
539
540                         rect->end_trim->property_x1() = x2 - trim_handle_size;
541                         rect->end_trim->property_y1() = 1.0;
542                         rect->end_trim->property_x2() = x2;
543                         rect->end_trim->property_y2() = 1.0 + trim_handle_size;
544
545                         rect->start_trim->show();
546                         rect->end_trim->show();
547                 } else {
548                         rect->start_trim->hide();
549                         rect->end_trim->hide();
550                 }
551
552                 rect->rect->show ();
553                 used_selection_rects.push_back (rect);
554         }
555 }
556
557 void
558 TimeAxisView::reshow_selection (TimeSelection& ts)
559 {
560         show_selection (ts);
561
562         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
563                 (*i)->show_selection (ts);
564         }
565 }
566
567 void
568 TimeAxisView::hide_selection ()
569 {
570         if (canvas_item_visible (selection_group)) {
571                 while (!used_selection_rects.empty()) {
572                         free_selection_rects.push_front (used_selection_rects.front());
573                         used_selection_rects.pop_front();
574                         free_selection_rects.front()->rect->hide();
575                         free_selection_rects.front()->start_trim->hide();
576                         free_selection_rects.front()->end_trim->hide();
577                 }
578                 selection_group->hide();
579         }
580         
581         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
582                 (*i)->hide_selection ();
583         }
584 }
585
586 void
587 TimeAxisView::order_selection_trims (ArdourCanvas::Item *item, bool put_start_on_top)
588 {
589         /* find the selection rect this is for. we have the item corresponding to one
590            of the trim handles.
591          */
592
593         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
594                 if ((*i)->start_trim == item || (*i)->end_trim == item) {
595                         
596                         /* make one trim handle be "above" the other so that if they overlap,
597                            the top one is the one last used.
598                         */
599                         
600                         (*i)->rect->raise_to_top ();
601                         (put_start_on_top ? (*i)->start_trim : (*i)->end_trim)->raise_to_top ();
602                         (put_start_on_top ? (*i)->end_trim : (*i)->start_trim)->raise_to_top ();
603                         
604                         break;
605                 }
606         }
607 }
608
609 SelectionRect *
610 TimeAxisView::get_selection_rect (uint32_t id)
611 {
612         SelectionRect *rect;
613
614         /* check to see if we already have a visible rect for this particular selection ID */
615
616         for (list<SelectionRect*>::iterator i = used_selection_rects.begin(); i != used_selection_rects.end(); ++i) {
617                 if ((*i)->id == id) {
618                         return (*i);
619                 }
620         }
621
622         /* ditto for the free rect list */
623
624         for (list<SelectionRect*>::iterator i = free_selection_rects.begin(); i != free_selection_rects.end(); ++i) {
625                 if ((*i)->id == id) {
626                         free_selection_rects.erase (i);
627                         return (*i);
628                 }
629         }
630
631         /* no existing matching rect, so go get a new one from the free list, or create one if there are none */
632         
633         if (free_selection_rects.empty()) {
634
635                 rect = new SelectionRect;
636
637                 rect->rect = new SimpleRect (*selection_group);
638                 rect->rect->property_x1() = 0.0;
639                 rect->rect->property_y1() = 0.0;
640                 rect->rect->property_x2() = 0.0;
641                 rect->rect->property_y2() = 0.0;
642                 rect->rect->property_fill_color_rgba() = color_map[cSelectionRectFill];
643                 rect->rect->property_outline_color_rgba() = color_map[cSelectionRectOutline];
644                 
645                 rect->start_trim = new SimpleRect (*selection_group);
646                 rect->start_trim->property_x1() = 0.0;
647                 rect->start_trim->property_x2() = 0.0;
648                 rect->start_trim->property_fill_color_rgba() = color_map[cSelectionStartFill];
649                 rect->start_trim->property_outline_color_rgba() = color_map[cSelectionStartOutline];
650                 
651                 rect->end_trim = new SimpleRect (*selection_group);
652                 rect->end_trim->property_x1() = 0.0;
653                 rect->end_trim->property_x2() = 0.0;
654                 rect->end_trim->property_fill_color_rgba() = color_map[cSelectionEndFill];
655                 rect->end_trim->property_outline_color_rgba() = color_map[cSelectionEndOutline];
656
657                 free_selection_rects.push_front (rect);
658
659                 rect->rect->signal_event().connect (bind (mem_fun (editor, &PublicEditor::canvas_selection_rect_event), rect->rect, rect));
660                 rect->start_trim->signal_event().connect (bind (mem_fun (editor, &PublicEditor::canvas_selection_start_trim_event), rect->rect, rect));
661                 rect->end_trim->signal_event().connect (bind (mem_fun (editor, &PublicEditor::canvas_selection_end_trim_event), rect->rect, rect));
662         } 
663
664         rect = free_selection_rects.front();
665         rect->id = id;
666         free_selection_rects.pop_front();
667         return rect;
668 }
669
670 bool
671 TimeAxisView::is_child (TimeAxisView* tav)
672 {
673         return find (children.begin(), children.end(), tav) != children.end();
674 }
675
676 void
677 TimeAxisView::add_child (TimeAxisView* child)
678 {
679         children.push_back (child);
680 }
681
682 void
683 TimeAxisView::remove_child (TimeAxisView* child)
684 {
685         vector<TimeAxisView*>::iterator i;
686
687         if ((i = find (children.begin(), children.end(), child)) != children.end()) {
688                 children.erase (i);
689         }
690 }
691
692 void
693 TimeAxisView::get_selectables (jack_nframes_t start, jack_nframes_t end, double top, double bot, list<Selectable*>& result)
694 {
695         return;
696 }
697
698 void
699 TimeAxisView::get_inverted_selectables (Selection& sel, list<Selectable*>& result)
700 {
701         return;
702 }
703
704 bool
705 TimeAxisView::touched (double top, double bot)
706 {
707         /* remember: this is X Window - coordinate space starts in upper left and moves down.
708            y_position is the "origin" or "top" of the track.
709          */
710
711         double mybot = y_position + height; // XXX need to include Editor::track_spacing; 
712         
713         return ((y_position <= bot && y_position >= top) || 
714                 ((mybot <= bot) && (top < mybot)) || 
715                 (mybot >= bot && y_position < top));
716 }               
717
718 void
719 TimeAxisView::set_parent (TimeAxisView& p)
720 {
721         parent = &p;
722 }
723
724 bool
725 TimeAxisView::has_state () const
726 {
727         return _has_state;
728 }
729
730 TimeAxisView*
731 TimeAxisView::get_parent_with_state ()
732 {
733         if (parent == 0) {
734                 return 0;
735         }
736
737         if (parent->has_state()) {
738                 return parent;
739         } 
740
741         return parent->get_parent_with_state ();
742 }               
743
744 void
745 TimeAxisView::set_state (const XMLNode& node)
746 {
747         const XMLProperty *prop;
748
749         if ((prop = node.property ("track_height")) != 0) {
750
751                 if (prop->value() == "largest") {
752                         set_height (Largest);
753                 } else if (prop->value() == "large") {
754                         set_height (Large);
755                 } else if (prop->value() == "larger") {
756                         set_height (Larger);
757                 } else if (prop->value() == "normal") {
758                         set_height (Normal);
759                 } else if (prop->value() == "smaller") {
760                         set_height (Smaller);
761                 } else if (prop->value() == "small") {
762                         set_height (Small);
763                 } else {
764                         error << string_compose(_("unknown track height name \"%1\" in XML GUI information"), prop->value()) << endmsg;
765                         set_height (Normal);
766                 }
767
768         } else {
769                 set_height (Normal);
770         }
771 }
772
773 void
774 TimeAxisView::reset_height()
775 {
776         set_height ((TrackHeight) height);
777
778         for (vector<TimeAxisView*>::iterator i = children.begin(); i != children.end(); ++i) {
779                 (*i)->set_height ((TrackHeight)(*i)->height);
780         }
781 }
782