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