summaryrefslogtreecommitdiff
path: root/frontends/gtk/tabs.c
blob: 4fa109b70f3c68d94fc9b4c1381e764ad1020e37 (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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * Copyright 2008 Michael Lester <element3260@gmail.com>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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 <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <string.h>

#include "utils/nsoption.h"
#include "utils/log.h"
#include "netsurf/browser_window.h"
#include "desktop/search.h"

#include "gtk/compat.h"
#include "gtk/toolbar_items.h"
#include "gtk/scaffolding.h"
#include "gtk/window.h"
#include "gtk/search.h"
#include "gtk/tabs.h"

#define TAB_WIDTH_N_CHARS 15

static gint srcpagenum;

/**
 * callback to update sizes when style-set gtk signal
 */
static void
nsgtk_tab_update_size(GtkWidget *hbox,
		      GtkStyle *previous_style,
		      GtkWidget *close_button)
{
	PangoFontMetrics *metrics;
	PangoContext *context;
	int char_width, h, w;
	GtkStyleContext *style;
	GtkStateFlags state;

	state = nsgtk_widget_get_state_flags(hbox);
	style = nsgtk_widget_get_style_context(hbox);

	context = gtk_widget_get_pango_context(hbox);
	metrics = pango_context_get_metrics(context,
				nsgtk_style_context_get_font(style, state),
				pango_context_get_language(context));

	char_width = pango_font_metrics_get_approximate_digit_width(metrics);
	pango_font_metrics_unref(metrics);

	nsgtk_icon_size_lookup_for_settings(gtk_widget_get_settings (hbox),
			GTK_ICON_SIZE_MENU, &w, &h);

	gtk_widget_set_size_request(hbox,
			TAB_WIDTH_N_CHARS * PANGO_PIXELS(char_width) + 2 * w,
			-1);

	gtk_widget_set_size_request(close_button, w + 4, h + 4);
}


/**
 * gtk event handler for button release on tab hbox
 */
static gboolean
nsgtk_tab_button_release(GtkWidget *widget,
			 GdkEventButton *event,
			 gpointer user_data)
{
	GtkWidget *page;

	if ((event->type == GDK_BUTTON_RELEASE) && (event->button == 2)) {
		page = (GtkWidget *)user_data;
		gtk_widget_destroy(page);
		return TRUE;
	}
	return FALSE;
}


/**
 * Create a notebook tab label
 *
 * \param page The page content widget
 * \param title The title of the page
 * \param icon_pixbuf The icon of the page
 */
static GtkWidget *
nsgtk_tab_label_setup(GtkWidget *page,
		      const char *title,
		      GdkPixbuf *icon_pixbuf)
{
	GtkWidget *ebox, *hbox, *favicon, *label, *button, *close;

	/* horizontal box */
	hbox = nsgtk_hbox_new(FALSE, 3);

	/* event box */
	ebox = gtk_event_box_new();
	gtk_widget_set_events(ebox, GDK_BUTTON_PRESS_MASK);
	gtk_container_add(GTK_CONTAINER(ebox), hbox);

	/* construct a favicon */
	favicon = gtk_image_new();
	if (icon_pixbuf != NULL) {
		gtk_image_set_from_pixbuf(GTK_IMAGE(favicon), icon_pixbuf);
	}

	/* construct a label */
	label = gtk_label_new(title);
	gtk_label_set_ellipsize(GTK_LABEL(label), PANGO_ELLIPSIZE_END);
	gtk_label_set_single_line_mode(GTK_LABEL(label), TRUE);
	nsgtk_widget_set_alignment(label, GTK_ALIGN_START, GTK_ALIGN_CENTER);
	nsgtk_widget_set_margins(label, 0, 0);
	gtk_widget_show(label);

	/* construct a close button  */
	button = gtk_button_new();

	close = nsgtk_image_new_from_stock(NSGTK_STOCK_CLOSE,
					   GTK_ICON_SIZE_LARGE_TOOLBAR);
	gtk_container_add(GTK_CONTAINER(button), close);
	nsgtk_button_set_focus_on_click(GTK_BUTTON(button), FALSE);
	gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE);
	gtk_widget_set_tooltip_text(button, "Close this tab.");

	/* pack the widgets into the label box */
	gtk_box_pack_start(GTK_BOX(hbox), favicon, FALSE, FALSE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);

	/* make the icon and label widgets findable by name */
	g_object_set_data(G_OBJECT(ebox), "favicon", favicon);
	g_object_set_data(G_OBJECT(ebox), "label", label);

	/* attach signal handlers */
	g_signal_connect_swapped(button,
				 "clicked",
				 G_CALLBACK(gtk_widget_destroy), page);

	g_signal_connect(hbox,
			 "style-set",
			 G_CALLBACK(nsgtk_tab_update_size),
			 button);

	g_signal_connect(ebox,
			 "button-release-event",
			 G_CALLBACK(nsgtk_tab_button_release),
			 page);


	gtk_widget_show_all(ebox);

	return ebox;
}


