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