shumate/shumate-license.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/6shumate_license_class_intern_init:
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1 time.
shumate_license_get_type:
✓ Branch 2 → 3 taken 1 time.
✓ Branch 2 → 7 taken 1 time.
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 7 not taken.
|
4 | 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 | 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", NULL, NULL, | |
| 153 | NULL, | ||
| 154 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); | ||
| 155 | |||
| 156 | /** | ||
| 157 | * ShumateLicense:xalign: | ||
| 158 | * | ||
| 159 | * The license's horizontal alignment | ||
| 160 | */ | ||
| 161 | 2 | obj_properties[PROP_XALIGN] = | |
| 162 | 1 | g_param_spec_float ("xalign", NULL, NULL, | |
| 163 | 0.0, 1.0, 0.5, | ||
| 164 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); | ||
| 165 | |||
| 166 | 1 | g_object_class_install_properties (object_class, N_PROPERTIES, obj_properties); | |
| 167 | |||
| 168 | 1 | gtk_widget_class_set_css_name (widget_class, "map-license"); | |
| 169 | 1 | gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT); | |
| 170 | |||
| 171 | 1 | display = gdk_display_get_default (); | |
| 172 |
1/2✗ Branch 9 → 10 not taken.
✓ Branch 9 → 15 taken 1 time.
|
1 | if (display) |
| 173 | { | ||
| 174 | ✗ | g_autoptr(GtkCssProvider) provider = gtk_css_provider_new (); | |
| 175 | ✗ | gtk_css_provider_load_from_resource (provider, "/org/gnome/shumate/license.css"); | |
| 176 | ✗ | gtk_style_context_add_provider_for_display (display, | |
| 177 | GTK_STYLE_PROVIDER (provider), | ||
| 178 | GTK_STYLE_PROVIDER_PRIORITY_FALLBACK); | ||
| 179 | } | ||
| 180 | 1 | } | |
| 181 | |||
| 182 | |||
| 183 | static void | ||
| 184 | ✗ | shumate_license_init (ShumateLicense *self) | |
| 185 | { | ||
| 186 | ✗ | self->map_sources = g_ptr_array_new_with_free_func (g_object_unref); | |
| 187 | ✗ | self->license_label = gtk_label_new (NULL); | |
| 188 | ✗ | self->extra_text_label = gtk_label_new (NULL); | |
| 189 | |||
| 190 | ✗ | g_object_set (gtk_widget_get_layout_manager (GTK_WIDGET (self)), | |
| 191 | "orientation", GTK_ORIENTATION_VERTICAL, | ||
| 192 | NULL); | ||
| 193 | ✗ | g_object_set (self->license_label, | |
| 194 | "wrap", TRUE, | ||
| 195 | "xalign", 1.0f, | ||
| 196 | NULL); | ||
| 197 | ✗ | g_object_set (self->extra_text_label, | |
| 198 | "visible", FALSE, | ||
| 199 | "wrap", TRUE, | ||
| 200 | "xalign", 1.0f, | ||
| 201 | NULL); | ||
| 202 | |||
| 203 | ✗ | gtk_widget_insert_after (self->license_label, GTK_WIDGET (self), NULL); | |
| 204 | ✗ | gtk_widget_insert_after (self->extra_text_label, GTK_WIDGET (self), self->license_label); | |
| 205 | ✗ | } | |
| 206 | |||
| 207 | |||
| 208 | /** | ||
| 209 | * shumate_license_new: | ||
| 210 | * | ||
| 211 | * Creates an instance of #ShumateLicense. | ||
| 212 | * | ||
| 213 | * Returns: a new #ShumateLicense. | ||
| 214 | */ | ||
| 215 | ShumateLicense * | ||
| 216 | ✗ | shumate_license_new (void) | |
| 217 | { | ||
| 218 | ✗ | return SHUMATE_LICENSE (g_object_new (SHUMATE_TYPE_LICENSE, NULL)); | |
| 219 | } | ||
| 220 | |||
| 221 | /** | ||
| 222 | * shumate_license_set_extra_text: | ||
| 223 | * @license: a #ShumateLicense | ||
| 224 | * @text: the additional license text | ||
| 225 | * | ||
| 226 | * Show the additional license text on the map view. The text will preceed the | ||
| 227 | * map's licence when displayed. Use "\n" to separate the lines. | ||
| 228 | */ | ||
| 229 | void | ||
| 230 | ✗ | shumate_license_set_extra_text (ShumateLicense *license, | |
| 231 | const char *text) | ||
| 232 | { | ||
| 233 | ✗ | g_return_if_fail (SHUMATE_IS_LICENSE (license)); | |
| 234 | |||
| 235 | ✗ | gtk_label_set_label (GTK_LABEL (license->extra_text_label), text); | |
| 236 | ✗ | gtk_widget_set_visible (license->extra_text_label, text != NULL); | |
| 237 | |||
| 238 | ✗ | g_object_notify_by_pspec (G_OBJECT (license), obj_properties[PROP_EXTRA_TEXT]); | |
| 239 | } | ||
| 240 | |||
| 241 | |||
| 242 | /** | ||
| 243 | * shumate_license_get_extra_text: | ||
| 244 | * @license: a #ShumateLicense | ||
| 245 | * | ||
| 246 | * Gets the additional license text. | ||
| 247 | * | ||
| 248 | * Returns: the additional license text | ||
| 249 | */ | ||
| 250 | const char * | ||
| 251 | ✗ | shumate_license_get_extra_text (ShumateLicense *license) | |
| 252 | { | ||
| 253 | ✗ | g_return_val_if_fail (SHUMATE_IS_LICENSE (license), NULL); | |
| 254 | |||
| 255 | ✗ | return gtk_label_get_label (GTK_LABEL (license->extra_text_label)); | |
| 256 | } | ||
| 257 | |||
| 258 | |||
| 259 | /** | ||
| 260 | * shumate_license_set_xalign: | ||
| 261 | * @license: a #ShumateLicense | ||
| 262 | * @xalign: The license's text horizontal alignment | ||
| 263 | * | ||
| 264 | * Set the license's text horizontal alignment. | ||
| 265 | */ | ||
| 266 | void | ||
| 267 | ✗ | shumate_license_set_xalign (ShumateLicense *license, | |
| 268 | float xalign) | ||
| 269 | { | ||
| 270 | ✗ | g_return_if_fail (SHUMATE_IS_LICENSE (license)); | |
| 271 | |||
| 272 | ✗ | gtk_label_set_xalign (GTK_LABEL (license->license_label), xalign); | |
| 273 | ✗ | gtk_label_set_xalign (GTK_LABEL (license->extra_text_label), xalign); | |
| 274 | |||
| 275 | ✗ | g_object_notify_by_pspec (G_OBJECT (license), obj_properties[PROP_XALIGN]); | |
| 276 | } | ||
| 277 | |||
| 278 | |||
| 279 | /** | ||
| 280 | * shumate_license_get_xalign: | ||
| 281 | * @license: The license | ||
| 282 | * | ||
| 283 | * Get the license's text horizontal alignment. | ||
| 284 | * | ||
| 285 | * Returns: the license's text horizontal alignment. | ||
| 286 | */ | ||
| 287 | float | ||
| 288 | ✗ | shumate_license_get_xalign (ShumateLicense *license) | |
| 289 | { | ||
| 290 | ✗ | g_return_val_if_fail (SHUMATE_IS_LICENSE (license), 1.0f); | |
| 291 | |||
| 292 | ✗ | return gtk_label_get_xalign (GTK_LABEL (license->license_label)); | |
| 293 | } | ||
| 294 | |||
| 295 | void | ||
| 296 | ✗ | shumate_license_append_map_source (ShumateLicense *license, | |
| 297 | ShumateMapSource *map_source) | ||
| 298 | { | ||
| 299 | ✗ | g_return_if_fail (SHUMATE_IS_LICENSE (license)); | |
| 300 | |||
| 301 | ✗ | g_ptr_array_add (license->map_sources, g_object_ref (map_source)); | |
| 302 | ✗ | shumate_license_sources_changed (license); | |
| 303 | } | ||
| 304 | |||
| 305 | void | ||
| 306 | ✗ | shumate_license_prepend_map_source (ShumateLicense *license, | |
| 307 | ShumateMapSource *map_source) | ||
| 308 | { | ||
| 309 | ✗ | g_return_if_fail (SHUMATE_IS_LICENSE (license)); | |
| 310 | |||
| 311 | ✗ | g_ptr_array_insert (license->map_sources, 0, g_object_ref (map_source)); | |
| 312 | ✗ | shumate_license_sources_changed (license); | |
| 313 | } | ||
| 314 | |||
| 315 | void | ||
| 316 | ✗ | shumate_license_remove_map_source (ShumateLicense *license, | |
| 317 | ShumateMapSource *map_source) | ||
| 318 | { | ||
| 319 | ✗ | g_return_if_fail (SHUMATE_IS_LICENSE (license)); | |
| 320 | |||
| 321 | ✗ | g_ptr_array_remove (license->map_sources, map_source); | |
| 322 | ✗ | shumate_license_sources_changed (license); | |
| 323 | } | ||
| 324 |