summaryrefslogtreecommitdiff
path: root/libs/gtkmm2ext/tabbable.cc
blob: 9c1452f11d7cb972212b8fdef196be97a6c6d90f (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
/*
    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/visibility_tracker.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, int position)
{
	notebook.insert_page (_contents, tab_title, position, false);
	notebook.set_tab_detachable (_contents);

	_parent_notebook = &notebook;
	_tab_title = tab_title;
	_notebook_position = position;
}

Window*
Tabbable::use_own_window ()
{
	return get (true);
}

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 ()
{
	(void) use_own_window ();
	
	/* 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->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 (_notebook_position);
		}
	}
}

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

	if (_window == toplevel) {

		std::cerr << " delete event for own window\n";
		
		/* 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) {

			std::cerr << "repack into parent notebook\n";
			
			_parent_notebook->insert_page (_contents, _tab_title, _notebook_position);
			_parent_notebook->set_tab_detachable (_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());
}