diff options
author | Benjamin Otte <otte@redhat.com> | 2018-08-13 05:45:12 +0200 |
---|---|---|
committer | Matthias Clasen <mclasen@redhat.com> | 2020-03-25 23:14:27 -0400 |
commit | 2309e1dd38daabc7b3e0fefb047ed8e3be78a3c0 (patch) | |
tree | 2ef650646e8a3626dc520a07d2d05fe5d23e9772 /gtk/gtkshortcuttrigger.c | |
parent | 90c7f4608a3840ec8aec9285ac3de8a63200907a (diff) | |
download | gtk+-2309e1dd38daabc7b3e0fefb047ed8e3be78a3c0.tar.gz |
shortcuttrigger: Add gtk_shortcut_trigger_to_label()
Provide a user-presentable string.
Diffstat (limited to 'gtk/gtkshortcuttrigger.c')
-rw-r--r-- | gtk/gtkshortcuttrigger.c | 120 |
1 files changed, 115 insertions, 5 deletions
diff --git a/gtk/gtkshortcuttrigger.c b/gtk/gtkshortcuttrigger.c index 5044e7a164..cdefa82059 100644 --- a/gtk/gtkshortcuttrigger.c +++ b/gtk/gtkshortcuttrigger.c @@ -39,7 +39,7 @@ #include "gtkshortcuttrigger.h" -#include "gtkaccelgroup.h" +#include "gtkaccelgroupprivate.h" typedef struct _GtkShortcutTriggerClass GtkShortcutTriggerClass; @@ -63,6 +63,9 @@ struct _GtkShortcutTriggerClass GdkEvent *event); void (* print) (GtkShortcutTrigger *trigger, GString *string); + gboolean (* print_label) (GtkShortcutTrigger *trigger, + GdkDisplay *display, + GString *string); }; G_DEFINE_BOXED_TYPE (GtkShortcutTrigger, gtk_shortcut_trigger, @@ -214,6 +217,71 @@ gtk_shortcut_trigger_print (GtkShortcutTrigger *self, return self->trigger_class->print (self, string); } +/** + * gtk_shortcut_trigger_to_label: + * @self: a #GtkShortcutTrigger + * @display: #GdkDisplay to print for + * + * Gets textual representation for the given trigger. This + * function is returning a translated string for presentation + * to end users for example in menu items or in help texts. + * + * The @display in use may influence the resulting string in + * various forms, such as resolving hardware keycodes or by + * causing display-specific modifier names. + * + * The form of the representation may change at any time and is + * not guaranteed to stay identical. + * + * Returns: (transfer full): a new string + **/ +char * +gtk_shortcut_trigger_to_label (GtkShortcutTrigger *self, + GdkDisplay *display) +{ + GString *string; + + g_return_val_if_fail (self != NULL, NULL); + + string = g_string_new (NULL); + gtk_shortcut_trigger_print_label (self, display, string); + + return g_string_free (string, FALSE); +} + +/** + * gtk_shortcut_trigger_print_label: + * @self: a #GtkShortcutTrigger + * @display: #GdkDisplay to print for + * @string: a #GString to print into + * + * Prints the given trigger into a string. This function is + * returning a translated string for presentation to end users + * for example in menu items or in help texts. + * + * The @display in use may influence the resulting string in + * various forms, such as resolving hardware keycodes or by + * causing display-specific modifier names. + * + * The form of the representation may change at any time and is + * not guaranteed to stay identical. + * + * Returns: %TRUE if something was printed or %FALSE if the + * trigger did not have a textual representation suitable + * for end users. + **/ +gboolean +gtk_shortcut_trigger_print_label (GtkShortcutTrigger *self, + GdkDisplay *display, + GString *string) +{ + g_return_val_if_fail (GTK_IS_SHORTCUT_TRIGGER (self), FALSE); + g_return_val_if_fail (GDK_IS_DISPLAY (display), FALSE); + g_return_val_if_fail (string != NULL, FALSE); + + return self->trigger_class->print_label (self, display, string); +} + /*** GTK_SHORTCUT_TRIGGER_NEVER ***/ typedef struct _GtkNeverTrigger GtkNeverTrigger; @@ -246,13 +314,22 @@ gtk_never_trigger_print (GtkShortcutTrigger *trigger, g_string_append (string, "<never>"); } +static gboolean +gtk_never_trigger_print_label (GtkShortcutTrigger *trigger, + GdkDisplay *display, + GString *string) +{ + return FALSE; +} + static const GtkShortcutTriggerClass GTK_NEVER_TRIGGER_CLASS = { GTK_SHORTCUT_TRIGGER_NEVER, sizeof (GtkNeverTrigger), "GtkNeverTrigger", gtk_never_trigger_finalize, gtk_never_trigger_trigger, - gtk_never_trigger_print + gtk_never_trigger_print, + gtk_never_trigger_print_label }; static GtkNeverTrigger never = { { >K_NEVER_TRIGGER_CLASS, 1 } }; @@ -324,13 +401,26 @@ gtk_keyval_trigger_print (GtkShortcutTrigger *trigger, g_free (accelerator_name); } +static gboolean +gtk_keyval_trigger_print_label (GtkShortcutTrigger *trigger, + GdkDisplay *display, + GString *string) +{ + GtkKeyvalTrigger *self = (GtkKeyvalTrigger *) trigger; + + gtk_accelerator_print_label (string, self->keyval, self->modifiers); + + return TRUE; +} + static const GtkShortcutTriggerClass GTK_KEYVAL_TRIGGER_CLASS = { GTK_SHORTCUT_TRIGGER_KEYVAL, sizeof (GtkKeyvalTrigger), "GtkKeyvalTrigger", gtk_keyval_trigger_finalize, gtk_keyval_trigger_trigger, - gtk_keyval_trigger_print + gtk_keyval_trigger_print, + gtk_keyval_trigger_print_label }; /** @@ -438,7 +528,6 @@ gtk_alternative_trigger_trigger (GtkShortcutTrigger *trigger, static void gtk_alternative_trigger_print (GtkShortcutTrigger *trigger, GString *string) - { GtkAlternativeTrigger *self = (GtkAlternativeTrigger *) trigger; @@ -447,13 +536,34 @@ gtk_alternative_trigger_print (GtkShortcutTrigger *trigger, gtk_shortcut_trigger_print (self->second, string); } +static gboolean +gtk_alternative_trigger_print_label (GtkShortcutTrigger *trigger, + GdkDisplay *display, + GString *string) +{ + GtkAlternativeTrigger *self = (GtkAlternativeTrigger *) trigger; + + if (gtk_shortcut_trigger_print_label (self->first, display, string)) + { + g_string_append (string, ", "); + if (!gtk_shortcut_trigger_print_label (self->second, display, string)) + g_string_truncate (string, string->len - 2); + return TRUE; + } + else + { + return gtk_shortcut_trigger_print_label (self->second, display, string); + } +} + static const GtkShortcutTriggerClass GTK_ALTERNATIVE_TRIGGER_CLASS = { GTK_SHORTCUT_TRIGGER_ALTERNATIVE, sizeof (GtkAlternativeTrigger), "GtkAlternativeTrigger", gtk_alternative_trigger_finalize, gtk_alternative_trigger_trigger, - gtk_alternative_trigger_print + gtk_alternative_trigger_print, + gtk_alternative_trigger_print_label }; /** |