[gtk+/gtk-style-context: 308/540] Add gtk_render_activity()



commit d6b41d2462a3f2e689e61c4c3f65ae1fdd1baa62
Author: Carlos Garnacho <carlosg gnome org>
Date:   Mon Nov 1 02:46:35 2010 +0100

    Add gtk_render_activity()
    
    This method will render progress bars and spinners, these should
    handle active and inconsistent states' animation progress in order
    to perform animations.

 gtk/gtkstylecontext.c  |   23 ++++++++++++
 gtk/gtkstylecontext.h  |    2 +-
 gtk/gtkthemingengine.c |   93 ++++++++++++++++++++++++++++++++++++++++++++++++
 gtk/gtkthemingengine.h |    6 +++
 4 files changed, 123 insertions(+), 1 deletions(-)
---
diff --git a/gtk/gtkstylecontext.c b/gtk/gtkstylecontext.c
index 6479c75..462d0df 100644
--- a/gtk/gtkstylecontext.c
+++ b/gtk/gtkstylecontext.c
@@ -3043,3 +3043,26 @@ gtk_render_handle (GtkStyleContext *context,
   _gtk_theming_engine_set_context (priv->theming_engine, context);
   engine_class->render_handle (priv->theming_engine, cr, x, y, width, height);
 }
+
+void
+gtk_render_activity (GtkStyleContext *context,
+                     cairo_t         *cr,
+                     gdouble          x,
+                     gdouble          y,
+                     gdouble          width,
+                     gdouble          height)
+{
+  GtkStyleContextPrivate *priv;
+  GtkThemingEngineClass *engine_class;
+
+  g_return_if_fail (GTK_IS_STYLE_CONTEXT (context));
+  g_return_if_fail (cr != NULL);
+
+  priv = context->priv;
+  engine_class = GTK_THEMING_ENGINE_GET_CLASS (priv->theming_engine);
+
+  store_animation_region (context, x, y, width, height);
+
+  _gtk_theming_engine_set_context (priv->theming_engine, context);
+  engine_class->render_activity (priv->theming_engine, cr, x, y, width, height);
+}
diff --git a/gtk/gtkstylecontext.h b/gtk/gtkstylecontext.h
index 212de85..5a37307 100644
--- a/gtk/gtkstylecontext.h
+++ b/gtk/gtkstylecontext.h
@@ -258,7 +258,7 @@ void gtk_render_handle    (GtkStyleContext *context,
                            gdouble          y,
                            gdouble          width,
                            gdouble          height);
-void gtk_render_progress  (GtkStyleContext *context,
+void gtk_render_activity  (GtkStyleContext *context,
                            cairo_t         *cr,
                            gdouble          x,
                            gdouble          y,
diff --git a/gtk/gtkthemingengine.c b/gtk/gtkthemingengine.c
index d1741d2..474bd0d 100644
--- a/gtk/gtkthemingengine.c
+++ b/gtk/gtkthemingengine.c
@@ -162,6 +162,12 @@ static void gtk_theming_engine_render_handle    (GtkThemingEngine *engine,
                                                  gdouble           y,
                                                  gdouble           width,
                                                  gdouble           height);
+static void gtk_theming_engine_render_activity  (GtkThemingEngine *engine,
+                                                 cairo_t          *cr,
+                                                 gdouble           x,
+                                                 gdouble           y,
+                                                 gdouble           width,
+                                                 gdouble           height);
 
 G_DEFINE_TYPE (GtkThemingEngine, gtk_theming_engine, G_TYPE_OBJECT)
 
@@ -213,6 +219,7 @@ gtk_theming_engine_class_init (GtkThemingEngineClass *klass)
   klass->render_frame_gap = gtk_theming_engine_render_frame_gap;
   klass->render_extension = gtk_theming_engine_render_extension;
   klass->render_handle = gtk_theming_engine_render_handle;
+  klass->render_activity = gtk_theming_engine_render_activity;
 
   /**
    * GtkThemingEngine:name:
@@ -2714,3 +2721,89 @@ gtk_theming_engine_render_handle (GtkThemingEngine *engine,
 
   gdk_rgba_free (bg_color);
 }
+
+static void
+gtk_theming_engine_render_activity (GtkThemingEngine *engine,
+                                    cairo_t          *cr,
+                                    gdouble           x,
+                                    gdouble           y,
+                                    gdouble           width,
+                                    gdouble           height)
+{
+  if (gtk_theming_engine_has_class (engine, "spinner"))
+    {
+      GtkStateFlags state;
+      guint num_steps, step;
+      GdkRGBA *color;
+      gdouble dx, dy;
+      gdouble progress;
+      gdouble radius;
+      gdouble half;
+      gint i;
+
+      gtk_theming_engine_get_style (engine,
+                                    "num-steps", &num_steps,
+                                    NULL);
+
+      state = gtk_theming_engine_get_state (engine);
+      gtk_theming_engine_get (engine, state,
+                              "color", &color,
+                              NULL);
+      if (num_steps == 0)
+        num_steps = 12;
+
+      if (gtk_theming_engine_state_is_running (engine, GTK_STATE_ACTIVE, &progress))
+        step = (guint) (progress * num_steps);
+      else
+        step = 0;
+
+      cairo_save (cr);
+
+      cairo_translate (cr, x, y);
+
+      /* draw clip region */
+      cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
+
+      dx = width / 2;
+      dy = height / 2;
+      radius = MIN (width / 2, height / 2);
+      half = num_steps / 2;
+
+      for (i = 0; i < num_steps; i++)
+        {
+          gint inset = 0.7 * radius;
+
+          /* transparency is a function of time and intial value */
+          gdouble t = (gdouble) ((i + num_steps - step)
+                                 % num_steps) / num_steps;
+
+          cairo_save (cr);
+
+          cairo_set_source_rgba (cr,
+                                 color->red,
+                                 color->green,
+                                 color->blue,
+                                 color->alpha * t);
+
+          cairo_set_line_width (cr, 2.0);
+          cairo_move_to (cr,
+                         dx + (radius - inset) * cos (i * G_PI / half),
+                         dy + (radius - inset) * sin (i * G_PI / half));
+          cairo_line_to (cr,
+                         dx + radius * cos (i * G_PI / half),
+                         dy + radius * sin (i * G_PI / half));
+          cairo_stroke (cr);
+
+          cairo_restore (cr);
+        }
+
+      cairo_restore (cr);
+
+      gdk_rgba_free (color);
+    }
+  else
+    {
+      gtk_theming_engine_render_background (engine, cr, x, y, width, height);
+      gtk_theming_engine_render_frame (engine, cr, x, y, width, height);
+    }
+}
diff --git a/gtk/gtkthemingengine.h b/gtk/gtkthemingengine.h
index 9cbae78..78842c6 100644
--- a/gtk/gtkthemingengine.h
+++ b/gtk/gtkthemingengine.h
@@ -151,6 +151,12 @@ struct _GtkThemingEngineClass
                              gdouble           y,
                              gdouble           width,
                              gdouble           height);
+  void (* render_activity) (GtkThemingEngine *engine,
+                            cairo_t          *cr,
+                            gdouble           x,
+                            gdouble           y,
+                            gdouble           width,
+                            gdouble           height);
 };
 
 GType gtk_theming_engine_get_type (void) G_GNUC_CONST;



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