2004-05-12 16:07:44 +00:00
|
|
|
/* gtkcellrendererprogress.c
|
|
|
|
* Copyright (C) 2002 Naba Kumar <kh_naba@users.sourceforge.net>
|
|
|
|
* heavily modified by Jörgen Scheibengruber <mfcn@gmx.de>
|
|
|
|
* heavily modified by Marco Pesenti Gritti <marco@gnome.org>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
2012-02-27 13:01:10 +00:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
2004-05-12 16:07:44 +00:00
|
|
|
*/
|
|
|
|
/*
|
2007-01-02 04:11:15 +00:00
|
|
|
* Modified by the GTK+ Team and others 1997-2007. See the AUTHORS
|
2004-05-12 16:07:44 +00:00
|
|
|
* file for a list of people on the GTK+ Team. See the ChangeLog
|
|
|
|
* files for a list of changes. These files are distributed with
|
|
|
|
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <stdlib.h>
|
2004-08-09 16:59:53 +00:00
|
|
|
|
2004-05-12 16:07:44 +00:00
|
|
|
#include "gtkcellrendererprogress.h"
|
2010-09-02 03:27:00 +00:00
|
|
|
#include "gtkorientable.h"
|
2005-03-22 02:14:55 +00:00
|
|
|
#include "gtkprivate.h"
|
2004-05-12 16:07:44 +00:00
|
|
|
#include "gtkintl.h"
|
|
|
|
|
|
|
|
|
2011-04-11 01:18:13 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-05-12 16:07:44 +00:00
|
|
|
enum
|
|
|
|
{
|
2006-12-06 21:40:02 +00:00
|
|
|
PROP_0,
|
|
|
|
PROP_VALUE,
|
2007-01-02 04:11:15 +00:00
|
|
|
PROP_TEXT,
|
2007-06-06 20:27:15 +00:00
|
|
|
PROP_PULSE,
|
|
|
|
PROP_TEXT_XALIGN,
|
2007-06-13 12:41:24 +00:00
|
|
|
PROP_TEXT_YALIGN,
|
2010-09-02 03:27:00 +00:00
|
|
|
PROP_ORIENTATION,
|
|
|
|
PROP_INVERTED
|
|
|
|
};
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
struct _GtkCellRendererProgressPrivate
|
|
|
|
{
|
|
|
|
gint value;
|
|
|
|
gchar *text;
|
|
|
|
gchar *label;
|
|
|
|
gint min_h;
|
|
|
|
gint min_w;
|
2007-01-02 04:11:15 +00:00
|
|
|
gint pulse;
|
|
|
|
gint offset;
|
2007-06-06 20:27:15 +00:00
|
|
|
gfloat text_xalign;
|
|
|
|
gfloat text_yalign;
|
2010-09-02 03:27:00 +00:00
|
|
|
GtkOrientation orientation;
|
|
|
|
gboolean inverted;
|
2004-05-12 16:07:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void gtk_cell_renderer_progress_finalize (GObject *object);
|
|
|
|
static void gtk_cell_renderer_progress_get_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gtk_cell_renderer_progress_set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec);
|
|
|
|
static void gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
|
|
|
|
gint value);
|
|
|
|
static void gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
|
2004-05-17 02:33:18 +00:00
|
|
|
const gchar *text);
|
2007-01-02 04:11:15 +00:00
|
|
|
static void gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
|
|
|
|
gint pulse);
|
2004-05-17 02:33:18 +00:00
|
|
|
static void compute_dimensions (GtkCellRenderer *cell,
|
|
|
|
GtkWidget *widget,
|
2004-05-12 16:07:44 +00:00
|
|
|
const gchar *text,
|
|
|
|
gint *width,
|
|
|
|
gint *height);
|
|
|
|
static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
|
|
|
|
GtkWidget *widget,
|
2010-10-04 14:22:09 +00:00
|
|
|
const GdkRectangle *cell_area,
|
2004-05-12 16:07:44 +00:00
|
|
|
gint *x_offset,
|
|
|
|
gint *y_offset,
|
|
|
|
gint *width,
|
|
|
|
gint *height);
|
|
|
|
static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
|
2010-08-21 11:18:14 +00:00
|
|
|
cairo_t *cr,
|
2004-05-12 16:07:44 +00:00
|
|
|
GtkWidget *widget,
|
2010-08-21 11:18:14 +00:00
|
|
|
const GdkRectangle *background_area,
|
|
|
|
const GdkRectangle *cell_area,
|
2010-12-23 03:58:40 +00:00
|
|
|
GtkCellRendererState flags);
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
G_DEFINE_TYPE_WITH_CODE (GtkCellRendererProgress, gtk_cell_renderer_progress, GTK_TYPE_CELL_RENDERER,
|
2013-06-27 19:02:52 +00:00
|
|
|
G_ADD_PRIVATE (GtkCellRendererProgress)
|
2010-09-02 03:27:00 +00:00
|
|
|
G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->finalize = gtk_cell_renderer_progress_finalize;
|
|
|
|
object_class->get_property = gtk_cell_renderer_progress_get_property;
|
|
|
|
object_class->set_property = gtk_cell_renderer_progress_set_property;
|
|
|
|
|
|
|
|
cell_class->get_size = gtk_cell_renderer_progress_get_size;
|
|
|
|
cell_class->render = gtk_cell_renderer_progress_render;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkCellRendererProgress:value:
|
2010-09-02 03:27:00 +00:00
|
|
|
*
|
2004-05-12 16:07:44 +00:00
|
|
|
* The "value" property determines the percentage to which the
|
2010-09-02 03:27:00 +00:00
|
|
|
* progress bar will be "filled in".
|
2004-05-12 16:07:44 +00:00
|
|
|
*
|
|
|
|
* Since: 2.6
|
|
|
|
**/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_VALUE,
|
|
|
|
g_param_spec_int ("value",
|
|
|
|
P_("Value"),
|
|
|
|
P_("Value of the progress bar"),
|
2004-05-17 02:33:18 +00:00
|
|
|
0, 100, 0,
|
2005-03-22 02:14:55 +00:00
|
|
|
GTK_PARAM_READWRITE));
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GtkCellRendererProgress:text:
|
|
|
|
*
|
|
|
|
* The "text" property determines the label which will be drawn
|
|
|
|
* over the progress bar. Setting this property to %NULL causes the default
|
|
|
|
* label to be displayed. Setting this property to an empty string causes
|
|
|
|
* no label to be displayed.
|
|
|
|
*
|
|
|
|
* Since: 2.6
|
|
|
|
**/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_TEXT,
|
|
|
|
g_param_spec_string ("text",
|
|
|
|
P_("Text"),
|
|
|
|
P_("Text on the progress bar"),
|
|
|
|
NULL,
|
2005-03-22 02:14:55 +00:00
|
|
|
GTK_PARAM_READWRITE));
|
2004-05-12 16:07:44 +00:00
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
/**
|
|
|
|
* GtkCellRendererProgress:pulse:
|
|
|
|
*
|
|
|
|
* Setting this to a non-negative value causes the cell renderer to
|
|
|
|
* enter "activity mode", where a block bounces back and forth to
|
2007-01-02 23:36:32 +00:00
|
|
|
* indicate that some progress is made, without specifying exactly how
|
2007-01-02 04:11:15 +00:00
|
|
|
* much.
|
|
|
|
*
|
|
|
|
* Each increment of the property causes the block to move by a little
|
|
|
|
* bit.
|
|
|
|
*
|
|
|
|
* To indicate that the activity has not started yet, set the property
|
|
|
|
* to zero. To indicate completion, set the property to %G_MAXINT.
|
|
|
|
*
|
|
|
|
* Since: 2.12
|
|
|
|
*/
|
|
|
|
g_object_class_install_property (object_class,
|
|
|
|
PROP_PULSE,
|
|
|
|
g_param_spec_int ("pulse",
|
|
|
|
P_("Pulse"),
|
|
|
|
P_("Set this to positive values to indicate that some progress is made, but you don't know how much."),
|
|
|
|
-1, G_MAXINT, -1,
|
|
|
|
GTK_PARAM_READWRITE));
|
|
|
|
|
2007-06-06 20:27:15 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
g_object_class_override_property (object_class,
|
|
|
|
PROP_ORIENTATION,
|
|
|
|
"orientation");
|
2007-06-13 12:41:24 +00:00
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
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));
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress)
|
|
|
|
{
|
2010-06-01 17:15:23 +00:00
|
|
|
GtkCellRendererProgressPrivate *priv;
|
|
|
|
|
2013-06-27 19:02:52 +00:00
|
|
|
cellprogress->priv = gtk_cell_renderer_progress_get_instance_private (cellprogress);
|
2010-06-01 17:15:23 +00:00
|
|
|
priv = cellprogress->priv;
|
2007-01-02 04:11:15 +00:00
|
|
|
|
|
|
|
priv->value = 0;
|
|
|
|
priv->text = NULL;
|
|
|
|
priv->label = NULL;
|
|
|
|
priv->min_w = -1;
|
|
|
|
priv->min_h = -1;
|
|
|
|
priv->pulse = -1;
|
|
|
|
priv->offset = 0;
|
|
|
|
|
2007-06-06 20:27:15 +00:00
|
|
|
priv->text_xalign = 0.5;
|
|
|
|
priv->text_yalign = 0.5;
|
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
priv->orientation = GTK_ORIENTATION_HORIZONTAL,
|
|
|
|
priv->inverted = FALSE;
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gtk_cell_renderer_progress_new:
|
|
|
|
*
|
|
|
|
* Creates a new #GtkCellRendererProgress.
|
|
|
|
*
|
|
|
|
* Return value: the new cell renderer
|
|
|
|
*
|
|
|
|
* Since: 2.6
|
|
|
|
**/
|
|
|
|
GtkCellRenderer*
|
|
|
|
gtk_cell_renderer_progress_new (void)
|
|
|
|
{
|
2004-11-19 23:18:38 +00:00
|
|
|
return g_object_new (GTK_TYPE_CELL_RENDERER_PROGRESS, NULL);
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
|
2007-01-02 04:11:15 +00:00
|
|
|
GtkCellRendererProgressPrivate *priv = cellprogress->priv;
|
2004-05-12 16:07:44 +00:00
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
g_free (priv->text);
|
|
|
|
g_free (priv->label);
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
G_OBJECT_CLASS (gtk_cell_renderer_progress_parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_get_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
|
2007-01-02 04:11:15 +00:00
|
|
|
GtkCellRendererProgressPrivate *priv = cellprogress->priv;
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_VALUE:
|
2007-01-02 04:11:15 +00:00
|
|
|
g_value_set_int (value, priv->value);
|
2004-05-12 16:07:44 +00:00
|
|
|
break;
|
|
|
|
case PROP_TEXT:
|
2007-01-02 04:11:15 +00:00
|
|
|
g_value_set_string (value, priv->text);
|
|
|
|
break;
|
|
|
|
case PROP_PULSE:
|
|
|
|
g_value_set_int (value, priv->pulse);
|
2004-05-12 16:07:44 +00:00
|
|
|
break;
|
2007-06-06 20:27:15 +00:00
|
|
|
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;
|
2007-06-13 12:41:24 +00:00
|
|
|
case PROP_ORIENTATION:
|
|
|
|
g_value_set_enum (value, priv->orientation);
|
|
|
|
break;
|
2010-09-02 03:27:00 +00:00
|
|
|
case PROP_INVERTED:
|
|
|
|
g_value_set_boolean (value, priv->inverted);
|
|
|
|
break;
|
2004-05-12 16:07:44 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_set_property (GObject *object,
|
|
|
|
guint param_id,
|
|
|
|
const GValue *value,
|
|
|
|
GParamSpec *pspec)
|
|
|
|
{
|
|
|
|
GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object);
|
2007-06-06 20:27:15 +00:00
|
|
|
GtkCellRendererProgressPrivate *priv = cellprogress->priv;
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
switch (param_id)
|
|
|
|
{
|
|
|
|
case PROP_VALUE:
|
|
|
|
gtk_cell_renderer_progress_set_value (cellprogress,
|
|
|
|
g_value_get_int (value));
|
|
|
|
break;
|
|
|
|
case PROP_TEXT:
|
|
|
|
gtk_cell_renderer_progress_set_text (cellprogress,
|
|
|
|
g_value_get_string (value));
|
|
|
|
break;
|
2007-01-02 04:11:15 +00:00
|
|
|
case PROP_PULSE:
|
|
|
|
gtk_cell_renderer_progress_set_pulse (cellprogress,
|
|
|
|
g_value_get_int (value));
|
|
|
|
break;
|
2007-06-06 20:27:15 +00:00
|
|
|
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;
|
2007-06-13 12:41:24 +00:00
|
|
|
case PROP_ORIENTATION:
|
|
|
|
priv->orientation = g_value_get_enum (value);
|
|
|
|
break;
|
2010-09-02 03:27:00 +00:00
|
|
|
case PROP_INVERTED:
|
2011-10-24 17:50:00 +00:00
|
|
|
priv->inverted = g_value_get_boolean (value);
|
2010-09-02 03:27:00 +00:00
|
|
|
break;
|
2004-05-12 16:07:44 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
static void
|
|
|
|
recompute_label (GtkCellRendererProgress *cellprogress)
|
|
|
|
{
|
|
|
|
GtkCellRendererProgressPrivate *priv = cellprogress->priv;
|
|
|
|
gchar *label;
|
|
|
|
|
|
|
|
if (priv->text)
|
|
|
|
label = g_strdup (priv->text);
|
|
|
|
else if (priv->pulse < 0)
|
2008-10-27 01:42:28 +00:00
|
|
|
label = g_strdup_printf (C_("progress bar label", "%d %%"), priv->value);
|
2007-01-02 04:11:15 +00:00
|
|
|
else
|
|
|
|
label = NULL;
|
|
|
|
|
|
|
|
g_free (priv->label);
|
|
|
|
priv->label = label;
|
|
|
|
}
|
|
|
|
|
2004-05-12 16:07:44 +00:00
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_set_value (GtkCellRendererProgress *cellprogress,
|
|
|
|
gint value)
|
|
|
|
{
|
|
|
|
cellprogress->priv->value = value;
|
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
recompute_label (cellprogress);
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_set_text (GtkCellRendererProgress *cellprogress,
|
2004-05-17 02:33:18 +00:00
|
|
|
const gchar *text)
|
2004-05-12 16:07:44 +00:00
|
|
|
{
|
|
|
|
gchar *new_text;
|
|
|
|
|
|
|
|
new_text = g_strdup (text);
|
|
|
|
g_free (cellprogress->priv->text);
|
|
|
|
cellprogress->priv->text = new_text;
|
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
recompute_label (cellprogress);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gtk_cell_renderer_progress_set_pulse (GtkCellRendererProgress *cellprogress,
|
|
|
|
gint pulse)
|
|
|
|
{
|
|
|
|
GtkCellRendererProgressPrivate *priv = cellprogress->priv;
|
|
|
|
|
|
|
|
if (pulse != priv->pulse)
|
|
|
|
{
|
2008-12-13 08:18:50 +00:00
|
|
|
if (pulse <= 0)
|
2007-01-02 04:11:15 +00:00
|
|
|
priv->offset = 0;
|
|
|
|
else
|
2008-12-13 08:18:50 +00:00
|
|
|
priv->offset = pulse;
|
2007-01-02 04:11:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
priv->pulse = pulse;
|
|
|
|
|
|
|
|
recompute_label (cellprogress);
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2004-05-17 02:33:18 +00:00
|
|
|
compute_dimensions (GtkCellRenderer *cell,
|
|
|
|
GtkWidget *widget,
|
|
|
|
const gchar *text,
|
|
|
|
gint *width,
|
|
|
|
gint *height)
|
2004-05-12 16:07:44 +00:00
|
|
|
{
|
|
|
|
PangoRectangle logical_rect;
|
|
|
|
PangoLayout *layout;
|
2010-05-30 20:38:33 +00:00
|
|
|
gint xpad, ypad;
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
layout = gtk_widget_create_pango_layout (widget, text);
|
|
|
|
pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
|
2010-05-30 20:38:33 +00:00
|
|
|
|
|
|
|
gtk_cell_renderer_get_padding (cell, &xpad, &ypad);
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
if (width)
|
2010-05-30 20:38:33 +00:00
|
|
|
*width = logical_rect.width + xpad * 2;
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
if (height)
|
2010-05-30 20:38:33 +00:00
|
|
|
*height = logical_rect.height + ypad * 2;
|
2006-12-07 15:56:03 +00:00
|
|
|
|
2004-11-19 23:18:38 +00:00
|
|
|
g_object_unref (layout);
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2010-10-04 14:22:09 +00:00
|
|
|
gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell,
|
|
|
|
GtkWidget *widget,
|
|
|
|
const GdkRectangle *cell_area,
|
|
|
|
gint *x_offset,
|
|
|
|
gint *y_offset,
|
|
|
|
gint *width,
|
|
|
|
gint *height)
|
2004-05-12 16:07:44 +00:00
|
|
|
{
|
|
|
|
GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
|
2007-01-02 04:11:15 +00:00
|
|
|
GtkCellRendererProgressPrivate *priv = cellprogress->priv;
|
2004-05-12 16:07:44 +00:00
|
|
|
gint w, h;
|
2004-05-17 02:33:18 +00:00
|
|
|
gchar *text;
|
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
if (priv->min_w < 0)
|
2004-05-17 02:33:18 +00:00
|
|
|
{
|
2008-10-27 01:42:28 +00:00
|
|
|
text = g_strdup_printf (C_("progress bar label", "%d %%"), 100);
|
2004-05-17 02:33:18 +00:00
|
|
|
compute_dimensions (cell, widget, text,
|
2007-01-02 04:11:15 +00:00
|
|
|
&priv->min_w,
|
|
|
|
&priv->min_h);
|
2004-05-17 02:33:18 +00:00
|
|
|
g_free (text);
|
|
|
|
}
|
2004-05-12 16:07:44 +00:00
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
compute_dimensions (cell, widget, priv->label, &w, &h);
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
if (width)
|
2007-01-02 04:11:15 +00:00
|
|
|
*width = MAX (priv->min_w, w);
|
2004-05-12 16:07:44 +00:00
|
|
|
|
|
|
|
if (height)
|
2007-01-02 04:11:15 +00:00
|
|
|
*height = MIN (priv->min_h, h);
|
2005-06-19 12:44:12 +00:00
|
|
|
|
|
|
|
/* FIXME: at the moment cell_area is only set when we are requesting
|
|
|
|
* the size for drawing the focus rectangle. We now just return
|
|
|
|
* the last size we used for drawing the progress bar, which will
|
|
|
|
* work for now. Not a really nice solution though.
|
|
|
|
*/
|
|
|
|
if (cell_area)
|
|
|
|
{
|
|
|
|
if (width)
|
|
|
|
*width = cell_area->width;
|
|
|
|
if (height)
|
|
|
|
*height = cell_area->height;
|
|
|
|
}
|
2006-12-03 20:02:04 +00:00
|
|
|
|
|
|
|
if (x_offset) *x_offset = 0;
|
|
|
|
if (y_offset) *y_offset = 0;
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 12:41:24 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-05-12 16:07:44 +00:00
|
|
|
static void
|
2010-08-21 11:18:14 +00:00
|
|
|
gtk_cell_renderer_progress_render (GtkCellRenderer *cell,
|
|
|
|
cairo_t *cr,
|
|
|
|
GtkWidget *widget,
|
|
|
|
const GdkRectangle *background_area,
|
|
|
|
const GdkRectangle *cell_area,
|
|
|
|
GtkCellRendererState flags)
|
2004-05-12 16:07:44 +00:00
|
|
|
{
|
|
|
|
GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (cell);
|
2011-01-12 20:28:44 +00:00
|
|
|
GtkCellRendererProgressPrivate *priv= cellprogress->priv;
|
|
|
|
GtkStyleContext *context;
|
2011-05-06 18:33:00 +00:00
|
|
|
GtkBorder padding;
|
2004-05-12 16:07:44 +00:00
|
|
|
PangoLayout *layout;
|
|
|
|
PangoRectangle logical_rect;
|
2007-06-13 12:41:24 +00:00
|
|
|
gint x, y, w, h, x_pos, y_pos, bar_position, bar_size, start, full_size;
|
2010-05-30 20:38:33 +00:00
|
|
|
gint xpad, ypad;
|
2004-05-12 16:07:44 +00:00
|
|
|
GdkRectangle clip;
|
|
|
|
gboolean is_rtl;
|
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
context = gtk_widget_get_style_context (widget);
|
2004-05-12 16:07:44 +00:00
|
|
|
is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
|
2010-05-30 20:38:33 +00:00
|
|
|
|
|
|
|
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;
|
2005-05-11 19:16:19 +00:00
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
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);
|
|
|
|
|
2011-05-06 18:33:00 +00:00
|
|
|
x += padding.left;
|
|
|
|
y += padding.top;
|
|
|
|
w -= padding.left + padding.right;
|
|
|
|
h -= padding.top + padding.bottom;
|
2011-01-12 20:28:44 +00:00
|
|
|
|
|
|
|
gtk_style_context_restore (context);
|
2006-12-06 21:40:02 +00:00
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
|
2007-01-02 04:11:15 +00:00
|
|
|
{
|
2007-06-13 12:41:24 +00:00
|
|
|
clip.y = y;
|
|
|
|
clip.height = h;
|
|
|
|
|
|
|
|
start = x;
|
|
|
|
full_size = w;
|
|
|
|
|
|
|
|
bar_size = get_bar_size (priv->pulse, priv->value, full_size);
|
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
if (!priv->inverted)
|
2007-06-13 12:41:24 +00:00
|
|
|
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;
|
2007-01-02 04:11:15 +00:00
|
|
|
}
|
2007-06-13 12:41:24 +00:00
|
|
|
else
|
2007-01-02 04:11:15 +00:00
|
|
|
{
|
|
|
|
clip.x = x;
|
|
|
|
clip.width = w;
|
2007-06-13 12:41:24 +00:00
|
|
|
|
|
|
|
start = y;
|
|
|
|
full_size = h;
|
|
|
|
|
|
|
|
bar_size = get_bar_size (priv->pulse, priv->value, full_size);
|
|
|
|
|
2010-09-02 03:27:00 +00:00
|
|
|
if (priv->inverted)
|
2007-06-13 12:41:24 +00:00
|
|
|
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;
|
2007-01-02 04:11:15 +00:00
|
|
|
}
|
2006-12-06 21:40:02 +00:00
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_style_context_save (context);
|
|
|
|
gtk_style_context_add_class (context, GTK_STYLE_CLASS_PROGRESSBAR);
|
|
|
|
|
2010-12-05 12:56:24 +00:00
|
|
|
if (bar_size > 0)
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_render_activity (context, cr,
|
|
|
|
clip.x, clip.y,
|
|
|
|
clip.width, clip.height);
|
|
|
|
|
|
|
|
gtk_style_context_restore (context);
|
2006-12-06 21:40:02 +00:00
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
if (priv->label)
|
|
|
|
{
|
2007-06-06 20:27:15 +00:00
|
|
|
gfloat text_xalign;
|
|
|
|
|
2007-01-02 04:11:15 +00:00
|
|
|
layout = gtk_widget_create_pango_layout (widget, priv->label);
|
|
|
|
pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
|
2007-06-06 20:27:15 +00:00
|
|
|
|
|
|
|
if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
|
|
|
|
text_xalign = 1.0 - priv->text_xalign;
|
|
|
|
else
|
|
|
|
text_xalign = priv->text_xalign;
|
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
x_pos = x + padding.left + text_xalign *
|
|
|
|
(w - padding.left - padding.right - logical_rect.width);
|
2007-06-06 20:27:15 +00:00
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
y_pos = y + padding.top + priv->text_yalign *
|
|
|
|
(h - padding.top - padding.bottom - logical_rect.height);
|
2010-08-11 21:16:40 +00:00
|
|
|
|
2010-08-21 11:18:14 +00:00
|
|
|
cairo_save (cr);
|
|
|
|
gdk_cairo_rectangle (cr, &clip);
|
|
|
|
cairo_clip (cr);
|
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
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);
|
2010-08-21 11:18:14 +00:00
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_style_context_restore (context);
|
2010-08-21 11:18:14 +00:00
|
|
|
cairo_restore (cr);
|
2007-01-02 04:11:15 +00:00
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_style_context_save (context);
|
|
|
|
gtk_style_context_add_class (context, GTK_STYLE_CLASS_TROUGH);
|
|
|
|
|
2007-06-13 12:41:24 +00:00
|
|
|
if (bar_position > start)
|
2007-01-02 04:11:15 +00:00
|
|
|
{
|
2010-09-02 03:27:00 +00:00
|
|
|
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
|
2007-06-13 12:41:24 +00:00
|
|
|
{
|
|
|
|
clip.x = x;
|
|
|
|
clip.width = bar_position - x;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
clip.y = y;
|
|
|
|
clip.height = bar_position - y;
|
|
|
|
}
|
2007-01-02 04:11:15 +00:00
|
|
|
|
2010-08-21 11:18:14 +00:00
|
|
|
cairo_save (cr);
|
|
|
|
gdk_cairo_rectangle (cr, &clip);
|
|
|
|
cairo_clip (cr);
|
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_render_layout (context, cr,
|
|
|
|
x_pos, y_pos,
|
|
|
|
layout);
|
2010-08-21 11:18:14 +00:00
|
|
|
|
|
|
|
cairo_restore (cr);
|
2007-01-02 04:11:15 +00:00
|
|
|
}
|
|
|
|
|
2007-06-13 12:41:24 +00:00
|
|
|
if (bar_position + bar_size < start + full_size)
|
2007-01-02 04:11:15 +00:00
|
|
|
{
|
2010-09-02 03:27:00 +00:00
|
|
|
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
|
2007-06-13 12:41:24 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2007-01-02 04:11:15 +00:00
|
|
|
|
2010-08-21 11:18:14 +00:00
|
|
|
cairo_save (cr);
|
|
|
|
gdk_cairo_rectangle (cr, &clip);
|
|
|
|
cairo_clip (cr);
|
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_render_layout (context, cr,
|
|
|
|
x_pos, y_pos,
|
|
|
|
layout);
|
|
|
|
|
2010-08-21 11:18:14 +00:00
|
|
|
cairo_restore (cr);
|
2007-01-02 04:11:15 +00:00
|
|
|
}
|
|
|
|
|
2011-01-12 20:28:44 +00:00
|
|
|
gtk_style_context_restore (context);
|
2007-01-02 04:11:15 +00:00
|
|
|
g_object_unref (layout);
|
|
|
|
}
|
2004-05-12 16:07:44 +00:00
|
|
|
}
|