most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[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         _visible = true;
39         _can_be_torn_off = true;
40
41         tearoff_event_box.add (tearoff_arrow);
42         tearoff_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
43         tearoff_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::tearoff_click));
44
45         close_event_box.add (close_arrow);
46         close_event_box.set_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK);
47         close_event_box.signal_button_release_event().connect (mem_fun (*this, &TearOff::close_click));
48         
49         own_window.add_events (BUTTON_PRESS_MASK|BUTTON_RELEASE_MASK|POINTER_MOTION_MASK|POINTER_MOTION_HINT_MASK);
50         own_window.set_resizable (allow_resize);
51         own_window.set_type_hint (WINDOW_TYPE_HINT_TOOLBAR);
52         
53         VBox* box1;
54         box1 = manage (new VBox);
55         box1->pack_start (close_event_box, false, false, 2);
56         
57         window_box.pack_end (*box1, false, false, 2);
58         own_window.add (window_box);
59         
60         own_window.signal_button_press_event().connect (mem_fun (*this, &TearOff::window_button_press));
61         own_window.signal_button_release_event().connect (mem_fun (*this, &TearOff::window_button_release));
62         own_window.signal_motion_notify_event().connect (mem_fun (*this, &TearOff::window_motion));
63         own_window.signal_delete_event().connect (mem_fun (*this, &TearOff::window_delete_event));
64         own_window.signal_realize().connect (bind (sigc::ptr_fun (Gtkmm2ext::set_decoration), &own_window, WMDecoration (DECOR_BORDER|DECOR_RESIZEH)));
65
66         tearoff_arrow.set_name ("TearOffArrow");
67         close_arrow.set_name ("TearOffArrow");
68
69         VBox* box2;
70         box2 = manage (new VBox);
71         box2->pack_start (tearoff_event_box, false, false, 2);
72
73         pack_start (contents);
74         pack_start (*box2, false, false, 2);
75
76 }
77
78 TearOff::~TearOff ()
79 {
80 }
81
82 void
83 TearOff::set_can_be_torn_off (bool yn)
84 {
85         if (yn != _can_be_torn_off) {
86                 if (yn) {
87                         tearoff_arrow.set_no_show_all (false);
88                         tearoff_arrow.show ();
89                 } else {
90                         tearoff_arrow.set_no_show_all (true);
91                         tearoff_arrow.hide ();
92                 }
93                 _can_be_torn_off = yn;
94         }
95 }
96
97 void
98 TearOff::set_visible (bool yn)
99 {
100         /* don't change visibility if torn off */
101
102         if (own_window.is_visible()) {
103                 return;
104         }
105
106         if (_visible != yn) {
107                 _visible = yn;
108                 if (yn) {
109                         show_all();
110                         Visible ();
111                 } else {
112                         hide ();
113                         Hidden ();
114                 }
115         }
116 }
117
118 gint
119 TearOff::tearoff_click (GdkEventButton* ev)
120 {
121         if (_can_be_torn_off) {
122                 remove (contents);
123                 window_box.pack_start (contents);
124                 own_window.set_name (get_name());
125                 close_event_box.set_name (get_name());
126                 own_window.show_all ();
127                 hide ();
128                 Detach ();
129         }
130
131         return true;
132 }
133
134 gint
135 TearOff::close_click (GdkEventButton* ev)
136 {
137         window_box.remove (contents);
138         pack_start (contents);
139         reorder_child (contents, 0);
140         own_window.hide ();
141         show_all ();
142         Attach ();
143         return true;
144 }               
145
146 gint
147 TearOff::window_button_press (GdkEventButton* ev)
148 {
149         if (dragging || ev->button != 1) {
150                 dragging = false;
151                 own_window.remove_modal_grab();
152                 return true;
153         }
154
155         dragging = true;
156         drag_x = ev->x_root;
157         drag_y = ev->y_root;
158
159         own_window.add_modal_grab();
160
161         return true;
162 }
163
164 gint
165 TearOff::window_button_release (GdkEventButton* ev)
166 {
167         dragging = false;
168         own_window.remove_modal_grab();
169         return true;
170 }
171
172 gint
173 TearOff::window_delete_event (GdkEventAny* ev)
174 {
175         return close_click(0);
176 }
177
178 gint
179 TearOff::window_motion (GdkEventMotion* ev)
180 {
181         gint x;
182         gint y;
183         gint mx, my;
184         double x_delta;
185         double y_delta;
186         RefPtr<Gdk::Window> win (own_window.get_window());
187         
188         own_window.get_pointer (mx, my);
189
190         if (!dragging) {
191                 return true;
192         }
193
194         if (!(ev->state & GDK_BUTTON1_MASK)) {
195                 dragging = false;
196                 own_window.remove_modal_grab();
197                 return true;
198         }
199
200         x_delta = ev->x_root - drag_x;
201         y_delta = ev->y_root - drag_y;
202
203         win->get_root_origin (x, y);
204         win->move ((gint) floor (x + x_delta), (gint) floor (y + y_delta));
205         
206         drag_x = ev->x_root;
207         drag_y = ev->y_root;
208         
209         return true;
210 }
211
212 bool
213 TearOff::torn_off() const
214 {
215         return own_window.is_visible();
216 }