OSC: Changed gainVCA to gainfader as VCA is already used.
[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 "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 }
77
78 void
79 Manager::remove (const ProxyBase* info)
80 {
81         for (Windows::iterator i = _windows.begin(); i != _windows.end(); ++i) {
82                 if ((*i) == info) {
83                         _windows.erase (i);
84                         return;
85                 }
86         }
87 }
88
89 void
90 Manager::toggle_window (ProxyBase* proxy)
91 {
92
93         Glib::RefPtr<Gtk::Action> act = ARDOUR_UI::instance()->global_actions.find_action (string_compose ("%1/%2", window_actions->get_name(), proxy->action_name()));
94         if (!act) {
95                 return;
96         }
97         Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (act);
98         if (!tact) {
99                 return;
100         }
101
102         if (tact->get_active()) {
103                 proxy->present ();
104         } else {
105                 proxy->hide ();
106         }
107 }
108
109 void
110 Manager::show_visible() const
111 {
112         for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
113                 if ((*i)->visible()) {
114                         if (! (*i)->get (true)) {
115                                 /* the window may be a plugin GUI for a plugin which
116                                  * is disabled or longer present.
117                                  */
118                                 continue;
119                         }
120                         (*i)->show_all ();
121                         (*i)->present ();
122                 }
123         }
124 }
125
126 void
127 Manager::add_state (XMLNode& root) const
128 {
129         for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
130                 /* don't save state for temporary proxy windows
131                  */
132
133                 if (dynamic_cast<ProxyTemporary*> (*i)) {
134                         continue;
135                 }
136
137                 root.add_child_nocopy ((*i)->get_state());
138         }
139 }
140
141 void
142 Manager::set_session (ARDOUR::Session* s)
143 {
144         SessionHandlePtr::set_session (s);
145         for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
146                 (*i)->set_session(s);
147         }
148 }
149
150 void
151 Manager::set_transient_for (Gtk::Window* parent)
152 {
153         /* OS X has a richer concept of window layering than X does (or
154          * certainly, than any accepted conventions on X), and so the use of
155          * Manager::set_transient_for() is not necessary on that platform.
156          *
157          * On OS X this is mostly taken care of by using the window type rather
158          * than explicit 1:1 transient-for relationships.
159          */
160
161 #ifndef __APPLE__
162         if (parent) {
163                 for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
164                         Gtk::Window* win = (*i)->get();
165                         if (win) {
166                                 win->set_transient_for (*parent);
167                         }
168                 }
169         } else {
170                 for (Windows::const_iterator i = _windows.begin(); i != _windows.end(); ++i) {
171                         Gtk::Window* win = (*i)->get();
172                         if (win) {
173                                 gtk_window_set_transient_for (win->gobj(), 0);
174                         }
175                 }
176         }
177
178         current_transient_parent = parent;
179 #endif
180 }
181
182 /*-------------------------*/
183
184 ProxyBase::ProxyBase (const std::string& name, const std::string& menu_name)
185         : WindowProxy (name, menu_name)
186 {
187 }
188
189 ProxyBase::ProxyBase (const std::string& name, const std::string& menu_name, const XMLNode& node)
190         : WindowProxy (name, menu_name, node)
191 {
192 }
193
194 void
195 ProxyBase::setup ()
196 {
197         WindowProxy::setup ();
198         set_session(_session);
199 }
200
201 /*-----------------------*/
202
203 ProxyTemporary::ProxyTemporary (const string& name, Gtk::Window* win)
204         : ProxyBase (name, string())
205 {
206         _window = win;
207 }
208
209 ProxyTemporary::~ProxyTemporary ()
210 {
211 }
212
213
214 ARDOUR::SessionHandlePtr*
215 ProxyTemporary::session_handle()
216 {
217         /* may return null */
218         ArdourWindow* aw = dynamic_cast<ArdourWindow*> (_window);
219         if (aw) { return aw; }
220         ArdourDialog* ad = dynamic_cast<ArdourDialog*> (_window);
221         if (ad) { return ad; }
222         return 0;
223 }