/*
* 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 .
*/
#include
#include
#include
#include
#include "tab-chrome.h"
#include "tab.h"
#include "back-forward-button-box.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
uri_activate_cb(GtkEntry *entry, MqTabChrome *chrome)
{
const gchar *uri;
uri = gtk_entry_get_text(GTK_ENTRY(entry));
mq_web_view_load_uri(chrome->web_view, uri);
}
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
load_changed_cb(MqWebView *web_view, WebKitLoadEvent load_event,
MqTabChrome *chrome)
{
const gchar *uri;
switch (load_event) {
case WEBKIT_LOAD_STARTED:
case WEBKIT_LOAD_REDIRECTED:
case WEBKIT_LOAD_COMMITTED:
uri = mq_web_view_get_uri(web_view);
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), uri);
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry),
NULL);
break;
case WEBKIT_LOAD_FINISHED:
gtk_entry_set_progress_fraction(
GTK_ENTRY(chrome->uri_entry), 0.0);
break;
}
}
static gboolean
load_failed_cb(WebKitWebView G_GNUC_UNUSED *web_view,
WebKitLoadEvent G_GNUC_UNUSED load_event,
gchar G_GNUC_UNUSED *failing_uri, GError G_GNUC_UNUSED *error,
MqTabChrome *chrome)
{
chrome->load_failed = TRUE;
return FALSE;
}
/* TODO: key in URI bar should reset to URI of current hovered link, if
* any, even if different from chrome->hovered_link_uri. */
static void
mouse_target_changed_cb(MqWebView G_GNUC_UNUSED *web_view,
WebKitHitTestResult *hit_test_result, guint G_GNUC_UNUSED modifiers,
MqTabChrome *chrome)
{
const gchar *uri;
uri = mq_web_view_get_uri(web_view);
if (webkit_hit_test_result_context_is_link(hit_test_result)) {
if (gtk_widget_has_focus(chrome->uri_entry)) {
} else if (chrome->hovered_link_uri &&
g_strcmp0(gtk_entry_get_text(
GTK_ENTRY(chrome->uri_entry)),
chrome->hovered_link_uri) != 0) {
/* The user has edited the hovered link URI in the URI
* bar. */
} else if (g_strcmp0(gtk_entry_get_text(
GTK_ENTRY(chrome->uri_entry)), uri) ==
0) {
g_free(chrome->hovered_link_uri);
chrome->hovered_link_uri = g_strdup(
webkit_hit_test_result_get_link_uri(
hit_test_result));
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
chrome->hovered_link_uri);
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry),
chrome->hovered_link_style);
} else if (gtk_entry_get_attributes(
GTK_ENTRY(chrome->uri_entry)) ==
chrome->hovered_link_style) {
/* The URI bar's text differs from the Web view's URI
* because the mouse was already targeting a different
* link. */
g_free(chrome->hovered_link_uri);
chrome->hovered_link_uri = g_strdup(
webkit_hit_test_result_get_link_uri(
hit_test_result));
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry),
chrome->hovered_link_uri);
}
} else if (gtk_entry_get_attributes(GTK_ENTRY(chrome->uri_entry)) ==
chrome->hovered_link_style) {
if (gtk_widget_has_focus(chrome->uri_entry)) {
} else if (g_strcmp0(gtk_entry_get_text(
GTK_ENTRY(chrome->uri_entry)),
chrome->hovered_link_uri) == 0) {
/* The user hasn't edited the hovered link URI in the
* URI bar. */
g_free(chrome->hovered_link_uri);
chrome->hovered_link_uri = NULL;
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), uri);
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry),
NULL);
}
}
}
static void
load_progress_cb(WebKitWebView *web_view, GParamSpec G_GNUC_UNUSED *paramspec,
MqTabChrome *chrome)
{
/*
* If loading fails, the WebKitWebView's "estimated-load-progress" is
* set to 1.0 after signals like "load-changed" and "load-failed" are
* emitted. So the only way to avoid leaving behind a full progress bar
* after, for example, canceling a page load is to save a flag on a
* failed load and only update the progress bar if the flag is unset.
*/
if (chrome->load_failed) {
chrome->load_failed = FALSE;
return;
}
gtk_entry_set_progress_fraction(GTK_ENTRY(chrome->uri_entry),
webkit_web_view_get_estimated_load_progress(web_view));
}
static void
uri_cb(MqWebView *web_view, GParamSpec G_GNUC_UNUSED *paramspec,
MqTabChrome *chrome)
{
const gchar *uri;
uri = mq_web_view_get_uri(web_view);
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), uri);
gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry), NULL);
}
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;
GtkWidget *back_forward_event_box;
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 = gtk_tool_item_new();
back_forward_event_box = mq_back_forward_button_box_new(chrome->tab,
chrome->web_view);
gtk_container_add(GTK_CONTAINER(back_forward_tool_item),
back_forward_event_box);
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 = gtk_tool_item_new();
chrome->uri_entry = gtk_entry_new();
if (uri) {
gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), uri);
}
gtk_entry_set_placeholder_text(GTK_ENTRY(chrome->uri_entry),
"URI...");
gtk_entry_set_icon_from_icon_name(GTK_ENTRY(chrome->uri_entry),
GTK_ENTRY_ICON_PRIMARY, "text-x-generic");
gtk_entry_set_progress_fraction(GTK_ENTRY(chrome->uri_entry), 0.0);
g_signal_connect(chrome->uri_entry, "activate",
G_CALLBACK(uri_activate_cb), chrome);
gtk_container_add(GTK_CONTAINER(uri_tool_item),
chrome->uri_entry);
gtk_tool_item_set_expand(uri_tool_item, TRUE);
gtk_toolbar_insert(navigation_toolbar, uri_tool_item, -1);
/* URI bar hovered link style */
chrome->hovered_link_style = pango_attr_list_new();
pango_attr_list_insert(chrome->hovered_link_style,
pango_attr_style_new(PANGO_STYLE_ITALIC));
/* 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);
chrome->load_failed = FALSE;
chrome->hovered_link_uri = NULL;
g_signal_connect(chrome->web_view, "load-changed",
G_CALLBACK(load_changed_cb), chrome);
g_signal_connect(chrome->web_view, "load-failed",
G_CALLBACK(load_failed_cb), chrome);
g_signal_connect(chrome->web_view, "mouse-target-changed",
G_CALLBACK(mouse_target_changed_cb), chrome);
g_signal_connect(chrome->web_view, "notify::estimated-load-progress",
G_CALLBACK(load_progress_cb), chrome);
g_signal_connect(chrome->web_view, "notify::rewritten-uri",
G_CALLBACK(uri_cb), chrome);
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;
}