update drobilla's fascistic dir-locals.el to force emacs users into whitespace submis...
[ardour.git] / gtk2_ardour / window_proxy.cc
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 #include <gtkmm/window.h>
21 #include "window_proxy.h"
22
23 using namespace std;
24
25 /** WindowProxyBase constructor.
26  *  @param name Unique internal name for this window.
27  *  @param node <UI> node containing <Window> children, the appropriate one of which is used
28  *  to set up this object.
29  */
30 WindowProxyBase::WindowProxyBase (string const & name, XMLNode const * node)
31         : _name (name)
32         , _visible (false)
33         , _x_off (-1)
34         , _y_off (-1)
35         , _width (-1)
36         , _height (-1)
37 {
38         if (!node) {
39                 return;
40         }
41         
42         XMLNodeList children = node->children ();
43
44         XMLNodeList::const_iterator i = children.begin ();
45         while (i != children.end()) {
46                 XMLProperty* prop = (*i)->property (X_("name"));
47                 if ((*i)->name() == X_("Window") && prop && prop->value() == _name) {
48                         break;
49                 }
50                 
51                 ++i;
52         }
53
54         if (i != children.end()) {
55
56                 XMLProperty* prop;
57
58                 if ((prop = (*i)->property (X_("visible"))) != 0) {
59                         _visible = string_is_affirmative (prop->value ());
60                 }
61                 
62                 if ((prop = (*i)->property (X_("x-off"))) != 0) {
63                         _x_off = atoi (prop->value().c_str());
64                 }
65                 if ((prop = (*i)->property (X_("y-off"))) != 0) {
66                         _y_off = atoi (prop->value().c_str());
67                 }
68                 if ((prop = (*i)->property (X_("x-size"))) != 0) {
69                         _width = atoi (prop->value().c_str());
70                 }
71                 if ((prop = (*i)->property (X_("y-size"))) != 0) {
72                         _height = atoi (prop->value().c_str());
73                 }
74         }
75 }
76
77 /** Show this window if it was configured as visible.  This should
78  *  be called at session startup only.
79  */
80 void
81 WindowProxyBase::maybe_show ()
82 {
83         if (_visible) {
84                 show ();
85         }
86 }
87
88 /** Set up our window's position and size */
89 void
90 WindowProxyBase::setup ()
91 {
92         Gtk::Window* window = get_gtk_window ();
93         if (!window) {
94                 return;
95         }
96
97         if (_width != -1 && _height != -1) {
98                 window->set_default_size (_width, _height);
99         }
100
101         if (_x_off != -1 && _y_off != -1) {
102                 window->move (_x_off, _y_off);
103         }
104 }
105
106 XMLNode *
107 WindowProxyBase::get_state () const
108 {
109         bool v = _visible;
110         int x = _x_off;
111         int y = _y_off;
112         int w = _width;
113         int h = _height;
114
115         /* If the window has been created, get its current state; otherwise use
116            the state that we started off with.
117         */
118         
119         Gtk::Window* gtk_window = get_gtk_window ();
120         if (gtk_window) {
121                 v = gtk_window->is_visible ();
122
123                 Glib::RefPtr<Gdk::Window> gdk_window = gtk_window->get_window ();
124                 if (gdk_window) {
125                         gdk_window->get_position (x, y);
126                         gdk_window->get_size (w, h);
127                 }
128
129         }
130
131         return state_node (v, x, y, w, h);
132 }
133
134
135 XMLNode *
136 WindowProxyBase::state_node (bool v, int x, int y, int w, int h) const
137 {
138         XMLNode* node = new XMLNode (X_("Window"));
139         node->add_property (X_("name"), _name);
140         node->add_property (X_("visible"), v ? X_("yes") : X_("no"));
141
142         char buf[32];
143         snprintf (buf, sizeof (buf), "%d", x);
144         node->add_property (X_("x-off"), buf);
145         snprintf (buf, sizeof (buf), "%d", y);
146         node->add_property (X_("y-off"), buf);
147         snprintf (buf, sizeof (buf), "%d", w);
148         node->add_property (X_("x-size"), buf);
149         snprintf (buf, sizeof (buf), "%d", h);
150         node->add_property (X_("y-size"), buf);
151
152         return node;
153 }