| 1 |
/* font-manager-preference-row.c |
| 2 |
* |
| 3 |
* Copyright (C) 2020-2023 Jerry Casiano |
| 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. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-preference-row.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-preference-row |
| 25 |
* @short_description: Preference row widget |
| 26 |
* @title: Preference Row |
| 27 |
* @include: font-manager-preference-row.h |
| 28 |
* |
| 29 |
* Convenience class to allow quickly creating consistent application preference rows. |
| 30 |
*/ |
| 31 |
|
| 32 |
struct _FontManagerPreferenceRow |
| 33 |
{ |
| 34 |
GtkWidget parent; |
| 35 |
|
| 36 |
GtkWidget *icon; |
| 37 |
GtkWidget *title; |
| 38 |
GtkWidget *subtitle; |
| 39 |
GtkWidget *control; |
| 40 |
GtkWidget *revealer; |
| 41 |
GtkWidget *children; |
| 42 |
}; |
| 43 |
|
| 44 |
G_DEFINE_TYPE(FontManagerPreferenceRow, font_manager_preference_row, GTK_TYPE_WIDGET) |
| 45 |
|
| 46 |
enum |
| 47 |
{ |
| 48 |
PROP_RESERVED, |
| 49 |
PROP_ICON_NAME, |
| 50 |
PROP_TITLE, |
| 51 |
PROP_SUBTITLE, |
| 52 |
PROP_CONTROL, |
| 53 |
N_PROPERTIES |
| 54 |
}; |
| 55 |
|
| 56 |
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
| 57 |
|
| 58 |
static void |
| 59 |
font_manager_preference_row_dispose (GObject *gobject) |
| 60 |
{ |
| 61 |
FontManagerPreferenceRow *self = FONT_MANAGER_PREFERENCE_ROW(gobject); |
| 62 |
g_return_if_fail(self != NULL); |
| 63 |
font_manager_widget_dispose(GTK_WIDGET(gobject)); |
| 64 |
G_OBJECT_CLASS(font_manager_preference_row_parent_class)->dispose(gobject); |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
static void |
| 69 |
font_manager_preference_row_set_label (GtkLabel *_label, const gchar *msg) |
| 70 |
{ |
| 71 |
gtk_label_set_text(_label, msg); |
| 72 |
gtk_widget_set_visible(GTK_WIDGET(_label), strlen(gtk_label_get_text(_label)) > 0); |
| 73 |
return; |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
font_manager_preference_row_set_icon_name (FontManagerPreferenceRow *self, |
| 78 |
const gchar *icon_name) |
| 79 |
{ |
| 80 |
gtk_image_set_from_icon_name(GTK_IMAGE(self->icon), icon_name); |
| 81 |
gtk_widget_set_visible(GTK_WIDGET(self->icon), icon_name != NULL); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
static void |
| 86 |
font_manager_preference_row_update_title_alignment (FontManagerPreferenceRow *self) |
| 87 |
{ |
| 88 |
gboolean icon_visible = gtk_widget_get_visible(self->icon); |
| 89 |
gboolean subtitle_visible = gtk_widget_get_visible(self->subtitle); |
| 90 |
if (!icon_visible || !subtitle_visible) { |
| 91 |
gtk_widget_set_halign(self->title, GTK_ALIGN_START); |
| 92 |
gtk_widget_set_valign(self->title, GTK_ALIGN_CENTER); |
| 93 |
} else { |
| 94 |
gtk_widget_set_halign(self->title, GTK_ALIGN_START); |
| 95 |
gtk_widget_set_valign(self->title, GTK_ALIGN_END); |
| 96 |
} |
| 97 |
return; |
| 98 |
} |
| 99 |
|
| 100 |
static void |
| 101 |
font_manager_preference_row_get_property (GObject *gobject, |
| 102 |
guint property_id, |
| 103 |
GValue *value, |
| 104 |
GParamSpec *pspec) |
| 105 |
{ |
| 106 |
g_return_if_fail(gobject != NULL); |
| 107 |
FontManagerPreferenceRow *self = FONT_MANAGER_PREFERENCE_ROW(gobject); |
| 108 |
g_autofree gchar *icon_name = NULL; |
| 109 |
switch (property_id) { |
| 110 |
case PROP_ICON_NAME: |
| 111 |
g_value_set_string(value, gtk_image_get_icon_name(GTK_IMAGE(self->icon))); |
| 112 |
break; |
| 113 |
case PROP_TITLE: |
| 114 |
g_value_set_string(value, gtk_label_get_text(GTK_LABEL(self->title))); |
| 115 |
break; |
| 116 |
case PROP_SUBTITLE: |
| 117 |
g_value_set_string(value, gtk_label_get_text(GTK_LABEL(self->subtitle))); |
| 118 |
break; |
| 119 |
default: |
| 120 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
font_manager_preference_row_set_property (GObject *gobject, |
| 127 |
guint property_id, |
| 128 |
const GValue *value, |
| 129 |
GParamSpec *pspec) |
| 130 |
{ |
| 131 |
g_return_if_fail(gobject != NULL); |
| 132 |
FontManagerPreferenceRow *self = FONT_MANAGER_PREFERENCE_ROW(gobject); |
| 133 |
gboolean string_type = pspec->value_type == G_TYPE_STRING; |
| 134 |
const gchar *val = string_type ? g_value_get_string(value) : NULL; |
| 135 |
switch (property_id) { |
| 136 |
case PROP_ICON_NAME: |
| 137 |
font_manager_preference_row_set_icon_name(self, val); |
| 138 |
font_manager_preference_row_update_title_alignment(self); |
| 139 |
break; |
| 140 |
case PROP_TITLE: |
| 141 |
font_manager_preference_row_set_label(GTK_LABEL(self->title), val); |
| 142 |
font_manager_preference_row_update_title_alignment(self); |
| 143 |
break; |
| 144 |
case PROP_SUBTITLE: |
| 145 |
font_manager_preference_row_set_label(GTK_LABEL(self->subtitle), val); |
| 146 |
font_manager_preference_row_update_title_alignment(self); |
| 147 |
break; |
| 148 |
case PROP_CONTROL: |
| 149 |
font_manager_preference_row_set_action_widget(self, g_value_get_object(value)); |
| 150 |
break; |
| 151 |
default: |
| 152 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 153 |
} |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
static void |
| 158 |
font_manager_preference_row_class_init (FontManagerPreferenceRowClass *klass) |
| 159 |
{ |
| 160 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 161 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| 162 |
|
| 163 |
object_class->dispose = font_manager_preference_row_dispose; |
| 164 |
object_class->get_property = font_manager_preference_row_get_property; |
| 165 |
object_class->set_property = font_manager_preference_row_set_property; |
| 166 |
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BOX_LAYOUT); |
| 167 |
|
| 168 |
/** |
| 169 |
* FontManagerPreferenceRow:icon-name: |
| 170 |
* |
| 171 |
* Named icon to display. |
| 172 |
*/ |
| 173 |
|
| 174 |
obj_properties[PROP_ICON_NAME] = g_param_spec_string("icon-name", |
| 175 |
NULL, |
| 176 |
"Named icon to display", |
| 177 |
NULL, |
| 178 |
G_PARAM_STATIC_STRINGS | |
| 179 |
G_PARAM_READWRITE); |
| 180 |
|
| 181 |
/** |
| 182 |
* FontManagerPreferenceRow:title: |
| 183 |
* |
| 184 |
* Name of preference. |
| 185 |
*/ |
| 186 |
|
| 187 |
obj_properties[PROP_TITLE] = g_param_spec_string("title", |
| 188 |
NULL, |
| 189 |
"Preference name", |
| 190 |
NULL, |
| 191 |
G_PARAM_STATIC_STRINGS | |
| 192 |
G_PARAM_READWRITE); |
| 193 |
|
| 194 |
/** |
| 195 |
* FontManagerPreferenceRow:subtitle: |
| 196 |
* |
| 197 |
* Short description of preference. |
| 198 |
*/ |
| 199 |
|
| 200 |
obj_properties[PROP_SUBTITLE] = g_param_spec_string("subtitle", |
| 201 |
NULL, |
| 202 |
"Subtitle to display under title", |
| 203 |
NULL, |
| 204 |
G_PARAM_STATIC_STRINGS | |
| 205 |
G_PARAM_READWRITE); |
| 206 |
|
| 207 |
/** |
| 208 |
* FontManagerPreferenceRow:action-widget: |
| 209 |
* |
| 210 |
* Widget to control preference state. |
| 211 |
*/ |
| 212 |
obj_properties[PROP_CONTROL] = g_param_spec_object("action-widget", |
| 213 |
NULL, |
| 214 |
"Widget to control preference state", |
| 215 |
GTK_TYPE_WIDGET, |
| 216 |
G_PARAM_STATIC_STRINGS | |
| 217 |
G_PARAM_WRITABLE); |
| 218 |
|
| 219 |
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
static void |
| 224 |
set_title_attributes (GtkWidget *widget) |
| 225 |
{ |
| 226 |
PangoAttrList *attrs = pango_attr_list_new(); |
| 227 |
pango_attr_list_insert(attrs, pango_attr_scale_new(PANGO_SCALE_MEDIUM)); |
| 228 |
gtk_label_set_attributes(GTK_LABEL(widget), attrs); |
| 229 |
pango_attr_list_unref(attrs); |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
static void |
| 234 |
set_subtitle_attributes (GtkWidget *widget) |
| 235 |
{ |
| 236 |
PangoAttrList *attrs = pango_attr_list_new(); |
| 237 |
pango_attr_list_insert(attrs, pango_attr_scale_new(PANGO_SCALE_SMALL)); |
| 238 |
gtk_label_set_attributes(GTK_LABEL(widget), attrs); |
| 239 |
gtk_widget_add_css_class(widget, "dim-label"); |
| 240 |
pango_attr_list_unref(attrs); |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
insert_widget (GtkGrid *grid, |
| 246 |
GtkWidget *widget, |
| 247 |
GtkAlign halign, |
| 248 |
GtkAlign valign, |
| 249 |
int column, |
| 250 |
int row, |
| 251 |
int width, |
| 252 |
int height, |
| 253 |
gboolean expand) |
| 254 |
{ |
| 255 |
gtk_widget_set_halign(widget, halign); |
| 256 |
gtk_widget_set_valign(widget, valign); |
| 257 |
font_manager_widget_set_margin(widget, FONT_MANAGER_DEFAULT_MARGIN); |
| 258 |
font_manager_widget_set_expand(widget, expand); |
| 259 |
gtk_grid_attach(grid, widget, column, row, width, height); |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
static void |
| 264 |
font_manager_preference_row_init (FontManagerPreferenceRow *self) |
| 265 |
{ |
| 266 |
g_return_if_fail(self != NULL); |
| 267 |
GtkWidget *container = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 268 |
self->revealer = gtk_revealer_new(); |
| 269 |
gtk_revealer_set_transition_duration(GTK_REVEALER(self->revealer), 500); |
| 270 |
GtkWidget *inner_container = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 271 |
self->control = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); |
| 272 |
self->children = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 273 |
gtk_widget_set_margin_start(self->children, FONT_MANAGER_DEFAULT_MARGIN * 3); |
| 274 |
gtk_widget_set_margin_end(self->children, FONT_MANAGER_DEFAULT_MARGIN * 3); |
| 275 |
gtk_widget_set_margin_top(self->children, FONT_MANAGER_DEFAULT_MARGIN * 2); |
| 276 |
gtk_widget_set_margin_bottom(self->children, 0); |
| 277 |
gtk_revealer_set_child(GTK_REVEALER(self->revealer), self->children); |
| 278 |
self->icon = gtk_image_new(); |
| 279 |
gtk_image_set_icon_size(GTK_IMAGE(self->icon), GTK_ICON_SIZE_LARGE); |
| 280 |
self->title = gtk_label_new(NULL); |
| 281 |
set_title_attributes(self->title); |
| 282 |
self->subtitle = gtk_label_new(NULL); |
| 283 |
set_subtitle_attributes(self->subtitle); |
| 284 |
GtkWidget *grid = gtk_grid_new(); |
| 285 |
insert_widget(GTK_GRID(grid), self->icon, |
| 286 |
GTK_ALIGN_CENTER, GTK_ALIGN_CENTER, |
| 287 |
0, 0, 2, 2, FALSE); |
| 288 |
insert_widget(GTK_GRID(grid), self->title, |
| 289 |
GTK_ALIGN_START, GTK_ALIGN_END, |
| 290 |
3, 0, 3, 1, TRUE); |
| 291 |
insert_widget(GTK_GRID(grid), self->subtitle, |
| 292 |
GTK_ALIGN_START, GTK_ALIGN_START, |
| 293 |
3, 1, 3, 1, TRUE); |
| 294 |
font_manager_widget_set_margin(self->icon, FONT_MANAGER_DEFAULT_MARGIN * 2); |
| 295 |
font_manager_widget_set_margin(GTK_WIDGET(self), FONT_MANAGER_DEFAULT_MARGIN * 2); |
| 296 |
font_manager_widget_set_expand(GTK_WIDGET(self), TRUE); |
| 297 |
gtk_box_append(GTK_BOX(inner_container), grid); |
| 298 |
gtk_box_append(GTK_BOX(inner_container), self->control); |
| 299 |
gtk_box_append(GTK_BOX(container), inner_container); |
| 300 |
gtk_box_append(GTK_BOX(container), self->revealer); |
| 301 |
gtk_widget_set_parent(container, GTK_WIDGET(self)); |
| 302 |
font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerPreferenceRow"); |
| 303 |
return; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* font_manager_preference_row_get_action_widget: |
| 308 |
* @self: #FontManagerPreferenceRow |
| 309 |
* |
| 310 |
* Returns: (transfer none) (nullable): The #GtkWidget set using |
| 311 |
* #font_manager_preference_row_get_action_widget or %NULL |
| 312 |
*/ |
| 313 |
GtkWidget * |
| 314 |
font_manager_preference_row_get_action_widget (FontManagerPreferenceRow *self) |
| 315 |
{ |
| 316 |
return gtk_widget_get_first_child(self->control); |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* font_manager_preference_row_set_action_widget: |
| 321 |
* @self: #FontManagerPreferenceRow |
| 322 |
* @control: (nullable): #GtkWidget |
| 323 |
* |
| 324 |
* Sets the action widget for @self. |
| 325 |
*/ |
| 326 |
void |
| 327 |
font_manager_preference_row_set_action_widget (FontManagerPreferenceRow *self, |
| 328 |
GtkWidget *control) |
| 329 |
{ |
| 330 |
g_return_if_fail(self != NULL); |
| 331 |
GtkWidget *child = gtk_widget_get_first_child(self->control); |
| 332 |
if (child) |
| 333 |
gtk_box_remove(GTK_BOX(self->control), child); |
| 334 |
if (control) { |
| 335 |
font_manager_widget_set_align(control, GTK_ALIGN_CENTER); |
| 336 |
font_manager_widget_set_margin(control, FONT_MANAGER_DEFAULT_MARGIN); |
| 337 |
gtk_box_append(GTK_BOX(self->control), control); |
| 338 |
} |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
static void |
| 343 |
on_state_set (GtkSwitch *control, |
| 344 |
gboolean state, |
| 345 |
FontManagerPreferenceRow *self) |
| 346 |
{ |
| 347 |
gboolean visible = gtk_switch_get_active(control); |
| 348 |
font_manager_preference_row_set_reveal_child(self, visible); |
| 349 |
return; |
| 350 |
} |
| 351 |
|
| 352 |
/** |
| 353 |
* font_manager_preference_row_append_child: |
| 354 |
* @parent: #FontManagerPreferenceRow |
| 355 |
* @child: #FontManagerPreferenceRow |
| 356 |
* |
| 357 |
* Appends @child to @parent. Children are typically preferences which are |
| 358 |
* dependent on the parents state and are therefore hidden by default. |
| 359 |
* |
| 360 |
* If the action widget set for @parent is a #GtkSwitch then children will |
| 361 |
* be revealed and concealed automatically when the widget is activated. |
| 362 |
* |
| 363 |
* Otherwise use #font_manager_preference_row_set_reveal_child to control |
| 364 |
* their visibility. |
| 365 |
*/ |
| 366 |
void |
| 367 |
font_manager_preference_row_append_child (FontManagerPreferenceRow *parent, |
| 368 |
FontManagerPreferenceRow *child) |
| 369 |
{ |
| 370 |
g_return_if_fail(parent != NULL); |
| 371 |
g_return_if_fail(child != NULL); |
| 372 |
gtk_box_append(GTK_BOX(parent->children), GTK_WIDGET(child)); |
| 373 |
font_manager_widget_set_margin(GTK_WIDGET(child), FONT_MANAGER_DEFAULT_MARGIN); |
| 374 |
GtkWidget *control = font_manager_preference_row_get_action_widget(parent); |
| 375 |
if (control && GTK_IS_SWITCH(control)) |
| 376 |
g_signal_connect_after(control, "notify::state", G_CALLBACK(on_state_set), parent); |
| 377 |
return; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* font_manager_preference_row_set_reveal_child: |
| 382 |
* @self: #FontManagerPreferenceRow |
| 383 |
* @visible: %TRUE to reveal child preferences |
| 384 |
*/ |
| 385 |
void |
| 386 |
font_manager_preference_row_set_reveal_child (FontManagerPreferenceRow *self, |
| 387 |
gboolean visible) |
| 388 |
{ |
| 389 |
g_return_if_fail(self != NULL); |
| 390 |
gtk_revealer_set_reveal_child(GTK_REVEALER(self->revealer), visible); |
| 391 |
if (visible) |
| 392 |
gtk_widget_set_margin_bottom(GTK_WIDGET(self), 0); |
| 393 |
else |
| 394 |
gtk_widget_set_margin_bottom(GTK_WIDGET(self), FONT_MANAGER_DEFAULT_MARGIN * 2); |
| 395 |
return; |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* font_manager_preference_row_new: |
| 400 |
* @title: Preference name |
| 401 |
* @subtitle: (nullable): Description of preference or %NULL |
| 402 |
* @icon_name: (nullable): Icon to display or %NULL |
| 403 |
* @action_widget: (nullable): #GtkWidget to control preference state or %NULL |
| 404 |
* |
| 405 |
* Returns: (transfer full): A newly created #FontManagerPreferenceRow. |
| 406 |
* Free the returned object using #g_object_unref(). |
| 407 |
*/ |
| 408 |
GtkWidget * |
| 409 |
font_manager_preference_row_new (const gchar *title, |
| 410 |
const gchar *subtitle, |
| 411 |
const gchar *icon_name, |
| 412 |
GtkWidget *action_widget) |
| 413 |
{ |
| 414 |
return g_object_new(FONT_MANAGER_TYPE_PREFERENCE_ROW, |
| 415 |
"title", title, |
| 416 |
"subtitle", subtitle, |
| 417 |
"icon-name", icon_name, |
| 418 |
"action-widget", action_widget, |
| 419 |
NULL); |
| 420 |
} |
| 421 |
|