2016-04-11 15:13:09 +00:00
|
|
|
/*
|
|
|
|
* Copyright © 2013 Canonical Limited
|
|
|
|
* Copyright © 2016 Sébastien Wilmet
|
|
|
|
*
|
|
|
|
* 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 of the licence, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Authors: Ryan Lortie <desrt@desrt.ca>
|
|
|
|
* Sébastien Wilmet <swilmet@gnome.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2018-02-02 09:13:10 +00:00
|
|
|
|
2016-04-11 15:13:09 +00:00
|
|
|
#include "gtkapplicationaccelsprivate.h"
|
2020-03-19 22:03:16 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
#include "gtkactionmuxerprivate.h"
|
|
|
|
#include "gtkshortcut.h"
|
|
|
|
#include "gtkshortcutaction.h"
|
|
|
|
#include "gtkshortcuttrigger.h"
|
2016-04-11 15:13:09 +00:00
|
|
|
|
|
|
|
struct _GtkApplicationAccels
|
|
|
|
{
|
|
|
|
GObject parent;
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
GListModel *shortcuts;
|
2016-04-11 15:13:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (GtkApplicationAccels, gtk_application_accels, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_application_accels_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkApplicationAccels *accels = GTK_APPLICATION_ACCELS (object);
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
g_list_store_remove_all (G_LIST_STORE (accels->shortcuts));
|
|
|
|
g_object_unref (accels->shortcuts);
|
2016-04-11 15:13:09 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_application_accels_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_application_accels_class_init (GtkApplicationAccelsClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gtk_application_accels_finalize;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_application_accels_init (GtkApplicationAccels *accels)
|
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
accels->shortcuts = G_LIST_MODEL (g_list_store_new (GTK_TYPE_SHORTCUT));
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GtkApplicationAccels *
|
|
|
|
gtk_application_accels_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (GTK_TYPE_APPLICATION_ACCELS, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gtk_application_accels_set_accels_for_action (GtkApplicationAccels *accels,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *detailed_action_name,
|
|
|
|
const char * const *accelerators)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
char *action_name;
|
2018-08-22 23:23:24 +00:00
|
|
|
GVariant *target;
|
|
|
|
GtkShortcut *shortcut;
|
|
|
|
GtkShortcutTrigger *trigger = NULL;
|
|
|
|
GError *error = NULL;
|
|
|
|
guint i;
|
|
|
|
|
|
|
|
if (!g_action_parse_detailed_name (detailed_action_name, &action_name, &target, &error))
|
|
|
|
{
|
|
|
|
g_critical ("Error parsing action name: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove the accelerator if it already exists */
|
|
|
|
for (i = 0; i < g_list_model_get_n_items (accels->shortcuts); i++)
|
|
|
|
{
|
|
|
|
GtkShortcut *shortcut_i = g_list_model_get_item (accels->shortcuts, i);
|
|
|
|
GtkShortcutAction *action = gtk_shortcut_get_action (shortcut_i);
|
|
|
|
GVariant *args = gtk_shortcut_get_arguments (shortcut_i);
|
|
|
|
|
2020-03-20 15:19:45 +00:00
|
|
|
if (!GTK_IS_NAMED_ACTION (action) ||
|
|
|
|
!g_str_equal (gtk_named_action_get_action_name (GTK_NAMED_ACTION (action)), action_name))
|
2020-03-28 07:34:35 +00:00
|
|
|
{
|
|
|
|
g_object_unref (shortcut_i);
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
|
|
|
|
if ((target == NULL && args != NULL) ||
|
|
|
|
(target != NULL && (args == NULL || !g_variant_equal (target, args))))
|
2020-03-28 07:34:35 +00:00
|
|
|
{
|
|
|
|
g_object_unref (shortcut_i);
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
g_list_store_remove (G_LIST_STORE (accels->shortcuts), i);
|
2020-07-16 12:46:18 +00:00
|
|
|
g_object_unref (shortcut_i);
|
2018-08-22 23:23:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
if (accelerators == NULL)
|
|
|
|
goto out;
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
for (i = 0; accelerators[i]; i++)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
GtkShortcutTrigger *new_trigger;
|
|
|
|
guint key, modifier;
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
if (!gtk_accelerator_parse (accelerators[i], &key, &modifier))
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
g_critical ("Unable to parse accelerator '%s': ignored request to install accelerators",
|
|
|
|
accelerators[i]);
|
2020-03-19 15:57:02 +00:00
|
|
|
g_clear_object (&trigger);
|
|
|
|
goto out;
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
new_trigger = gtk_keyval_trigger_new (key, modifier);
|
|
|
|
if (trigger)
|
|
|
|
trigger = gtk_alternative_trigger_new (trigger, new_trigger);
|
|
|
|
else
|
|
|
|
trigger = new_trigger;
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
if (trigger == NULL)
|
|
|
|
goto out;
|
|
|
|
|
2020-03-20 15:19:45 +00:00
|
|
|
shortcut = gtk_shortcut_new (trigger, gtk_named_action_new (action_name));
|
2018-08-22 23:23:24 +00:00
|
|
|
gtk_shortcut_set_arguments (shortcut, target);
|
|
|
|
g_list_store_append (G_LIST_STORE (accels->shortcuts), shortcut);
|
|
|
|
g_object_unref (shortcut);
|
|
|
|
|
|
|
|
out:
|
|
|
|
g_free (action_name);
|
|
|
|
if (target)
|
|
|
|
g_variant_unref (target);
|
|
|
|
}
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
static void
|
|
|
|
append_accelerators (GPtrArray *accels,
|
|
|
|
GtkShortcutTrigger *trigger)
|
|
|
|
{
|
2020-03-19 15:57:02 +00:00
|
|
|
if (GTK_IS_KEYVAL_TRIGGER (trigger))
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2020-03-19 15:57:02 +00:00
|
|
|
GtkKeyvalTrigger *kt = GTK_KEYVAL_TRIGGER (trigger);
|
|
|
|
guint keyval = gtk_keyval_trigger_get_keyval (kt);
|
|
|
|
GdkModifierType mods = gtk_keyval_trigger_get_modifiers (kt);
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2020-03-19 15:57:02 +00:00
|
|
|
g_ptr_array_add (accels, gtk_accelerator_name (keyval, mods));
|
2018-08-22 23:23:24 +00:00
|
|
|
return;
|
2020-03-19 15:57:02 +00:00
|
|
|
}
|
|
|
|
else if (GTK_IS_ALTERNATIVE_TRIGGER (trigger))
|
|
|
|
{
|
|
|
|
GtkAlternativeTrigger *at = GTK_ALTERNATIVE_TRIGGER (trigger);
|
|
|
|
GtkShortcutTrigger *first = gtk_alternative_trigger_get_first (at);
|
|
|
|
GtkShortcutTrigger *second = gtk_alternative_trigger_get_second (at);
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2020-03-19 15:57:02 +00:00
|
|
|
append_accelerators (accels, first);
|
|
|
|
append_accelerators (accels, second);
|
2018-08-22 23:23:24 +00:00
|
|
|
return;
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char **
|
2016-04-11 15:13:09 +00:00
|
|
|
gtk_application_accels_get_accels_for_action (GtkApplicationAccels *accels,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *detailed_action_name)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
GPtrArray *result;
|
|
|
|
char *action_name;
|
|
|
|
GVariant *target;
|
|
|
|
GError *error = NULL;
|
|
|
|
guint i;
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
result = g_ptr_array_new ();
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
if (!g_action_parse_detailed_name (detailed_action_name, &action_name, &target, &error))
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
g_critical ("Error parsing action name: %s", error->message);
|
|
|
|
g_error_free (error);
|
|
|
|
g_ptr_array_add (result, NULL);
|
2020-07-24 18:40:36 +00:00
|
|
|
return (char **) g_ptr_array_free (result, FALSE);
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
for (i = 0; i < g_list_model_get_n_items (accels->shortcuts); i++)
|
|
|
|
{
|
|
|
|
GtkShortcut *shortcut = g_list_model_get_item (accels->shortcuts, i);
|
|
|
|
GtkShortcutAction *action = gtk_shortcut_get_action (shortcut);
|
|
|
|
GVariant *args = gtk_shortcut_get_arguments (shortcut);
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2020-03-20 15:19:45 +00:00
|
|
|
if (!GTK_IS_NAMED_ACTION (action) ||
|
|
|
|
!g_str_equal (gtk_named_action_get_action_name (GTK_NAMED_ACTION (action)), action_name))
|
2020-03-28 07:34:35 +00:00
|
|
|
{
|
|
|
|
g_object_unref (shortcut);
|
|
|
|
continue;
|
|
|
|
}
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
if ((target == NULL && args != NULL) ||
|
|
|
|
(target != NULL && (args == NULL || !g_variant_equal (target, args))))
|
2020-03-28 07:34:35 +00:00
|
|
|
{
|
|
|
|
g_object_unref (shortcut);
|
|
|
|
continue;
|
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
append_accelerators (result, gtk_shortcut_get_trigger (shortcut));
|
2020-03-28 07:34:35 +00:00
|
|
|
g_object_unref (shortcut);
|
2018-08-22 23:23:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free (action_name);
|
|
|
|
if (target)
|
|
|
|
g_variant_unref (target);
|
|
|
|
g_ptr_array_add (result, NULL);
|
2020-07-24 18:40:36 +00:00
|
|
|
return (char **) g_ptr_array_free (result, FALSE);
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
static gboolean
|
|
|
|
trigger_matches_accel (GtkShortcutTrigger *trigger,
|
|
|
|
guint keyval,
|
|
|
|
GdkModifierType modifiers)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2020-03-19 15:57:02 +00:00
|
|
|
if (GTK_IS_KEYVAL_TRIGGER (trigger))
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2020-03-19 15:57:02 +00:00
|
|
|
GtkKeyvalTrigger *kt = GTK_KEYVAL_TRIGGER (trigger);
|
2018-08-22 23:23:24 +00:00
|
|
|
|
2020-03-19 15:57:02 +00:00
|
|
|
return gtk_keyval_trigger_get_keyval (kt) == keyval
|
|
|
|
&& gtk_keyval_trigger_get_modifiers (kt) == modifiers;
|
|
|
|
}
|
|
|
|
else if (GTK_IS_ALTERNATIVE_TRIGGER (trigger))
|
|
|
|
{
|
|
|
|
GtkAlternativeTrigger *at = GTK_ALTERNATIVE_TRIGGER (trigger);
|
2018-08-22 23:23:24 +00:00
|
|
|
|
2020-03-19 15:57:02 +00:00
|
|
|
return trigger_matches_accel (gtk_alternative_trigger_get_first (at), keyval, modifiers)
|
|
|
|
|| trigger_matches_accel (gtk_alternative_trigger_get_second (at), keyval, modifiers);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
return FALSE;
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
}
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
static char *
|
|
|
|
get_detailed_name_for_shortcut (GtkShortcut *shortcut)
|
|
|
|
{
|
|
|
|
GtkShortcutAction *action = gtk_shortcut_get_action (shortcut);
|
2020-03-19 22:03:16 +00:00
|
|
|
|
2020-03-20 15:19:45 +00:00
|
|
|
if (!GTK_IS_NAMED_ACTION (action))
|
2018-08-22 23:23:24 +00:00
|
|
|
return NULL;
|
2020-03-19 22:03:16 +00:00
|
|
|
|
2020-03-20 15:19:45 +00:00
|
|
|
return g_action_print_detailed_name (gtk_named_action_get_action_name (GTK_NAMED_ACTION (action)),
|
|
|
|
gtk_shortcut_get_arguments (shortcut));
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char **
|
2018-08-22 23:23:24 +00:00
|
|
|
gtk_application_accels_get_actions_for_accel (GtkApplicationAccels *accels,
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *accel)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
GPtrArray *result;
|
|
|
|
guint key, modifiers;
|
|
|
|
guint i;
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
if (!gtk_accelerator_parse (accel, &key, &modifiers))
|
|
|
|
{
|
|
|
|
g_critical ("invalid accelerator string '%s'", accel);
|
|
|
|
return NULL;
|
|
|
|
}
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
result = g_ptr_array_new ();
|
2020-03-28 07:34:35 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
for (i = 0; i < g_list_model_get_n_items (accels->shortcuts); i++)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
GtkShortcut *shortcut = g_list_model_get_item (accels->shortcuts, i);
|
|
|
|
char *detailed_name;
|
|
|
|
|
|
|
|
if (!trigger_matches_accel (gtk_shortcut_get_trigger (shortcut), key, modifiers))
|
2020-03-28 07:34:35 +00:00
|
|
|
{
|
|
|
|
g_object_unref (shortcut);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
detailed_name = get_detailed_name_for_shortcut (shortcut);
|
|
|
|
if (detailed_name)
|
|
|
|
g_ptr_array_add (result, detailed_name);
|
2020-03-28 07:34:35 +00:00
|
|
|
|
|
|
|
g_object_unref (shortcut);
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
g_ptr_array_add (result, NULL);
|
2020-07-24 18:40:36 +00:00
|
|
|
return (char **) g_ptr_array_free (result, FALSE);
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2020-07-24 18:40:36 +00:00
|
|
|
char **
|
2018-08-22 23:23:24 +00:00
|
|
|
gtk_application_accels_list_action_descriptions (GtkApplicationAccels *accels)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
GPtrArray *result;
|
|
|
|
guint i;
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
result = g_ptr_array_new ();
|
2020-03-28 07:34:35 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
for (i = 0; i < g_list_model_get_n_items (accels->shortcuts); i++)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
GtkShortcut *shortcut = g_list_model_get_item (accels->shortcuts, i);
|
|
|
|
char *detailed_name;
|
2016-04-11 15:13:09 +00:00
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
detailed_name = get_detailed_name_for_shortcut (shortcut);
|
|
|
|
if (detailed_name)
|
|
|
|
g_ptr_array_add (result, detailed_name);
|
2020-03-28 07:34:35 +00:00
|
|
|
|
|
|
|
g_object_unref (shortcut);
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
2018-08-22 23:23:24 +00:00
|
|
|
|
|
|
|
g_ptr_array_add (result, NULL);
|
2020-07-24 18:40:36 +00:00
|
|
|
return (char **) g_ptr_array_free (result, FALSE);
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|
|
|
|
|
2018-08-22 23:23:24 +00:00
|
|
|
GListModel *
|
|
|
|
gtk_application_accels_get_shortcuts (GtkApplicationAccels *accels)
|
2016-04-11 15:13:09 +00:00
|
|
|
{
|
2018-08-22 23:23:24 +00:00
|
|
|
return accels->shortcuts;
|
2016-04-11 15:13:09 +00:00
|
|
|
}
|