shortcutaction: INtegrate with GtkBuilder property parsing

<property name="action">action(win.quit)</property> style action
specifications now work for GtkShortcutAction properties.
This commit is contained in:
Benjamin Otte 2018-08-24 06:58:44 +02:00 committed by Matthias Clasen
parent 5218dd6a34
commit 458fcba457
3 changed files with 120 additions and 1 deletions

View File

@ -222,6 +222,7 @@
#include "gtkicontheme.h" #include "gtkicontheme.h"
#include "gtkintl.h" #include "gtkintl.h"
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkshortcutactionprivate.h"
#include "gtkshortcuttrigger.h" #include "gtkshortcuttrigger.h"
#include "gtktestutils.h" #include "gtktestutils.h"
#include "gtktypebuiltins.h" #include "gtktypebuiltins.h"
@ -2110,6 +2111,13 @@ gtk_builder_value_from_string_type (GtkBuilder *builder,
ret = FALSE; ret = FALSE;
} }
} }
else if (G_VALUE_HOLDS (value, GTK_TYPE_SHORTCUT_ACTION))
{
GtkShortcutAction *action = gtk_shortcut_action_parse_builder (builder, string, error);
/* Works for success and failure (NULL) case */
g_value_take_boxed (value, action);
}
else if (G_VALUE_HOLDS (value, G_TYPE_STRV)) else if (G_VALUE_HOLDS (value, G_TYPE_STRV))
{ {
gchar **vector = g_strsplit (string, "\n", 0); gchar **vector = g_strsplit (string, "\n", 0);

View File

@ -37,8 +37,9 @@
#include "config.h" #include "config.h"
#include "gtkshortcutaction.h" #include "gtkshortcutactionprivate.h"
#include "gtkbuilder.h"
#include "gtkwidgetprivate.h" #include "gtkwidgetprivate.h"
typedef struct _GtkShortcutActionClass GtkShortcutActionClass; typedef struct _GtkShortcutActionClass GtkShortcutActionClass;
@ -226,6 +227,87 @@ gtk_shortcut_action_activate (GtkShortcutAction *self,
return self->action_class->activate (self, flags, widget, args); return self->action_class->activate (self, flags, widget, args);
} }
static char *
string_is_function (const char *string,
const char *function_name)
{
gsize len;
if (!g_str_has_prefix (string, function_name))
return NULL;
string += strlen (function_name);
if (string[0] != '(')
return NULL;
string ++;
len = strlen (string);
if (len == 0 || string[len - 1] != ')')
return NULL;
return g_strndup (string, len - 1);
}
GtkShortcutAction *
gtk_shortcut_action_parse_builder (GtkBuilder *builder,
const char *string,
GError **error)
{
GtkShortcutAction *result;
char *arg;
if (g_str_equal (string, "nothing"))
return gtk_nothing_action_new ();
if (g_str_equal (string, "activate"))
return gtk_activate_action_new ();
if (g_str_equal (string, "mnemonic-activate"))
return gtk_mnemonic_action_new ();
if ((arg = string_is_function (string, "action")))
{
result = gtk_action_action_new (arg);
g_free (arg);
}
else if ((arg = string_is_function (string, "signal")))
{
result = gtk_signal_action_new (arg);
g_free (arg);
}
else if ((arg = string_is_function (string, "gaction")))
{
GObject *object = gtk_builder_get_object (builder, arg);
if (object == NULL)
{
g_set_error (error,
GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ID,
"Action with ID \"%s\" not found", arg);
g_free (arg);
return NULL;
}
else if (!G_IS_ACTION (object))
{
g_set_error (error,
GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_ID,
"Object with ID \"%s\" is not a GAction", arg);
g_free (arg);
return NULL;
}
result = gtk_gaction_action_new (G_ACTION (object));
g_free (arg);
}
else
{
g_set_error (error,
GTK_BUILDER_ERROR, GTK_BUILDER_ERROR_INVALID_VALUE,
"String \"%s\" does not specify a GtkShortcutAction", string);
return NULL;
}
return result;
}
/*** GTK_SHORTCUT_ACTION_NOTHING ***/ /*** GTK_SHORTCUT_ACTION_NOTHING ***/
typedef struct _GtkNothingAction GtkNothingAction; typedef struct _GtkNothingAction GtkNothingAction;

View File

@ -0,0 +1,29 @@
/*
* Copyright © 2018 Benjamin Otte
*
* 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.
*
* 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: Benjamin Otte <otte@gnome.org>
*/
#ifndef __GTK_SHORTCUT_ACTION_PRIVATE_H__
#define __GTK_SHORTCUT_ACTION_PRIVATE_H__
#include "gtkshortcutaction.h"
GtkShortcutAction * gtk_shortcut_action_parse_builder (GtkBuilder *builder,
const char *string,
GError **error);
#endif /* __GTK_SHORTCUT_ACTION_PRIVATE_H__ */