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/visibility_tracker.h"
31 using namespace Gtkmm2ext;
35 Tabbable::Tabbable (Widget& w, const string& name)
38 , tab_close_image (Stock::CLOSE, ICON_SIZE_BUTTON)
40 _tab_box.pack_start (_tab_label, true, true);
41 _tab_box.pack_start (_tab_close_button, false, false);
42 _tab_close_button.add (tab_close_image);
44 _tab_close_button.signal_clicked().connect (sigc::mem_fun (*this, &Tabbable::tab_close_clicked));
47 Tabbable::~Tabbable ()
56 Tabbable::tab_close_clicked ()
58 /* for this to happen, the tab must be visible so we
59 can assume that the contents are displayed in the
63 if (_parent_notebook) {
64 _parent_notebook->remove_page (_contents);
69 Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title)
71 _tab_label.set_text (tab_title);
74 notebook.append_page (_contents, _tab_box);
76 Gtkmm2ext::UI::instance()->set_tip (_tab_label,
77 string_compose (_("Drag this tab to the desktop to show %1 in its own window\n\n"
78 "To put the window back, click on its \"close\" button"), tab_title));
80 notebook.set_tab_detachable (_contents);
81 notebook.set_tab_reorderable (_contents);
83 _parent_notebook = ¬ebook;
84 _tab_title = tab_title;
88 Tabbable::use_own_window (bool and_pack_it)
90 Gtk::Window* win = get (true);
93 Gtk::Container* parent = _contents.get_parent();
95 parent->remove (_contents);
97 _own_notebook.append_page (_contents, _tab_box);
105 Tabbable::window_visible ()
115 Tabbable::get (bool create)
125 /* From here on, we're creating the window
128 if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
132 _window->add (_own_notebook);
133 _own_notebook.show ();
134 _own_notebook.set_show_tabs (false);
136 /* do other window-related setup */
140 /* window should be ready for derived classes to do something with it */
146 Tabbable::tab_root_drop ()
148 Gtk::Allocation alloc;
150 alloc = _contents.get_parent()->get_allocation();
152 (void) use_own_window (false);
154 /* This is called after a drop of a tab onto the root window. Its
155 * responsibility is to return the notebook that this Tabbable's
156 * contents should be packed into before the drop handling is
157 * completed. It is not responsible for actually taking care of this
161 _window->set_default_size (alloc.get_width(), alloc.get_height());
162 _window->show_all ();
165 return &_own_notebook;
169 Tabbable::show_window ()
173 if (_window && (current_toplevel() == _window)) {
174 if (!_visible) { /* was hidden, update status */
181 Tabbable::delete_event_handler (GdkEventAny *ev)
183 Window* toplevel = dynamic_cast<Window*> (_contents.get_toplevel());
185 if (_window == toplevel) {
187 /* unpack Tabbable from parent, put it back in the main tabbed
191 save_pos_and_size ();
193 _contents.get_parent()->remove (_contents);
195 /* leave the window around */
199 if (_parent_notebook) {
201 _parent_notebook->append_page (_contents, _tab_box);
202 _parent_notebook->set_tab_detachable (_contents);
203 _parent_notebook->set_tab_reorderable (_contents);
204 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
207 /* don't let anything else handle this */
217 Tabbable::is_tabbed () const
219 Window* toplevel = (Window*) _contents.get_toplevel();
221 if (_window && (toplevel == _window)) {
225 if (_parent_notebook && _contents.get_parent()) {
233 Tabbable::show_tab ()
235 if (!window_visible() && _parent_notebook) {
236 if (_contents.get_parent() == 0) {
237 add_to_notebook (*_parent_notebook, _tab_title);
239 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
244 Tabbable::current_toplevel () const
246 return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
250 Tabbable::xml_node_name()
252 return WindowProxy::xml_node_name();
256 Tabbable::get_state()
258 XMLNode& node (WindowProxy::get_state());
264 Tabbable::set_state (const XMLNode& node, int version)
268 if ((ret = WindowProxy::set_state (node, version)) == 0) {
270 if (use_own_window (true) == 0) {
280 Tabbable::make_visible ()
282 if (_window && (current_toplevel() == _window)) {