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