0817a31128d498904bac25122055516305042492
[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 #ifdef WAF_BUILD
21 #include "gtk2ardour-config.h"
22 #endif
23
24 #include <glibmm/miscutils.h>
25 #include <gtkmm2ext/utils.h>
26 #include <gtkmm2ext/window_title.h>
27
28 #include "pbd/enumwriter.h"
29
30 #include "ardour/rc_configuration.h"
31
32 #include "actions.h"
33 #include "ardour_ui.h"
34 #include "audio_time_axis.h"
35 #include "automation_time_axis.h"
36 #include "editor.h"
37 #include "editor_routes.h"
38 #include "editor_route_groups.h"
39 #include "editor_regions.h"
40 #include "gui_thread.h"
41 #include "midi_time_axis.h"
42 #include "mixer_strip.h"
43 #include "mixer_ui.h"
44 #include "selection.h"
45
46 #include "i18n.h"
47
48 using namespace std;
49 using namespace Gtkmm2ext;
50 using namespace PBD;
51
52 void
53 Editor::editor_mixer_button_toggled ()
54 {
55         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
56         if (act) {
57                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
58                 show_editor_mixer (tact->get_active());
59         }
60 }
61
62 void
63 Editor::editor_list_button_toggled ()
64 {
65         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
66         if (act) {
67                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
68                 show_editor_list (tact->get_active());
69         }
70 }
71
72 void
73 Editor::show_editor_mixer (bool yn)
74 {
75         boost::shared_ptr<ARDOUR::Route> r;
76
77         show_editor_mixer_when_tracks_arrive = false;
78
79         if (!_session) {
80                 show_editor_mixer_when_tracks_arrive = yn;
81                 return;
82         }
83
84         if (yn) {
85
86                 if (selection->tracks.empty()) {
87
88                         if (track_views.empty()) {
89                                 show_editor_mixer_when_tracks_arrive = true;
90                                 return;
91                         }
92
93                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
94                                 RouteTimeAxisView* atv;
95
96                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
97                                         r = atv->route();
98                                         break;
99                                 }
100                         }
101
102                 } else {
103                         sort_track_selection ();
104
105                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
106                                 RouteTimeAxisView* atv;
107
108                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
109                                         r = atv->route();
110                                         break;
111                                 }
112                         }
113                 }
114
115                 if (r) {
116                         if (current_mixer_strip == 0) {
117                                 create_editor_mixer ();
118                         }
119
120                         current_mixer_strip->set_route (r);
121                         current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
122                 }
123
124                 if (current_mixer_strip->get_parent() == 0) {
125                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
126                         global_hpacker.reorder_child (*current_mixer_strip, 0);
127                         current_mixer_strip->show ();
128                 }
129
130         } else {
131
132                 if (current_mixer_strip) {
133                         if (current_mixer_strip->get_parent() != 0) {
134                                 global_hpacker.remove (*current_mixer_strip);
135                         }
136                 }
137         }
138
139 #ifdef GTKOSX
140         /* XXX gtk problem here */
141         ensure_all_elements_drawn();
142 #endif
143 }
144
145 #ifdef GTKOSX
146 void
147 Editor::ensure_all_elements_drawn ()
148 {
149         controls_layout.queue_draw ();
150         ruler_label_event_box.queue_draw ();
151         time_button_event_box.queue_draw ();
152 }
153 #endif
154
155 void
156 Editor::create_editor_mixer ()
157 {
158         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
159                                               _session,
160                                               false);
161         current_mixer_strip->Hiding.connect (sigc::mem_fun(*this, &Editor::current_mixer_strip_hidden));
162         current_mixer_strip->WidthChanged.connect (sigc::mem_fun (*this, &Editor::mixer_strip_width_changed));
163
164 #ifdef GTKOSX
165         current_mixer_strip->WidthChanged.connect (sigc::mem_fun(*this, &Editor::ensure_all_elements_drawn));
166 #endif
167         current_mixer_strip->set_embedded (true);
168 }
169
170 void
171 Editor::set_selected_mixer_strip (TimeAxisView& view)
172 {
173         if (!_session) {
174                 return;
175         }
176
177         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
178
179         if (act) {
180                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
181                 if (!tact || !tact->get_active()) {
182                         /* not showing mixer strip presently */
183                         return;
184                 }
185         }
186
187         if (current_mixer_strip == 0) {
188                 create_editor_mixer ();
189         }
190
191
192         // if this is an automation track, then we shold the mixer strip should
193         // show the parent
194
195         boost::shared_ptr<ARDOUR::Route> route;
196         AutomationTimeAxisView* atv;
197
198         if ((atv = dynamic_cast<AutomationTimeAxisView*>(&view)) != 0) {
199
200                 AudioTimeAxisView *parent = dynamic_cast<AudioTimeAxisView*>(view.get_parent());
201
202                 if (parent) {
203                         route = parent->route ();
204                 }
205
206         } else {
207
208                 AudioTimeAxisView* at = dynamic_cast<AudioTimeAxisView*> (&view);
209
210                 if (at) {
211                         route = at->route();
212                 } else {
213                         MidiTimeAxisView* mt = dynamic_cast<MidiTimeAxisView*> (&view);
214                         if (mt) {
215                                 route = mt->route();
216                         }
217                 }
218         }
219
220         if (current_mixer_strip->route() == route) {
221                 return;
222         }
223
224         if (route) {
225                 current_mixer_strip->set_route (route);
226                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
227         }
228 }
229
230 void
231 Editor::current_mixer_strip_hidden ()
232 {
233         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
234         if (act) {
235                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
236                 tact->set_active (false);
237         }
238 }
239
240 void
241 Editor::maybe_add_mixer_strip_width (XMLNode& node)
242 {
243         if (current_mixer_strip) {
244                 node.add_property ("mixer-width", enum_2_string (editor_mixer_strip_width));
245         }
246 }
247
248 void
249 Editor::mixer_strip_width_changed ()
250 {
251 #ifdef GTKOSX
252         ensure_all_elements_drawn ();
253 #endif
254
255         editor_mixer_strip_width = current_mixer_strip->get_width_enum ();
256 }
257
258 void
259 Editor::track_mixer_selection ()
260 {
261         Mixer_UI::instance()->selection().RoutesChanged.connect (sigc::mem_fun (*this, &Editor::follow_mixer_selection));
262 }
263
264 void
265 Editor::follow_mixer_selection ()
266 {
267         if (!ARDOUR::Config->get_link_editor_and_mixer_selection() || _following_mixer_selection) {
268                 return;
269         }
270
271         _following_mixer_selection = true;
272         selection->block_tracks_changed (true);
273
274         RouteUISelection& s (Mixer_UI::instance()->selection().routes);
275
276         selection->clear_tracks ();
277
278         for (RouteUISelection::iterator i = s.begin(); i != s.end(); ++i) {
279                 TimeAxisView* tav = get_route_view_by_route_id ((*i)->route()->id());
280                 if (tav) {
281                         selection->add (tav);
282                 }
283         }
284
285         _following_mixer_selection = false;
286         selection->block_tracks_changed (false);
287         selection->TracksChanged (); /* EMIT SIGNAL */
288 }