summaryrefslogtreecommitdiff
path: root/libs/gtkmm2ext/tabbable.cc
blob: 4249da7fff2f4ace4cf349572d60fc7b808f32aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
    Copyright (C) 2015 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <gtkmm/action.h>
#include <gtkmm/notebook.h>
#include <gtkmm/window.h>

#include "gtkmm2ext/tabbable.h"
#include "gtkmm2ext/gtk_ui.h"
#include "gtkmm2ext/visibility_tracker.h"

#include "i18n.h"

using namespace Gtkmm2ext;
using namespace Gtk;
using std::string;

Tabbable::Tabbable (Widget& w, const string& name)
	: WindowProxy (name)
	, _contents (w)
{
}

Tabbable::~Tabbable ()
{
	if (_window) {
		delete _window;
		_window = 0;
	}
}

void
Tabbable::add_to_notebook (Notebook& notebook, const string& tab_title)
{
	notebook.append_page (_contents, tab_title, false);
	Gtk::Widget* tab_label = notebook.get_tab_label (_contents);

	if (tab_label) {
		Gtkmm2ext::UI::instance()->set_tip (*tab_label,
		                                    string_compose (_("Drag this tab to the desktop to show %1 in its own window\n\n"
		                                                      "To put the window back, click on its \"close\" button"), tab_title));
	}
	
	notebook.set_tab_detachable (_contents);
	notebook.set_tab_reorderable (_contents);

	_parent_notebook = &notebook;
	_tab_title = tab_title;
}

Window*
Tabbable::use_own_window (bool and_pack_it)
{
	Gtk::Window* win = get (true);

	if (and_pack_it) {
		Gtk::Container* parent = _contents.get_parent();
		if (parent) {
			parent->remove (_contents);
		}
		_own_notebook.append_page (_contents, _tab_title);
	}

	return win;

}

bool
Tabbable::window_visible ()
{
	if (!own_window()) {
		return false;
	}

	return visible();
}

Window*
Tabbable::get (bool create)
{
	if (_window) {
		return _window;
	}

	if (!create) {
		return 0;
	}

	/* From here on, we're creating the window 
	 */
	
	if ((_window = new Window (WINDOW_TOPLEVEL)) == 0) {
		return 0;
	}

	_window->add (_own_notebook);
	_own_notebook.show ();
	_own_notebook.set_show_tabs (false);

	/* do other window-related setup */

	setup ();

	/* window should be ready for derived classes to do something with it */
	
	return _window;
}

Gtk::Notebook*
Tabbable::tab_root_drop ()
{
	Gtk::Allocation alloc;

	alloc = _contents.get_parent()->get_allocation();
	
	(void) use_own_window (false);
	
	/* This is called after a drop of a tab onto the root window. Its
	 * responsibility is to return the notebook that this Tabbable's
	 * contents should be packed into before the drop handling is
	 * completed. It is not responsible for actually taking care of this
	 * packing.
	 */

	_window->set_default_size (alloc.get_width(), alloc.get_height());
	_window->show_all ();
	_window->present ();

	return &_own_notebook;
}

void
Tabbable::show_window ()
{
	Window* toplevel = dynamic_cast<Window*> (_contents.get_toplevel());

	if (toplevel == _window) {
		_window->present ();
	}

	if (!_visible) { /* was hidden, update status */
		set_pos_and_size ();
	}

	if (toplevel != _window) {
		/* not in its own window, just switch parent notebook to show
		   this Tabbable.
		*/
		if (_parent_notebook) {
			_parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
		}
	}
}

bool
Tabbable::delete_event_handler (GdkEventAny *ev)
{
	Window* toplevel = dynamic_cast<Window*> (_contents.get_toplevel());

	if (_window == toplevel) {

		/* unpack Tabbable from parent, put it back in the main tabbed
		 * notebook
		 */

		save_pos_and_size ();

		_contents.get_parent()->remove (_contents);

		/* leave the window around */

		_window->hide ();
		
		if (_parent_notebook) {

			_parent_notebook->append_page (_contents, _tab_title);
			_parent_notebook->set_tab_detachable (_contents);
			_parent_notebook->set_tab_reorderable (_contents);
			_parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
		}

		/* don't let anything else handle this */
		
		return true;
	} 

	/* nothing to do */
	return false;
}

bool
Tabbable::is_tabbed () const
{
	Window* toplevel = (Window*) _contents.get_toplevel();

	if (_window && (toplevel == _window)) {
		return false;
	}

	if (_parent_notebook) {
		return true;
	}
	
	return false;
}

void
Tabbable::show_tab ()
{
	if (!window_visible() && _parent_notebook) {
		_parent_notebook->set_current_page (_parent_notebook->page_num (_contents));
	}
}

Gtk::Window*
Tabbable::current_toplevel () const
{
	return dynamic_cast<Gtk::Window*> (contents().get_toplevel());
}

string
Tabbable::xml_node_name()
{
	return WindowProxy::xml_node_name();
}

XMLNode&
Tabbable::get_state()
{
	XMLNode& node (WindowProxy::get_state());

	return node;
}

int
Tabbable::set_state (const XMLNode& node, int version)
{
	int ret;

	if ((ret = WindowProxy::set_state (node, version)) == 0) {
		if (_visible) {
			if (use_own_window (true) == 0) {
				ret = -1;
			}
		}
	}

	return ret;
}