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