Mix group tabs in the mixer.
[ardour.git] / gtk2_ardour / editor_mixer.cc
1 /*
2     Copyright (C) 2003-2004 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 <glibmm/miscutils.h>
21 #include <gtkmm2ext/utils.h>
22 #include <gtkmm2ext/window_title.h>
23
24 #include "pbd/enumwriter.h"
25
26 #include "ardour/audioengine.h"
27
28 #include "editor.h"
29 #include "mixer_strip.h"
30 #include "ardour_ui.h"
31 #include "selection.h"
32 #include "audio_time_axis.h"
33 #include "actions.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace Gtkmm2ext;
39 using namespace PBD;
40
41 void
42 Editor::editor_mixer_button_toggled ()
43 {
44         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
45         if (act) {
46                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
47                 show_editor_mixer (tact->get_active());
48         }
49 }
50
51 void
52 Editor::editor_list_button_toggled ()
53 {
54         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
55         if (act) {
56                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
57                 show_editor_list (tact->get_active());
58         }
59 }
60
61 void
62 Editor::cms_deleted ()
63 {
64         current_mixer_strip = 0;
65 }
66
67 void
68 Editor::show_editor_mixer (bool yn)
69 {
70         boost::shared_ptr<ARDOUR::Route> r;
71
72         show_editor_mixer_when_tracks_arrive = false;
73
74         if (!session) {
75                 show_editor_mixer_when_tracks_arrive = yn;
76                 return;
77         }
78
79         if (yn) {
80
81                 if (selection->tracks.empty()) {
82
83                         if (track_views.empty()) {      
84                                 show_editor_mixer_when_tracks_arrive = true;
85                                 return;
86                         } 
87
88                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
89                                 RouteTimeAxisView* atv;
90                                 
91                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
92                                         r = atv->route();
93                                         break;
94                                 }
95                         }
96
97                 } else {
98                         sort_track_selection ();
99                         
100                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
101                                 RouteTimeAxisView* atv;
102                                 
103                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
104                                         r = atv->route();
105                                         break;
106                                 }
107                         }
108                 }
109
110                 if (r) {
111                         bool created;
112
113                         if (current_mixer_strip == 0) {
114                                 create_editor_mixer ();
115                                 created = true;
116                         } else {
117                                 created = false;
118                         }
119
120                         current_mixer_strip->set_route (r);
121
122                         if (created) {
123                                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
124                         }
125                 }
126                 
127                 if (current_mixer_strip->get_parent() == 0) {
128                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
129                         global_hpacker.reorder_child (*current_mixer_strip, 0);
130                         current_mixer_strip->show_all ();
131                 }
132
133         } else {
134
135                 if (current_mixer_strip) {
136                         if (current_mixer_strip->get_parent() != 0) {
137                                 global_hpacker.remove (*current_mixer_strip);
138                         }
139                 }
140         }
141
142 #ifdef GTKOSX
143         /* XXX gtk problem here */
144         ensure_all_elements_drawn();
145 #endif
146 }
147
148 #ifdef GTKOSX
149 void
150 Editor::ensure_all_elements_drawn ()
151 {
152         controls_layout.queue_draw ();
153         ruler_label_event_box.queue_draw ();
154         time_button_event_box.queue_draw ();
155 }
156 #endif
157
158 void
159 Editor::show_editor_list (bool yn)
160 {
161         if (yn) {
162                 the_notebook.show();
163         } else {
164                 the_notebook.hide();
165         }
166 }
167
168 void
169 Editor::create_editor_mixer ()
170 {
171         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
172                                               *session,
173                                               false);
174         current_mixer_strip->Hiding.connect (mem_fun(*this, &Editor::current_mixer_strip_hidden));
175         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::current_mixer_strip_removed));
176 #ifdef GTKOSX
177         current_mixer_strip->WidthChanged.connect (mem_fun(*this, &Editor::ensure_all_elements_drawn));
178 #endif
179         current_mixer_strip->set_embedded (true);
180 }       
181
182 void
183 Editor::set_selected_mixer_strip (TimeAxisView& view)
184 {
185         RouteTimeAxisView* at;
186         bool created;
187
188         if (!session || (at = dynamic_cast<RouteTimeAxisView*>(&view)) == 0) {
189                 return;
190         }
191
192         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
193         
194         if (act) {
195                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
196                 if (!tact || !tact->get_active()) {
197                         /* not showing mixer strip presently */
198                         return;
199                 }
200         }
201
202         if (current_mixer_strip == 0) {
203                 create_editor_mixer ();
204                 created = true;
205         } else {
206                 created = false;
207         }
208
209         /* might be nothing to do */
210         
211         if (current_mixer_strip->route() == at->route()) {
212                 return;
213         }
214         
215         if (created) {
216                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
217         }
218
219         current_mixer_strip->set_route (at->route());
220 }
221
222 double current = 0.0;
223
224 void
225 Editor::update_current_screen ()
226 {
227         if (_pending_locate_request) {
228                 /* we don't update things when there's a pending locate request, otherwise
229                    when the editor requests a locate there is a chance that this method
230                    will move the playhead before the locate request is processed, causing
231                    a visual glitch. */
232                 return;
233         }
234         
235         if (session && session->engine().running()) {
236
237                 nframes64_t const frame = session->audible_frame();
238
239                 if (_dragging_playhead) {
240                         goto almost_done;
241                 }
242
243                 /* only update if the playhead is on screen or we are following it */
244
245                 if (_follow_playhead && session->requested_return_frame() < 0) {
246
247                         //playhead_cursor->canvas_item.show();
248
249                         if (frame != last_update_frame) {
250
251
252 #undef CONTINUOUS_SCROLL
253 #ifndef  CONTINUOUS_SCROLL
254                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
255                                         
256                                         if (session->transport_speed() < 0) {
257                                                 if (frame > (current_page_frames()/2)) {
258                                                         center_screen (frame-(current_page_frames()/2));
259                                                 } else {
260                                                         center_screen (current_page_frames()/2);
261                                                 }
262                                         } else {
263                                                 center_screen (frame+(current_page_frames()/2));
264                                         }
265                                 }
266
267                                 playhead_cursor->set_position (frame);
268
269 #else  // CONTINUOUS_SCROLL
270                                 
271                                 /* don't do continuous scroll till the new position is in the rightmost quarter of the 
272                                    editor canvas
273                                 */
274                                 
275                                 if (session->transport_speed()) {
276                                         double target = ((double)frame - (double)current_page_frames()/2.0) / frames_per_unit;
277                                         if (target <= 0.0) target = 0.0;
278                                         if ( fabs(target - current) < current_page_frames()/frames_per_unit ) {
279                                                 target = (target * 0.15) + (current * 0.85);
280                                         } else {
281                                                 /* relax */
282                                         }
283                                         //printf("frame: %d,  cpf: %d,  fpu: %6.6f, current: %6.6f, target : %6.6f\n", frame, current_page_frames(), frames_per_unit, current, target );
284                                         current = target;
285                                         horizontal_adjustment.set_value ( current );
286                                 }
287                                 
288                                 playhead_cursor->set_position (frame);
289
290 #endif // CONTINUOUS_SCROLL
291
292                         }
293
294                 } else {
295                         if (frame != last_update_frame) {
296                                 playhead_cursor->set_position (frame);
297                         }
298                 }
299
300           almost_done:
301                 last_update_frame = frame;
302                 if (current_mixer_strip) {
303                         current_mixer_strip->fast_update ();
304                 }
305                 
306         }
307 }
308
309 void
310 Editor::current_mixer_strip_removed ()
311 {
312         if (current_mixer_strip) {
313                 /* it is being deleted elsewhere */
314                 current_mixer_strip = 0;
315         }
316 }
317
318 void
319 Editor::current_mixer_strip_hidden ()
320 {
321         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
322                 
323                 RouteTimeAxisView* tmp;
324                 
325                 if ((tmp = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
326                         if (tmp->route() == current_mixer_strip->route()) {
327                                 (*i)->set_selected (false);
328                                 break;
329                         }
330                 }
331         }
332
333         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
334         if (act) {
335                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
336                 tact->set_active (false);
337         }
338 }
339
340 void
341 Editor::session_going_away ()
342 {
343         _have_idled = false;
344         
345         for (vector<sigc::connection>::iterator i = session_connections.begin(); i != session_connections.end(); ++i) {
346                 (*i).disconnect ();
347         }
348
349         stop_scrolling ();
350         selection->clear ();
351         cut_buffer->clear ();
352
353         clicked_regionview = 0;
354         clicked_axisview = 0;
355         clicked_routeview = 0;
356         clicked_crossfadeview = 0;
357         entered_regionview = 0;
358         entered_track = 0;
359         last_update_frame = 0;
360         _drag = 0;
361
362         playhead_cursor->canvas_item.hide ();
363
364         /* hide all tracks */
365
366         hide_all_tracks (false);
367
368         /* rip everything out of the list displays */
369
370         region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
371         route_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
372         named_selection_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
373         edit_group_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
374
375         region_list_model->clear ();
376         route_display_model->clear ();
377         named_selection_model->clear ();
378         group_model->clear ();
379
380         region_list_display.set_model (region_list_model);
381         route_list_display.set_model (route_display_model);
382         named_selection_display.set_model (named_selection_model);
383         edit_group_display.set_model (group_model);
384
385         edit_point_clock_connection_a.disconnect();
386         edit_point_clock_connection_b.disconnect();
387
388         edit_point_clock.set_session (0);
389         zoom_range_clock.set_session (0);
390         nudge_clock.set_session (0);
391
392         editor_list_button.set_active(false);
393         editor_list_button.set_sensitive(false);
394         
395         /* clear tempo/meter rulers */
396         remove_metric_marks ();
397         hide_measures ();
398         clear_marker_display ();
399
400         delete current_bbt_points;
401         current_bbt_points = 0;
402         
403         /* get rid of any existing editor mixer strip */
404         
405         if (current_mixer_strip) {
406                 if (current_mixer_strip->get_parent() != 0) {
407                         global_hpacker.remove (*current_mixer_strip);
408                 }
409                 delete current_mixer_strip;
410                 current_mixer_strip = 0;
411         }
412
413         WindowTitle title(Glib::get_application_name());
414         title += _("Editor");
415
416         set_title (title.get_string());
417
418         session = 0;
419 }
420
421 void
422 Editor::maybe_add_mixer_strip_width (XMLNode& node)
423 {
424         if (current_mixer_strip) {
425                 node.add_property ("mixer-width", enum_2_string (current_mixer_strip->get_width()));
426         }
427 }
428