]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkcellrendererprogress.c
GtkBubbleWindow: allocate CSS borders and paddings
[~andy/gtk] / gtk / gtkcellrendererprogress.c
index a4afff66dbc763f1718304427029f6d89eb08b08..2e4b9b31c37376564d589b907b5723f90a3afa47 100644 (file)
@@ -14,9 +14,7 @@
  * Library General Public License for more details.
  *
  * You should have received a copy of the GNU Library General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 /*
  * Modified by the GTK+ Team and others 1997-2007.  See the AUTHORS
 #include <stdlib.h>
 
 #include "gtkcellrendererprogress.h"
+#include "gtkorientable.h"
 #include "gtkprivate.h"
 #include "gtkintl.h"
-#include "gtkalias.h"
 
-#define GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object),                        \
-                                                                                     GTK_TYPE_CELL_RENDERER_PROGRESS, \
-                                                                                     GtkCellRendererProgressPrivate))
+
+/**
+ * SECTION:gtkcellrendererprogress
+ * @Short_description: Renders numbers as progress bars
+ * @Title: GtkCellRendererProgress
+ *
+ * #GtkCellRendererProgress renders a numeric value as a progress par in a cell.
+ * Additionally, it can display a text on top of the progress bar.
+ *
+ * The #GtkCellRendererProgress cell renderer was added in GTK+ 2.6.
+ */
+
 
 enum
 {
   PROP_0,
   PROP_VALUE,
   PROP_TEXT,
-  PROP_PULSE
-}; 
+  PROP_PULSE,
+  PROP_TEXT_XALIGN,
+  PROP_TEXT_YALIGN,
+  PROP_ORIENTATION,
+  PROP_INVERTED
+};
 
 struct _GtkCellRendererProgressPrivate
 {
@@ -54,6 +65,10 @@ struct _GtkCellRendererProgressPrivate
   gint min_w;
   gint pulse;
   gint offset;
+  gfloat text_xalign;
+  gfloat text_yalign;
+  GtkOrientation orientation;
+  gboolean inverted;
 };
 
 static void gtk_cell_renderer_progress_finalize     (GObject                 *object);
@@ -78,21 +93,21 @@ static void compute_dimensions                      (GtkCellRenderer         *ce
                                                     gint                    *height);
 static void gtk_cell_renderer_progress_get_size     (GtkCellRenderer         *cell,
                                                     GtkWidget               *widget,
-                                                    GdkRectangle            *cell_area,
+                                                    const GdkRectangle      *cell_area,
                                                     gint                    *x_offset,
                                                     gint                    *y_offset,
                                                     gint                    *width,
                                                     gint                    *height);
 static void gtk_cell_renderer_progress_render       (GtkCellRenderer         *cell,
-                                                    GdkWindow               *window,
+                                                    cairo_t                 *cr,
                                                     GtkWidget               *widget,
-                                                    GdkRectangle            *background_area,
-                                                    GdkRectangle            *cell_area,
-                                                    GdkRectangle            *expose_area,
-                                                    guint                    flags);
+                                                    const GdkRectangle      *background_area,
+                                                    const GdkRectangle      *cell_area,
+                                                    GtkCellRendererState    flags);
 
      
-G_DEFINE_TYPE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER)
+G_DEFINE_TYPE_WITH_CODE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
 
 static void
 gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
@@ -109,9 +124,9 @@ gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
   
   /**
    * GtkCellRendererProgress:value:
-   * 
+   *
    * The "value" property determines the percentage to which the
-   * progress bar will be "filled in". 
+   * progress bar will be "filled in".
    *
    * Since: 2.6
    **/
@@ -146,7 +161,7 @@ gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
    * 
    * Setting this to a non-negative value causes the cell renderer to
    * enter "activity mode", where a block bounces back and forth to 
-   * indicate that some progress is made, with specifying exactly how
+   * indicate that some progress is made, without specifying exactly how
    * much.
    *
    * Each increment of the property causes the block to move by a little 
