Commit da58e6cc authored by Paolo Bacchilega's avatar Paolo Bacchilega
Browse files

list tools: install a shortcut for every script

parent 413a9f66
......@@ -32,6 +32,7 @@
#define BROWSER_DATA_KEY "list-tools-browser-data"
#define SCRIPTS_GROUP "scripts"
static const GActionEntry actions[] = {
......@@ -43,7 +44,6 @@ static const GActionEntry actions[] = {
typedef struct {
GthBrowser *browser;
gulong scripts_changed_id;
gboolean menu_initialized;
guint menu_merge_id;
} BrowserData;
......@@ -76,6 +76,7 @@ list_tools__gth_browser_update_sensitivity_cb (GthBrowser *browser)
static void
update_scripts_menu (BrowserData *data)
update_scripts (BrowserData *data)
{
GthMenuManager *menu_manager;
GList *script_list;
......@@ -86,31 +87,33 @@ update_scripts_menu (BrowserData *data)
gth_menu_manager_remove_entries (menu_manager, data->menu_merge_id);
data->menu_merge_id = gth_menu_manager_new_merge_id (menu_manager);
gth_window_remove_shortcuts (GTH_WINDOW (data->browser), SCRIPTS_GROUP);
script_list = gth_script_file_get_scripts (gth_script_file_get ());
for (scan = script_list; scan; scan = scan->next) {
GthScript *script = scan->data;
guint keyval;
GdkModifierType modifiers;
char *accelerator_name;
char *detailed_action;
if (! gth_script_is_visible (script))
continue;
detailed_action = g_strdup_printf ("win.exec-script('%s')", gth_script_get_id (script));
gth_script_get_accelerator (script, &keyval, &modifiers);
accelerator_name = gtk_accelerator_name (keyval, modifiers);
gth_menu_manager_append_entry (menu_manager,
data->menu_merge_id,
gth_script_get_display_name (script),
detailed_action,
accelerator_name,
NULL);
GthScript *script = scan->data;
GthShortcut *shortcut;
shortcut = gth_script_get_shortcut (script);
gth_window_add_removable_shortcut (GTH_WINDOW (data->browser),
SCRIPTS_GROUP,
shortcut);
if (gth_script_is_visible (script)) {
char *detailed_action;
detailed_action = g_strdup_printf ("win.exec-script('%s')", gth_script_get_id (script));
gth_menu_manager_append_entry (menu_manager,
data->menu_merge_id,
gth_script_get_display_name (script),
detailed_action,
shortcut->label,
NULL);
g_free (detailed_action);
}
g_free (accelerator_name);
g_free (detailed_action);
gth_shortcut_free (shortcut);
}
list_tools__gth_browser_update_sensitivity_cb (data->browser);
......@@ -121,33 +124,9 @@ update_scripts_menu (BrowserData *data)
static void
scripts_changed_cb (GthScriptFile *script_file,
BrowserData *data)
BrowserData *data)
{
update_scripts_menu (data);
}
static void
tools_menu_button_button_press_event_cb (GtkToggleButton *togglebutton,
GdkEvent *event,
gpointer user_data)
{
BrowserData *data = user_data;
if (gtk_toggle_button_get_active (togglebutton))
return;
if (! data->menu_initialized) {
data->menu_initialized = TRUE;
update_scripts_menu (data);
data->scripts_changed_id = g_signal_connect (gth_script_file_get (),
"changed",
G_CALLBACK (scripts_changed_cb),
data);
}
list_tools__gth_browser_update_sensitivity_cb (data->browser);
update_scripts (data);
}
......@@ -179,7 +158,6 @@ list_tools__gth_browser_construct_cb (GthBrowser *browser)
/* browser tools */
button = _gtk_menu_button_new_for_header_bar ("tools-symbolic");
g_signal_connect (button, "button-press-event", G_CALLBACK (tools_menu_button_button_press_event_cb), data);
gtk_widget_set_tooltip_text (button, _("Tools"));
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu);
gtk_widget_set_halign (GTK_WIDGET (gtk_menu_button_get_popup (GTK_MENU_BUTTON (button))), GTK_ALIGN_CENTER);
......@@ -189,7 +167,6 @@ list_tools__gth_browser_construct_cb (GthBrowser *browser)
/* viewer edit */
button = _gtk_menu_button_new_for_header_bar ("tools-symbolic");
g_signal_connect (button, "button-press-event", G_CALLBACK (tools_menu_button_button_press_event_cb), data);
gtk_widget_set_tooltip_text (button, _("Tools"));
gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (button), menu);
gtk_widget_set_halign (GTK_WIDGET (gtk_menu_button_get_popup (GTK_MENU_BUTTON (button))), GTK_ALIGN_CENTER);
......@@ -197,37 +174,16 @@ list_tools__gth_browser_construct_cb (GthBrowser *browser)
gtk_box_pack_end (GTK_BOX (gth_browser_get_headerbar_section (browser, GTH_BROWSER_HEADER_SECTION_VIEWER_EDIT)), button, FALSE, FALSE, 0);
g_object_unref (builder);
update_scripts (data);
data->scripts_changed_id = g_signal_connect (gth_script_file_get (),
"changed",
G_CALLBACK (scripts_changed_cb),
data);
}
gpointer
list_tools__gth_browser_file_list_key_press_cb (GthBrowser *browser,
GdkEventKey *event)
{
gpointer result = NULL;
guint event_key;
GdkModifierType event_modifiers;
GList *script_list;
GList *scan;
event_key = gdk_keyval_to_lower (event->keyval);
event_modifiers = event->state & gtk_accelerator_get_default_mod_mask ();
script_list = gth_script_file_get_scripts (gth_script_file_get ());
for (scan = script_list; scan; scan = scan->next) {
GthScript *script = scan->data;
guint keyval;
GdkModifierType modifiers;
gth_script_get_accelerator (script, &keyval, &modifiers);
if ((keyval == event_key) && (modifiers == event_modifiers)) {
gth_browser_exec_script (browser, script);
result = GINT_TO_POINTER (1);
break;
}
}
_g_object_list_unref (script_list);
return result;
}
......@@ -25,7 +25,5 @@
#include <gthumb.h>
void list_tools__gth_browser_construct_cb (GthBrowser *browser);
gpointer list_tools__gth_browser_file_list_key_press_cb (GthBrowser *browser,
GdkEventKey *event);
#endif /* CALLBACKS_H */
......@@ -1034,3 +1034,21 @@ gth_script_get_accelerator (GthScript *self,
if (keyval) *keyval = self->priv->accelerator.keyval;
if (modifiers) *modifiers = self->priv->accelerator.modifiers;
}
GthShortcut *
gth_script_get_shortcut (GthScript *self)
{
GthShortcut *shortcut;
shortcut = gth_shortcut_new ();
shortcut->action_name = g_strdup ("exec-script");
shortcut->action_parameter = g_variant_ref_sink (g_variant_new_string (gth_script_get_id (self)));
shortcut->description = g_strdup (self->priv->display_name);
shortcut->context = GTH_SHORTCUT_CONTEXT_BROWSER_VIEWER;
shortcut->category = GTH_SHORTCUT_CATEGORY_FILE_MANAGER;
gth_shortcut_set_key (shortcut, self->priv->accelerator.keyval, self->priv->accelerator.modifiers);
shortcut->default_accelerator = g_strdup (shortcut->accelerator);
return shortcut;
}
......@@ -68,6 +68,7 @@ char * gth_script_get_command_line (GthScript *script,
void gth_script_get_accelerator (GthScript *script,
guint *keyval,
GdkModifierType *modifiers);
GthShortcut * gth_script_get_shortcut (GthScript *script);
G_END_DECLS
......
......@@ -30,7 +30,6 @@ G_MODULE_EXPORT void
gthumb_extension_activate (void)
{
gth_hook_add_callback ("gth-browser-construct", 5, G_CALLBACK (list_tools__gth_browser_construct_cb), NULL);
gth_hook_add_callback ("gth-browser-file-list-key-press", 10, G_CALLBACK (list_tools__gth_browser_file_list_key_press_cb), NULL);
}
......
......@@ -23,6 +23,7 @@
#include <glib/gi18n.h>
#include "dom.h"
#include "gio-utils.h"
#include "glib-utils.h"
#include "gth-shortcut.h"
#include "gth-user-dir.h"
......@@ -34,6 +35,7 @@ gth_shortcut_new (void)
shortcut = g_new (GthShortcut, 1);
shortcut->action_name = NULL;
shortcut->action_parameter = NULL;
shortcut->description = NULL;
shortcut->context = 0;
shortcut->category = NULL;
......@@ -54,6 +56,10 @@ gth_shortcut_dup (const GthShortcut *shortcut)
new_shortcut = gth_shortcut_new ();
new_shortcut->action_name = g_strdup (shortcut->action_name);
if (shortcut->action_parameter != NULL)
new_shortcut->action_parameter = g_variant_ref_sink (shortcut->action_parameter);
else
new_shortcut->action_parameter = NULL;
new_shortcut->description = g_strdup (shortcut->description);
new_shortcut->context = shortcut->context;
new_shortcut->category = shortcut->category;
......@@ -68,6 +74,8 @@ void
gth_shortcut_free (GthShortcut *shortcut)
{
g_free (shortcut->action_name);
if (shortcut->action_parameter != NULL)
g_variant_unref (shortcut->action_parameter);
g_free (shortcut->description);
g_free (shortcut->default_accelerator);
g_free (shortcut->accelerator);
......
......@@ -52,6 +52,7 @@ typedef struct {
char *label;
guint keyval;
GdkModifierType modifiers;
GVariant *action_parameter;
} GthShortcut;
......
......@@ -58,6 +58,7 @@ struct _GthWindowPrivate {
GtkAccelGroup *accel_group;
GHashTable *shortcuts;
GPtrArray *shortcuts_v;
GHashTable *shortcut_groups;
};
......@@ -255,6 +256,7 @@ gth_window_finalize (GObject *object)
g_object_unref (window->priv->accel_group);
g_hash_table_unref (window->priv->shortcuts);
g_ptr_array_unref (window->priv->shortcuts_v);
g_hash_table_unref (window->priv->shortcut_groups);
G_OBJECT_CLASS (gth_window_parent_class)->finalize (object);
}
......@@ -408,6 +410,8 @@ gth_window_init (GthWindow *window)
window->priv->shortcuts = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
window->priv->shortcuts_v = g_ptr_array_new_with_free_func ((GDestroyNotify) gth_shortcut_free);
window->priv->shortcut_groups = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_ptr_array_unref);
gtk_window_set_application (GTK_WINDOW (window), Main_Application);
}
......@@ -712,6 +716,15 @@ _gth_window_add_shortcut (GthWindow *window,
}
static void
_gth_window_remove_shortcut (GthWindow *window,
GthShortcut *shorcut)
{
g_hash_table_remove (window->priv->shortcuts, shorcut->action_name);
g_ptr_array_remove (window->priv->shortcuts_v, shorcut);
}
void
gth_window_add_accelerators (GthWindow *window,
const GthAccelerator *accelerators,
......@@ -879,14 +892,8 @@ gth_window_activate_shortcut (GthWindow *window,
action = g_action_map_lookup_action (G_ACTION_MAP (window), shortcut->action_name);
if (action != NULL) {
GVariant *variant;
variant = g_action_get_state (action);
g_action_activate (action, variant);
g_action_activate (action, shortcut->action_parameter);
activated = TRUE;
if (variant != NULL)
g_variant_unref (variant);
}
}
......@@ -903,3 +910,53 @@ gth_window_load_shortcuts (GthWindow *window)
window->priv->shortcuts,
NULL);
}
void
gth_window_add_removable_shortcut (GthWindow *window,
const char *group_name,
GthShortcut *shortcut)
{
GPtrArray *shortcuts_v;
GthShortcut *new_shortcut;
g_return_if_fail (GTH_IS_WINDOW (window));
g_return_if_fail (group_name != NULL);
shortcuts_v = g_hash_table_lookup (window->priv->shortcut_groups, group_name);
if (shortcuts_v == NULL) {
shortcuts_v = g_ptr_array_new ();
g_hash_table_insert (window->priv->shortcut_groups,
g_strdup (group_name),
shortcuts_v);
}
new_shortcut = gth_shortcut_dup (shortcut);
gth_shortcut_set_accelerator (new_shortcut, shortcut->default_accelerator);
_gth_window_add_shortcut (window, new_shortcut);
g_ptr_array_add (shortcuts_v, new_shortcut);
}
void
gth_window_remove_shortcuts (GthWindow *window,
const char *group_name)
{
GPtrArray *shortcuts_v;
int i;
g_return_if_fail (GTH_IS_WINDOW (window));
g_return_if_fail (group_name != NULL);
shortcuts_v = g_hash_table_lookup (window->priv->shortcut_groups, group_name);
if (shortcuts_v == NULL)
return;
for (i = 0; i < shortcuts_v->len; i++) {
GthShortcut *shortcut = g_ptr_array_index (shortcuts_v, i);
_gth_window_remove_shortcut (window, shortcut);
}
g_hash_table_remove (window->priv->shortcut_groups, group_name);
}
......@@ -132,6 +132,12 @@ gboolean gth_window_activate_shortcut (GthWindow *window,
guint keycode,
GdkModifierType modifiers);
void gth_window_load_shortcuts (GthWindow *window);
void gth_window_add_removable_shortcut
(GthWindow *window,
const char *group_name,
GthShortcut *shortcut);
void gth_window_remove_shortcuts (GthWindow *window,
const char *group_name);
G_END_DECLS
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment