enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / gtk2_ardour / window_manager.cc
1 /*
2     Copyright (C) 2013 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 #include <gtkmm/window.h>
20
21 #include "pbd/xml++.h"
22
23 #include "ardour/session_handle.h"
24
25 #include "gtkmm2ext/bindings.h"
26 #include "gtkmm2ext/visibility_tracker.h"
27
28 #include "actions.h"
29 #include "ardour_dialog.h"
30 #include "ardour_ui.h"
31 #include "ardour_window.h"
32 #include "window_manager.h"
33 #include "processor_box.h"
34
35 #include "pbd/i18n.h"
36
37 using std::string;
38 using namespace WM;
39 using namespace PBD;
40
41 Manager* Manager::_instance = 0;
42
43 Manager&
44 Manager::instance ()
45 {
46         if (!_instance) {
47                 _instance = new Manager;
48         }
49         return *_instance;
50 }
51
52 Manager::Manager ()
53         : current_transient_parent (0)
54 {
55 }
56
57 Manager::~Manager ()
58 {
59 }
60
61 void
62 Manager::register_window (ProxyBase* info)
63 {
64         _windows.push_back (info);
65
66         if (!info->menu_name().empty()) {
67
68                 if (!window_actions) {
69                         window_actions = ARDOUR_UI::instance()->global_actions.create_action_group (X_("Window"));
70                 }
71
72                 info->set_action (ARDOUR_UI::instance()->global_actions.register_toggle_action (window_actions,
73                          info->action_name().c_str(), info->menu_name().c_str(),
74                          sigc::bind (sigc::mem_fun (*this, &Manager::toggle_window), info)));
75
76                 info->signal_map.connect (sigc::bind (sigc::mem_fun (*this, &Manager::window_proxy_was_mapped), info));
77                 info->signal_unmap.connect (sigc::bind (sigc::mem_fun (*this, &Manager::window_proxy_was_unmapped), info));
78
79         }
80 }
81
82 void
83 Manager::window_proxy_was_mapped (ProxyBase* proxy)
84 {
85         Glib::RefPtr<Gtk::Action> act = ARDOUR_UI::instance()->global_actions.find_action (string_compose ("%1/%2", window_actions->get_name(), proxy->action_name()));
86         if (!act) {
87                 return;
88         }
89         Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (act);
90         if (!tact) {
91                 return;
92         }
93
94         tact->set_active (true);
95 }
96
97 void
98 Manager::window_proxy_was_unmapped (ProxyBase* proxy)
99 {
100         Glib::RefPtr<Gtk::Action> act = ARDOUR_UI::instance()->global_actions.find_action (string_compose ("%1/%2", window_actions->get_name(), proxy->action_name()));
101         if (!act) {
102                 return;
103         }
104         Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (act);
105         if (!tact) {
106                 return;
107         }
108
109         tact->set_active (false);
110 }
111
112 void
113 Manager::remove (const ProxyBase* info)
114 {
115         for (Windows::iterator i = _windows.begin(); i != _windows.end(); ++i) {
116                 if ((*i) == info) {
117                         _windows.erase (i);
118                         return;
119                 }
120         }
121 }
122
123 void
124 Manager::toggle_window (ProxyBase* proxy)
125 {
126         Glib::RefPtr<Gtk::Action> act = ARDOUR_UI::instance()->global_actions.find_action (string_compose ("%1/%2", window_actions->get_name(), proxy->action_name()));
127         if (!act) {
128                 return;
129         }
130         Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (act);
131         if (!tact) {
132                 return;
133         }
134
135         if (tact->get_active()) {
136                 proxy->present ();
137         } else {
138                 proxy->hide ();
139         }
140 }
141
142 void
143 Manager::show_visible() const
144 {
145         for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
146                 if ((*i)->visible()) {
147                         Gtk::Window* win = (*i)->get (true);
148                         if (!win) {
149                                 /* the window may be a plugin GUI for a plugin which
150                                  * is disabled or longer present.
151                                  */
152                                 continue;
153                         }
154                         if (dynamic_cast<ArdourDialog*> (win)) {
155                                 /* do not show dialogs at startup. Most
156                                  * dialogs require some signal connection work
157                                  * because we are trying to avoid recursive
158                                  * event loops (connecting instead to
159                                  * ::signal_response(). This means we need to
160                                  * destroy the window as well, so that the code
161                                  * which checks if it should be created will
162                                  * find that it is missing and will create it
163                                  * and connect to any necessary signals.
164                                  */
165                                 (*i)->drop_window ();
166                                 continue;
167                         }
168                         (*i)->show_all ();
169                         (*i)->present ();
170                 }
171         }
172 }
173
174 void
175 Manager::add_state (XMLNode& root) const
176 {
177         for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
178                 /* don't save state for temporary proxy windows
179                  */
180
181                 if (dynamic_cast<ProxyTemporary*> (*i)) {
182                         continue;
183                 }
184
185                 root.add_child_nocopy ((*i)->get_state());
186         }
187 }
188
189 void
190 Manager::set_session (ARDOUR::Session* s)
191 {
192         SessionHandlePtr::set_session (s);
193         for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
194                 (*i)->set_session(s);
195         }
196 }
197
198 void
199 Manager::set_transient_for (Gtk::Window* parent)
200 {
201         /* OS X has a richer concept of window layering than X does (or
202          * certainly, than any accepted conventions on X), and so the use of
203          * Manager::set_transient_for() is not necessary on that platform.
204          *
205          * On OS X this is mostly taken care of by using the window type rather
206          * than explicit 1:1 transient-for relationships.
207          */
208
209 #ifndef __APPLE__
210         if (parent) {
211                 for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
212                         Gtk::Window* win = (*i)->get();
213                         if (win) {
214                                 win->set_transient_for (*parent);
215                         }
216                 }
217         } else {
218                 for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
219                         Gtk::Window* win = (*i)->get();
220                         if (win) {
221                                 gtk_window_set_transient_for (win->gobj(), 0);
222                         }
223                 }
224         }
225
226         current_transient_parent = parent;
227 #endif
228 }
229
230 /*-------------------------*/
231
232 ProxyBase::ProxyBase (const std::string& name, const std::string& menu_name)
233         : WindowProxy (name, menu_name)
234 {
235 }
236
237 ProxyBase::ProxyBase (const std::string& name, const std::string& menu_name, const XMLNode& node)
238         : WindowProxy (name, menu_name, node)
239 {
240 }
241
242 void
243 ProxyBase::setup ()
244 {
245         WindowProxy::setup ();
246         set_session(_session);
247
248 }
249
250 /*-----------------------*/
251
252 ProxyTemporary::ProxyTemporary (const string& name, Gtk::Window* win)
253         : ProxyBase (name, string())
254 {
255         _window = win;
256 }
257
258 ProxyTemporary::~ProxyTemporary ()
259 {
260 }
261
262
263 ARDOUR::SessionHandlePtr*
264 ProxyTemporary::session_handle()
265 {
266         /* may return null */
267         ArdourWindow* aw = dynamic_cast<ArdourWindow*> (_window);
268         if (aw) { return aw; }
269         ArdourDialog* ad = dynamic_cast<ArdourDialog*> (_window);
270         if (ad) { return ad; }
271         return 0;
272 }