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