| 1 /* |
|
| 2 * @file gtkcellrendererprogress.c GTK+ Cell Renderer Progress |
|
| 3 * @ingroup pidgin |
|
| 4 */ |
|
| 5 |
|
| 6 /* pidgin |
|
| 7 * |
|
| 8 * Pidgin is the legal property of its developers, whose names are too numerous |
|
| 9 * to list here. Please refer to the COPYRIGHT file distributed with this |
|
| 10 * source distribution. |
|
| 11 * |
|
| 12 * This program is free software; you can redistribute it and/or modify |
|
| 13 * it under the terms of the GNU General Public License as published by |
|
| 14 * the Free Software Foundation; either version 2 of the License, or |
|
| 15 * (at your option) any later version. |
|
| 16 * |
|
| 17 * This program is distributed in the hope that it will be useful, |
|
| 18 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
| 20 * GNU General Public License for more details. |
|
| 21 * |
|
| 22 * You should have received a copy of the GNU General Public License |
|
| 23 * along with this program; if not, write to the Free Software |
|
| 24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA |
|
| 25 * |
|
| 26 */ |
|
| 27 |
|
| 28 /* This is taken largely from GtkCellRenderer[Text|Pixbuf|Toggle] by |
|
| 29 * Jonathon Blandford <jrb@redhat.com> for RedHat, Inc. |
|
| 30 */ |
|
| 31 |
|
| 32 #include "gtkcellrendererprogress.h" |
|
| 33 |
|
| 34 static void pidgin_cell_renderer_progress_get_property (GObject *object, |
|
| 35 guint param_id, |
|
| 36 GValue *value, |
|
| 37 GParamSpec *pspec); |
|
| 38 static void pidgin_cell_renderer_progress_set_property (GObject *object, |
|
| 39 guint param_id, |
|
| 40 const GValue *value, |
|
| 41 GParamSpec *pspec); |
|
| 42 static void pidgin_cell_renderer_progress_init (PidginCellRendererProgress *cellprogress); |
|
| 43 static void pidgin_cell_renderer_progress_class_init (PidginCellRendererProgressClass *class); |
|
| 44 static void pidgin_cell_renderer_progress_get_size (GtkCellRenderer *cell, |
|
| 45 GtkWidget *widget, |
|
| 46 GdkRectangle *cell_area, |
|
| 47 gint *x_offset, |
|
| 48 gint *y_offset, |
|
| 49 gint *width, |
|
| 50 gint *height); |
|
| 51 static void pidgin_cell_renderer_progress_render (GtkCellRenderer *cell, |
|
| 52 GdkWindow *window, |
|
| 53 GtkWidget *widget, |
|
| 54 GdkRectangle *background_area, |
|
| 55 GdkRectangle *cell_area, |
|
| 56 GdkRectangle *expose_area, |
|
| 57 guint flags); |
|
| 58 #if 0 |
|
| 59 static gboolean pidgin_cell_renderer_progress_activate (GtkCellRenderer *cell, |
|
| 60 GdkEvent *event, |
|
| 61 GtkWidget *widget, |
|
| 62 const gchar *path, |
|
| 63 GdkRectangle *background_area, |
|
| 64 GdkRectangle *cell_area, |
|
| 65 guint flags); |
|
| 66 #endif |
|
| 67 static void pidgin_cell_renderer_progress_finalize (GObject *gobject); |
|
| 68 |
|
| 69 enum { |
|
| 70 LAST_SIGNAL |
|
| 71 }; |
|
| 72 |
|
| 73 enum { |
|
| 74 PROP_0, |
|
| 75 PROP_PERCENTAGE, |
|
| 76 PROP_TEXT, |
|
| 77 PROP_SHOW_TEXT |
|
| 78 }; |
|
| 79 |
|
| 80 static gpointer parent_class; |
|
| 81 /* static guint progress_cell_renderer_signals [LAST_SIGNAL]; */ |
|
| 82 |
|
| 83 GType pidgin_cell_renderer_progress_get_type (void) |
|
| 84 { |
|
| 85 static GType cell_progress_type = 0; |
|
| 86 |
|
| 87 if (!cell_progress_type) |
|
| 88 { |
|
| 89 static const GTypeInfo cell_progress_info = |
|
| 90 { |
|
| 91 sizeof (PidginCellRendererProgressClass), |
|
| 92 NULL, /* base_init */ |
|
| 93 NULL, /* base_finalize */ |
|
| 94 (GClassInitFunc) pidgin_cell_renderer_progress_class_init, |
|
| 95 NULL, /* class_finalize */ |
|
| 96 NULL, /* class_data */ |
|
| 97 sizeof (PidginCellRendererProgress), |
|
| 98 0, /* n_preallocs */ |
|
| 99 (GInstanceInitFunc) pidgin_cell_renderer_progress_init, |
|
| 100 NULL /* value_table */ |
|
| 101 }; |
|
| 102 |
|
| 103 cell_progress_type = |
|
| 104 g_type_register_static (GTK_TYPE_CELL_RENDERER, |
|
| 105 "PidginCellRendererProgress", |
|
| 106 &cell_progress_info, 0); |
|
| 107 } |
|
| 108 |
|
| 109 return cell_progress_type; |
|
| 110 } |
|
| 111 |
|
| 112 static void pidgin_cell_renderer_progress_init (PidginCellRendererProgress *cellprogress) |
|
| 113 { |
|
| 114 GTK_CELL_RENDERER(cellprogress)->mode = GTK_CELL_RENDERER_MODE_INERT; |
|
| 115 GTK_CELL_RENDERER(cellprogress)->xpad = 2; |
|
| 116 GTK_CELL_RENDERER(cellprogress)->ypad = 2; |
|
| 117 } |
|
| 118 |
|
| 119 static void pidgin_cell_renderer_progress_class_init (PidginCellRendererProgressClass *class) |
|
| 120 { |
|
| 121 GObjectClass *object_class = G_OBJECT_CLASS(class); |
|
| 122 GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class); |
|
| 123 |
|
| 124 parent_class = g_type_class_peek_parent (class); |
|
| 125 object_class->finalize = pidgin_cell_renderer_progress_finalize; |
|
| 126 |
|
| 127 object_class->get_property = pidgin_cell_renderer_progress_get_property; |
|
| 128 object_class->set_property = pidgin_cell_renderer_progress_set_property; |
|
| 129 |
|
| 130 cell_class->get_size = pidgin_cell_renderer_progress_get_size; |
|
| 131 cell_class->render = pidgin_cell_renderer_progress_render; |
|
| 132 |
|
| 133 g_object_class_install_property (object_class, |
|
| 134 PROP_PERCENTAGE, |
|
| 135 g_param_spec_double ("percentage", |
|
| 136 "Percentage", |
|
| 137 "The fractional progress to display", |
|
| 138 0, 1, 0, |
|
| 139 G_PARAM_READWRITE)); |
|
| 140 g_object_class_install_property (object_class, |
|
| 141 PROP_TEXT, |
|
| 142 g_param_spec_string ("text", |
|
| 143 "Text", |
|
| 144 "Text to overlay over progress bar", |
|
| 145 NULL, |
|
| 146 G_PARAM_READWRITE)); |
|
| 147 g_object_class_install_property(object_class, |
|
| 148 PROP_SHOW_TEXT, |
|
| 149 g_param_spec_string("text_set", |
|
| 150 "Text set", |
|
| 151 "Whether to overlay text on the progress bar", |
|
| 152 FALSE, |
|
| 153 G_PARAM_READABLE | G_PARAM_WRITABLE)); |
|
| 154 } |
|
| 155 |
|
| 156 static void pidgin_cell_renderer_progress_finalize (GObject *object) |
|
| 157 { |
|
| 158 /* |
|
| 159 PidginCellRendererProgress *cellprogress = PIDGIN_CELL_RENDERER_PROGRESS(object); |
|
| 160 */ |
|
| 161 |
|
| 162 (* G_OBJECT_CLASS (parent_class)->finalize) (object); |
|
| 163 } |
|
| 164 |
|
| 165 static void pidgin_cell_renderer_progress_get_property (GObject *object, |
|
| 166 guint param_id, |
|
| 167 GValue *value, |
|
| 168 GParamSpec *psec) |
|
| 169 { |
|
| 170 PidginCellRendererProgress *cellprogress = PIDGIN_CELL_RENDERER_PROGRESS(object); |
|
| 171 |
|
| 172 switch (param_id) |
|
| 173 { |
|
| 174 case PROP_PERCENTAGE: |
|
| 175 g_value_set_double(value, cellprogress->progress); |
|
| 176 break; |
|
| 177 case PROP_TEXT: |
|
| 178 g_value_set_string(value, cellprogress->text); |
|
| 179 break; |
|
| 180 case PROP_SHOW_TEXT: |
|
| 181 g_value_set_boolean(value, cellprogress->text_set); |
|
| 182 break; |
|
| 183 default: |
|
| 184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec); |
|
| 185 break; |
|
| 186 } |
|
| 187 } |
|
| 188 |
|
| 189 static void pidgin_cell_renderer_progress_set_property (GObject *object, |
|
| 190 guint param_id, |
|
| 191 const GValue *value, |
|
| 192 GParamSpec *pspec) |
|
| 193 { |
|
| 194 PidginCellRendererProgress *cellprogress = PIDGIN_CELL_RENDERER_PROGRESS (object); |
|
| 195 |
|
| 196 switch (param_id) |
|
| 197 { |
|
| 198 case PROP_PERCENTAGE: |
|
| 199 cellprogress->progress = g_value_get_double(value); |
|
| 200 break; |
|
| 201 case PROP_TEXT: |
|
| 202 if (cellprogress->text) |
|
| 203 g_free(cellprogress->text); |
|
| 204 cellprogress->text = g_strdup(g_value_get_string(value)); |
|
| 205 g_object_notify(object, "text"); |
|
| 206 break; |
|
| 207 case PROP_SHOW_TEXT: |
|
| 208 cellprogress->text_set = g_value_get_boolean(value); |
|
| 209 break; |
|
| 210 default: |
|
| 211 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec); |
|
| 212 break; |
|
| 213 } |
|
| 214 } |
|
| 215 |
|
| 216 GtkCellRenderer *pidgin_cell_renderer_progress_new(void) |
|
| 217 { |
|
| 218 return g_object_new(PIDGIN_TYPE_GTK_CELL_RENDERER_PROGRESS, NULL); |
|
| 219 } |
|
| 220 |
|
| 221 static void pidgin_cell_renderer_progress_get_size (GtkCellRenderer *cell, |
|
| 222 GtkWidget *widget, |
|
| 223 GdkRectangle *cell_area, |
|
| 224 gint *x_offset, |
|
| 225 gint *y_offset, |
|
| 226 gint *width, |
|
| 227 gint *height) |
|
| 228 { |
|
| 229 gint calc_width; |
|
| 230 gint calc_height; |
|
| 231 |
|
| 232 calc_width = (gint) cell->xpad * 2 + 50; |
|
| 233 calc_height = (gint) cell->ypad * 2 + 12; |
|
| 234 |
|
| 235 if (width) |
|
| 236 *width = calc_width; |
|
| 237 |
|
| 238 if (height) |
|
| 239 *height = calc_height; |
|
| 240 |
|
| 241 if (cell_area) |
|
| 242 { |
|
| 243 if (x_offset) |
|
| 244 { |
|
| 245 *x_offset = cell->xalign * (cell_area->width - calc_width); |
|
| 246 *x_offset = MAX (*x_offset, 0); |
|
| 247 } |
|
| 248 if (y_offset) |
|
| 249 { |
|
| 250 *y_offset = cell->yalign * (cell_area->height - calc_height); |
|
| 251 *y_offset = MAX (*y_offset, 0); |
|
| 252 } |
|
| 253 } |
|
| 254 } |
|
| 255 |
|
| 256 |
|
| 257 static void pidgin_cell_renderer_progress_render (GtkCellRenderer *cell, |
|
| 258 GdkWindow *window, |
|
| 259 GtkWidget *widget, |
|
| 260 GdkRectangle *background_area, |
|
| 261 GdkRectangle *cell_area, |
|
| 262 GdkRectangle *expose_area, |
|
| 263 guint flags) |
|
| 264 { |
|
| 265 PidginCellRendererProgress *cellprogress = (PidginCellRendererProgress *) cell; |
|
| 266 |
|
| 267 gint width, height; |
|
| 268 GtkStateType state; |
|
| 269 |
|
| 270 width = cell_area->width; |
|
| 271 height = cell_area->height; |
|
| 272 |
|
| 273 if (GTK_WIDGET_HAS_FOCUS (widget)) |
|
| 274 state = GTK_STATE_ACTIVE; |
|
| 275 else |
|
| 276 state = GTK_STATE_NORMAL; |
|
| 277 |
|
| 278 width -= cell->xpad*2; |
|
| 279 height -= cell->ypad*2; |
|
| 280 |
|
| 281 gtk_paint_box (widget->style, |
|
| 282 window, |
|
| 283 GTK_STATE_NORMAL, GTK_SHADOW_IN, |
|
| 284 NULL, widget, "trough", |
|
| 285 cell_area->x + cell->xpad, |
|
| 286 cell_area->y + cell->ypad, |
|
| 287 width - 1, height - 1); |
|
| 288 gtk_paint_box (widget->style, |
|
| 289 window, |
|
| 290 state, GTK_SHADOW_OUT, |
|
| 291 NULL, widget, "bar", |
|
| 292 cell_area->x + cell->xpad + 1, |
|
| 293 cell_area->y + cell->ypad + 1, |
|
| 294 (width - 3) * cellprogress->progress, |
|
| 295 height - 3); |
|
| 296 } |
|