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