2 Copyright (C) 2003 Paul Barton-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.
24 #include "pbd/xml++.h"
26 #include "gtkmm2ext/tearoff.h"
27 #include "gtkmm2ext/utils.h"
31 using namespace Gtkmm2ext;
37 TearOff::TearOff (Widget& c, bool allow_resize)
39 own_window (Gtk::WINDOW_TOPLEVEL),
40 tearoff_arrow (ARROW_DOWN, SHADOW_OUT),
41 close_arrow (ARROW_UP, SHADOW_OUT)
45 _can_be_torn_off = true;
47 own_window_height = 0;
51 tearoff_event_box.add (tearoff_arrow);
52 tearoff_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
53 tearoff_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::tearoff_click));
55 close_event_box.add (close_arrow);
56 close_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
57 close_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::close_click));
59 own_window.add_events (KEY_PRESS_MASK|KEY_RELEASE_MASK|BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK|POINTER_MOTION_MASK|POINTER_MOTION_HINT_MASK);
60 own_window.set_resizable (allow_resize);
61 own_window.set_type_hint (WINDOW_TYPE_HINT_TOOLBAR);
62 own_window.signal_realize().connect (sigc::mem_fun (*this, &TearOff::own_window_realized));
63 own_window.signal_configure_event().connect (sigc::mem_fun (*this, &TearOff::own_window_configured), false);
66 box1 = manage (new VBox);
67 box1->pack_start (close_event_box, false, false, 2);
69 window_box.pack_end (*box1, false, false, 2);
70 own_window.add (window_box);
72 own_window.signal_button_press_event().connect (mem_fun (*this, &TearOff::window_button_press));
73 own_window.signal_button_release_event().connect (mem_fun (*this, &TearOff::window_button_release));
74 own_window.signal_motion_notify_event().connect (mem_fun (*this, &TearOff::window_motion));
75 own_window.signal_delete_event().connect (mem_fun (*this, &TearOff::window_delete_event));
76 own_window.signal_realize().connect (bind (sigc::ptr_fun (Gtkmm2ext::set_decoration), &own_window, WMDecoration (DECOR_BORDER|DECOR_RESIZEH)));
78 tearoff_arrow.set_name ("TearOffArrow");
79 close_arrow.set_name ("TearOffArrow");
82 box2 = manage (new VBox);
83 box2->pack_start (tearoff_event_box, false, false, 2);
85 pack_start (contents);
86 pack_start (*box2, false, false, 2);
94 TearOff::set_can_be_torn_off (bool yn)
96 if (yn != _can_be_torn_off) {
98 tearoff_arrow.set_no_show_all (false);
99 tearoff_arrow.show ();
101 tearoff_arrow.set_no_show_all (true);
102 tearoff_arrow.hide ();
104 _can_be_torn_off = yn;
109 TearOff::set_visible (bool yn)
111 /* don't change visibility if torn off */
113 if (own_window.is_visible()) {
117 if (_visible != yn) {
130 TearOff::tearoff_click (GdkEventButton* /*ev*/)
137 TearOff::tear_it_off ()
139 if (!_can_be_torn_off) {
148 window_box.pack_start (contents);
149 own_window.set_name (get_name());
150 close_event_box.set_name (get_name());
151 own_window.show_all ();
152 own_window.present ();
158 TearOff::close_click (GdkEventButton* /*ev*/)
165 TearOff::put_it_back ()
171 window_box.remove (contents);
172 pack_start (contents);
173 reorder_child (contents, 0);
180 TearOff::window_button_press (GdkEventButton* ev)
182 if (dragging || ev->button != 1) {
184 own_window.remove_modal_grab();
192 own_window.add_modal_grab();
198 TearOff::window_button_release (GdkEventButton* /*ev*/)
201 own_window.remove_modal_grab();
206 TearOff::window_delete_event (GdkEventAny* /*ev*/)
208 return close_click(0);
212 TearOff::window_motion (GdkEventMotion* ev)
219 RefPtr<Gdk::Window> win (own_window.get_window());
221 own_window.get_pointer (mx, my);
227 if (!(ev->state & GDK_BUTTON1_MASK)) {
229 own_window.remove_modal_grab();
233 x_delta = ev->x_root - drag_x;
234 y_delta = ev->y_root - drag_y;
236 win->get_root_origin (x, y);
237 win->move ((gint) floor (x + x_delta), (gint) floor (y + y_delta));
246 TearOff::torn_off() const
248 return own_window.is_visible();
252 TearOff::add_tornoff_state (XMLNode& node) const
254 node.add_property ("tornoff", (own_window.is_visible() ? "yes" : "no"));
256 if (own_window_width > 0) {
259 snprintf (buf, sizeof (buf), "%d", own_window_width);
260 node.add_property ("width", buf);
261 snprintf (buf, sizeof (buf), "%d", own_window_height);
262 node.add_property ("height", buf);
263 snprintf (buf, sizeof (buf), "%d", own_window_xpos);
264 node.add_property ("xpos", buf);
265 snprintf (buf, sizeof (buf), "%d", own_window_ypos);
266 node.add_property ("ypos", buf);
271 TearOff::set_tornoff_state (const XMLNode& node)
273 Glib::RefPtr<Gdk::Window> win;
274 const XMLProperty* prop;
276 if ((prop = node.property (X_("tornoff"))) == 0) {
280 if (prop->value() == "yes") {
286 if ((prop = node.property (X_("width"))) != 0) {
287 sscanf (prop->value().c_str(), "%d", &own_window_width);
289 if ((prop = node.property (X_("height"))) != 0) {
290 sscanf (prop->value().c_str(), "%d", &own_window_height);
292 if ((prop = node.property (X_("xpos"))) != 0) {
293 sscanf (prop->value().c_str(), "%d", &own_window_xpos);
295 if ((prop = node.property (X_("ypos"))) != 0) {
296 sscanf (prop->value().c_str(), "%d", &own_window_ypos);
299 own_window.set_default_size (own_window_width, own_window_height);
300 own_window.move (own_window_xpos, own_window_ypos);
304 TearOff::own_window_realized ()
306 if (own_window_width > 0) {
307 own_window.set_default_size (own_window_width, own_window_height);
308 own_window.move (own_window_xpos, own_window_ypos);
313 TearOff::own_window_configured (GdkEventConfigure*)
315 Glib::RefPtr<const Gdk::Window> win;
317 win = own_window.get_window ();
320 win->get_size (own_window_width, own_window_height);
321 win->get_position (own_window_xpos, own_window_ypos);