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