e4c86e838469096110c7e499a71097db616a8182
[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
24 #include "gtkmm2ext/tabbable.h"
25 #include "gtkmm2ext/visibility_tracker.h"
26
27 using namespace Gtkmm2ext;
28 using namespace Gtk;
29 using std::string;
30
31 Tabbable::Tabbable (Widget& w, const string& name)
32         : WindowProxy (name)
33         , _contents (w)
34 {
35 }
36
37 Tabbable::~Tabbable ()
38 {
39         if (_window) {
40                 delete _window;
41                 _window = 0;
42         }
43 }
44
45 void
46 Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title, int position)
47 {
48         notebook.insert_page (_contents, tab_title, position, false);
49         notebook.set_tab_detachable (_contents);
50
51         _parent_notebook = &notebook;
52         _tab_title = tab_title;
53         _notebook_position = position;
54 }
55
56 Window*
57 Tabbable::use_own_window ()
58 {
59         return get (true);
60 }
61
62 bool
63 Tabbable::window_visible ()
64 {
65         if (!own_window()) {
66                 return false;
67         }
68
69         return visible();
70 }
71
72 Window*
73 Tabbable::get (bool create)
74 {
75         if (_window) {
76                 return _window;
77         }
78
79         if (!create) {
80                 return 0;
81         }
82
83         /* From here on, we're creating the window 
84          */
85         
86         if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
87                 return 0;
88         }
89
90         _window->add (_own_notebook);
91         _own_notebook.show ();
92         _own_notebook.set_show_tabs (false);
93
94         /* do other window-related setup */
95
96         setup ();
97
98         /* window should be ready for derived classes to do something with it */
99         
100         return _window;
101 }
102
103 Gtk::Notebook*
104 Tabbable::tab_root_drop ()
105 {
106         Gtk::Allocation alloc;
107
108         alloc = _contents.get_parent()->get_allocation();
109         
110         (void) use_own_window ();
111         
112         /* This is called after a drop of a tab onto the root window. Its
113          * responsibility is to return the notebook that this Tabbable's
114          * contents should be packed into before the drop handling is
115          * completed. It is not responsible for actually taking care of this
116          * packing.
117          */
118
119         _window->set_default_size (alloc.get_width(), alloc.get_height());
120         _window->show_all ();
121         _window->present ();
122
123         return &_own_notebook;
124 }
125
126 void
127 Tabbable::show_window ()
128 {
129         Window* toplevel = dynamic_cast<Window*> (_contents.get_toplevel());
130
131         if (toplevel == _window) {
132                 _window->present ();
133         }
134
135         if (!_visible) { /* was hidden, update status */
136                 set_pos_and_size ();
137         }
138
139         if (toplevel != _window) {
140                 /* not in its own window, just switch parent notebook to show
141                    this Tabbable.
142                 */
143                 if (_parent_notebook) {
144                         _parent_notebook->set_current_page (_notebook_position);
145                 }
146         }
147 }
148
149 bool
150 Tabbable::delete_event_handler (GdkEventAny *ev)
151 {
152         Window* toplevel = dynamic_cast<Window*> (_contents.get_toplevel());
153
154         if (_window == toplevel) {
155
156                 /* unpack Tabbable from parent, put it back in the main tabbed
157                  * notebook
158                  */
159
160                 save_pos_and_size ();
161
162                 _contents.get_parent()->remove (_contents);
163
164                 /* leave the window around */
165
166                 _window->hide ();
167                 
168                 if (_parent_notebook) {
169
170                         _parent_notebook->insert_page (_contents, _tab_title, _notebook_position);
171                         _parent_notebook->set_tab_detachable (_contents);
172                         _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
173                 }
174
175                 /* don't let anything else handle this */
176                 
177                 return true;
178         } 
179
180         /* nothing to do */
181         return false;
182 }
183
184 bool
185 Tabbable::is_tabbed () const
186 {
187         Window* toplevel = (Window*) _contents.get_toplevel();
188
189         if (_window && (toplevel == _window)) {
190                 return false;
191         }
192
193         if (_parent_notebook) {
194                 return true;
195         }
196         
197         return false;
198 }
199
200 void
201 Tabbable::show_tab ()
202 {
203         if (!window_visible() && _parent_notebook) {
204                 _parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
205         }
206 }
207
208 Gtk::Window*
209 Tabbable::current_toplevel () const
210 {
211         return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
212 }