summaryrefslogtreecommitdiffstats
path: root/src/tab-chrome.c
blob: faedcfdc081666ecaba89cec908c397e07f125c7 (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
/*
 * Tab chrome
 *
 * 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 <stdlib.h>
#include <string.h>

#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

#include "tab-chrome.h"
#include "tab.h"
#include "back-forward-button-box.h"
#include "uri-entry.h"
#include "main-menu.h"
#include "find-toolbar.h"

static void
stop_reload_clicked_cb(GtkToolButton G_GNUC_UNUSED *toolbutton,
	MqTabChrome *chrome)
{
	if (webkit_web_view_is_loading(WEBKIT_WEB_VIEW(chrome->web_view))) {
		webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(chrome->web_view));
	} else {
		webkit_web_view_reload(WEBKIT_WEB_VIEW(chrome->web_view));
	}
}

static void
home_clicked_cb(GtkToolButton G_GNUC_UNUSED *toolbutton,
	MqTabChrome *chrome)
{
	const gchar *uri;

	uri = mq_config_get_string(chrome->config, "tabs.home");

	mq_web_view_load_uri(chrome->web_view, uri);
}

static void
loading_cb(MqWebView *web_view, GParamSpec G_GNUC_UNUSED *paramspec,
	MqTabChrome *chrome)
{
	if (webkit_web_view_is_loading(WEBKIT_WEB_VIEW(web_view))) {
		gtk_tool_button_set_icon_widget(
			GTK_TOOL_BUTTON(chrome->stop_reload_button),
			chrome->stop_icon);
		gtk_tool_button_set_label(
			GTK_TOOL_BUTTON(chrome->stop_reload_button), "Stop");
		gtk_widget_set_tooltip_text(
			GTK_WIDGET(chrome->stop_reload_button),
			"Stop loading the current page");
	} else {
		gtk_tool_button_set_icon_widget(
			GTK_TOOL_BUTTON(chrome->stop_reload_button),
			chrome->reload_icon);
		gtk_tool_button_set_label(
			GTK_TOOL_BUTTON(chrome->stop_reload_button), "Reload");
		gtk_widget_set_tooltip_text(
			GTK_WIDGET(chrome->stop_reload_button),
			"Reload the current page");
	}
	gtk_widget_show_all(GTK_WIDGET(chrome->stop_reload_button));
}

static GtkWidget *
navigation_toolbar_new(MqTabChrome *chrome, const gchar *uri)
{
	GtkToolbar  *navigation_toolbar;
	GtkToolItem *back_forward_tool_item;
	GtkToolItem *uri_tool_item;
	GtkToolItem *home_button;
	GtkToolItem *menu_button;

	navigation_toolbar = GTK_TOOLBAR(gtk_toolbar_new());

	/* Back/forward button box */
	back_forward_tool_item = mq_back_forward_button_box_new(chrome->tab,
		chrome->web_view);
	gtk_toolbar_insert(navigation_toolbar, back_forward_tool_item, -1);

	/* Stop/reload button */
	chrome->stop_icon = gtk_image_new_from_icon_name("process-stop",
		GTK_ICON_SIZE_SMALL_TOOLBAR);
	g_object_ref_sink(chrome->stop_icon);
	chrome->reload_icon = gtk_image_new_from_icon_name("view-refresh",
		GTK_ICON_SIZE_SMALL_TOOLBAR);
	g_object_ref_sink(chrome->reload_icon);
	chrome->stop_reload_button = gtk_tool_button_new(chrome->stop_icon,
		"Stop");
	gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->stop_reload_button),
		"Stop loading the current page");
	g_signal_connect(chrome->stop_reload_button, "clicked",
		G_CALLBACK(stop_reload_clicked_cb), chrome);
	gtk_toolbar_insert(navigation_toolbar, chrome->stop_reload_button, -1);

	/* URI bar */
	uri_tool_item = mq_uri_entry_new(chrome->web_view, uri);
	gtk_toolbar_insert(navigation_toolbar, uri_tool_item, -1);

	/* Home button */
	home_button = gtk_tool_button_new(gtk_image_new_from_icon_name(
			"go-home", GTK_ICON_SIZE_SMALL_TOOLBAR), "Home");
	gtk_widget_set_tooltip_text(GTK_WIDGET(home_button),
		"Load the home page");
	g_signal_connect(home_button, "clicked",
		G_CALLBACK(home_clicked_cb), chrome);
	gtk_toolbar_insert(navigation_toolbar, home_button, -1);

	/* Menu button */
	menu_button = mq_main_menu_new(chrome->tab,
		chrome->find_toolbar, chrome->web_view);
	gtk_toolbar_insert(navigation_toolbar, menu_button, -1);

	gtk_widget_set_hexpand(GTK_WIDGET(navigation_toolbar), TRUE);

	g_signal_connect(chrome->web_view, "notify::is-loading",
		G_CALLBACK(loading_cb), chrome);

	return GTK_WIDGET(navigation_toolbar);
}

MqTabChrome *
mq_tab_chrome_new(MqTab *tab, MqFindToolbar *find_toolbar, MqWebView *web_view,
	const gchar *uri)
{
	MqTabChrome *chrome;

	chrome = malloc(sizeof(*chrome));
	chrome->config = mq_application_get_config(mq_tab_get_application(tab));
	chrome->tab = tab;
	chrome->find_toolbar = find_toolbar;
	chrome->web_view = web_view;

	chrome->container = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
	gtk_box_pack_start(GTK_BOX(chrome->container),
		navigation_toolbar_new(chrome, uri), FALSE, FALSE, 0);

	return chrome;
}

GtkWidget *
mq_tab_chrome_get_container(MqTabChrome *chrome)
{
	return chrome->container;
}