@@ -165,6 +180,52 @@ gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
                                                      -1, G_MAXINT, -1,
                                                      GTK_PARAM_READWRITE));
 
+  /**
+   * GtkCellRendererProgress:text-xalign:
+   *
+   * The "text-xalign" property controls the horizontal alignment of the
+   * text in the progress bar.  Valid values range from 0 (left) to 1
+   * (right).  Reserved for RTL layouts.
+   *
+   * Since: 2.12
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_TEXT_XALIGN,
+                                   g_param_spec_float ("text-xalign",
+                                                       P_("Text x alignment"),
+                                                       P_("The horizontal text alignment, from 0 (left) to 1 (right). Reversed for RTL layouts."),
+                                                       0.0, 1.0, 0.5,
+                                                       GTK_PARAM_READWRITE));
+
+  /**
+   * GtkCellRendererProgress:text-yalign:
+   *
+   * The "text-yalign" property controls the vertical alignment of the
+   * text in the progress bar.  Valid values range from 0 (top) to 1
+   * (bottom).
+   *
+   * Since: 2.12
+   */
+  g_object_class_install_property (object_class,
+                                   PROP_TEXT_YALIGN,
+                                   g_param_spec_float ("text-yalign",
+                                                       P_("Text y alignment"),
+                                                       P_("The vertical text alignment, from 0 (top) to 1 (bottom)."),
+                                                       0.0, 1.0, 0.5,
+                                                       GTK_PARAM_READWRITE));
+
+  g_object_class_override_property (object_class,
+                                    PROP_ORIENTATION,
+                                    "orientation");
+
+  g_object_class_install_property (object_class,
+                                   PROP_INVERTED,
+                                   g_param_spec_boolean ("inverted",
+                                                         P_("Inverted"),
+                                                         P_("Invert the direction in which the progress bar grows"),
+                                                         FALSE,
+                                                         GTK_PARAM_READWRITE));
+
   g_type_class_add_private (object_class, 
                            sizeof (GtkCellRendererProgressPrivate));
 }
@@ -172,7 +233,12 @@ gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
 static void
 gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
 {
-  GtkCellRendererProgressPrivate *priv = GTK_CELL_RENDERER_PROGRESS_GET_PRIVATE (cellprogress);
+  GtkCellRendererProgressPrivate *priv;
+
+  cellprogress->priv = G_TYPE_INSTANCE_GET_PRIVATE (cellprogress,
+                                                    GTK_TYPE_CELL_RENDERER_PROGRESS,
+                                                    GtkCellRendererProgressPrivate);
+  priv = cellprogress->priv;
 
   priv->value = 0;
   priv->text = NULL;
@@ -182,7 +248,11 @@ gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
   priv->pulse = -1;
   priv->offset = 0;
 
-  cellprogress->priv = priv;
+  priv->text_xalign = 0.5;
+  priv->text_yalign = 0.5;
+
+  priv->orientation = GTK_ORIENTATION_HORIZONTAL,
+  priv->inverted = FALSE;
 }
 
 
@@ -233,6 +303,18 @@ gtk_cell_renderer_progress_get_property (GObject *object,
     case PROP_PULSE:
       g_value_set_int (value, priv->pulse);
       break;
+    case PROP_TEXT_XALIGN:
+      g_value_set_float (value, priv->text_xalign);
+      break;
+    case PROP_TEXT_YALIGN:
+      g_value_set_float (value, priv->text_yalign);
+      break;
+    case PROP_ORIENTATION:
+      g_value_set_enum (value, priv->orientation);
+      break;
+    case PROP_INVERTED:
+      g_value_set_boolean (value, priv->inverted);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
     }
