-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtk-multiview.c
364 lines (302 loc) · 10.9 KB
/
gtk-multiview.c
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
* Copyright (C) 2000 Jonathan Blandford
* Copyright (C) 2001-2019 the AUTHORS
*
* License changed from the GNU LGPL to the GNU GPL (as permitted under
* Term 3 of the GNU LGPL) by Gary Wong for distribution with GNU Backgammon.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* $Id: gtk-multiview.c,v 1.20 2021/04/03 21:09:26 plm Exp $
*/
#include "config.h"
#include "common.h"
#include "gtklocdefs.h"
#include <gtk/gtk.h>
#include "gtk-multiview.h"
G_DEFINE_TYPE(GtkMultiview, gtk_multiview, GTK_TYPE_CONTAINER)
static void
gtk_multiview_size_request(GtkWidget * widget, GtkRequisition * requisition);
#if GTK_CHECK_VERSION(3,0,0)
static void
gtk_multiview_get_preferred_width(GtkWidget * widget, gint * minimal_width, gint * natural_width);
static void
gtk_multiview_get_preferred_height(GtkWidget * widget, gint * minimal_height, gint * natural_height);
#endif
static void
gtk_multiview_size_allocate(GtkWidget * widget, GtkAllocation * allocation);
static void
gtk_multiview_map(GtkWidget * widget);
static void
gtk_multiview_unmap(GtkWidget * widget);
static GType
gtk_multiview_child_type(GtkContainer * container);
static void
gtk_multiview_forall(GtkContainer * container,
gboolean include_internals, GtkCallback callback, gpointer callback_data);
static void
gtk_multiview_add(GtkContainer * widget, GtkWidget * child);
static void
gtk_multiview_remove(GtkContainer * widget, GtkWidget * child);
static void
gtk_multiview_init(GtkMultiview * multiview)
{
gtk_widget_set_has_window(GTK_WIDGET(multiview), FALSE);
multiview->current = NULL;
multiview->children = NULL;
}
static void
gtk_multiview_class_init(GtkMultiviewClass * klass)
{
GtkWidgetClass *widget_class;
GtkContainerClass *container_class;
widget_class = (GtkWidgetClass *) klass;
container_class = (GtkContainerClass *) klass;
#if GTK_CHECK_VERSION(3,0,0)
widget_class->get_preferred_width = gtk_multiview_get_preferred_width;
widget_class->get_preferred_height = gtk_multiview_get_preferred_height;
#else
widget_class->size_request = gtk_multiview_size_request;
#endif
widget_class->size_allocate = gtk_multiview_size_allocate;
widget_class->map = gtk_multiview_map;
widget_class->unmap = gtk_multiview_unmap;
container_class->forall = gtk_multiview_forall;
container_class->add = gtk_multiview_add;
container_class->remove = gtk_multiview_remove;
container_class->child_type = gtk_multiview_child_type;
}
static void
gtk_multiview_size_request(GtkWidget * widget, GtkRequisition * requisition)
{
GList *tmp_list;
GtkMultiview *multiview;
GtkRequisition child_requisition;
GtkWidget *child;
multiview = GTK_MULTIVIEW(widget);
requisition->width = 0;
requisition->height = 0;
/* We find the maximum size of all children widgets */
tmp_list = multiview->children;
while (tmp_list) {
child = GTK_WIDGET(tmp_list->data);
tmp_list = tmp_list->next;
if (gtk_widget_get_visible(child)) {
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_get_preferred_size(child, &child_requisition, NULL);
#else
gtk_widget_size_request(child, &child_requisition);
#endif
requisition->width = MAX(requisition->width, child_requisition.width);
requisition->height = MAX(requisition->height, child_requisition.height);
if (gtk_widget_get_mapped(child) && child != multiview->current) {
gtk_widget_unmap(GTK_WIDGET(child));
}
}
}
}
#if GTK_CHECK_VERSION(3,0,0)
static void
gtk_multiview_get_preferred_width(GtkWidget * widget, gint * minimal_width, gint * natural_width)
{
GtkRequisition requisition;
gtk_multiview_size_request(widget, &requisition);
*minimal_width = *natural_width = requisition.width;
}
static void
gtk_multiview_get_preferred_height(GtkWidget * widget, gint * minimal_height, gint * natural_height)
{
GtkRequisition requisition;
gtk_multiview_size_request(widget, &requisition);
*minimal_height = *natural_height = requisition.height;
}
#endif
static void
gtk_multiview_size_allocate(GtkWidget * widget, GtkAllocation * allocation)
{
GtkMultiview *multiview;
GList *tmp_list;
g_return_if_fail(widget != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(widget));
multiview = GTK_MULTIVIEW(widget);
gtk_widget_set_allocation(widget, allocation);
tmp_list = multiview->children;
while (tmp_list) {
GtkWidget *child = GTK_WIDGET(tmp_list->data);
tmp_list = tmp_list->next;
if (gtk_widget_get_visible(child)) {
gtk_widget_size_allocate(child, allocation);
}
}
}
static void
gtk_multiview_map(GtkWidget * widget)
{
GtkMultiview *multiview;
g_return_if_fail(widget != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(widget));
multiview = GTK_MULTIVIEW(widget);
gtk_widget_set_mapped(widget, TRUE);
if (multiview->current && gtk_widget_get_visible(multiview->current) && !gtk_widget_get_mapped(multiview->current)) {
gtk_widget_map(GTK_WIDGET(multiview->current));
}
}
static void
gtk_multiview_unmap(GtkWidget * widget)
{
GtkMultiview *multiview;
g_return_if_fail(widget != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(widget));
multiview = GTK_MULTIVIEW(widget);
gtk_widget_set_mapped(widget, FALSE);
if (multiview->current && gtk_widget_get_visible(multiview->current) && gtk_widget_get_mapped(multiview->current)) {
gtk_widget_unmap(GTK_WIDGET(multiview->current));
}
}
static GType
gtk_multiview_child_type(GtkContainer * UNUSED(container))
{
return gtk_widget_get_type();
}
static void
gtk_multiview_forall(GtkContainer * container,
gboolean UNUSED(include_internals), GtkCallback callback, gpointer callback_data)
{
GtkMultiview *multiview;
GList *tmp_list;
g_return_if_fail(container != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(container));
g_return_if_fail(callback != NULL);
multiview = GTK_MULTIVIEW(container);
tmp_list = multiview->children;
while (tmp_list) {
GtkWidget *child = GTK_WIDGET(tmp_list->data);
tmp_list = tmp_list->next;
(*callback) (GTK_WIDGET(child), callback_data);
}
}
static void
gtk_multiview_add(GtkContainer * container, GtkWidget * child)
{
g_return_if_fail(container != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(container));
g_return_if_fail(child != NULL);
g_return_if_fail(GTK_IS_WIDGET(child));
gtk_multiview_append_child(GTK_MULTIVIEW(container), child);
}
static void
gtk_multiview_remove(GtkContainer * container, GtkWidget * child)
{
GtkMultiview *multiview;
GList *list;
g_return_if_fail(container != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(container));
g_return_if_fail(child != NULL);
multiview = GTK_MULTIVIEW(container);
list = g_list_find(multiview->children, child);
g_return_if_fail(list != NULL);
/* If we are mapped and visible, we want to deal with changing the page. */
if ((gtk_widget_get_mapped(GTK_WIDGET(container))) &&
(list->data == (gpointer) multiview->current) && (list->next != NULL)) {
gtk_multiview_set_current(multiview, GTK_WIDGET(list->next->data));
}
multiview->children = g_list_remove(multiview->children, child);
gtk_widget_unparent(child);
}
/* Public Functions */
GtkWidget *
gtk_multiview_new(void)
{
return g_object_new(GTK_TYPE_MULTIVIEW, NULL);
}
void
gtk_multiview_prepend_child(GtkMultiview * multiview, GtkWidget * child)
{
g_return_if_fail(multiview != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(multiview));
g_return_if_fail(child != NULL);
g_return_if_fail(GTK_IS_WIDGET(child));
gtk_multiview_insert_child(multiview, NULL, child);
}
void
gtk_multiview_insert_child(GtkMultiview * multiview, GtkWidget * back_child, GtkWidget * child)
{
GList *list;
g_return_if_fail(multiview != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(multiview));
g_return_if_fail(child != NULL);
g_return_if_fail(GTK_IS_WIDGET(child));
list = g_list_find(multiview->children, back_child);
if (list == NULL) {
multiview->children = g_list_prepend(multiview->children, child);
} else {
GList *new_el = g_list_alloc();
new_el->next = list->next;
new_el->prev = list;
if (new_el->next)
new_el->next->prev = new_el;
new_el->prev->next = new_el;
new_el->data = (gpointer) child;
}
gtk_widget_set_parent(GTK_WIDGET(child), GTK_WIDGET(multiview));
if (gtk_widget_get_realized(GTK_WIDGET(multiview)))
gtk_widget_realize(GTK_WIDGET(child));
if (gtk_widget_get_visible(GTK_WIDGET(multiview)) && gtk_widget_get_visible(GTK_WIDGET(child))) {
if (gtk_widget_get_mapped(GTK_WIDGET(child)))
gtk_widget_unmap(GTK_WIDGET(child));
gtk_widget_queue_resize(GTK_WIDGET(multiview));
}
/* if it's the first and only entry, we want to bring it to the foreground. */
if (multiview->children->next == NULL)
gtk_multiview_set_current(multiview, child);
}
void
gtk_multiview_append_child(GtkMultiview * multiview, GtkWidget * child)
{
GList *list;
g_return_if_fail(multiview != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(multiview));
g_return_if_fail(child != NULL);
g_return_if_fail(GTK_IS_WIDGET(child));
list = g_list_last(multiview->children);
if (list) {
gtk_multiview_insert_child(multiview, GTK_WIDGET(list->data), child);
} else {
gtk_multiview_insert_child(multiview, NULL, child);
}
}
void
gtk_multiview_set_current(GtkMultiview * multiview, GtkWidget * child)
{
GList *list;
GtkWidget *old = NULL;
g_return_if_fail(multiview != NULL);
g_return_if_fail(GTK_IS_MULTIVIEW(multiview));
g_return_if_fail(child != NULL);
g_return_if_fail(GTK_IS_WIDGET(child));
if (multiview->current == child)
return;
list = g_list_find(multiview->children, child);
g_return_if_fail(list != NULL);
if ((multiview->current) &&
(gtk_widget_get_visible(multiview->current)) && (gtk_widget_get_mapped(GTK_WIDGET(multiview)))) {
old = GTK_WIDGET(multiview->current);
}
multiview->current = GTK_WIDGET(list->data);
if (gtk_widget_get_visible(multiview->current) && (gtk_widget_get_mapped(GTK_WIDGET(multiview)))) {
gtk_widget_map(multiview->current);
}
if (old && gtk_widget_get_mapped(old))
gtk_widget_unmap(old);
}