Merge branch 'master' into cairocanvas
[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 <gtkmm/messagedialog.h>
26 #include <gtkmm2ext/utils.h>
27 #include <gtkmm2ext/window_title.h>
28
29 #include "pbd/enumwriter.h"
30
31 #include "ardour/rc_configuration.h"
32
33 #include "actions.h"
34 #include "ardour_ui.h"
35 #include "audio_time_axis.h"
36 #include "automation_time_axis.h"
37 #include "editor.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 (yn) {
80                 Glib::RefPtr<Gdk::Window> win = get_window ();
81                 Glib::RefPtr<Gdk::Screen> screen;
82                 
83                 if (win) {
84                          screen = win->get_screen();
85                 } else {
86                         screen = Gdk::Screen::get_default();
87                 }
88
89                 if (g_getenv ("ARDOUR_LOVES_STUPID_TINY_SCREENS") == 0 && screen && screen->get_height() < 700) {
90                         Gtk::MessageDialog msg (_("This screen is not tall enough to display the editor mixer"));
91                         msg.run ();
92                         return;
93                 }
94         }
95
96         if (!_session) {
97                 show_editor_mixer_when_tracks_arrive = yn;
98                 return;
99         }
100
101         if (yn) {
102
103                 if (selection->tracks.empty()) {
104
105                         if (track_views.empty()) {
106                                 show_editor_mixer_when_tracks_arrive = true;
107                                 return;
108                         }
109
110                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
111                                 RouteTimeAxisView* atv;
112
113                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
114                                         r = atv->route();
115                                         break;
116                                 }
117                         }
118
119                 } else {
120                         sort_track_selection (selection->tracks);
121
122                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
123                                 RouteTimeAxisView* atv;
124
125                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
126                                         r = atv->route();
127                                         break;
128                                 }
129                         }
130                 }
131
132                 if (r) {
133                         if (current_mixer_strip == 0) {
134                                 create_editor_mixer ();
135                         }
136                 }
137
138                 if (current_mixer_strip && current_mixer_strip->get_parent() == 0) {
139                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
140                         global_hpacker.reorder_child (*current_mixer_strip, 0);
141                         current_mixer_strip->show ();
142                 }
143
144                 if (r) {
145                         current_mixer_strip->set_route (r);
146                         current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
147                 }
148
149         } else {
150
151                 if (current_mixer_strip) {
152                         if (current_mixer_strip->get_parent() != 0) {
153                                 global_hpacker.remove (*current_mixer_strip);
154                         }
155                 }
156         }
157
158 #ifdef GTKOSX
159         /* XXX gtk problem here */
160         ensure_all_elements_drawn();
161 #endif
162 }
163
164 #ifdef GTKOSX
165 void
166 Editor::ensure_all_elements_drawn ()
167 {
168         controls_layout.queue_draw ();
169         ruler_label_event_box.queue_draw ();
170         time_bars_event_box.queue_draw ();
171 }
172 #endif
173
174 void
175 Editor::create_editor_mixer ()
176 {
177         current_mixer_strip = new MixerStrip (*ARDOUR_UI::instance()->the_mixer(),
178                                               _session,
179                                               false);
180         current_mixer_strip->Hiding.connect (sigc::mem_fun(*this, &Editor::current_mixer_strip_hidden));
181         current_mixer_strip->WidthChanged.connect (sigc::mem_fun (*this, &Editor::mixer_strip_width_changed));
182
183 #ifdef GTKOSX
184         current_mixer_strip->WidthChanged.connect (sigc::mem_fun(*this, &Editor::ensure_all_elements_drawn));
185 #endif
186         current_mixer_strip->set_embedded (true);
187 }
188
189 void
190 Editor::set_selected_mixer_strip (TimeAxisView& view)
191 {
192         if (!_session) {
193                 return;
194         }
195
196         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
197
198         if (act) {
199                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
200                 if (!tact || !tact->get_active()) {
201                         /* not showing mixer strip presently */
202                         return;
203                 }
204         }
205
206         if (current_mixer_strip == 0) {
207                 create_editor_mixer ();
208         }
209
210
211         // if this is an automation track, then we shold the mixer strip should
212         // show the parent
213
214         boost::shared_ptr<ARDOUR::Route> route;
215         AutomationTimeAxisView* atv;
216
217         if ((atv = dynamic_cast<AutomationTimeAxisView*>(&view)) != 0) {
218
219                 AudioTimeAxisView *parent = dynamic_cast<AudioTimeAxisView*>(view.get_parent());
220
221                 if (parent) {
222                         route = parent->route ();
223                 }
224
225         } else {
226
227                 AudioTimeAxisView* at = dynamic_cast<AudioTimeAxisView*> (&view);
228
229                 if (at) {
230                         route = at->route();
231                 } else {
232                         MidiTimeAxisView* mt = dynamic_cast<MidiTimeAxisView*> (&view);
233                         if (mt) {
234                                 route = mt->route();
235                         }
236                 }
237         }
238
239         if (current_mixer_strip->route() == route) {
240                 return;
241         }
242
243         if (route) {
244                 current_mixer_strip->set_route (route);
245                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
246         }
247 }
248
249 void
250 Editor::current_mixer_strip_hidden ()
251 {
252         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
253         if (act) {
254                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
255                 tact->set_active (false);
256         }
257 }
258
259 void
260 Editor::maybe_add_mixer_strip_width (XMLNode& node)
261 {
262         if (current_mixer_strip) {
263                 node.add_property ("mixer-width", enum_2_string (editor_mixer_strip_width));
264         }
265 }
266
267 void
268 Editor::mixer_strip_width_changed ()
269 {
270 #ifdef GTKOSX
271         ensure_all_elements_drawn ();
272 #endif
273
274         editor_mixer_strip_width = current_mixer_strip->get_width_enum ();
275 }
276
277 void
278 Editor::track_mixer_selection ()
279 {
280         Mixer_UI::instance()->selection().RoutesChanged.connect (sigc::mem_fun (*this, &Editor::follow_mixer_selection));
281 }
282
283 void
284 Editor::follow_mixer_selection ()
285 {
286         if (!ARDOUR::Config->get_link_editor_and_mixer_selection() || _following_mixer_selection) {
287                 return;
288         }
289
290         _following_mixer_selection = true;
291         selection->block_tracks_changed (true);
292
293         RouteUISelection& s (Mixer_UI::instance()->selection().routes);
294
295         selection->clear_tracks ();
296
297         for (RouteUISelection::iterator i = s.begin(); i != s.end(); ++i) {
298                 TimeAxisView* tav = get_route_view_by_route_id ((*i)->route()->id());
299                 if (tav) {
300                         selection->add (tav);
301                 }
302         }
303
304         _following_mixer_selection = false;
305         selection->block_tracks_changed (false);
306         selection->TracksChanged (); /* EMIT SIGNAL */
307 }