@@ -245,6 +327,7 @@ gtk_cell_renderer_progress_set_property (GObject *object,
                                         GParamSpec   *pspec)
 {
   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
+  GtkCellRendererProgressPrivate *priv = cellprogress->priv;
   
   switch (param_id)
     {
@@ -260,6 +343,18 @@ gtk_cell_renderer_progress_set_property (GObject *object,
       gtk_cell_renderer_progress_set_pulse (cellprogress, 
                                            g_value_get_int (value));
       break;
+    case PROP_TEXT_XALIGN:
+      priv->text_xalign = g_value_get_float (value);
+      break;
+    case PROP_TEXT_YALIGN:
+      priv->text_yalign = g_value_get_float (value);
+      break;
+    case PROP_ORIENTATION:
+      priv->orientation = g_value_get_enum (value);
+      break;
+    case PROP_INVERTED:
+      priv->inverted = g_value_get_boolean (value);
+      break;
     default:
       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
     }
@@ -274,8 +369,7 @@ recompute_label (GtkCellRendererProgress *cellprogress)
   if (priv->text)
     label = g_strdup (priv->text);
   else if (priv->pulse < 0)
-    /* do not translate the part before the | */
-    label = g_strdup_printf (Q_("progress bar label|%d %%"), priv->value);
+    label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
   else
     label = NULL;
  
@@ -313,10 +407,10 @@ gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
 
    if (pulse != priv->pulse)
      {
-       if (priv->pulse <= 0)
+       if (pulse <= 0)
          priv->offset = 0;
        else
-         priv->offset++;
+         priv->offset = pulse;
      }
 
    priv->pulse = pulse;
@@ -333,27 +427,30 @@ compute_dimensions (GtkCellRenderer *cell,
 {
   PangoRectangle logical_rect;
   PangoLayout *layout;
+  gint xpad, ypad;
   
   layout = gtk_widget_create_pango_layout (widget, text);
   pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
+
+  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
   
   if (width)
-    *width = logical_rect.width + cell->xpad * 2;
+    *width = logical_rect.width + xpad * 2;
   
   if (height)
-    *height = logical_rect.height + cell->ypad * 2;
+    *height = logical_rect.height + ypad * 2;
 
   g_object_unref (layout);
 }
 
 static void
-gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
-                                    GtkWidget       *widget,
-                                    GdkRectangle    *cell_area,
-                                    gint            *x_offset,
-                                    gint            *y_offset,
-                                    gint            *width,
-                                    gint            *height)
+gtk_cell_renderer_progress_get_size (GtkCellRenderer    *cell,
+                                    GtkWidget          *widget,
+                                    const GdkRectangle *cell_area,
+                                    gint               *x_offset,
+                                    gint               *y_offset,
+                                    gint               *width,
+                                    gint               *height)
 {
   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
   GtkCellRendererProgressPrivate *priv = cellprogress->priv;
@@ -362,7 +459,7 @@ gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
 
   if (priv->min_w < 0)
     {
-      text = g_strdup_printf (Q_("progress bar label|%d %%"), 100);
+      text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
       compute_dimensions (cell, widget, text,
                          &priv->min_w,
                          &priv->min_h);
@@ -394,116 +491,228 @@ gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
   if (y_offset) *y_offset = 0;
 }
 
+static inline gint
+get_bar_size (gint pulse,
+             gint value,
+             gint full_size)
+{
+  gint bar_size;
+
+  if (pulse < 0)
+    bar_size = full_size * MAX (0, value) / 100;
+  else if (pulse == 0)
+    bar_size = 0;
+  else if (pulse == G_MAXINT)
+    bar_size = full_size;
+  else
+    bar_size = MAX (2, full_size / 5);
+
+  return bar_size;
+}
+
+static inline gint
+get_bar_position (gint     start,
+                 gint     full_size,
+                 gint     bar_size,
+                 gint     pulse,
+                 gint     offset,
+                 gboolean is_rtl)
+{
+  gint position;
+
+  if (pulse < 0 || pulse == 0 || pulse == G_MAXINT)
+    {
+      position = is_rtl ? (start + full_size - bar_size) : start;
+    }
+  else
+    {
+      position = (is_rtl ? offset + 12 : offset) % 24;
+      if (position > 12)
+       position = 24 - position;
+      position = start + full_size * position / 15;
+    }
+
+  return position;
+}
+
 static void
-gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
-                                  GdkWindow       *window,
-                                  GtkWidget       *widget,
-                                  GdkRectangle    *background_area,
-                                  GdkRectangle    *cell_area,
-                                  GdkRectangle    *expose_area,
-                                  guint            flags)
+gtk_cell_renderer_progress_render (GtkCellRenderer      *cell,
+                                   cairo_t              *cr,
+                                  GtkWidget            *widget,
+                                  const GdkRectangle   *background_area,
+                                  const GdkRectangle   *cell_area,
+                                  GtkCellRendererState  flags)
 {
   GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
-  GtkCellRendererProgressPrivate *priv= cellprogress->priv; 
+  GtkCellRendererProgressPrivate *priv= cellprogress->priv;
+  GtkStyleContext *context;
+  GtkBorder padding;
   PangoLayout *layout;
   PangoRectangle logical_rect;
-  gint x, y, w, h, x_pos, y_pos, bar_x, bar_width;
+  gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
+  gint xpad, ypad;
   GdkRectangle clip;
   gboolean is_rtl;
 
+  context = gtk_widget_get_style_context (widget);
   is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
-  
-  x = cell_area->x + cell->xpad;
-  y = cell_area->y + cell->ypad;
-  w = cell_area->width - cell->xpad * 2;
-  h = cell_area->height - cell->ypad * 2;
-
-  /* FIXME: GtkProgressBar draws the box with "trough" detail,
-   * but some engines don't paint anything with that detail for
-   * non-GtkProgressBar widgets.
-   */
-  gtk_paint_box (widget->style,
-                window,
-                GTK_STATE_NORMAL, GTK_SHADOW_IN, 
-                NULL, widget, NULL,
-                x, y, w, h);
-
-  clip.y = y;
-  clip.height = h;
-  if (priv->pulse < 0)
-    {
-      clip.width = w * MAX (0, priv->value) / 100;
-      clip.x = is_rtl ? (x + w - clip.width) : x;
-    }
-  else if (priv->pulse == 0)
+
+  gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
+  x = cell_area->x + xpad;
+  y = cell_area->y + ypad;
+  w = cell_area->width - xpad * 2;
+  h = cell_area->height - ypad * 2;
+
+  gtk_style_context_save (context);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
+
+  gtk_render_background (context, cr, x, y, w, h);
+  gtk_render_frame (context, cr, x, y, w, h);
+
+  gtk_style_context_get_padding (context, GTK_STATE_FLAG_NORMAL, &padding);
+
+  x += padding.left;
+  y += padding.top;
+  w -= padding.left + padding.right;
+  h -= padding.top + padding.bottom;
+
+  gtk_style_context_restore (context);
+
+  if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
     {
-      clip.x = x;
-      clip.width = 0;
+      clip.y = y;
+      clip.height = h;
+
+      start = x;
+      full_size = w;
+
+      bar_size = get_bar_size (priv->pulse, priv->value, full_size);
+
+      if (!priv->inverted)
+       bar_position = get_bar_position (start, full_size, bar_size,
+                                        priv->pulse, priv->offset, is_rtl);
+      else
+       bar_position = get_bar_position (start, full_size, bar_size,
+                                        priv->pulse, priv->offset, !is_rtl);
+
+      clip.width = bar_size;
+      clip.x = bar_position;
     }
-  else if (priv->pulse == G_MAXINT)
+  else
     {
       clip.x = x;
       clip.width = w;
+
+      start = y;
+      full_size = h;
+
+      bar_size = get_bar_size (priv->pulse, priv->value, full_size);
+
+      if (priv->inverted)
+       bar_position = get_bar_position (start, full_size, bar_size,
+                                        priv->pulse, priv->offset, TRUE);
+      else
+       bar_position = get_bar_position (start, full_size, bar_size,
+                                        priv->pulse, priv->offset, FALSE);
+
+      clip.height = bar_size;
+      clip.y = bar_position;
     }
-  else
-    {
-      clip.width = MAX (2, w / 5);
-      x_pos = (is_rtl ? priv->offset + 12 : priv->offset) % 24;
-      if (x_pos > 12)
-        x_pos = 24 - x_pos;
-      clip.x = x + w * x_pos / 15; 
-    }
 
-  gtk_paint_box (widget->style,
-                window,
-                GTK_STATE_SELECTED, GTK_SHADOW_OUT,
-                &clip, widget, "bar",
-                clip.x, clip.y,
-                clip.width, clip.height);
+  gtk_style_context_save (context);
+  gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
+
+  if (bar_size > 0)
+    gtk_render_activity (context, cr,
+                         clip.x, clip.y,
+                         clip.width, clip.height);
+
+  gtk_style_context_restore (context);
 
   if (priv->label)
     {
+      gfloat text_xalign;
+
       layout = gtk_widget_create_pango_layout (widget, priv->label);
       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
-      
-      x_pos = x + (w - logical_rect.width) / 2;
-      y_pos = y + (h - logical_rect.height) / 2;
-  
-      gtk_paint_layout (widget->style, window, 
-                       GTK_STATE_SELECTED,
-                       FALSE, &clip, widget, "progressbar",
-                       x_pos, y_pos, 
-                       layout);
-
-      bar_x = clip.x;
-      bar_width = clip.width;
-      if (bar_x > x)
+
+      if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
+       text_xalign = 1.0 - priv->text_xalign;
+      else
+       text_xalign = priv->text_xalign;
+
+      x_pos = x + padding.left + text_xalign *
+       (w - padding.left - padding.right - logical_rect.width);
+
+      y_pos = y + padding.top + priv->text_yalign *
+       (h - padding.top - padding.bottom - logical_rect.height);
+
+      cairo_save (cr);
+      gdk_cairo_rectangle (cr, &clip);
+      cairo_clip (cr);
+
+      gtk_style_context_save (context);
+      gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
+
+      gtk_render_layout (context, cr,
+                         x_pos, y_pos,
+                         layout);
+
+      gtk_style_context_restore (context);
+      cairo_restore (cr);
+
+      gtk_style_context_save (context);
+      gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
+
+      if (bar_position > start)
         {
-          clip.x = x;
-          clip.width = bar_x - x;
-
-          gtk_paint_layout (widget->style, window, 
-                           GTK_STATE_NORMAL,
-                           FALSE, &clip, widget, "progressbar",
-                           x_pos, y_pos,
-                           layout);
+         if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+           {
+             clip.x = x;
+             clip.width = bar_position - x;
+           }
+         else
+           {
+             clip.y = y;
+             clip.height = bar_position - y;
+           }
+
+          cairo_save (cr);
+          gdk_cairo_rectangle (cr, &clip);
+          cairo_clip (cr);
+
+          gtk_render_layout (context, cr,
+                             x_pos, y_pos,
+                             layout);
+
+          cairo_restore (cr);
         }
 
-      if (bar_x + bar_width < x + w)
+      if (bar_position + bar_size < start + full_size)
         {
-          clip.x = bar_x + bar_width;
-          clip.width = x + w - (bar_x + bar_width);  
-
-          gtk_paint_layout (widget->style, window, 
-                           GTK_STATE_NORMAL,
-                           FALSE, &clip, widget, "progressbar",
-                           x_pos, y_pos,
-                           layout);
+         if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
+           {
+             clip.x = bar_position + bar_size;
+             clip.width = x + w - (bar_position + bar_size);
+           }
+         else
+           {
+             clip.y = bar_position + bar_size;
+             clip.height = y + h - (bar_position + bar_size);
+           }
+
+          cairo_save (cr);
+          gdk_cairo_rectangle (cr, &clip);
+          cairo_clip (cr);
+
+          gtk_render_layout (context, cr,
+                             x_pos, y_pos,
+                             layout);
+
+          cairo_restore (cr);
         }
 
+      gtk_style_context_restore (context);
       g_object_unref (layout);
     }
 }
-
-#define __GTK_CELL_RENDERER_PROGRESS_C__
-#include "gtkaliasdef.c"