fix audio clock restore, provide XMLNode::property (string) and speed up the property...
[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     $Id$
19 */
20
21 #include <gtkmm2ext/utils.h>
22 #include <ardour/audioengine.h>
23
24 #include "editor.h"
25 #include "mixer_strip.h"
26 #include "ardour_ui.h"
27 #include "selection.h"
28 #include "audio_time_axis.h"
29 #include "actions.h"
30
31 #include "i18n.h"
32
33 void
34 Editor::editor_mixer_button_toggled ()
35 {
36         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
37         if (act) {
38                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
39                 show_editor_mixer (tact->get_active());
40         }
41 }
42
43 void
44 Editor::cms_deleted ()
45 {
46         current_mixer_strip = 0;
47 }
48
49 void
50 Editor::show_editor_mixer (bool yn)
51 {
52         show_editor_mixer_when_tracks_arrive = false;
53
54         if (yn) {
55
56                 if (current_mixer_strip == 0) {
57
58                         if (selection->tracks.empty()) {
59                                 
60                                 if (track_views.empty()) {      
61                                         show_editor_mixer_when_tracks_arrive = true;
62                                         return;
63                                 } 
64                                 
65                                 for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
66                                         AudioTimeAxisView* atv;
67
68                                         if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
69                                                 
70                                                 current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
71                                                                                       *session,
72                                                                                       atv->route(), false);
73
74                                                 current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));                                          
75                                                 break;
76                                         }
77                                 }
78
79                         } else {
80                                 for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
81                                         AudioTimeAxisView* atv;
82
83                                         if ((atv = dynamic_cast<AudioTimeAxisView*> (*i)) != 0) {
84
85                                                 current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
86                                                                                       *session,
87                                                                                       atv->route(), false);
88                                                 current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));                                          
89                                                 break;
90                                         }
91                                 }
92
93                         }
94
95                         if (current_mixer_strip == 0) {
96                                 return;
97                         }               
98                 }
99                 
100                 if (current_mixer_strip->get_parent() == 0) {
101                         current_mixer_strip->set_embedded (true);
102                         current_mixer_strip->Hiding.connect (mem_fun(*this, &Editor::current_mixer_strip_hidden));
103                         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::current_mixer_strip_removed));
104                         current_mixer_strip->set_width (editor_mixer_strip_width);
105                         
106                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
107                         global_hpacker.reorder_child (*current_mixer_strip, 0);
108
109                         current_mixer_strip->show_all ();
110                 }
111
112         } else {
113
114                 if (current_mixer_strip) {
115                         editor_mixer_strip_width = current_mixer_strip->get_width ();
116                         if (current_mixer_strip->get_parent() != 0) {
117                                 global_hpacker.remove (*current_mixer_strip);
118                         }
119                 }
120         }
121 }
122
123 void
124 Editor::set_selected_mixer_strip (TimeAxisView& view)
125 {
126         AudioTimeAxisView* at;
127         bool show = false;
128
129         if (!session || (at = dynamic_cast<AudioTimeAxisView*>(&view)) == 0) {
130                 return;
131         }
132         
133         if (current_mixer_strip) {
134
135                 /* might be nothing to do */
136
137                 if (current_mixer_strip->route() == at->route()) {
138                         return;
139                 }
140
141                 if (current_mixer_strip->get_parent()) {
142                         show = true;
143                 }
144                 delete current_mixer_strip;
145                 current_mixer_strip = 0;
146         }
147
148         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
149                                               *session,
150                                               at->route());
151         current_mixer_strip->GoingAway.connect (mem_fun(*this, &Editor::cms_deleted));
152         
153         if (show) {
154                 show_editor_mixer (true);
155         }
156 }
157
158 void
159 Editor::update_current_screen ()
160 {
161         if (session && engine.running()) {
162
163                 nframes_t frame;
164
165                 frame = session->audible_frame();
166
167                 if (_dragging_playhead) {
168                         goto almost_done;
169                 }
170
171                 /* only update if the playhead is on screen or we are following it */
172
173                 if (_follow_playhead) {
174
175                         playhead_cursor->canvas_item.show();
176
177                         if (frame != last_update_frame) {
178
179                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
180                                         
181                                         if (session->transport_speed() < 0) {
182                                                 if (frame > (current_page_frames()/2)) {
183                                                         center_screen (frame-(current_page_frames()/2));
184                                                 } else {
185                                                         center_screen (current_page_frames()/2);
186                                                 }
187                                         } else {
188                                                 center_screen (frame+(current_page_frames()/2));
189                                         }
190                                 }
191
192                                 playhead_cursor->set_position (frame);
193                         }
194
195                 } else {
196                         
197                         if (frame != last_update_frame) {
198                                 if (frame < leftmost_frame || frame > leftmost_frame + current_page_frames()) {
199                                         playhead_cursor->canvas_item.hide();
200                                 } else {
201                                         playhead_cursor->set_position (frame);
202                                 }
203                         }
204                 }
205
206           almost_done:
207                 last_update_frame = frame;
208
209                 if (current_mixer_strip) {
210                         current_mixer_strip->fast_update ();
211                 }
212                 
213         }
214 }
215
216 void
217 Editor::current_mixer_strip_removed ()
218 {
219         if (current_mixer_strip) {
220                 /* it is being deleted */
221                 current_mixer_strip = 0;
222         }
223 }
224
225 void
226 Editor::current_mixer_strip_hidden ()
227 {
228         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
229                 
230                 AudioTimeAxisView* tmp;
231                 
232                 if ((tmp = dynamic_cast<AudioTimeAxisView*>(*i)) != 0) {
233                         if (tmp->route() == current_mixer_strip->route()) {
234                                 (*i)->set_selected (false);
235                                 break;
236                         }
237                 }
238         }
239
240         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
241         if (act) {
242                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
243                 tact->set_active (false);
244         }
245 }
246
247 void
248 Editor::session_going_away ()
249 {
250         for (vector<sigc::connection>::iterator i = session_connections.begin(); i != session_connections.end(); ++i) {
251                 (*i).disconnect ();
252         }
253
254         stop_scrolling ();
255         selection->clear ();
256         cut_buffer->clear ();
257
258         clicked_regionview = 0;
259         clicked_trackview = 0;
260         clicked_audio_trackview = 0;
261         clicked_crossfadeview = 0;
262         entered_regionview = 0;
263         entered_track = 0;
264         latest_regionview = 0;
265         last_update_frame = 0;
266         drag_info.item = 0;
267         last_canvas_frame = 0;
268
269         /* hide all tracks */
270
271         hide_all_tracks (false);
272
273         /* rip everything out of the list displays */
274
275         region_list_clear (); // no clear() method in gtkmm 1.2 
276         route_display_model->clear ();
277         named_selection_model->clear ();
278         group_model->clear ();
279
280         edit_cursor_clock.set_session (0);
281         zoom_range_clock.set_session (0);
282         nudge_clock.set_session (0);
283
284         /* put editor/mixer toggle button in off position and disable until a new session is loaded */
285
286         editor_mixer_button.set_active(false);
287         editor_mixer_button.set_sensitive(false);
288         /* clear tempo/meter rulers */
289
290         remove_metric_marks ();
291         hide_measures ();
292         clear_marker_display ();
293
294         if (current_bbt_points) {
295                 delete current_bbt_points;
296                 current_bbt_points = 0;
297         }
298
299         /* mixer strip will be deleted all by itself 
300            when its route is deleted.
301         */
302
303         current_mixer_strip = 0;
304
305         set_title (_("ardour: editor"));
306
307         session = 0;
308 }