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)
41 , _parent_notebook (0)
42 , tab_requested_by_state (true)
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();
72 parent->remove (_contents);
74 _own_notebook.append_page (_contents);
82 Tabbable::window_visible () const
88 return _window->is_visible();
92 Tabbable::get (bool create)
102 /* From here on, we're creating the window
105 if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
109 _window->add (_own_notebook);
110 _own_notebook.show ();
111 _own_notebook.set_show_tabs (false);
113 _window->signal_map().connect (sigc::mem_fun (*this, &Tabbable::window_mapped));
114 _window->signal_unmap().connect (sigc::mem_fun (*this, &Tabbable::window_unmapped));
116 /* do other window-related setup */
120 /* window should be ready for derived classes to do something with it */
126 Tabbable::show_own_window (bool and_pack_it)
128 Gtk::Widget* parent = _contents.get_parent();
129 Gtk::Allocation alloc;
132 alloc = parent->get_allocation();
135 (void) use_own_window (and_pack_it);
138 _window->set_default_size (alloc.get_width(), alloc.get_height());
141 tab_requested_by_state = false;
147 Tabbable::tab_root_drop ()
149 /* This is called after a drop of a tab onto the root window. Its
150 * responsibility xois to return the notebook that this Tabbable's
151 * contents should be packed into before the drop handling is
152 * completed. It is not responsible for actually taking care of this
156 show_own_window (false);
157 return &_own_notebook;
161 Tabbable::show_window ()
165 if (_window && (current_toplevel() == _window)) {
166 if (!_visible) { /* was hidden, update status */
172 /** If this Tabbable is currently parented by a tab, ensure that the tab is the
173 * current one. If it is parented by a window, then toggle the visibility of
177 Tabbable::change_visibility ()
180 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
184 if (tab_requested_by_state) {
185 /* should be tabbed, but currently isn't parented by a notebook */
189 if (_window && (current_toplevel() == _window)) {
190 /* Use WindowProxy method which will rotate then hide */
196 Tabbable::make_visible ()
198 if (_window && (current_toplevel() == _window)) {
203 if (!tab_requested_by_state) {
204 show_own_window (true);
212 Tabbable::make_invisible ()
214 if (_window && (current_toplevel() == _window)) {
224 show_own_window (true);
230 if (!_parent_notebook) {
240 if (_window && current_toplevel() == _window) {
241 /* unpack Tabbable from parent, put it back in the main tabbed
245 save_pos_and_size ();
247 _contents.get_parent()->remove (_contents);
249 /* leave the window around */
254 _parent_notebook->append_page (_contents);
255 _parent_notebook->set_tab_detachable (_contents);
256 _parent_notebook->set_tab_reorderable (_contents);
257 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
259 /* have to force this on, which is semantically correct, since
260 * the user has effectively asked for it.
263 tab_requested_by_state = true;
268 Tabbable::delete_event_handler (GdkEventAny *ev)
276 Tabbable::tabbed () const
278 if (_window && (current_toplevel() == _window)) {
282 if (_parent_notebook && (_parent_notebook->page_num (_contents) >= 0)) {
290 Tabbable::hide_tab ()
293 _parent_notebook->remove_page (_contents);
299 Tabbable::show_tab ()
301 if (!window_visible() && _parent_notebook) {
302 if (_contents.get_parent() == 0) {
303 tab_requested_by_state = true;
304 add_to_notebook (*_parent_notebook, _tab_title);
306 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
307 current_toplevel()->present ();
312 Tabbable::current_toplevel () const
314 return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
318 Tabbable::xml_node_name()
320 return WindowProxy::xml_node_name();
324 Tabbable::tabbed_by_default() const
326 return tab_requested_by_state;
330 Tabbable::get_state()
332 XMLNode& node (WindowProxy::get_state());
334 node.add_property (X_("tabbed"), tabbed() ? X_("yes") : X_("no"));
340 Tabbable::set_state (const XMLNode& node, int version)
344 if ((ret = WindowProxy::set_state (node, version)) != 0) {
349 show_own_window (true);
352 XMLNodeList children = node.children ();
353 XMLNode* window_node = node.child ("Window");
356 XMLProperty const * prop = window_node->property (X_("tabbed"));
358 tab_requested_by_state = PBD::string_is_affirmative (prop->value());
363 if (tab_requested_by_state) {
366 /* this does nothing if not tabbed */
375 Tabbable::window_mapped ()
381 Tabbable::window_unmapped ()