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