2009-06-11 13:42:54 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2009 Christian Dywan <christian@twotoasts.de>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
See the file COPYING for the full license text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <midori/midori.h>
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2009-07-24 21:55:57 +00:00
|
|
|
static void
|
|
|
|
shortcuts_browser_populate_tool_menu_cb (MidoriBrowser* browser,
|
|
|
|
GtkWidget* menu,
|
|
|
|
MidoriExtension* extension);
|
|
|
|
|
2009-06-11 13:42:54 +00:00
|
|
|
static void
|
|
|
|
shortcuts_app_add_browser_cb (MidoriApp* app,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension);
|
|
|
|
|
|
|
|
static void
|
|
|
|
shortcuts_deactivate_cb (MidoriExtension* extension,
|
2009-07-24 21:55:57 +00:00
|
|
|
MidoriBrowser* browser)
|
2009-06-11 13:42:54 +00:00
|
|
|
{
|
|
|
|
MidoriApp* app = midori_extension_get_app (extension);
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
2009-07-24 21:55:57 +00:00
|
|
|
browser, shortcuts_browser_populate_tool_menu_cb, extension);
|
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
extension, shortcuts_deactivate_cb, browser);
|
2009-06-11 13:42:54 +00:00
|
|
|
g_signal_handlers_disconnect_by_func (
|
|
|
|
app, shortcuts_app_add_browser_cb, extension);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shortcuts_accel_edited_cb (GtkCellRenderer* renderer,
|
|
|
|
const gchar* tree_path,
|
|
|
|
guint accel_key,
|
|
|
|
GdkModifierType accel_mods,
|
|
|
|
guint keycode,
|
|
|
|
GtkTreeModel* model)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (gtk_tree_model_get_iter_from_string (model, &iter, tree_path))
|
|
|
|
{
|
|
|
|
GtkAction* action;
|
|
|
|
const gchar* accel_path;
|
2009-12-07 22:40:06 +00:00
|
|
|
GtkTreeIter child_iter;
|
|
|
|
GtkTreeModel* liststore;
|
2009-06-11 13:42:54 +00:00
|
|
|
|
2009-12-05 00:24:14 +00:00
|
|
|
gtk_tree_model_get (model, &iter, 6, &action, -1);
|
2009-06-11 13:42:54 +00:00
|
|
|
accel_path = gtk_action_get_accel_path (action);
|
|
|
|
gtk_accel_map_change_entry (accel_path, accel_key, accel_mods, TRUE);
|
|
|
|
|
2009-12-07 22:40:06 +00:00
|
|
|
gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model),
|
|
|
|
&child_iter, &iter);
|
|
|
|
liststore = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model));
|
|
|
|
gtk_list_store_set (GTK_LIST_STORE (liststore),
|
|
|
|
&child_iter, 1, accel_key, 2, accel_mods, -1);
|
2009-12-05 00:24:14 +00:00
|
|
|
|
2009-06-11 13:42:54 +00:00
|
|
|
g_object_unref (action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shortcuts_accel_cleared_cb (GtkCellRenderer* renderer,
|
|
|
|
const gchar* tree_path,
|
|
|
|
GtkTreeModel* model)
|
|
|
|
{
|
|
|
|
GtkTreeIter iter;
|
|
|
|
|
|
|
|
if (gtk_tree_model_get_iter_from_string (model, &iter, tree_path))
|
|
|
|
{
|
|
|
|
GtkAction* action;
|
|
|
|
const gchar* accel_path;
|
2009-12-07 22:40:06 +00:00
|
|
|
GtkTreeIter child_iter;
|
|
|
|
GtkTreeModel* liststore;
|
2009-06-11 13:42:54 +00:00
|
|
|
|
2009-12-05 00:24:14 +00:00
|
|
|
gtk_tree_model_get (model, &iter, 6, &action, -1);
|
2009-06-11 13:42:54 +00:00
|
|
|
accel_path = gtk_action_get_accel_path (action);
|
|
|
|
gtk_accel_map_change_entry (accel_path, 0, 0, FALSE);
|
|
|
|
|
2009-12-07 22:40:06 +00:00
|
|
|
gtk_tree_model_sort_convert_iter_to_child_iter (GTK_TREE_MODEL_SORT (model),
|
|
|
|
&child_iter, &iter);
|
|
|
|
liststore = gtk_tree_model_sort_get_model (GTK_TREE_MODEL_SORT (model));
|
|
|
|
gtk_list_store_set (GTK_LIST_STORE (liststore),
|
|
|
|
&child_iter, 1, 0, 2, 0, -1);
|
2009-12-05 00:24:14 +00:00
|
|
|
|
2009-06-11 13:42:54 +00:00
|
|
|
g_object_unref (action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-05 00:24:14 +00:00
|
|
|
static gchar*
|
|
|
|
shortcuts_label_for_action (GtkAction* action)
|
|
|
|
{
|
2011-02-20 03:13:30 +00:00
|
|
|
const gchar* name = gtk_action_get_name (action);
|
2009-12-05 00:24:14 +00:00
|
|
|
gchar* label;
|
|
|
|
gchar* stripped;
|
|
|
|
|
2011-02-20 03:13:30 +00:00
|
|
|
if (g_str_equal (name, "ReloadStop"))
|
|
|
|
{
|
|
|
|
label = NULL;
|
|
|
|
stripped = g_strdup (_("Reload page or stop loading"));
|
|
|
|
}
|
|
|
|
else if ((label = katze_object_get_string (action, "label")))
|
2009-12-05 00:24:14 +00:00
|
|
|
stripped = katze_strip_mnemonics (label);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GtkStockItem item;
|
|
|
|
|
|
|
|
g_object_get (action, "stock-id", &label, NULL);
|
|
|
|
if (gtk_stock_lookup (label, &item))
|
|
|
|
stripped = katze_strip_mnemonics (item.label);
|
|
|
|
else
|
|
|
|
stripped = g_strdup ("");
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (label);
|
|
|
|
return stripped;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
shortcuts_hotkey_for_action (GtkAction* action,
|
|
|
|
GtkAccelKey* key)
|
|
|
|
{
|
|
|
|
const gchar* accel_path = gtk_action_get_accel_path (action);
|
|
|
|
if (accel_path)
|
|
|
|
if (gtk_accel_map_lookup_entry (accel_path, key))
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-12-29 22:49:10 +00:00
|
|
|
static void
|
|
|
|
shortcuts_preferences_response_cb (GtkWidget* dialog,
|
|
|
|
gint response,
|
|
|
|
gpointer data)
|
|
|
|
{
|
|
|
|
gtk_widget_destroy (dialog);
|
|
|
|
}
|
|
|
|
|
2009-06-11 13:42:54 +00:00
|
|
|
static GtkWidget*
|
|
|
|
shortcuts_get_preferences_dialog (MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
MidoriApp* app;
|
|
|
|
GtkWidget* browser;
|
|
|
|
const gchar* dialog_title;
|
|
|
|
GtkWidget* dialog;
|
|
|
|
gint width, height;
|
|
|
|
GtkWidget* xfce_heading;
|
|
|
|
GtkWidget* hbox;
|
|
|
|
GtkListStore* liststore;
|
2009-12-07 22:40:06 +00:00
|
|
|
GtkTreeModel* model;
|
2009-06-11 13:42:54 +00:00
|
|
|
GtkWidget* treeview;
|
|
|
|
GtkTreeViewColumn* column;
|
2009-12-05 00:24:14 +00:00
|
|
|
GtkCellRenderer* renderer;
|
2009-06-11 13:42:54 +00:00
|
|
|
GtkWidget* scrolled;
|
|
|
|
GtkActionGroup* action_group;
|
|
|
|
GList* actions;
|
|
|
|
guint i;
|
|
|
|
GtkAction* action;
|
2011-10-01 13:12:04 +00:00
|
|
|
GtkWidget* dialog_vbox;
|
2009-06-11 13:42:54 +00:00
|
|
|
#if HAVE_OSX
|
|
|
|
GtkWidget* icon;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
app = midori_extension_get_app (extension);
|
|
|
|
browser = katze_object_get_object (app, "browser");
|
|
|
|
|
2009-07-22 21:53:29 +00:00
|
|
|
dialog_title = _("Customize Keyboard shortcuts");
|
2009-06-11 13:42:54 +00:00
|
|
|
dialog = gtk_dialog_new_with_buttons (dialog_title, GTK_WINDOW (browser),
|
|
|
|
GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
|
|
|
|
#if !HAVE_OSX
|
|
|
|
GTK_STOCK_CLOSE, GTK_RESPONSE_CLOSE,
|
|
|
|
#endif
|
|
|
|
NULL);
|
|
|
|
g_signal_connect (dialog, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &dialog);
|
|
|
|
gtk_window_set_icon_name (GTK_WINDOW (dialog), GTK_STOCK_PROPERTIES);
|
|
|
|
sokoke_widget_get_text_size (dialog, "M", &width, &height);
|
|
|
|
gtk_window_set_default_size (GTK_WINDOW (dialog), width * 52, height * 24);
|
|
|
|
g_signal_connect (dialog, "response",
|
2009-12-29 22:49:10 +00:00
|
|
|
G_CALLBACK (shortcuts_preferences_response_cb), NULL);
|
2011-10-01 13:12:04 +00:00
|
|
|
|
|
|
|
dialog_vbox = gtk_dialog_get_content_area(GTK_DIALOG(dialog));
|
2009-06-11 13:42:54 +00:00
|
|
|
if ((xfce_heading = sokoke_xfce_header_new (
|
|
|
|
gtk_window_get_icon_name (GTK_WINDOW (dialog)), dialog_title)))
|
2011-10-01 13:12:04 +00:00
|
|
|
gtk_box_pack_start (GTK_BOX (dialog_vbox),
|
2009-06-11 13:42:54 +00:00
|
|
|
xfce_heading, FALSE, FALSE, 0);
|
|
|
|
hbox = gtk_hbox_new (FALSE, 0);
|
2011-10-01 13:12:04 +00:00
|
|
|
|
|
|
|
gtk_box_pack_start (GTK_BOX (dialog_vbox), hbox,
|
2009-06-11 13:42:54 +00:00
|
|
|
TRUE, TRUE, 12);
|
2009-12-05 00:24:14 +00:00
|
|
|
liststore = gtk_list_store_new (7,
|
|
|
|
G_TYPE_STRING, G_TYPE_INT, G_TYPE_INT, G_TYPE_INT, G_TYPE_BOOLEAN,
|
|
|
|
G_TYPE_STRING, GTK_TYPE_ACTION);
|
2009-12-07 22:40:06 +00:00
|
|
|
model = gtk_tree_model_sort_new_with_model (GTK_TREE_MODEL (liststore));
|
|
|
|
treeview = gtk_tree_view_new_with_model (model);
|
2009-06-11 13:42:54 +00:00
|
|
|
gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
|
|
|
|
column = gtk_tree_view_column_new ();
|
2009-12-05 00:24:14 +00:00
|
|
|
renderer = gtk_cell_renderer_text_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, renderer, FALSE);
|
|
|
|
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column), renderer, "text", 0);
|
2009-06-11 13:42:54 +00:00
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
|
|
|
|
column = gtk_tree_view_column_new ();
|
2009-12-05 00:24:14 +00:00
|
|
|
renderer = gtk_cell_renderer_accel_new ();
|
|
|
|
gtk_tree_view_column_pack_start (column, renderer, TRUE);
|
|
|
|
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column), renderer, "accel-key", 1);
|
|
|
|
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column), renderer, "accel-mods", 2);
|
|
|
|
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column), renderer, "accel-mode", 3);
|
|
|
|
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column), renderer, "sensitive", 4);
|
|
|
|
gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (column), renderer, "editable", 4);
|
|
|
|
g_signal_connect (renderer, "accel-edited",
|
2009-12-07 22:40:06 +00:00
|
|
|
G_CALLBACK (shortcuts_accel_edited_cb), model);
|
2009-12-05 00:24:14 +00:00
|
|
|
g_signal_connect (renderer, "accel-cleared",
|
2009-12-07 22:40:06 +00:00
|
|
|
G_CALLBACK (shortcuts_accel_cleared_cb), model);
|
2009-06-11 13:42:54 +00:00
|
|
|
gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
|
|
|
|
scrolled = gtk_scrolled_window_new (NULL, NULL);
|
|
|
|
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
|
|
|
|
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
|
|
|
|
gtk_container_add (GTK_CONTAINER (scrolled), treeview);
|
|
|
|
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
|
|
|
|
GTK_SHADOW_IN);
|
|
|
|
gtk_box_pack_start (GTK_BOX (hbox), scrolled, TRUE, TRUE, 5);
|
|
|
|
|
|
|
|
action_group = midori_browser_get_action_group (MIDORI_BROWSER (browser));
|
|
|
|
actions = gtk_action_group_list_actions (action_group);
|
|
|
|
i = 0;
|
|
|
|
/* FIXME: Catch added and removed actions */
|
|
|
|
while ((action = g_list_nth_data (actions, i++)))
|
2009-12-05 00:24:14 +00:00
|
|
|
{
|
|
|
|
gchar* label = shortcuts_label_for_action (action);
|
|
|
|
GtkAccelKey key;
|
|
|
|
gboolean has_hotkey = shortcuts_hotkey_for_action (action, &key);
|
2009-06-11 13:42:54 +00:00
|
|
|
gtk_list_store_insert_with_values (GTK_LIST_STORE (liststore),
|
2009-12-05 00:24:14 +00:00
|
|
|
NULL, G_MAXINT, 0, label, 1, key.accel_key, 2, key.accel_mods,
|
|
|
|
3, GTK_CELL_RENDERER_ACCEL_MODE_OTHER,
|
|
|
|
4, has_hotkey, 6, action, -1);
|
|
|
|
g_free (label);
|
|
|
|
}
|
2009-06-11 13:42:54 +00:00
|
|
|
g_list_free (actions);
|
|
|
|
|
|
|
|
g_object_unref (liststore);
|
2009-12-07 22:40:06 +00:00
|
|
|
gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (model),
|
|
|
|
0, GTK_SORT_ASCENDING);
|
|
|
|
g_object_unref (model);
|
2009-06-11 13:42:54 +00:00
|
|
|
|
2011-10-01 13:12:04 +00:00
|
|
|
gtk_widget_show_all (gtk_dialog_get_content_area(GTK_DIALOG (dialog)));
|
2009-06-11 13:42:54 +00:00
|
|
|
|
|
|
|
g_object_unref (browser);
|
|
|
|
|
|
|
|
return dialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shortcuts_menu_configure_shortcuts_activate_cb (GtkWidget* menuitem,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
static GtkWidget* dialog = NULL;
|
|
|
|
|
|
|
|
if (!dialog)
|
|
|
|
{
|
|
|
|
dialog = shortcuts_get_preferences_dialog (extension);
|
|
|
|
g_signal_connect (dialog, "destroy",
|
|
|
|
G_CALLBACK (gtk_widget_destroyed), &dialog);
|
|
|
|
gtk_widget_show (dialog);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
gtk_window_present (GTK_WINDOW (dialog));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-07-24 21:55:57 +00:00
|
|
|
shortcuts_browser_populate_tool_menu_cb (MidoriBrowser* browser,
|
|
|
|
GtkWidget* menu,
|
|
|
|
MidoriExtension* extension)
|
2009-06-11 13:42:54 +00:00
|
|
|
{
|
|
|
|
GtkWidget* menuitem;
|
|
|
|
|
2009-07-22 21:53:29 +00:00
|
|
|
menuitem = gtk_menu_item_new_with_mnemonic (_("Customize Sh_ortcuts..."));
|
2009-06-11 13:42:54 +00:00
|
|
|
g_signal_connect (menuitem, "activate",
|
|
|
|
G_CALLBACK (shortcuts_menu_configure_shortcuts_activate_cb), extension);
|
|
|
|
gtk_widget_show (menuitem);
|
2009-07-24 21:55:57 +00:00
|
|
|
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
|
|
|
|
}
|
2009-06-11 13:42:54 +00:00
|
|
|
|
2009-07-24 21:55:57 +00:00
|
|
|
static void
|
|
|
|
shortcuts_app_add_browser_cb (MidoriApp* app,
|
|
|
|
MidoriBrowser* browser,
|
|
|
|
MidoriExtension* extension)
|
|
|
|
{
|
|
|
|
g_signal_connect (browser, "populate-tool-menu",
|
|
|
|
G_CALLBACK (shortcuts_browser_populate_tool_menu_cb), extension);
|
2009-06-11 13:42:54 +00:00
|
|
|
g_signal_connect (extension, "deactivate",
|
2009-07-24 21:55:57 +00:00
|
|
|
G_CALLBACK (shortcuts_deactivate_cb), browser);
|
2009-06-11 13:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shortcuts_activate_cb (MidoriExtension* extension,
|
|
|
|
MidoriApp* app)
|
|
|
|
{
|
|
|
|
KatzeArray* browsers;
|
|
|
|
MidoriBrowser* browser;
|
|
|
|
|
|
|
|
browsers = katze_object_get_object (app, "browsers");
|
2010-09-11 21:39:51 +00:00
|
|
|
KATZE_ARRAY_FOREACH_ITEM (browser, browsers)
|
2009-06-11 13:42:54 +00:00
|
|
|
shortcuts_app_add_browser_cb (app, browser, extension);
|
|
|
|
g_signal_connect (app, "add-browser",
|
|
|
|
G_CALLBACK (shortcuts_app_add_browser_cb), extension);
|
|
|
|
g_object_unref (browsers);
|
|
|
|
}
|
|
|
|
|
|
|
|
MidoriExtension*
|
|
|
|
extension_init (void)
|
|
|
|
{
|
|
|
|
MidoriExtension* extension = g_object_new (MIDORI_TYPE_EXTENSION,
|
|
|
|
"name", _("Shortcuts"),
|
|
|
|
"description", _("View and edit keyboard shortcuts"),
|
|
|
|
"version", "0.1",
|
|
|
|
"authors", "Christian Dywan <christian@twotoasts.de>",
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
g_signal_connect (extension, "activate",
|
|
|
|
G_CALLBACK (shortcuts_activate_cb), NULL);
|
|
|
|
|
|
|
|
return extension;
|
|
|
|
}
|