Debian Codegraph @

shumate/shumate-license.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2011-2013 Jiri Techet <[email protected]>
3 * Copyright (C) 2019 Marcus Lundblad <[email protected]>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library 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 GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /**
21 * ShumateLicense:
22 *
23 * A widget that displays license text.
24 */
25
26 #include "shumate-license.h"
27
28 enum
29 {
30 PROP_EXTRA_TEXT = 1,
31 PROP_XALIGN,
32 N_PROPERTIES,
33 };
34
35 static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, };
36
37 struct _ShumateLicense
38 {
39 GtkWidget parent_instance;
40
41 GtkWidget *extra_text_label;
42 GtkWidget *license_label;
43 GPtrArray *map_sources;
44 };
45
46 G_DEFINE_TYPE (ShumateLicense, shumate_license, GTK_TYPE_WIDGET);
47
48 static void
49 shumate_license_get_property (GObject *object,
50 guint prop_id,
51 GValue *value,
52 GParamSpec *pspec)
53 {
54 ShumateLicense *self = SHUMATE_LICENSE (object);
55
56 switch (prop_id)
57 {
58 case PROP_EXTRA_TEXT:
59 g_value_set_string (value, gtk_label_get_label (GTK_LABEL (self->extra_text_label)));
60 break;
61
62 case PROP_XALIGN:
63 g_value_set_float (value, gtk_label_get_xalign (GTK_LABEL (self->license_label)));
64 break;
65
66 default:
67 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
68 }
69 }
70
71
72 static void
73 shumate_license_set_property (GObject *object,
74 guint prop_id,
75 const GValue *value,
76 GParamSpec *pspec)
77 {
78 ShumateLicense *license = SHUMATE_LICENSE (object);
79
80 switch (prop_id)
81 {
82 case PROP_EXTRA_TEXT:
83 shumate_license_set_extra_text (license, g_value_get_string (value));
84 break;
85
86 case PROP_XALIGN:
87 shumate_license_set_xalign (license, g_value_get_float (value));
88 break;
89
90 default:
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92 }
93 }
94
95 static void
96 shumate_license_sources_changed (ShumateLicense *self)
97 {
98 g_autoptr(GString) license_str = NULL;
99 guint i;
100
101 g_assert (SHUMATE_IS_LICENSE (self));
102
103 license_str = g_string_new (NULL);
104 for (i = 0; i < self->map_sources->len; i++)
105 {
106 ShumateMapSource *map_source = g_ptr_array_index (self->map_sources, i);
107 const char *license = shumate_map_source_get_license (map_source);
108
109 if (license == NULL)
110 continue;
111
112 if (license_str->len == 0)
113 g_string_append (license_str, license);
114 else
115 g_string_append_printf (license_str, "\n%s", license);
116 }
117
118 gtk_label_set_label (GTK_LABEL (self->license_label), license_str->str);
119 }
120
121 static void
122 shumate_license_dispose (GObject *object)
123 {
124 ShumateLicense *self = SHUMATE_LICENSE (object);
125
126 g_clear_pointer (&self->map_sources, g_ptr_array_unref);
127 g_clear_pointer (&self->extra_text_label, gtk_widget_unparent);
128 g_clear_pointer (&self->license_label, gtk_widget_unparent);
129
130 G_OBJECT_CLASS (shumate_license_parent_class)->dispose (object);
131 }
132
133 static void
134 shumate_license_class_init (ShumateLicenseClass *klass)
135 {
136 GObjectClass *object_class = G_OBJECT_CLASS (klass);
137 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
138 GdkDisplay *display;
139
140 object_class->dispose = shumate_license_dispose;
141 object_class->get_property = shumate_license_get_property;
142 object_class->set_property = shumate_license_set_property;
143
144 /**
145 * ShumateLicense:extra-text:
146 *
147 * Sets additional text to be displayed in the license area. The map's
148 * license will be added below it. Your text can have multiple lines, just use
149 * "\n" in between.
150 */
151 obj_properties[PROP_EXTRA_TEXT] =
152 g_param_spec_string ("extra-text",
153 "Additional license",
154 "Additional license text",
155 NULL,
156 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
157
158 /**
159 * ShumateLicense:xalign:
160 *
161 * The license's horizontal alignment
162 */
163 obj_properties[PROP_XALIGN] =
164 g_param_spec_float ("xalign",
165 "Horizontal Alignment",
166 "X alignment of the child",
167 0.0, 1.0, 0.5,
168 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
169
170 g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
171
172 gtk_widget_class_set_css_name (widget_class, "map-license");
173 gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
174
175 display = gdk_display_get_default ();
176 if (display)
177 {
178 g_autoptr(GtkCssProvider) provider = gtk_css_provider_new ();
179 gtk_css_provider_load_from_resource (provider, "/org/gnome/shumate/license.css");
180 gtk_style_context_add_provider_for_display (display,
181 GTK_STYLE_PROVIDER (provider),
182 GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
183 }
184 }
185
186
187 static void
188 shumate_license_init (ShumateLicense *self)
189 {
190 self->map_sources = g_ptr_array_new_with_free_func (g_object_unref);
191 self->license_label = gtk_label_new (NULL);
192 self->extra_text_label = gtk_label_new (NULL);
193
194 g_object_set (gtk_widget_get_layout_manager (GTK_WIDGET (self)),
195 "orientation", GTK_ORIENTATION_VERTICAL,
196 NULL);
197 g_object_set (self->license_label,
198 "wrap", TRUE,
199 "xalign", 1.0f,
200 NULL);
201 g_object_set (self->extra_text_label,
202 "visible", FALSE,
203 "wrap", TRUE,
204 "xalign", 1.0f,
205 NULL);
206
207 gtk_widget_insert_after (self->license_label, GTK_WIDGET (self), NULL);
208 gtk_widget_insert_after (self->extra_text_label, GTK_WIDGET (self), self->license_label);
209 }
210
211
212 /**
213 * shumate_license_new:
214 *
215 * Creates an instance of #ShumateLicense.
216 *
217 * Returns: a new #ShumateLicense.
218 */
219 ShumateLicense *
220 shumate_license_new (void)
221 {
222 return SHUMATE_LICENSE (g_object_new (SHUMATE_TYPE_LICENSE, NULL));
223 }
224
225 /**
226 * shumate_license_set_extra_text:
227 * @license: a #ShumateLicense
228 * @text: the additional license text
229 *
230 * Show the additional license text on the map view. The text will preceed the
231 * map's licence when displayed. Use "\n" to separate the lines.
232 */
233 void
234 shumate_license_set_extra_text (ShumateLicense *license,
235 const char *text)
236 {
237 g_return_if_fail (SHUMATE_IS_LICENSE (license));
238
239 gtk_label_set_label (GTK_LABEL (license->extra_text_label), text);
240 gtk_widget_set_visible (license->extra_text_label, text != NULL);
241
242 g_object_notify_by_pspec (G_OBJECT (license), obj_properties[PROP_EXTRA_TEXT]);
243 }
244
245
246 /**
247 * shumate_license_get_extra_text:
248 * @license: a #ShumateLicense
249 *
250 * Gets the additional license text.
251 *
252 * Returns: the additional license text
253 */
254 const char *
255 shumate_license_get_extra_text (ShumateLicense *license)
256 {
257 g_return_val_if_fail (SHUMATE_IS_LICENSE (license), NULL);
258
259 return gtk_label_get_label (GTK_LABEL (license->extra_text_label));
260 }
261
262
263 /**
264 * shumate_license_set_xalign:
265 * @license: a #ShumateLicense
266 * @xalign: The license's text horizontal alignment
267 *
268 * Set the license's text horizontal alignment.
269 */
270 void
271 shumate_license_set_xalign (ShumateLicense *license,
272 float xalign)
273 {
274 g_return_if_fail (SHUMATE_IS_LICENSE (license));
275
276 gtk_label_set_xalign (GTK_LABEL (license->license_label), xalign);
277 gtk_label_set_xalign (GTK_LABEL (license->extra_text_label), xalign);
278
279 g_object_notify_by_pspec (G_OBJECT (license), obj_properties[PROP_XALIGN]);
280 }
281
282
283 /**
284 * shumate_license_get_xalign:
285 * @license: The license
286 *
287 * Get the license's text horizontal alignment.
288 *
289 * Returns: the license's text horizontal alignment.
290 */
291 float
292 shumate_license_get_xalign (ShumateLicense *license)
293 {
294 g_return_val_if_fail (SHUMATE_IS_LICENSE (license), 1.0f);
295
296 return gtk_label_get_xalign (GTK_LABEL (license->license_label));
297 }
298
299 void
300 shumate_license_append_map_source (ShumateLicense *license,
301 ShumateMapSource *map_source)
302 {
303 g_return_if_fail (SHUMATE_IS_LICENSE (license));
304
305 g_ptr_array_add (license->map_sources, g_object_ref (map_source));
306 shumate_license_sources_changed (license);
307 }
308
309 void
310 shumate_license_prepend_map_source (ShumateLicense *license,
311 ShumateMapSource *map_source)
312 {
313 g_return_if_fail (SHUMATE_IS_LICENSE (license));
314
315 g_ptr_array_insert (license->map_sources, 0, g_object_ref (map_source));
316 shumate_license_sources_changed (license);
317 }
318
319 void
320 shumate_license_remove_map_source (ShumateLicense *license,
321 ShumateMapSource *map_source)
322 {
323 g_return_if_fail (SHUMATE_IS_LICENSE (license));
324
325 g_ptr_array_remove (license->map_sources, map_source);
326 shumate_license_sources_changed (license);
327 }