~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to panels/user-accounts/um-strength-bar.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright 2010  Red Hat, Inc,
 
4
 *
 
5
 * This program is free software; you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation; either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 *
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
18
 *
 
19
 * Written by: Matthias Clasen <mclasen@redhat.com>
 
20
 */
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <glib.h>
 
25
#include <glib/gi18n.h>
 
26
#include <gtk/gtk.h>
 
27
 
 
28
#include "um-strength-bar.h"
 
29
 
 
30
#define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UM_TYPE_STRENGTH_BAR, UmStrengthBarPrivate))
 
31
 
 
32
struct _UmStrengthBarPrivate {
 
33
        gdouble strength;
 
34
};
 
35
 
 
36
enum {
 
37
        PROP_0,
 
38
        PROP_STRENGTH
 
39
};
 
40
 
 
41
G_DEFINE_TYPE (UmStrengthBar, um_strength_bar, GTK_TYPE_WIDGET);
 
42
 
 
43
 
 
44
static void
 
45
um_strength_bar_set_property (GObject      *object,
 
46
                              guint         prop_id,
 
47
                              const GValue *value,
 
48
                              GParamSpec   *pspec)
 
49
{
 
50
        UmStrengthBar *bar = UM_STRENGTH_BAR (object);
 
51
 
 
52
        switch (prop_id) {
 
53
        case PROP_STRENGTH:
 
54
                um_strength_bar_set_strength (bar, g_value_get_double (value));
 
55
                break;
 
56
        default:
 
57
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
58
                break;
 
59
        }
 
60
}
 
61
 
 
62
static void
 
63
um_strength_bar_get_property (GObject    *object,
 
64
                              guint       prop_id,
 
65
                              GValue     *value,
 
66
                              GParamSpec *pspec)
 
67
{
 
68
        UmStrengthBar *bar = UM_STRENGTH_BAR (object);
 
69
 
 
70
        switch (prop_id) {
 
71
        case PROP_STRENGTH:
 
72
                g_value_set_double (value, um_strength_bar_get_strength (bar));
 
73
                break;
 
74
        default:
 
75
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
76
                break;
 
77
        }
 
78
}
 
79
 
 
80
static void
 
81
curved_rectangle (cairo_t *cr,
 
82
                  double   x0,
 
83
                  double   y0,
 
84
                  double   width,
 
85
                  double   height,
 
86
                  double   radius)
 
87
{
 
88
        double x1;
 
89
        double y1;
 
90
 
 
91
        x1 = x0 + width;
 
92
        y1 = y0 + height;
 
93
 
 
94
        if (!width || !height) {
 
95
                return;
 
96
        }
 
97
 
 
98
        if (width / 2 < radius) {
 
99
                if (height / 2 < radius) {
 
100
                        cairo_move_to  (cr, x0, (y0 + y1) / 2);
 
101
                        cairo_curve_to (cr, x0 ,y0, x0, y0, (x0 + x1) / 2, y0);
 
102
                        cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1) / 2);
 
103
                        cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0) / 2, y1);
 
104
                        cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1) / 2);
 
105
                } else {
 
106
                        cairo_move_to  (cr, x0, y0 + radius);
 
107
                        cairo_curve_to (cr, x0, y0, x0, y0, (x0 + x1) / 2, y0);
 
108
                        cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
 
109
                        cairo_line_to (cr, x1, y1 - radius);
 
110
                        cairo_curve_to (cr, x1, y1, x1, y1, (x1 + x0) / 2, y1);
 
111
                        cairo_curve_to (cr, x0, y1, x0, y1, x0, y1 - radius);
 
112
                }
 
113
        } else {
 
114
                if (height / 2 < radius) {
 
115
                        cairo_move_to  (cr, x0, (y0 + y1) / 2);
 
116
                        cairo_curve_to (cr, x0, y0, x0 , y0, x0 + radius, y0);
 
117
                        cairo_line_to (cr, x1 - radius, y0);
 
118
                        cairo_curve_to (cr, x1, y0, x1, y0, x1, (y0 + y1) / 2);
 
119
                        cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
 
120
                        cairo_line_to (cr, x0 + radius, y1);
 
121
                        cairo_curve_to (cr, x0, y1, x0, y1, x0, (y0 + y1) / 2);
 
122
                } else {
 
123
                        cairo_move_to  (cr, x0, y0 + radius);
 
124
                        cairo_curve_to (cr, x0 , y0, x0 , y0, x0 + radius, y0);
 
125
                        cairo_line_to (cr, x1 - radius, y0);
 
126
                        cairo_curve_to (cr, x1, y0, x1, y0, x1, y0 + radius);
 
127
                        cairo_line_to (cr, x1, y1 - radius);
 
128
                        cairo_curve_to (cr, x1, y1, x1, y1, x1 - radius, y1);
 
129
                        cairo_line_to (cr, x0 + radius, y1);
 
130
                        cairo_curve_to (cr, x0, y1, x0, y1, x0, y1 - radius);
 
131
                }
 
132
        }
 
133
 
 
134
        cairo_close_path (cr);
 
135
}
 
