1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright 2010 Red Hat, Inc,
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.
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.
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.
19
* Written by: Matthias Clasen <mclasen@redhat.com>
25
#include <glib/gi18n.h>
28
#include "um-strength-bar.h"
30
#define GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UM_TYPE_STRENGTH_BAR, UmStrengthBarPrivate))
32
struct _UmStrengthBarPrivate {
41
G_DEFINE_TYPE (UmStrengthBar, um_strength_bar, GTK_TYPE_WIDGET);
45
um_strength_bar_set_property (GObject *object,
50
UmStrengthBar *bar = UM_STRENGTH_BAR (object);
54
um_strength_bar_set_strength (bar, g_value_get_double (value));
57
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
63
um_strength_bar_get_property (GObject *object,
68
UmStrengthBar *bar = UM_STRENGTH_BAR (object);
72
g_value_set_double (value, um_strength_bar_get_strength (bar));
75
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81
curved_rectangle (cairo_t *cr,
94
if (!width || !height) {
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);
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);
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);
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);
134
cairo_close_path (cr);
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 };
143
get_color (gdouble value, GdkRGBA *color)
148
else if (value < 0.75) {
151
else if (value < 0.90) {
160
um_strength_bar_draw (GtkWidget *widget,
163
UmStrengthBar *bar = UM_STRENGTH_BAR (widget);
166
GtkStyleContext *context;
168
GdkRGBA border_color;
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);
174
width = gtk_widget_get_allocated_width (widget);
175
height = gtk_widget_get_allocated_height (widget);
179
cairo_set_line_width (cr, 1);
185
bar->priv->strength * width, height);
188
curved_rectangle (cr,
190
width - 1, height - 1,
192
get_color (bar->priv->strength, &color);
193
gdk_cairo_set_source_rgba (cr, &color);
194
cairo_fill_preserve (cr);
196
cairo_restore (cr); /* undo clip */
198
gdk_cairo_set_source_rgba (cr, &border_color);
207
um_strength_bar_class_init (UmStrengthBarClass *class)
209
GObjectClass *gobject_class;
210
GtkWidgetClass *widget_class;
212
gobject_class = (GObjectClass*)class;
213
widget_class = (GtkWidgetClass*)class;
215
gobject_class->set_property = um_strength_bar_set_property;
216
gobject_class->get_property = um_strength_bar_get_property;
218
widget_class->draw = um_strength_bar_draw;
220
g_object_class_install_property (gobject_class,
222
g_param_spec_double ("strength",
228
g_type_class_add_private (class, sizeof (UmStrengthBarPrivate));
232
um_strength_bar_init (UmStrengthBar *bar)
234
gtk_widget_set_has_window (GTK_WIDGET (bar), FALSE);
235
gtk_widget_set_size_request (GTK_WIDGET (bar), 120, 8);
237
bar->priv = GET_PRIVATE (bar);
238
bar->priv->strength = 0.0;
242
um_strength_bar_new (void)
244
return (GtkWidget*) g_object_new (UM_TYPE_STRENGTH_BAR, NULL);
248
um_strength_bar_set_strength (UmStrengthBar *bar,
251
bar->priv->strength = strength;
253
g_object_notify (G_OBJECT (bar), "strength");
255
if (gtk_widget_is_drawable (GTK_WIDGET (bar))) {
256
gtk_widget_queue_draw (GTK_WIDGET (bar));
261
um_strength_bar_get_strength (UmStrengthBar *bar)
263
return bar->priv->strength;