[ekiga] Improved a little the GmPowermeter code



commit 0d6cb292a38c7d89e99d6d970694cced1d84689c
Author: Snark <jpuydt gnome org>
Date:   Sun Nov 7 21:04:53 2010 +0100

    Improved a little the GmPowermeter code
    
    Mostly GObject stuff, but also removed some dead "FIXME" code. Still lots
    of FIXME in there.

 lib/gui/gmpowermeter.c |  222 ++++++++++++++++++-----------------------------
 lib/gui/gmpowermeter.h |   55 ++++--------
 2 files changed, 104 insertions(+), 173 deletions(-)
---
diff --git a/lib/gui/gmpowermeter.c b/lib/gui/gmpowermeter.c
index 194e936..6994b6d 100644
--- a/lib/gui/gmpowermeter.c
+++ b/lib/gui/gmpowermeter.c
@@ -26,107 +26,100 @@
  */
 
 
-#include <gtk/gtk.h>
 #include <math.h>
 
 #include "gmpowermeter.h"
 
+struct _GmPowermeterPrivate {
+
+  GmPowermeterIconset *iconset;
+  /*!< used icons to draw the level, in a NULL terminated vector */
+
+  gfloat level;
+  /*!< the level to display, a float between 0.0 and 1.0 */
+};
+
+#define NUMBER_OF_PICTURES 5
+
 #include "pixmaps/gm_powermeter_default_00.xpm"
 #include "pixmaps/gm_powermeter_default_01.xpm"
 #include "pixmaps/gm_powermeter_default_02.xpm"
 #include "pixmaps/gm_powermeter_default_03.xpm"
 #include "pixmaps/gm_powermeter_default_04.xpm"
 
-static GObjectClass *parent_class = NULL;
+G_DEFINE_TYPE (GmPowermeter, gm_powermeter, GTK_TYPE_IMAGE);
 
-static void gm_powermeter_dispose (GObject *obj);
+/* some helpers */
 
-static void gm_powermeter_finalize (GObject *obj);
+static guint
+gm_powermeter_get_index_by_level (guint maxindex,
+				  gfloat level)
+{
+  /* FIXME? */
+  gfloat stepvalue = 0.0;
+  gfloat stepnumber = 0.0;
 
-static void gm_powermeter_class_init (gpointer g_class,
-				      gpointer class_data);
+  if (level <= 0.0)
+    return 0;
+  if (level >= 1.0)
+    return maxindex;
+
+  stepvalue = 1.0 / maxindex;
+  stepnumber = level / stepvalue;
 
-static void gm_powermeter_init (GmPowermeter*);
+  return (guint) rintf ((float) stepnumber);
+}
+
+static void
+gm_powermeter_redraw (GmPowermeter* powermeter)
+{
+  guint calculated_index = 0;
 
-static guint gm_powermeter_get_index_by_level (guint,
-					       gfloat);
+  g_return_if_fail (GM_IS_POWERMETER (powermeter));
 
-void gm_powermeter_redraw (GmPowermeter*);
+  calculated_index =
+    gm_powermeter_get_index_by_level (powermeter->priv->iconset->max_index,
+				      powermeter->priv->level);
 
+  gtk_image_set_from_pixbuf (GTK_IMAGE (powermeter),
+			     powermeter->priv->iconset->iconv [calculated_index]);
+}
 
