1 /* gtd-star-widget.c
2 *
3 * Copyright 2020 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
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, see <http://www.gnu.org/licenses/>.
17 *
18 * SPDX-License-Identifier: GPL-3.0-or-later
19 */
20
21 #include "gtd-star-widget.h"
22
23 struct _GtdStarWidget
24 {
25 GtdWidget parent;
26
27 GtkWidget *filled_star;
28 GtkWidget *empty_star;
29
30 gboolean active;
31 };
32
33 G_DEFINE_TYPE (GtdStarWidget, gtd_star_widget, GTD_TYPE_WIDGET)
34
35 enum
36 {
37 PROP_0,
38 PROP_ACTIVE,
39 N_PROPS
40 };
41
42 static GParamSpec *properties [N_PROPS];
43
44
45 /*
46 * Callbacks
47 */
48
49 static void
on_star_widget_clicked_cb(GtkGestureClick * gesture,gint n_press,gdouble x,gdouble y,GtdStarWidget * self)50 on_star_widget_clicked_cb (GtkGestureClick *gesture,
51 gint n_press,
52 gdouble x,
53 gdouble y,
54 GtdStarWidget *self)
55 {
56 gtd_star_widget_set_active (self, !self->active);
57 gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
58 }
59
60
61 /*
62 * GObject overrides
63 */
64
65 static void
gtd_star_widget_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)66 gtd_star_widget_get_property (GObject *object,
67 guint prop_id,
68 GValue *value,
69 GParamSpec *pspec)
70 {
71 GtdStarWidget *self = GTD_STAR_WIDGET (object);
72
73 switch (prop_id)
74 {
75 case PROP_ACTIVE:
76 g_value_set_boolean (value, self->active);
77 break;
78
79 default:
80 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
81 }
82 }
83
84 static void
gtd_star_widget_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)85 gtd_star_widget_set_property (GObject *object,
86 guint prop_id,
87 const GValue *value,
88 GParamSpec *pspec)
89 {
90 GtdStarWidget *self = GTD_STAR_WIDGET (object);
91
92 switch (prop_id)
93 {
94 case PROP_ACTIVE:
95 gtd_star_widget_set_active (self, g_value_get_boolean (value));
96 break;
97
98 default:
99 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
100 }
101 }
102
103 static void
gtd_star_widget_class_init(GtdStarWidgetClass * klass)104 gtd_star_widget_class_init (GtdStarWidgetClass *klass)
105 {
106 GObjectClass *object_class = G_OBJECT_CLASS (klass);
107 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
108
109 object_class->get_property = gtd_star_widget_get_property;
110 object_class->set_property = gtd_star_widget_set_property;
111
112 /**
113 * GtdStarWidget:active:
114 *
115 * Whether the star widget is active or not. When active, the
116 * star appears filled.
117 */
118 properties[PROP_ACTIVE] = g_param_spec_boolean ("active",
119 "Active",
120 "Active",
121 FALSE,
122 G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
123
124 g_object_class_install_properties (object_class, N_PROPS, properties);
125
126 gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BIN_LAYOUT);
127 gtk_widget_class_set_css_name (widget_class, "star");
128 }
129
130 static void
gtd_star_widget_init(GtdStarWidget * self)131 gtd_star_widget_init (GtdStarWidget *self)
132 {
133 GtkGesture *click_gesture;
134
135 click_gesture = gtk_gesture_click_new ();
136 gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (click_gesture), FALSE);
137 gtk_gesture_single_set_exclusive (GTK_GESTURE_SINGLE (click_gesture), TRUE);
138 gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (click_gesture), GDK_BUTTON_PRIMARY);
139 gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (click_gesture), GTK_PHASE_CAPTURE);
140 g_signal_connect (click_gesture, "pressed", G_CALLBACK (on_star_widget_clicked_cb), self);
141 gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (click_gesture));
142
143 gtk_widget_set_cursor_from_name (GTK_WIDGET (self), "default");
144
145 self->empty_star = gtk_image_new_from_icon_name ("non-starred-symbolic");
146 gtk_widget_set_parent (self->empty_star, GTK_WIDGET (self));
147
148 self->filled_star = gtk_image_new_from_icon_name ("starred-symbolic");
149 gtk_widget_set_parent (self->filled_star, GTK_WIDGET (self));
150 gtk_widget_hide (self->filled_star);
151
152 g_object_bind_property (self->filled_star,
153 "visible",
154 self->empty_star,
155 "visible",
156 G_BINDING_INVERT_BOOLEAN | G_BINDING_SYNC_CREATE);
157 }
158
159 GtkWidget*
gtd_star_widget_new(void)160 gtd_star_widget_new (void)
161 {
162 return g_object_new (GTD_TYPE_STAR_WIDGET, NULL);
163 }
164
165 gboolean
gtd_star_widget_get_active(GtdStarWidget * self)166 gtd_star_widget_get_active (GtdStarWidget *self)
167 {
168 g_return_val_if_fail (GTD_IS_STAR_WIDGET (self), FALSE);
169
170 return self->active;
171 }
172
173 void
gtd_star_widget_set_active(GtdStarWidget * self,gboolean active)174 gtd_star_widget_set_active (GtdStarWidget *self,
175 gboolean active)
176 {
177 g_return_if_fail (GTD_IS_STAR_WIDGET (self));
178
179 if (self->active == active)
180 return;
181
182 self->active = active;
183 gtk_widget_set_visible (self->filled_star, active);
184
185 if (active)
186 gtk_widget_set_state_flags (GTK_WIDGET (self), GTK_STATE_FLAG_CHECKED, FALSE);
187 else
188 gtk_widget_unset_state_flags (GTK_WIDGET (self), GTK_STATE_FLAG_CHECKED);
189
190 /* TODO: explosion effect */
191
192 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_ACTIVE]);
193 }
194