/* * 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 "tab-chrome.h" static void back_clicked_cb(GtkButton __attribute__((unused)) *toolbutton, MqTabChrome *chrome) { webkit_web_view_go_back(chrome->web_view); } static void forward_clicked_cb(GtkButton __attribute__((unused)) *toolbutton, MqTabChrome *chrome) { webkit_web_view_go_forward(chrome->web_view); } static void back_forward_toggle_button_toggled_cb(GtkToggleButton *toggle_button, GtkStack *stack) { /* Use gtk_widget_show() and gtk_widget_hide() instead of * gtk_stack_set_visible_child() so that the stack fits the size of only * the visible child. */ gtk_widget_show(gtk_stack_get_child_by_name(stack, gtk_toggle_button_get_active(toggle_button) ? "text" : "list")); gtk_widget_hide(gtk_stack_get_child_by_name(stack, gtk_toggle_button_get_active(toggle_button) ? "list" : "text")); } static GtkWidget * back_forward_list_item_new(WebKitBackForwardListItem *list_item, gint type) { GtkWidget *label; GtkWidget *icon_stack; GtkWidget *box; label = gtk_label_new(webkit_back_forward_list_item_get_title( list_item)); gtk_widget_set_halign(label, GTK_ALIGN_START); icon_stack = gtk_stack_new(); switch (type) { case 0: gtk_stack_add_named(GTK_STACK(icon_stack), gtk_radio_button_new(NULL), "current"); gtk_stack_add_named(GTK_STACK(icon_stack), gtk_image_new_from_icon_name("go-previous", GTK_ICON_SIZE_BUTTON), "back"); break; case -1: gtk_stack_add_named(GTK_STACK(icon_stack), gtk_image_new_from_icon_name("go-previous", GTK_ICON_SIZE_BUTTON), "back"); gtk_stack_add_named(GTK_STACK(icon_stack), gtk_radio_button_new(NULL), "current"); break; case 1: gtk_stack_add_named(GTK_STACK(icon_stack), gtk_image_new_from_icon_name("go-next", GTK_ICON_SIZE_BUTTON), "forward"); gtk_stack_add_named(GTK_STACK(icon_stack), gtk_radio_button_new(NULL), "current"); break; } box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); gtk_box_pack_start(GTK_BOX(box), icon_stack, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(box), label, TRUE, TRUE, 0); gtk_widget_set_tooltip_text(box, webkit_back_forward_list_item_get_uri(list_item)); return box; } static void back_forward_list_box_row_activated_cb(GtkListBox __attribute__((unused)) *box, GtkListBoxRow *row, MqTabChrome *chrome) { webkit_web_view_go_to_back_forward_list_item(chrome->web_view, webkit_back_forward_list_get_nth_item( webkit_web_view_get_back_forward_list(chrome->web_view), gtk_list_box_row_get_index(row) - chrome->back_items)); gtk_widget_hide(chrome->back_forward_popover); } static gboolean back_forward_box_button_press_cb(GtkWidget *widget, GdkEvent *event, MqTabChrome *chrome) { WebKitBackForwardList *back_forward_list; GtkWidget *list_box; GtkWidget *text_view; GtkTextBuffer *text_buffer; GtkTextIter text_iter; GList *list_item; gchar *str; GtkWidget *stack; GtkWidget *toggle_button; GtkWidget *box; /* Make sure this is a mouse button press event. Either the middle or * right button is OK. */ if (event->type != GDK_BUTTON_PRESS) { return FALSE; } /* Get the back/forward list for the Web view. */ back_forward_list = webkit_web_view_get_back_forward_list( chrome->web_view); /* Set up the list box. */ list_box = gtk_list_box_new(); gtk_list_box_set_selection_mode(GTK_LIST_BOX(list_box), GTK_SELECTION_BROWSE); gtk_list_box_set_activate_on_single_click(GTK_LIST_BOX(list_box), TRUE); g_signal_connect(list_box, "row-activated", G_CALLBACK(back_forward_list_box_row_activated_cb), chrome); /* Set up the text view. */ text_view = gtk_text_view_new(); text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)); gtk_text_view_set_editable(GTK_TEXT_VIEW(text_view), FALSE); /* Insert the current item. */ gtk_list_box_insert(GTK_LIST_BOX(list_box), back_forward_list_item_new( webkit_back_forward_list_get_current_item( back_forward_list), 0), -1); gtk_list_box_select_row(GTK_LIST_BOX(list_box), gtk_list_box_get_row_at_index(GTK_LIST_BOX(list_box), 0)); gtk_text_buffer_get_start_iter(text_buffer, &text_iter); gtk_text_buffer_insert(text_buffer, &text_iter, webkit_back_forward_list_item_get_uri( webkit_back_forward_list_get_current_item( back_forward_list)), -1); /* Insert back list items. */ list_item = webkit_back_forward_list_get_back_list( back_forward_list); chrome->back_items = 0; for (; list_item; list_item = list_item->next) { gtk_list_box_insert(GTK_LIST_BOX(list_box), back_forward_list_item_new(list_item->data, -1), 0); gtk_text_buffer_get_start_iter(text_buffer, &text_iter); str = g_strdup_printf("%s\n", webkit_back_forward_list_item_get_uri(list_item->data)); gtk_text_buffer_insert(text_buffer, &text_iter, str, -1); g_free(str); ++chrome->back_items; } /* Insert forward list items. */ list_item = webkit_back_forward_list_get_forward_list( back_forward_list); for (; list_item; list_item = list_item->next) { gtk_list_box_insert(GTK_LIST_BOX(list_box), back_forward_list_item_new(list_item->data, 1), -1); gtk_text_buffer_get_end_iter(text_buffer, &text_iter); str = g_strdup_printf("\n%s", webkit_back_forward_list_item_get_uri(list_item->data)); gtk_text_buffer_insert(text_buffer, &text_iter, str, -1); g_free(str); } /* Set up the stack. */ stack = gtk_stack_new(); gtk_stack_add_named(GTK_STACK(stack), list_box, "list"); gtk_stack_add_named(GTK_STACK(stack), text_view, "text"); /* Set up the toggle button. */ toggle_button = gtk_toggle_button_new(); gtk_button_set_image(GTK_BUTTON(toggle_button), gtk_image_new_from_icon_name("edit-select-all", GTK_ICON_SIZE_SMALL_TOOLBAR)); gtk_widget_set_halign(toggle_button, GTK_ALIGN_START); g_signal_connect(toggle_button, "toggled", G_CALLBACK(back_forward_toggle_button_toggled_cb), GTK_STACK(stack)); /* Set up the containing box. */ box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); gtk_box_pack_start(GTK_BOX(box), toggle_button, TRUE, FALSE, 0); gtk_box_pack_start(GTK_BOX(box), stack, TRUE, FALSE, 0); /* Set up the popover. */ chrome->back_forward_popover = gtk_popover_new(widget); gtk_container_add(GTK_CONTAINER(chrome->back_forward_popover), box); /* NB: gtk_popover_popup() is new in GTK+ 3.22. */ gtk_widget_show_all(chrome->back_forward_popover); gtk_widget_hide(text_view); return FALSE; } static void stop_reload_clicked_cb(GtkToolButton __attribute__((unused)) *toolbutton, MqTabChrome *chrome) { if (webkit_web_view_is_loading(chrome->web_view)) { webkit_web_view_stop_loading(chrome->web_view); } else { webkit_web_view_reload(chrome->web_view); } } static void uri_activate_cb(GtkEntry *entry, MqTabChrome *chrome) { webkit_web_view_load_uri(chrome->web_view, gtk_entry_get_text(GTK_ENTRY(entry))); } static GtkWidget * navigation_toolbar_new(MqTabChrome *chrome, gchar *uri) { GtkToolbar *navigation_toolbar; GtkToolItem *back_forward_tool_item; GtkWidget *back_forward_event_box; GtkToolItem *uri_tool_item; navigation_toolbar = GTK_TOOLBAR(gtk_toolbar_new()); back_forward_tool_item = gtk_tool_item_new(); chrome->back_forward_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); /* Back button */ chrome->back_button = gtk_button_new_from_icon_name("go-previous", GTK_ICON_SIZE_SMALL_TOOLBAR); gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->back_button), "Go back one page"); g_signal_connect(chrome->back_button, "clicked", G_CALLBACK(back_clicked_cb), chrome); gtk_box_pack_start(GTK_BOX(chrome->back_forward_box), chrome->back_button, FALSE, FALSE, 0); /* Forward button */ chrome->forward_button = gtk_button_new_from_icon_name("go-next", GTK_ICON_SIZE_SMALL_TOOLBAR); gtk_widget_set_tooltip_text(GTK_WIDGET(chrome->forward_button), "Go forward one page"); g_signal_connect(chrome->forward_button, "clicked", G_CALLBACK(forward_clicked_cb), chrome); gtk_box_pack_start(GTK_BOX(chrome->back_forward_box), chrome->forward_button, FALSE, FALSE, 0); gtk_style_context_add_class( gtk_widget_get_style_context(chrome->back_forward_box), "linked"); back_forward_event_box = gtk_event_box_new(); g_signal_connect(back_forward_event_box, "button-press-event", G_CALLBACK(back_forward_box_button_press_cb), chrome); gtk_container_add(GTK_CONTAINER(back_forward_event_box), chrome->back_forward_box); 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(); 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)); gtk_widget_set_hexpand(GTK_WIDGET(navigation_toolbar), TRUE); chrome->load_failed = FALSE; return GTK_WIDGET(navigation_toolbar); } MqTabChrome * mq_tab_chrome_new(gchar *uri, GtkWidget *tab_image, GtkWidget *tab_label) { MqTabChrome *chrome; chrome = malloc(sizeof(*chrome)); chrome->tab_image = tab_image; chrome->tab_label = tab_label; chrome->container = gtk_grid_new(); gtk_grid_attach(GTK_GRID(chrome->container), navigation_toolbar_new(chrome, uri), 0, 0, 1, 1); return chrome; } GtkWidget * mq_tab_chrome_get_container(MqTabChrome *chrome) { return chrome->container; } static void update_tab_image(MqTabChrome *chrome) { gtk_image_set_from_surface(GTK_IMAGE(chrome->tab_image), webkit_web_view_get_favicon(chrome->web_view)); } static void update_tab_label(MqTabChrome *chrome) { gtk_label_set_text(GTK_LABEL(chrome->tab_label), g_strdup_printf("%d. %s", chrome->tab_position + 1, webkit_web_view_get_title(chrome->web_view))); } void mq_tab_chrome_update_tab_position(MqTabChrome *chrome, guint position) { chrome->tab_position = position; update_tab_label(chrome); } static void load_changed_cb(WebKitWebView *web_view, WebKitLoadEvent load_event, MqTabChrome *chrome) { switch (load_event) { case WEBKIT_LOAD_STARTED: case WEBKIT_LOAD_REDIRECTED: case WEBKIT_LOAD_COMMITTED: gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), webkit_web_view_get_uri(web_view)); 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; } gtk_widget_set_sensitive(GTK_WIDGET(chrome->back_button), webkit_web_view_can_go_back(web_view)); gtk_widget_set_sensitive(GTK_WIDGET(chrome->forward_button), webkit_web_view_can_go_forward(web_view)); } static gboolean load_failed_cb(WebKitWebView __attribute__((unused)) *web_view, WebKitLoadEvent __attribute__((unused)) load_event, gchar __attribute__((unused)) *failing_uri, GError __attribute__((unused)) *error, MqTabChrome *chrome) { chrome->load_failed = TRUE; return FALSE; } static void mouse_target_changed_cb(WebKitWebView __attribute__((unused)) *web_view, WebKitHitTestResult *hit_test_result, guint __attribute__((unused)) modifiers, MqTabChrome *chrome) { if (webkit_hit_test_result_context_is_link(hit_test_result)) { if (g_strcmp0(gtk_entry_get_text(GTK_ENTRY(chrome->uri_entry)), webkit_web_view_get_uri(web_view)) == 0) { gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), webkit_hit_test_result_get_link_uri( hit_test_result)); 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. */ gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), webkit_hit_test_result_get_link_uri( hit_test_result)); } } else { if (gtk_entry_get_attributes(GTK_ENTRY(chrome->uri_entry)) == chrome->hovered_link_style) { gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), webkit_web_view_get_uri(web_view)); gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry), NULL); } } } static void load_progress_cb(WebKitWebView *web_view, GParamSpec __attribute__((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(WebKitWebView *web_view, GParamSpec __attribute__((unused)) *paramspec, MqTabChrome *chrome) { gtk_entry_set_text(GTK_ENTRY(chrome->uri_entry), webkit_web_view_get_uri(web_view)); gtk_entry_set_attributes(GTK_ENTRY(chrome->uri_entry), NULL); } static void favicon_cb(WebKitWebView __attribute__((unused)) *web_view, GParamSpec __attribute__((unused)) *paramspec, MqTabChrome *chrome) { update_tab_image(chrome); } static void title_cb(WebKitWebView __attribute__((unused)) *web_view, GParamSpec __attribute__((unused)) *paramspec, MqTabChrome *chrome) { update_tab_label(chrome); } static void loading_cb(WebKitWebView *web_view, GParamSpec __attribute__((unused)) *paramspec, MqTabChrome *chrome) { if (webkit_web_view_is_loading(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 void connect_web_view(MqTabChrome *chrome) { 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::uri", G_CALLBACK(uri_cb), chrome); /* FIXME: Doesn't work? */ g_signal_connect(chrome->web_view, "notify::favicon", G_CALLBACK(favicon_cb), chrome); g_signal_connect(chrome->web_view, "notify::title", G_CALLBACK(title_cb), chrome); g_signal_connect(chrome->web_view, "notify::is-loading", G_CALLBACK(loading_cb), chrome); } void mq_tab_chrome_set_web_view(MqTabChrome *chrome, WebKitWebView *web_view) { chrome->web_view = web_view; connect_web_view(chrome); }