merge 3870-3890 from 2.0-ongoing into 3.X (compiles, runs, no other promises
[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 Gtkmm2ext;
38 using namespace PBD;
39
40 void
41 Editor::editor_mixer_button_toggled ()
42 {
43         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
44         if (act) {
45                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
46                 show_editor_mixer (tact->get_active());
47         }
48 }
49
50 void
51 Editor::editor_list_button_toggled ()
52 {
53         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
54         if (act) {
55                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
56                 show_editor_list (tact->get_active());
57         }
58 }
59
60 void
61 Editor::cms_deleted ()
62 {
63         current_mixer_strip = 0;
64 }
65
66 void
67 Editor::show_editor_mixer (bool yn)
68 {
69         boost::shared_ptr<ARDOUR::Route> r;
70
71         show_editor_mixer_when_tracks_arrive = false;
72
73         if (!session) {
74                 show_editor_mixer_when_tracks_arrive = yn;
75                 return;
76         }
77
78         if (yn) {
79
80                 if (selection->tracks.empty()) {
81                         
82                         if (track_views.empty()) {      
83                                 show_editor_mixer_when_tracks_arrive = true;
84                                 return;
85                         } 
86
87                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
88                                 AudioTimeAxisView* atv;
89                                 
90                                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
91                                         r = atv->route();
92                                         break;
93                                 }
94                         }
95
96                 } else {
97
98                         sort_track_selection ();
99                         
100                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
101                                 AudioTimeAxisView* atv;
102                                 
103                                 if ((atv = dynamic_cast<AudioTimeAxisView*> (*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 (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::create_editor_mixer ()
160 {
161         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
162                                               *session,
163                                               false);
164         current_mixer_strip->Hiding.connect (mem_fun(*this, &Editor::current_mixer_strip_hidden));
165         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::current_mixer_strip_removed));
166 #ifdef GTKOSX
167         current_mixer_strip->WidthChanged.connect (mem_fun(*this, &Editor::ensure_all_elements_drawn));
168 #endif
169         current_mixer_strip->set_embedded (true);
170 }       
171
172 void
173 Editor::show_editor_list (bool yn)
174 {
175         if (yn) {
176                 the_notebook.show();
177         } else {
178                 the_notebook.hide();
179         }
180 }
181
182 void
183 Editor::set_selected_mixer_strip (TimeAxisView& view)
184 {
185         AudioTimeAxisView* at;
186         bool show = false;
187         bool created;
188
189         if (!session || (at = dynamic_cast<AudioTimeAxisView*>(&view)) == 0) {
190                 return;
191         }
192
193         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
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 (current_mixer_strip->get_parent()) {
216                 show = true;
217         }
218
219         current_mixer_strip->set_route (at->route());
220
221         if (created) {
222                 current_mixer_strip->set_width (editor_mixer_strip_width, (void*) this);
223         }
224
225         if (show) {
226                 show_editor_mixer (true);
227         }
228 }
229
230 double current = 0.0;
231 bool currentInitialized = 0;
232
233 void
234 Editor::update_current_screen ()
235 {
236         if (session && session->engine().running()) {
237
238                 nframes64_t frame;
239
240                 frame = session->audible_frame();
241
242                 if (_dragging_playhead) {
243                         goto almost_done;
244                 }
245
246                 /* only update if the playhead is on screen or we are following it */
247
248                 if (_follow_playhead && session->requested_return_frame() < 0) {
249
250                         //playhead_cursor->canvas_item.show();
251
252                         if (frame != last_update_frame) {
253
254
255 #undef CONTINUOUS_SCROLL
256 #ifndef  CONTINUOUS_SCROLL
257                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
258                                         
259                                         if (session->transport_speed() < 0) {
260                                                 if (frame > (current_page_frames()/2)) {
261                                                         center_screen (frame-(current_page_frames()/2));
262                                                 } else {
263                                                         center_screen (current_page_frames()/2);
264                                                 }
265                                         } else {
266                                                 center_screen (frame+(current_page_frames()/2));
267                                         }
268                                 }
269
270                                 playhead_cursor->set_position (frame);
271
272 #else  // CONTINUOUS_SCROLL
273                                 
274                                 /* don't do continuous scroll till the new position is in the rightmost quarter of the 
275                                    editor canvas
276                                 */
277                                 
278                                 if (session->transport_speed()) {
279                                         double target = ((double)frame - (double)current_page_frames()/2.0) / frames_per_unit;
280                                         if (target <= 0.0) target = 0.0;
281                                         if ( fabs(target - current) < current_page_frames()/frames_per_unit ) {
282                                                 target = (target * 0.15) + (current * 0.85);
283                                         } else {
284                                                 /* relax */
285                                         }
286                                         //printf("frame: %d,  cpf: %d,  fpu: %6.6f, current: %6.6f, target : %6.6f\n", frame, current_page_frames(), frames_per_unit, current, target );
287                                         current = target;
288                                         horizontal_adjustment.set_value ( current );
289                                 }
290                                 
291                                 playhead_cursor->set_position (frame);
292
293 #endif // CONTINUOUS_SCROLL
294
295                         }
296
297                 } else {
298                         
299                         if (frame != last_update_frame) {
300                                 playhead_cursor->set_position (frame);
301                         }
302                 }
303
304           almost_done:
305                 last_update_frame = frame;
306                 if (current_mixer_strip) {
307                         current_mixer_strip->fast_update ();
308                 }
309                 
310         }
311 }
312
313 void
314 Editor::current_mixer_strip_removed ()
315 {
316         if (current_mixer_strip) {
317                 /* it is being deleted elsewhere */
318                 current_mixer_strip = 0;
319         }
320 }
321
322 void
323 Editor::current_mixer_strip_hidden ()
324 {
325         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
326                 
327                 AudioTimeAxisView* tmp;
328                 
329                 if ((tmp = dynamic_cast<AudioTimeAxisView*>(*i)) != 0) {
330                         if (tmp->route() == current_mixer_strip->route()) {
331                                 (*i)->set_selected (false);
332                                 break;
333                         }
334                 }
335         }
336
337         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
338         if (act) {
339                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
340                 tact->set_active (false);
341         }
342 }
343
344 void
345 Editor::session_going_away ()
346 {
347         _have_idled = false;
348         
349         for (vector<sigc::connection>::iterator i = session_connections.begin(); i != session_connections.end(); ++i) {
350                 (*i).disconnect ();
351         }
352
353         stop_scrolling ();
354         selection->clear ();
355         cut_buffer->clear ();
356
357         clicked_regionview = 0;
358         clicked_axisview = 0;
359         clicked_routeview = 0;
360         clicked_crossfadeview = 0;
361         entered_regionview = 0;
362         entered_track = 0;
363         last_update_frame = 0;
364         drag_info.item = 0;
365
366         playhead_cursor->canvas_item.hide ();
367
368         /* hide all tracks */
369
370         hide_all_tracks (false);
371
372         /* rip everything out of the list displays */
373
374         region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
375         route_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
376         named_selection_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
377         edit_group_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
378
379         region_list_model->clear ();
380         route_display_model->clear ();
381         named_selection_model->clear ();
382         group_model->clear ();
383
384         region_list_display.set_model (region_list_model);
385         route_list_display.set_model (route_display_model);
386         named_selection_display.set_model (named_selection_model);
387         edit_group_display.set_model (group_model);
388
389         edit_point_clock_connection_a.disconnect();
390         edit_point_clock_connection_b.disconnect();
391
392         edit_point_clock.set_session (0);
393         zoom_range_clock.set_session (0);
394         nudge_clock.set_session (0);
395
396         editor_list_button.set_active(false);
397         editor_list_button.set_sensitive(false);
398         
399         /* clear tempo/meter rulers */
400         remove_metric_marks ();
401         hide_measures ();
402         clear_marker_display ();
403
404         if (current_bbt_points) {
405                 delete current_bbt_points;
406                 current_bbt_points = 0;
407         }
408
409         /* mixer strip will be deleted all by itself 
410            when its route is deleted.
411         */
412
413         current_mixer_strip = 0;
414
415         WindowTitle title(Glib::get_application_name());
416         title += _("Editor");
417
418         set_title (title.get_string());
419
420         session = 0;
421 }
422
423 void
424 Editor::maybe_add_mixer_strip_width (XMLNode& node)
425 {
426         if (current_mixer_strip) {
427                 node.add_property ("mixer-width", enum_2_string (current_mixer_strip->get_width()));
428         }
429 }
430