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