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