provide Tabbable::change_visibility(), which has slightly odd semantics that are...
[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 "pbd/stacktrace.h"
31
32 #include "i18n.h"
33
34 using namespace Gtkmm2ext;
35 using namespace Gtk;
36 using std::string;
37
38 Tabbable::Tabbable (Widget& w, const string& name)
39         : WindowProxy (name)
40         , _contents (w)
41         , _parent_notebook (0)
42         , tab_requested_by_state (true)
43 {
44 }
45
46 Tabbable::~Tabbable ()
47 {
48         if (_window) {
49                 delete _window;
50                 _window = 0;
51         }
52 }
53
54 void
55 Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title)
56 {
57         _parent_notebook = &notebook;
58
59         if (tab_requested_by_state) {
60                 attach ();
61         }
62 }
63
64 Window*
65 Tabbable::use_own_window (bool and_pack_it)
66 {
67         Gtk::Window* win = get (true);
68
69         if (and_pack_it) {
70                 Gtk::Container* parent = _contents.get_parent();
71                 if (parent) {
72                         parent->remove (_contents);
73                 }
74                 _own_notebook.append_page (_contents);
75         }
76
77         return win;
78
79 }
80
81 bool
82 Tabbable::window_visible () const
83 {
84         if (!_window) {
85                 return false;
86         }
87
88         return _window->is_visible();
89 }
90
91 Window*
92 Tabbable::get (bool create)
93 {
94         if (_window) {
95                 return _window;
96         }
97
98         if (!create) {
99                 return 0;
100         }
101
102         /* From here on, we're creating the window
103          */
104
105         if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
106                 return 0;
107         }
108
109         _window->add (_own_notebook);
110         _own_notebook.show ();
111         _own_notebook.set_show_tabs (false);
112
113         _window->signal_map().connect (sigc::mem_fun (*this, &Tabbable::window_mapped));
114         _window->signal_unmap().connect (sigc::mem_fun (*this, &Tabbable::window_unmapped));
115
116         /* do other window-related setup */
117
118         setup ();
119
120         /* window should be ready for derived classes to do something with it */
121
122         return _window;
123 }
124
125 void
126 Tabbable::show_own_window (bool and_pack_it)
127 {
128         Gtk::Widget* parent = _contents.get_parent();
129         Gtk::Allocation alloc;
130
131         if (parent) {
132                 alloc = parent->get_allocation();
133         }
134
135         (void) use_own_window (and_pack_it);
136
137         if (parent) {
138                 _window->set_default_size (alloc.get_width(), alloc.get_height());
139         }
140
141         tab_requested_by_state = false;
142
143         _window->show_all ();
144         _window->present ();
145 }
146
147 Gtk::Notebook*
148 Tabbable::tab_root_drop ()
149 {
150         /* This is called after a drop of a tab onto the root window. Its
151          * responsibility xois to return the notebook that this Tabbable's
152          * contents should be packed into before the drop handling is
153          * completed. It is not responsible for actually taking care of this
154          * packing.
155          */
156
157         show_own_window (false);
158         return &_own_notebook;
159 }
160
161 void
162 Tabbable::show_window ()
163 {
164         make_visible ();
165
166         if (_window && (current_toplevel() == _window)) {
167                 if (!_visible) { /* was hidden, update status */
168                         set_pos_and_size ();
169                 }
170         }
171 }
172
173 /** If this Tabbable is currently parented by a tab, ensure that the tab is the
174  * current one. If it is parented by a window, then toggle the visibility of
175  * that window.
176  */
177 void
178 Tabbable::change_visibility ()
179 {
180         if (tabbed()) {
181                 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
182                 return;
183         }
184
185         if (tab_requested_by_state) {
186                 /* should be tabbed, but currently isn't parented by a notebook */
187                 return;
188         }
189
190         if (_window && (current_toplevel() == _window)) {
191                 if (_window->is_visible ()) {
192                         _window->hide ();
193                 } else {
194                         _window->present ();
195                 }
196         }
197 }
198
199 void
200 Tabbable::make_visible ()
201 {
202         if (_window && (current_toplevel() == _window)) {
203                 _window->present ();
204         } else {
205
206                 if (!tab_requested_by_state) {
207                         show_own_window (true);
208                 } else {
209                         show_tab ();
210                 }
211         }
212 }
213
214 void
215 Tabbable::make_invisible ()
216 {
217         if (_window && (current_toplevel() == _window)) {
218                 _window->hide ();
219         } else {
220                 hide_tab ();
221         }
222 }
223
224 void
225 Tabbable::detach ()
226 {
227         show_own_window (true);
228 }
229
230 void
231 Tabbable::attach ()
232 {
233         if (!_parent_notebook) {
234                 return;
235         }
236
237         if (tabbed()) {
238                 /* already tabbed */
239                 return;
240         }
241
242
243         if (_window && current_toplevel() == _window) {
244                 /* unpack Tabbable from parent, put it back in the main tabbed
245                  * notebook
246                  */
247
248                 save_pos_and_size ();
249
250                 _contents.get_parent()->remove (_contents);
251
252                 /* leave the window around */
253
254                 _window->hide ();
255         }
256
257         _parent_notebook->append_page (_contents);
258         _parent_notebook->set_tab_detachable (_contents);
259         _parent_notebook->set_tab_reorderable (_contents);
260         _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
261
262         /* have to force this on, which is semantically correct, since
263          * the user has effectively asked for it.
264          */
265
266         tab_requested_by_state = true;
267         StateChange (*this);
268 }
269
270 bool
271 Tabbable::delete_event_handler (GdkEventAny *ev)
272 {
273         _window->hide();
274
275         return true;
276 }
277
278 bool
279 Tabbable::tabbed () const
280 {
281         if (_window && (current_toplevel() == _window)) {
282                 return false;
283         }
284
285         if (_parent_notebook && (_parent_notebook->page_num (_contents) >= 0)) {
286                 return true;
287         }
288
289         return false;
290 }
291
292 void
293 Tabbable::hide_tab ()
294 {
295         if (tabbed()) {
296                 _parent_notebook->remove_page (_contents);
297                 StateChange (*this);
298         }
299 }
300
301 void
302 Tabbable::show_tab ()
303 {
304         if (!window_visible() && _parent_notebook) {
305                 if (_contents.get_parent() == 0) {
306                         tab_requested_by_state = true;
307                         add_to_notebook (*_parent_notebook, _tab_title);
308                 }
309                 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
310         }
311 }
312
313 Gtk::Window*
314 Tabbable::current_toplevel () const
315 {
316         return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
317 }
318
319 string
320 Tabbable::xml_node_name()
321 {
322         return WindowProxy::xml_node_name();
323 }
324
325 bool
326 Tabbable::tabbed_by_default() const
327 {
328         return tab_requested_by_state;
329 }
330
331 XMLNode&
332 Tabbable::get_state()
333 {
334         XMLNode& node (WindowProxy::get_state());
335
336         node.add_property (X_("tabbed"),  tabbed() ? X_("yes") : X_("no"));
337
338         return node;
339 }
340
341 int
342 Tabbable::set_state (const XMLNode& node, int version)
343 {
344         int ret;
345
346         if ((ret = WindowProxy::set_state (node, version)) != 0) {
347                 return ret;
348         }
349
350         if (_visible) {
351                 show_own_window (true);
352         }
353
354         XMLNodeList children = node.children ();
355         XMLNode* window_node = node.child ("Window");
356
357         if (window_node) {
358                 const XMLProperty* prop = window_node->property (X_("tabbed"));
359                 if (prop) {
360                         tab_requested_by_state = PBD::string_is_affirmative (prop->value());
361                 }
362         }
363
364         if (!_visible) {
365                 if (tab_requested_by_state) {
366                         attach ();
367                 } else {
368                         /* this does nothing if not tabbed */
369                         hide_tab ();
370                 }
371         }
372
373         return ret;
374 }
375
376 void
377 Tabbable::window_mapped ()
378 {
379         StateChange (*this);
380 }
381
382 void
383 Tabbable::window_unmapped ()
384 {
385         StateChange (*this);
386 }