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_close_image (ArdourIcon::CloseCross, 0xffffffff)
43 , tab_requested_by_state (true)
45 /* sizes will be scaled during rendering */
46 tab_close_image.set_size_request (15,15);
48 _tab_box.add_events (Gdk::BUTTON_PRESS_MASK|Gdk::BUTTON_RELEASE_MASK);
49 _tab_box.set_spacing (2);
50 _tab_box.pack_start (_tab_label, true, true);
51 _tab_box.pack_start (tab_close_image, false, false);
53 tab_close_image.signal_button_release_event().connect (sigc::mem_fun (*this, &Tabbable::tab_close_clicked));
56 Tabbable::~Tabbable ()
65 Tabbable::tab_close_clicked (GdkEventButton*)
72 Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title)
74 _parent_notebook = ¬ebook;
75 _tab_title = tab_title;
76 _tab_label.set_text (tab_title);
79 if (tab_requested_by_state) {
85 Tabbable::use_own_window (bool and_pack_it)
87 Gtk::Window* win = get (true);
90 Gtk::Container* parent = _contents.get_parent();
92 parent->remove (_contents);
94 _own_notebook.append_page (_contents, _tab_box);
102 Tabbable::window_visible () const
108 return _window->is_visible();
112 Tabbable::get (bool create)
122 /* From here on, we're creating the window
125 if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
129 _window->add (_own_notebook);
130 _own_notebook.show ();
131 _own_notebook.set_show_tabs (false);
133 _window->signal_map().connect (sigc::mem_fun (*this, &Tabbable::window_mapped));
134 _window->signal_unmap().connect (sigc::mem_fun (*this, &Tabbable::window_unmapped));
136 /* do other window-related setup */
140 /* window should be ready for derived classes to do something with it */
146 Tabbable::show_own_window (bool and_pack_it)
148 Gtk::Widget* parent = _contents.get_parent();
149 Gtk::Allocation alloc;
152 alloc = parent->get_allocation();
155 (void) use_own_window (and_pack_it);
158 _window->set_default_size (alloc.get_width(), alloc.get_height());
161 tab_requested_by_state = false;
163 _window->show_all ();
168 Tabbable::tab_root_drop ()
170 /* This is called after a drop of a tab onto the root window. Its
171 * responsibility xois to return the notebook that this Tabbable's
172 * contents should be packed into before the drop handling is
173 * completed. It is not responsible for actually taking care of this
177 show_own_window (false);
178 return &_own_notebook;
182 Tabbable::show_window ()
186 if (_window && (current_toplevel() == _window)) {
187 if (!_visible) { /* was hidden, update status */
194 Tabbable::make_visible ()
196 if (_window && (current_toplevel() == _window)) {
200 if (!tab_requested_by_state) {
201 show_own_window (true);
209 Tabbable::make_invisible ()
211 if (_window && (current_toplevel() == _window)) {
221 show_own_window (true);
227 if (!_parent_notebook) {
237 if (_window && current_toplevel() == _window) {
238 /* unpack Tabbable from parent, put it back in the main tabbed
242 save_pos_and_size ();
244 _contents.get_parent()->remove (_contents);
246 /* leave the window around */
251 _parent_notebook->append_page (_contents, _tab_box);
252 _contents.set_data ("close-button", &tab_close_image);
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));
257 Gtkmm2ext::UI::instance()->set_tip (_tab_label,
258 string_compose (_("Drag this tab to the desktop to show %1 in its own window\n\n"
259 "To put the window back, use the Window > %1 > Attach menu action"), _tab_title));
262 /* have to force this on, which is semantically correct, since
263 * the user has effectively asked for it.
266 tab_requested_by_state = true;
271 Tabbable::delete_event_handler (GdkEventAny *ev)
279 Tabbable::tabbed () const
281 if (_window && (current_toplevel() == _window)) {
285 if (_parent_notebook && (_parent_notebook->page_num (_contents) >= 0)) {
293 Tabbable::hide_tab ()
296 _parent_notebook->remove_page (_contents);
302 Tabbable::show_tab ()
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);
309 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
314 Tabbable::current_toplevel () const
316 return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
320 Tabbable::xml_node_name()
322 return WindowProxy::xml_node_name();
326 Tabbable::tabbed_by_default() const
328 return tab_requested_by_state;
332 Tabbable::get_state()
334 XMLNode& node (WindowProxy::get_state());
336 node.add_property (X_("tabbed"), tabbed() ? X_("yes") : X_("no"));
342 Tabbable::set_state (const XMLNode& node, int version)
346 if ((ret = WindowProxy::set_state (node, version)) != 0) {
351 show_own_window (true);
354 XMLNodeList children = node.children ();
355 XMLNode* window_node = node.child ("Window");
358 const XMLProperty* prop = window_node->property (X_("tabbed"));
360 tab_requested_by_state = PBD::string_is_affirmative (prop->value());
365 if (tab_requested_by_state) {
368 /* this does nothing if not tabbed */
377 Tabbable::window_mapped ()
383 Tabbable::window_unmapped ()