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