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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
|
/*
* Tabbed notebook
*
* Copyright (C) 2017 Patrick McDermott
*
* This file is part of Marquee.
*
* Marquee 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.
*
* Marquee 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 Marquee. If not, see <http://www.gnu.org/licenses/>.
*/
#include "notebook.h"
#include <glib.h>
#include <gtk/gtk.h>
#include "tab-label.h"
#include "tab-page.h"
#include "tree.h"
#include "window.h"
#define MQ_TAB_TREE(obj) ((MqTabTree *) (obj))
typedef struct {
MqTree parent_instance;
MqTabLabel *label;
MqTabPage *page;
} MqTabTree;
struct _MqNotebook {
GtkNotebook parent_instance;
MqWindow *window;
MqTabTree *tree;
MqTabPage *find_page;
MqTabTree *found_node;
MqTabPage *current_page;
GtkWidget *tab_tree_popover;
};
enum {
PROP_WINDOW = 1,
N_PROPERTIES
};
static GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};
struct _MqNotebookClass {
GtkNotebookClass parent_class;
};
G_DEFINE_TYPE(MqNotebook, mq_notebook, GTK_TYPE_NOTEBOOK)
static void
get_property(GObject *object, guint property_id, GValue *value,
GParamSpec *param_spec)
{
MqNotebook *notebook;
notebook = MQ_NOTEBOOK(object);
switch (property_id) {
case PROP_WINDOW:
g_value_set_object(value, notebook->window);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
param_spec);
break;
}
}
static void
set_property(GObject *object, guint property_id, const GValue *value,
GParamSpec *param_spec)
{
MqNotebook *notebook;
notebook = MQ_NOTEBOOK(object);
switch (property_id) {
case PROP_WINDOW:
notebook->window = g_value_get_object(value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
param_spec);
break;
}
}
static void
mq_notebook_class_init(MqNotebookClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->get_property = get_property;
object_class->set_property = set_property;
obj_properties[PROP_WINDOW] = g_param_spec_object(
"window",
"MqWindow",
"The parent MqWindow instance",
MQ_TYPE_WINDOW,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB);
g_object_class_install_properties(object_class, N_PROPERTIES,
obj_properties);
}
static void
switch_page_cb(MqNotebook *notebook, MqTabPage *page,
guint G_GNUC_UNUSED page_num)
{
notebook->current_page = page;
mq_window_set_title(notebook->window, mq_tab_page_get_title(page));
}
static void
new_tab_clicked_cb(GtkWidget G_GNUC_UNUSED *button, MqNotebook *notebook)
{
mq_notebook_insert_child(notebook, NULL, notebook->current_page);
}
static void
create_tab_tree_model_recurse(MqTabTree *node, GtkTreeStore *tree_store,
GtkTreeIter *parent_tree_iter)
{
GtkTreeIter tree_iter;
for (; node; node = MQ_TAB_TREE(mq_tree_next(node))) {
gtk_tree_store_append(tree_store, &tree_iter, parent_tree_iter);
gtk_tree_store_set(tree_store, &tree_iter, 0,
mq_tab_page_get_title(node->page), -1);
create_tab_tree_model_recurse(
MQ_TAB_TREE(mq_tree_first_child(node)),
tree_store, &tree_iter);
}
}
static GtkTreeModel *
create_tab_tree_model(MqNotebook *notebook)
{
GtkTreeStore *tree_store;
tree_store = gtk_tree_store_new(1, G_TYPE_STRING);
create_tab_tree_model_recurse(
MQ_TAB_TREE(mq_tree_first_child(notebook->tree)),
tree_store, NULL);
return GTK_TREE_MODEL(tree_store);
}
static void
tab_tree_row_activated_cb(GtkTreeView G_GNUC_UNUSED *tree_view,
GtkTreePath *tree_path,
GtkTreeViewColumn G_GNUC_UNUSED *tree_view_column, MqNotebook *notebook)
{
gint *indices;
gint depth;
indices = gtk_tree_path_get_indices_with_depth(tree_path, &depth);
g_assert(depth == 1);
mq_notebook_set_current_page(notebook, indices[0]);
gtk_widget_hide(notebook->tab_tree_popover);
}
static GtkWidget *
create_tab_tree_view(MqNotebook *notebook)
{
GtkTreeModel *tree_model;
GtkWidget *tree_view;
GtkTreeSelection *tree_selection;
GtkCellRenderer *cell_renderer;
tree_model = create_tab_tree_model(notebook);
tree_view = gtk_tree_view_new_with_model(tree_model);
tree_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view));
gtk_tree_selection_set_mode(tree_selection, GTK_SELECTION_BROWSE);
gtk_tree_selection_select_path(tree_selection,
gtk_tree_path_new_from_indices(
gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook)),
-1));
gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree_view), FALSE);
gtk_tree_view_set_activate_on_single_click(GTK_TREE_VIEW(tree_view),
TRUE);
gtk_tree_view_expand_all(GTK_TREE_VIEW(tree_view));
gtk_tree_view_set_reorderable(GTK_TREE_VIEW(tree_view), TRUE);
gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(tree_view), TRUE);
g_signal_connect(tree_view, "row-activated",
G_CALLBACK(tab_tree_row_activated_cb), notebook);
cell_renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(tree_view),
-1, NULL, cell_renderer, "text", 0, NULL);
return tree_view;
}
static void
tab_list_clicked_cb(GtkWidget *button, MqNotebook *notebook)
{
GtkWidget *tab_tree_view;
GtkWidget *scrolled_window;
tab_tree_view = create_tab_tree_view(notebook);
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_scrolled_window_set_min_content_width(
GTK_SCROLLED_WINDOW(scrolled_window), 400);
gtk_scrolled_window_set_min_content_height(
GTK_SCROLLED_WINDOW(scrolled_window), 200);
gtk_container_add(GTK_CONTAINER(scrolled_window), tab_tree_view);
notebook->tab_tree_popover = gtk_popover_new(button);
gtk_container_add(GTK_CONTAINER(notebook->tab_tree_popover),
scrolled_window);
/* NB: gtk_popover_popup() is new in GTK+ 3.22. */
gtk_widget_show_all(notebook->tab_tree_popover);
}
static void
mq_notebook_init(MqNotebook *notebook)
{
GtkWidget *new_tab_button;
GtkWidget *tab_list_button;
notebook->tree = MQ_TAB_TREE(mq_tree_insert_root_allocated(
MQ_TREE(g_new0(MqTabTree, 1)), NULL));
gtk_notebook_set_show_border(GTK_NOTEBOOK(notebook), FALSE);
gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
gtk_notebook_set_group_name(GTK_NOTEBOOK(notebook), "mq-tabs");
gtk_widget_set_can_focus(GTK_WIDGET(notebook), FALSE);
new_tab_button = gtk_button_new_from_icon_name("tab-new-symbolic",
GTK_ICON_SIZE_BUTTON);
gtk_button_set_relief(GTK_BUTTON(new_tab_button), GTK_RELIEF_NONE);
gtk_widget_set_tooltip_text(new_tab_button, "New tab");
gtk_notebook_set_action_widget(GTK_NOTEBOOK(notebook), new_tab_button,
GTK_PACK_START);
gtk_widget_show_all(new_tab_button);
g_signal_connect(new_tab_button, "clicked",
G_CALLBACK(new_tab_clicked_cb), notebook);
tab_list_button = gtk_button_new_from_icon_name("pan-down-symbolic",
GTK_ICON_SIZE_BUTTON);
gtk_button_set_relief(GTK_BUTTON(tab_list_button), GTK_RELIEF_NONE);
gtk_widget_set_tooltip_text(tab_list_button, "Tab list");
gtk_notebook_set_action_widget(GTK_NOTEBOOK(notebook), tab_list_button,
GTK_PACK_END);
gtk_widget_show_all(tab_list_button);
g_signal_connect(tab_list_button, "clicked",
G_CALLBACK(tab_list_clicked_cb), notebook);
g_signal_connect(notebook, "switch-page",
G_CALLBACK(switch_page_cb), NULL);
}
GtkWidget *
mq_notebook_new(MqWindow *window)
{
return g_object_new(MQ_TYPE_NOTEBOOK,
"window", window,
NULL);
}
static gboolean
find_node_compare(MqTree *node, gpointer user_data)
{
MqNotebook *notebook;
notebook = MQ_NOTEBOOK(user_data);
if (MQ_TAB_TREE(node)->page == notebook->find_page) {
notebook->found_node = MQ_TAB_TREE(node);
return MQ_TREE_STOP;
} else {
return MQ_TREE_CONTINUE;
}
}
static void
find_node(MqNotebook *notebook, MqTabPage *page)
{
notebook->find_page = page;
notebook->found_node = NULL;
mq_tree_foreach(MQ_TREE(notebook->tree), find_node_compare, notebook);
notebook->find_page = NULL;
g_assert(notebook->found_node);
}
static gboolean
update_position(MqTree *node, gpointer G_GNUC_UNUSED user_data)
{
mq_tab_page_set_position (MQ_TAB_TREE(node)->page, node->position);
mq_tab_label_set_position(MQ_TAB_TREE(node)->label, node->position);
return MQ_TREE_CONTINUE;
}
static void
insert_page(MqNotebook *notebook, MqTabTree *node, const gchar *uri)
{
node->page = mq_tab_page_new(notebook->window, uri);
node->label = mq_tab_page_get_label(node->page);
gtk_notebook_insert_page(GTK_NOTEBOOK(notebook),
GTK_WIDGET(node->page), GTK_WIDGET(node->label),
MQ_TREE(node)->position - 1);
gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(notebook),
GTK_WIDGET(node->page),
TRUE);
gtk_notebook_set_tab_detachable(GTK_NOTEBOOK(notebook),
GTK_WIDGET(node->page), TRUE);
gtk_widget_show_all(GTK_WIDGET(node->page));
gtk_widget_show_all(GTK_WIDGET(node->label));
mq_tree_foreach_from(MQ_TREE(node), update_position, NULL);
}
MqTabPage *
mq_notebook_insert_top(MqNotebook *notebook, const gchar *uri)
{
MqTabTree *node;
node = MQ_TAB_TREE(mq_tree_insert_child_allocated(
MQ_TREE(g_new0(MqTabTree, 1)),
MQ_TREE(notebook->tree),
NULL));
insert_page(notebook, node, uri);
return node->page;
}
MqTabPage *
mq_notebook_insert_sibling(MqNotebook *notebook, const gchar *uri,
MqTabPage *sibling)
{
MqTabTree *node;
g_assert(sibling);
find_node(notebook, sibling);
node = MQ_TAB_TREE(mq_tree_insert_sibling_allocated(
MQ_TREE(g_new0(MqTabTree, 1)),
MQ_TREE(notebook->found_node),
NULL));
insert_page(notebook, node, uri);
return node->page;
}
MqTabPage *
mq_notebook_insert_child(MqNotebook *notebook, const gchar *uri,
MqTabPage *child)
{
MqTabTree *node;
g_assert(child);
find_node(notebook, child);
node = MQ_TAB_TREE(mq_tree_insert_child_allocated(
MQ_TREE(g_new0(MqTabTree, 1)),
MQ_TREE(notebook->found_node),
NULL));
insert_page(notebook, node, uri);
return node->page;
}
gint
mq_notebook_get_n_pages(MqNotebook *notebook)
{
return mq_tree_size(MQ_TREE(notebook->tree)) - 1;
}
void
mq_notebook_set_current_page(MqNotebook *notebook, gint page_num)
{
MqTabTree *node;
node = MQ_TAB_TREE(mq_tree_seek(MQ_TREE(notebook->tree), page_num + 1));
notebook->current_page = node->page;
gtk_notebook_set_current_page(GTK_NOTEBOOK(notebook), page_num);
}
void
mq_notebook_update_tab_title(MqNotebook *notebook, MqTabPage *tab_page,
const gchar *title)
{
if (tab_page == notebook->current_page) {
mq_window_set_title(notebook->window, title);
}
}
|