2 Copyright (C) 2015 Paul 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.
20 #include <gtkmm/action.h>
21 #include <gtkmm/notebook.h>
22 #include <gtkmm/window.h>
23 #include <gtkmm/stock.h>
25 #include "gtkmm2ext/tabbable.h"
26 #include "gtkmm2ext/gtk_ui.h"
27 #include "gtkmm2ext/utils.h"
28 #include "gtkmm2ext/visibility_tracker.h"
30 #include "pbd/stacktrace.h"
34 using namespace Gtkmm2ext;
38 Tabbable::Tabbable (Widget& w, const string& name, bool tabbed_by_default)
41 , _parent_notebook (0)
42 , tab_requested_by_state (tabbed_by_default)
46 Tabbable::~Tabbable ()
55 Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title)
57 _parent_notebook = ¬ebook;
59 if (tab_requested_by_state) {
65 Tabbable::use_own_window (bool and_pack_it)
67 Gtk::Window* win = get (true);
70 Gtk::Container* parent = _contents.get_parent();
73 parent->remove (_contents);
75 _own_notebook.append_page (_contents);
84 Tabbable::window_visible () const
90 return _window->is_visible();
94 Tabbable::get (bool create)
104 /* From here on, we're creating the window
107 if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
111 _window->add (_own_notebook);
112 _own_notebook.show ();
113 _own_notebook.set_show_tabs (false);
115 _window->signal_map().connect (sigc::mem_fun (*this, &Tabbable::window_mapped));
116 _window->signal_unmap().connect (sigc::mem_fun (*this, &Tabbable::window_unmapped));
118 /* do other window-related setup */
122 /* window should be ready for derived classes to do something with it */
128 Tabbable::show_own_window (bool and_pack_it)
130 Gtk::Widget* parent = _contents.get_parent();
131 Gtk::Allocation alloc;
134 alloc = parent->get_allocation();
137 (void) use_own_window (and_pack_it);
140 _window->set_default_size (alloc.get_width(), alloc.get_height());
143 tab_requested_by_state = false;
149 Tabbable::tab_root_drop ()
151 /* This is called after a drop of a tab onto the root window. Its
152 * responsibility xois to return the notebook that this Tabbable's
153 * contents should be packed into before the drop handling is
154 * completed. It is not responsible for actually taking care of this
158 show_own_window (false);
159 return &_own_notebook;
163 Tabbable::show_window ()
167 if (_window && (current_toplevel() == _window)) {
168 if (!_visible) { /* was hidden, update status */
174 /** If this Tabbable is currently parented by a tab, ensure that the tab is the
175 * current one. If it is parented by a window, then toggle the visibility of
179 Tabbable::change_visibility ()
182 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
186 if (tab_requested_by_state) {
187 /* should be tabbed, but currently isn't parented by a notebook */
191 if (_window && (current_toplevel() == _window)) {
192 /* Use WindowProxy method which will rotate then hide */
198 Tabbable::make_visible ()
200 if (_window && (current_toplevel() == _window)) {
205 if (!tab_requested_by_state) {
206 show_own_window (true);
214 Tabbable::make_invisible ()
216 if (_window && (current_toplevel() == _window)) {
226 show_own_window (true);
232 if (!_parent_notebook) {
242 if (_window && current_toplevel() == _window) {
243 /* unpack Tabbable from parent, put it back in the main tabbed
247 save_pos_and_size ();
250 _contents.get_parent()->remove (_contents);
252 /* leave the window around */
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));
263 /* have to force this on, which is semantically correct, since
264 * the user has effectively asked for it.
267 tab_requested_by_state = true;
272 Tabbable::delete_event_handler (GdkEventAny *ev)
280 Tabbable::tabbed () const
282 if (_window && (current_toplevel() == _window)) {
286 if (_parent_notebook && (_parent_notebook->page_num (_contents) >= 0)) {
294 Tabbable::hide_tab ()
298 _parent_notebook->remove_page (_contents);
304 Tabbable::show_tab ()
306 if (!window_visible() && _parent_notebook) {
307 if (_contents.get_parent() == 0) {
308 tab_requested_by_state = true;
309 add_to_notebook (*_parent_notebook, _tab_title);
311 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
313 current_toplevel()->present ();
318 Tabbable::current_toplevel () const
320 return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
324 Tabbable::xml_node_name()
326 return WindowProxy::xml_node_name();
330 Tabbable::tabbed_by_default() const
332 return tab_requested_by_state;
336 Tabbable::get_state()
338 XMLNode& node (WindowProxy::get_state());
340 node.set_property (X_("tabbed"), tabbed());
346 Tabbable::set_state (const XMLNode& node, int version)
350 if ((ret = WindowProxy::set_state (node, version)) != 0) {
355 show_own_window (true);
358 XMLNodeList children = node.children ();
359 XMLNode* window_node = node.child ("Window");
362 window_node->get_property (X_("tabbed"), tab_requested_by_state);
366 if (tab_requested_by_state) {
369 /* this does nothing if not tabbed */
378 Tabbable::window_mapped ()
384 Tabbable::window_unmapped ()