Separate route list code from Editor into its own object, EditorRouteList. Hopefully...
[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 #include "editor_route_list.h"
35
36 #include "i18n.h"
37
38 using namespace std;
39 using namespace Gtkmm2ext;
40 using namespace PBD;
41
42 void
43 Editor::editor_mixer_button_toggled ()
44 {
45         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
46         if (act) {
47                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
48                 show_editor_mixer (tact->get_active());
49         }
50 }
51
52 void
53 Editor::editor_list_button_toggled ()
54 {
55         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
56         if (act) {
57                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
58                 show_editor_list (tact->get_active());
59         }
60 }
61
62 void
63 Editor::cms_deleted ()
64 {
65         current_mixer_strip = 0;
66 }
67
68 void
69 Editor::show_editor_mixer (bool yn)
70 {
71         boost::shared_ptr<ARDOUR::Route> r;
72
73         show_editor_mixer_when_tracks_arrive = false;
74
75         if (!session) {
76                 show_editor_mixer_when_tracks_arrive = yn;
77                 return;
78         }
79
80         if (yn) {
81
82                 if (selection->tracks.empty()) {
83
84                         if (track_views.empty()) {      
85                                 show_editor_mixer_when_tracks_arrive = true;
86                                 return;
87                         } 
88
89                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
90                                 RouteTimeAxisView* atv;
91                                 
92                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
93                                         r = atv->route();
94                                         break;
95                                 }
96                         }
97
98                 } else {
99                         sort_track_selection ();
100                         
101                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
102                                 RouteTimeAxisView* atv;
103                                 
104                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
105                                         r = atv->route();
106                                         break;
107                                 }
108                         }
109                 }
110
111                 if (r) {
112                         bool created;
113
114                         if (current_mixer_strip == 0) {
115                                 create_editor_mixer ();
116                                 created = true;
117                         } else {
118                                 created = false;
119                         }
120
121                         current_mixer_strip->set_route (r);
122
123                         if (created) {
124                                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
125                         }
126                 }
127                 
128                 if (current_mixer_strip->get_parent() == 0) {
129                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
130                         global_hpacker.reorder_child (*current_mixer_strip, 0);
131                         current_mixer_strip->show_all ();
132                 }
133
134         } else {
135
136                 if (current_mixer_strip) {
137                         if (current_mixer_strip->get_parent() != 0) {
138                                 global_hpacker.remove (*current_mixer_strip);
139                         }
140                 }
141         }
142
143 #ifdef GTKOSX
144         /* XXX gtk problem here */
145         ensure_all_elements_drawn();
146 #endif
147 }
148
149 #ifdef GTKOSX
150 void
151 Editor::ensure_all_elements_drawn ()
152 {
153         controls_layout.queue_draw ();
154         ruler_label_event_box.queue_draw ();
155         time_button_event_box.queue_draw ();
156 }
157 #endif
158
159 void
160 Editor::show_editor_list (bool yn)
161 {
162         if (yn) {
163                 the_notebook.show();
164         } else {
165                 the_notebook.hide();
166         }
167 }
168
169 void
170 Editor::create_editor_mixer ()
171 {
172         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
173                                               *session,
174                                               false);
175         current_mixer_strip->Hiding.connect (mem_fun(*this, &Editor::current_mixer_strip_hidden));
176         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::current_mixer_strip_removed));
177 #ifdef GTKOSX
178         current_mixer_strip->WidthChanged.connect (mem_fun(*this, &Editor::ensure_all_elements_drawn));
179 #endif
180         current_mixer_strip->set_embedded (true);
181 }       
182
183 void
184 Editor::set_selected_mixer_strip (TimeAxisView& view)
185 {
186         RouteTimeAxisView* at;
187         bool created;
188
189         if (!session || (at = dynamic_cast<RouteTimeAxisView*>(&view)) == 0) {
190                 return;
191         }
192
193         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
194         
195         if (act) {
196                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
197                 if (!tact || !tact->get_active()) {
198                         /* not showing mixer strip presently */
199                         return;
200                 }
201         }
202
203         if (current_mixer_strip == 0) {
204                 create_editor_mixer ();
205                 created = true;
206         } else {
207                 created = false;
208         }
209
210         /* might be nothing to do */
211         
212         if (current_mixer_strip->route() == at->route()) {
213                 return;
214         }
215         
216         if (created) {
217                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
218         }
219
220         current_mixer_strip->set_route (at->route());
221 }
222
223 double current = 0.0;
224
225 void
226 Editor::update_current_screen ()
227 {
228         if (_pending_locate_request) {
229                 /* we don't update things when there's a pending locate request, otherwise
230                    when the editor requests a locate there is a chance that this method
231                    will move the playhead before the locate request is processed, causing
232                    a visual glitch. */
233                 return;
234         }
235         
236         if (session && session->engine().running()) {
237
238                 nframes64_t const frame = session->audible_frame();
239
240                 if (_dragging_playhead) {
241                         goto almost_done;
242                 }
243
244                 /* only update if the playhead is on screen or we are following it */
245
246                 if (_follow_playhead && session->requested_return_frame() < 0) {
247
248                         //playhead_cursor->canvas_item.show();
249
250                         if (frame != last_update_frame) {
251
252
253 #undef CONTINUOUS_SCROLL
254 #ifndef  CONTINUOUS_SCROLL
255                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
256                                         
257                                         if (session->transport_speed() < 0) {
258                                                 if (frame > (current_page_frames()/2)) {
259                                                         center_screen (frame-(current_page_frames()/2));
260                                                 } else {
261                                                         center_screen (current_page_frames()/2);
262                                                 }
263                                         } else {
264                                                 center_screen (frame+(current_page_frames()/2));
265                                         }
266                                 }
267
268                                 playhead_cursor->set_position (frame);
269
270 #else  // CONTINUOUS_SCROLL
271                                 
272                                 /* don't do continuous scroll till the new position is in the rightmost quarter of the 
273                                    editor canvas
274                                 */
275                                 
276                                 if (session->transport_speed()) {
277                                         double target = ((double)frame - (double)current_page_frames()/2.0) / frames_per_unit;
278                                         if (target <= 0.0) target = 0.0;
279                                         if ( fabs(target - current) < current_page_frames()/frames_per_unit ) {
280                                                 target = (target * 0.15) + (current * 0.85);
281                                         } else {
282                                                 /* relax */
283                                         }
284                                         //printf("frame: %d,  cpf: %d,  fpu: %6.6f, current: %6.6f, target : %6.6f\n", frame, current_page_frames(), frames_per_unit, current, target );
285                                         current = target;
286                                         horizontal_adjustment.set_value ( current );
287                                 }
288                                 
289                                 playhead_cursor->set_position (frame);
290
291 #endif // CONTINUOUS_SCROLL
292
293                         }
294
295                 } else {
296                         if (frame != last_update_frame) {
297                                 playhead_cursor->set_position (frame);
298                         }
299                 }
300
301           almost_done:
302                 last_update_frame = frame;
303                 if (current_mixer_strip) {
304                         current_mixer_strip->fast_update ();
305                 }
306                 
307         }
308 }
309
310 void
311 Editor::current_mixer_strip_removed ()
312 {
313         if (current_mixer_strip) {
314                 /* it is being deleted elsewhere */
315                 current_mixer_strip = 0;
316         }
317 }
318
319 void
320 Editor::current_mixer_strip_hidden ()
321 {
322         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
323                 
324                 RouteTimeAxisView* tmp;
325                 
326                 if ((tmp = dynamic_cast<RouteTimeAxisView*>(*i)) != 0) {
327                         if (tmp->route() == current_mixer_strip->route()) {
328                                 (*i)->set_selected (false);
329                                 break;
330                         }
331                 }
332         }
333
334         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
335         if (act) {
336                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
337                 tact->set_active (false);
338         }
339 }
340
341 void
342 Editor::session_going_away ()
343 {
344         _have_idled = false;
345         
346         for (vector<sigc::connection>::iterator i = session_connections.begin(); i != session_connections.end(); ++i) {
347                 (*i).disconnect ();
348         }
349
350         stop_scrolling ();
351         selection->clear ();
352         cut_buffer->clear ();
353
354         clicked_regionview = 0;
355         clicked_axisview = 0;
356         clicked_routeview = 0;
357         clicked_crossfadeview = 0;
358         entered_regionview = 0;
359         entered_track = 0;
360         last_update_frame = 0;
361         _drag = 0;
362
363         playhead_cursor->canvas_item.hide ();
364
365         /* hide all tracks */
366
367         _route_list->hide_all_tracks (false);
368
369         /* rip everything out of the list displays */
370
371         region_list_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
372         _route_list->clear ();
373         named_selection_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
374         route_group_display.set_model (Glib::RefPtr<Gtk::TreeStore>(0));
375
376         region_list_model->clear ();
377         named_selection_model->clear ();
378         group_model->clear ();
379
380         region_list_display.set_model (region_list_model);
381         named_selection_display.set_model (named_selection_model);
382         route_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_enum()));
425         }
426 }
427