9769d616fe7e6692563c99f46975e735347b202f
[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 #include "ui_config.h"
46
47 #include "i18n.h"
48
49 using namespace std;
50 using namespace Gtkmm2ext;
51 using namespace PBD;
52
53 void
54 Editor::editor_mixer_button_toggled ()
55 {
56         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
57         if (act) {
58                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
59                 show_editor_mixer (tact->get_active());
60         }
61 }
62
63 void
64 Editor::editor_list_button_toggled ()
65 {
66         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-list"));
67         if (act) {
68                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
69                 show_editor_list (tact->get_active());
70         }
71 }
72
73 void
74 Editor::show_editor_mixer (bool yn)
75 {
76         boost::shared_ptr<ARDOUR::Route> r;
77
78         show_editor_mixer_when_tracks_arrive = false;
79
80         if (yn) {
81                 Glib::RefPtr<Gdk::Window> win = get_window ();
82                 Glib::RefPtr<Gdk::Screen> screen;
83
84                 if (win) {
85                          screen = win->get_screen();
86                 } else {
87                         screen = Gdk::Screen::get_default();
88                 }
89
90                 if (g_getenv ("ARDOUR_LOVES_STUPID_TINY_SCREENS") == 0 && screen && screen->get_height() < 700) {
91                         Gtk::MessageDialog msg (_("This screen is not tall enough to display the editor mixer"));
92                         msg.run ();
93                         return;
94                 }
95         }
96
97         if (!_session) {
98                 show_editor_mixer_when_tracks_arrive = yn;
99                 return;
100         }
101
102         if (yn) {
103
104                 if (selection->tracks.empty()) {
105
106                         if (track_views.empty()) {
107                                 show_editor_mixer_when_tracks_arrive = true;
108                                 return;
109                         }
110
111                         for (TrackViewList::iterator i = track_views.begin(); i != track_views.end(); ++i) {
112                                 RouteTimeAxisView* atv;
113
114                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
115                                         r = atv->route();
116                                         break;
117                                 }
118                         }
119
120                 } else {
121                         sort_track_selection (selection->tracks);
122
123                         for (TrackSelection::iterator i = selection->tracks.begin(); i != selection->tracks.end(); ++i) {
124                                 RouteTimeAxisView* atv;
125
126                                 if ((atv = dynamic_cast<RouteTimeAxisView*> (*i)) != 0) {
127                                         r = atv->route();
128                                         break;
129                                 }
130                         }
131                 }
132
133                 if (r) {
134                         if (current_mixer_strip == 0) {
135                                 create_editor_mixer ();
136                         }
137                 }
138
139                 if (current_mixer_strip && current_mixer_strip->get_parent() == 0) {
140                         global_hpacker.pack_start (*current_mixer_strip, Gtk::PACK_SHRINK );
141                         global_hpacker.reorder_child (*current_mixer_strip, 0);
142                         current_mixer_strip->show ();
143                 }
144
145                 if (r) {
146                         current_mixer_strip->set_route (r);
147                         current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
148                 }
149
150         } else {
151
152                 if (current_mixer_strip) {
153                         if (current_mixer_strip->get_parent() != 0) {
154                                 global_hpacker.remove (*current_mixer_strip);
155                         }
156                 }
157         }
158
159 #ifdef GTKOSX
160         /* XXX gtk problem here */
161         ensure_all_elements_drawn();
162 #endif
163 }
164
165 #ifdef GTKOSX
166 void
167 Editor::ensure_all_elements_drawn ()
168 {
169         controls_layout.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
190 void
191 Editor::set_selected_mixer_strip (TimeAxisView& view)
192 {
193         if (!_session) {
194                 return;
195         }
196
197         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
198
199         if (act) {
200                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
201                 if (!tact || !tact->get_active()) {
202                         /* not showing mixer strip presently */
203                         return;
204                 }
205         }
206
207         if (current_mixer_strip == 0) {
208                 create_editor_mixer ();
209         }
210
211
212         // if this is an automation track, then we shold the mixer strip should
213         // show the parent
214
215         boost::shared_ptr<ARDOUR::Route> route;
216         AutomationTimeAxisView* atv;
217
218         if ((atv = dynamic_cast<AutomationTimeAxisView*>(&view)) != 0) {
219
220                 AudioTimeAxisView *parent = dynamic_cast<AudioTimeAxisView*>(view.get_parent());
221
222                 if (parent) {
223                         route = parent->route ();
224                 }
225
226         } else {
227
228                 AudioTimeAxisView* at = dynamic_cast<AudioTimeAxisView*> (&view);
229
230                 if (at) {
231                         route = at->route();
232                 } else {
233                         MidiTimeAxisView* mt = dynamic_cast<MidiTimeAxisView*> (&view);
234                         if (mt) {
235                                 route = mt->route();
236                         }
237                 }
238         }
239
240         if (current_mixer_strip->route() == route) {
241                 return;
242         }
243
244         if (route) {
245                 current_mixer_strip->set_route (route);
246                 current_mixer_strip->set_width_enum (editor_mixer_strip_width, (void*) this);
247         }
248 }
249
250 void
251 Editor::current_mixer_strip_hidden ()
252 {
253         Glib::RefPtr<Gtk::Action> act = ActionManager::get_action (X_("Editor"), X_("show-editor-mixer"));
254         if (act) {
255                 Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic(act);
256                 tact->set_active (false);
257         }
258 }
259
260 void
261 Editor::maybe_add_mixer_strip_width (XMLNode& node)
262 {
263         if (current_mixer_strip) {
264                 node.add_property ("mixer-width", enum_2_string (editor_mixer_strip_width));
265         }
266 }
267
268 void
269 Editor::mixer_strip_width_changed ()
270 {
271 #ifdef GTKOSX
272         ensure_all_elements_drawn ();
273 #endif
274
275         editor_mixer_strip_width = current_mixer_strip->get_width_enum ();
276 }
277
278 void
279 Editor::track_mixer_selection ()
280 {
281         Mixer_UI::instance()->selection().RoutesChanged.connect (sigc::mem_fun (*this, &Editor::follow_mixer_selection));
282 }
283
284 void
285 Editor::follow_mixer_selection ()
286 {
287         if (_following_mixer_selection) {
288                 return;
289         }
290
291         _following_mixer_selection = true;
292         selection->block_tracks_changed (true);
293
294         RouteUISelection& s (Mixer_UI::instance()->selection().routes);
295
296         selection->clear_tracks ();
297
298         for (RouteUISelection::iterator i = s.begin(); i != s.end(); ++i) {
299                 TimeAxisView* tav = get_route_view_by_route_id ((*i)->route()->id());
300                 if (tav) {
301                         selection->add (tav);
302                 }
303         }
304
305         _following_mixer_selection = false;
306         selection->block_tracks_changed (false);
307         selection->TracksChanged (); /* EMIT SIGNAL */
308 }