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