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