2 Copyright (C) 2015 Paul Davis
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.
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.
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.
20 #include <gtkmm/action.h>
21 #include <gtkmm/window.h>
23 #include "pbd/convert.h"
24 #include "pbd/xml++.h"
25 #include "pbd/stacktrace.h"
27 #include "gtkmm2ext/window_proxy.h"
28 #include "gtkmm2ext/visibility_tracker.h"
33 using namespace Gtkmm2ext;
36 WindowProxy::WindowProxy (const std::string& name)
48 WindowProxy::WindowProxy (const std::string& name, const std::string& menu_name)
50 , _menu_name (menu_name)
61 WindowProxy::WindowProxy (const std::string& name, const std::string& menu_name, const XMLNode& node)
63 , _menu_name (menu_name)
75 WindowProxy::~WindowProxy ()
82 WindowProxy::set_state (const XMLNode& node, int /* version */)
84 XMLNodeList children = node.children ();
86 XMLNodeList::const_iterator i = children.begin ();
88 while (i != children.end()) {
89 XMLProperty* prop = (*i)->property (X_("name"));
90 if ((*i)->name() == X_("Window") && prop && prop->value() == _name) {
97 if (i != children.end()) {
101 if ((prop = (*i)->property (X_("visible"))) != 0) {
102 _visible = PBD::string_is_affirmative (prop->value ());
105 if ((prop = (*i)->property (X_("x-off"))) != 0) {
106 _x_off = atoi (prop->value());
108 if ((prop = (*i)->property (X_("y-off"))) != 0) {
109 _y_off = atoi (prop->value());
111 if ((prop = (*i)->property (X_("x-size"))) != 0) {
112 _width = atoi (prop->value());
114 if ((prop = (*i)->property (X_("y-size"))) != 0) {
115 _height = atoi (prop->value());
127 WindowProxy::set_action (Glib::RefPtr<Gtk::Action> act)
133 WindowProxy::action_name() const
135 return string_compose (X_("toggle-%1"), _name);
139 WindowProxy::toggle()
145 /* XXX this is a hack - the window object should really
146 ensure its components are all visible. sigh.
149 /* we'd like to just call this and nothing else */
152 if (_window->is_mapped()) {
156 vistracker->cycle_visibility ();
158 if (_window->is_mapped()) {
159 if (_width != -1 && _height != -1) {
160 _window->set_default_size (_width, _height);
162 if (_x_off != -1 && _y_off != -1) {
163 _window->move (_x_off, _y_off);
170 WindowProxy::xml_node_name()
176 WindowProxy::get_state ()
178 XMLNode* node = new XMLNode (xml_node_name());
181 node->add_property (X_("name"), _name);
183 if (_window && vistracker) {
185 /* we have a window, so use current state */
187 _visible = vistracker->partially_visible ();
188 _window->get_position (_x_off, _y_off);
189 _window->get_size (_width, _height);
192 node->add_property (X_("visible"), _visible? X_("yes") : X_("no"));
193 snprintf (buf, sizeof (buf), "%d", _x_off);
194 node->add_property (X_("x-off"), buf);
195 snprintf (buf, sizeof (buf), "%d", _y_off);
196 node->add_property (X_("y-off"), buf);
197 snprintf (buf, sizeof (buf), "%d", _width);
198 node->add_property (X_("x-size"), buf);
199 snprintf (buf, sizeof (buf), "%d", _height);
200 node->add_property (X_("y-size"), buf);
206 WindowProxy::drop_window ()
218 WindowProxy::use_window (Gtk::Window& win)
226 WindowProxy::setup ()
230 vistracker = new Gtkmm2ext::VisibilityTracker (*_window);
231 _window->signal_delete_event().connect (sigc::mem_fun (*this, &WindowProxy::delete_event_handler));
237 WindowProxy::visible() const
240 /* update with current state */
241 _visible = vistracker->partially_visible();
247 WindowProxy::fully_visible () const
250 /* no vistracker .. no window .. cannot be fully visible */
253 return vistracker->fully_visible();
265 WindowProxy::maybe_show ()
273 WindowProxy::show_all ()
277 _window->show_all ();
281 WindowProxy::present ()
286 _window->show_all ();
289 /* turn off any mouse-based positioning */
290 _window->set_position (Gtk::WIN_POS_NONE);
303 WindowProxy::delete_event_handler (GdkEventAny* /*ev*/)
306 _action->activate ();
315 WindowProxy::save_pos_and_size ()
318 _window->get_position (_x_off, _y_off);
319 _window->get_size (_width, _height);
324 WindowProxy::set_pos_and_size ()
330 if (_width != -1 || _height != -1 || _x_off != -1 || _y_off != -1) {
331 /* cancel any mouse-based positioning */
332 _window->set_position (Gtk::WIN_POS_NONE);
335 if (_width != -1 && _height != -1) {
336 _window->resize (_width, _height);
339 if (_x_off != -1 && _y_off != -1) {
340 _window->move (_x_off, _y_off);
345 WindowProxy::set_pos ()
351 if (_width != -1 || _height != -1 || _x_off != -1 || _y_off != -1) {
352 /* cancel any mouse-based positioning */
353 _window->set_position (Gtk::WIN_POS_NONE);
356 if (_x_off != -1 && _y_off != -1) {
357 _window->move (_x_off, _y_off);