/**
 * The before switch-page gtk signal handler
 *
 * This signal is handled both before and after delivery to work round
 * issue that setting the selected tab during the switch-page signal
 * fails
 *
 * \param notebook The notebook being changed
 * \param page The notebook page being switched to
 * \param selpagenum The currently selected page number
 * \param user_data Unused
 */
static void
nsgtk_tab_switch_page(GtkNotebook *notebook,
		      GtkWidget *page,
		      guint selpagenum,
		      gpointer user_data)
{
	srcpagenum = gtk_notebook_get_current_page(notebook);
}


/**
 * The after switch-page gtk signal handler
 *
 * \param notebook The notebook being changed
 * \param selpage The notebook page selected
 * \param selpagenum The currently selected page number
 * \param user_data Unused
 */
static void
nsgtk_tab_switch_page_after(GtkNotebook *notebook,
			    GtkWidget *selpage,
			    guint selpagenum,
			    gpointer user_data)
{
	GtkWidget *srcpage;
	GtkWidget *addpage;
	GtkMenuBar *menubar;
	struct gui_window *gw = NULL;
	nserror res = NSERROR_INVALID;

	addpage = g_object_get_data(G_OBJECT(notebook), "addtab");

	/* check if trying to select the "add page" tab */
	if (selpage != addpage) {
		NSLOG(netsurf, INFO, "sel %d", selpagenum);
		menubar = nsgtk_scaffolding_menu_bar(nsgtk_scaffolding_from_notebook(notebook));
		gw = g_object_get_data(G_OBJECT(selpage), "gui_window");
		if (gw != NULL) {
			/* tab with web page in it */
			nsgtk_scaffolding_set_top_level(gw);
			gtk_widget_show(GTK_WIDGET(addpage));
			gtk_widget_set_sensitive(GTK_WIDGET(menubar), true);
		} else {
			/* tab with non browser content (e.g. tb customize) */
			gtk_widget_hide(GTK_WIDGET(addpage));
			gtk_widget_set_sensitive(GTK_WIDGET(menubar), false);
		}
		return;
	}

	NSLOG(netsurf, INFO, "src %d sel %d", srcpagenum, selpagenum);

	/* ensure the add tab is not already selected */
	if ((srcpagenum == -1) || (srcpagenum == (gint)selpagenum)) {
		return;
	}

	srcpage = gtk_notebook_get_nth_page(notebook, srcpagenum);

	gw = g_object_get_data(G_OBJECT(srcpage), "gui_window");

	if (gw != NULL) {
		res = nsgtk_window_item_activate(gw, NEWTAB_BUTTON);
	}
	if (res != NSERROR_OK) {
		NSLOG(netsurf, INFO, "Failed to open new tab.");
	}
}


/**
 * The tab reordered gtk signal handler
 *
 * \param notebook The notebook being changed
 * \param page_num The currently selected page number
 * \param user_data Unused
 */
static void
nsgtk_tab_page_reordered(GtkNotebook *notebook,
			 GtkWidget *child,
			 guint page_num,
			 gpointer user_data)
{
	gint pages;
	GtkWidget *addpage;

	pages = gtk_notebook_get_n_pages(notebook);
	addpage = g_object_get_data(G_OBJECT(notebook), "addtab");

	if (((gint)page_num == (pages - 1)) &&
	    (child != addpage)) {
		/* moved tab to end */
		gtk_notebook_reorder_child(notebook, addpage, -1);
	}
}

/**
 * The tab orientation signal handler
 *
 * \param notebook The notebook being changed
 * \param page_num The currently selected page number
 * \param user_data Unused
 */
