| 1 |
/* font-manager-preview-pane.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2025 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-pane.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-preview-pane |
| 25 |
* @short_description: Preview pane widget |
| 26 |
* @title: Preview Pane |
| 27 |
* @include: font-manager-preview-pane.h |
| 28 |
* |
| 29 |
* Full featured font preview widget. |
| 30 |
* |
| 31 |
* This widget combines several other widgets to provide as much information |
| 32 |
* about a particular font file as possible, previews, provided characters, |
| 33 |
* basic file properties and license information (if available). |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* font_manager_preview_pane_page_to_string: |
| 38 |
* @page: #FontManagerPreviewPanePage |
| 39 |
* |
| 40 |
* Returns: (transfer none) (nullable): @page as a string |
| 41 |
*/ |
| 42 |
const gchar * |
| 43 |
font_manager_preview_pane_page_to_string (FontManagerPreviewPanePage page) |
| 44 |
{ |
| 45 |
switch (page) { |
| 46 |
case FONT_MANAGER_PREVIEW_PANE_PAGE_CHARACTER_MAP: |
| 47 |
return _("Characters"); |
| 48 |
case FONT_MANAGER_PREVIEW_PANE_PAGE_PROPERTIES: |
| 49 |
return _("Properties"); |
| 50 |
case FONT_MANAGER_PREVIEW_PANE_PAGE_LICENSE: |
| 51 |
return _("License"); |
| 52 |
default: |
| 53 |
return NULL; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
GType |
| 58 |
font_manager_preview_pane_page_get_type (void) |
| 59 |
{ |
| 60 |
static gsize g_define_type_id__volatile = 0; |
| 61 |
|
| 62 |
if (g_once_init_enter (&g_define_type_id__volatile)) |
| 63 |
{ |
| 64 |
static const GEnumValue values[] = { |
| 65 |
{ FONT_MANAGER_PREVIEW_PANE_PAGE_PREVIEW, "FONT_MANAGER_PREVIEW_PANE_PAGE_PREVIEW", "preview" }, |
| 66 |
{ FONT_MANAGER_PREVIEW_PANE_PAGE_CHARACTER_MAP, "FONT_MANAGER_PREVIEW_PANE_PAGE_CHARACTER_MAP", "character-map" }, |
| 67 |
{ FONT_MANAGER_PREVIEW_PANE_PAGE_PROPERTIES, "FONT_MANAGER_PREVIEW_PANE_PAGE_PROPERTIES", "properties" }, |
| 68 |
{ FONT_MANAGER_PREVIEW_PANE_PAGE_LICENSE, "FONT_MANAGER_PREVIEW_PANE_PAGE_LICENSE", "license" }, |
| 69 |
{ 0, NULL, NULL } |
| 70 |
}; |
| 71 |
GType g_define_type_id = |
| 72 |
g_enum_register_static (g_intern_static_string ("FontManagerPreviewPanePage"), values); |
| 73 |
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); |
| 74 |
} |
| 75 |
|
| 76 |
return g_define_type_id__volatile; |
| 77 |
} |
| 78 |
|
| 79 |
struct _FontManagerPreviewPane |
| 80 |
{ |
| 81 |
GtkWidget parent; |
| 82 |
|
| 83 |
gint page; |
| 84 |
gint line_spacing; |
| 85 |
gboolean update_required; |
| 86 |
gboolean show_line_size; |
| 87 |
gdouble preview_size; |
| 88 |
gdouble glyph_preview_size; |
| 89 |
gchar *preview_text; |
| 90 |
gchar *current_uri; |
| 91 |
GtkWidget *preview; |
| 92 |
GtkWidget *character_map; |
| 93 |
GtkWidget *properties; |
| 94 |
GtkWidget *license; |
| 95 |
GtkWidget *search; |
| 96 |
GtkWidget *action_area; |
| 97 |
GtkNotebook *notebook; |
| 98 |
|
| 99 |
FontManagerFont *font; |
| 100 |
FontManagerDatabase *db; |
| 101 |
FontManagerPreviewPageMode mode; |
| 102 |
}; |
| 103 |
|
| 104 |
G_DEFINE_TYPE(FontManagerPreviewPane, font_manager_preview_pane, GTK_TYPE_WIDGET) |
| 105 |
|
| 106 |
enum |
| 107 |
{ |
| 108 |
CHANGED, |
| 109 |
NUM_SIGNALS |
| 110 |
}; |
| 111 |
|
| 112 |
static guint signals[NUM_SIGNALS]; |
| 113 |
|
| 114 |
enum |
| 115 |
{ |
| 116 |
PROP_RESERVED, |
| 117 |
PROP_PREVIEW_SIZE, |
| 118 |
PROP_GLYPH_PREVIEW_SIZE, |
| 119 |
PROP_PREVIEW_TEXT, |
| 120 |
PROP_PREVIEW_MODE, |
| 121 |
PROP_FONT, |
| 122 |
PROP_ORTHOGRAPHY, |
| 123 |
PROP_SHOW_LINE_SIZE, |
| 124 |
PROP_LINE_SPACING, |
| 125 |
PROP_PAGE, |
| 126 |
N_PROPERTIES |
| 127 |
}; |
| 128 |
|
| 129 |
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
| 130 |
|
| 131 |
static void update_mode (FontManagerPreviewPane *self); |
| 132 |
|
| 133 |
static void |
| 134 |
font_manager_preview_pane_dispose (GObject *gobject) |
| 135 |
{ |
| 136 |
g_return_if_fail(gobject != NULL); |
| 137 |
FontManagerPreviewPane *self = FONT_MANAGER_PREVIEW_PANE(gobject); |
| 138 |
g_clear_object(&self->font); |
| 139 |
g_clear_object(&self->db); |
| 140 |
g_clear_pointer(&self->preview_text, g_free); |
| 141 |
g_clear_pointer(&self->current_uri, g_free); |
| 142 |
font_manager_clear_application_fonts(); |
| 143 |
font_manager_widget_dispose(GTK_WIDGET(self)); |
| 144 |
G_OBJECT_CLASS(font_manager_preview_pane_parent_class)->dispose(gobject); |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
font_manager_preview_pane_get_property (GObject *gobject, |
| 150 |
guint property_id, |
| 151 |
GValue *value, |
| 152 |
GParamSpec *pspec) |
| 153 |
{ |
| 154 |
g_return_if_fail(gobject != NULL); |
| 155 |
FontManagerPreviewPane *self = FONT_MANAGER_PREVIEW_PANE(gobject); |
| 156 |
switch (property_id) { |
| 157 |
case PROP_PREVIEW_SIZE: |
| 158 |
g_value_set_double(value, self->preview_size); |
| 159 |
break; |
| 160 |
case PROP_GLYPH_PREVIEW_SIZE: |
| 161 |
g_value_set_double(value, self->glyph_preview_size); |
| 162 |
break; |
| 163 |
case PROP_PREVIEW_MODE: |
| 164 |
g_value_set_enum(value, self->mode); |
| 165 |
break; |
| 166 |
case PROP_PREVIEW_TEXT: |
| 167 |
g_value_set_string(value, self->preview_text); |
| 168 |
break; |
| 169 |
case PROP_FONT: |
| 170 |
g_value_set_object(value, self->font); |
| 171 |
break; |
| 172 |
case PROP_SHOW_LINE_SIZE: |
| 173 |
g_value_set_boolean(value, self->show_line_size); |
| 174 |
break; |
| 175 |
case PROP_LINE_SPACING: |
| 176 |
g_value_set_int(value, self->line_spacing); |
| 177 |
break; |
| 178 |
case PROP_PAGE: |
| 179 |
g_value_set_int(value, self->page); |
| 180 |
break; |
| 181 |
default: |
| 182 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 183 |
} |
| 184 |
return; |
| 185 |
} |
| 186 |
|
| 187 |
static void |
| 188 |
font_manager_preview_pane_set_property (GObject *gobject, |
| 189 |
guint property_id, |
| 190 |
const GValue *value, |
| 191 |
GParamSpec *pspec) |
| 192 |
{ |
| 193 |
g_return_if_fail(gobject != NULL); |
| 194 |
FontManagerPreviewPane *self = FONT_MANAGER_PREVIEW_PANE(gobject); |
| 195 |
switch (property_id) { |
| 196 |
case PROP_PREVIEW_SIZE: |
| 197 |
self->preview_size = g_value_get_double(value); |
| 198 |
break; |
| 199 |
case PROP_GLYPH_PREVIEW_SIZE: |
| 200 |
self->glyph_preview_size = g_value_get_double(value); |
| 201 |
break; |
| 202 |
case PROP_PREVIEW_MODE: |
| 203 |
self->mode = g_value_get_enum(value); |
| 204 |
update_mode(self); |
| 205 |
break; |
| 206 |
case PROP_PREVIEW_TEXT: |
| 207 |
g_clear_pointer(&self->preview_text, g_free); |
| 208 |
self->preview_text = g_value_dup_string(value); |
| 209 |
break; |
| 210 |
case PROP_FONT: |
| 211 |
font_manager_preview_pane_set_font(self, g_value_get_object(value)); |
| 212 |
break; |
| 213 |
case PROP_ORTHOGRAPHY: |
| 214 |
font_manager_preview_pane_set_orthography(self, g_value_get_object(value)); |
| 215 |
break; |
| 216 |
case PROP_SHOW_LINE_SIZE: |
| 217 |
self->show_line_size = g_value_get_boolean(value); |
| 218 |
break; |
| 219 |
case PROP_LINE_SPACING: |
| 220 |
self->line_spacing = g_value_get_int(value); |
| 221 |
break; |
| 222 |
case PROP_PAGE: |
| 223 |
self->page = g_value_get_int(value); |
| 224 |
break; |
| 225 |
default: |
| 226 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 227 |
} |
| 228 |
return; |
| 229 |
} |
| 230 |
|
| 231 |
static void |
| 232 |
on_search_action_activated (GtkWidget *widget, |
| 233 |
G_GNUC_UNUSED const char *action_name, |
| 234 |
G_GNUC_UNUSED GVariant *parameter) |
| 235 |
{ |
| 236 |
g_return_if_fail(widget != NULL); |
| 237 |
FontManagerPreviewPane *self = FONT_MANAGER_PREVIEW_PANE(widget); |
| 238 |
gboolean current_state = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(self->search)); |
| 239 |
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(self->search), !current_state); |
| 240 |
return; |
| 241 |
} |
| 242 |
|
| 243 |
static void |
| 244 |
font_manager_preview_pane_class_init (FontManagerPreviewPaneClass *klass) |
| 245 |
{ |
| 246 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 247 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| 248 |
|
| 249 |
object_class->dispose = font_manager_preview_pane_dispose; |
| 250 |
object_class->get_property = font_manager_preview_pane_get_property; |
| 251 |
object_class->set_property = font_manager_preview_pane_set_property; |
| 252 |
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BIN_LAYOUT); |
| 253 |
|
| 254 |
gtk_widget_class_install_action(widget_class, |
| 255 |
"character-search", |
| 256 |
NULL, |
| 257 |
on_search_action_activated); |
| 258 |
|
| 259 |
gtk_widget_class_add_binding_action(widget_class, |
| 260 |
GDK_KEY_f, |
| 261 |
GDK_CONTROL_MASK, |
| 262 |
"character-search", NULL); |
| 263 |
|
| 264 |
/** |
| 265 |
* FontManagerPreviewPane::changed: |
| 266 |
* |
| 267 |
* Emitted whenever the the preview is updated. |
| 268 |
*/ |
| 269 |
signals[CHANGED] = g_signal_new("changed", |
| 270 |
FONT_MANAGER_TYPE_PREVIEW_PANE, |
| 271 |
G_SIGNAL_RUN_FIRST, |
| 272 |
0, NULL, NULL, NULL, G_TYPE_NONE, 0); |
| 273 |
|
| 274 |
/** |
| 275 |
* FontManagerPreviewPane:preview-size: |
| 276 |
* |
| 277 |
* Size to use for font in preview mode. |
| 278 |
*/ |
| 279 |
obj_properties[PROP_PREVIEW_SIZE] = g_param_spec_double("preview-size", |
| 280 |
NULL, |
| 281 |
"Font preview size", |
| 282 |
FONT_MANAGER_MIN_FONT_SIZE, |
| 283 |
FONT_MANAGER_MAX_FONT_SIZE, |
| 284 |
FONT_MANAGER_DEFAULT_PREVIEW_SIZE, |
| 285 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 286 |
|
| 287 |
/** |
| 288 |
* FontManagerPreviewPane:character-map-preview-size: |
| 289 |
* |
| 290 |
* Size to use for font in character map. |
| 291 |
*/ |
| 292 |
obj_properties[PROP_GLYPH_PREVIEW_SIZE] = g_param_spec_double("character-map-preview-size", |
| 293 |
NULL, |
| 294 |
"Font preview size", |
| 295 |
FONT_MANAGER_MIN_FONT_SIZE, |
| 296 |
FONT_MANAGER_MAX_FONT_SIZE, |
| 297 |
FONT_MANAGER_LARGE_PREVIEW_SIZE, |
| 298 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 299 |
|
| 300 |
/** |
| 301 |
* FontManagerPreviewPane:preview-text: |
| 302 |
* |
| 303 |
* Text to display in interactive preview mode. |
| 304 |
*/ |
| 305 |
obj_properties[PROP_PREVIEW_TEXT] = g_param_spec_string("preview-text", |
| 306 |
NULL, |
| 307 |
"Preview text", |
| 308 |
NULL, |
| 309 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 310 |
|
| 311 |
/** |
| 312 |
* FontManagerPreviewPane:preview-mode: |
| 313 |
*/ |
| 314 |
obj_properties[PROP_PREVIEW_MODE] = g_param_spec_enum("preview-mode", |
| 315 |
NULL, |
| 316 |
"Preview mode", |
| 317 |
FONT_MANAGER_TYPE_PREVIEW_PAGE_MODE, |
| 318 |
FONT_MANAGER_PREVIEW_PAGE_MODE_WATERFALL, |
| 319 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 320 |
|
| 321 |
/** |
| 322 |
* FontManagerPreviewPane:font: |
| 323 |
* |
| 324 |
* #FontManagerFont to display. |
| 325 |
*/ |
| 326 |
obj_properties[PROP_FONT] = g_param_spec_object("font", |
| 327 |
NULL, |
| 328 |
"#FontManagerFont to display", |
| 329 |
FONT_MANAGER_TYPE_FONT, |
| 330 |
G_PARAM_READWRITE | |
| 331 |
G_PARAM_STATIC_STRINGS | |
| 332 |
G_PARAM_EXPLICIT_NOTIFY); |
| 333 |
|
| 334 |
/** |
| 335 |
* FontManagerPreviewPane:orthography: |
| 336 |
* |
| 337 |
* #FontManagerOrthography to display in character map. |
| 338 |
*/ |
| 339 |
obj_properties[PROP_ORTHOGRAPHY] = g_param_spec_object("orthography", |
| 340 |
NULL, |
| 341 |
"#FontManagerOrthography to display", |
| 342 |
FONT_MANAGER_TYPE_ORTHOGRAPHY, |
| 343 |
G_PARAM_WRITABLE | |
| 344 |
G_PARAM_STATIC_STRINGS | |
| 345 |
G_PARAM_EXPLICIT_NOTIFY); |
| 346 |
|
| 347 |
/** |
| 348 |
* FontManagerFontPreviewPane:show-line-size: |
| 349 |
* |
| 350 |
* Whether to display line size in Waterfall preview or not. |
| 351 |
*/ |
| 352 |
obj_properties[PROP_SHOW_LINE_SIZE] = g_param_spec_boolean("show-line-size", |
| 353 |
NULL, |
| 354 |
"Whether to display Waterfall preview line size", |
| 355 |
TRUE, |
| 356 |
G_PARAM_STATIC_STRINGS | |
| 357 |
G_PARAM_READWRITE); |
| 358 |
|
| 359 |
/** |
| 360 |
* FontManagerPreviewPane:line-spacing: |
| 361 |
* |
| 362 |
* Pixels between lines in Waterfall preview. |
| 363 |
*/ |
| 364 |
obj_properties[PROP_LINE_SPACING] = g_param_spec_int("line-spacing", |
| 365 |
NULL, |
| 366 |
"Waterfall preview line spacing", |
| 367 |
0, |
| 368 |
G_MAXINT, |
| 369 |
0, |
| 370 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 371 |
|
| 372 |
/** |
| 373 |
* FontManagerPreviewPane:page: |
| 374 |
* |
| 375 |
* Current page number. |
| 376 |
*/ |
| 377 |
obj_properties[PROP_PAGE] = g_param_spec_int("page", |
| 378 |
NULL, |
| 379 |
"Current page", |
| 380 |
0, |
| 381 |
3, |
| 382 |
0, |
| 383 |
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS); |
| 384 |
|
| 385 |
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties); |
| 386 |
return; |
| 387 |
} |
| 388 |
|
| 389 |
enum { WIDTH, WEIGHT, SLANT, SPACING, NUM_STYLE_DETAILS }; |
| 390 |
static const gchar *style_detail [NUM_STYLE_DETAILS] = { "width", "weight", "slant", "spacing" }; |
| 391 |
|
| 392 |
static gboolean |
| 393 |
font_manager_preview_pane_update_metadata (FontManagerPreviewPane *self) |
| 394 |
{ |
| 395 |
g_return_val_if_fail(self != NULL, G_SOURCE_REMOVE); |
| 396 |
if (!self->update_required || !FONT_MANAGER_IS_FONT(self->font)) |
| 397 |
return G_SOURCE_REMOVE; |
| 398 |
GError *error = NULL; |
| 399 |
g_autoptr(JsonObject) res = NULL; |
| 400 |
if (!self->db) |
| 401 |
self->db = font_manager_database_new(); |
| 402 |
JsonObject *source = NULL; |
| 403 |
g_object_get(G_OBJECT(self->font), "source-object", &source, NULL); |
| 404 |
if (!source) { |
| 405 |
g_critical("Failed to get source object! Unable to update metadata."); |
| 406 |
return G_SOURCE_REMOVE; |
| 407 |
} |
| 408 |
int index = json_object_get_int_member_with_default(source, "index", 0); |
| 409 |
const char *filepath = json_object_get_string_member(source, "filepath"); |
| 410 |
if (error == NULL) { |
| 411 |
const gchar *select = "SELECT * FROM Metadata WHERE filepath = %s AND findex = '%i'"; |
| 412 |
char *path = sqlite3_mprintf("%Q", filepath); |
| 413 |
g_autofree gchar *query = g_strdup_printf(select, path, index); |
| 414 |
res = font_manager_database_get_object(self->db, query, &error); |
| 415 |
sqlite3_free(path); |
| 416 |
} |
| 417 |
if (error != NULL) { |
| 418 |
g_clear_error(&error); |
| 419 |
} |
| 420 |
if (!res) { |
| 421 |
res = font_manager_get_metadata(filepath, index, &error); |
| 422 |
if (error != NULL) { |
| 423 |
g_critical("Failed to get metadata for %s : %s", filepath, error->message); |
| 424 |
g_clear_error(&error); |
| 425 |
} |
| 426 |
} |
| 427 |
if (res) { |
| 428 |
for (gint i = 0; i < NUM_STYLE_DETAILS; i++) { |
| 429 |
const gchar *str = NULL; |
| 430 |
if (!json_object_has_member(source, style_detail[i])) { |
| 431 |
g_debug("Missing %s in source object", style_detail[i]); |
| 432 |
continue; |
| 433 |
} |
| 434 |
int value = json_object_get_int_member(source, style_detail[i]); |
| 435 |
switch (i) { |
| 436 |
case WIDTH: |
| 437 |
str = font_manager_width_to_string((FontManagerWidth) value); |
| 438 |
break; |
| 439 |
case WEIGHT: |
| 440 |
str = font_manager_weight_to_string((FontManagerWeight) value); |
| 441 |
break; |
| 442 |
case SLANT: |
| 443 |
str = font_manager_slant_to_string((FontManagerSlant) value); |
| 444 |
break; |
| 445 |
case SPACING: |
| 446 |
str = font_manager_spacing_to_string((FontManagerSpacing) value); |
| 447 |
break; |
| 448 |
} |
| 449 |
json_object_set_string_member(res, style_detail[i], |
| 450 |
str ? str : i == WEIGHT ? _("Regular") : _("Normal")); |
| 451 |
} |
| 452 |
gint64 fsType = json_object_get_int_member(res, "fsType"); |
| 453 |
const gchar *license_data = json_object_get_string_member(res, "license-data"); |
| 454 |
const gchar *license_url = json_object_get_string_member(res, "license-url"); |
| 455 |
g_object_set(G_OBJECT(self->license), "fstype", (FontManagerfsType) fsType, |
| 456 |
"license-data", license_data, |
| 457 |
"license-url", license_url, NULL); |
| 458 |
} else { |
| 459 |
g_object_set(G_OBJECT(self->license), "fstype", FONT_MANAGER_FSTYPE_RESTRICTED_LICENSE, |
| 460 |
"license-data", NULL, "license-url", NULL, NULL); |
| 461 |
} |
| 462 |
font_manager_font_properties_page_update(FONT_MANAGER_PROPERTIES_PAGE(self->properties), res); |
| 463 |
//g_debug("PreviewPane.update_metadata : %s", font_manager_print_json_object(res, true)); |
| 464 |
self->update_required = FALSE; |
| 465 |
json_object_unref(source); |
| 466 |
return G_SOURCE_REMOVE; |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
static gboolean |
| 472 |
font_manager_preview_pane_update (FontManagerPreviewPane *self) |
| 473 |
{ |
| 474 |
g_return_val_if_fail(FONT_MANAGER_IS_PREVIEW_PANE(self), G_SOURCE_REMOVE); |
| 475 |
/* XXX : How is this a thing that happens intermittently ?! */ |
| 476 |
if (!GTK_IS_NOTEBOOK(self->notebook)) |
| 477 |
return G_SOURCE_REMOVE; |
| 478 |
gint page = gtk_notebook_get_current_page(self->notebook); |
| 479 |
GtkWidget *action_widget_box = gtk_notebook_get_action_widget(self->notebook, GTK_PACK_START); |
| 480 |
GtkWidget *menu = gtk_widget_get_first_child(action_widget_box); |
| 481 |
gtk_widget_set_visible(self->search, page == FONT_MANAGER_PREVIEW_PANE_PAGE_CHARACTER_MAP); |
| 482 |
gboolean menu_sensitive = (page == FONT_MANAGER_PREVIEW_PANE_PAGE_PREVIEW); |
| 483 |
gtk_widget_add_css_class(menu, menu_sensitive ? "image-button" : FONT_MANAGER_STYLE_CLASS_FLAT); |
| 484 |
gtk_widget_remove_css_class(menu, menu_sensitive ? FONT_MANAGER_STYLE_CLASS_FLAT : "image-button"); |
| 485 |
gtk_widget_set_sensitive(menu, menu_sensitive); |
| 486 |
g_idle_add((GSourceFunc) font_manager_preview_pane_update_metadata, self); |
| 487 |
g_signal_emit(self, signals[CHANGED], 0); |
| 488 |
gtk_widget_queue_draw(self->preview); |
| 489 |
return G_SOURCE_REMOVE; |
| 490 |
} |
| 491 |
|
| 492 |
static void |
| 493 |
on_search_toggled (GtkToggleButton *button, FontManagerPreviewPane *self) |
| 494 |
{ |
| 495 |
g_return_if_fail(self != NULL && button != NULL); |
| 496 |
g_object_set(G_OBJECT(self->character_map), "search-mode", gtk_toggle_button_get_active(button), NULL); |
| 497 |
return; |
| 498 |
} |
| 499 |
|
| 500 |
GtkWidget * |
| 501 |
create_search_button (FontManagerPreviewPane *self) |
| 502 |
{ |
| 503 |
GtkWidget *search = gtk_toggle_button_new(); |
| 504 |
GtkWidget *search_icon = gtk_image_new_from_icon_name("system-search-symbolic"); |
| 505 |
gtk_button_set_child(GTK_BUTTON(search), search_icon); |
| 506 |
gtk_widget_set_tooltip_text(search, _("Search available characters")); |
| 507 |
g_signal_connect(search, "toggled", G_CALLBACK(on_search_toggled), self); |
| 508 |
font_manager_widget_set_margin(search, 2); |
| 509 |
gtk_widget_set_margin_top(search, 1); |
| 510 |
gtk_widget_set_margin_bottom(search, 1); |
| 511 |
gtk_widget_set_visible(search, FALSE); |
| 512 |
return search; |
| 513 |
} |
| 514 |
|
| 515 |
void |
| 516 |
on_page_switch (FontManagerPreviewPane *self, |
| 517 |
GtkNotebook *notebook, |
| 518 |
GtkWidget *page, |
| 519 |
guint page_num) |
| 520 |
{ |
| 521 |
g_return_if_fail(self != NULL); |
| 522 |
g_idle_add((GSourceFunc) font_manager_preview_pane_update, self); |
| 523 |
return; |
| 524 |
} |
| 525 |
|
| 526 |
static void |
| 527 |
append_page (GtkNotebook *notebook, |
| 528 |
GtkWidget *widget, |
| 529 |
const gchar *title) |
| 530 |
{ |
| 531 |
gint page_added = gtk_notebook_append_page(notebook, widget, gtk_label_new(title)); |
| 532 |
g_warn_if_fail(page_added >= 0); |
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
static void |
| 537 |
update_mode (FontManagerPreviewPane *self) |
| 538 |
{ |
| 539 |
g_return_if_fail(self != NULL); |
| 540 |
GtkWidget *widget = gtk_notebook_get_tab_label(self->notebook, self->preview); |
| 541 |
gtk_label_set_text(GTK_LABEL(widget), font_manager_preview_page_mode_to_translatable_string(self->mode)); |
| 542 |
return; |
| 543 |
} |
| 544 |
|
| 545 |
static gboolean |
| 546 |
on_drop (GtkDropTarget *target, |
| 547 |
const GValue *value, |
| 548 |
double x, |
| 549 |
double y, |
| 550 |
FontManagerPreviewPane *self) |
| 551 |
{ |
| 552 |
if (G_VALUE_HOLDS(value, GDK_TYPE_FILE_LIST)) { |
| 553 |
GSList *files = g_value_get_boxed(value); |
| 554 |
/* We only handle the first face in the first file at this level */ |
| 555 |
if (g_slist_length(files) > 0) { |
| 556 |
GFile *file = g_slist_nth_data(files, 0); |
| 557 |
g_autofree gchar *uri = g_file_get_uri(file); |
| 558 |
font_manager_preview_pane_show_uri(self, uri); |
| 559 |
} |
| 560 |
} |
| 561 |
return TRUE; |
| 562 |
} |
| 563 |
|
| 564 |
static void |
| 565 |
font_manager_preview_pane_init (FontManagerPreviewPane *self) |
| 566 |
{ |
| 567 |
g_return_if_fail(self != NULL); |
| 568 |
font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerPreviewPane"); |
| 569 |
self->notebook = GTK_NOTEBOOK(gtk_notebook_new()); |
| 570 |
GtkWidget *box = gtk_widget_get_first_child(GTK_WIDGET(self->notebook)); |
| 571 |
gtk_widget_add_css_class(box, FONT_MANAGER_STYLE_CLASS_BACKGROUND); |
| 572 |
gtk_notebook_set_show_border(self->notebook, FALSE); |
| 573 |
gtk_widget_set_parent(GTK_WIDGET(self->notebook), GTK_WIDGET(self)); |
| 574 |
self->preview = font_manager_preview_page_new(); |
| 575 |
self->character_map = font_manager_character_map_new(); |
| 576 |
self->properties = font_manager_font_properties_page_new(); |
| 577 |
self->license = font_manager_license_page_new(); |
| 578 |
self->update_required = TRUE; |
| 579 |
self->show_line_size = TRUE; |
| 580 |
FontManagerPreviewPageMode mode = font_manager_preview_page_get_preview_mode(FONT_MANAGER_PREVIEW_PAGE(self->preview)); |
| 581 |
append_page(self->notebook, self->preview, font_manager_preview_page_mode_to_translatable_string(mode)); |
| 582 |
append_page(self->notebook, self->character_map, _("Characters")); |
| 583 |
append_page(self->notebook, self->properties, _("Properties")); |
| 584 |
append_page(self->notebook, self->license, _("License")); |
| 585 |
self->page = gtk_notebook_get_current_page(self->notebook); |
| 586 |
GtkWidget *menu_button = font_manager_preview_page_get_action_widget(FONT_MANAGER_PREVIEW_PAGE(self->preview)); |
| 587 |
gtk_notebook_set_action_widget(self->notebook, menu_button, GTK_PACK_START); |
| 588 |
self->action_area = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 1); |
| 589 |
self->search = create_search_button(self); |
| 590 |
gtk_box_append(GTK_BOX(self->action_area), self->search); |
| 591 |
gtk_notebook_set_action_widget(self->notebook, self->action_area, GTK_PACK_END); |
| 592 |
font_manager_widget_set_expand(GTK_WIDGET(self), TRUE); |
| 593 |
GBindingFlags flags = (G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE); |
| 594 |
g_object_bind_property(self->notebook, "page", self, "page", flags); |
| 595 |
g_object_bind_property(self->preview, "font", self, "font", flags); |
| 596 |
g_object_bind_property(self->preview, "preview-size", self, "preview-size", flags); |
| 597 |
g_object_bind_property(self->preview, "preview-text", self, "preview-text", flags); |
| 598 |
g_object_bind_property(self->preview, "preview-mode", self, "preview-mode", flags); |
| 599 |
g_object_bind_property(self->preview, "show-line-size", self, "show-line-size", flags); |
| 600 |
g_object_bind_property(self->preview, "line-spacing", self, "line-spacing", flags); |
| 601 |
g_object_bind_property(self->character_map, "font", self, "font", flags); |
| 602 |
g_object_bind_property(self->character_map, "preview-size", self, "character-map-preview-size", flags); |
| 603 |
g_signal_connect_swapped(self->notebook, "switch-page", G_CALLBACK(on_page_switch), self); |
| 604 |
g_signal_connect(self, "notify::preview-mode", G_CALLBACK(update_mode), NULL); |
| 605 |
GtkDropTarget *target = gtk_drop_target_new(GDK_TYPE_FILE_LIST, GDK_ACTION_COPY); |
| 606 |
g_signal_connect(target, "drop", G_CALLBACK(on_drop), self); |
| 607 |
gtk_widget_add_controller(GTK_WIDGET(self), GTK_EVENT_CONTROLLER(target)); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
/* |
| 612 |
* font_manager_preview_pane_show_uri: |
| 613 |
* @self: #FontManagerPreviewPane |
| 614 |
* @uri: filepath to display |
| 615 |
* |
| 616 |
* Returns: %TRUE on success |
| 617 |
*/ |
| 618 |
gboolean |
| 619 |
font_manager_preview_pane_show_uri (FontManagerPreviewPane *self, |
| 620 |
const gchar *uri) |
| 621 |
{ |
| 622 |
g_return_val_if_fail(self != NULL, FALSE); |
| 623 |
if (self->current_uri && g_strcmp0(self->current_uri, uri) == 0) |
| 624 |
return FALSE; |
| 625 |
g_clear_pointer(&self->current_uri, g_free); |
| 626 |
g_autoptr(GFile) file = g_file_new_for_commandline_arg(uri); |
| 627 |
g_return_val_if_fail(g_file_is_native(file), FALSE); |
| 628 |
GError *error = NULL; |
| 629 |
g_autoptr(GFileInfo) info = g_file_query_info(file, |
| 630 |
G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, |
| 631 |
G_FILE_QUERY_INFO_NONE, |
| 632 |
NULL, |
| 633 |
&error); |
| 634 |
if (error != NULL) { |
| 635 |
g_critical("Failed to query file info for %s : %s", uri, error->message); |
| 636 |
g_clear_error(&error); |
| 637 |
return FALSE; |
| 638 |
} |
| 639 |
const gchar *content_type = g_file_info_get_content_type(info); |
| 640 |
if (!g_strrstr(content_type, "font")) { |
| 641 |
g_warning("Ignoring unsupported filetype : %s", content_type); |
| 642 |
return FALSE; |
| 643 |
} |
| 644 |
g_autofree gchar *path = g_file_get_path(file); |
| 645 |
font_manager_add_application_font(path); |
| 646 |
font_manager_clear_pango_cache(gtk_widget_get_pango_context((GtkWidget *) self)); |
| 647 |
g_autoptr(JsonObject) source = font_manager_get_attributes_from_filepath(path, &error); |
| 648 |
if (error != NULL) { |
| 649 |
g_critical("%s : %s", error->message, path); |
| 650 |
g_clear_error(&error); |
| 651 |
return FALSE; |
| 652 |
} |
| 653 |
g_autoptr(FontManagerFont) font = font_manager_font_new(); |
| 654 |
g_autoptr(FontManagerFamily) family = font_manager_family_new(); |
| 655 |
g_object_set(family, "source-object", source, NULL); |
| 656 |
JsonObject *default_variant = font_manager_family_get_default_variant(family); |
| 657 |
g_autofree gchar *sample = font_manager_get_sample_string(default_variant); |
| 658 |
json_object_set_string_member(default_variant, "preview-text", sample); |
| 659 |
g_object_set(font, "source-object", default_variant, NULL); |
| 660 |
font_manager_preview_pane_set_font(self, font); |
| 661 |
self->current_uri = g_strdup(uri); |
| 662 |
return TRUE; |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* font_manager_preview_pane_set_font: |
| 667 |
* @self: #FontManagerPreviewPane |
| 668 |
* @font: #FontManagerFont |
| 669 |
*/ |
| 670 |
void |
| 671 |
font_manager_preview_pane_set_font (FontManagerPreviewPane *self, |
| 672 |
FontManagerFont *font) |
| 673 |
{ |
| 674 |
g_return_if_fail(FONT_MANAGER_IS_PREVIEW_PANE(self)); |
| 675 |
g_clear_pointer(&self->current_uri, g_free); |
| 676 |
if (g_set_object(&self->font, font)) |
| 677 |
g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_FONT]); |
| 678 |
self->update_required = TRUE; |
| 679 |
font_manager_preview_pane_update(self); |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* font_manager_preview_pane_set_orthography: |
| 685 |
* @self: #FontManagerPreviewPane |
| 686 |
* @orthography: (transfer none) (nullable): #FontManagerOrthography |
| 687 |
* |
| 688 |
* Filter character map using provided @orthography |
| 689 |
*/ |
| 690 |
void |
| 691 |
font_manager_preview_pane_set_orthography (FontManagerPreviewPane *self, |
| 692 |
FontManagerOrthography *orthography) |
| 693 |
{ |
| 694 |
g_return_if_fail(self != NULL); |
| 695 |
font_manager_character_map_set_filter(FONT_MANAGER_CHARACTER_MAP(self->character_map), |
| 696 |
font_manager_orthography_get_filter(orthography)); |
| 697 |
return; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* font_manager_preview_pane_restore_state: |
| 702 |
* @self: #FontManagerPreviewPage |
| 703 |
* @settings: #GSettings |
| 704 |
* |
| 705 |
* Applies the values in @settings to @self and also binds those settings to their |
| 706 |
* respective properties so that they are updated when any changes take place. |
| 707 |
* |
| 708 |
* The following keys MUST be present in @settings: |
| 709 |
* |
| 710 |
* - preview-text |
| 711 |
* - preview-mode |
| 712 |
* - preview-page |
| 713 |
* - preview-font-size |
| 714 |
* - charmap-font-size |
| 715 |
* - preview-font-size |
| 716 |
* - preview-mode |
| 717 |
* - preview-text |
| 718 |
* - show-line-size |
| 719 |
* - min-waterfall-size |
| 720 |
* - max-waterfall-size |
| 721 |
* - waterfall-size-ratio |
| 722 |
* - charmap-font-size |
| 723 |
* - waterfall-line-spacing |
| 724 |
*/ |
| 725 |
void |
| 726 |
font_manager_preview_pane_restore_state (FontManagerPreviewPane *self, |
| 727 |
GSettings *settings) |
| 728 |
{ |
| 729 |
g_return_if_fail(self != NULL); |
| 730 |
g_return_if_fail(settings != NULL); |
| 731 |
g_settings_bind(settings, "preview-page", self, "page", G_SETTINGS_BIND_DEFAULT); |
| 732 |
font_manager_preview_page_restore_state(FONT_MANAGER_PREVIEW_PAGE(self->preview), settings); |
| 733 |
font_manager_character_map_restore_state(FONT_MANAGER_CHARACTER_MAP(self->character_map), settings); |
| 734 |
return; |
| 735 |
} |
| 736 |
|
| 737 |
/** |
| 738 |
* font_manager_preview_pane_set_waterfall_size: |
| 739 |
* @self: #FontManagerFontPreview |
| 740 |
* @min_size: Minimum point size to use for waterfall previews. (-1.0 to keep current) |
| 741 |
* @max_size: Maximum size to use for waterfall previews. (-1.0 to keep current) |
| 742 |
* @ratio: Waterfall point size common ratio. (-1.0 to keep current) |
| 743 |
*/ |
| 744 |
void |
| 745 |
font_manager_preview_pane_set_waterfall_size (FontManagerPreviewPane *self, |
| 746 |
gdouble min_size, |
| 747 |
gdouble max_size, |
| 748 |
gdouble ratio) |
| 749 |
{ |
| 750 |
g_return_if_fail(self != NULL); |
| 751 |
FontManagerPreviewPage *preview = FONT_MANAGER_PREVIEW_PAGE(self->preview); |
| 752 |
font_manager_preview_page_set_waterfall_size(preview, |
| 753 |
min_size, |
| 754 |
max_size, |
| 755 |
ratio); |
| 756 |
return; |
| 757 |
} |
| 758 |
|
| 759 |
/** |
| 760 |
* font_manager_preview_pane_add_action_widget: |
| 761 |
* @self: #FontManagerFontPreview |
| 762 |
* @widget: #GtkWidget to add as an action widget |
| 763 |
* @pack_type: #GtkPackType |
| 764 |
*/ |
| 765 |
void |
| 766 |
font_manager_preview_pane_add_action_widget (FontManagerPreviewPane *self, |
| 767 |
GtkWidget *widget, |
| 768 |
GtkPackType pack_type) |
| 769 |
{ |
| 770 |
/* gtk_notebook_set_action_widget(GTK_NOTEBOOK(self->notebook), widget, pack_type); */ |
| 771 |
/* XXX : pack_type is ignored... we need a front box. Keep line #482 in mind though. */ |
| 772 |
gtk_box_append(GTK_BOX(self->action_area), widget); |
| 773 |
return; |
| 774 |
} |
| 775 |
|
| 776 |
/** |
| 777 |
* font_manager_preview_pane_new: |
| 778 |
* |
| 779 |
* Returns: (transfer full): A newly created #FontManagerPreviewPane. |
| 780 |
* Free the returned object using #g_object_unref(). |
| 781 |
*/ |
| 782 |
GtkWidget * |
| 783 |
font_manager_preview_pane_new () |
| 784 |
{ |
| 785 |
return g_object_new(FONT_MANAGER_TYPE_PREVIEW_PANE, NULL); |
| 786 |
} |
| 787 |
|