1 /* gtkcellrendererprogress.c
2 * Copyright (C) 2002, Sean Egan <bj91704@binghamton.edu>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 /* This is taken largely from GtkCellRenderer[Text|Pixbuf|Toggle] by
21 * Jonathon Blandford <jrb@redhat.com> for RedHat, Inc.
24 #include "gtkcellrendererprogress.h"
26 static void gtk_cell_renderer_progress_get_property (GObject
*object
,
30 static void gtk_cell_renderer_progress_set_property (GObject
*object
,
34 static void gtk_cell_renderer_progress_init (GtkCellRendererProgress
*cellprogress
);
35 static void gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass
*class);
36 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer
*cell
,
38 GdkRectangle
*cell_area
,
43 static void gtk_cell_renderer_progress_render (GtkCellRenderer
*cell
,
46 GdkRectangle
*background_area
,
47 GdkRectangle
*cell_area
,
48 GdkRectangle
*expose_area
,
50 static gboolean
gtk_cell_renderer_progress_activate (GtkCellRenderer
*cell
,
54 GdkRectangle
*background_area
,
55 GdkRectangle
*cell_area
,
57 static void gtk_cell_renderer_progress_finalize (GObject
*gobject
);
71 static gpointer parent_class
;
72 static guint progress_cell_renderer_signals
[LAST_SIGNAL
];
74 GType
gtk_cell_renderer_progress_get_type (void)
76 static GType cell_progress_type
= 0;
78 if (!cell_progress_type
)
80 static const GTypeInfo cell_progress_info
=
82 sizeof (GtkCellRendererProgressClass
),
84 NULL
, /* base_finalize */
85 (GClassInitFunc
) gtk_cell_renderer_progress_class_init
,
86 NULL
, /* class_finalize */
87 NULL
, /* class_data */
88 sizeof (GtkCellRendererProgress
),
90 (GInstanceInitFunc
) gtk_cell_renderer_progress_init
,
94 g_type_register_static (GTK_TYPE_CELL_RENDERER
, "GtkCellRendererProgress",
95 &cell_progress_info
, 0);
98 return cell_progress_type
;
102 static void gtk_cell_renderer_progress_init (GtkCellRendererProgress
*cellprogress
)
104 GTK_CELL_RENDERER(cellprogress
)->mode
= GTK_CELL_RENDERER_MODE_INERT
;
105 GTK_CELL_RENDERER(cellprogress
)->xpad
= 2;
106 GTK_CELL_RENDERER(cellprogress
)->ypad
= 2;
109 static void gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass
*class)
111 GObjectClass
*object_class
= G_OBJECT_CLASS(class);
112 GtkCellRendererClass
*cell_class
= GTK_CELL_RENDERER_CLASS(class);
114 parent_class
= g_type_class_peek_parent (class);
115 object_class
->finalize
= gtk_cell_renderer_progress_finalize
;
117 object_class
->get_property
= gtk_cell_renderer_progress_get_property
;
118 object_class
->set_property
= gtk_cell_renderer_progress_set_property
;
120 cell_class
->get_size
= gtk_cell_renderer_progress_get_size
;
121 cell_class
->render
= gtk_cell_renderer_progress_render
;
123 g_object_class_install_property (object_class
,
125 g_param_spec_double ("percentage",
127 "The fractional progress to display",
131 g_object_class_install_property (object_class
,
133 g_param_spec_string ("text",
135 "Text to overlay over progress bar",
138 g_object_class_install_property(object_class
,
140 g_param_spec_string("text_set",
142 "Whether to overlay text on the progress bar",
144 G_PARAM_READABLE
| G_PARAM_WRITABLE
));
149 static void gtk_cell_renderer_progress_finalize (GObject
*object
)
151 GtkCellRendererProgress
*cellprogress
= GTK_CELL_RENDERER_PROGRESS(object
);
153 (* G_OBJECT_CLASS (parent_class
)->finalize
) (object
);
156 static void gtk_cell_renderer_progress_get_property (GObject
*object
,
161 GtkCellRendererProgress
*cellprogress
= GTK_CELL_RENDERER_PROGRESS(object
);
165 case PROP_PERCENTAGE
:
166 g_value_set_double(value
, cellprogress
->progress
);
169 g_value_set_string(value
, cellprogress
->text
);
172 g_value_set_boolean(value
, cellprogress
->text_set
);
175 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, param_id
, psec
);
180 static void gtk_cell_renderer_progress_set_property (GObject
*object
,
185 GtkCellRendererProgress
*cellprogress
= GTK_CELL_RENDERER_PROGRESS (object
);
189 case PROP_PERCENTAGE
:
190 cellprogress
->progress
= g_value_get_double(value
);
193 if (cellprogress
->text
)
194 g_free(cellprogress
->text
);
195 cellprogress
->text
= g_strdup(g_value_get_string(value
));
196 g_object_notify(object
, "text");
199 cellprogress
->text_set
= g_value_get_boolean(value
);
202 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
, param_id
, pspec
);
207 GtkCellRenderer
*gtk_cell_renderer_progress_new(void)
209 return g_object_new(GTK_TYPE_CELL_RENDERER_PROGRESS
, NULL
);
212 static void gtk_cell_renderer_progress_get_size (GtkCellRenderer
*cell
,
214 GdkRectangle
*cell_area
,
223 calc_width
= (gint
) cell
->xpad
* 2 + 50;
224 calc_height
= (gint
) cell
->ypad
* 2 + 10;
230 *height
= calc_height
;
236 *x_offset
= cell
->xalign
* (cell_area
->width
- calc_width
);
237 *x_offset
= MAX (*x_offset
, 0);
241 *y_offset
= cell
->yalign
* (cell_area
->height
- calc_height
);
242 *y_offset
= MAX (*y_offset
, 0);
248 static void gtk_cell_renderer_progress_render (GtkCellRenderer
*cell
,
251 GdkRectangle
*background_area
,
252 GdkRectangle
*cell_area
,
253 GdkRectangle
*expose_area
,
256 GtkCellRendererProgress
*cellprogress
= (GtkCellRendererProgress
*) cell
;
259 gint x_offset
, y_offset
;
262 gtk_cell_renderer_progress_get_size (cell
, widget
, cell_area
,
263 &x_offset
, &y_offset
,
267 if (GTK_WIDGET_HAS_FOCUS (widget
))
268 state
= GTK_STATE_ACTIVE
;
270 state
= GTK_STATE_NORMAL
;
274 width
-= cell
->xpad
*2;
275 height
-= cell
->ypad
*2;
277 gtk_paint_box (widget
->style
,
279 GTK_STATE_NORMAL
, GTK_SHADOW_IN
,
280 NULL
, widget
, "trough",
281 cell_area
->x
+ x_offset
+ cell
->xpad
,
282 cell_area
->y
+ y_offset
+ cell
->ypad
,
283 width
- 1, height
- 1);
284 gtk_paint_box (widget
->style
,
286 state
, GTK_SHADOW_OUT
,
288 cell_area
->x
+ x_offset
+ cell
->xpad
+ 1,
289 cell_area
->y
+ y_offset
+ cell
->ypad
+ 1,
290 width
* cellprogress
->progress
,