c4762c6b968c9ed353544dd59dd72524ea79ceff
[ardour.git] / gtk2_ardour / ardour_ui_dependents.cc
1 /*
2     Copyright (C) 2000 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 /* this file exists solely to break compilation dependencies that
25    would connect changes to the mixer or editor objects.
26 */
27
28 #include <cstdio>
29
30 #include "pbd/error.h"
31
32 #include "ardour/session.h"
33
34 #include "actions.h"
35 #include "ardour_ui.h"
36 #include "public_editor.h"
37 #include "meterbridge.h"
38 #include "mixer_ui.h"
39 #include "keyboard.h"
40 #include "splash.h"
41 #include "route_params_ui.h"
42 #include "opts.h"
43 #include "utils.h"
44
45 #include "i18n.h"
46
47 using namespace Gtk;
48 using namespace PBD;
49
50 namespace ARDOUR {
51         class Session;
52         class Route;
53 }
54
55 using namespace ARDOUR;
56
57 void
58 ARDOUR_UI::we_have_dependents ()
59 {
60         install_actions ();
61         load_bindings ();
62         
63         ProcessorBox::register_actions ();
64
65         editor->setup_tooltips ();
66         editor->UpdateAllTransportClocks.connect (sigc::mem_fun (*this, &ARDOUR_UI::update_transport_clocks));
67
68         /* catch up on tabbable state */
69
70         tabbable_state_change (*editor);
71         tabbable_state_change (*mixer);
72         tabbable_state_change (*rc_option_editor);
73         
74         /* all actions are defined */
75
76         ActionManager::load_menus (ARDOUR_COMMAND_LINE::menus_file);
77
78         editor->track_mixer_selection ();
79         mixer->track_editor_selection ();
80
81         /* catch up on parameters */
82         
83         boost::function<void (std::string)> pc (boost::bind (&ARDOUR_UI::parameter_changed, this, _1));
84         Config->map_parameters (pc);
85
86         ARDOUR_UI_UTILS::reset_dpi ();
87 }
88
89 void
90 ARDOUR_UI::connect_dependents_to_session (ARDOUR::Session *s)
91 {
92         DisplaySuspender ds;
93         BootMessage (_("Setup Editor"));
94         editor->set_session (s);
95         BootMessage (_("Setup Mixer"));
96         mixer->set_session (s);
97         meterbridge->set_session (s);
98
99         /* its safe to do this now */
100
101         BootMessage (_("Reload Session History"));
102         s->restore_history ("");
103 }
104
105 /** The main editor window has been closed */
106 gint
107 ARDOUR_UI::exit_on_main_window_close (GdkEventAny * /*ev*/)
108 {
109 #ifdef TOP_MENUBAR
110         /* just hide the window, and return - the top menu stays up */
111         editor->hide ();
112         return TRUE;
113 #else
114         /* time to get out of here */
115         finish();
116         return TRUE;
117 #endif
118 }
119
120 GtkNotebook*
121 ARDOUR_UI::tab_window_root_drop (GtkNotebook* src,
122                                  GtkWidget* w,
123                                  gint x,
124                                  gint y,
125                                  gpointer)
126 {
127         using namespace std;
128         Gtk::Notebook* nb = 0;
129         Gtk::Window* win = 0;
130         Gtkmm2ext::Tabbable* tabbable = 0;
131
132
133         if (w == GTK_WIDGET(editor->contents().gobj())) {
134                 tabbable = editor;
135         } else if (w == GTK_WIDGET(mixer->contents().gobj())) {
136                 tabbable = mixer;
137         } else if (w == GTK_WIDGET(rc_option_editor->contents().gobj())) {
138                 tabbable = rc_option_editor;
139         } else {
140                 return 0;
141         }
142
143         nb = tabbable->tab_root_drop ();
144         win = tabbable->own_window ();
145
146         if (nb) {
147                 win->move (x, y);
148                 win->show_all ();
149                 win->present ();
150                 return nb->gobj();
151         }
152         
153         return 0; /* what was that? */
154 }
155
156 bool
157 ARDOUR_UI::idle_ask_about_quit ()
158 {
159         if (_session && _session->dirty()) {
160                 finish ();
161         } else {
162                 /* no session or session not dirty, but still ask anyway */
163
164                 Gtk::MessageDialog msg (string_compose ("Quit %1?", PROGRAM_NAME),
165                                         false, /* no markup */
166                                         Gtk::MESSAGE_INFO,
167                                         Gtk::BUTTONS_YES_NO,
168                                         true); /* modal */
169                 msg.set_default_response (Gtk::RESPONSE_YES);
170
171                 if (msg.run() == Gtk::RESPONSE_YES) {
172                         finish ();
173                 }
174         }
175
176         /* not reached but keep the compiler happy */
177
178         return false;
179 }
180
181 bool
182 ARDOUR_UI::main_window_delete_event (GdkEventAny* ev)
183 {
184         /* quit the application as soon as we go idle. If we call this here,
185          * the window manager/desktop can think we're taking too longer to
186          * handle the "delete" event
187          */
188         
189         Glib::signal_idle().connect (sigc::mem_fun (*this, &ARDOUR_UI::idle_ask_about_quit));   
190         
191         return true;
192 }
193
194 static GtkNotebook*
195 tab_window_root_drop (GtkNotebook* src,
196                       GtkWidget* w,
197                       gint x,
198                       gint y,
199                       gpointer user_data)
200 {
201         return ARDOUR_UI::instance()->tab_window_root_drop (src, w, x, y, user_data);
202 }
203
204 int
205 ARDOUR_UI::setup_windows ()
206 {
207         /* actions do not need to be defined when we load keybindings. They
208          * will be lazily discovered. But bindings do need to exist when we
209          * create windows/tabs with their own binding sets.
210          */
211
212         keyboard->setup_keybindings ();
213
214         /* we don't use a widget with its own window for the tab close button,
215            which makes it impossible to rely on GTK+ to generate signals for
216            events occuring "in" this widget. Instead, we pre-connect a
217            handler to the relevant events on the notebook and then check
218            to see if the event coordinates tell us that it occured "in"
219            the close button.
220         */
221         _tabs.signal_button_press_event().connect (sigc::mem_fun (*this, &ARDOUR_UI::tabs_button_event), false);
222         _tabs.signal_button_release_event().connect (sigc::mem_fun (*this, &ARDOUR_UI::tabs_button_event), false);
223
224         rc_option_editor = new RCOptionEditor;
225         rc_option_editor->StateChange.connect (sigc::mem_fun (*this, &ARDOUR_UI::tabbable_state_change));
226
227         if (create_editor ()) {
228                 error << _("UI: cannot setup editor") << endmsg;
229                 return -1;
230         }
231
232         if (create_mixer ()) {
233                 error << _("UI: cannot setup mixer") << endmsg;
234                 return -1;
235         }
236
237         if (create_meterbridge ()) {
238                 error << _("UI: cannot setup meterbridge") << endmsg;
239                 return -1;
240         }
241
242         /* order of addition affects order seen in initial window display */
243         
244         rc_option_editor->add_to_notebook (_tabs, _("Preferences"));
245         mixer->add_to_notebook (_tabs, _("Mixer"));
246         editor->add_to_notebook (_tabs, _("Editor"));
247
248         /* all other dialogs are created conditionally */
249
250         we_have_dependents ();
251
252 #ifdef TOP_MENUBAR
253         EventBox* status_bar_event_box = manage (new EventBox);
254
255         status_bar_event_box->add (status_bar_label);
256         status_bar_event_box->add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
257         status_bar_label.set_size_request (300, -1);
258
259         status_bar_label.show ();
260         status_bar_event_box->show ();
261
262         status_bar_event_box->signal_button_press_event().connect (mem_fun (*this, &ARDOUR_UI::status_bar_button_press));
263
264         status_bar_hpacker.pack_start (*status_bar_event_box, true, true, 6);
265         status_bar_hpacker.pack_start (menu_bar_base, false, false, 2);
266 #else
267         top_packer.pack_start (menu_bar_base, false, false);
268 #endif
269
270         main_vpacker.pack_start (top_packer, false, false);
271
272         /* now add the transport frame to the top of main window */
273         
274         main_vpacker.pack_start (transport_frame, false, false);
275         main_vpacker.pack_start (_tabs, true, true);
276
277 #ifdef TOP_MENUBAR
278         main_vpacker.pack_start (status_bar_hpacker, false, false);
279 #endif
280
281         setup_transport();
282         build_menu_bar ();
283         setup_tooltips ();
284
285         _main_window.signal_delete_event().connect (sigc::mem_fun (*this, &ARDOUR_UI::main_window_delete_event));
286         
287         /* pack the main vpacker into the main window and show everything
288          */
289
290         _main_window.add (main_vpacker);
291         transport_frame.show_all ();
292
293         const XMLNode* mnode = main_window_settings ();
294
295         if (mnode) {
296                 const XMLProperty* prop;
297                 gint x = -1;
298                 gint y = -1;
299                 gint w = -1;
300                 gint h = -1;
301
302                 if ((prop = mnode->property (X_("x"))) != 0) {
303                         x = atoi (prop->value());
304                 }
305
306                 if ((prop = mnode->property (X_("y"))) != 0) {
307                         y = atoi (prop->value());
308                 } 
309
310                 if ((prop = mnode->property (X_("w"))) != 0) {
311                         w = atoi (prop->value());
312                 } 
313                 
314                 if ((prop = mnode->property (X_("h"))) != 0) {
315                         h = atoi (prop->value());
316                 }
317
318                 if (x >= 0 && y >= 0 && w >= 0 && h >= 0) {
319                         _main_window.set_position (Gtk::WIN_POS_NONE);
320                 }
321                 
322                 if (x >= 0 && y >= 0) {
323                         _main_window.move (x, y);
324                 }
325                 
326                 if (w > 0 && h > 0) {
327                         _main_window.set_default_size (w, h);
328                 }
329
330                 std::string current_tab;
331                 
332                 if ((prop = mnode->property (X_("current-tab"))) != 0) {
333                         current_tab = prop->value();
334                 } else {
335                         current_tab = "editor";
336                 }
337                 if (mixer && current_tab == "mixer") {
338                         _tabs.set_current_page (_tabs.page_num (mixer->contents()));
339                 } else if (rc_option_editor && current_tab == "preferences") {
340                         _tabs.set_current_page (_tabs.page_num (rc_option_editor->contents()));
341                 } else if (editor) {
342                         _tabs.set_current_page (_tabs.page_num (editor->contents()));
343                 }
344         }
345         
346         _main_window.show_all ();
347         setup_toplevel_window (_main_window, "", this);
348         
349         _tabs.signal_switch_page().connect (sigc::mem_fun (*this, &ARDOUR_UI::tabs_switch));
350         _tabs.signal_page_removed().connect (sigc::mem_fun (*this, &ARDOUR_UI::tabs_page_removed));
351         _tabs.signal_page_added().connect (sigc::mem_fun (*this, &ARDOUR_UI::tabs_page_added));
352
353         /* It would be nice if Gtkmm had wrapped this rather than just
354          * deprecating the old set_window_creation_hook() method, but oh well...
355          */
356         g_signal_connect (_tabs.gobj(), "create-window", (GCallback) ::tab_window_root_drop, this);
357
358         return 0;
359 }