update drobilla's fascistic dir-locals.el to force emacs users into whitespace submis...
[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         /** @return true if the configuration for this window should be
55          *  global (ie across all sessions), otherwise false if it should
56          *  be session-specific.
57          */
58         virtual bool rc_configured () const = 0;
59
60         virtual Gtk::Window* get_gtk_window () const = 0;
61
62 private:
63         XMLNode* state_node (bool, int, int, int, int) const;
64         
65         std::string _name; ///< internal unique name for this window
66         bool _visible; ///< true if the window should be visible on startup
67         int _x_off; ///< x position
68         int _y_off; ///< y position 
69         int _width; ///< width
70         int _height; ///< height
71 };
72
73 /** Templated WindowProxy which contains a pointer to the window that is proxying for */
74 template <class T>
75 class WindowProxy : public WindowProxyBase
76 {
77 public:
78         WindowProxy (std::string const & name, XMLNode const * node)
79                 : WindowProxyBase (name, node)
80                 , _window (0)
81         {
82                 
83         }
84
85         Gtk::Window* get_gtk_window () const {
86                 return _window;
87         }
88
89         T* get () const {
90                 return _window;
91         }
92
93         /** Set the window and maybe set it up.  To be used after initial window creation */
94         void set (T* w, bool s = true) {
95                 _window = w;
96                 if (s) {
97                         setup ();
98                 }
99         }
100
101 private:
102         T* _window;
103 };
104
105 /** WindowProxy for windows that are created in response to a GTK Action being set active.
106  *  Templated on the type of the window.
107  */
108 template <class T>
109 class ActionWindowProxy : public WindowProxy<T>
110 {
111 public:
112         /** ActionWindowProxy constructor.
113          *  @param name Unique internal name for this window.
114          *  @param node <UI> node containing <Window> children, the appropriate one of which is used
115          *  to set up this object.
116          *  @param action Name of the ToggleAction that controls this window's visibility.
117          */
118         ActionWindowProxy (std::string const & name, XMLNode const * node, std::string const & action)
119                 : WindowProxy<T> (name, node)
120                 , _action (action)
121         {
122                 
123         }
124
125         void show () {
126                 /* Set the appropriate action active so that the window gets shown */
127                 Glib::RefPtr<Gtk::Action> act = ActionManager::get_action ("Common", _action.c_str());
128                 if (act) {
129                         Glib::RefPtr<Gtk::ToggleAction> tact = Glib::RefPtr<Gtk::ToggleAction>::cast_dynamic (act);
130                         assert (tact);
131                         tact->set_active (true);
132                 }
133         }
134
135         bool rc_configured () const {
136                 return true;
137         }
138
139 private:
140         std::string _action;
141 };
142
143 #endif