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