Merged with trunk R1622.
[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 #include <ardour/audioengine.h>
24
25 #include "editor.h"
26 #include "mixer_strip.h"
27 #include "ardour_ui.h"
28 #include "selection.h"
29 #include "audio_time_axis.h"
30 #include "actions.h"
31
32 #include "i18n.h"
33
34 using namespace Gtkmm2ext;
35
36 void
37 Editor::editor_mixer_button_toggled ()
38 {
39         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
40         if (act) {
41                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
42                 show_editor_mixer (tact->get_active());
43         }
44 }
45
46 void
47 Editor::cms_deleted ()
48 {
49         current_mixer_strip = 0;
50 }
51
52 void
53 Editor::show_editor_mixer (bool yn)
54 {
55         show_editor_mixer_when_tracks_arrive = false;
56
57         if (yn) {
58
59                 if (current_mixer_strip == 0) {
60
61                         if (selection->tracks.empty()) {
62                                 
63                                 if (track_views.empty()) {      
64                                         show_editor_mixer_when_tracks_arrive = true;
65                                         return;
66                                 } 
67                                 
68                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
69                                         AudioTimeAxisView* atv;
70
71                                         if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
72                                                 
73                                                 current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
74                                                                                       *session,
75                                                                                       atv->route(), false);
76
77                                                 current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));                                          
78                                                 break;
79                                         }
80                                 }
81
82                         } else {
83
84                                 sort_track_selection ();
85
86                                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
87                                         AudioTimeAxisView* atv;
88
89                                         if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
90
91                                                 current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
92                                                                                       *session,
93                                                                                       atv->route(), false);
94                                                 current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));                                          
95                                                 break;
96                                         }
97                                 }
98
99                         }
100
101                         if (current_mixer_strip == 0) {
102                                 return;
103                         }               
104                 }
105                 
106                 if (current_mixer_strip->get_parent() == 0) {
107                         current_mixer_strip->set_embedded (true);
108                         current_mixer_strip->Hiding.connect (mem_fun(*this, &Editor::current_mixer_strip_hidden));
109                         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::current_mixer_strip_removed));
110                         current_mixer_strip->set_width (editor_mixer_strip_width);
111                         
112                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
113                         global_hpacker.reorder_child (*current_mixer_strip, 0);
114
115                         current_mixer_strip->show_all ();
116                 }
117
118         } else {
119
120                 if (current_mixer_strip) {
121                         editor_mixer_strip_width = current_mixer_strip->get_width ();
122                         if (current_mixer_strip->get_parent() != 0) {
123                                 global_hpacker.remove (*current_mixer_strip);
124                         }
125                 }
126         }
127 }
128
129 void
130 Editor::set_selected_mixer_strip (TimeAxisView& view)
131 {
132         AudioTimeAxisView* at;
133         bool show = false;
134
135         if (!session || (at = dynamic_cast<AudioTimeAxisView*>(&view)) == 0) {
136                 return;
137         }
138         
139         if (current_mixer_strip) {
140
141                 /* might be nothing to do */
142
143                 if (current_mixer_strip->route() == at->route()) {
144                         return;
145                 }
146
147                 if (current_mixer_strip->get_parent()) {
148                         show = true;
149                 }
150                 delete current_mixer_strip;
151                 current_mixer_strip = 0;
152         }
153
154         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
155                                               *session,
156                                               at->route());
157         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));
158         
159         if (show) {
160                 show_editor_mixer (true);
161         }
162 }
163
164 double current = 0.0;
165 bool currentInitialized = 0;
166
167 void
168 Editor::update_current_screen ()
169 {
170         if (session && session->engine().running()) {
171
172                 nframes_t frame;
173
174                 frame = session->audible_frame();
175
176                 if (_dragging_playhead) {
177                         goto almost_done;
178                 }
179
180                 /* only update if the playhead is on screen or we are following it */
181
182                 if (_follow_playhead) {
183
184                         playhead_cursor->canvas_item.show();
185
186                         if (frame != last_update_frame) {
187
188
189 #undef CONTINUOUS_SCROLL
190 #ifndef  CONTINUOUS_SCROLL
191                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
192                                         
193                                         if (session->transport_speed() < 0) {
194                                                 if (frame > (current_page_frames()/2)) {
195                                                         center_screen (frame-(current_page_frames()/2));
196                                                 } else {
197                                                         center_screen (current_page_frames()/2);
198                                                 }
199                                         } else {
200                                                 center_screen (frame+(current_page_frames()/2));
201                                         }
202                                 }
203
204                                 playhead_cursor->set_position (frame);
205
206 #else  // CONTINUOUS_SCROLL
207                                 
208                                 /* don't do continuous scroll till the new position is in the rightmost quarter of the 
209                                    editor canvas
210                                 */
211                                 
212                                 if (session->transport_speed()) {
213                                         double target = ((double)frame - (double)current_page_frames()/2.0) / frames_per_unit;
214                                         if (target <= 0.0) target = 0.0;
215                                         if ( fabs(target - current) < current_page_frames()/frames_per_unit ) {
216                                                 target = (target * 0.15) + (current * 0.85);
217                                         } else {
218                                                 /* relax */
219                                         }
220                                         //printf("frame: %d,  cpf: %d,  fpu: %6.6f, current: %6.6f, target : %6.6f\n", frame, current_page_frames(), frames_per_unit, current, target );
221                                         current = target;
222                                         horizontal_adjustment.set_value ( current );
223                                 }
224                                 
225                                 playhead_cursor->set_position (frame);
226
227 #endif // CONTINUOUS_SCROLL
228
229                         }
230
231                 } else {
232                         
233                         if (frame != last_update_frame) {
234                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
235                                         playhead_cursor->canvas_item.hide();
236                                 } else {
237                                         playhead_cursor->set_position (frame);
238                                 }
239                         }
240                 }
241
242           almost_done:
243                 last_update_frame = frame;
244
245                 if (current_mixer_strip) {
246                         current_mixer_strip->fast_update ();
247                 }
248                 
249         }
250 }
251
252 void
253 Editor::current_mixer_strip_removed ()
254 {
255         if (current_mixer_strip) {
256                 /* it is being deleted */
257                 current_mixer_strip = 0;
258         }
259 }
260
261 void
262 Editor::current_mixer_strip_hidden ()
263 {
264         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
265                 
266                 AudioTimeAxisView* tmp;
267                 
268                 if ((tmp = dynamic_cast<AudioTimeAxisView*>(*i)) != 0) {
269                         if (tmp->route() == current_mixer_strip->route()) {
270                                 (*i)->set_selected (false);
271                                 break;
272                         }
273                 }
274         }
275
276         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
277         if (act) {
278                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
279                 tact->set_active (false);
280         }
281 }
282
283 void
284 Editor::session_going_away ()
285 {
286         for (vector<sigc::connection>::iterator i = session_connections.begin(); i != session_connections.end(); ++i) {
287                 (*i).disconnect ();
288         }
289
290         stop_scrolling ();
291         selection->clear ();
292         cut_buffer->clear ();
293
294         clicked_regionview = 0;
295         clicked_axisview = 0;
296         clicked_routeview = 0;
297         clicked_crossfadeview = 0;
298         entered_regionview = 0;
299         entered_track = 0;
300         latest_regionview = 0;
301         last_update_frame = 0;
302         drag_info.item = 0;
303         last_canvas_frame = 0;
304
305         /* hide all tracks */
306
307         hide_all_tracks (false);
308
309         /* rip everything out of the list displays */
310
311         region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
312         route_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
313         named_selection_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
314         edit_group_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
315
316         region_list_model->clear ();
317         route_display_model->clear ();
318         named_selection_model->clear ();
319         group_model->clear ();
320
321         region_list_display.set_model (region_list_model);
322         route_list_display.set_model (route_display_model);
323         named_selection_display.set_model (named_selection_model);
324         edit_group_display.set_model (group_model);
325
326         edit_cursor_clock.set_session (0);
327         zoom_range_clock.set_session (0);
328         nudge_clock.set_session (0);
329
330         /* put editor/mixer toggle button in off position and disable until a new session is loaded */
331
332         editor_mixer_button.set_active(false);
333         editor_mixer_button.set_sensitive(false);
334         /* clear tempo/meter rulers */
335
336         remove_metric_marks ();
337         hide_measures ();
338         clear_marker_display ();
339
340         if (current_bbt_points) {
341                 delete current_bbt_points;
342                 current_bbt_points = 0;
343         }
344
345         /* mixer strip will be deleted all by itself 
346            when its route is deleted.
347         */
348
349         current_mixer_strip = 0;
350
351         WindowTitle title(Glib::get_application_name());
352         title += _("Editor");
353
354         set_title (title.get_string());
355
356         session = 0;
357 }