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