Don't defer Editor::track_canvas_size_allocated () (defer the timebar redrawing inste...
[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_new (boost::shared_ptr<ARDOUR::Route> r)
62 {
63         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(), *session, r);
64         current_mixer_strip->GoingAway.connect (mem_fun (*this, &Editor::cms_deleted));
65 }
66
67 void
68 Editor::cms_deleted ()
69 {
70         current_mixer_strip = 0;
71 }
72
73 void
74 Editor::show_editor_mixer (bool yn)
75 {
76         show_editor_mixer_when_tracks_arrive = false;
77
78         if (yn) {
79
80                 if (current_mixer_strip == 0) {
81
82                         if (selection->tracks.empty()) {
83                                 
84                                 if (track_views.empty()) {      
85                                         show_editor_mixer_when_tracks_arrive = true;
86                                         return;
87                                 } 
88                                 
89                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
90                                         AudioTimeAxisView* atv;
91
92                                         if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
93                                                 cms_new (atv->route ());
94                                                 break;
95                                         }
96                                 }
97
98                         } else {
99
100                                 sort_track_selection ();
101
102                                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
103                                         AudioTimeAxisView* atv;
104
105                                         if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
106                                                 cms_new (atv->route ());
107                                                 break;
108                                         }
109                                 }
110
111                         }
112
113                         if (current_mixer_strip == 0) {
114                                 return;
115                         }               
116                 }
117                 
118                 if (current_mixer_strip->get_parent() == 0) {
119                         current_mixer_strip->set_embedded (true);
120                         current_mixer_strip->Hiding.connect (mem_fun(*this, &Editor::current_mixer_strip_hidden));
121                         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::current_mixer_strip_removed));
122                         current_mixer_strip->set_width (editor_mixer_strip_width, (void*) this);
123
124                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
125                         global_hpacker.reorder_child (*current_mixer_strip, 0);
126
127                         current_mixer_strip->show_all ();
128                 }
129
130         } else {
131
132                 if (current_mixer_strip) {
133                         editor_mixer_strip_width = current_mixer_strip->get_width ();
134                         if (current_mixer_strip->get_parent() != 0) {
135                                 global_hpacker.remove (*current_mixer_strip);
136                         }
137                 }
138         }
139 }
140
141 void
142 Editor::show_editor_list (bool yn)
143 {
144         if (yn) {
145                 the_notebook.show();
146         } else {
147                 the_notebook.hide();
148         }
149 }
150
151 void
152 Editor::set_selected_mixer_strip (TimeAxisView& view)
153 {
154         RouteTimeAxisView* rt;
155         bool show = false;
156
157         if (!session || (rt = dynamic_cast<RouteTimeAxisView*>(&view)) == 0) {
158                 return;
159         }
160         
161         if (current_mixer_strip) {
162
163                 /* might be nothing to do */
164
165                 if (current_mixer_strip->route() == rt->route()) {
166                         return;
167                 }
168
169                 if (current_mixer_strip->get_parent()) {
170                         show = true;
171                 }
172                 delete current_mixer_strip;
173                 current_mixer_strip = 0;
174         }
175
176         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
177                                               *session,
178                                               rt->route(), false);
179         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));
180         
181         if (show) {
182                 show_editor_mixer (true);
183         }
184 }
185
186 double current = 0.0;
187 bool currentInitialized = 0;
188
189 void
190 Editor::update_current_screen ()
191 {
192         if (session && session->engine().running()) {
193
194                 nframes64_t frame;
195
196                 frame = session->audible_frame();
197
198                 if (_dragging_playhead) {
199                         goto almost_done;
200                 }
201
202                 /* only update if the playhead is on screen or we are following it */
203
204                 if (_follow_playhead && session->requested_return_frame() < 0) {
205
206                         //playhead_cursor->canvas_item.show();
207
208                         if (frame != last_update_frame) {
209
210
211 #undef CONTINUOUS_SCROLL
212 #ifndef  CONTINUOUS_SCROLL
213                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
214                                         
215                                         if (session->transport_speed() < 0) {
216                                                 if (frame > (current_page_frames()/2)) {
217                                                         center_screen (frame-(current_page_frames()/2));
218                                                 } else {
219                                                         center_screen (current_page_frames()/2);
220                                                 }
221                                         } else {
222                                                 center_screen (frame+(current_page_frames()/2));
223                                         }
224                                 }
225
226                                 playhead_cursor->set_position (frame);
227
228 #else  // CONTINUOUS_SCROLL
229                                 
230                                 /* don't do continuous scroll till the new position is in the rightmost quarter of the 
231                                    editor canvas
232                                 */
233                                 
234                                 if (session->transport_speed()) {
235                                         double target = ((double)frame - (double)current_page_frames()/2.0) / frames_per_unit;
236                                         if (target <= 0.0) target = 0.0;
237                                         if ( fabs(target - current) < current_page_frames()/frames_per_unit ) {
238                                                 target = (target * 0.15) + (current * 0.85);
239                                         } else {
240                                                 /* relax */
241                                         }
242                                         //printf("frame: %d,  cpf: %d,  fpu: %6.6f, current: %6.6f, target : %6.6f\n", frame, current_page_frames(), frames_per_unit, current, target );
243                                         current = target;
244                                         horizontal_adjustment.set_value ( current );
245                                 }
246                                 
247                                 playhead_cursor->set_position (frame);
248
249 #endif // CONTINUOUS_SCROLL
250
251                         }
252
253                 } else {
254                         
255                         if (frame != last_update_frame) {
256                                 playhead_cursor->set_position (frame);
257                         }
258                 }
259
260           almost_done:
261                 last_update_frame = frame;
262 #ifdef GTKOSX
263                 /*XXX in a perfect world we would not have to do this. */
264                 track_canvas->update_now();
265 #endif
266                 if (current_mixer_strip) {
267                         current_mixer_strip->fast_update ();
268                 }
269                 
270         }
271 }
272
273 void
274 Editor::current_mixer_strip_removed ()
275 {
276         if (current_mixer_strip) {
277                 /* it is being deleted */
278                 current_mixer_strip = 0;
279         }
280 }
281
282 void
283 Editor::current_mixer_strip_hidden ()
284 {
285         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
286                 
287                 AudioTimeAxisView* tmp;
288                 
289                 if ((tmp = dynamic_cast<AudioTimeAxisView*>(*i)) != 0) {
290                         if (tmp->route() == current_mixer_strip->route()) {
291                                 (*i)->set_selected (false);
292                                 break;
293                         }
294                 }
295         }
296
297         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
298         if (act) {
299                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
300                 tact->set_active (false);
301         }
302 }
303
304 void
305 Editor::session_going_away ()
306 {
307         _have_idled = false;
308         
309         for (vector<sigc::connection>::iterator i = session_connections.begin(); i != session_connections.end(); ++i) {
310                 (*i).disconnect ();
311         }
312
313         stop_scrolling ();
314         selection->clear ();
315         cut_buffer->clear ();
316
317         clicked_regionview = 0;
318         clicked_axisview = 0;
319         clicked_routeview = 0;
320         clicked_crossfadeview = 0;
321         entered_regionview = 0;
322         entered_track = 0;
323         last_update_frame = 0;
324         drag_info.item = 0;
325         last_canvas_frame = 0;
326
327         playhead_cursor->canvas_item.hide ();
328
329         /* hide all tracks */
330
331         hide_all_tracks (false);
332
333         /* rip everything out of the list displays */
334
335         region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
336         route_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
337         named_selection_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
338         edit_group_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
339
340         region_list_model->clear ();
341         route_display_model->clear ();
342         named_selection_model->clear ();
343         group_model->clear ();
344
345         region_list_display.set_model (region_list_model);
346         route_list_display.set_model (route_display_model);
347         named_selection_display.set_model (named_selection_model);
348         edit_group_display.set_model (group_model);
349
350         edit_point_clock_connection_a.disconnect();
351         edit_point_clock_connection_b.disconnect();
352
353         edit_point_clock.set_session (0);
354         zoom_range_clock.set_session (0);
355         nudge_clock.set_session (0);
356
357         /* put editor/mixer toggle button in off position and disable until a new session is loaded */
358
359         editor_mixer_button.set_active(false);
360         editor_mixer_button.set_sensitive(false);
361         editor_list_button.set_active(false);
362         editor_list_button.set_sensitive(false);
363         
364         /* clear tempo/meter rulers */
365         remove_metric_marks ();
366         hide_measures ();
367         clear_marker_display ();
368
369         if (current_bbt_points) {
370                 delete current_bbt_points;
371                 current_bbt_points = 0;
372         }
373
374         /* mixer strip will be deleted all by itself 
375            when its route is deleted.
376         */
377
378         current_mixer_strip = 0;
379
380         WindowTitle title(Glib::get_application_name());
381         title += _("Editor");
382
383         set_title (title.get_string());
384
385         session = 0;
386 }
387
388 void
389 Editor::maybe_add_mixer_strip_width (XMLNode& node)
390 {
391         if (current_mixer_strip) {
392                 node.add_property ("mixer-width", enum_2_string (current_mixer_strip->get_width()));
393         }
394 }
395