static void
nsgtk_tab_orientation(GtkNotebook *notebook)
{
	switch (nsoption_int(position_tab)) {
	case 0:
		gtk_notebook_set_tab_pos(notebook, GTK_POS_TOP);
		break;

	case 1:
		gtk_notebook_set_tab_pos(notebook, GTK_POS_LEFT);
		break;

	case 2:
		gtk_notebook_set_tab_pos(notebook, GTK_POS_RIGHT);
		break;

	case 3:
		gtk_notebook_set_tab_pos(notebook, GTK_POS_BOTTOM);
		break;

	}
}

/**
 * adds a "new tab" tab
 */
static GtkWidget *
nsgtk_tab_add_newtab(GtkNotebook *notebook)
{
	GtkWidget *tablabel;
	GtkWidget *tabcontents;
	GtkWidget *add;

	tablabel = nsgtk_hbox_new(FALSE, 1);
	tabcontents = nsgtk_hbox_new(FALSE, 1);

	add = gtk_image_new_from_icon_name(NSGTK_STOCK_ADD,
					   GTK_ICON_SIZE_LARGE_TOOLBAR);
	gtk_widget_set_tooltip_text(add, "New Tab");

	gtk_box_pack_start(GTK_BOX(tablabel), add, FALSE, FALSE, 0);

	gtk_widget_show_all(tablabel);

	gtk_notebook_append_page(notebook, tabcontents, tablabel);

	gtk_notebook_set_tab_reorderable(notebook, tabcontents, false);

	gtk_widget_show_all(tabcontents);

	g_object_set_data(G_OBJECT(notebook), "addtab", tabcontents);

	return tablabel;
}


/**
 * callback to alter tab visibility when pages are added or removed
 */
static void
nsgtk_tab_visibility_update(GtkNotebook *notebook, GtkWidget *child, guint page)
{
	gint pagec;
	GtkWidget *addpage;

	pagec = gtk_notebook_get_n_pages(notebook);
	if (pagec > 1) {
		addpage = g_object_get_data(G_OBJECT(notebook), "addtab");
		if (addpage != NULL) {
			pagec--; /* skip the add tab */
			if ((gint)page == pagec) {
				/* ensure the add new tab cannot be current */
				gtk_notebook_set_current_page(notebook,
							      page - 1);
			}
		}
	}

	if ((nsoption_bool(show_single_tab) == true) || (pagec > 1)) {
		gtk_notebook_set_show_tabs(notebook, TRUE);
	} else {
		gtk_notebook_set_show_tabs(notebook, FALSE);
	}
}


/* exported interface documented in gtk/tabs.h */
void nsgtk_tab_options_changed(GtkNotebook *notebook)
{
	nsgtk_tab_orientation(notebook);
	nsgtk_tab_visibility_update(notebook, NULL, 0);
}


/* exported interface documented in gtk/tabs.h */
nserror nsgtk_notebook_create(GtkBuilder *builder, GtkNotebook **notebook_out)
{
	GtkNotebook *notebook;

	notebook = GTK_NOTEBOOK(gtk_builder_get_object(builder, "notebook"));

	nsgtk_tab_add_newtab(notebook);

	g_signal_connect(notebook,
			 "switch-page",
			 G_CALLBACK(nsgtk_tab_switch_page),
			 NULL);
	g_signal_connect_after(notebook,
			       "switch-page",
			       G_CALLBACK(nsgtk_tab_switch_page_after),
			       NULL);
	g_signal_connect(notebook,
			 "page-removed",
			 G_CALLBACK(nsgtk_tab_visibility_update),
			 NULL);
	g_signal_connect(notebook,
			 "page-added",
			 G_CALLBACK(nsgtk_tab_visibility_update),
			 NULL);
	g_signal_connect(notebook,
			 "page-reordered",
			 G_CALLBACK(nsgtk_tab_page_reordered),
			 NULL);

	nsgtk_tab_options_changed(notebook);

	*notebook_out = notebook;

	return NSERROR_OK;
}

/* exported interface documented in gtk/tabs.h */
nserror
nsgtk_tab_add_page(GtkNotebook *notebook,
		   GtkWidget *tab_contents,
		   bool background,
		   const char *title,
		   GdkPixbuf *icon_pixbuf)
{
	GtkWidget *tabBox;
	gint remember;
	gint pages;
	gint newpage;

	tabBox = nsgtk_tab_label_setup(tab_contents, title, icon_pixbuf);

	remember = gtk_notebook_get_current_page(notebook);

	pages = gtk_notebook_get_n_pages(notebook);

	newpage = gtk_notebook_insert_page(notebook, tab_contents, tabBox, pages - 1);

	gtk_notebook_set_tab_reorderable(notebook, tab_contents, true);

	gtk_widget_show_all(tab_contents);

	if (background) {
		gtk_notebook_set_current_page(notebook, remember);
	} else {
		gtk_notebook_set_current_page(notebook, newpage);
	}

	return NSERROR_OK;
}


