diff options
Diffstat (limited to 'gtk')
-rw-r--r-- | gtk/gtkbuilder.c | 8 | ||||
-rw-r--r-- | gtk/gtkshortcutaction.c | 59 | ||||
-rw-r--r-- | gtk/gtkshortcutactionprivate.h | 29 |
3 files changed, 95 insertions, 1 deletions
diff --git a/gtk/gtkbuilder.c b/gtk/gtkbuilder.c index c2285dfbf9..7045e8f9df 100644 --- a/gtk/gtkbuilder.c +++ b/gtk/gtkbuilder.c @@ -222,6 +222,7 @@ #include "gtkicontheme.h" #include "gtkintl.h" #include "gtkprivate.h" +#include "gtkshortcutactionprivate.h" #include "gtkshortcuttrigger.h" #include "gtktestutils.h" #include "gtktypebuiltins.h" @@ -2111,6 +2112,13 @@ gtk_builder_value_from_string_type (GtkBuilder *builder, 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)) { gchar **vector = g_strsplit (string, "\n", 0); diff --git a/gtk/gtkshortcutaction.c b/gtk/gtkshortcutaction.c index f919954e50..301985bd9a 100644 --- a/gtk/gtkshortcutaction.c +++ b/gtk/gtkshortcutaction.c @@ -37,8 +37,9 @@ #include "config.h" -#include "gtkshortcutaction.h" +#include "gtkshortcutactionprivate.h" +#include "gtkbuilder.h" #include "gtkwidgetprivate.h" typedef struct _GtkShortcutActionClass GtkShortcutActionClass; @@ -226,6 +227,62 @@ gtk_shortcut_action_activate (GtkShortcutAction *self, 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); + } + { + 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 ***/ typedef struct _GtkNothingAction GtkNothingAction; diff --git a/gtk/gtkshortcutactionprivate.h b/gtk/gtkshortcutactionprivate.h new file mode 100644 index 0000000000..4c5a0db3ef --- /dev/null +++ b/gtk/gtkshortcutactionprivate.h @@ -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__ */ |