[gtk/new-style-menu: 2/2] model button: Show shortcuts



commit 5241bf107541a85e1c3c8c02ffa4b59857a97548
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jun 6 04:48:13 2019 +0000

    model button: Show shortcuts
    
    Most of the machinery for this is already
    in place. We just need to add an accel property
    to GtkMenuButton and pass it through.

 gtk/gtkmenusectionbox.c |  4 ++++
 gtk/gtkmodelbutton.c    | 51 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 55 insertions(+)
---
diff --git a/gtk/gtkmenusectionbox.c b/gtk/gtkmenusectionbox.c
index 9179d2a0ce..e512d7bc33 100644
--- a/gtk/gtkmenusectionbox.c
+++ b/gtk/gtkmenusectionbox.c
@@ -342,6 +342,7 @@ gtk_menu_section_box_insert_func (GtkMenuTrackerItem *item,
       g_object_bind_property (item, "sensitive", widget, "sensitive", G_BINDING_SYNC_CREATE);
       g_object_bind_property (item, "role", widget, "role", G_BINDING_SYNC_CREATE);
       g_object_bind_property (item, "toggled", widget, "active", G_BINDING_SYNC_CREATE);
+      g_object_bind_property (item, "accel", widget, "accel", G_BINDING_SYNC_CREATE);
       g_signal_connect (widget, "clicked", G_CALLBACK (gtk_popover_item_activate), item);
     }
 
@@ -532,6 +533,7 @@ gtk_menu_section_box_new_section (GtkMenuTrackerItem *item,
       gtk_box_set_homogeneous (box->item_box, TRUE);
       gtk_orientable_set_orientation (GTK_ORIENTABLE (box->item_box), GTK_ORIENTATION_HORIZONTAL);
       gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (box->item_box)), 
GTK_STYLE_CLASS_LINKED);
+      gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (box->item_box)), 
"horizontal-buttons");
       box->iconic = TRUE;
 
       if (text_direction)
@@ -554,6 +556,7 @@ gtk_menu_section_box_new_section (GtkMenuTrackerItem *item,
       box->inline_buttons = TRUE;
 
       gtk_orientable_set_orientation (GTK_ORIENTABLE (box->item_box), GTK_ORIENTATION_HORIZONTAL);
+      gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (box->item_box)), 
"inline-buttons");
 
       spacer = gtk_icon_new ("none");
       gtk_icon_set_image (GTK_ICON (spacer), GTK_CSS_IMAGE_BUILTIN_NONE);
@@ -579,6 +582,7 @@ gtk_menu_section_box_new_section (GtkMenuTrackerItem *item,
     {
       gtk_box_set_homogeneous (box->item_box, TRUE);
       gtk_orientable_set_orientation (GTK_ORIENTABLE (box->item_box), GTK_ORIENTATION_HORIZONTAL);
+      gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (box->item_box)), 
"circular-buttons");
       box->circular = TRUE;
     }
 
diff --git a/gtk/gtkmodelbutton.c b/gtk/gtkmodelbutton.c
index 82676d0f37..7591ecade5 100644
--- a/gtk/gtkmodelbutton.c
+++ b/gtk/gtkmodelbutton.c
@@ -39,6 +39,8 @@
 #include "gtkcontainerprivate.h"
 #include "gtkiconprivate.h"
 #include "gtksizegroup.h"
+#include "gtkaccellabelprivate.h"
+#include "gtkactionable.h"
 
 /**
  * SECTION:gtkmodelbutton
@@ -153,6 +155,7 @@ struct _GtkModelButton
   GtkWidget *box;
   GtkWidget *image;
   GtkWidget *label;
+  GtkWidget *accel;
   GtkWidget *start_indicator;
   GtkWidget *end_indicator;
   gboolean active;
@@ -177,6 +180,7 @@ enum
   PROP_ACTIVE,
   PROP_MENU_NAME,
   PROP_ICONIC,
+  PROP_ACCEL,
   PROP_INDICATOR_SIZE_GROUP,
   LAST_PROPERTY
 };
@@ -517,6 +521,31 @@ gtk_model_button_set_iconic (GtkModelButton *button,
   g_object_notify_by_pspec (G_OBJECT (button), properties[PROP_ICONIC]);
 }
 
+static void
+gtk_model_button_set_accel (GtkModelButton *button,
+                            const char     *accel)
+{
+  if (accel)
+    {
+      guint key;
+      GdkModifierType mods;
+      GtkAccelLabelClass *accel_class;
+      char *str;
+
+      gtk_accelerator_parse (accel, &key, &mods);
+
+      accel_class = g_type_class_ref (GTK_TYPE_ACCEL_LABEL);
+      str = _gtk_accel_label_class_get_accelerator_label (accel_class, key, mods);
+      gtk_label_set_label (GTK_LABEL (button->accel), str);
+      g_free (str);
+      g_type_class_unref (accel_class);
+
+      gtk_widget_show (button->accel);
+    }
+  else
+    gtk_widget_hide (button->accel);
+}
+
 static void
 gtk_model_button_get_property (GObject    *object,
                                guint       prop_id,
@@ -555,6 +584,10 @@ gtk_model_button_get_property (GObject    *object,
       g_value_set_boolean (value, button->iconic);
       break;
 
+    case PROP_ACCEL:
+      g_value_set_string (value, gtk_label_get_label (GTK_LABEL (button->accel)));
+      break;
+
     case PROP_INDICATOR_SIZE_GROUP:
       g_value_set_object (value, button->indicators);
       break;
@@ -603,6 +636,10 @@ gtk_model_button_set_property (GObject      *object,
       gtk_model_button_set_iconic (button, g_value_get_boolean (value));
       break;
 
+    case PROP_ACCEL:
+      gtk_model_button_set_accel (button, g_value_get_string (value));
+      break;
+
     case PROP_INDICATOR_SIZE_GROUP:
       if (button->indicators)
         gtk_size_group_remove_widget (button->indicators, button->start_indicator);
@@ -1021,6 +1058,12 @@ gtk_model_button_class_init (GtkModelButtonClass *class)
                           P_("Size group"),
                           GTK_TYPE_SIZE_GROUP,
                           G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
+  properties[PROP_ACCEL] =
+    g_param_spec_string ("accel",
+                         P_("Accel"),
+                         P_("The accelerator"),
+                         NULL,
+                         G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
   g_object_class_install_properties (object_class, LAST_PROPERTY, properties);
 
   gtk_widget_class_set_accessible_role (GTK_WIDGET_CLASS (class), ATK_ROLE_PUSH_BUTTON);
@@ -1038,8 +1081,16 @@ gtk_model_button_init (GtkModelButton *button)
   gtk_widget_hide (button->image);
   button->label = gtk_label_new ("");
   gtk_widget_hide (button->label);
+  button->accel = g_object_new (GTK_TYPE_LABEL,
+                                "css-name", "accelerator",
+                                NULL);
+  gtk_widget_set_hexpand (button->accel, TRUE);
+  gtk_label_set_xalign (GTK_LABEL (button->accel), 0.0f);
+  gtk_widget_set_halign (button->accel, GTK_ALIGN_END);
+  gtk_widget_hide (button->accel);
   gtk_container_add (GTK_CONTAINER (button->box), button->image);
   gtk_container_add (GTK_CONTAINER (button->box), button->label);
+  gtk_container_add (GTK_CONTAINER (button->box), button->accel);
   gtk_container_add (GTK_CONTAINER (button), button->box);
 
   button->start_indicator = gtk_icon_new ("none");


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]