Skip to content

Commit 1b29d64

Browse files
authored
st-button: Add an :icon-name property (#13491)
Adds a convenient way to create an icon only button
1 parent b24d042 commit 1b29d64

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

src/st/st-button.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040

4141
#include "st-button.h"
4242

43+
#include "st-icon.h"
4344
#include "st-enum-types.h"
4445
#include "st-texture-cache.h"
4546
#include "st-private.h"
@@ -51,6 +52,7 @@ enum
5152
PROP_0,
5253

5354
PROP_LABEL,
55+
PROP_ICON_NAME,
5456
PROP_BUTTON_MASK,
5557
PROP_TOGGLE_MODE,
5658
PROP_CHECKED,
@@ -316,6 +318,9 @@ st_button_set_property (GObject *gobject,
316318
case PROP_LABEL:
317319
st_button_set_label (button, g_value_get_string (value));
318320
break;
321+
case PROP_ICON_NAME:
322+
st_button_set_icon_name (button, g_value_get_string (value));
323+
break;
319324
case PROP_BUTTON_MASK:
320325
st_button_set_button_mask (button, g_value_get_flags (value));
321326
break;
@@ -346,6 +351,9 @@ st_button_get_property (GObject *gobject,
346351
case PROP_LABEL:
347352
g_value_set_string (value, priv->text);
348353
break;
354+
case PROP_ICON_NAME:
355+
g_value_set_string (value, st_button_get_icon_name (ST_BUTTON (gobject)));
356+
break;
349357
case PROP_BUTTON_MASK:
350358
g_value_set_flags (value, priv->button_mask);
351359
break;
@@ -405,6 +413,12 @@ st_button_class_init (StButtonClass *klass)
405413
NULL, G_PARAM_READWRITE);
406414
g_object_class_install_property (gobject_class, PROP_LABEL, pspec);
407415

416+
pspec = g_param_spec_string ("icon-name",
417+
"Icon name",
418+
"Icon name of the button",
419+
NULL, G_PARAM_READWRITE);
420+
g_object_class_install_property (gobject_class, PROP_ICON_NAME, pspec);
421+
408422
pspec = g_param_spec_flags ("button-mask",
409423
"Button mask",
410424
"Which buttons trigger the 'clicked' signal",
@@ -553,6 +567,66 @@ st_button_set_label (StButton *button,
553567
g_object_notify (G_OBJECT (button), "label");
554568
}
555569

570+
/**
571+
* st_button_get_icon_name:
572+
* @button: a #StButton
573+
*
574+
* Get the icon name of the button. If the button isn't showing an icon,
575+
* the return value will be %NULL.
576+
*
577+
* Returns: (transfer none) (nullable): the icon name of the button
578+
*/
579+
const char *
580+
st_button_get_icon_name (StButton *button)
581+
{
582+
ClutterActor *icon;
583+
584+
g_return_val_if_fail (ST_IS_BUTTON (button), NULL);
585+
586+
icon = st_bin_get_child (ST_BIN (button));
587+
if (ST_IS_ICON (icon))
588+
return st_icon_get_icon_name (ST_ICON (icon));
589+
return NULL;
590+
}
591+
592+
/**
593+
* st_button_set_icon_name:
594+
* @button: a #Stbutton
595+
* @icon_name: an icon name
596+
*
597+
* Adds an `StIcon` with the given icon name as a child.
598+
*
599+
* If @button already contains a child actor, that child will
600+
* be removed and replaced with the icon.
601+
*/
602+
void
603+
st_button_set_icon_name (StButton *button,
604+
const char *icon_name)
605+
{
606+
ClutterActor *icon;
607+
608+
g_return_if_fail (ST_IS_BUTTON (button));
609+
g_return_if_fail (icon_name != NULL);
610+
611+
icon = st_bin_get_child (ST_BIN (button));
612+
613+
if (ST_IS_ICON (icon))
614+
{
615+
st_icon_set_icon_name (ST_ICON (icon), icon_name);
616+
}
617+
else
618+
{
619+
icon = g_object_new (ST_TYPE_ICON,
620+
"icon-name", icon_name,
621+
"x-align", CLUTTER_ACTOR_ALIGN_CENTER,
622+
"y-align", CLUTTER_ACTOR_ALIGN_CENTER,
623+
NULL);
624+
st_bin_set_child (ST_BIN (button), icon);
625+
}
626+
627+
g_object_notify (G_OBJECT (button), "icon-name");
628+
}
629+
556630
/**
557631
* st_button_get_button_mask:
558632
* @button: a #StButton

src/st/st-button.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ StWidget *st_button_new_with_label (const gchar *text);
7373
const gchar *st_button_get_label (StButton *button);
7474
void st_button_set_label (StButton *button,
7575
const gchar *text);
76+
const char *st_button_get_icon_name (StButton *button);
77+
void st_button_set_icon_name (StButton *button,
78+
const char *icon_name);
7679
void st_button_set_toggle_mode (StButton *button,
7780
gboolean toggle);
7881
gboolean st_button_get_toggle_mode (StButton *button);

0 commit comments

Comments
 (0)