| 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 |
#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 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 160 |
|
| 161 |
|
| 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 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
ShumateLicense * |
| 220 |
shumate_license_new (void) |
| 221 |
{ |
| 222 |
return SHUMATE_LICENSE (g_object_new (SHUMATE_TYPE_LICENSE, NULL)); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 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 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 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 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 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 |
} |