-/* Implementation */
+/* GObject implementation */
 
 static void
 gm_powermeter_dispose (GObject *obj)
 {
-  if (((GmPowermeter*)obj)->iconset->iconv[0])
-    g_object_unref (((GmPowermeter*)obj)->iconset->iconv[0]);
-  ((GmPowermeter*)obj)->iconset->iconv[0] = NULL;
-  if (((GmPowermeter*)obj)->iconset->iconv[1])
-    g_object_unref (((GmPowermeter*)obj)->iconset->iconv[1]);
-  ((GmPowermeter*)obj)->iconset->iconv[1] = NULL;
-  if (((GmPowermeter*)obj)->iconset->iconv[2])
-    g_object_unref (((GmPowermeter*)obj)->iconset->iconv[2]);
-  ((GmPowermeter*)obj)->iconset->iconv[2] = NULL;
-  if (((GmPowermeter*)obj)->iconset->iconv[3])
-    g_object_unref (((GmPowermeter*)obj)->iconset->iconv[3]);
-  ((GmPowermeter*)obj)->iconset->iconv[3] = NULL;
-  if (((GmPowermeter*)obj)->iconset->iconv[4])
-    g_object_unref (((GmPowermeter*)obj)->iconset->iconv[4]);
-  ((GmPowermeter*)obj)->iconset->iconv[4] = NULL;
-
-  parent_class->dispose (obj);
+  int ii;
+
+  for (ii = 0; ii < NUMBER_OF_PICTURES; ii++) {
+
+  if (((GmPowermeter*)obj)->priv->iconset->iconv[ii])
+    g_object_unref (((GmPowermeter*)obj)->priv->iconset->iconv[ii]);
+  ((GmPowermeter*)obj)->priv->iconset->iconv[ii] = NULL;
+  }
+
+  G_OBJECT_CLASS (gm_powermeter_parent_class)->dispose (obj);
 }
 
 static void
 gm_powermeter_finalize (GObject *obj)
 {
-  g_free (((GmPowermeter*)obj)->iconset->iconv);
-  g_free (((GmPowermeter*)obj)->iconset);
+  g_free (((GmPowermeter*)obj)->priv->iconset->iconv);
+  g_free (((GmPowermeter*)obj)->priv->iconset);
 
-  parent_class->finalize (obj);
+  G_OBJECT_CLASS (gm_powermeter_parent_class)->finalize (obj);
 }
 
-GType
-gm_powermeter_get_type (void)
-{
-  static GType gm_powermeter_type = 0;
-
-  if (!gm_powermeter_type)
-    {
-      static const GTypeInfo gm_powermeter_info =
-	{
-	  sizeof (GmPowermeterClass),
-	  NULL,
-	  NULL,
-	  (GClassInitFunc) gm_powermeter_class_init,
-	  NULL,
-	  NULL,
-	  sizeof (GmPowermeter),
-	  0,
-	  (GInstanceInitFunc) gm_powermeter_init,
-	  NULL
-	};
-      gm_powermeter_type =
-	g_type_register_static (GTK_TYPE_IMAGE,
-				"GmPowermeter",
-				&gm_powermeter_info, (GTypeFlags) 0);
-    }
-  return gm_powermeter_type;
-}
-
-
 static void
-gm_powermeter_class_init (gpointer g_class,
-			  G_GNUC_UNUSED gpointer class_data)
+gm_powermeter_class_init (GmPowermeterClass* klass)
 {
-  GObjectClass *gobject_class = NULL;
+  GObjectClass* gobject_class = G_OBJECT_CLASS (klass);
 
-  parent_class = (GObjectClass *) g_type_class_peek_parent (g_class);
-
-  gobject_class = (GObjectClass*)g_class;
   gobject_class->dispose = gm_powermeter_dispose;
   gobject_class->finalize = gm_powermeter_finalize;
+
+  g_type_class_add_private (klass, sizeof (GmPowermeterPrivate));
 }
 
 
@@ -134,20 +127,20 @@ static void
 gm_powermeter_init (GmPowermeter* powermeter)
 {
   /* adjust that when you change the number of pictures for the default set! */
-  const int num_pics = 5;
   char ** tmp_xmp = { NULL };
 
-  g_return_if_fail (powermeter != NULL);
-  g_return_if_fail (GM_IS_POWERMETER (powermeter));
+  powermeter->priv = G_TYPE_INSTANCE_GET_PRIVATE (powermeter,
+						  GM_TYPE_POWERMETER, GmPowermeterPrivate);
 
-  powermeter->level = 0.0;
+  powermeter->priv->level = 0.0;
 
   /* set the default icon set FIXME isn't that ugly? */
-  powermeter->iconset = (GmPowermeterIconset*) g_malloc (sizeof (GmPowermeterIconset));
-  powermeter->iconset->max_index = num_pics - 1;
+  powermeter->priv->iconset = (GmPowermeterIconset*) g_malloc (sizeof (GmPowermeterIconset));
+  powermeter->priv->iconset->max_index = NUMBER_OF_PICTURES - 1;
 
   /* allocate the vector table (plus 1 for NULL) */
-  powermeter->iconset->iconv = (GdkPixbuf**) g_malloc (sizeof (GdkPixbuf*) * (num_pics + 1));
+  powermeter->priv->iconset->iconv = 
+    (GdkPixbuf**) g_malloc (sizeof (GdkPixbuf*) * (NUMBER_OF_PICTURES + 1));
 
   /* populate the vector table and append NULL */
   /* append/remove lines when you change the number of
@@ -159,16 +152,21 @@ gm_powermeter_init (GmPowermeter* powermeter)
    * without compiler warnings!
    */
   tmp_xmp = (char **) gm_powermeter_default_00_xpm;
-  powermeter->iconset->iconv[0] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+  powermeter->priv->iconset->iconv[0] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+
   tmp_xmp = (char **) gm_powermeter_default_01_xpm;
-  powermeter->iconset->iconv[1] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+  powermeter->priv->iconset->iconv[1] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+
   tmp_xmp = (char **) gm_powermeter_default_02_xpm;
-  powermeter->iconset->iconv[2] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+  powermeter->priv->iconset->iconv[2] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+
   tmp_xmp = (char **) gm_powermeter_default_03_xpm;
-  powermeter->iconset->iconv[3] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+  powermeter->priv->iconset->iconv[3] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+
   tmp_xmp = (char **) gm_powermeter_default_04_xpm;
-  powermeter->iconset->iconv[4] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
-  powermeter->iconset->iconv[num_pics] = NULL;
+  powermeter->priv->iconset->iconv[4] = gdk_pixbuf_new_from_xpm_data ((const char**) tmp_xmp);
+
+  powermeter->priv->iconset->iconv[NUMBER_OF_PICTURES] = NULL;
 
   gm_powermeter_redraw (powermeter);
 }
