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