Skip to content

Commit

Permalink
Merge pull request #1295 from b4n/webhelper/stuff
Browse files Browse the repository at this point in the history
Various WebHelper improvements

* Improve documentation
* Add support for webkit2gtk-4.1
* Add a key binding & menu item for loading the current file into the
  web view (for e.g. static HTML pages)
* Cleanup use of deprecated library symbols
  • Loading branch information
b4n committed Apr 27, 2024
2 parents 2b897dc + 8b17184 commit 8da1471
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 28 deletions.
20 changes: 14 additions & 6 deletions build/webhelper.m4
Expand Up @@ -2,12 +2,11 @@ AC_DEFUN([GP_CHECK_WEBHELPER],
[
GP_ARG_DISABLE([WebHelper], [auto])
GTK_VERSION=2.16
dnl 2.22 for glib-mkenums' @basename@ template
GLIB_VERSION=2.22
GIO_VERSION=2.18
GTK_VERSION=3.0
GLIB_VERSION=2.38
GIO_VERSION=2.30
GDK_PIXBUF_VERSION=2.0
WEBKIT_VERSION=1.1.18
WEBKIT_VERSION=2.18
AC_PATH_PROG([GLIB_MKENUMS], [glib-mkenums], [no])
AC_SUBST([GLIB_MKENUMS])
Expand All @@ -19,13 +18,22 @@ AC_DEFUN([GP_CHECK_WEBHELPER],
fi
fi
dnl Support both webkit2gtk 4.0 and 4.1, as the only difference is the
dnl libsoup version in the API, which we don't use.
dnl Prefer the 4.1 version, but use the 4.0 version as fallback if
dnl available -- yet still ask for the 4.1 if neither are available
webkit_package=webkit2gtk-4.1
PKG_CHECK_EXISTS([${webkit_package} >= ${WEBKIT_VERSION}],,
[PKG_CHECK_EXISTS([webkit2gtk-4.0 >= ${WEBKIT_VERSION}],
[webkit_package=webkit2gtk-4.0])])
GP_CHECK_PLUGIN_GTK3_ONLY([webhelper])
GP_CHECK_PLUGIN_DEPS([WebHelper], [WEBHELPER],
[$GP_GTK_PACKAGE >= ${GTK_VERSION}
glib-2.0 >= ${GLIB_VERSION}
gio-2.0 >= ${GIO_VERSION}
gdk-pixbuf-2.0 >= ${GDK_PIXBUF_VERSION}
webkit2gtk-4.0 >= ${WEBKIT_VERSION}
$webkit_package >= ${WEBKIT_VERSION}
gthread-2.0])
Expand Down
18 changes: 15 additions & 3 deletions webhelper/README
Expand Up @@ -26,9 +26,9 @@ Prominent features
Requirements
============

This plugin requires GTK+ (>= 2.16), GLib (>= 2.16), GIO (>= 2.18),
GdkPixbuf (>= 2.0), WebKitGTK (>= 1.1.18), and obviously Geany (>= 0.20) to
work. If you intend to build it yourself, you will need to get the development
This plugin requires GTK3, GLib (>= 2.38), GIO (>= 2.30), GdkPixbuf,
WebKit2GTK (either API 4.0 or 4.1), and obviously Geany (>= 1.25) to work.
If you intend to build it yourself, you will need to get the development
files of these libraries. You can find these packages at http://www.geany.org/,
http://www.gtk.org/ and http://www.webkitgtk.org/.

Expand All @@ -47,6 +47,18 @@ When loaded into Geany, this plugins adds a web view in the message window
(the default), the sidebar or in a separate window. You can find most of the
features from this view.

The web view displays the URL chosen in the address bar. This can be any valid
URL that would work in a web browser, for example it could be a local web
server like ``http://localhost/test/index.php``, a local file like
``file:///home/user/projects/test/index.html``, or a remote website like
``https://en.wikipedia.org/wiki/``.

In addition to a rendered preview, the main feature is the inspector. To
inspect the displayed page, either open the inspector using the toolbar item on
the far end of the address bar, or right click on any element of the page and
select *Inspect Element*. For more documentation on the inspector's features,
see https://webkit.org/web-inspector/.

Bookmarks
---------

Expand Down
82 changes: 77 additions & 5 deletions webhelper/src/gwh-browser.c
Expand Up @@ -31,6 +31,8 @@
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

#include <document.h>

#include "gwh-utils.h"
#include "gwh-settings.h"
#include "gwh-keybindings.h"
Expand Down Expand Up @@ -76,7 +78,7 @@ static const gdouble zoom_in_factor = 1.2;
static const gdouble zoom_out_factor = 1.0 / 1.2;


G_DEFINE_TYPE (GwhBrowser, gwh_browser, GTK_TYPE_VBOX)
G_DEFINE_TYPE_WITH_PRIVATE (GwhBrowser, gwh_browser, GTK_TYPE_BOX)


static void
Expand Down Expand Up @@ -225,6 +227,27 @@ on_item_bookmark_toggled (GtkCheckMenuItem *item,
}
}

static void
item_show_accelerator (GtkWidget *item,
gsize key_id)
{
GeanyKeyBinding *binding = keybindings_get_item (gwh_keybindings_get_group (),
key_id);

if (binding->key) {
/* we need an accel group for setting the accelerator, but we can't get
* Geany's. It doesn't matter though, as this is only for showing the
* accelarator, not actually for tiggering the item. */
GtkAccelGroup *dummy_accel_group = gtk_accel_group_new ();

gtk_widget_add_accelerator (item, "activate", dummy_accel_group,
binding->key, binding->mods,
GTK_ACCEL_VISIBLE);
g_object_set_data_full (G_OBJECT (item), "dummy_accel_group",
dummy_accel_group, g_object_unref);
}
}

static void
on_url_entry_icon_press (GtkEntry *entry,
GtkEntryIconPosition icon_pos,
Expand All @@ -243,12 +266,43 @@ on_url_entry_icon_press (GtkEntry *entry,
G_CALLBACK (on_item_bookmark_toggled), self);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show (item);
item_show_accelerator (item, GWH_KB_TOGGLE_BOOKMARK);

gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
event->button, event->time);
}
}

