diff --git a/docs/reference/gtk/gtk4-sections.txt b/docs/reference/gtk/gtk4-sections.txt index f511ab6d9d..54b4ee9fa9 100644 --- a/docs/reference/gtk/gtk4-sections.txt +++ b/docs/reference/gtk/gtk4-sections.txt @@ -565,6 +565,8 @@ GtkCheckButton gtk_check_button_new gtk_check_button_new_with_label gtk_check_button_new_with_mnemonic +gtk_check_button_get_draw_indicator +gtk_check_button_set_draw_indicator GTK_CHECK_BUTTON GTK_IS_CHECK_BUTTON @@ -3188,8 +3190,6 @@ GtkToggleButton gtk_toggle_button_new gtk_toggle_button_new_with_label gtk_toggle_button_new_with_mnemonic -gtk_toggle_button_set_mode -gtk_toggle_button_get_mode gtk_toggle_button_toggled gtk_toggle_button_get_active gtk_toggle_button_set_active diff --git a/gtk/gtkcheckbutton.c b/gtk/gtkcheckbutton.c index 167095a63e..08fbcdf5e5 100644 --- a/gtk/gtkcheckbutton.c +++ b/gtk/gtkcheckbutton.c @@ -63,7 +63,7 @@ * ╰── * ]| * - * A GtkCheckButton with indicator (see gtk_toggle_button_set_mode()) has a + * A GtkCheckButton with indicator (see gtk_check_button_set_draw_indicator()) has a * main CSS node with name checkbutton and a subnode with name check. * * |[ @@ -86,10 +86,21 @@ static void gtk_check_button_snapshot (GtkWidget *widget, typedef struct { GtkCssGadget *gadget; GtkCssGadget *indicator_gadget; + + guint draw_indicator : 1; } GtkCheckButtonPrivate; +enum { + PROP_0, + PROP_DRAW_INDICATOR, + NUM_PROPERTIES +}; + +static GParamSpec *props[NUM_PROPERTIES] = { NULL, }; + G_DEFINE_TYPE_WITH_PRIVATE (GtkCheckButton, gtk_check_button, GTK_TYPE_TOGGLE_BUTTON) + static void gtk_check_button_update_node_state (GtkWidget *widget) { @@ -186,7 +197,7 @@ gtk_check_button_measure (GtkWidget *widget, GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (GTK_CHECK_BUTTON (widget)); GtkCssGadget *gadget; - if (gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (widget))) + if (priv->draw_indicator) gadget = priv->gadget; else gadget = GTK_BUTTON (widget)->priv->gadget; @@ -198,6 +209,42 @@ gtk_check_button_measure (GtkWidget *widget, minimum_baseline, natural_baseline); } +static void +gtk_check_button_set_property (GObject *object, + guint prop_id, + const GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) + { + case PROP_DRAW_INDICATOR: + gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (object), + g_value_get_boolean (value)); + + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + +static void +gtk_check_button_get_property (GObject *object, + guint prop_id, + GValue *value, + GParamSpec *pspec) +{ + switch (prop_id) + { + case PROP_DRAW_INDICATOR: + g_value_set_boolean (value, gtk_check_button_get_draw_indicator (GTK_CHECK_BUTTON (object))); + break; + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); + break; + } +} + static void gtk_check_button_class_init (GtkCheckButtonClass *class) { @@ -206,6 +253,8 @@ gtk_check_button_class_init (GtkCheckButtonClass *class) GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class); object_class->finalize = gtk_check_button_finalize; + object_class->set_property = gtk_check_button_set_property; + object_class->get_property = gtk_check_button_get_property; widget_class->measure = gtk_check_button_measure; widget_class->size_allocate = gtk_check_button_size_allocate; @@ -216,32 +265,38 @@ gtk_check_button_class_init (GtkCheckButtonClass *class) container_class->add = gtk_check_button_add; container_class->remove = gtk_check_button_remove; + props[PROP_DRAW_INDICATOR] = + g_param_spec_boolean ("draw-indicator", + P_("Draw Indicator"), + P_("If the indicator part of the button is displayed"), + TRUE, + GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); + + g_object_class_install_properties (object_class, NUM_PROPERTIES, props); + gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_CHECK_BOX); gtk_widget_class_set_css_name (widget_class, "checkbutton"); } static void -draw_indicator_changed (GObject *object, - GParamSpec *pspec, - gpointer user_data) +draw_indicator_changed (GtkCheckButton *check_button) { - GtkButton *button = GTK_BUTTON (object); - GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (GTK_CHECK_BUTTON (button)); + GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button); GtkCssNode *widget_node; GtkCssNode *indicator_node; - widget_node = gtk_widget_get_css_node (GTK_WIDGET (button)); + widget_node = gtk_widget_get_css_node (GTK_WIDGET (check_button)); indicator_node = gtk_css_gadget_get_node (priv->indicator_gadget); - if (gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (button))) + if (priv->draw_indicator) { gtk_css_node_set_visible (indicator_node, TRUE); - if (GTK_IS_RADIO_BUTTON (button)) + if (GTK_IS_RADIO_BUTTON (check_button)) { gtk_css_node_remove_class (widget_node, g_quark_from_static_string ("radio")); gtk_css_node_set_name (widget_node, I_("radiobutton")); } - else if (GTK_IS_CHECK_BUTTON (button)) + else if (GTK_IS_CHECK_BUTTON (check_button)) { gtk_css_node_remove_class (widget_node, g_quark_from_static_string ("check")); gtk_css_node_set_name (widget_node, I_("checkbutton")); @@ -250,12 +305,12 @@ draw_indicator_changed (GObject *object, else { gtk_css_node_set_visible (indicator_node, FALSE); - if (GTK_IS_RADIO_BUTTON (button)) + if (GTK_IS_RADIO_BUTTON (check_button)) { gtk_css_node_add_class (widget_node, g_quark_from_static_string ("radio")); gtk_css_node_set_name (widget_node, I_("button")); } - else if (GTK_IS_CHECK_BUTTON (button)) + else if (GTK_IS_CHECK_BUTTON (check_button)) { gtk_css_node_add_class (widget_node, g_quark_from_static_string ("check")); gtk_css_node_set_name (widget_node, I_("button")); @@ -269,9 +324,9 @@ gtk_check_button_init (GtkCheckButton *check_button) GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button); GtkCssNode *widget_node; + priv->draw_indicator = TRUE; + gtk_widget_set_receives_default (GTK_WIDGET (check_button), FALSE); - g_signal_connect (check_button, "notify::draw-indicator", G_CALLBACK (draw_indicator_changed), NULL); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (check_button), TRUE); gtk_style_context_remove_class (gtk_widget_get_style_context (GTK_WIDGET (check_button)), "toggle"); @@ -347,7 +402,7 @@ gtk_check_button_size_allocate (GtkWidget *widget, PangoContext *pango_context; PangoFontMetrics *metrics; - if (gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (widget))) + if (priv->draw_indicator) gadget = priv->gadget; else gadget = button->priv->gadget; @@ -387,7 +442,7 @@ gtk_check_button_snapshot (GtkWidget *widget, { GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (GTK_CHECK_BUTTON (widget)); - if (!gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (widget))) + if (!priv->draw_indicator) GTK_WIDGET_CLASS (gtk_check_button_parent_class)->snapshot (widget, snapshot); else gtk_css_gadget_snapshot (priv->gadget, snapshot); @@ -400,3 +455,49 @@ gtk_check_button_get_indicator_node (GtkCheckButton *check_button) return gtk_css_gadget_get_node (priv->indicator_gadget); } + +/** + * gtk_check_button_set_draw_indicator: + * @check_button: a #GtkCheckButton + * @draw_indicator: Whether or not to draw the indicator part of the button + * + * Sets whether the indicator part of the button is drawn. This is important for + * cases where the check button should have the functinality of a check button, + * but the visuals of a regular button, like in a #GtkStackSwitcher. + */ +void +gtk_check_button_set_draw_indicator (GtkCheckButton *check_button, + gboolean draw_indicator) +{ + GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button); + + g_return_if_fail (GTK_IS_CHECK_BUTTON (check_button)); + + draw_indicator = !!draw_indicator; + + if (draw_indicator != priv->draw_indicator) + { + priv->draw_indicator = draw_indicator; + draw_indicator_changed (check_button); + gtk_widget_queue_resize (GTK_WIDGET (check_button)); + g_object_notify_by_pspec (G_OBJECT (check_button), props[PROP_DRAW_INDICATOR]); + } +} + +/** + * gtk_check_button_get_draw_indicator: + * @check_button: a #GtkCheckButton + * + * Returns Whether or not the indicator part of the button gets drawn. + * + * Returns: The value of the GtkCheckButton:draw-indicator property. + */ +gboolean +gtk_check_button_get_draw_indicator (GtkCheckButton *check_button) +{ + GtkCheckButtonPrivate *priv = gtk_check_button_get_instance_private (check_button); + + g_return_val_if_fail (GTK_IS_CHECK_BUTTON (check_button), FALSE); + + return priv->draw_indicator; +} diff --git a/gtk/gtkcheckbutton.h b/gtk/gtkcheckbutton.h index 416ee96ccf..fff8cad0d4 100644 --- a/gtk/gtkcheckbutton.h +++ b/gtk/gtkcheckbutton.h @@ -71,6 +71,11 @@ GDK_AVAILABLE_IN_ALL GtkWidget* gtk_check_button_new_with_label (const gchar *label); GDK_AVAILABLE_IN_ALL GtkWidget* gtk_check_button_new_with_mnemonic (const gchar *label); +GDK_AVAILABLE_IN_3_90 +void gtk_check_button_set_draw_indicator (GtkCheckButton *check_button, + gboolean draw_indicator); +GDK_AVAILABLE_IN_3_90 +gboolean gtk_check_button_get_draw_indicator (GtkCheckButton *check_button); G_END_DECLS diff --git a/gtk/gtkradiobutton.c b/gtk/gtkradiobutton.c index b351227d6b..a86cd37599 100644 --- a/gtk/gtkradiobutton.c +++ b/gtk/gtkradiobutton.c @@ -80,7 +80,7 @@ * ╰── * ]| * - * A GtkRadioButton with indicator (see gtk_toggle_button_set_mode()) has a + * A GtkRadioButton with indicator (see gtk_check_button_set_draw_indicator())) has a * main CSS node with name radiobutton and a subnode with name radio. * * |[ @@ -631,7 +631,7 @@ gtk_radio_button_focus (GtkWidget *widget, /* Radio buttons with draw_indicator unset focus "normally", since * they look like buttons to the user. */ - if (!gtk_toggle_button_get_mode (GTK_TOGGLE_BUTTON (widget))) + if (!gtk_check_button_get_draw_indicator (GTK_CHECK_BUTTON (widget))) return GTK_WIDGET_CLASS (gtk_radio_button_parent_class)->focus (widget, direction); if (gtk_widget_is_focus (widget)) diff --git a/gtk/gtkradiotoolbutton.c b/gtk/gtkradiotoolbutton.c index 81665ddddd..74e26810ce 100644 --- a/gtk/gtkradiotoolbutton.c +++ b/gtk/gtkradiotoolbutton.c @@ -91,7 +91,7 @@ static void gtk_radio_tool_button_init (GtkRadioToolButton *button) { GtkToolButton *tool_button = GTK_TOOL_BUTTON (button); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (_gtk_tool_button_get_button (tool_button)), FALSE); + gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (_gtk_tool_button_get_button (tool_button)), FALSE); } static void diff --git a/gtk/gtkstackswitcher.c b/gtk/gtkstackswitcher.c index 723900ed7f..37678fdd31 100644 --- a/gtk/gtkstackswitcher.c +++ b/gtk/gtkstackswitcher.c @@ -360,7 +360,7 @@ add_child (GtkWidget *widget, button = gtk_radio_button_new (NULL); gtk_widget_set_focus_on_click (button, FALSE); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE); + gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (button), FALSE); update_button (self, widget, button); diff --git a/gtk/gtktogglebutton.c b/gtk/gtktogglebutton.c index 3a20c778e3..95e18d1d85 100644 --- a/gtk/gtktogglebutton.c +++ b/gtk/gtktogglebutton.c @@ -76,10 +76,6 @@ * text = "Hi, i’m a toggle button."; * toggle1 = gtk_toggle_button_new_with_label (text); * - * // Makes this toggle button invisible - * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle1), - * TRUE); - * * g_signal_connect (toggle1, "toggled", * G_CALLBACK (output_state), * NULL); @@ -88,8 +84,6 @@ * * text = "Hi, i’m a toggle button."; * toggle2 = gtk_toggle_button_new_with_label (text); - * gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (toggle2), - * FALSE); * g_signal_connect (toggle2, "toggled", * G_CALLBACK (output_state), * NULL); @@ -104,7 +98,6 @@ struct _GtkToggleButtonPrivate { guint active : 1; - guint draw_indicator : 1; guint inconsistent : 1; }; @@ -117,7 +110,6 @@ enum { PROP_0, PROP_ACTIVE, PROP_INCONSISTENT, - PROP_DRAW_INDICATOR, NUM_PROPERTIES }; @@ -175,13 +167,6 @@ gtk_toggle_button_class_init (GtkToggleButtonClass *class) FALSE, GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); - toggle_button_props[PROP_DRAW_INDICATOR] = - g_param_spec_boolean ("draw-indicator", - P_("Draw Indicator"), - P_("If the toggle part of the button is displayed"), - FALSE, - GTK_PARAM_READWRITE); - g_object_class_install_properties (gobject_class, NUM_PROPERTIES, toggle_button_props); /** @@ -211,7 +196,6 @@ gtk_toggle_button_init (GtkToggleButton *toggle_button) toggle_button->priv = gtk_toggle_button_get_instance_private (toggle_button); toggle_button->priv->active = FALSE; - toggle_button->priv->draw_indicator = FALSE; context = gtk_widget_get_style_context (GTK_WIDGET (toggle_button)); gtk_style_context_add_class (context, "toggle"); @@ -283,9 +267,6 @@ gtk_toggle_button_set_property (GObject *object, case PROP_INCONSISTENT: gtk_toggle_button_set_inconsistent (tb, g_value_get_boolean (value)); break; - case PROP_DRAW_INDICATOR: - gtk_toggle_button_set_mode (tb, g_value_get_boolean (value)); - break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; @@ -309,73 +290,12 @@ gtk_toggle_button_get_property (GObject *object, case PROP_INCONSISTENT: g_value_set_boolean (value, priv->inconsistent); break; - case PROP_DRAW_INDICATOR: - g_value_set_boolean (value, priv->draw_indicator); - break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } } -/** - * gtk_toggle_button_set_mode: - * @toggle_button: a #GtkToggleButton - * @draw_indicator: if %TRUE, draw the button as a separate indicator - * and label; if %FALSE, draw the button like a normal button - * - * Sets whether the button is displayed as a separate indicator and label. - * You can call this function on a checkbutton or a radiobutton with - * @draw_indicator = %FALSE to make the button look like a normal button. - * - * This can be used to create linked strip of buttons that work like - * a #GtkStackSwitcher. - * - * This function only affects instances of classes like #GtkCheckButton - * and #GtkRadioButton that derive from #GtkToggleButton, - * not instances of #GtkToggleButton itself. - */ -void -gtk_toggle_button_set_mode (GtkToggleButton *toggle_button, - gboolean draw_indicator) -{ - GtkToggleButtonPrivate *priv; - - g_return_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button)); - - priv = toggle_button->priv; - - draw_indicator = draw_indicator ? TRUE : FALSE; - - if (priv->draw_indicator != draw_indicator) - { - priv->draw_indicator = draw_indicator; - - if (gtk_widget_get_visible (GTK_WIDGET (toggle_button))) - gtk_widget_queue_resize (GTK_WIDGET (toggle_button)); - - g_object_notify_by_pspec (G_OBJECT (toggle_button), toggle_button_props[PROP_DRAW_INDICATOR]); - } -} - -/** - * gtk_toggle_button_get_mode: - * @toggle_button: a #GtkToggleButton - * - * Retrieves whether the button is displayed as a separate indicator - * and label. See gtk_toggle_button_set_mode(). - * - * Returns: %TRUE if the togglebutton is drawn as a separate indicator - * and label. - **/ -gboolean -gtk_toggle_button_get_mode (GtkToggleButton *toggle_button) -{ - g_return_val_if_fail (GTK_IS_TOGGLE_BUTTON (toggle_button), FALSE); - - return toggle_button->priv->draw_indicator; -} - /** * gtk_toggle_button_set_active: * @toggle_button: a #GtkToggleButton. diff --git a/gtk/gtktogglebutton.h b/gtk/gtktogglebutton.h index 692aed3e2b..cc582f88b2 100644 --- a/gtk/gtktogglebutton.h +++ b/gtk/gtktogglebutton.h @@ -78,11 +78,6 @@ GtkWidget* gtk_toggle_button_new_with_label (const gchar *label); GDK_AVAILABLE_IN_ALL GtkWidget* gtk_toggle_button_new_with_mnemonic (const gchar *label); GDK_AVAILABLE_IN_ALL -void gtk_toggle_button_set_mode (GtkToggleButton *toggle_button, - gboolean draw_indicator); -GDK_AVAILABLE_IN_ALL -gboolean gtk_toggle_button_get_mode (GtkToggleButton *toggle_button); -GDK_AVAILABLE_IN_ALL void gtk_toggle_button_set_active (GtkToggleButton *toggle_button, gboolean is_active); GDK_AVAILABLE_IN_ALL diff --git a/tests/testbaseline.c b/tests/testbaseline.c index 95b951602d..a6e1d7eafc 100644 --- a/tests/testbaseline.c +++ b/tests/testbaseline.c @@ -206,7 +206,6 @@ main (int argc, gtk_container_add (GTK_CONTAINER (hbox), button); button = gtk_toggle_button_new_with_label ("│Xyj,Ö"); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), TRUE); if (j == 0) gtk_widget_set_valign (button, GTK_ALIGN_BASELINE); gtk_container_add (GTK_CONTAINER (hbox), button); diff --git a/tests/testgtk.c b/tests/testgtk.c index f96812572e..91a2decf90 100644 --- a/tests/testgtk.c +++ b/tests/testgtk.c @@ -626,20 +626,20 @@ create_radio_buttons (GtkWidget *widget) gtk_box_pack_start (GTK_BOX (box1), box2, TRUE, TRUE); button = gtk_radio_button_new_with_label (NULL, "button4"); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE); + gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (button), FALSE); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE); button = gtk_radio_button_new_with_label ( gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)), "button5"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE); + gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (button), FALSE); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE); button = gtk_radio_button_new_with_label ( gtk_radio_button_get_group (GTK_RADIO_BUTTON (button)), "button6"); - gtk_toggle_button_set_mode (GTK_TOGGLE_BUTTON (button), FALSE); + gtk_check_button_set_draw_indicator (GTK_CHECK_BUTTON (button), FALSE); gtk_box_pack_start (GTK_BOX (box2), button, TRUE, TRUE); separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);