Tidy up redrawing and notifications in KatzeThrobber

This commit is contained in:
Christian Dywan 2008-08-02 04:21:38 +02:00
parent 68fe5e05e1
commit f210b90ff9

View file

@ -1,5 +1,5 @@
/* /*
Copyright (C) 2007 Christian Dywan <christian@twotoasts.de> Copyright (C) 2007-2008 Christian Dywan <christian@twotoasts.de>
This library is free software; you can redistribute it and/or This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public modify it under the terms of the GNU Lesser General Public
@ -130,7 +130,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_ICON_SIZE, PROP_ICON_SIZE,
g_param_spec_int ( g_param_spec_int (
"icon-size", "icon-size",
"Icon size", _("Icon size"),
_("Symbolic size to use for the animation"), _("Symbolic size to use for the animation"),
0, G_MAXINT, GTK_ICON_SIZE_MENU, 0, G_MAXINT, GTK_ICON_SIZE_MENU,
flags)); flags));
@ -139,7 +139,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_ICON_NAME, PROP_ICON_NAME,
g_param_spec_string ( g_param_spec_string (
"icon-name", "icon-name",
"Icon Name", _("Icon Name"),
_("The name of an icon containing animation frames"), _("The name of an icon containing animation frames"),
"process-working", "process-working",
flags)); flags));
@ -148,7 +148,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_PIXBUF, PROP_PIXBUF,
g_param_spec_object ( g_param_spec_object (
"pixbuf", "pixbuf",
"Pixbuf", _("Pixbuf"),
_("A GdkPixbuf containing animation frames"), _("A GdkPixbuf containing animation frames"),
GDK_TYPE_PIXBUF, GDK_TYPE_PIXBUF,
G_PARAM_READWRITE)); G_PARAM_READWRITE));
@ -157,7 +157,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_ANIMATED, PROP_ANIMATED,
g_param_spec_boolean ( g_param_spec_boolean (
"animated", "animated",
"Animated", _("Animated"),
_("Whether the throbber should be animated"), _("Whether the throbber should be animated"),
FALSE, FALSE,
flags)); flags));
@ -166,7 +166,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_STATIC_ICON_NAME, PROP_STATIC_ICON_NAME,
g_param_spec_string ( g_param_spec_string (
"static-icon-name", "static-icon-name",
"Static Icon Name", _("Static Icon Name"),
_("The name of an icon to be used as the static image"), _("The name of an icon to be used as the static image"),
NULL, NULL,
flags)); flags));
@ -175,7 +175,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_PIXBUF, PROP_PIXBUF,
g_param_spec_object ( g_param_spec_object (
"static-pixbuf", "static-pixbuf",
"Static Pixbuf", _("Static Pixbuf"),
_("A GdkPixbuf to be used as the static image"), _("A GdkPixbuf to be used as the static image"),
GDK_TYPE_PIXBUF, GDK_TYPE_PIXBUF,
G_PARAM_READWRITE)); G_PARAM_READWRITE));
@ -184,7 +184,7 @@ katze_throbber_class_init (KatzeThrobberClass* class)
PROP_STATIC_STOCK_ID, PROP_STATIC_STOCK_ID,
g_param_spec_string ( g_param_spec_string (
"static-stock-id", "static-stock-id",
"Static Stock ID", _("Static Stock ID"),
_("The stock ID of an icon to be used as the static image"), _("The stock ID of an icon to be used as the static image"),
NULL, NULL,
flags)); flags));
@ -326,14 +326,20 @@ void
katze_throbber_set_icon_size (KatzeThrobber* throbber, katze_throbber_set_icon_size (KatzeThrobber* throbber,
GtkIconSize icon_size) GtkIconSize icon_size)
{ {
GtkSettings* gtk_settings;
g_return_if_fail (KATZE_IS_THROBBER (throbber)); g_return_if_fail (KATZE_IS_THROBBER (throbber));
g_return_if_fail (gtk_icon_size_lookup (icon_size, gtk_settings = gtk_widget_get_settings (GTK_WIDGET (throbber));
g_return_if_fail (gtk_icon_size_lookup_for_settings (gtk_settings,
icon_size,
&throbber->width, &throbber->width,
&throbber->height)); &throbber->height));
throbber->icon_size = icon_size; throbber->icon_size = icon_size;
g_object_notify (G_OBJECT (throbber), "icon-size"); g_object_notify (G_OBJECT (throbber), "icon-size");
gtk_widget_queue_draw (GTK_WIDGET (throbber));
} }
/** /**
@ -375,20 +381,24 @@ katze_throbber_set_pixbuf (KatzeThrobber* throbber,
GdkPixbuf* pixbuf) GdkPixbuf* pixbuf)
{ {
g_return_if_fail (KATZE_IS_THROBBER (throbber)); g_return_if_fail (KATZE_IS_THROBBER (throbber));
g_return_if_fail (pixbuf == NULL || GDK_IS_PIXBUF (pixbuf)); g_return_if_fail (!pixbuf || GDK_IS_PIXBUF (pixbuf));
katze_object_assign (throbber->pixbuf, pixbuf); katze_object_assign (throbber->pixbuf, pixbuf);
g_object_freeze_notify (G_OBJECT (throbber));
if (pixbuf) if (pixbuf)
{ {
g_object_ref (pixbuf); g_object_ref (pixbuf);
katze_assign (throbber->icon_name, NULL); katze_assign (throbber->icon_name, NULL);
g_object_notify (G_OBJECT (throbber), "icon-name");
} }
gtk_widget_queue_draw (GTK_WIDGET (throbber)); gtk_widget_queue_draw (GTK_WIDGET (throbber));
g_object_notify (G_OBJECT (throbber), "pixbuf"); g_object_notify (G_OBJECT (throbber), "pixbuf");
g_object_thaw_notify (G_OBJECT (throbber));
} }
/** /**
@ -438,14 +448,20 @@ katze_throbber_set_static_icon_name (KatzeThrobber* throbber,
katze_assign (throbber->static_icon_name, g_strdup (icon_name)); katze_assign (throbber->static_icon_name, g_strdup (icon_name));
g_object_freeze_notify (G_OBJECT (throbber));
if (icon_name) if (icon_name)
{ {
katze_assign (throbber->static_stock_id, NULL); katze_assign (throbber->static_stock_id, NULL);
icon_theme_changed (throbber); icon_theme_changed (throbber);
g_object_notify (G_OBJECT (throbber), "static-pixbuf");
g_object_notify (G_OBJECT (throbber), "static-stock-id");
} }
g_object_notify (G_OBJECT (throbber), "static-icon-name"); g_object_notify (G_OBJECT (throbber), "static-icon-name");
g_object_thaw_notify (G_OBJECT (throbber));
} }
/** /**
@ -468,15 +484,23 @@ katze_throbber_set_static_pixbuf (KatzeThrobber* throbber,
katze_object_assign (throbber->static_pixbuf, pixbuf); katze_object_assign (throbber->static_pixbuf, pixbuf);
g_object_freeze_notify (G_OBJECT (throbber));
if (pixbuf) if (pixbuf)
{ {
g_object_ref (pixbuf); g_object_ref (pixbuf);
katze_assign (throbber->static_icon_name, NULL); katze_assign (throbber->static_icon_name, NULL);
katze_assign (throbber->static_stock_id, NULL); katze_assign (throbber->static_stock_id, NULL);
gtk_widget_queue_draw (GTK_WIDGET (throbber));
g_object_notify (G_OBJECT (throbber), "static-icon-name");
g_object_notify (G_OBJECT (throbber), "static-stock-id");
} }
g_object_notify (G_OBJECT (throbber), "static-pixbuf"); g_object_notify (G_OBJECT (throbber), "static-pixbuf");
g_object_thaw_notify (G_OBJECT (throbber));
} }
/** /**
@ -486,7 +510,7 @@ katze_throbber_set_static_pixbuf (KatzeThrobber* throbber,
* *
* Sets the stock ID of an icon that should provide the static image. * Sets the stock ID of an icon that should provide the static image.
* *
* The statc icon name and pixbuf are automatically invalidated. * The static icon name and pixbuf are automatically invalidated.
**/ **/
void void
katze_throbber_set_static_stock_id (KatzeThrobber* throbber, katze_throbber_set_static_stock_id (KatzeThrobber* throbber,
@ -494,10 +518,15 @@ katze_throbber_set_static_stock_id (KatzeThrobber* throbber,
{ {
g_return_if_fail (KATZE_IS_THROBBER (throbber)); g_return_if_fail (KATZE_IS_THROBBER (throbber));
g_object_freeze_notify (G_OBJECT (throbber));
if (stock_id) if (stock_id)
{ {
GtkStockItem stock_item; GtkStockItem stock_item;
g_return_if_fail (gtk_stock_lookup (stock_id, &stock_item)); g_return_if_fail (gtk_stock_lookup (stock_id, &stock_item));
g_object_notify (G_OBJECT (throbber), "static-icon-name");
g_object_notify (G_OBJECT (throbber), "static-pixbuf");
} }
katze_assign (throbber->static_stock_id, g_strdup (stock_id)); katze_assign (throbber->static_stock_id, g_strdup (stock_id));
@ -506,6 +535,7 @@ katze_throbber_set_static_stock_id (KatzeThrobber* throbber,
icon_theme_changed (throbber); icon_theme_changed (throbber);
g_object_notify (G_OBJECT (throbber), "static-stock-id"); g_object_notify (G_OBJECT (throbber), "static-stock-id");
g_object_thaw_notify (G_OBJECT (throbber));
} }
/** /**
@ -637,13 +667,8 @@ katze_throbber_realize (GtkWidget* widget)
static void static void
katze_throbber_unrealize (GtkWidget* widget) katze_throbber_unrealize (GtkWidget* widget)
{ {
KatzeThrobber* throbber = KATZE_THROBBER (widget);
if (GTK_WIDGET_CLASS (katze_throbber_parent_class)->unrealize) if (GTK_WIDGET_CLASS (katze_throbber_parent_class)->unrealize)
GTK_WIDGET_CLASS (katze_throbber_parent_class)->unrealize (widget); GTK_WIDGET_CLASS (katze_throbber_parent_class)->unrealize (widget);
katze_object_assign (throbber->pixbuf, NULL);
katze_object_assign (throbber->static_pixbuf, NULL);
} }
static void static void
@ -651,11 +676,14 @@ pixbuf_assign_icon (GdkPixbuf** pixbuf,
const gchar* icon_name, const gchar* icon_name,
KatzeThrobber* throbber) KatzeThrobber* throbber)
{ {
GdkScreen* screen;
GtkIconTheme* icon_theme;
if (*pixbuf) if (*pixbuf)
g_object_unref (*pixbuf); g_object_unref (*pixbuf);
GdkScreen* screen = gtk_widget_get_screen (GTK_WIDGET (throbber)); screen = gtk_widget_get_screen (GTK_WIDGET (throbber));
GtkIconTheme* icon_theme = gtk_icon_theme_get_for_screen (screen); icon_theme = gtk_icon_theme_get_for_screen (screen);
*pixbuf = gtk_icon_theme_load_icon (icon_theme, *pixbuf = gtk_icon_theme_load_icon (icon_theme,
icon_name, icon_name,
MAX (throbber->width, throbber->height), MAX (throbber->width, throbber->height),
@ -667,22 +695,23 @@ static void
icon_theme_changed (KatzeThrobber* throbber) icon_theme_changed (KatzeThrobber* throbber)
{ {
if (throbber->icon_name) if (throbber->icon_name)
pixbuf_assign_icon (&throbber->pixbuf, throbber->icon_name, pixbuf_assign_icon (&throbber->pixbuf,
throbber); throbber->icon_name, throbber);
if (throbber->static_icon_name) if (throbber->static_icon_name)
pixbuf_assign_icon (&throbber->static_pixbuf, throbber->static_icon_name, pixbuf_assign_icon (&throbber->static_pixbuf,
throbber); throbber->static_icon_name, throbber);
else if (throbber->static_stock_id) else if (throbber->static_stock_id)
{ katze_object_assign (throbber->static_pixbuf,
if (throbber->static_pixbuf) gtk_widget_render_icon (GTK_WIDGET (throbber),
g_object_unref (throbber->static_pixbuf);
throbber->static_pixbuf = gtk_widget_render_icon (GTK_WIDGET (throbber),
throbber->static_stock_id, throbber->static_stock_id,
throbber->icon_size, throbber->icon_size,
NULL); NULL));
}
g_object_freeze_notify (G_OBJECT (throbber));
g_object_notify (G_OBJECT (throbber), "pixbuf");
g_object_notify (G_OBJECT (throbber), "static-pixbuf");
g_object_thaw_notify (G_OBJECT (throbber));
gtk_widget_queue_draw (GTK_WIDGET (throbber)); gtk_widget_queue_draw (GTK_WIDGET (throbber));
} }
@ -765,8 +794,8 @@ katze_throbber_expose_event (GtkWidget* widget,
if (throbber->animated && !throbber->pixbuf && !throbber->icon_name) if (throbber->animated && !throbber->pixbuf && !throbber->icon_name)
return TRUE; return TRUE;
if (!throbber->animated && if (!throbber->animated && (throbber->static_pixbuf
(throbber->static_pixbuf || throbber->static_icon_name || throbber->static_stock_id)) || throbber->static_icon_name || throbber->static_stock_id))
{ {
if (G_UNLIKELY (!throbber->static_pixbuf && throbber->static_icon_name)) if (G_UNLIKELY (!throbber->static_pixbuf && throbber->static_icon_name))
{ {
@ -774,9 +803,10 @@ katze_throbber_expose_event (GtkWidget* widget,
if (!throbber->static_pixbuf) if (!throbber->static_pixbuf)
{ {
g_warning ("Named icon '%s' couldn't be loaded", g_warning (_("Named icon '%s' couldn't be loaded"),
throbber->static_icon_name); throbber->static_icon_name);
katze_assign (throbber->static_icon_name, NULL); katze_assign (throbber->static_icon_name, NULL);
g_object_notify (G_OBJECT (throbber), "static-icon-name");
return TRUE; return TRUE;
} }
} }
@ -786,9 +816,10 @@ katze_throbber_expose_event (GtkWidget* widget,
if (!throbber->static_pixbuf) if (!throbber->static_pixbuf)
{ {
g_warning ("Stock icon '%s' couldn't be loaded", g_warning (_("Stock icon '%s' couldn't be loaded"),
throbber->static_stock_id); throbber->static_stock_id);
katze_assign (throbber->static_stock_id, NULL); katze_assign (throbber->static_stock_id, NULL);
g_object_notify (G_OBJECT (throbber), "static-stock-id");
return TRUE; return TRUE;
} }
} }
@ -808,8 +839,9 @@ katze_throbber_expose_event (GtkWidget* widget,
if (!throbber->pixbuf) if (!throbber->pixbuf)
{ {
g_warning ("Icon '%s' couldn't be loaded", throbber->icon_name); g_warning (_("Icon '%s' couldn't be loaded"), throbber->icon_name);
katze_assign (throbber->icon_name, NULL); katze_assign (throbber->icon_name, NULL);
g_object_notify (G_OBJECT (throbber), "icon-name");
return TRUE; return TRUE;
} }
} }
@ -839,9 +871,14 @@ katze_throbber_expose_event (GtkWidget* widget,
} }
else else
{ {
g_warning ("Animation frames are broken"); g_warning (_("Animation frames are broken"));
katze_assign (throbber->icon_name, NULL); katze_assign (throbber->icon_name, NULL);
katze_object_assign (throbber->pixbuf, NULL); katze_object_assign (throbber->pixbuf, NULL);
g_object_freeze_notify (G_OBJECT (throbber));
g_object_notify (G_OBJECT (throbber), "icon-name");
g_object_notify (G_OBJECT (throbber), "pixbuf");
g_object_thaw_notify (G_OBJECT (throbber));
} }
} }