[gtk+/wip/strength-bar: 3/3] strength-bar: introduce GtkStrengthBar



commit a878dcd7859a6180b0088376e2bfb3aab2a83618
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Tue May 29 17:00:33 2012 -0400

    strength-bar: introduce GtkStrengthBar
    
    This is similar to CcStrengthBar from gnome-control-center, but it's
    implemented as a subclass of GtkProgressbar, and is drawn completely
    using CSS from the theme.

 docs/reference/gtk/gtk3-sections.txt |   22 +++
 gtk/Makefile.am                      |    2 +
 gtk/gtk.h                            |    1 +
 gtk/gtk.symbols                      |    6 +
 gtk/gtkstrengthbar.c                 |  262 ++++++++++++++++++++++++++++++++++
 gtk/gtkstrengthbar.h                 |   71 +++++++++
 gtk/gtkstylecontext.h                |   10 ++
 7 files changed, 374 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 934af0f..3aab77b 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -3206,6 +3206,27 @@ gtk_status_icon_get_type
 </SECTION>
 
 <SECTION>
+<FILE>gtkstrengthbar</FILE>
+<TITLE>GtkStrenghtBar</TITLE>
+GtkStrenghtBar
+gtk_strength_bar_new
+gtk_strength_bar_get_warning_fraction
+gtk_strength_bar_get_success_fraction
+gtk_strength_bar_set_warning_fraction
+gtk_strength_bar_set_success_fraction
+<SUBSECTION Standard>
+GTK_STRENGTH_BAR
+GTK_IS_STRENGTH_BAR
+GTK_TYPE_STRENGTH_BAR
+GTK_STRENGTH_BAR_CLASS
+GTK_IS_STRENGTH_BAR_CLASS
+GTK_STRENGTH_BAR_GET_CLASS
+<SUBSECTION Private>
+GtkStrengthBarPrivate
+gtk_strength_bar_get_type
+</SECTION>
+
+<SECTION>
 <FILE>gtktable</FILE>
 <TITLE>GtkTable</TITLE>
 GtkTable
@@ -5679,6 +5700,7 @@ GTK_STYLE_CLASS_RIGHT
 GTK_STYLE_CLASS_LINKED
 GTK_STYLE_CLASS_ARROW
 GTK_STYLE_CLASS_OSD
+GTK_STYLE_CLASS_STRENGTH_BAR
 GTK_STYLE_CLASS_SUCCESS
 GTK_STYLE_REGION_COLUMN
 GTK_STYLE_REGION_COLUMN_HEADER
diff --git a/gtk/Makefile.am b/gtk/Makefile.am
index dfa90ac..ec4c6ed 100644
--- a/gtk/Makefile.am
+++ b/gtk/Makefile.am
@@ -323,6 +323,7 @@ gtk_public_h_sources = 		\
 	gtkstatusbar.h		\
 	gtkstatusicon.h		\
 	gtkstock.h		\
+	gtkstrengthbar.h	\
 	gtkstylecontext.h	\
 	gtkstyleproperties.h	\
 	gtkstyleprovider.h	\
@@ -761,6 +762,7 @@ gtk_base_c_sources = 		\
 	gtkprivate.c		\
 	gtkprivatetypebuiltins.c \
 	gtkprogressbar.c	\
+	gtkstrengthbar.c	\
 	gtkradioaction.c	\
 	gtkradiobutton.c	\
 	gtkradiomenuitem.c	\
diff --git a/gtk/gtk.h b/gtk/gtk.h
index de0181b..bcb3765 100644
--- a/gtk/gtk.h
+++ b/gtk/gtk.h
@@ -178,6 +178,7 @@
 #include <gtk/gtkstatusbar.h>
 #include <gtk/gtkstatusicon.h>
 #include <gtk/gtkstock.h>
+#include <gtk/gtkstrengthbar.h>
 #include <gtk/gtkstylecontext.h>
 #include <gtk/gtkstyleproperties.h>
 #include <gtk/gtkstyleprovider.h>
diff --git a/gtk/gtk.symbols b/gtk/gtk.symbols
index 6f2be20..c7c3e00 100644
--- a/gtk/gtk.symbols
+++ b/gtk/gtk.symbols
@@ -2553,6 +2553,12 @@ gtk_switch_get_active
 gtk_switch_get_type
 gtk_switch_new
 gtk_switch_set_active
