Move our #inclusions for "lua_script_manager.h"
[ardour.git] / gtk2_ardour / window_manager.h
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
20 #ifndef __gtk2_ardour_window_manager_h__
21 #define __gtk2_ardour_window_manager_h__
22
23 #include <string>
24 #include <map>
25
26 #include <boost/function.hpp>
27 #include <glibmm/refptr.h>
28 #include <sigc++/trackable.h>
29
30 #include "lua_script_manager.h"
31
32 #include "gtkmm2ext/bindings.h"
33 #include "gtkmm2ext/window_proxy.h"
34
35 class XMLNode;
36
37 namespace Gtk {
38 class Window;
39 class Action;
40 }
41
42 namespace Gtkmm2ext {
43         class VisibilityTracker;
44 }
45
46 namespace ARDOUR {
47         class Session;
48         class SessionHandlePtr;
49 }
50
51 namespace WM {
52
53 class ProxyBase;
54
55 class Manager : public ARDOUR::SessionHandlePtr
56 {
57   public:
58         static Manager& instance();
59
60         void register_window (ProxyBase*);
61         void remove (const ProxyBase*);
62         void toggle_window (ProxyBase*);
63         void show_visible () const;
64         void set_session (ARDOUR::Session*);
65         void add_state (XMLNode&) const;
66
67         /* HACK HACK HACK */
68         void set_transient_for (Gtk::Window*);
69         Gtk::Window* transient_parent() const { return current_transient_parent; }
70
71                                         private:
72         typedef std::list<ProxyBase*> Windows;
73         Windows _windows;
74         Glib::RefPtr<Gtk::ActionGroup> window_actions;
75         Gtk::Window* current_transient_parent;
76
77         Manager();
78         ~Manager();
79
80         static Manager* _instance;
81 };
82
83 class ProxyBase : public ARDOUR::SessionHandlePtr, public Gtkmm2ext::WindowProxy
84 {
85   public:
86         ProxyBase (const std::string& name, const std::string& menu_name);
87         ProxyBase (const std::string& name, const std::string& menu_name, const XMLNode&);
88
89         virtual ARDOUR::SessionHandlePtr* session_handle () = 0;
90
91   protected:
92         void setup ();
93 };
94
95 class ProxyTemporary: public ProxyBase
96 {
97   public:
98         ProxyTemporary (const std::string& name, Gtk::Window* win);
99         ~ProxyTemporary();
100
101         Gtk::Window* get (bool create = false) {
102                 (void) create;
103                 return _window;
104         }
105
106         Gtk::Window* operator->() {
107                 return _window;
108         }
109
110         ARDOUR::SessionHandlePtr* session_handle ();
111 };
112
113 template<typename T>
114 class ProxyWithConstructor: public ProxyBase
115 {
116   public:
117         ProxyWithConstructor (const std::string& name, const std::string& menu_name, const boost::function<T*()>& c)
118                 : ProxyBase (name, menu_name) , creator (c) {}
119
120         ProxyWithConstructor (const std::string& name, const std::string& menu_name, const boost::function<T*()>& c, const XMLNode* node)
121                 : ProxyBase (name, menu_name, *node) , creator (c) {}
122
123         Gtk::Window* get (bool create = false) {
124                 if (!_window) {
125                         if (!create) {
126                                 return 0;
127                         }
128
129                         _window = dynamic_cast<Gtk::Window*> (creator ());
130
131                         if (_window) {
132                                 setup ();
133                         }
134                 }
135
136                 return _window;
137         }
138
139         T* operator->() {
140                 return dynamic_cast<T*> (get (true));
141         }
142
143         ARDOUR::SessionHandlePtr* session_handle () {
144                 /* may return null */
145                 return dynamic_cast<T*> (_window);
146         }
147
148         void set_session(ARDOUR::Session *s) {
149                 SessionHandlePtr::set_session (s);
150                 ARDOUR::SessionHandlePtr* sp = session_handle ();
151                 if (sp) {
152                         sp->set_session (s);
153                         dynamic_cast<T*>(_window)->set_session(s);
154                 }
155         }
156
157                                         private:
158         boost::function<T*()>   creator;
159 };
160
161 template<typename T>
162 class Proxy : public ProxyBase
163 {
164   public:
165         Proxy (const std::string& name, const std::string& menu_name)
166                 : ProxyBase (name, menu_name) {}
167
168         Proxy (const std::string& name, const std::string& menu_name, const XMLNode* node)
169                 : ProxyBase (name, menu_name, *node)  {}
170
171         Gtk::Window* get (bool create = false) {
172                 if (!_window) {
173                         if (!create) {
174                                 return 0;
175                         }
176
177                         _window = new T ();
178
179                         if (_window) {
180                                 setup ();
181                         }
182                 }
183
184                 return _window;
185         }
186
187         T* operator->() {
188                 return dynamic_cast<T*> (get(true));
189         }
190
191         ARDOUR::SessionHandlePtr* session_handle () {
192                 /* may return null */
193                 return dynamic_cast<T*> (_window);
194         }
195
196         void set_session(ARDOUR::Session *s) {
197                 SessionHandlePtr::set_session (s);
198                 ARDOUR::SessionHandlePtr* sp = session_handle ();
199                 if (sp) {
200                         sp->set_session (s);
201                         dynamic_cast<T*>(_window)->set_session(s);
202                 }
203         }
204
205   private:
206         boost::function<T*()>   creator;
207 };
208
209 } /* namespace */
210
211 #endif /* __gtk2_ardour_window_manager_h__ */