summaryrefslogtreecommitdiffstats
path: root/src/toolbars/navigation/stop-reload-button.c
blob: 3922b1c20b70886668597b9e4e210ccbda8beaf9 (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
/*
 * Stop/reload button
 *
 * 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 "stop-reload-button.h"

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

#include "../../i18n.h"
#include "../../web-view.h"

struct _MqStopReloadButton {
	GtkToolButton  parent_instance;
	GtkWidget     *stop_icon;
	GtkWidget     *reload_icon;
	MqWebView     *web_view;
};

enum {
	PROP_WEB_VIEW = 1,
	N_PROPERTIES
};

static GParamSpec *obj_properties[N_PROPERTIES] = {NULL,};

struct _MqStopReloadButtonClass {
	GtkToolButtonClass parent_class;
};

G_DEFINE_TYPE(MqStopReloadButton, mq_stop_reload_button, GTK_TYPE_TOOL_BUTTON)

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

static void
clicked_cb(MqStopReloadButton *stop_reload_button)
{
	WebKitWebView *web_view;

	web_view = WEBKIT_WEB_VIEW(stop_reload_button->web_view);

	if (webkit_web_view_is_loading(web_view)) {
		webkit_web_view_stop_loading(web_view);
	} else {
		webkit_web_view_reload(web_view);
	}
}

static void
set_web_view(MqStopReloadButton *stop_reload_button, MqWebView *web_view)
{
	stop_reload_button->web_view = web_view;

	g_signal_connect(web_view, "notify::is-loading",
		G_CALLBACK(loading_cb), stop_reload_button);
}

static void
finalize(GObject *object)
{
	MqStopReloadButton *stop_reload_button;

	stop_reload_button = MQ_STOP_RELOAD_BUTTON(object);

	g_object_unref(stop_reload_button->stop_icon);
	g_object_unref(stop_reload_button->reload_icon);

	G_OBJECT_CLASS(mq_stop_reload_button_parent_class)->finalize(object);
}

static void
get_property(GObject *object, guint property_id, GValue *value,
	GParamSpec *param_spec)
{
	switch (property_id) {
		case PROP_WEB_VIEW:
			g_value_set_object(value, NULL);
			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)
{
	MqStopReloadButton *stop_reload_button;

	stop_reload_button = MQ_STOP_RELOAD_BUTTON(object);

	switch (property_id) {
		case PROP_WEB_VIEW:
			set_web_view(stop_reload_button,
				g_value_get_object(value));
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(object, property_id,
				param_spec);
			break;
	}
}

static void
mq_stop_reload_button_class_init(MqStopReloadButtonClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->finalize     = finalize;
	object_class->get_property = get_property;
	object_class->set_property = set_property;

	obj_properties[PROP_WEB_VIEW] = g_param_spec_object(
		"web-view",
		P_("MqWebView"),
		P_("The associated MqWebView instance"),
		MQ_TYPE_WEB_VIEW,
		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
mq_stop_reload_button_init(MqStopReloadButton *stop_reload_button)
{
	/* Stop icon */
	stop_reload_button->stop_icon = gtk_image_new_from_icon_name(
		"process-stop", GTK_ICON_SIZE_SMALL_TOOLBAR);
	g_object_ref_sink(stop_reload_button->stop_icon);

	/* Reload icon */
	stop_reload_button->reload_icon = gtk_image_new_from_icon_name(
		"view-refresh", GTK_ICON_SIZE_SMALL_TOOLBAR);
	g_object_ref_sink(stop_reload_button->reload_icon);

	/* Button */
	gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(stop_reload_button),
		stop_reload_button->stop_icon);
	gtk_tool_button_set_label(GTK_TOOL_BUTTON(stop_reload_button),
		_("Stop"));
	gtk_widget_set_can_focus(gtk_bin_get_child(GTK_BIN(stop_reload_button)),
		FALSE);
	gtk_widget_set_tooltip_text(GTK_WIDGET(stop_reload_button),
		_("Stop loading the current page"));
	g_signal_connect(stop_reload_button, "clicked",
		G_CALLBACK(clicked_cb), NULL);
}

GtkToolItem *
mq_stop_reload_button_new(MqWebView *web_view)
{
	return g_object_new(MQ_TYPE_STOP_RELOAD_BUTTON,
		"web-view", web_view,
		NULL);
}