+gtk_strength_bar_get_type
+gtk_strength_bar_new
+gtk_strength_bar_get_warning_fraction
+gtk_strength_bar_get_success_fraction
+gtk_strength_bar_set_warning_fraction
+gtk_strength_bar_set_success_fraction
 gtk_style_apply_default_background
 gtk_style_attach
 gtk_style_context_add_class
diff --git a/gtk/gtkstrengthbar.c b/gtk/gtkstrengthbar.c
new file mode 100644
index 0000000..2363fe5
--- /dev/null
+++ b/gtk/gtkstrengthbar.c
@@ -0,0 +1,262 @@
+/* GTK - The GIMP Toolkit
+ * Copyright  2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Cosimo Cecchi <cosimoc gnome org>
+ *
+ */
+
+/**
+ * SECTION:gtkstrengthbar
+ * @Title: GtkStrengthBar
+ * @Short_description: A progress bar used as a strength indicator
+ *
+ * The #GtkStrengthBar is a #GtkProgressBar subclass that can be used
+ * as a strength indicator. Typical use cases are displaying the strength
+ * of a password, or showing the charge level of a battery.
+ *
+ * Use #gtk_progress_bar_set_fraction as you would normally do on a
+ * #GtkProgressBar to set the current value.
+ * #GtkStrengthBar will draw the fill with a different style according to
+ * the current fraction: the #GTK_STYLE_CLASS_WARNING will be set on the
+ * widget when the fraction is less than #GtkStrengthBar:warning-fraction, 
+ * and #GTK_STYLE_CLASS_SUCCESS will be set when the fraction is more 
+ * than #GtkStrengthBar:success-fraction.
+ */
+#include "config.h"
+
+#include "gtkintl.h"
+#include "gtkstrengthbar.h"
+#include "gtkstylecontext.h"
+#include "gtkwidget.h"
+
+G_DEFINE_TYPE (GtkStrengthBar, gtk_strength_bar, GTK_TYPE_PROGRESS_BAR);
+
+enum {
+  PROP_WARNING_FRACTION = 1,
+  PROP_SUCCESS_FRACTION,
+  LAST_PROPERTY
+};
+
+static GParamSpec *properties[LAST_PROPERTY] = { NULL, };
+
+struct _GtkStrengthBarPrivate {
+  gdouble warning_fraction;
+  gdouble success_fraction;
+};
+
+static void
+gtk_strength_bar_update_style_classes (GtkStrengthBar *self)
+{
+  GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET (self));
+  gdouble fraction = gtk_progress_bar_get_fraction (GTK_PROGRESS_BAR (self));
+
+  if (fraction < self->priv->warning_fraction)
+    {
+      gtk_style_context_remove_class (context, GTK_STYLE_CLASS_SUCCESS);
+      gtk_style_context_add_class (context, GTK_STYLE_CLASS_WARNING);
+    }
+  else if (fraction > self->priv->success_fraction)
+    {
+      gtk_style_context_remove_class (context, GTK_STYLE_CLASS_WARNING);
+      gtk_style_context_add_class (context, GTK_STYLE_CLASS_SUCCESS);
+    }
+}
+
+static void
+gtk_strength_bar_notify (GObject *obj,
+                         GParamSpec *pspec)
+{
+  if ((g_strcmp0 (pspec->name, "fraction") == 0) ||
+      (g_strcmp0 (pspec->name, "success-fraction") == 0) ||
+      (g_strcmp0 (pspec->name, "warning-fraction") == 0))
+    gtk_strength_bar_update_style_classes (GTK_STRENGTH_BAR (obj));
+}
+
+static void
+gtk_strength_bar_get_property (GObject    *obj,
+                               guint       property_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  GtkStrengthBar *self = GTK_STRENGTH_BAR (obj);
+
+  switch (property_id)
+    {
+    case PROP_WARNING_FRACTION:
+      g_value_set_double (value, gtk_strength_bar_get_warning_fraction (self));
+      break;
+    case PROP_SUCCESS_FRACTION:
+      g_value_set_double (value, gtk_strength_bar_get_success_fraction (self));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_strength_bar_set_property (GObject      *obj,
+                               guint         property_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  GtkStrengthBar *self = GTK_STRENGTH_BAR (obj);
+
+  switch (property_id)
+    {
+    case PROP_WARNING_FRACTION:
+      gtk_strength_bar_set_warning_fraction (self, g_value_get_double (value));
+      break;
+    case PROP_SUCCESS_FRACTION:
+      gtk_strength_bar_set_success_fraction (self, g_value_get_double (value));
+      break;
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gtk_strength_bar_class_init (GtkStrengthBarClass *klass)
+{
+  GObjectClass *oclass = G_OBJECT_CLASS (klass);
+
+  oclass->notify = gtk_strength_bar_notify;
+  oclass->get_property = gtk_strength_bar_get_property;
+  oclass->set_property = gtk_strength_bar_set_property;
+
+  /**
+   * GtkStrengthBar:warning-fraction:
+   *
+   * The #GtkStrengthBar:warning-fraction property determines
+   * the fraction below which the bar displays in a warning state.
+   */
+  properties[PROP_WARNING_FRACTION] =
+    g_param_spec_double ("warning-fraction",
+                         P_("Fraction level for the warning state"),
+                         P_("Fraction level below which the bar is treated in a warning state"),
+                         0.0, 1.0, 0.25,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+  /**
+   * GtkStrengthBar:success-fraction:
+   *
+   * The #GtkStrengthBar:success-fraction property determines
+   * the fraction above which the bar displays in a successful state.
+   */
+  properties[PROP_SUCCESS_FRACTION] =
+    g_param_spec_double ("success-fraction",
+                         P_("Fraction level for the successful state"),
+                         P_("Fraction level above which the bar is treated as successful"),
+                         0.0, 1.0, 0.75,
+                         G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
+
+  g_type_class_add_private (klass, sizeof (GtkStrengthBarPrivate));
+  g_object_class_install_properties (oclass, LAST_PROPERTY, properties);
+}
+
+static void
+gtk_strength_bar_init (GtkStrengthBar *self)
+{
+  GtkStyleContext *context;
+
+  self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTK_TYPE_STRENGTH_BAR, GtkStrengthBarPrivate);
+
+  context = gtk_widget_get_style_context (GTK_WIDGET (self));
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_STRENGTH_BAR);
+
+  self->priv->warning_fraction = 0.25;
+  self->priv->success_fraction = 0.75;
+}
+
+GtkWidget *
+gtk_strength_bar_new (void)
+{
+  return g_object_new (GTK_TYPE_STRENGTH_BAR, NULL);
+}
+
+/**
+ * gtk_strength_bar_get_warning_fraction:
+ * @self: a #GtkStrengthBar
+ * 
+ * Returns the value of the #GtkStrengthBar:warning-fraction property.
+ *
+ * Returns: a fraction from 0.0 to 1.0.
+ */
+gdouble
+gtk_strength_bar_get_warning_fraction (GtkStrengthBar *self)
+{
+  g_return_val_if_fail (GTK_IS_STRENGTH_BAR (self), 0.0);
+
+  return self->priv->warning_fraction;
+}
+
+/**
+ * gtk_strength_bar_get_success_fraction:
+ * @self: a #GtkStrengthBar
+ * 
+ * Returns the value of the #GtkStrengthBar:success-fraction property.
+ *
+ * Returns: a fraction from 0.0 to 1.0.
+ */
+gdouble
+gtk_strength_bar_get_success_fraction (GtkStrengthBar *self)
+{
+  g_return_val_if_fail (GTK_IS_STRENGTH_BAR (self), 0.0);
+
+  return self->priv->success_fraction;
+}
+
+/**
+ * gtk_strength_bar_set_warning_fraction:
+ * @self: a #GtkStrengthBar
+ * @fraction: a fraction from 0.0 to 1.0
+ * 
+ * Sets the value of the #GtkStrengthBar:warning-fraction property.
+ */
+void
+gtk_strength_bar_set_warning_fraction (GtkStrengthBar *self,
+                                       gdouble         fraction)
+{
+  g_return_if_fail (GTK_IS_STRENGTH_BAR (self));
+  g_return_if_fail (fraction >= 0.0 && fraction <= 1.0);
+
+  if (fraction != self->priv->warning_fraction)
+    {
+      self->priv->warning_fraction = fraction;
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_WARNING_FRACTION]);
+    }
+}
+
+/**
+ * gtk_strength_bar_set_success_fraction:
+ * @self: a #GtkStrengthBar
+ * @fraction: a fraction from 0.0 to 1.0
+ * 
+ * Sets the value of the #GtkStrengthBar:success-fraction property.
+ */
+void
+gtk_strength_bar_set_success_fraction (GtkStrengthBar *self,
+                                       gdouble         fraction)
+{
+  g_return_if_fail (GTK_IS_STRENGTH_BAR (self));
+  g_return_if_fail (fraction >= 0.0 && fraction <= 1.0);
+
+  if (fraction != self->priv->success_fraction)
+    {
+      self->priv->success_fraction = fraction;
+      g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_SUCCESS_FRACTION]);
+    }
+}
diff --git a/gtk/gtkstrengthbar.h b/gtk/gtkstrengthbar.h
new file mode 100644
index 0000000..de32926
--- /dev/null
+++ b/gtk/gtkstrengthbar.h
@@ -0,0 +1,71 @@
+/* GTK - The GIMP Toolkit
+ * Copyright  2012 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Cosimo Cecchi <cosimoc gnome org>
+ *
+ */
+
+#if !defined (__GTK_H_INSIDE__) && !defined (GTK_COMPILATION)
+#error "Only <gtk/gtk.h> can be included directly."
+#endif
+
+#ifndef __GTK_STRENGTH_BAR_H__
+#define __GTK_STRENGTH_BAR_H__
+
+#include <gtk/gtkprogressbar.h>
+
+G_BEGIN_DECLS
+
+#define GTK_TYPE_STRENGTH_BAR            (gtk_strength_bar_get_type ())
+#define GTK_STRENGTH_BAR(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_STRENGTH_BAR, GtkStrengthBar))
+#define GTK_STRENGTH_BAR_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_STRENGTH_BAR, GtkStrengthBarClass))
+#define GTK_IS_STRENGTH_BAR(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_STRENGTH_BAR))
+#define GTK_IS_STRENGTH_BAR_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_STRENGTH_BAR))
+#define GTK_STRENGTH_BAR_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_STRENGTH_BAR, GtkStrengthBarClass))
+
+typedef struct _GtkStrengthBarClass GtkStrengthBarClass;
+typedef struct _GtkStrengthBar GtkStrengthBar;
+typedef struct _GtkStrengthBarPrivate GtkStrengthBarPrivate;
+
+struct _GtkStrengthBar {
+  GtkProgressBar parent;
+
+  /*< private >*/
+  GtkStrengthBarPrivate *priv;
+};
+
+struct _GtkStrengthBarClass {
+  GtkProgressBarClass parent_class;
+
+  /* padding for future class expansion */
+  gpointer padding[16];
+};
+
+GType      gtk_strength_bar_get_type             (void) G_GNUC_CONST;
+
+GtkWidget *gtk_strength_bar_new                  (void);
+
+void gtk_strength_bar_set_warning_fraction (GtkStrengthBar *self,
+                                            gdouble         fraction);
+void gtk_strength_bar_set_success_fraction (GtkStrengthBar *self,
+                                            gdouble         fraction);
+
+gdouble gtk_strength_bar_get_success_fraction (GtkStrengthBar *self);
+gdouble gtk_strength_bar_get_warning_fraction (GtkStrengthBar *self);
+
+G_END_DECLS
+
+#endif /* __GTK_STRENGTH_BAR_H__ */
diff --git a/gtk/gtkstylecontext.h b/gtk/gtkstylecontext.h
index e2873dc..0d23cd4 100644
--- a/gtk/gtkstylecontext.h
+++ b/gtk/gtkstylecontext.h
@@ -692,6 +692,16 @@ struct _GtkStyleContextClass
 #define GTK_STYLE_CLASS_OSD "osd"
 
 /**
+ * GTK_STYLE_CLASS_STRENGTH_BAR:
+ *
+ * A CSS class used when rendering a strength indicator, such
+ * as a battery charge level, or a password strength.
+ *
+ * This is used by #GtkStrengthBar.
+ */
+#define GTK_STYLE_CLASS_STRENGTH_BAR "strength-bar"
+
+/**
  * GTK_STYLE_CLASS_SUCCESS:
  *
  * A CSS class used when the rendered element is in a successful state.



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