| 1 |
/* font-manager-preview-controls.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-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-preview-controls.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-preview-controls |
| 25 |
* @short_description: Font preview controls |
| 26 |
* @title: Preview Controls |
| 27 |
* @include: font-manager-preview-controls.h |
| 28 |
* |
| 29 |
* Widget which provides controls for setting preview text justification, |
| 30 |
* enabling editing, resetting the preview back to its default state and |
| 31 |
* also displays a description of the current font. |
| 32 |
*/ |
| 33 |
|
| 34 |
struct _FontManagerPreviewControls |
| 35 |
{ |
| 36 |
GtkWidget parent; |
| 37 |
|
| 38 |
GtkWidget *description; |
| 39 |
GtkWidget *undo_button; |
| 40 |
|
| 41 |
GtkJustification justification; |
| 42 |
}; |
| 43 |
|
| 44 |
G_DEFINE_TYPE(FontManagerPreviewControls, font_manager_preview_controls, GTK_TYPE_WIDGET) |
| 45 |
|
| 46 |
enum |
| 47 |
{ |
| 48 |
EDIT_TOGGLED, |
| 49 |
UNDO_CLICKED, |
| 50 |
N_SIGNALS |
| 51 |
}; |
| 52 |
|
| 53 |
enum |
| 54 |
{ |
| 55 |
PROP_RESERVED, |
| 56 |
PROP_FONT, |
| 57 |
PROP_DESCRIPTION, |
| 58 |
PROP_JUSTIFICATION, |
| 59 |
PROP_UNDO_AVAILABLE, |
| 60 |
N_PROPERTIES |
| 61 |
}; |
| 62 |
|
| 63 |
static guint signals[N_SIGNALS]; |
| 64 |
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
| 65 |
|
| 66 |
static const struct |
| 67 |
{ |
| 68 |
const gchar *name; |
| 69 |
const gchar *tooltip_text; |
| 70 |
const gchar *icon_name; |
| 71 |
const GtkJustification justification; |
| 72 |
} |
| 73 |
JustificationControls [] = |
| 74 |
{ |
| 75 |
{ |
| 76 |
"left", |
| 77 |
N_("Left Aligned"), |
| 78 |
"format-justify-left-symbolic", |
| 79 |
GTK_JUSTIFY_LEFT |
| 80 |
}, |
| 81 |
{ |
| 82 |
"center", |
| 83 |
N_("Centered"), |
| 84 |
"format-justify-center-symbolic", |
| 85 |
GTK_JUSTIFY_CENTER |
| 86 |
}, |
| 87 |
{ |
| 88 |
"fill", |
| 89 |
N_("Fill"), |
| 90 |
"format-justify-fill-symbolic", |
| 91 |
GTK_JUSTIFY_FILL |
| 92 |
}, |
| 93 |
{ |
| 94 |
"right", |
| 95 |
N_("Right Aligned"), |
| 96 |
"format-justify-right-symbolic", |
| 97 |
GTK_JUSTIFY_RIGHT |
| 98 |
} |
| 99 |
}; |
| 100 |
|
| 101 |
static void |
| 102 |
font_manager_preview_controls_dispose (GObject *gobject) |
| 103 |
{ |
| 104 |
font_manager_widget_dispose(GTK_WIDGET(gobject)); |
| 105 |
G_OBJECT_CLASS(font_manager_preview_controls_parent_class)->dispose(gobject); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
static void |
| 110 |
set_font (FontManagerPreviewControls *self, |
| 111 |
FontManagerFont *font) |
| 112 |
{ |
| 113 |
if (font) { |
| 114 |
g_autofree gchar *description = NULL; |
| 115 |
g_object_get(font, "description", &description, NULL); |
| 116 |
gtk_label_set_text(GTK_LABEL(self->description), description); |
| 117 |
} |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
static void |
| 122 |
font_manager_preview_controls_get_property (GObject *gobject, |
| 123 |
guint property_id, |
| 124 |
GValue *value, |
| 125 |
GParamSpec *pspec) |
| 126 |
{ |
| 127 |
FontManagerPreviewControls *self = FONT_MANAGER_PREVIEW_CONTROLS(gobject); |
| 128 |
g_return_if_fail(self != NULL); |
| 129 |
switch (property_id) { |
| 130 |
case PROP_UNDO_AVAILABLE: |
| 131 |
g_value_set_boolean(value, gtk_widget_get_sensitive(self->undo_button)); |
| 132 |
break; |
| 133 |
case PROP_JUSTIFICATION: |
| 134 |
g_value_set_enum(value, (gint) self->justification); |
| 135 |
break; |
| 136 |
default: |
| 137 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 138 |
} |
| 139 |
return; |
| 140 |
} |
| 141 |
|
| 142 |
static void |
| 143 |
font_manager_preview_controls_set_property (GObject *gobject, |
| 144 |
guint property_id, |
| 145 |
const GValue *value, |
| 146 |
GParamSpec *pspec) |
| 147 |
{ |
| 148 |
FontManagerPreviewControls *self = FONT_MANAGER_PREVIEW_CONTROLS(gobject); |
| 149 |
g_return_if_fail(self != NULL); |
| 150 |
switch (property_id) { |
| 151 |
case PROP_FONT: |
| 152 |
set_font(self, g_value_get_object(value)); |
| 153 |
g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_FONT]); |
| 154 |
break; |
| 155 |
case PROP_DESCRIPTION: |
| 156 |
gtk_label_set_text(GTK_LABEL(self->description), g_value_get_string(value)); |
| 157 |
g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_DESCRIPTION]); |
| 158 |
break; |
| 159 |
case PROP_UNDO_AVAILABLE: |
| 160 |
gtk_widget_set_sensitive(self->undo_button, g_value_get_boolean(value)); |
| 161 |
break; |
| 162 |
case PROP_JUSTIFICATION: |
| 163 |
self->justification = (GtkJustification) g_value_get_enum(value); |
| 164 |
static const gchar *justify [4] = { "left", "right", "center", "fill" }; |
| 165 |
gpointer toggle = g_object_get_data(gobject, justify[(gint) self->justification]); |
| 166 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(toggle), TRUE); |
| 167 |
g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_JUSTIFICATION]); |
| 168 |
break; |
| 169 |
default: |
| 170 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 171 |
} |
| 172 |
return; |
| 173 |
} |
| 174 |
|
| 175 |
static void |
| 176 |
font_manager_preview_controls_class_init (FontManagerPreviewControlsClass *klass) |
| 177 |
{ |
| 178 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 179 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| 180 |
|
| 181 |
object_class->dispose = font_manager_preview_controls_dispose; |
| 182 |
object_class->get_property = font_manager_preview_controls_get_property; |
| 183 |
object_class->set_property = font_manager_preview_controls_set_property; |
| 184 |
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_CENTER_LAYOUT); |
| 185 |
|
| 186 |
/** |
| 187 |
* FontManagerPreviewControls:font: |
| 188 |
* |
| 189 |
* #FontManagerFont for currently displayed font. |
| 190 |
*/ |
| 191 |
obj_properties[PROP_FONT] = g_param_spec_object("font", |
| 192 |
NULL, |
| 193 |
"FontManagerFont", |
| 194 |
FONT_MANAGER_TYPE_FONT, |
| 195 |
G_PARAM_WRITABLE | |
| 196 |
G_PARAM_STATIC_STRINGS); |
| 197 |
|
| 198 |
/** |
| 199 |
* FontManagerPreviewControls:description: |
| 200 |
* |
| 201 |
* Currently displayed font description string |
| 202 |
*/ |
| 203 |
obj_properties[PROP_DESCRIPTION] = g_param_spec_string("description", |
| 204 |
NULL, |
| 205 |
"Font description", |
| 206 |
NULL, |
| 207 |
G_PARAM_STATIC_STRINGS | |
| 208 |
G_PARAM_WRITABLE | |
| 209 |
G_PARAM_EXPLICIT_NOTIFY); |
| 210 |
|
| 211 |
/** |
| 212 |
* FontManagerPreviewControls:undo-available: |
| 213 |
* |
| 214 |
* Whether the undo button should be available or not. |
| 215 |
*/ |
| 216 |
obj_properties[PROP_UNDO_AVAILABLE] = g_param_spec_boolean("undo-available", |
| 217 |
NULL, |
| 218 |
"Whether the undo button should be available", |
| 219 |
FALSE, |
| 220 |
G_PARAM_STATIC_STRINGS | |
| 221 |
G_PARAM_READWRITE); |
| 222 |
|
| 223 |
/** |
| 224 |
* FontManagerPreviewControls:justification: |
| 225 |
* |
| 226 |
* Preview text justification. |
| 227 |
*/ |
| 228 |
obj_properties[PROP_JUSTIFICATION] = g_param_spec_enum("justification", |
| 229 |
NULL, |
| 230 |
"Preview text justification.", |
| 231 |
GTK_TYPE_JUSTIFICATION, |
| 232 |
GTK_JUSTIFY_CENTER, |
| 233 |
G_PARAM_STATIC_STRINGS | |
| 234 |
G_PARAM_READWRITE | |
| 235 |
G_PARAM_EXPLICIT_NOTIFY); |
| 236 |
|
| 237 |
/** |
| 238 |
* FontManagerPreviewControls:edit-toggled: |
| 239 |
* |
| 240 |
* Emitted whenever the edit button has been toggled. |
| 241 |
*/ |
| 242 |
signals[EDIT_TOGGLED] = g_signal_new("edit-toggled", |
| 243 |
FONT_MANAGER_TYPE_PREVIEW_CONTROLS, |
| 244 |
G_SIGNAL_RUN_FIRST, |
| 245 |
0, |
| 246 |
NULL, |
| 247 |
NULL, |
| 248 |
NULL, |
| 249 |
G_TYPE_NONE, |
| 250 |
1, |
| 251 |
G_TYPE_BOOLEAN); |
| 252 |
|
| 253 |
/** |
| 254 |
* FontManagerPreviewControls:undo-clicked: |
| 255 |
* |
| 256 |
* Emitted whenever the undo button has been clicked. |
| 257 |
*/ |
| 258 |
signals[UNDO_CLICKED] = g_signal_new("undo-clicked", |
| 259 |
FONT_MANAGER_TYPE_PREVIEW_CONTROLS, |
| 260 |
G_SIGNAL_RUN_FIRST, |
| 261 |
0, |
| 262 |
NULL, |
| 263 |
NULL, |
| 264 |
NULL, |
| 265 |
G_TYPE_NONE, |
| 266 |
0); |
| 267 |
|
| 268 |
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties); |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
on_justification_set (FontManagerPreviewControls *self, GtkToggleButton *button) |
| 274 |
{ |
| 275 |
if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(button))) { |
| 276 |
int index = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(button), "index")); |
| 277 |
GtkJustification justification = JustificationControls[index].justification; |
| 278 |
self->justification = justification; |
| 279 |
g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_JUSTIFICATION]); |
| 280 |
} |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
static void |
| 285 |
on_edit_toggled (FontManagerPreviewControls *self, GtkToggleButton *button) |
| 286 |
{ |
| 287 |
gboolean active = gtk_toggle_button_get_active(button); |
| 288 |
g_signal_emit(self, signals[EDIT_TOGGLED], 0, active); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
static void |
| 293 |
on_undo_clicked (FontManagerPreviewControls *self, GtkButton *button) |
| 294 |
{ |
| 295 |
g_signal_emit(self, signals[UNDO_CLICKED], 0); |
| 296 |
return; |
| 297 |
} |
| 298 |
|
| 299 |
static void |
| 300 |
set_start_widget (FontManagerPreviewControls *self, GtkCenterLayout *layout) |
| 301 |
{ |
| 302 |
g_return_if_fail(self != NULL); |
| 303 |
GtkWidget *start = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2); |
| 304 |
GtkWidget *last = NULL; |
| 305 |
for (gint i = 0; i < G_N_ELEMENTS(JustificationControls); i++) { |
| 306 |
GtkWidget *widget = gtk_toggle_button_new(); |
| 307 |
gtk_toggle_button_set_group(GTK_TOGGLE_BUTTON(widget), GTK_TOGGLE_BUTTON(last)); |
| 308 |
GtkWidget *icon = gtk_image_new_from_icon_name(JustificationControls[i].icon_name); |
| 309 |
gtk_button_set_child(GTK_BUTTON(widget), icon); |
| 310 |
gtk_button_set_has_frame(GTK_BUTTON(widget), FALSE); |
| 311 |
gtk_widget_set_tooltip_text(widget, gettext(JustificationControls[i].tooltip_text)); |
| 312 |
gtk_box_append(GTK_BOX(start), widget); |
| 313 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), (i == 1)); |
| 314 |
g_object_set_data(G_OBJECT(widget), "index", GINT_TO_POINTER(i)); |
| 315 |
g_signal_connect_swapped(widget, "toggled", G_CALLBACK(on_justification_set), self); |
| 316 |
g_object_set_data(G_OBJECT(self), JustificationControls[i].name, widget); |
| 317 |
last = widget; |
| 318 |
} |
| 319 |
font_manager_widget_set_margin(start, FONT_MANAGER_MIN_MARGIN); |
| 320 |
gtk_widget_set_parent(start, GTK_WIDGET(self)); |
| 321 |
gtk_center_layout_set_start_widget(layout, start); |
| 322 |
return; |
| 323 |
} |
| 324 |
|
| 325 |
static void |
| 326 |
set_center_widget (FontManagerPreviewControls *self, GtkCenterLayout *layout) |
| 327 |
{ |
| 328 |
g_return_if_fail(self != NULL); |
| 329 |
self->description = gtk_label_new("<FontDescription>"); |
| 330 |
gtk_widget_add_css_class(self->description, FONT_MANAGER_STYLE_CLASS_DIM_LABEL); |
| 331 |
gtk_widget_set_parent(self->description, GTK_WIDGET(self)); |
| 332 |
gtk_center_layout_set_center_widget(layout, self->description); |
| 333 |
return; |
| 334 |
} |
| 335 |
|
| 336 |
static void |
| 337 |
set_end_widget (FontManagerPreviewControls *self, GtkCenterLayout *layout) |
| 338 |
{ |
| 339 |
GtkWidget *end = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 2); |
| 340 |
GtkWidget *edit = gtk_toggle_button_new(); |
| 341 |
GtkWidget *edit_icon = gtk_image_new_from_icon_name("document-edit-symbolic"); |
| 342 |
self->undo_button = gtk_button_new(); |
| 343 |
GtkWidget *undo_icon = gtk_image_new_from_icon_name("edit-undo-symbolic"); |
| 344 |
gtk_button_set_child(GTK_BUTTON(edit), edit_icon); |
| 345 |
gtk_button_set_child(GTK_BUTTON(self->undo_button), undo_icon); |
| 346 |
gtk_widget_set_sensitive(self->undo_button, FALSE); |
| 347 |
gtk_button_set_has_frame(GTK_BUTTON(edit), FALSE); |
| 348 |
gtk_button_set_has_frame(GTK_BUTTON(self->undo_button), FALSE); |
| 349 |
gtk_widget_set_tooltip_text(edit, _("Edit preview text")); |
| 350 |
gtk_widget_set_tooltip_text(self->undo_button, _("Undo changes")); |
| 351 |
gtk_box_append(GTK_BOX(end), edit); |
| 352 |
gtk_box_append(GTK_BOX(end), self->undo_button); |
| 353 |
gtk_widget_set_parent(end, GTK_WIDGET(self)); |
| 354 |
gtk_center_layout_set_end_widget(layout, end); |
| 355 |
font_manager_widget_set_margin(end, FONT_MANAGER_MIN_MARGIN); |
| 356 |
g_signal_connect_swapped(edit, "toggled", G_CALLBACK(on_edit_toggled), self); |
| 357 |
g_signal_connect_swapped(self->undo_button, "clicked", G_CALLBACK(on_undo_clicked), self); |
| 358 |
return; |
| 359 |
} |
| 360 |
|
| 361 |
static void |
| 362 |
font_manager_preview_controls_init (FontManagerPreviewControls *self) |
| 363 |
{ |
| 364 |
GtkLayoutManager *layout = gtk_widget_get_layout_manager(GTK_WIDGET(self)); |
| 365 |
set_start_widget(self, GTK_CENTER_LAYOUT(layout)); |
| 366 |
set_center_widget(self, GTK_CENTER_LAYOUT(layout)); |
| 367 |
set_end_widget(self, GTK_CENTER_LAYOUT(layout)); |
| 368 |
gtk_widget_add_css_class(GTK_WIDGET(self), FONT_MANAGER_STYLE_CLASS_VIEW); |
| 369 |
gtk_widget_set_valign(GTK_WIDGET(self), GTK_ALIGN_START); |
| 370 |
gtk_widget_set_hexpand(GTK_WIDGET(self), TRUE); |
| 371 |
font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerPreviewControls"); |
| 372 |
return; |
| 373 |
} |
| 374 |
|
| 375 |
/** |
| 376 |
* font_manager_preview_controls_new: |
| 377 |
* |
| 378 |
* Returns: (transfer full): A newly created #FontManagerPreviewControls. |
| 379 |
* Free the returned object using #g_object_unref(). |
| 380 |
*/ |
| 381 |
GtkWidget * |
| 382 |
font_manager_preview_controls_new (void) |
| 383 |
{ |
| 384 |
return g_object_new(FONT_MANAGER_TYPE_PREVIEW_CONTROLS, NULL); |
| 385 |
} |