@@ -177,85 +175,35 @@ gm_powermeter_init (GmPowermeter* powermeter)
 GtkWidget*
 gm_powermeter_new (void)
 {
-  return GTK_WIDGET (g_object_new (gm_powermeter_get_type (), NULL));
-}
-
-
-GtkWidget*
-gm_powermeter_new_with_icon_set (G_GNUC_UNUSED GmPowermeterIconset* iconset)
-{
-  /* FIXME */
-  return gm_powermeter_new ();
+  return GTK_WIDGET (g_object_new (GM_TYPE_POWERMETER, NULL));
 }
 
-
-guint
-gm_powermeter_get_index_by_level (guint maxindex,
-				  gfloat level)
-{
-  /* FIXME? */
-  gfloat stepvalue = 0.0;
-  gfloat stepnumber = 0.0;
-
-  if (level <= 0.0)
-    return 0;
-  if (level >= 1.0)
-    return maxindex;
-
-  stepvalue = 1.0 / maxindex;
-  stepnumber = level / stepvalue;
-
-  return (guint) rintf ((float) stepnumber);
-}
-
-
-void
-gm_powermeter_redraw (GmPowermeter* powermeter)
-{
-  guint calculated_index = 0;
-
-  g_return_if_fail (powermeter != NULL);
-  g_return_if_fail (GM_IS_POWERMETER (powermeter));
-
-  calculated_index =
-    gm_powermeter_get_index_by_level (powermeter->iconset->max_index,
-				      powermeter->level);
-
-  gtk_image_set_from_pixbuf (GTK_IMAGE (powermeter),
-			     powermeter->iconset->iconv [calculated_index]);
-}
-
-
 void
 gm_powermeter_set_level (GmPowermeter* powermeter,
 			 gfloat level)
 {
-  g_return_if_fail (powermeter != NULL);
   g_return_if_fail (GM_IS_POWERMETER (powermeter));
 
   /* don't bother if we're requested to display the same
    * level we already do */
-  if (fabs (level - powermeter->level) <= 0.0001)
+  if (fabs (level - powermeter->priv->level) <= 0.0001)
     return;
 
-  powermeter->level = level;
+  powermeter->priv->level = level;
 
   /* limit the level to values between 0 and 1, inclusive */
-  if (powermeter->level < 0.0)
-    powermeter->level = 0.0;
-  if (powermeter->level > 1.0)
-    powermeter->level = 1.0;
+  if (powermeter->priv->level < 0.0)
+    powermeter->priv->level = 0.0;
+  if (powermeter->priv->level > 1.0)
+    powermeter->priv->level = 1.0;
 
   gm_powermeter_redraw (powermeter);
 }
 
-
 gfloat
 gm_powermeter_get_level (GmPowermeter* powermeter)
 {
-  g_return_val_if_fail (powermeter != NULL, 0.0);
   g_return_val_if_fail (GM_IS_POWERMETER (powermeter), 0.0);
 
-  return powermeter->level;
+  return powermeter->priv->level;
 }
