Wed, 30 Oct 2002 16:20:23 +0000
[gaim-migrate @ 3981]
Here Rob.
| 3832 | 1 | /* gtkcellrendererprogress.c |
| 2 | * Copyright (C) 2002, Sean Egan <bj91704@binghamton.edu> | |
| 3 | * | |
| 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. | |
| 8 | * | |
| 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. | |
| 13 | * | |
| 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 | |
| 17 | * | |
| 18 | */ | |
| 19 | ||
| 20 | /* This is taken largely from GtkCellRenderer[Text|Pixbuf|Toggle] by | |
| 21 | * Jonathon Blandford <jrb@redhat.com> for RedHat, Inc. | |
| 22 | */ | |
| 23 | ||
| 24 | #include "gtkcellrendererprogress.h" | |
| 25 | ||
| 26 | static void gtk_cell_renderer_progress_get_property (GObject *object, | |
| 27 | guint param_id, | |
| 28 | GValue *value, | |
| 29 | GParamSpec *pspec); | |
| 30 | static void gtk_cell_renderer_progress_set_property (GObject *object, | |
| 31 | guint param_id, | |
| 32 | const GValue *value, | |
| 33 | GParamSpec *pspec); | |
| 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, | |
| 37 | GtkWidget *widget, | |
| 38 | GdkRectangle *cell_area, | |
| 39 | gint *x_offset, | |
| 40 | gint *y_offset, | |
| 41 | gint *width, | |
| 42 | gint *height); | |
| 43 | static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell, | |
| 44 | GdkWindow *window, | |
| 45 | GtkWidget *widget, | |
| 46 | GdkRectangle *background_area, | |
| 47 | GdkRectangle *cell_area, | |
| 48 | GdkRectangle *expose_area, | |
| 49 | guint flags); | |
| 50 | static gboolean gtk_cell_renderer_progress_activate (GtkCellRenderer *cell, | |
| 51 | GdkEvent *event, | |
| 52 | GtkWidget *widget, | |
| 53 | const gchar *path, | |
| 54 | GdkRectangle *background_area, | |
| 55 | GdkRectangle *cell_area, | |
| 56 | guint flags); | |
| 57 | static void gtk_cell_renderer_progress_finalize (GObject *gobject); | |
| 58 | ||
| 59 | enum { | |
| 60 | LAST_SIGNAL | |
| 61 | }; | |
| 62 | ||
| 63 | enum { | |
| 64 | PROP_0, | |
| 65 | ||
| 66 | PROP_PERCENTAGE, | |
| 67 | PROP_TEXT, | |
| 68 | PROP_SHOW_TEXT | |
| 69 | }; | |
| 70 | ||
| 71 | static gpointer parent_class; | |
| 72 | static guint progress_cell_renderer_signals [LAST_SIGNAL]; | |
| 73 | ||
| 74 | GType gtk_cell_renderer_progress_get_type (void) | |
| 75 | { | |
| 76 | static GType cell_progress_type = 0; | |
| 77 | ||
| 78 | if (!cell_progress_type) | |
| 79 | { | |
| 80 | static const GTypeInfo cell_progress_info = | |
| 81 | { | |
| 82 | sizeof (GtkCellRendererProgressClass), | |
| 83 | NULL, /* base_init */ | |
| 84 | NULL, /* base_finalize */ | |
| 85 | (GClassInitFunc) gtk_cell_renderer_progress_class_init, | |
| 86 | NULL, /* class_finalize */ | |
| 87 | NULL, /* class_data */ | |
| 88 | sizeof (GtkCellRendererProgress), | |
| 89 | 0, /* n_preallocs */ | |
| 90 | (GInstanceInitFunc) gtk_cell_renderer_progress_init, | |
| 91 | }; | |
| 92 | ||
| 93 | cell_progress_type = | |
| 94 | g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererProgress", | |
| 95 | &cell_progress_info, 0); | |
| 96 | } | |
| 97 | ||
| 98 | return cell_progress_type; | |
| 99 | } | |
| 100 | ||
| 101 | ||
| 102 | static void gtk_cell_renderer_progress_init (GtkCellRendererProgress *cellprogress) | |
| 103 | { | |
| 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; | |
| 107 | } | |
| 108 | ||
| 109 | static void gtk_cell_renderer_progress_class_init (GtkCellRendererProgressClass *class) | |
| 110 | { | |
| 111 | GObjectClass *object_class = G_OBJECT_CLASS(class); | |
| 112 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS(class); | |
| 113 | ||
| 114 | parent_class = g_type_class_peek_parent (class); | |
| 115 | object_class->finalize = gtk_cell_renderer_progress_finalize; | |
| 116 | ||
| 117 | object_class->get_property = gtk_cell_renderer_progress_get_property; | |
| 118 | object_class->set_property = gtk_cell_renderer_progress_set_property; | |
| 119 | ||
| 120 | cell_class->get_size = gtk_cell_renderer_progress_get_size; | |
| 121 | cell_class->render = gtk_cell_renderer_progress_render; | |
| 122 | ||
| 123 | g_object_class_install_property (object_class, | |
| 124 | PROP_PERCENTAGE, | |
| 125 | g_param_spec_double ("percentage", | |
| 126 | "Percentage", | |
| 127 | "The fractional progress to display", | |
| 128 | 0, 1, 0, | |
| 129 | G_PARAM_READWRITE)); | |
| 130 | ||
| 131 | g_object_class_install_property (object_class, | |
| 132 | PROP_TEXT, | |
| 133 | g_param_spec_string ("text", | |
| 134 | "Text", | |
| 135 | "Text to overlay over progress bar", | |
| 136 | NULL, | |
| 137 | G_PARAM_READWRITE)); | |
| 138 | g_object_class_install_property(object_class, | |
| 139 | PROP_SHOW_TEXT, | |
| 140 | g_param_spec_string("text_set", | |
| 141 | "Text set", | |
| 142 | "Whether to overlay text on the progress bar", | |
| 143 | FALSE, | |
| 144 | G_PARAM_READABLE | G_PARAM_WRITABLE)); | |
| 145 | } | |
| 146 | ||
| 147 | ||
| 148 | ||
| 149 | static void gtk_cell_renderer_progress_finalize (GObject *object) | |
| 150 | { | |
| 151 | GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS(object); | |
| 152 | ||
| 153 | (* G_OBJECT_CLASS (parent_class)->finalize) (object); | |
| 154 | } | |
| 155 | ||
| 156 | static void gtk_cell_renderer_progress_get_property (GObject *object, | |
| 157 | guint param_id, | |
| 158 | GValue *value, | |
| 159 | GParamSpec *psec) | |
| 160 | { | |
| 161 | GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS(object); | |
| 162 | ||
| 163 | switch (param_id) | |
| 164 | { | |
| 165 | case PROP_PERCENTAGE: | |
| 166 | g_value_set_double(value, cellprogress->progress); | |
| 167 | break; | |
| 168 | case PROP_TEXT: | |
| 169 | g_value_set_string(value, cellprogress->text); | |
| 170 | break; | |
| 171 | case PROP_SHOW_TEXT: | |
| 172 | g_value_set_boolean(value, cellprogress->text_set); | |
| 173 | break; | |
| 174 | default: | |
| 175 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, psec); | |
| 176 | break; | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 180 | static void gtk_cell_renderer_progress_set_property (GObject *object, | |
| 181 | guint param_id, | |
| 182 | const GValue *value, | |
| 183 | GParamSpec *pspec) | |
| 184 | { | |
| 185 | GtkCellRendererProgress *cellprogress = GTK_CELL_RENDERER_PROGRESS (object); | |
| 186 | ||
| 187 | switch (param_id) | |
| 188 | { | |
| 189 | case PROP_PERCENTAGE: | |
| 190 | cellprogress->progress = g_value_get_double(value); | |
| 191 | break; | |
| 192 | case PROP_TEXT: | |
| 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"); | |
| 197 | break; | |
| 198 | case PROP_SHOW_TEXT: | |
| 199 | cellprogress->text_set = g_value_get_boolean(value); | |
| 200 | break; | |
| 201 | default: | |
| 202 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, param_id, pspec); | |
| 203 | break; | |
| 204 | } | |
| 205 | } | |
| 206 | ||
| 207 | GtkCellRenderer *gtk_cell_renderer_progress_new(void) | |
| 208 | { | |
| 209 | return g_object_new(GTK_TYPE_CELL_RENDERER_PROGRESS, NULL); | |
| 210 | } | |
| 211 | ||
| 212 | static void gtk_cell_renderer_progress_get_size (GtkCellRenderer *cell, | |
| 213 | GtkWidget *widget, | |
| 214 | GdkRectangle *cell_area, | |
| 215 | gint *x_offset, | |
| 216 | gint *y_offset, | |
| 217 | gint *width, | |
| 218 | gint *height) | |
| 219 | { | |
| 220 | gint calc_width; | |
| 221 | gint calc_height; | |
| 222 | ||
| 223 | calc_width = (gint) cell->xpad * 2 + 50; | |
| 224 | calc_height = (gint) cell->ypad * 2 + 10; | |
| 225 | ||
| 226 | if (width) | |
| 227 | *width = calc_width; | |
| 228 | ||
| 229 | if (height) | |
| 230 | *height = calc_height; | |
| 231 | ||
| 232 | if (cell_area) | |
| 233 | { | |
| 234 | if (x_offset) | |
| 235 | { | |
| 236 | *x_offset = cell->xalign * (cell_area->width - calc_width); | |
| 237 | *x_offset = MAX (*x_offset, 0); | |
| 238 | } | |
| 239 | if (y_offset) | |
| 240 | { | |
| 241 | *y_offset = cell->yalign * (cell_area->height - calc_height); | |
| 242 | *y_offset = MAX (*y_offset, 0); | |
| 243 | } | |
| 244 | } | |
| 245 | } | |
| 246 | ||
| 247 | ||
| 248 | static void gtk_cell_renderer_progress_render (GtkCellRenderer *cell, | |
| 249 | GdkWindow *window, | |
| 250 | GtkWidget *widget, | |
| 251 | GdkRectangle *background_area, | |
| 252 | GdkRectangle *cell_area, | |
| 253 | GdkRectangle *expose_area, | |
| 254 | guint flags) | |
| 255 | { | |
| 256 | GtkCellRendererProgress *cellprogress = (GtkCellRendererProgress *) cell; | |
| 257 | ||
| 258 | gint width, height; | |
| 259 | gint x_offset, y_offset; | |
| 260 | GtkStateType state; | |
| 261 | ||
| 262 | gtk_cell_renderer_progress_get_size (cell, widget, cell_area, | |
| 263 | &x_offset, &y_offset, | |
| 264 | &width, &height); | |
| 265 | ||
| 266 | ||
| 267 | if (GTK_WIDGET_HAS_FOCUS (widget)) | |
| 268 | state = GTK_STATE_ACTIVE; | |
| 269 | else | |
| 270 | state = GTK_STATE_NORMAL; | |
| 271 | ||
| 272 | ||
| 273 | ||
| 274 | width -= cell->xpad*2; | |
| 275 | height -= cell->ypad*2; | |
| 276 | ||
| 277 | gtk_paint_box (widget->style, | |
| 278 | window, | |
| 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, | |
| 285 | window, | |
| 286 | state, GTK_SHADOW_OUT, | |
| 287 | NULL, widget, "bar", | |
| 288 | cell_area->x + x_offset + cell->xpad + 1, | |
| 289 | cell_area->y + y_offset + cell->ypad + 1, | |
| 290 | width * cellprogress->progress, | |
| 291 | height - 2); | |
| 292 | } |