change/extend Tabbable API to allow for show/hide/attach/detach
[ardour.git] / libs / gtkmm2ext / tabbable.cc
1 /*
2     Copyright (C) 2015 Paul 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 */
19
20 #include <gtkmm/action.h>
21 #include <gtkmm/notebook.h>
22 #include <gtkmm/window.h>
23 #include <gtkmm/stock.h>
24
25 #include "gtkmm2ext/tabbable.h"
26 #include "gtkmm2ext/gtk_ui.h"
27 #include "gtkmm2ext/utils.h"
28 #include "gtkmm2ext/visibility_tracker.h"
29
30 #include "i18n.h"
31
32 using namespace Gtkmm2ext;
33 using namespace Gtk;
34 using std::string;
35
36 Tabbable::Tabbable (Widget& w, const string& name)
37         : WindowProxy (name)
38         , _contents (w)
39         , tab_close_image (ArdourIcon::CloseCross, 0xffffffff)
40 {
41         /* make the image about the same size as an actual X */
42         set_size_request_to_display_given_text (tab_close_image, "X", 0, 0);
43         
44         _tab_box.set_spacing (2);
45         _tab_box.pack_start (_tab_label, true, true);
46         _tab_box.pack_start (_tab_close_button, false, false);
47         _tab_close_button.add (tab_close_image);
48         
49         _tab_close_button.signal_clicked().connect (sigc::mem_fun (*this, &Tabbable::tab_close_clicked));
50 }
51
52 Tabbable::~Tabbable ()
53 {
54         if (_window) {
55                 delete _window;
56                 _window = 0;
57         }
58 }
59
60 void
61 Tabbable::set_allow_hide (bool yn)
62 {
63         if (yn) {
64                 tab_close_image.show ();
65         } else {
66                 tab_close_image.hide ();
67         }
68 }
69
70 void
71 Tabbable::tab_close_clicked ()
72 {
73         hide_tab ();
74 }
75
76 void
77 Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title)
78 {
79         _tab_label.set_text (tab_title);
80         _tab_box.show_all ();
81
82         notebook.append_page (_contents, _tab_box);
83
84         Gtkmm2ext::UI::instance()->set_tip (_tab_label,
85                                             string_compose (_("Drag this tab to the desktop to show %1 in its own window\n\n"
86                                                               "To put the window back, click on its \"close\" button"), tab_title));
87         
88         notebook.set_tab_detachable (_contents);
89         notebook.set_tab_reorderable (_contents);
90
91         _parent_notebook = &notebook;
92         _tab_title = tab_title;
93 }
94
95 Window*
96 Tabbable::use_own_window (bool and_pack_it)
97 {
98         Gtk::Window* win = get (true);
99
100         if (and_pack_it) {
101                 Gtk::Container* parent = _contents.get_parent();
102                 if (parent) {
103                         parent->remove (_contents);
104                 }
105                 _own_notebook.append_page (_contents, _tab_box);
106         }
107
108         return win;
109
110 }
111
112 bool
113 Tabbable::window_visible ()
114 {
115         if (!own_window()) {
116                 return false;
117         }
118
119         return visible();
120 }
121
122 Window*
123 Tabbable::get (bool create)
124 {
125         if (_window) {
126                 return _window;
127         }
128
129         if (!create) {
130                 return 0;
131         }
132
133         /* From here on, we're creating the window 
134          */
135         
136         if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
137                 return 0;
138         }
139
140         _window->add (_own_notebook);
141         _own_notebook.show ();
142         _own_notebook.set_show_tabs (false);
143
144         /* do other window-related setup */
145
146         setup ();
147
148         /* window should be ready for derived classes to do something with it */
149         
150         return _window;
151 }
152
153 void
154 Tabbable::show_own_window (bool and_pack_it)
155 {
156         Gtk::Widget* parent = _contents.get_parent();
157         Gtk::Allocation alloc;
158
159         if (parent) {
160                 alloc = parent->get_allocation();
161         }
162         
163         (void) use_own_window (and_pack_it);
164         
165         if (parent) {
166                 _window->set_default_size (alloc.get_width(), alloc.get_height());
167         }
168
169         _window->show_all ();
170         _window->present ();
171 }
172
173 Gtk::Notebook*
174 Tabbable::tab_root_drop ()
175 {
176         /* This is called after a drop of a tab onto the root window. Its
177          * responsibility xois to return the notebook that this Tabbable's
178          * contents should be packed into before the drop handling is
179          * completed. It is not responsible for actually taking care of this
180          * packing.
181          */
182
183         show_own_window (false);
184         return &_own_notebook;
185 }
186
187 void
188 Tabbable::show_window ()
189 {
190         make_visible ();
191
192         if (_window && (current_toplevel() == _window)) {
193                 if (!_visible) { /* was hidden, update status */
194                         set_pos_and_size ();
195                 }
196         }
197 }
198
199 void
200 Tabbable::make_visible ()
201 {
202         if (_window && (current_toplevel() == _window)) {
203                 _window->present ();
204         } else {
205                 show_tab ();
206         }
207 }
208
209 void
210 Tabbable::make_invisible ()
211 {
212         if (_window && (current_toplevel() == _window)) {
213                 _window->hide ();
214         } else {
215                 hide_tab ();
216         }
217 }
218         
219 void
220 Tabbable::detach ()
221 {
222         show_own_window (true);
223 }
224
225 void
226 Tabbable::attach ()
227 {
228         if (!_parent_notebook) {
229                 return;
230         }
231         
232         if (_parent_notebook->page_num (_contents) >= 0) {
233                 /* already tabbed */
234                 return;
235         }
236
237
238         if (_window && current_toplevel() == _window) {
239                 /* unpack Tabbable from parent, put it back in the main tabbed
240                  * notebook
241                  */
242                 
243                 save_pos_and_size ();
244                 
245                 _contents.get_parent()->remove (_contents);
246         
247                 /* leave the window around */
248                 
249                 _window->hide ();
250         }
251         
252         _parent_notebook->append_page (_contents, _tab_box);
253         _parent_notebook->set_tab_detachable (_contents);
254         _parent_notebook->set_tab_reorderable (_contents);
255         _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
256 }
257
258 bool
259 Tabbable::delete_event_handler (GdkEventAny *ev)
260 {
261         _window->hide();
262
263         return true;
264 }
265
266 bool
267 Tabbable::is_tabbed () const
268 {
269         Window* toplevel = (Window*) _contents.get_toplevel();
270
271         if (_window && (toplevel == _window)) {
272                 return false;
273         }
274
275         if (_parent_notebook && _contents.get_parent()) {
276                 return true;
277         }
278         
279         return false;
280 }
281
282 void
283 Tabbable::hide_tab ()
284 {
285         if (_parent_notebook) {
286                 _parent_notebook->remove_page (_contents);
287         }
288 }
289
290 void
291 Tabbable::show_tab ()
292 {
293         if (!window_visible() && _parent_notebook) {
294                 if (_contents.get_parent() == 0) {
295                         add_to_notebook (*_parent_notebook, _tab_title);
296                 }
297                 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
298         }
299 }
300
301 Gtk::Window*
302 Tabbable::current_toplevel () const
303 {
304         return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
305 }
306
307 string
308 Tabbable::xml_node_name()
309 {
310         return WindowProxy::xml_node_name();
311 }
312
313 XMLNode&
314 Tabbable::get_state()
315 {
316         XMLNode& node (WindowProxy::get_state());
317
318         return node;
319 }
320
321 int
322 Tabbable::set_state (const XMLNode& node, int version)
323 {
324         int ret;
325
326         if ((ret = WindowProxy::set_state (node, version)) == 0) {
327                 if (_visible) {
328                         if (use_own_window (true) == 0) {
329                                 ret = -1;
330                         }
331                 }
332         }
333
334         return ret;
335 }
336