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.
23 #include "pbd/xml++.h"
25 #include "gtkmm2ext/utils.h"
27 #include "widgets/tearoff.h"
35 using namespace ArdourWidgets;
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)
49 own_window_height = 0;
53 tearoff_event_box.add (tearoff_arrow);
54 tearoff_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
55 tearoff_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::tearoff_click));
57 tearoff_event_box.set_tooltip_text (_("Click to tear this into its own window"));
59 close_event_box.add (close_arrow);
60 close_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
61 close_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::close_click));
63 close_event_box.set_tooltip_text (_("Click to put this back in the main window"));
66 box1 = manage (new VBox);
67 box1->pack_start (close_event_box, false, false, 2);
69 window_box.pack_end (*box1, false, false, 2);
71 own_window.add_events (KEY_PRESS_MASK|KEY_RELEASE_MASK|BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK|POINTER_MOTION_MASK|POINTER_MOTION_HINT_MASK);
72 own_window.set_resizable (allow_resize);
73 own_window.set_type_hint (WINDOW_TYPE_HINT_UTILITY);
75 own_window.add (window_box);
77 own_window.signal_button_press_event().connect (mem_fun (*this, &TearOff::window_button_press));
78 own_window.signal_button_release_event().connect (mem_fun (*this, &TearOff::window_button_release));
79 own_window.signal_motion_notify_event().connect (mem_fun (*this, &TearOff::window_motion));
80 own_window.signal_delete_event().connect (mem_fun (*this, &TearOff::window_delete_event));
81 own_window.signal_realize().connect (sigc::mem_fun (*this, &TearOff::own_window_realized));
82 own_window.signal_configure_event().connect (sigc::mem_fun (*this, &TearOff::own_window_configured), false);
84 tearoff_arrow.set_name ("TearOffArrow");
85 close_arrow.set_name ("TearOffArrow");
88 box2 = manage (new VBox);
89 box2->pack_start (tearoff_event_box, false, false);
91 pack_start (contents);
92 pack_start (*box2, false, false);
100 TearOff::set_can_be_torn_off (bool yn)
102 if (yn != _can_be_torn_off) {
104 tearoff_arrow.set_no_show_all (false);
105 tearoff_arrow.show ();
107 tearoff_arrow.set_no_show_all (true);
108 tearoff_arrow.hide ();
110 _can_be_torn_off = yn;
115 TearOff::set_visible (bool yn, bool force)
117 /* don't change visibility if torn off */
123 if (_visible != yn || force) {
136 TearOff::tearoff_click (GdkEventButton* /*ev*/)
143 TearOff::tear_it_off ()
145 if (!_can_be_torn_off) {
154 window_box.pack_start (contents);
155 own_window.set_name (get_name());
156 close_event_box.set_name (get_name());
157 if (own_window_width == 0) {
158 own_window.set_position (WIN_POS_MOUSE);
160 own_window.show_all ();
161 own_window.present ();
170 TearOff::close_click (GdkEventButton* /*ev*/)
177 TearOff::put_it_back ()
183 window_box.remove (contents);
184 pack_start (contents);
185 reorder_child (contents, 0);
195 TearOff::window_button_press (GdkEventButton* ev)
197 if (dragging || ev->button != 1) {
199 own_window.remove_modal_grab();
207 own_window.add_modal_grab();
213 TearOff::window_button_release (GdkEventButton* /*ev*/)
216 own_window.remove_modal_grab();
221 TearOff::window_delete_event (GdkEventAny* /*ev*/)
223 return close_click(0);
227 TearOff::window_motion (GdkEventMotion* ev)
234 RefPtr<Gdk::Window> win (own_window.get_window());
236 own_window.get_pointer (mx, my);
242 if (!(ev->state & GDK_BUTTON1_MASK)) {
244 own_window.remove_modal_grab();
248 x_delta = ev->x_root - drag_x;
249 y_delta = ev->y_root - drag_y;
251 win->get_root_origin (x, y);
252 win->move ((gint) floor (x + x_delta), (gint) floor (y + y_delta));
261 TearOff::torn_off() const
267 TearOff::add_state (XMLNode& node) const
269 node.set_property ("tornoff", _torn);
271 if (own_window_width > 0) {
272 node.set_property ("width", own_window_width);
273 node.set_property ("height", own_window_height);
274 node.set_property ("xpos", own_window_xpos);
275 node.set_property ("ypos", own_window_ypos);
280 TearOff::set_state (const XMLNode& node)
282 Glib::RefPtr<Gdk::Window> win;
285 if (!node.get_property (X_("tornoff"), tornoff)) {
295 node.get_property (X_("width"), own_window_width);
296 node.get_property (X_("height"), own_window_height);
297 node.get_property (X_("xpos"), own_window_xpos);
298 node.get_property (X_("ypos"), own_window_ypos);
300 if (own_window.is_realized ()) {
301 own_window.set_default_size (own_window_width, own_window_height);
302 own_window.move (own_window_xpos, own_window_ypos);
304 /* otherwise do it once the window is realized, see below */
308 TearOff::own_window_realized ()
310 own_window.get_window()->set_decorations (WMDecoration (DECOR_BORDER|DECOR_RESIZEH));
312 if (own_window_width > 0) {
313 own_window.set_default_size (own_window_width, own_window_height);
314 own_window.move (own_window_xpos, own_window_ypos);
319 TearOff::own_window_configured (GdkEventConfigure*)
321 Glib::RefPtr<const Gdk::Window> win;
323 win = own_window.get_window ();
326 win->get_size (own_window_width, own_window_height);
327 win->get_position (own_window_xpos, own_window_ypos);
334 TearOff::hide_visible ()