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