libgimpwidgets: GimpChainButton now a final type too.

This commit is contained in:
Jehan 2024-10-16 21:03:22 +02:00
parent 03b88d8ac0
commit 925faf83ac
2 changed files with 49 additions and 81 deletions

View file

@ -68,8 +68,10 @@ enum
}; };
typedef struct _GimpChainButtonPrivate struct _GimpChainButton
{ {
GtkGrid parent_instance;
GimpChainPosition position; GimpChainPosition position;
gboolean active; gboolean active;
@ -77,7 +79,7 @@ typedef struct _GimpChainButtonPrivate
GtkWidget *line1; GtkWidget *line1;
GtkWidget *line2; GtkWidget *line2;
GtkWidget *image; GtkWidget *image;
} GimpChainButtonPrivate; };
static void gimp_chain_button_constructed (GObject *object); static void gimp_chain_button_constructed (GObject *object);
@ -102,7 +104,7 @@ static GtkWidget * gimp_chain_line_new (GimpChainPosition position,
gint which); gint which);
G_DEFINE_TYPE_WITH_PRIVATE (GimpChainButton, gimp_chain_button, GTK_TYPE_GRID) G_DEFINE_TYPE (GimpChainButton, gimp_chain_button, GTK_TYPE_GRID)
#define parent_class gimp_chain_button_parent_class #define parent_class gimp_chain_button_parent_class
@ -133,12 +135,10 @@ gimp_chain_button_class_init (GimpChainButtonClass *klass)
g_signal_new ("toggled", g_signal_new ("toggled",
G_TYPE_FROM_CLASS (klass), G_TYPE_FROM_CLASS (klass),
G_SIGNAL_RUN_FIRST, G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GimpChainButtonClass, toggled), 0,
NULL, NULL, NULL, NULL, NULL, NULL,
G_TYPE_NONE, 0); G_TYPE_NONE, 0);
klass->toggled = NULL;
/** /**
* GimpChainButton:position: * GimpChainButton:position:
* *
@ -190,19 +190,16 @@ gimp_chain_button_class_init (GimpChainButtonClass *klass)
static void static void
gimp_chain_button_init (GimpChainButton *button) gimp_chain_button_init (GimpChainButton *button)
{ {
GimpChainButtonPrivate *private; button->position = GIMP_CHAIN_TOP;
private = gimp_chain_button_get_instance_private (button); button->active = FALSE;
button->image = gtk_image_new ();
button->button = gtk_button_new ();
private->position = GIMP_CHAIN_TOP; gtk_button_set_relief (GTK_BUTTON (button->button), GTK_RELIEF_NONE);
private->active = FALSE; gtk_container_add (GTK_CONTAINER (button->button), button->image);
private->image = gtk_image_new (); gtk_widget_show (button->image);
private->button = gtk_button_new ();
gtk_button_set_relief (GTK_BUTTON (private->button), GTK_RELIEF_NONE); g_signal_connect (button->button, "clicked",
gtk_container_add (GTK_CONTAINER (private->button), private->image);
gtk_widget_show (private->image);
g_signal_connect (private->button, "clicked",
G_CALLBACK (gimp_chain_button_clicked_callback), G_CALLBACK (gimp_chain_button_clicked_callback),
button); button);
} }
@ -210,38 +207,37 @@ gimp_chain_button_init (GimpChainButton *button)
static void static void
gimp_chain_button_constructed (GObject *object) gimp_chain_button_constructed (GObject *object)
{ {
GimpChainButton *button = GIMP_CHAIN_BUTTON (object); GimpChainButton *button = GIMP_CHAIN_BUTTON (object);
GimpChainButtonPrivate *private = gimp_chain_button_get_instance_private (button);
G_OBJECT_CLASS (parent_class)->constructed (object); G_OBJECT_CLASS (parent_class)->constructed (object);
private->line1 = gimp_chain_line_new (private->position, 1); button->line1 = gimp_chain_line_new (button->position, 1);
private->line2 = gimp_chain_line_new (private->position, -1); button->line2 = gimp_chain_line_new (button->position, -1);
gimp_chain_button_update_image (button); gimp_chain_button_update_image (button);
if (private->position & GIMP_CHAIN_LEFT) /* are we a vertical chainbutton? */ if (button->position & GIMP_CHAIN_LEFT) /* are we a vertical chainbutton? */
{ {
gtk_widget_set_vexpand (private->line1, TRUE); gtk_widget_set_vexpand (button->line1, TRUE);
gtk_widget_set_vexpand (private->line2, TRUE); gtk_widget_set_vexpand (button->line2, TRUE);
gtk_grid_attach (GTK_GRID (button), private->line1, 0, 0, 1, 1); gtk_grid_attach (GTK_GRID (button), button->line1, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (button), private->button, 0, 1, 1, 1); gtk_grid_attach (GTK_GRID (button), button->button, 0, 1, 1, 1);
gtk_grid_attach (GTK_GRID (button), private->line2, 0, 2, 1, 1); gtk_grid_attach (GTK_GRID (button), button->line2, 0, 2, 1, 1);
} }
else else
{ {
gtk_widget_set_hexpand (private->line1, TRUE); gtk_widget_set_hexpand (button->line1, TRUE);
gtk_widget_set_hexpand (private->line2, TRUE); gtk_widget_set_hexpand (button->line2, TRUE);
gtk_grid_attach (GTK_GRID (button), private->line1, 0, 0, 1, 1); gtk_grid_attach (GTK_GRID (button), button->line1, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (button), private->button, 1, 0, 1, 1); gtk_grid_attach (GTK_GRID (button), button->button, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID (button), private->line2, 2, 0, 1, 1); gtk_grid_attach (GTK_GRID (button), button->line2, 2, 0, 1, 1);
} }
gtk_widget_show (private->button); gtk_widget_show (button->button);
gtk_widget_show (private->line1); gtk_widget_show (button->line1);
gtk_widget_show (private->line2); gtk_widget_show (button->line2);
} }
static void static void
@ -250,17 +246,16 @@ gimp_chain_button_set_property (GObject *object,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
GimpChainButton *button = GIMP_CHAIN_BUTTON (object); GimpChainButton *button = GIMP_CHAIN_BUTTON (object);
GimpChainButtonPrivate *private = gimp_chain_button_get_instance_private (button);
switch (property_id) switch (property_id)
{ {
case PROP_POSITION: case PROP_POSITION:
private->position = g_value_get_enum (value); button->position = g_value_get_enum (value);
break; break;
case PROP_ICON_SIZE: case PROP_ICON_SIZE:
g_object_set_property (G_OBJECT (private->image), "icon-size", value); g_object_set_property (G_OBJECT (button->image), "icon-size", value);
break; break;
case PROP_ACTIVE: case PROP_ACTIVE:
@ -279,17 +274,16 @@ gimp_chain_button_get_property (GObject *object,
GValue *value, GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
GimpChainButton *button = GIMP_CHAIN_BUTTON (object); GimpChainButton *button = GIMP_CHAIN_BUTTON (object);
GimpChainButtonPrivate *private = gimp_chain_button_get_instance_private (button);
switch (property_id) switch (property_id)
{ {
case PROP_POSITION: case PROP_POSITION:
g_value_set_enum (value, private->position); g_value_set_enum (value, button->position);
break; break;
case PROP_ICON_SIZE: case PROP_ICON_SIZE:
g_object_get_property (G_OBJECT (private->image), "icon-size", value); g_object_get_property (G_OBJECT (button->image), "icon-size", value);
break; break;
case PROP_ACTIVE: case PROP_ACTIVE:
@ -394,15 +388,13 @@ void
gimp_chain_button_set_active (GimpChainButton *button, gimp_chain_button_set_active (GimpChainButton *button,
gboolean active) gboolean active)
{ {
GimpChainButtonPrivate *private;
g_return_if_fail (GIMP_IS_CHAIN_BUTTON (button)); g_return_if_fail (GIMP_IS_CHAIN_BUTTON (button));
private = gimp_chain_button_get_instance_private (button); button = gimp_chain_button_get_instance_private (button);
if (private->active != active) if (button->active != active)
{ {
private->active = active ? TRUE : FALSE; button->active = active ? TRUE : FALSE;
gimp_chain_button_update_image (button); gimp_chain_button_update_image (button);
@ -423,13 +415,11 @@ gimp_chain_button_set_active (GimpChainButton *button,
gboolean gboolean
gimp_chain_button_get_active (GimpChainButton *button) gimp_chain_button_get_active (GimpChainButton *button)
{ {
GimpChainButtonPrivate *private;
g_return_val_if_fail (GIMP_IS_CHAIN_BUTTON (button), FALSE); g_return_val_if_fail (GIMP_IS_CHAIN_BUTTON (button), FALSE);
private = gimp_chain_button_get_instance_private (button); button = gimp_chain_button_get_instance_private (button);
return private->active; return button->active;
} }
/** /**
@ -443,13 +433,11 @@ gimp_chain_button_get_active (GimpChainButton *button)
GtkWidget * GtkWidget *
gimp_chain_button_get_button (GimpChainButton *button) gimp_chain_button_get_button (GimpChainButton *button)
{ {
GimpChainButtonPrivate *private;
g_return_val_if_fail (GIMP_IS_CHAIN_BUTTON (button), FALSE); g_return_val_if_fail (GIMP_IS_CHAIN_BUTTON (button), FALSE);
private = gimp_chain_button_get_instance_private (button); button = gimp_chain_button_get_instance_private (button);
return private->button; return button->button;
} }
@ -459,20 +447,17 @@ static void
gimp_chain_button_clicked_callback (GtkWidget *widget, gimp_chain_button_clicked_callback (GtkWidget *widget,
GimpChainButton *button) GimpChainButton *button)
{ {
GimpChainButtonPrivate *private = gimp_chain_button_get_instance_private (button); gimp_chain_button_set_active (button, ! button->active);
gimp_chain_button_set_active (button, ! private->active);
} }
static void static void
gimp_chain_button_update_image (GimpChainButton *button) gimp_chain_button_update_image (GimpChainButton *button)
{ {
GimpChainButtonPrivate *private = gimp_chain_button_get_instance_private (button); guint i;
guint i;
i = ((private->position & GIMP_CHAIN_LEFT) << 1) + (private->active ? 0 : 1); i = ((button->position & GIMP_CHAIN_LEFT) << 1) + (button->active ? 0 : 1);
gtk_image_set_from_icon_name (GTK_IMAGE (private->image), gtk_image_set_from_icon_name (GTK_IMAGE (button->image),
gimp_chain_icon_names[i], gimp_chain_icon_names[i],
gimp_chain_button_get_icon_size (button)); gimp_chain_button_get_icon_size (button));
} }

View file

@ -38,24 +38,7 @@ G_BEGIN_DECLS
#define GIMP_TYPE_CHAIN_BUTTON (gimp_chain_button_get_type ()) #define GIMP_TYPE_CHAIN_BUTTON (gimp_chain_button_get_type ())
G_DECLARE_DERIVABLE_TYPE (GimpChainButton, gimp_chain_button, GIMP, CHAIN_BUTTON, GtkGrid) G_DECLARE_FINAL_TYPE (GimpChainButton, gimp_chain_button, GIMP, CHAIN_BUTTON, GtkGrid)
struct _GimpChainButtonClass
{
GtkGridClass parent_class;
void (* toggled) (GimpChainButton *button);
/* Padding for future expansion */
void (* _gimp_reserved1) (void);
void (* _gimp_reserved2) (void);
void (* _gimp_reserved3) (void);
void (* _gimp_reserved4) (void);
void (* _gimp_reserved5) (void);
void (* _gimp_reserved6) (void);
void (* _gimp_reserved7) (void);
void (* _gimp_reserved8) (void);
};
GtkWidget * gimp_chain_button_new (GimpChainPosition position); GtkWidget * gimp_chain_button_new (GimpChainPosition position);