Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2011-2013 Jiri Techet <techet@gmail.com>
3 : : * Copyright (C) 2019 Marcus Lundblad <ml@update.uu.se>
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 [ + + + - ]: 4 : G_DEFINE_TYPE (ShumateLicense, shumate_license, GTK_TYPE_WIDGET);
47 : :
48 : : static void
49 : 0 : shumate_license_get_property (GObject *object,
50 : : guint prop_id,
51 : : GValue *value,
52 : : GParamSpec *pspec)
53 : : {
54 : 0 : ShumateLicense *self = SHUMATE_LICENSE (object);
55 : :
56 [ # # # ]: 0 : switch (prop_id)
57 : : {
58 : 0 : case PROP_EXTRA_TEXT:
59 : 0 : g_value_set_string (value, gtk_label_get_label (GTK_LABEL (self->extra_text_label)));
60 : 0 : break;
61 : :
62 : 0 : case PROP_XALIGN:
63 : 0 : g_value_set_float (value, gtk_label_get_xalign (GTK_LABEL (self->license_label)));
64 : 0 : break;
65 : :
66 : 0 : default:
67 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
68 : : }
69 : 0 : }
70 : :
71 : :
72 : : static void
73 : 0 : shumate_license_set_property (GObject *object,
74 : : guint prop_id,
75 : : const GValue *value,
76 : : GParamSpec *pspec)
77 : : {
78 : 0 : ShumateLicense *license = SHUMATE_LICENSE (object);
79 : :
80 [ # # # ]: 0 : switch (prop_id)
81 : : {
82 : 0 : case PROP_EXTRA_TEXT:
83 : 0 : shumate_license_set_extra_text (license, g_value_get_string (value));
84 : 0 : break;
85 : :
86 : 0 : case PROP_XALIGN:
87 : 0 : shumate_license_set_xalign (license, g_value_get_float (value));
88 : 0 : break;
89 : :
90 : 0 : default:
91 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
92 : : }
93 : 0 : }
94 : :
95 : : static void
96 : 0 : shumate_license_sources_changed (ShumateLicense *self)
97 : : {
98 : 0 : g_autoptr(GString) license_str = NULL;
99 : 0 : guint i;
100 : :
101 [ # # ]: 0 : g_assert (SHUMATE_IS_LICENSE (self));
102 : :
103 : 0 : license_str = g_string_new (NULL);
104 [ # # ]: 0 : for (i = 0; i < self->map_sources->len; i++)
105 : : {
106 : 0 : ShumateMapSource *map_source = g_ptr_array_index (self->map_sources, i);
107 : 0 : const char *license = shumate_map_source_get_license (map_source);
108 : :
109 [ # # ]: 0 : if (license == NULL)
110 : 0 : continue;
111 : :
112 [ # # ]: 0 : if (license_str->len == 0)
113 [ # # ]: 0 : g_string_append (license_str, license);
114 : : else
115 : 0 : g_string_append_printf (license_str, "\n%s", license);
116 : : }
117 : :
118 : 0 : gtk_label_set_label (GTK_LABEL (self->license_label), license_str->str);
119 : 0 : }
120 : :
121 : : static void
122 : 0 : shumate_license_dispose (GObject *object)
123 : : {
124 : 0 : ShumateLicense *self = SHUMATE_LICENSE (object);
125 : :
126 [ # # ]: 0 : g_clear_pointer (&self->map_sources, g_ptr_array_unref);
127 [ # # ]: 0 : g_clear_pointer (&self->extra_text_label, gtk_widget_unparent);
128 [ # # ]: 0 : g_clear_pointer (&self->license_label, gtk_widget_unparent);
129 : :
130 : 0 : G_OBJECT_CLASS (shumate_license_parent_class)->dispose (object);
131 : 0 : }
132 : :
133 : : static void
134 : 1 : shumate_license_class_init (ShumateLicenseClass *klass)
135 : : {
136 : 1 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
137 : 1 : GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
138 : 1 : GdkDisplay *display;
139 : :
140 : 1 : object_class->dispose = shumate_license_dispose;
141 : 1 : object_class->get_property = shumate_license_get_property;
142 : 1 : 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 : 2 : obj_properties[PROP_EXTRA_TEXT] =
152 : 1 : 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 : 2 : obj_properties[PROP_XALIGN] =
164 : 1 : 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 : 1 : g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties);
171 : :
172 : 1 : gtk_widget_class_set_css_name (widget_class, "map-license");
173 : 1 : gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
174 : :
175 : 1 : display = gdk_display_get_default ();
176 [ - + ]: 1 : if (display)
177 : : {
178 : 0 : g_autoptr(GtkCssProvider) provider = gtk_css_provider_new ();
179 : 0 : gtk_css_provider_load_from_resource (provider, "/org/gnome/shumate/license.css");
180 [ # # ]: 0 : gtk_style_context_add_provider_for_display (display,
181 : : GTK_STYLE_PROVIDER (provider),
182 : : GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
183 : : }
184 : 1 : }
185 : :
186 : :
187 : : static void
188 : 0 : shumate_license_init (ShumateLicense *self)
189 : : {
190 : 0 : self->map_sources = g_ptr_array_new_with_free_func (g_object_unref);
191 : 0 : self->license_label = gtk_label_new (NULL);
192 : 0 : self->extra_text_label = gtk_label_new (NULL);
193 : :
194 : 0 : g_object_set (gtk_widget_get_layout_manager (GTK_WIDGET (self)),
195 : : "orientation", GTK_ORIENTATION_VERTICAL,
196 : : NULL);
197 : 0 : g_object_set (self->license_label,
198 : : "wrap", TRUE,
199 : : "xalign", 1.0f,
200 : : NULL);
201 : 0 : g_object_set (self->extra_text_label,
202 : : "visible", FALSE,
203 : : "wrap", TRUE,
204 : : "xalign", 1.0f,
205 : : NULL);
206 : :
207 : 0 : gtk_widget_insert_after (self->license_label, GTK_WIDGET (self), NULL);
208 : 0 : gtk_widget_insert_after (self->extra_text_label, GTK_WIDGET (self), self->license_label);
209 : 0 : }
210 : :
211 : :
212 : : /**
213 : : * shumate_license_new:
214 : : *
215 : : * Creates an instance of #ShumateLicense.
216 : : *
217 : : * Returns: a new #ShumateLicense.
218 : : */
219 : : ShumateLicense *
220 : 0 : shumate_license_new (void)
221 : : {
222 : 0 : 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 : 0 : shumate_license_set_extra_text (ShumateLicense *license,
235 : : const char *text)
236 : : {
237 [ # # ]: 0 : g_return_if_fail (SHUMATE_IS_LICENSE (license));
238 : :
239 : 0 : gtk_label_set_label (GTK_LABEL (license->extra_text_label), text);
240 : 0 : gtk_widget_set_visible (license->extra_text_label, text != NULL);
241 : :
242 : 0 : 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 : 0 : shumate_license_get_extra_text (ShumateLicense *license)
256 : : {
257 [ # # ]: 0 : g_return_val_if_fail (SHUMATE_IS_LICENSE (license), NULL);
258 : :
259 : 0 : 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 : 0 : shumate_license_set_xalign (ShumateLicense *license,
272 : : float xalign)
273 : : {
274 [ # # ]: 0 : g_return_if_fail (SHUMATE_IS_LICENSE (license));
275 : :
276 : 0 : gtk_label_set_xalign (GTK_LABEL (license->license_label), xalign);
277 : 0 : gtk_label_set_xalign (GTK_LABEL (license->extra_text_label), xalign);
278 : :
279 : 0 : 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 : 0 : shumate_license_get_xalign (ShumateLicense *license)
293 : : {
294 [ # # ]: 0 : g_return_val_if_fail (SHUMATE_IS_LICENSE (license), 1.0f);
295 : :
296 : 0 : return gtk_label_get_xalign (GTK_LABEL (license->license_label));
297 : : }
298 : :
299 : : void
300 : 0 : shumate_license_append_map_source (ShumateLicense *license,
301 : : ShumateMapSource *map_source)
302 : : {
303 [ # # ]: 0 : g_return_if_fail (SHUMATE_IS_LICENSE (license));
304 : :
305 : 0 : g_ptr_array_add (license->map_sources, g_object_ref (map_source));
306 : 0 : shumate_license_sources_changed (license);
307 : : }
308 : :
309 : : void
310 : 0 : shumate_license_prepend_map_source (ShumateLicense *license,
311 : : ShumateMapSource *map_source)
312 : : {
313 [ # # ]: 0 : g_return_if_fail (SHUMATE_IS_LICENSE (license));
314 : :
315 : 0 : g_ptr_array_insert (license->map_sources, 0, g_object_ref (map_source));
316 : 0 : shumate_license_sources_changed (license);
317 : : }
318 : :
319 : : void
320 : 0 : shumate_license_remove_map_source (ShumateLicense *license,
321 : : ShumateMapSource *map_source)
322 : : {
323 [ # # ]: 0 : g_return_if_fail (SHUMATE_IS_LICENSE (license));
324 : :
325 : 0 : g_ptr_array_remove (license->map_sources, map_source);
326 : 0 : shumate_license_sources_changed (license);
327 : : }
|