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