mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-01-11 23:16:14 +01:00
emblemedicon: make GEmblemedIcon subclassable
Hiding the object/class structs in the source file makes this class not subclassable. Move them to the public header, and add a property for the icon, so that subclasses can just use g_object_new (DERIVED_TYPE, "gicon", icon, NULL); to create an emblemed icon. https://bugzilla.gnome.org/show_bug.cgi?id=636892
This commit is contained in:
parent
bc4e1fc622
commit
01ba7bd8e8
@ -46,20 +46,19 @@
|
||||
* of the emblems. See also #GEmblem for more information.
|
||||
**/
|
||||
|
||||
static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
|
||||
|
||||
struct _GEmblemedIcon
|
||||
{
|
||||
GObject parent_instance;
|
||||
enum {
|
||||
PROP_GICON = 1,
|
||||
NUM_PROPERTIES
|
||||
};
|
||||
|
||||
struct _GEmblemedIconPrivate {
|
||||
GIcon *icon;
|
||||
GList *emblems;
|
||||
};
|
||||
|
||||
struct _GEmblemedIconClass
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
static GParamSpec *properties[NUM_PROPERTIES] = { NULL, };
|
||||
|
||||
static void g_emblemed_icon_icon_iface_init (GIconIface *iface);
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (GEmblemedIcon, g_emblemed_icon, G_TYPE_OBJECT,
|
||||
G_IMPLEMENT_INTERFACE (G_TYPE_ICON,
|
||||
@ -73,23 +72,78 @@ g_emblemed_icon_finalize (GObject *object)
|
||||
|
||||
emblemed = G_EMBLEMED_ICON (object);
|
||||
|
||||
g_object_unref (emblemed->icon);
|
||||
g_list_foreach (emblemed->emblems, (GFunc) g_object_unref, NULL);
|
||||
g_list_free (emblemed->emblems);
|
||||
g_object_unref (emblemed->priv->icon);
|
||||
g_list_foreach (emblemed->priv->emblems, (GFunc) g_object_unref, NULL);
|
||||
g_list_free (emblemed->priv->emblems);
|
||||
|
||||
(*G_OBJECT_CLASS (g_emblemed_icon_parent_class)->finalize) (object);
|
||||
}
|
||||
|
||||
static void
|
||||
g_emblemed_icon_set_property (GObject *object,
|
||||
guint property_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
GEmblemedIcon *self = G_EMBLEMED_ICON (object);
|
||||
|
||||
switch (property_id)
|
||||
{
|
||||
case PROP_GICON:
|
||||
self->priv->icon = g_value_dup_object (value);
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
g_emblemed_icon_get_property (GObject *object,
|
||||
guint property_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
GEmblemedIcon *self = G_EMBLEMED_ICON (object);
|
||||
|
||||
switch (property_id)
|
||||
{
|
||||
case PROP_GICON:
|
||||
g_value_set_object (value, self->priv->icon);
|
||||
break;
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
g_emblemed_icon_class_init (GEmblemedIconClass *klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
gobject_class->finalize = g_emblemed_icon_finalize;
|
||||
gobject_class->set_property = g_emblemed_icon_set_property;
|
||||
gobject_class->get_property = g_emblemed_icon_get_property;
|
||||
|
||||
properties[PROP_GICON] =
|
||||
g_param_spec_object ("gicon",
|
||||
P_("The base GIcon"),
|
||||
P_("The GIcon to attach emblems to"),
|
||||
G_TYPE_ICON,
|
||||
G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
|
||||
|
||||
g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
|
||||
|
||||
g_type_class_add_private (klass, sizeof (GEmblemedIconPrivate));
|
||||
}
|
||||
|
||||
static void
|
||||
g_emblemed_icon_init (GEmblemedIcon *emblemed)
|
||||
{
|
||||
emblemed->priv =
|
||||
G_TYPE_INSTANCE_GET_PRIVATE (emblemed, G_TYPE_EMBLEMED_ICON,
|
||||
GEmblemedIconPrivate);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,8 +166,9 @@ g_emblemed_icon_new (GIcon *icon,
|
||||
g_return_val_if_fail (G_IS_ICON (icon), NULL);
|
||||
g_return_val_if_fail (!G_IS_EMBLEM (icon), NULL);
|
||||
|
||||
emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON, NULL));
|
||||
emblemed->icon = g_object_ref (icon);
|
||||
emblemed = G_EMBLEMED_ICON (g_object_new (G_TYPE_EMBLEMED_ICON,
|
||||
"gicon", icon,
|
||||
NULL));
|
||||
|
||||
if (emblem != NULL)
|
||||
g_emblemed_icon_add_emblem (emblemed, emblem);
|
||||
@ -128,7 +183,7 @@ g_emblemed_icon_new (GIcon *icon,
|
||||
*
|
||||
* Gets the main icon for @emblemed.
|
||||
*
|
||||
* Returns: (transfer full): a #GIcon that is owned by @emblemed
|
||||
* Returns: (transfer none): a #GIcon that is owned by @emblemed
|
||||
*
|
||||
* Since: 2.18
|
||||
**/
|
||||
@ -137,7 +192,7 @@ g_emblemed_icon_get_icon (GEmblemedIcon *emblemed)
|
||||
{
|
||||
g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
|
||||
|
||||
return emblemed->icon;
|
||||
return emblemed->priv->icon;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -157,7 +212,7 @@ g_emblemed_icon_get_emblems (GEmblemedIcon *emblemed)
|
||||
{
|
||||
g_return_val_if_fail (G_IS_EMBLEMED_ICON (emblemed), NULL);
|
||||
|
||||
return emblemed->emblems;
|
||||
return emblemed->priv->emblems;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -173,11 +228,11 @@ g_emblemed_icon_clear_emblems (GEmblemedIcon *emblemed)
|
||||
{
|
||||
g_return_if_fail (G_IS_EMBLEMED_ICON (emblemed));
|
||||
|
||||
if (emblemed->emblems == NULL)
|
||||
if (emblemed->priv->emblems == NULL)
|
||||
return;
|
||||
|
||||
g_list_free_full (emblemed->emblems, g_object_unref);
|
||||
emblemed->emblems = NULL;
|
||||
g_list_free_full (emblemed->priv->emblems, g_object_unref);
|
||||
emblemed->priv->emblems = NULL;
|
||||
}
|
||||
|
||||
static gint
|
||||
@ -213,8 +268,8 @@ g_emblemed_icon_add_emblem (GEmblemedIcon *emblemed,
|
||||
g_return_if_fail (G_IS_EMBLEM (emblem));
|
||||
|
||||
g_object_ref (emblem);
|
||||
emblemed->emblems = g_list_insert_sorted (emblemed->emblems, emblem,
|
||||
(GCompareFunc) g_emblem_comp);
|
||||
emblemed->priv->emblems = g_list_insert_sorted (emblemed->priv->emblems, emblem,
|
||||
(GCompareFunc) g_emblem_comp);
|
||||
}
|
||||
|
||||
static guint
|
||||
@ -222,9 +277,9 @@ g_emblemed_icon_hash (GIcon *icon)
|
||||
{
|
||||
GEmblemedIcon *emblemed = G_EMBLEMED_ICON (icon);
|
||||
GList *list;
|
||||
guint hash = g_icon_hash (emblemed->icon);
|
||||
guint hash = g_icon_hash (emblemed->priv->icon);
|
||||
|
||||
for (list = emblemed->emblems; list != NULL; list = list->next)
|
||||
for (list = emblemed->priv->emblems; list != NULL; list = list->next)
|
||||
hash ^= g_icon_hash (G_ICON (list->data));
|
||||
|
||||
return hash;
|
||||
@ -238,11 +293,11 @@ g_emblemed_icon_equal (GIcon *icon1,
|
||||
GEmblemedIcon *emblemed2 = G_EMBLEMED_ICON (icon2);
|
||||
GList *list1, *list2;
|
||||
|
||||
if (!g_icon_equal (emblemed1->icon, emblemed2->icon))
|
||||
if (!g_icon_equal (emblemed1->priv->icon, emblemed2->priv->icon))
|
||||
return FALSE;
|
||||
|
||||
list1 = emblemed1->emblems;
|
||||
list2 = emblemed2->emblems;
|
||||
list1 = emblemed1->priv->emblems;
|
||||
list2 = emblemed2->priv->emblems;
|
||||
|
||||
while (list1 && list2)
|
||||
{
|
||||
@ -274,13 +329,13 @@ g_emblemed_icon_to_tokens (GIcon *icon,
|
||||
|
||||
*out_version = 0;
|
||||
|
||||
s = g_icon_to_string (emblemed_icon->icon);
|
||||
s = g_icon_to_string (emblemed_icon->priv->icon);
|
||||
if (s == NULL)
|
||||
return FALSE;
|
||||
|
||||
g_ptr_array_add (tokens, s);
|
||||
|
||||
for (l = emblemed_icon->emblems; l != NULL; l = l->next)
|
||||
for (l = emblemed_icon->priv->emblems; l != NULL; l = l->next)
|
||||
{
|
||||
GIcon *emblem_icon = G_ICON (l->data);
|
||||
|
||||
@ -326,8 +381,8 @@ g_emblemed_icon_from_tokens (gchar **tokens,
|
||||
}
|
||||
|
||||
emblemed_icon = g_object_new (G_TYPE_EMBLEMED_ICON, NULL);
|
||||
emblemed_icon->icon = g_icon_new_for_string (tokens[0], error);
|
||||
if (emblemed_icon->icon == NULL)
|
||||
emblemed_icon->priv->icon = g_icon_new_for_string (tokens[0], error);
|
||||
if (emblemed_icon->priv->icon == NULL)
|
||||
goto fail;
|
||||
|
||||
for (n = 1; n < num_tokens; n++)
|
||||
@ -348,7 +403,7 @@ g_emblemed_icon_from_tokens (gchar **tokens,
|
||||
goto fail;
|
||||
}
|
||||
|
||||
emblemed_icon->emblems = g_list_append (emblemed_icon->emblems, emblem);
|
||||
emblemed_icon->priv->emblems = g_list_append (emblemed_icon->priv->emblems, emblem);
|
||||
}
|
||||
|
||||
return G_ICON (emblemed_icon);
|
||||
|
@ -47,6 +47,20 @@ G_BEGIN_DECLS
|
||||
**/
|
||||
typedef struct _GEmblemedIcon GEmblemedIcon;
|
||||
typedef struct _GEmblemedIconClass GEmblemedIconClass;
|
||||
typedef struct _GEmblemedIconPrivate GEmblemedIconPrivate;
|
||||
|
||||
struct _GEmblemedIcon
|
||||
{
|
||||
GObject parent_instance;
|
||||
|
||||
/*< private >*/
|
||||
GEmblemedIconPrivate *priv;
|
||||
};
|
||||
|
||||
struct _GEmblemedIconClass
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
GType g_emblemed_icon_get_type (void) G_GNUC_CONST;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user