static void
on_item_load_current_file_activate (GtkMenuItem *item,
GwhBrowser *self)
{
gwh_browser_set_uri_from_document (self, document_get_current ());
}

static void
on_url_entry_populate_popup (GtkEntry *entry,
GtkWidget *menu,
GwhBrowser *self)
{
GtkWidget *item;
GeanyDocument *doc = document_get_current ();

/* separator */
item = gtk_separator_menu_item_new ();
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show (item);

/* load current file item */
item = gtk_menu_item_new_with_mnemonic (_("_Load current file"));
gtk_widget_set_sensitive (item, doc && doc->real_path);
g_signal_connect (item, "activate",
G_CALLBACK (on_item_load_current_file_activate), self);
gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
gtk_widget_show (item);
item_show_accelerator (item, GWH_KB_LOAD_CURRENT_FILE);
}

static gboolean
on_entry_completion_match_selected (GtkEntryCompletion *comp,
GtkTreeModel *model,
Expand Down Expand Up @@ -631,8 +685,6 @@ gwh_browser_class_init (GwhBrowserClass *klass)
"The browser's toolbar",
GTK_TYPE_TOOLBAR,
G_PARAM_READABLE));

g_type_class_add_private (klass, sizeof (GwhBrowserPrivate));
}

/* a GtkEntryCompletionMatchFunc matching anywhere in the haystack */
Expand Down Expand Up @@ -751,6 +803,8 @@ create_toolbar (GwhBrowser *self)
G_CALLBACK (on_url_entry_activate), self);
g_signal_connect (G_OBJECT (self->priv->url_entry), "icon-press",
G_CALLBACK (on_url_entry_icon_press), self);
g_signal_connect (G_OBJECT (self->priv->url_entry), "populate-popup",
G_CALLBACK (on_url_entry_populate_popup), self);
g_signal_connect (G_OBJECT (self->priv->url_combo), "notify::active",
G_CALLBACK (on_url_combo_active_notify), self);
g_signal_connect (G_OBJECT (comp), "match-selected",
Expand Down Expand Up @@ -837,8 +891,9 @@ gwh_browser_init (GwhBrowser *self)
WebKitWebContext *wkcontext;
gboolean inspector_detached;

self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GWH_TYPE_BROWSER,
GwhBrowserPrivate);
self->priv = gwh_browser_get_instance_private (self);

gtk_orientable_set_orientation (GTK_ORIENTABLE (self), GTK_ORIENTATION_VERTICAL);

self->priv->default_icon = NULL;
/* web view need to be created first because we use it in create_toolbar() */
Expand Down Expand Up @@ -950,6 +1005,23 @@ gwh_browser_set_uri (GwhBrowser *self,
g_free (real_uri);
}

gboolean
gwh_browser_set_uri_from_document (GwhBrowser *self,
GeanyDocument *doc)
{
gchar *uri;

/* document must exist on disk */
if (! doc || ! doc->real_path)
return FALSE;

uri = g_strconcat ("file://", doc->file_name, NULL);
gwh_browser_set_uri (self, uri);
g_free (uri);

return TRUE;
}