-
diff --git a/lib/gui/gmpowermeter.h b/lib/gui/gmpowermeter.h
index d7d3c8e..ca6b4ba 100644
--- a/lib/gui/gmpowermeter.h
+++ b/lib/gui/gmpowermeter.h
@@ -48,18 +48,10 @@
 #ifndef __GM_POWERMETER_H__
 #define __GM_POWERMETER_H__
 
-#include <glib.h>
 #include <gtk/gtk.h>
 
 G_BEGIN_DECLS
 
-#define GM_POWERMETER_TYPE              (gm_powermeter_get_type())
-#define GM_POWERMETER(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GM_POWERMETER_TYPE, GmPowermeter))
-#define GM_POWERMETER_CLASS(klass)      ((G_TYPE_CHECK_CLASS_CAST ((klass), GM_POWERMETER_TYPE, GmPowermeterClass)))
-#define GM_IS_POWERMETER(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GM_POWERMETER_TYPE))
-#define GM_IS_POWERMETER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GM_POWERMETER_TYPE))
-
-
 typedef struct {
   guint max_index;
   GdkPixbuf** iconv;
@@ -72,6 +64,13 @@ typedef struct {
  */
 typedef struct _GmPowermeter GmPowermeter;
 
+
+/*!\typedef GmPowermeterPrivate
+ * \brief type for the _GmPowermeterPrivate private instance structure
+ * \see _GmPowermeterPrivate
+ */
+typedef struct _GmPowermeterPrivate GmPowermeterPrivate;
+
 /*!\typedef GmPowermeterClass
  * \brief type for the _GmPowermeterClass class structure
  * \see _GmPowermeterClass
@@ -88,11 +87,8 @@ struct _GmPowermeter
   GtkImage actualimage;
   /*!< parent widget, a GtkImage */
 
-  GmPowermeterIconset *iconset;
-  /*!< used icons to draw the level, in a NULL terminated vector */
-
-  gfloat level;
-  /*!< the level to display, a float between 0.0 and 1.0 */
+  GmPowermeterPrivate* priv;
+  /*< private data*/
 };
 
 
@@ -106,19 +102,6 @@ struct _GmPowermeterClass
   /*!< parent class, a GtkImageClass */
 };
 
-
-/*!\fn gm_powermeter_get_type (void)
- * \brief retrieve the GType of the GmPowermeter
- *
- * Usually used by the macros or by GLib/GTK itself
- * \see GM_POWERMETER_TYPE()
- * \see GM_POWERMETER()
- * \see GM_POWERMETER_CLASS()
- * \see GM_IS_POWERMETER()
- * \see GM_IS_POWERMETER_CLASS()
- */
-GType                           gm_powermeter_get_type (void);
-
 /*!\fn gm_powermeter_new (void)
  * \brief return a new instance of a GmPowermeter, with default icon set
  * \see GmPowermeter
@@ -126,16 +109,6 @@ GType                           gm_powermeter_get_type (void);
  */
 GtkWidget*                      gm_powermeter_new (void);
 
-/*!\fn gm_powermeter_new_with_icon_set (GdkPixbuf**)
- * \brief return a new instance of a GmPowermeter, with a given icon set
- *
- * The GmPowermeter does NOT make a private copy of the vector and its data.
- * It MUST NOT be freed while the stuff is running.
- * \see GmPowermeter
- * \see _GmPowermeter
- */
-GtkWidget*                      gm_powermeter_new_with_icon_set (GmPowermeterIconset*);
-
 /*!\fn gm_powermeter_set_level (gfloat)
  * \brief sets the level to display
  *
@@ -152,6 +125,16 @@ void                            gm_powermeter_set_level (GmPowermeter*,
  */
 gfloat                          gm_powermeter_get_level (GmPowermeter*);
 
+/* GObject boilerplate */
+
+GType                           gm_powermeter_get_type (void);
+
+#define GM_TYPE_POWERMETER              (gm_powermeter_get_type())
+#define GM_POWERMETER(obj)              (G_TYPE_CHECK_INSTANCE_CAST ((obj), GM_TYPE_POWERMETER, GmPowermeter))
+#define GM_POWERMETER_CLASS(klass)      ((G_TYPE_CHECK_CLASS_CAST ((klass), GM_TYPE_POWERMETER, GmPowermeterClass)))
+#define GM_IS_POWERMETER(obj)           (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GM_TYPE_POWERMETER))
+#define GM_IS_POWERMETER_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE ((klass), GM_TYPE_POWERMETER))
+
 G_END_DECLS
 
 #endif /* __GM_POWERMETER_H__ */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]