1 | /* |
---|
2 | * Copyright (C) 2008 Red Hat, Inc. |
---|
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 | * Written by: Ray Strode <rstrode@redhat.com> |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | #include "gdm-cell-renderer-timer.h" |
---|
23 | #include <glib/gi18n.h> |
---|
24 | |
---|
25 | #define GDM_CELL_RENDERER_TIMER_GET_PRIVATE(object) (G_TYPE_INSTANCE_GET_PRIVATE ((object), GDM_TYPE_CELL_RENDERER_TIMER, GdmCellRendererTimerPrivate)) |
---|
26 | |
---|
27 | struct _GdmCellRendererTimerPrivate |
---|
28 | { |
---|
29 | gdouble value; |
---|
30 | }; |
---|
31 | |
---|
32 | enum |
---|
33 | { |
---|
34 | PROP_0, |
---|
35 | PROP_VALUE, |
---|
36 | }; |
---|
37 | |
---|
38 | G_DEFINE_TYPE (GdmCellRendererTimer, gdm_cell_renderer_timer, GTK_TYPE_CELL_RENDERER) |
---|
39 | |
---|
40 | static void |
---|
41 | gdm_cell_renderer_timer_get_property (GObject *object, |
---|
42 | guint param_id, |
---|
43 | GValue *value, |
---|
44 | GParamSpec *pspec) |
---|
45 | { |
---|
46 | GdmCellRendererTimer *renderer; |
---|
47 | |
---|
48 | renderer = GDM_CELL_RENDERER_TIMER (object); |
---|
49 | |
---|
50 | switch (param_id) { |
---|
51 | case PROP_VALUE: |
---|
52 | g_value_set_double (value, renderer->priv->value); |
---|
53 | break; |
---|
54 | default: |
---|
55 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
59 | static void |
---|
60 | gdm_cell_renderer_timer_set_value (GdmCellRendererTimer *renderer, |
---|
61 | gdouble value) |
---|
62 | { |
---|
63 | renderer->priv->value = value; |
---|
64 | } |
---|
65 | |
---|
66 | static void |
---|
67 | gdm_cell_renderer_timer_set_property (GObject *object, |
---|
68 | guint param_id, |
---|
69 | const GValue *value, |
---|
70 | GParamSpec *pspec) |
---|
71 | { |
---|
72 | GdmCellRendererTimer *renderer; |
---|
73 | |
---|
74 | renderer = GDM_CELL_RENDERER_TIMER (object); |
---|
75 | |
---|
76 | switch (param_id) { |
---|
77 | case PROP_VALUE: |
---|
78 | gdm_cell_renderer_timer_set_value (renderer, |
---|
79 | g_value_get_double (value)); |
---|
80 | break; |
---|
81 | default: |
---|
82 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | static void |
---|
87 | gdm_cell_renderer_timer_get_size (GtkCellRenderer *cell, |
---|
88 | GtkWidget *widget, |
---|
89 | GdkRectangle *cell_area, |
---|
90 | gint *x_offset, |
---|
91 | gint *y_offset, |
---|
92 | gint *width, |
---|
93 | gint *height) |
---|
94 | { |
---|
95 | |
---|
96 | GdmCellRendererTimer *renderer; |
---|
97 | |
---|
98 | renderer = GDM_CELL_RENDERER_TIMER (cell); |
---|
99 | |
---|
100 | if (cell_area != NULL) { |
---|
101 | if (x_offset != NULL) { |
---|
102 | *x_offset = 0; |
---|
103 | } |
---|
104 | |
---|
105 | if (y_offset != NULL) { |
---|
106 | *y_offset = 0; |
---|
107 | } |
---|
108 | } |
---|
109 | |
---|
110 | if (width != NULL) { |
---|
111 | if (cell->width >= 0) { |
---|
112 | *width = cell->width; |
---|
113 | } else { |
---|
114 | *width = renderer->priv->value <= G_MINDOUBLE? 0 : 64; |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | if (height != NULL) { |
---|
119 | if (cell->height >= 0) { |
---|
120 | *height = cell->height; |
---|
121 | } else { |
---|
122 | *height = renderer->priv->value <= G_MINDOUBLE? 0 : 64; |
---|
123 | } |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | static double |
---|
128 | get_opacity_for_value (double value) |
---|
129 | { |
---|
130 | const double start_value = 0.05; |
---|
131 | const double end_value = 0.33; |
---|
132 | |
---|
133 | if (value < start_value) { |
---|
134 | return 0.0; |
---|
135 | } |
---|
136 | |
---|
137 | if (value >= end_value) { |
---|
138 | return 1.0; |
---|
139 | } |
---|
140 | |
---|
141 | return ((value - start_value) / (end_value - start_value)); |
---|
142 | } |
---|
143 | |
---|
144 | static void |
---|
145 | draw_timer (GdmCellRendererTimer *renderer, |
---|
146 | cairo_t *context, |
---|
147 | GdkColor *fg, |
---|
148 | GdkColor *bg, |
---|
149 | int width, |
---|
150 | int height) |
---|
151 | { |
---|
152 | double radius; |
---|
153 | double opacity; |
---|
154 | |
---|
155 | opacity = get_opacity_for_value (renderer->priv->value); |
---|
156 | |
---|
157 | if (opacity <= G_MINDOUBLE) { |
---|
158 | return; |
---|
159 | } |
---|
160 | |
---|
161 | radius = .5 * (MIN (width, height) / 2.0); |
---|
162 | |
---|
163 | cairo_translate (context, width / 2., height / 2.); |
---|
164 | |
---|
165 | cairo_set_source_rgba (context, |
---|
166 | fg->red / 65535.0, |
---|
167 | fg->green / 65535.0, |
---|
168 | fg->blue / 65535.0, |
---|
169 | opacity); |
---|
170 | |
---|
171 | cairo_move_to (context, 0, 0); |
---|
172 | cairo_arc (context, 0, 0, radius + 1, 0, 2 * G_PI); |
---|
173 | cairo_fill (context); |
---|
174 | |
---|
175 | cairo_set_operator (context, CAIRO_OPERATOR_SOURCE); |
---|
176 | cairo_set_source_rgb (context, |
---|
177 | bg->red / 65535.0, |
---|
178 | bg->green / 65535.0, |
---|
179 | bg->blue / 65535.0); |
---|
180 | cairo_move_to (context, 0, 0); |
---|
181 | cairo_arc (context, 0, 0, radius, - G_PI / 2, |
---|
182 | renderer->priv->value * 2 * G_PI - G_PI / 2); |
---|
183 | cairo_clip (context); |
---|
184 | cairo_paint_with_alpha (context, opacity); |
---|
185 | } |
---|
186 | |
---|
187 | static void |
---|
188 | gdm_cell_renderer_timer_render (GtkCellRenderer *cell, |
---|
189 | GdkWindow *window, |
---|
190 | GtkWidget *widget, |
---|
191 | GdkRectangle *background_area, |
---|
192 | GdkRectangle *cell_area, |
---|
193 | GdkRectangle *expose_area, |
---|
194 | GtkCellRendererState renderer_state) |
---|
195 | { |
---|
196 | GdmCellRendererTimer *renderer; |
---|
197 | cairo_t *context; |
---|
198 | GtkStateType widget_state; |
---|
199 | |
---|
200 | renderer = GDM_CELL_RENDERER_TIMER (cell); |
---|
201 | |
---|
202 | if (renderer->priv->value <= G_MINDOUBLE) { |
---|
203 | return; |
---|
204 | } |
---|
205 | |
---|
206 | context = gdk_cairo_create (GDK_DRAWABLE (window)); |
---|
207 | |
---|
208 | if (expose_area != NULL) { |
---|
209 | gdk_cairo_rectangle (context, expose_area); |
---|
210 | cairo_clip (context); |
---|
211 | } |
---|
212 | |
---|
213 | cairo_translate (context, |
---|
214 | cell_area->x + cell->xpad, |
---|
215 | cell_area->y + cell->ypad); |
---|
216 | |
---|
217 | widget_state = GTK_STATE_NORMAL; |
---|
218 | if (renderer_state & GTK_CELL_RENDERER_SELECTED) { |
---|
219 | if (GTK_WIDGET_HAS_FOCUS (widget)) { |
---|
220 | widget_state = GTK_STATE_SELECTED; |
---|
221 | } else { |
---|
222 | widget_state = GTK_STATE_ACTIVE; |
---|
223 | } |
---|
224 | } |
---|
225 | |
---|
226 | if (renderer_state & GTK_CELL_RENDERER_INSENSITIVE) { |
---|
227 | widget_state = GTK_STATE_INSENSITIVE; |
---|
228 | } |
---|
229 | |
---|
230 | draw_timer (renderer, context, |
---|
231 | &widget->style->text_aa[widget_state], |
---|
232 | &widget->style->base[widget_state], |
---|
233 | cell_area->width, cell_area->height); |
---|
234 | |
---|
235 | cairo_destroy (context); |
---|
236 | } |
---|
237 | |
---|
238 | static void |
---|
239 | gdm_cell_renderer_timer_class_init (GdmCellRendererTimerClass *klass) |
---|
240 | { |
---|
241 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
---|
242 | GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (klass); |
---|
243 | |
---|
244 | object_class->get_property = gdm_cell_renderer_timer_get_property; |
---|
245 | object_class->set_property = gdm_cell_renderer_timer_set_property; |
---|
246 | |
---|
247 | cell_class->get_size = gdm_cell_renderer_timer_get_size; |
---|
248 | cell_class->render = gdm_cell_renderer_timer_render; |
---|
249 | |
---|
250 | g_object_class_install_property (object_class, |
---|
251 | PROP_VALUE, |
---|
252 | g_param_spec_double ("value", |
---|
253 | _("Value"), |
---|
254 | _("percentage of time complete"), |
---|
255 | 0.0, 1.0, 0.0, |
---|
256 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); |
---|
257 | |
---|
258 | g_type_class_add_private (object_class, |
---|
259 | sizeof (GdmCellRendererTimerPrivate)); |
---|
260 | } |
---|
261 | |
---|
262 | static void |
---|
263 | gdm_cell_renderer_timer_init (GdmCellRendererTimer *renderer) |
---|
264 | { |
---|
265 | renderer->priv = GDM_CELL_RENDERER_TIMER_GET_PRIVATE (renderer); |
---|
266 | } |
---|
267 | |
---|
268 | GtkCellRenderer* |
---|
269 | gdm_cell_renderer_timer_new (void) |
---|
270 | { |
---|
271 | return g_object_new (GDM_TYPE_CELL_RENDERER_TIMER, NULL); |
---|
272 | } |
---|
273 | |
---|