136
 
 
137
GdkRGBA color1 = { 213.0/255.0,   4.0/255.0,   4.0/255.0, 1.0 };
 
138
GdkRGBA color2 = { 234.0/255.0, 236.0/255.0,  31.0/255.0, 1.0 };
 
139
GdkRGBA color3 = { 141.0/255.0, 133.0/255.0, 241.0/255.0, 1.0 };
 
140
GdkRGBA color4 = {  99.0/255.0, 251.0/255.0, 107.0/255.0, 1.0 };
 
141
 
 
142
static void
 
143
get_color (gdouble value, GdkRGBA *color)
 
144
{
 
145
        if (value < 0.50) {
 
146
                *color = color1;
 
147
        }
 
148
        else if (value < 0.75) {
 
149
                *color = color2;
 
150
        }
 
151
        else if (value < 0.90) {
 
152
                *color = color3;
 
153
        }
 
154
        else {
 
155
                *color = color4;
 
156
        }
 
157
}
 
158
 
 
159
static gboolean
 
160
um_strength_bar_draw (GtkWidget *widget,
 
161
                      cairo_t   *cr)
 
162
{
 
163
        UmStrengthBar *bar = UM_STRENGTH_BAR (widget);
 
164
        GdkRGBA color;
 
165
        gint width, height;
 
166
        GtkStyleContext *context;
 
167
        GtkStateFlags state;
 
168
        GdkRGBA border_color;
 
169
 
 
170
        context = gtk_widget_get_style_context (widget);
 
171
        state = gtk_widget_get_state_flags (widget);
 
172
        gtk_style_context_get_border_color (context, state, &border_color);
 
173
 
 
174
        width = gtk_widget_get_allocated_width (widget);
 
175
        height = gtk_widget_get_allocated_height (widget);
 
176
 
 
177
        cairo_save (cr);
 
178
 
 
179
        cairo_set_line_width (cr, 1);
 
180
 
 
181
        cairo_save (cr);
 
182
 
 
183
        cairo_rectangle (cr,
 
184
                         0, 0,
 
185
                         bar->priv->strength * width, height);
 
186
        cairo_clip (cr);
 
187
 
 
188
        curved_rectangle (cr,
 
189
                          0.5, 0.5,
 
190
                          width - 1, height - 1,
 
191
                          4);
 
192
        get_color (bar->priv->strength, &color);
 
193
        gdk_cairo_set_source_rgba (cr, &color);
 
194
        cairo_fill_preserve (cr);
 
195
 
 
196
        cairo_restore (cr); /* undo clip */
 
197
 
 
198
        gdk_cairo_set_source_rgba (cr, &border_color);
 
199
        cairo_stroke (cr);
 
200
 
 
201
        cairo_restore (cr);
 
202
 
 
203
        return FALSE;
 
204
}
 
205
 
 
206
static void
 
207
um_strength_bar_class_init (UmStrengthBarClass *class)
 
208
{
 
209
        GObjectClass *gobject_class;
 
210
        GtkWidgetClass *widget_class;
 
211
 
 
212
        gobject_class = (GObjectClass*)class;
 
213
        widget_class = (GtkWidgetClass*)class;
 
214
 
 
215
        gobject_class->set_property = um_strength_bar_set_property;
 
216
        gobject_class->get_property = um_strength_bar_get_property;
 
217
 
 
218
        widget_class->draw = um_strength_bar_draw;
 
219
 
 
220
         g_object_class_install_property (gobject_class,
 
221
                                          PROP_STRENGTH,
 
222
                                          g_param_spec_double ("strength",
 
223
                                                               "Strength",
 
224
                                                               "Strength",
 
225
                                                               0.0, 1.0, 0.0,
 
226
                                                               G_PARAM_READWRITE));
 
227
 
 
228
        g_type_class_add_private (class, sizeof (UmStrengthBarPrivate));
 
229
}
 
230
 
 
231
static void
 
232
um_strength_bar_init (UmStrengthBar *bar)
 
233
{
 
234
        gtk_widget_set_has_window (GTK_WIDGET (bar), FALSE);
 
235
        gtk_widget_set_size_request (GTK_WIDGET (bar), 120, 8);
 
236
 
 
237
        bar->priv = GET_PRIVATE (bar);
 
238
        bar->priv->strength = 0.0;
 
239
}
 
240
 
 
241
GtkWidget *
 
242
um_strength_bar_new (void)
 
243
{
 
244
        return (GtkWidget*) g_object_new (UM_TYPE_STRENGTH_BAR, NULL);
 
245
}
 
246
 
 
247
void
 
248
um_strength_bar_set_strength (UmStrengthBar *bar,
 
249
                              gdouble        strength)
 
250
{
 
251
        bar->priv->strength = strength;
 
252
 
 
253
        g_object_notify (G_OBJECT (bar), "strength");
 
254
 
 
255
        if (gtk_widget_is_drawable (GTK_WIDGET (bar))) {
 
256
                gtk_widget_queue_draw (GTK_WIDGET (bar));
 
257
        }
 
258
}
 
259
 
 
260
gdouble
 
261
um_strength_bar_get_strength (UmStrengthBar *bar)
 
262
{
 
263
        return bar->priv->strength;
 
264
}