fix segv
[ardour.git] / libs / gtkmm2ext / tearoff.cc
1 /*
2     Copyright (C) 2003 Paul Barton-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     $Id$
19 */
20
21 #include <cmath>
22 #include <gtkmm2ext/tearoff.h>
23 #include <gtkmm2ext/utils.h>
24
25 using namespace Gtkmm2ext;
26 using namespace Gtk;
27 using namespace Gdk;
28 using namespace Glib;
29 using namespace std;
30
31 TearOff::TearOff (Widget& c, bool allow_resize)
32         : contents (c),
33           own_window (Gtk::WINDOW_TOPLEVEL),
34           tearoff_arrow (ARROW_DOWN, SHADOW_OUT),
35           close_arrow (ARROW_UP, SHADOW_OUT)
36 {
37         dragging = false;
38
39         tearoff_event_box.add (tearoff_arrow);
40         tearoff_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
41         tearoff_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::tearoff_click));
42
43         close_event_box.add (close_arrow);
44         close_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
45         close_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::close_click));
46         
47         own_window.add_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK|POINTER_MOTION_MASK|POINTER_MOTION_HINT_MASK);
48         own_window.set_resizable (allow_resize);
49         own_window.set_type_hint (WINDOW_TYPE_HINT_TOOLBAR);
50         
51         VBox* box1;
52         box1 = manage (new VBox);
53         box1->pack_start (close_event_box, false, false, 5);
54         
55         window_box.pack_end (*box1, false, false, 2);
56         own_window.add (window_box);
57         
58         own_window.signal_button_press_event().connect (mem_fun (*this, &TearOff::window_button_press));
59         own_window.signal_button_release_event().connect (mem_fun (*this, &TearOff::window_button_release));
60         own_window.signal_motion_notify_event().connect (mem_fun (*this, &TearOff::window_motion));
61         own_window.signal_delete_event().connect (mem_fun (*this, &TearOff::window_delete_event));
62         own_window.signal_realize().connect (bind (sigc::ptr_fun (Gtkmm2ext::set_decoration), &own_window, WMDecoration (DECOR_BORDER|DECOR_RESIZEH)));
63
64         tearoff_arrow.set_name ("TearOffArrow");
65         close_arrow.set_name ("TearOffArrow");
66
67         VBox* box2;
68         box2 = manage (new VBox);
69         box2->pack_start (tearoff_event_box, false, false, 5);
70
71         pack_start (contents);
72         pack_start (*box2, false, false, 2);
73
74 }
75
76 TearOff::~TearOff ()
77 {
78 }
79
80 gint
81 TearOff::tearoff_click (GdkEventButton* ev)
82 {
83         remove (contents);
84         window_box.pack_start (contents);
85         own_window.set_name (get_name());
86         close_event_box.set_name (get_name());
87         own_window.show_all ();
88 //      own_window.realize ();
89         hide ();
90         Detach ();
91         return TRUE;
92 }
93
94 gint
95 TearOff::close_click (GdkEventButton* ev)
96 {
97         window_box.remove (contents);
98         pack_start (contents);
99         reorder_child (contents, 0);
100         own_window.hide ();
101         show_all ();
102         Attach ();
103         return TRUE;
104 }               
105
106 gint
107 TearOff::window_button_press (GdkEventButton* ev)
108 {
109         dragging = true;
110         drag_x = ev->x_root;
111         drag_y = ev->y_root;
112
113         own_window.add_modal_grab();
114
115         return TRUE;
116 }
117
118 gint
119 TearOff::window_button_release (GdkEventButton* ev)
120 {
121         dragging = false;
122         own_window.remove_modal_grab();
123         return TRUE;
124 }
125
126 gint
127 TearOff::window_delete_event (GdkEventAny* ev)
128 {
129         return close_click(0);
130 }
131
132 gint
133 TearOff::window_motion (GdkEventMotion* ev)
134 {
135         gint x;
136         gint y;
137         gint mx, my;
138         double x_delta;
139         double y_delta;
140         RefPtr<Gdk::Window> win (own_window.get_window());
141         
142         own_window.get_pointer (mx, my);
143
144         if (!dragging) {
145                 return TRUE;
146         }
147
148         x_delta = ev->x_root - drag_x;
149         y_delta = ev->y_root - drag_y;
150
151         win->get_root_origin (x, y);
152         win->move ((gint) floor (x + x_delta), (gint) floor (y + y_delta));
153         
154         drag_x = ev->x_root;
155         drag_y = ev->y_root;
156         
157         return TRUE;
158 }
159
160 bool
161 TearOff::torn_off() const
162 {
163         return own_window.is_visible();
164 }