e2b015c27b22758593339b1f14456ca2274c8f2c
[ardour.git] / gtk2_ardour / window_proxy.h
1 /*
2     Copyright (C) 2010 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 __ardour_window_proxy_h__
21 #define __ardour_window_proxy_h__
22
23 #include <gtkmm/action.h>
24 #include <gtkmm/toggleaction.h>
25 #include "actions.h"
26
27 class XMLNode;
28
29 /** A class to proxy for a window that may not have been created yet.
30  *  It allows the management of visibility, position and size state
31  *  so that it can be saved and restored across session loads.
32  *
33  *  Subclasses of WindowProxy handle windows that are created in different
34  *  ways.
35  */
36  
37 class WindowProxyBase
38 {
39 public:
40         WindowProxyBase (std::string const &, XMLNode const *);
41         virtual ~WindowProxyBase () {}
42
43         std::string name () const {
44                 return _name;
45         }
46         
47         void maybe_show ();
48         XMLNode* get_state () const;
49         void setup ();
50
51         /** Show this window */
52         virtual void show () = 0;
53
54         virtual Gtk::Window* get_gtk_window () const = 0;
55
56 private:
57         XMLNode* state_node (bool, int, int, int, int) const;
58         
59         std::string _name; ///< internal unique name for this window
60         bool _visible; ///< true if the window should be visible on startup
61         int _x_off; ///< x position
62         int _y_off; ///< y position 
63         int _width; ///< width
64         int _height; ///< height
65 };
66
67 /** Templated WindowProxy which contains a pointer to the window that is proxying for */
68 template <class T>
69 class WindowProxy : public WindowProxyBase
70 {
71 public:
72         WindowProxy (std::string const & name, XMLNode const * node)
73                 : WindowProxyBase (name, node)
74                 , _window (0)
75         {
76                 
77         }
78
79         Gtk::Window* get_gtk_window () const {
80                 return _window;
81         }
82
83         T* get () const {
84                 return _window;
85         }
86
87         /** Set the window and set it up.  To be used after initial window creation */
88         void set (T* w) {
89                 _window = w;
90                 setup ();
91         }
92
93 private:
94         T* _window;
95 };
96
97 /** WindowProxy for windows that are created in response to a GTK Action being set active.
98  *  Templated on the type of the window.
99  */
100 template <class T>
101 class ActionWindowProxy : public WindowProxy<T>
102 {
103 public:
104         /** ActionWindowProxy constructor.
105          *  @param name Unique internal name for this window.
106          *  @param node <UI> node containing <Window> children, the appropriate one of which is used
107          *  to set up this object.
108          *  @param action Name of the ToggleAction that controls this window's visibility.
109          */
110         ActionWindowProxy (std::string const & name, XMLNode const * node, std::string const & action)
111                 : WindowProxy<T> (name, node)
112                 , _action (action)
113         {
114                 
115         }
116
117         void show () {
118                 /* Set the appropriate action active so that the window gets shown */
119                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action ("Common", _action.c_str());
120                 if (act) {
121                         Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (act);
122                         assert (tact);
123                         tact->set_active (true);
124                 }
125         }
126
127 private:
128         std::string _action;
129 };
130
131 #endif