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