const gchar *
gwh_browser_get_uri (GwhBrowser *self)
{
Expand Down
5 changes: 5 additions & 0 deletions webhelper/src/gwh-browser.h
Expand Up @@ -24,6 +24,8 @@
#include <gtk/gtk.h>
#include <webkit2/webkit2.h>

#include <document.h>

G_BEGIN_DECLS


Expand Down Expand Up @@ -69,6 +71,9 @@ G_GNUC_INTERNAL
void gwh_browser_set_uri (GwhBrowser *self,
const gchar *uri);
G_GNUC_INTERNAL
gboolean gwh_browser_set_uri_from_document (GwhBrowser *self,
GeanyDocument *doc);
G_GNUC_INTERNAL
const gchar *gwh_browser_get_uri (GwhBrowser *self);
G_GNUC_INTERNAL
GtkToolbar *gwh_browser_get_toolbar (GwhBrowser *self);
Expand Down
1 change: 1 addition & 0 deletions webhelper/src/gwh-keybindings.h
Expand Up @@ -33,6 +33,7 @@ enum {
GWH_KB_TOGGLE_INSPECTOR,
GWH_KB_SHOW_HIDE_SEPARATE_WINDOW,
GWH_KB_TOGGLE_BOOKMARK,
GWH_KB_LOAD_CURRENT_FILE,
GWH_KB_COUNT
};

Expand Down
21 changes: 13 additions & 8 deletions webhelper/src/gwh-plugin.c
Expand Up @@ -300,6 +300,13 @@ on_kb_toggle_bookmark (guint key_id)
}
}

static void
on_kb_load_current_file (guint key_id)
{
gwh_browser_set_uri_from_document (GWH_BROWSER (G_browser),
document_get_current ());
}


static gchar *
get_config_filename (void)
Expand Down Expand Up @@ -417,11 +424,6 @@ plugin_init (GeanyData *data)
* (g_quark_from_static_string() for example) so it's not safe to remove it */
plugin_module_make_resident (geany_plugin);

/* webkit uses threads but don't initialize the thread system */
if (! g_thread_supported ()) {
g_thread_init (NULL);
}

load_config ();
gwh_keybindings_init ();

Expand Down Expand Up @@ -460,6 +462,9 @@ plugin_init (GeanyData *data)
keybindings_set_item (gwh_keybindings_get_group (), GWH_KB_TOGGLE_BOOKMARK,
on_kb_toggle_bookmark, 0, 0, "toggle_bookmark",
_("Toggle bookmark for the current website"), NULL);
keybindings_set_item (gwh_keybindings_get_group (), GWH_KB_LOAD_CURRENT_FILE,
on_kb_load_current_file, 0, 0, "load_current_file",
_("Load the current file in the web view"), NULL);
}

void
Expand Down Expand Up @@ -522,11 +527,11 @@ plugin_configure (GtkDialog *dialog)
cdialog = g_malloc (sizeof *cdialog);

/* Top-level box, containing the different frames */
box1 = gtk_vbox_new (FALSE, 12);
box1 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);

/* Browser */
gtk_box_pack_start (GTK_BOX (box1), ui_frame_new_with_alignment (_("Browser"), &alignment), FALSE, FALSE, 0);
box = gtk_vbox_new (FALSE, 0);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (alignment), box);
/* browser position */
cdialog->browser_position = gwh_settings_widget_new (G_settings, "browser-position");
Expand All @@ -538,7 +543,7 @@ plugin_configure (GtkDialog *dialog)

/* Windows */
gtk_box_pack_start (GTK_BOX (box1), ui_frame_new_with_alignment (_("Windows"), &alignment), FALSE, FALSE, 0);
box = gtk_vbox_new (FALSE, 0);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
gtk_container_add (GTK_CONTAINER (alignment), box);
/* skip taskbar */
cdialog->secondary_windows_skip_taskbar = gwh_settings_widget_new (G_settings,
Expand Down
9 changes: 3 additions & 6 deletions webhelper/src/gwh-settings.c
Expand Up @@ -35,7 +35,7 @@ struct _GwhSettingsPrivate
};


G_DEFINE_TYPE (GwhSettings, gwh_settings, G_TYPE_OBJECT)
G_DEFINE_TYPE_WITH_PRIVATE (GwhSettings, gwh_settings, G_TYPE_OBJECT)


static void
Expand Down Expand Up @@ -124,15 +124,12 @@ gwh_settings_class_init (GwhSettingsClass *klass)
object_class->finalize = gwh_settings_finalize;
object_class->get_property = gwh_settings_get_property;
object_class->set_property = gwh_settings_set_property;

g_type_class_add_private (klass, sizeof (GwhSettingsPrivate));
}

static void
gwh_settings_init (GwhSettings *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GWH_TYPE_SETTINGS,
GwhSettingsPrivate);
self->priv = gwh_settings_get_instance_private (self);
self->priv->prop_array = g_ptr_array_new ();
}

Expand Down Expand Up @@ -189,7 +186,7 @@ gwh_settings_install_property (GwhSettings *self,
break;

HANDLE_BASIC_TYPE (BOOLEAN, Boolean, boolean)
HANDLE_BASIC_TYPE (CHAR, Char, char)
HANDLE_BASIC_TYPE (CHAR, Char, schar)
HANDLE_BASIC_TYPE (UCHAR, UChar, uchar)
HANDLE_BASIC_TYPE (INT, Int, int)
HANDLE_BASIC_TYPE (UINT, UInt, uint)
Expand Down

0 comments on commit 8da1471

Please sign in to comment.