1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
* vi:set noexpandtab tabstop=8 shiftwidth=8:
*
* Copyright (C) 2013 Richard Hughes <richard@hughsie.com>
* Copyright (C) 2014-2015 Kalev Lember <klember@redhat.com>
*
* SPDX-License-Identifier: GPL-2.0+
*/
#include "config.h"
#include <math.h>
#include "gs-common.h"
#include "gs-star-widget.h"
typedef struct
{
gboolean interactive;
gint rating;
guint icon_size;
GtkWidget *box1;
GtkImage *images[5];
} GsStarWidgetPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (GsStarWidget, gs_star_widget, GTK_TYPE_BIN)
typedef enum {
PROP_ICON_SIZE = 1,
PROP_INTERACTIVE,
PROP_RATING,
} GsStarWidgetProperty;
enum {
RATING_CHANGED,
SIGNAL_LAST
};
static GParamSpec *properties[PROP_RATING + 1] = { 0, };
static guint signals [SIGNAL_LAST] = { 0 };
const gint rate_to_star[] = {20, 40, 60, 80, 100, -1};
static void gs_star_widget_refresh (GsStarWidget *star);
gint
gs_star_widget_get_rating (GsStarWidget *star)
{
GsStarWidgetPrivate *priv;
g_return_val_if_fail (GS_IS_STAR_WIDGET (star), -1);
priv = gs_star_widget_get_instance_private (star);
return priv->rating;
}
void
gs_star_widget_set_icon_size (GsStarWidget *star, guint pixel_size)
{
GsStarWidgetPrivate *priv;
g_return_if_fail (GS_IS_STAR_WIDGET (star));
priv = gs_star_widget_get_instance_private (star);
if (priv->icon_size == pixel_size)
return;
priv->icon_size = pixel_size;
g_object_notify_by_pspec (G_OBJECT (star), properties[PROP_ICON_SIZE]);
gs_star_widget_refresh (star);
}
static void
gs_star_widget_button_clicked_cb (GtkButton *button, GsStarWidget *star)
{
GsStarWidgetPrivate *priv;
gint rating;
priv = gs_star_widget_get_instance_private (star);
if (!priv->interactive)
return;
rating = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (button),
"GsStarWidget::value"));
gs_star_widget_set_rating (star, rating);
g_signal_emit (star, signals[RATING_CHANGED], 0, priv->rating);
}
/* Update the star styles to display the new rating */
static void
gs_star_widget_refresh_rating (GsStarWidget *star)
{
GsStarWidgetPrivate *priv = gs_star_widget_get_instance_private (star);
if (!gtk_widget_get_realized (GTK_WIDGET (star)))
return;
for (guint i = 0; i < G_N_ELEMENTS (priv->images); i++) {
GtkWidget *im = GTK_WIDGET (priv->images[i]);
gboolean enabled;
/* add fudge factor so we can actually get 5 stars in reality */
enabled = priv->rating >= rate_to_star[i] - 10;
gtk_style_context_add_class (gtk_widget_get_style_context (im),
enabled ? "star-enabled" : "star-disabled");
gtk_style_context_remove_class (gtk_widget_get_style_context (im),
enabled ? "star-disabled" : "star-enabled");
}
}
static void
gs_star_widget_refresh (GsStarWidget *star)
{
GsStarWidgetPrivate *priv = gs_star_widget_get_instance_private (star);
if (!gtk_widget_get_realized (GTK_WIDGET (star)))
return;
/* remove all existing widgets */
gs_container_remove_all (GTK_CONTAINER (priv->box1));
for (guint i = 0; i < G_N_ELEMENTS (priv->images); i++) {
GtkWidget *w;
GtkWidget *im;
/* create image */
im = gtk_image_new_from_icon_name ("starred-symbolic",
GTK_ICON_SIZE_DIALOG);
gtk_image_set_pixel_size (GTK_IMAGE (im), (gint) priv->icon_size);
priv->images[i] = GTK_IMAGE (im);
/* create button */
if (priv->interactive) {
w = gtk_button_new ();
g_signal_connect (w, "clicked",
G_CALLBACK (gs_star_widget_button_clicked_cb), star);
g_object_set_data (G_OBJECT (w),
"GsStarWidget::value",
GINT_TO_POINTER (rate_to_star[i]));
gtk_container_add (GTK_CONTAINER (w), im);
gtk_widget_set_visible (im, TRUE);
} else {
w = im;
}
gtk_widget_set_sensitive (w, priv->interactive);
gtk_style_context_add_class (gtk_widget_get_style_context (w), "star");
gtk_widget_set_visible (w, TRUE);
gtk_container_add (GTK_CONTAINER (priv->box1), w);
}
gs_star_widget_refresh_rating (star);
}
void
gs_star_widget_set_interactive (GsStarWidget *star, gboolean interactive)
{
GsStarWidgetPrivate *priv;
g_return_if_fail (GS_IS_STAR_WIDGET (star));
priv = gs_star_widget_get_instance_private (star);
if (priv->interactive == interactive)
return;
priv->interactive = interactive;
g_object_notify_by_pspec (G_OBJECT (star), properties[PROP_INTERACTIVE]);
gs_star_widget_refresh (star);
}
void
gs_star_widget_set_rating (GsStarWidget *star,
gint rating)
{
GsStarWidgetPrivate *priv;
g_return_if_fail (GS_IS_STAR_WIDGET (star));
priv = gs_star_widget_get_instance_private (star);
if (priv->rating == rating)
return;
priv->rating = rating;
g_object_notify_by_pspec (G_OBJECT (star), properties[PROP_RATING]);
gs_star_widget_refresh_rating (star);
}
static void
gs_star_widget_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GsStarWidget *self = GS_STAR_WIDGET (object);
GsStarWidgetPrivate *priv = gs_star_widget_get_instance_private (self);
switch ((GsStarWidgetProperty) prop_id) {
case PROP_ICON_SIZE:
g_value_set_uint (value, priv->icon_size);
break;
case PROP_INTERACTIVE:
g_value_set_boolean (value, priv->interactive);
break;
case PROP_RATING:
g_value_set_int (value, priv->rating);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gs_star_widget_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GsStarWidget *self = GS_STAR_WIDGET (object);
switch ((GsStarWidgetProperty) prop_id) {
case PROP_ICON_SIZE:
gs_star_widget_set_icon_size (self, g_value_get_uint (value));
break;
case PROP_INTERACTIVE:
gs_star_widget_set_interactive (self, g_value_get_boolean (value));
break;
case PROP_RATING:
gs_star_widget_set_rating (self, g_value_get_int (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gs_star_widget_destroy (GtkWidget *widget)
{
GTK_WIDGET_CLASS (gs_star_widget_parent_class)->destroy (widget);
}
static void
gs_star_widget_realize (GtkWidget *widget)
{
GTK_WIDGET_CLASS (gs_star_widget_parent_class)->realize (widget);
/* Create child widgets. */
gs_star_widget_refresh (GS_STAR_WIDGET (widget));
}
static void
gs_star_widget_init (GsStarWidget *star)
{
gtk_widget_set_has_window (GTK_WIDGET (star), FALSE);
gtk_widget_init_template (GTK_WIDGET (star));
}
static void
gs_star_widget_class_init (GsStarWidgetClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
widget_class->destroy = gs_star_widget_destroy;
widget_class->realize = gs_star_widget_realize;
object_class->get_property = gs_star_widget_get_property;
object_class->set_property = gs_star_widget_set_property;
/**
* GsStarWidget:icon-size:
*
* Size of the star icons to use in the widget, in pixels.
*
* Since: 3.38
*/
properties[PROP_ICON_SIZE] =
g_param_spec_uint ("icon-size",
"Icon Size",
"Size of icons to use, in pixels",
0, G_MAXUINT, 12,
G_PARAM_READWRITE);
/**
* GsStarWidget:interactive:
*
* Whether the widget accepts user input to change #GsStarWidget:rating.
*
* Since: 3.38
*/
properties[PROP_INTERACTIVE] =
g_param_spec_boolean ("interactive",
"Interactive",
"Whether the rating is interactive",
FALSE,
G_PARAM_READWRITE);
/**
* GsStarWidget:rating:
*
* The rating to display on the widget, as a percentage. `-1` indicates
* that the rating is unknown.
*
* Since: 3.38
*/
properties[PROP_RATING] =
g_param_spec_int ("rating",
"Rating",
"Rating, out of 100%, or -1 for unknown",
-1, 100, -1,
G_PARAM_READWRITE);
signals [RATING_CHANGED] =
g_signal_new ("rating-changed",
G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GsStarWidgetClass, rating_changed),
NULL, NULL, g_cclosure_marshal_VOID__UINT,
G_TYPE_NONE, 1, G_TYPE_UINT);
g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/Software/gs-star-widget.ui");
gtk_widget_class_bind_template_child_private (widget_class, GsStarWidget, box1);
}
GtkWidget *
gs_star_widget_new (void)
{
GsStarWidget *star;
star = g_object_new (GS_TYPE_STAR_WIDGET, NULL);
return GTK_WIDGET (star);
}
|