From: Christopher Davis Date: Sun, 12 Feb 2023 21:14:43 +0000 (-0500) Subject: menubutton: Add `active` property and getter/setter X-Git-Tag: archive/raspbian/4.12.3+ds-1+rpi1~1^2^2^2~22^2~6^2~76^2 X-Git-Url: https://dgit.raspbian.org/?a=commitdiff_plain;h=ff45145eac76255a0d3d95063c7f053302611471;p=gtk4.git menubutton: Add `active` property and getter/setter GtkMenuButton currently does not provide a way to tell if it's open programmatically. The existing methods, `popup()` and `popdown()`, do not expose any state to callers. If someone wanted to know whether or not a menubutton was open, they needed the popover. Given that GtkMenuButton can manage the popovers itself, that's not always an option for app developers. This commit adds the `active` property and associated methods, where `gtk_menu_button_set_active ()` replaces both `gtk_menu_popup ()` and `gtk_menu_popdown ()`. This addition also mirrors changes in other places, Such as `GtkWidget:visible` vs `show()`/`hide()`. --- diff --git a/gtk/gtkmenubutton.c b/gtk/gtkmenubutton.c index 73a5fe0a65..872be35f1d 100644 --- a/gtk/gtkmenubutton.c +++ b/gtk/gtkmenubutton.c @@ -150,6 +150,7 @@ enum PROP_HAS_FRAME, PROP_PRIMARY, PROP_CHILD, + PROP_ACTIVE, LAST_PROP }; @@ -208,6 +209,9 @@ gtk_menu_button_set_property (GObject *object, case PROP_CHILD: gtk_menu_button_set_child (self, g_value_get_object (value)); break; + case PROP_ACTIVE: + gtk_menu_button_set_active (self, g_value_get_boolean (value)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -253,6 +257,9 @@ gtk_menu_button_get_property (GObject *object, case PROP_CHILD: g_value_set_object (value, gtk_menu_button_get_child (self)); break; + case PROP_ACTIVE: + g_value_set_boolean (value, gtk_menu_button_get_active (self)); + break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); } @@ -314,6 +321,8 @@ gtk_menu_button_toggled (GtkMenuButton *self) GTK_ACCESSIBLE_STATE_EXPANDED); } } + + g_object_notify_by_pspec (G_OBJECT (self), menu_button_props[PROP_ACTIVE]); } static void @@ -513,6 +522,18 @@ gtk_menu_button_class_init (GtkMenuButtonClass *klass) GTK_TYPE_WIDGET, GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); + /** + * GtkMenuButton:active: (attributes org.gtk.Property.get=gtk_menu_button_get_active org.gtk.Property.set=gtk_menu_button_set_active) + * + * Whether the menu button is active. + * + * Since: 4.10 + */ + menu_button_props[PROP_ACTIVE] = + g_param_spec_boolean ("active", NULL, NULL, + FALSE, + GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); + g_object_class_install_properties (gobject_class, LAST_PROP, menu_button_props); /** @@ -1499,3 +1520,45 @@ gtk_menu_button_get_child (GtkMenuButton *menu_button) return menu_button->child; } + +/** + * gtk_menu_button_set_active: (attributes org.gtk.Method.set_property=active) + * @menu_button: a `GtkMenuButton` + * @active: whether the menu button is active + * + * Sets whether menu button acts is active. + * + * Since: 4.10 + */ +void +gtk_menu_button_set_active (GtkMenuButton *menu_button, + gboolean active) +{ + g_return_if_fail (GTK_IS_MENU_BUTTON (menu_button)); + + if (active == gtk_menu_button_get_active (menu_button)) + return; + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menu_button->button), + active); + + g_object_notify_by_pspec (G_OBJECT (menu_button), menu_button_props[PROP_ACTIVE]); +} + +/** + * gtk_menu_button_get_active: (attributes org.gtk.Method.get_property=active) + * @menu_button: a `GtkMenuButton` + * + * Returns whether the menu button is active. + * + * Returns: TRUE if the button is active + * + * Since: 4.10 + */ +gboolean +gtk_menu_button_get_active (GtkMenuButton *menu_button) +{ + g_return_val_if_fail (GTK_IS_MENU_BUTTON (menu_button), FALSE); + + return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (menu_button->button)); +} diff --git a/gtk/gtkmenubutton.h b/gtk/gtkmenubutton.h index 412f43ca3b..87d87976b2 100644 --- a/gtk/gtkmenubutton.h +++ b/gtk/gtkmenubutton.h @@ -127,6 +127,12 @@ void gtk_menu_button_set_child (GtkMenuButton *menu_button, GDK_AVAILABLE_IN_4_6 GtkWidget * gtk_menu_button_get_child (GtkMenuButton *menu_button); +GDK_AVAILABLE_IN_4_10 +void gtk_menu_button_set_active (GtkMenuButton *menu_button, + gboolean active); +GDK_AVAILABLE_IN_4_10 +gboolean gtk_menu_button_get_active (GtkMenuButton *menu_button); + G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkMenuButton, g_object_unref) G_END_DECLS