/* exported interface documented in gtk/tabs.h */
void nsgtk_tab_add(struct gui_window *gw,
		   GtkWidget *tab_contents,
		   bool background,
		   const char *title,
		   GdkPixbuf *icon_pixbuf)
{
	GtkNotebook *notebook;

	g_object_set_data(G_OBJECT(tab_contents), "gui_window", gw);

	notebook = nsgtk_scaffolding_notebook(nsgtk_get_scaffold(gw));

	nsgtk_tab_add_page(notebook, tab_contents, background, title, icon_pixbuf);

}


/* exported interface documented in gtk/tabs.h */
nserror nsgtk_tab_set_icon(GtkWidget *page, GdkPixbuf *pixbuf)
{
	GtkImage *favicon;
	GtkWidget *tab_label;
	GtkNotebook *notebook;

	if (pixbuf == NULL) {
		return NSERROR_INVALID;
	}
	notebook = GTK_NOTEBOOK(gtk_widget_get_ancestor(page, GTK_TYPE_NOTEBOOK));
	if (notebook == NULL) {
		return NSERROR_BAD_PARAMETER;
	}

	tab_label = gtk_notebook_get_tab_label(notebook, page);
	if (tab_label == NULL) {
		return NSERROR_INVALID;
	}

	favicon = GTK_IMAGE(g_object_get_data(G_OBJECT(tab_label), "favicon"));

	gtk_image_set_from_pixbuf(favicon, pixbuf);

	return NSERROR_OK;
}


/* exported interface documented in gtk/tabs.h */
nserror nsgtk_tab_set_title(GtkWidget *page, const char *title)
{
	GtkLabel *label;
	GtkWidget *tab_label;
	GtkNotebook *notebook;

	if (title == NULL) {
		return NSERROR_INVALID;
	}

	notebook = GTK_NOTEBOOK(gtk_widget_get_ancestor(page, GTK_TYPE_NOTEBOOK));
	if (notebook == NULL) {
		return NSERROR_BAD_PARAMETER;
	}

	tab_label = gtk_notebook_get_tab_label(notebook, page);
	if (tab_label == NULL) {
		return NSERROR_INVALID;
	}

	label = GTK_LABEL(g_object_get_data(G_OBJECT(tab_label), "label"));

	gtk_label_set_text(label, title);
	gtk_widget_set_tooltip_text(tab_label, title);

	return NSERROR_OK;
}

/* exported interface documented in gtk/tabs.h */
nserror nsgtk_tab_close_current(GtkNotebook *notebook)
{
	gint pagen;
	GtkWidget *page;
	struct gui_window *gw;
	GtkWidget *addpage;

	pagen = gtk_notebook_get_current_page(notebook);
	if (pagen == -1) {
		return NSERROR_OK;
	}

	page = gtk_notebook_get_nth_page(notebook, pagen);
	if (page == NULL) {
		return NSERROR_OK;
	}

	addpage = g_object_get_data(G_OBJECT(notebook), "addtab");
	if (page == addpage) {
		/* the add new tab page is current, cannot close that */
		return NSERROR_OK;
	}

	gw = g_object_get_data(G_OBJECT(page), "gui_window");
	if (gw == NULL) {
		return NSERROR_OK;
	}

	nsgtk_window_destroy_browser(gw);

	return NSERROR_OK;
}

nserror nsgtk_tab_prev(GtkNotebook *notebook)
{
	gtk_notebook_prev_page(notebook);

	return NSERROR_OK;

}

nserror nsgtk_tab_next(GtkNotebook *notebook)
{
	gint pagen;
	GtkWidget *page;
	GtkWidget *addpage;

	pagen = gtk_notebook_get_current_page(notebook);
	if (pagen == -1) {
		return NSERROR_OK;
	}

	page = gtk_notebook_get_nth_page(notebook, pagen + 1);
	if (page == NULL) {
		return NSERROR_OK;
	}

	addpage = g_object_get_data(G_OBJECT(notebook), "addtab");
	if (page == addpage) {
		/* cannot make add new tab page current */
		return NSERROR_OK;
	}

	gtk_notebook_set_current_page(notebook, pagen + 1);

	return NSERROR_OK;
}