| 1 |
/* unicode-character-map.c |
| 2 |
* |
| 3 |
* Originally a part of Gucharmap |
| 4 |
* |
| 5 |
* Copyright (C) 2017-2025 Jerry Casiano |
| 6 |
* |
| 7 |
* |
| 8 |
* Copyright © 2004 Noah Levitt |
| 9 |
* Copyright © 2007, 2008, 2010 Christian Persch |
| 10 |
* |
| 11 |
* Some code copied from gtk+/gtk/gtkiconview: |
| 12 |
* Copyright © 2002, 2004 Anders Carlsson <[email protected]> |
| 13 |
* |
| 14 |
* This program is free software: you can redistribute it and/or modify |
| 15 |
* it under the terms of the GNU General Public License as published by |
| 16 |
* the Free Software Foundation, either version 3 of the License, or |
| 17 |
* (at your option) any later version. |
| 18 |
* |
| 19 |
* This program is distributed in the hope that it will be useful, |
| 20 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 |
* GNU General Public License for more details. |
| 23 |
* |
| 24 |
* You should have received a copy of the GNU General Public License |
| 25 |
* along with this program. |
| 26 |
* |
| 27 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include "unicode-character-map.h" |
| 31 |
|
| 32 |
/** |
| 33 |
* SECTION: unicode-character-map |
| 34 |
* @short_description: Character map widget |
| 35 |
* @title: Unicode Character Map |
| 36 |
* @include: unicode-character-map.h |
| 37 |
* |
| 38 |
* Widget which displays all the available characters in the selected font. |
| 39 |
*/ |
| 40 |
|
| 41 |
#define CELL_PADDING 2.5 |
| 42 |
#define VALID_FONT_SIZE(X) (X < 6.0 ? 6.0 : X > 96.0 ? 96.0 : X) |
| 43 |
#define FIRST_CELL_IN_SAME_ROW(x) ((x) - ((x) % self->columns)) |
| 44 |
#define N_REGIONAL_INDICATORS G_N_ELEMENTS(FontManagerRegionalIndicatorSymbols) |
| 45 |
|
| 46 |
enum |
| 47 |
{ |
| 48 |
SELECTION_CHANGED, |
| 49 |
NUM_SIGNALS |
| 50 |
}; |
| 51 |
|
| 52 |
static guint signals[NUM_SIGNALS]; |
| 53 |
|
| 54 |
enum |
| 55 |
{ |
| 56 |
PROP_0, |
| 57 |
PROP_HADJUSTMENT, |
| 58 |
PROP_VADJUSTMENT, |
| 59 |
PROP_HSCROLL_POLICY, |
| 60 |
PROP_VSCROLL_POLICY, |
| 61 |
PROP_ACTIVE_CELL, |
| 62 |
PROP_FONT_DESC, |
| 63 |
PROP_PREVIEW_SIZE |
| 64 |
}; |
| 65 |
|
| 66 |
struct _FontManagerUnicodeCharacterMap |
| 67 |
{ |
| 68 |
GtkDrawingArea parent; |
| 69 |
|
| 70 |
gint rows; |
| 71 |
gint columns; |
| 72 |
gint active_cell; /* the active cell index */ |
| 73 |
gint drag_cell; /* the cell currently being dragged */ |
| 74 |
gint last_cell; /* charset length */ |
| 75 |
gint min_cell_height; /* depends on font_desc and size allocation */ |
| 76 |
gint min_cell_width; /* depends on font_desc and size allocation */ |
| 77 |
gint n_padded_columns; /* columns 0..n-1 will be 1px wider than min_cell_width */ |
| 78 |
gint n_padded_rows; /* rows 0..n-1 will be 1px taller than min_cell_height */ |
| 79 |
gint page_first_cell; /* the cell index of the top left corner */ |
| 80 |
gint page_size; /* rows * cols */ |
| 81 |
|
| 82 |
GtkWidget *context_widget; |
| 83 |
|
| 84 |
/* Drawing */ |
| 85 |
gdouble preview_size; |
| 86 |
PangoLayout *pango_layout; |
| 87 |
PangoLayout *zoom_layout; |
| 88 |
GtkWidget *zoom_center_box; |
| 89 |
PangoFontDescription *font_desc; |
| 90 |
/* GtkScrollable implementation */ |
| 91 |
guint hscroll_policy : 1; /* unused */ |
| 92 |
guint vscroll_policy : 1; |
| 93 |
GtkAdjustment *hadjustment; /* unused */ |
| 94 |
GtkAdjustment *vadjustment; |
| 95 |
gulong vadjustment_changed_handler_id; |
| 96 |
/* Character set information */ |
| 97 |
gboolean has_regional_indicator_symbols; |
| 98 |
gboolean is_regional_indicator_filter; |
| 99 |
GList *charset; |
| 100 |
GList *filter; |
| 101 |
}; |
| 102 |
|
| 103 |
G_DEFINE_TYPE_WITH_CODE (FontManagerUnicodeCharacterMap, font_manager_unicode_character_map, |
| 104 |
GTK_TYPE_DRAWING_AREA, |
| 105 |
G_IMPLEMENT_INTERFACE(GTK_TYPE_SCROLLABLE, NULL)) |
| 106 |
|
| 107 |
static GtkWidget * get_context_widget (FontManagerUnicodeCharacterMap *self); |
| 108 |
|
| 109 |
static void |
| 110 |
update_scrollbar_adjustment (FontManagerUnicodeCharacterMap *self) |
| 111 |
{ |
| 112 |
g_return_if_fail(self != NULL); |
| 113 |
if (self->vadjustment != NULL) { |
| 114 |
gtk_adjustment_configure(self->vadjustment, |
| 115 |
self->page_first_cell / self->columns, /* current value */ |
| 116 |
0 /* lower */, |
| 117 |
self->last_cell / self->columns + 1 /* upper */, |
| 118 |
1 /* step increment */, |
| 119 |
self->rows /* page increment */, |
| 120 |
self->rows); /* page size */ |
| 121 |
} |
| 122 |
return; |
| 123 |
} |
| 124 |
|
| 125 |
static void |
| 126 |
clear_pango_layout (FontManagerUnicodeCharacterMap *self) |
| 127 |
{ |
| 128 |
g_return_if_fail(self != NULL); |
| 129 |
g_clear_object(&self->pango_layout); |
| 130 |
g_clear_object(&self->zoom_layout); |
| 131 |
return; |
| 132 |
} |
| 133 |
|
| 134 |
static void |
| 135 |
ensure_pango_layout (FontManagerUnicodeCharacterMap *self) |
| 136 |
{ |
| 137 |
g_return_if_fail(self != NULL); |
| 138 |
if (self->pango_layout == NULL || self->zoom_layout == NULL) { |
| 139 |
g_autoptr(PangoAttrList) attrs = pango_attr_list_new(); |
| 140 |
pango_attr_list_insert(attrs, pango_attr_fallback_new (FALSE)); |
| 141 |
if (self->pango_layout == NULL) { |
| 142 |
self->pango_layout = gtk_widget_create_pango_layout(GTK_WIDGET(self), NULL); |
| 143 |
pango_layout_set_font_description(self->pango_layout, self->font_desc); |
| 144 |
pango_layout_set_attributes(self->pango_layout, attrs); |
| 145 |
} |
| 146 |
if (self->zoom_layout == NULL) { |
| 147 |
GtkWidget *popover = get_context_widget(self); |
| 148 |
GtkWidget *box = gtk_popover_get_child(GTK_POPOVER(popover)); |
| 149 |
GtkWidget *widget = gtk_widget_get_first_child(box); |
| 150 |
self->zoom_layout = gtk_widget_create_pango_layout(widget, NULL); |
| 151 |
g_autoptr(PangoFontDescription) font_desc = pango_font_description_copy(self->font_desc); |
| 152 |
pango_font_description_set_size(font_desc, 96 * PANGO_SCALE); |
| 153 |
pango_layout_set_font_description(self->zoom_layout, font_desc); |
| 154 |
pango_layout_set_alignment(self->zoom_layout, PANGO_ALIGN_CENTER); |
| 155 |
pango_layout_set_attributes(self->zoom_layout, attrs); |
| 156 |
} |
| 157 |
} |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
static gint |
| 162 |
get_index (FontManagerUnicodeCharacterMap *self, GSList *codepoints) |
| 163 |
{ |
| 164 |
g_return_val_if_fail(self != NULL, -1); |
| 165 |
if (!codepoints || g_slist_length(codepoints) < 1) |
| 166 |
return -1; |
| 167 |
gunichar code1 = (gunichar) GPOINTER_TO_INT(g_slist_nth_data(codepoints, 0)); |
| 168 |
if (self->filter && self->is_regional_indicator_filter) { |
| 169 |
if (g_slist_length(codepoints) == 2) { |
| 170 |
gunichar code2 = (gunichar) GPOINTER_TO_INT(g_slist_nth_data(codepoints, 1)); |
| 171 |
for (int i = 0; i < N_REGIONAL_INDICATORS; i++) |
| 172 |
if (FontManagerRegionalIndicatorSymbols[i].code1 == code1 |
| 173 |
&& FontManagerRegionalIndicatorSymbols[i].code2 == code2) |
| 174 |
return i; |
| 175 |
} |
| 176 |
return -1; |
| 177 |
} else if (self->filter) |
| 178 |
return g_list_index(self->filter, GINT_TO_POINTER(code1)); |
| 179 |
else |
| 180 |
return self->charset != NULL ? (gint) g_list_index(self->charset, GINT_TO_POINTER(code1)) : -1; |
| 181 |
} |
| 182 |
|
| 183 |
static gint |
| 184 |
get_last_index (FontManagerUnicodeCharacterMap *self) |
| 185 |
{ |
| 186 |
g_return_val_if_fail(self != NULL, -1); |
| 187 |
if (self->filter && self->is_regional_indicator_filter) |
| 188 |
return N_REGIONAL_INDICATORS - 1; |
| 189 |
else if (self->filter) |
| 190 |
return g_list_length(self->filter) - 1; |
| 191 |
if (!self->charset) |
| 192 |
return 0; |
| 193 |
if (!self->has_regional_indicator_symbols) |
| 194 |
return (gint) g_list_length(self->charset) - 1; |
| 195 |
return (((gint) g_list_length(self->charset)) + N_REGIONAL_INDICATORS) - 1; |
| 196 |
} |
| 197 |
|
| 198 |
static GSList * |
| 199 |
get_codepoints (FontManagerUnicodeCharacterMap *self, gint index) |
| 200 |
{ |
| 201 |
g_return_val_if_fail(self != NULL, NULL); |
| 202 |
gint base_codepoints = (gint) g_list_length(self->charset); |
| 203 |
GSList *results = NULL; |
| 204 |
if (index < base_codepoints) { |
| 205 |
if (self->filter && self->is_regional_indicator_filter) { |
| 206 |
if (index < N_REGIONAL_INDICATORS) { |
| 207 |
results = g_slist_append(results, GINT_TO_POINTER(FontManagerRegionalIndicatorSymbols[index].code1)); |
| 208 |
results = g_slist_append(results, GINT_TO_POINTER(FontManagerRegionalIndicatorSymbols[index].code2)); |
| 209 |
} |
| 210 |
} else if (self->filter) |
| 211 |
results = g_slist_append(results, g_list_nth_data(self->filter, index)); |
| 212 |
else |
| 213 |
results = g_slist_append(results, self->charset != NULL ? |
| 214 |
g_list_nth_data(self->charset, index) : |
| 215 |
GINT_TO_POINTER(-1)); |
| 216 |
} else if (base_codepoints > 0) { |
| 217 |
gint _index = index - base_codepoints; |
| 218 |
if (_index < N_REGIONAL_INDICATORS) { |
| 219 |
results = g_slist_append(results, GINT_TO_POINTER(FontManagerRegionalIndicatorSymbols[_index].code1)); |
| 220 |
results = g_slist_append(results, GINT_TO_POINTER(FontManagerRegionalIndicatorSymbols[_index].code2)); |
| 221 |
} |
| 222 |
} |
| 223 |
return results; |
| 224 |
} |
| 225 |
|
| 226 |
static gchar * |
| 227 |
get_text_for_cell (FontManagerUnicodeCharacterMap *self, gint index) |
| 228 |
{ |
| 229 |
g_return_val_if_fail(self != NULL, NULL); |
| 230 |
GSList *codepoints = get_codepoints(self, index); |
| 231 |
for (GSList *iter = codepoints; iter != NULL; iter = iter->next) { |
| 232 |
gunichar wc = GPOINTER_TO_INT(iter->data); |
| 233 |
if (wc > FONT_MANAGER_UNICHAR_MAX || !font_manager_unicode_unichar_validate(wc)) |
| 234 |
return NULL; |
| 235 |
} |
| 236 |
gchar text [24]; |
| 237 |
gchar *p = text; |
| 238 |
for (GSList *iter = codepoints; iter != NULL; iter = iter->next) |
| 239 |
p += font_manager_unicode_unichar_to_printable_utf8((gunichar) GPOINTER_TO_INT(iter->data), p); |
| 240 |
p[0] = 0; |
| 241 |
g_slist_free(codepoints); |
| 242 |
return g_strdup(text); |
| 243 |
} |
| 244 |
|
| 245 |
static void |
| 246 |
resize_context_widget (FontManagerUnicodeCharacterMap *self) |
| 247 |
{ |
| 248 |
g_return_if_fail(self != NULL); |
| 249 |
ensure_pango_layout(self); |
| 250 |
g_autofree gchar *cell_text = get_text_for_cell(self, self->active_cell); |
| 251 |
pango_layout_set_text(self->zoom_layout, cell_text, -1); |
| 252 |
PangoRectangle char_rect; |
| 253 |
gint border = FONT_MANAGER_DEFAULT_MARGIN * 4, glyph_pad = border, width = -1, height = -1; |
| 254 |
pango_layout_get_pixel_size(self->zoom_layout, &width, &height); |
| 255 |
pango_layout_get_pixel_extents(self->zoom_layout, NULL, &char_rect); |
| 256 |
width = width > 0 ? width :char_rect.width; |
| 257 |
height = height > 0 ? height : char_rect.height; |
| 258 |
gint min_width = width + glyph_pad; |
| 259 |
gint min_height = height + glyph_pad; |
| 260 |
GtkWidget *parent = gtk_widget_get_parent(self->zoom_center_box); |
| 261 |
gtk_widget_set_size_request(parent, min_width + (border * 1.25), min_height + (border * 3)); |
| 262 |
gtk_widget_set_size_request(self->zoom_center_box, min_width, min_height); |
| 263 |
return; |
| 264 |
} |
| 265 |
|
| 266 |
static void |
| 267 |
font_manager_unicode_character_map_resize (GtkDrawingArea *widget, int width, int height) |
| 268 |
{ |
| 269 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(widget); |
| 270 |
gint old_rows = self->rows; |
| 271 |
gint old_cols = self->columns; |
| 272 |
gint min_width = CELL_PADDING * self->preview_size; |
| 273 |
gint min_height = CELL_PADDING * self->preview_size; |
| 274 |
graphene_rect_t allocation = GRAPHENE_RECT_INIT_ZERO; |
| 275 |
if (!gtk_widget_compute_bounds(GTK_WIDGET(widget), GTK_WIDGET(widget), &allocation)) { |
| 276 |
g_critical("Failed to get bounds for GtkDrawingArea containing FontManagerCharacterMap"); |
| 277 |
} |
| 278 |
self->rows = allocation.size.height / min_height; |
| 279 |
self->columns = allocation.size.width / min_width; |
| 280 |
/* Avoid a horrible floating point exception crash */ |
| 281 |
self->rows = self->rows > 0 ? self->rows : 1; |
| 282 |
self->columns = self->columns > 0 ? self->columns : 1; |
| 283 |
self->page_size = self->rows * self->columns; |
| 284 |
gint extra = allocation.size.width - (self->columns * min_width + 1); |
| 285 |
self->min_cell_width = min_width + extra / self->columns; |
| 286 |
self->n_padded_columns = allocation.size.width - (self->min_cell_width * self->columns + 1); |
| 287 |
extra = allocation.size.height - (self->rows * min_height + 1); |
| 288 |
self->min_cell_height = min_height + extra / self->rows; |
| 289 |
self->n_padded_rows = allocation.size.height - (self->min_cell_height * self->rows + 1); |
| 290 |
if (self->rows != old_rows || self->columns != old_cols) { |
| 291 |
/* Need to recalculate the first cell, see bug #517188 */ |
| 292 |
gint new_first_cell = FIRST_CELL_IN_SAME_ROW(self->active_cell); |
| 293 |
if ((new_first_cell + self->page_size) > (self->last_cell)) { |
| 294 |
/* Last cell is visible, so make sure it is in the last row */ |
| 295 |
new_first_cell = FIRST_CELL_IN_SAME_ROW(self->last_cell) - self->page_size + self->columns; |
| 296 |
if (new_first_cell < 0) |
| 297 |
new_first_cell = 0; |
| 298 |
} |
| 299 |
self->page_first_cell = new_first_cell; |
| 300 |
} |
| 301 |
update_scrollbar_adjustment(self); |
| 302 |
return; |
| 303 |
} |
| 304 |
|
| 305 |
static gint |
| 306 |
get_cell_at_rowcol (FontManagerUnicodeCharacterMap *self, gint row, gint col) |
| 307 |
{ |
| 308 |
/* Depends on directionality */ |
| 309 |
if (gtk_widget_get_direction(GTK_WIDGET(self)) == GTK_TEXT_DIR_RTL) |
| 310 |
return self->page_first_cell + row * self->columns + (self->columns - col - 1); |
| 311 |
else |
| 312 |
return self->page_first_cell + row * self->columns + col; |
| 313 |
} |
| 314 |
|
| 315 |
static gint |
| 316 |
cell_column (FontManagerUnicodeCharacterMap *self, guint cell) |
| 317 |
{ |
| 318 |
/* Depends on directionality. Column 0 is the furthest left. */ |
| 319 |
if (gtk_widget_get_direction(GTK_WIDGET(self)) == GTK_TEXT_DIR_RTL) |
| 320 |
return self->columns - (cell - self->page_first_cell) % self->columns - 1; |
| 321 |
else |
| 322 |
return (cell - self->page_first_cell) % self->columns; |
| 323 |
} |
| 324 |
|
| 325 |
static gint |
| 326 |
column_width (FontManagerUnicodeCharacterMap *self, gint col) |
| 327 |
{ |
| 328 |
g_return_val_if_fail(self != NULL, self->min_cell_width); |
| 329 |
/* Not all columns are necessarily the same width because of padding */ |
| 330 |
if (self->columns - col <= self->n_padded_columns) |
| 331 |
return self->min_cell_width + 1; |
| 332 |
else |
| 333 |
return self->min_cell_width; |
| 334 |
} |
| 335 |
|
| 336 |
static gint |
| 337 |
row_height (FontManagerUnicodeCharacterMap *self, gint row) |
| 338 |
{ |
| 339 |
g_return_val_if_fail(self != NULL, self->min_cell_height); |
| 340 |
/* Not all rows are necessarily the same height because of padding */ |
| 341 |
if (self->rows - row <= self->n_padded_rows) |
| 342 |
return self->min_cell_height + 1; |
| 343 |
else |
| 344 |
return self->min_cell_height; |
| 345 |
} |
| 346 |
|
| 347 |
static gint |
| 348 |
get_cell_at_xy (FontManagerUnicodeCharacterMap *self, gint x, gint y) |
| 349 |
{ |
| 350 |
gint c, r, x0, y0, cell; |
| 351 |
|
| 352 |
for (c = 0, x0 = 0; x0 <= x && c < self->columns; c++) |
| 353 |
x0 += column_width(self, c); |
| 354 |
|
| 355 |
for (r = 0, y0 = 0; y0 <= y && r < self->rows; r++) |
| 356 |
y0 += row_height(self, r); |
| 357 |
|
| 358 |
cell = get_cell_at_rowcol(self, r-1, c-1); |
| 359 |
|
| 360 |
if (cell > self->last_cell) |
| 361 |
return self->last_cell; |
| 362 |
|
| 363 |
return cell; |
| 364 |
} |
| 365 |
|
| 366 |
/* Calculate the position of the left end of the column (just to the right |
| 367 |
* of the left border) |
| 368 |
* XXX: calling this repeatedly is not the most efficient, but it probably |
| 369 |
* is the most readable |
| 370 |
*/ |
| 371 |
static gint |
| 372 |
get_x_offset (FontManagerUnicodeCharacterMap *self, gint col) |
| 373 |
{ |
| 374 |
gint c, x; |
| 375 |
for (c = 0, x = 1; c < col; c++) |
| 376 |
x += column_width(self, c); |
| 377 |
return x; |
| 378 |
} |
| 379 |
|
| 380 |
/* Calculate the position of the top end of the row (just below the top |
| 381 |
* border) |
| 382 |
* XXX: calling this repeatedly is not the most efficient, but it probably |
| 383 |
* is the most readable |
| 384 |
*/ |
| 385 |
static gint |
| 386 |
get_y_offset (FontManagerUnicodeCharacterMap *self, gint row) |
| 387 |
{ |
| 388 |
gint r, y; |
| 389 |
for (r = 0, y = 1; r < row; r++) |
| 390 |
y += row_height(self, r); |
| 391 |
return y; |
| 392 |
} |
| 393 |
|
| 394 |
/* FIXME ! */ |
| 395 |
G_GNUC_BEGIN_IGNORE_DEPRECATIONS |
| 396 |
|
| 397 |
/* |
| 398 |
* NOTE : GTK is working on removing the ability to get theme colors. |
| 399 |
* Need to find a workaround for this before replacing the deprecated |
| 400 |
* functions used for this widget. |
| 401 |
*/ |
| 402 |
|
| 403 |
/* |
| 404 |
|
| 405 |
void |
| 406 |
render_line (cairo_t *cr, double x0, double y0, double x1, double y1) |
| 407 |
{ |
| 408 |
cairo_move_to(cr, x0, y0); |
| 409 |
cairo_line_to(cr, x1, y1); |
| 410 |
cairo_stroke(cr); |
| 411 |
return; |
| 412 |
} |
| 413 |
|
| 414 |
static void |
| 415 |
draw_character_with_metrics (GtkDrawingArea *widget, |
| 416 |
cairo_t *cr, |
| 417 |
gint unused_width, |
| 418 |
gint unused_height, |
| 419 |
gpointer user_data) |
| 420 |
{ |
| 421 |
g_return_if_fail(user_data != NULL); |
| 422 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 423 |
ensure_pango_layout(self); |
| 424 |
g_autofree gchar *cell_text = get_text_for_cell(self, self->active_cell); |
| 425 |
pango_layout_set_text(self->zoom_layout, cell_text, -1); |
| 426 |
PangoRectangle char_rect; |
| 427 |
graphene_rect_t allocation = GRAPHENE_RECT_INIT_ZERO; |
| 428 |
if (!gtk_widget_compute_bounds(GTK_WIDGET(widget), GTK_WIDGET(widget), &allocation)) { |
| 429 |
g_critical("Failed to get bounds for GtkDrawingArea containing FontManagerCharacterMap"); |
| 430 |
} |
| 431 |
gint glyph_pad = 48, width = -1, height = -1; |
| 432 |
pango_layout_get_pixel_size(self->zoom_layout, &width, &height); |
| 433 |
pango_layout_get_pixel_extents(self->zoom_layout, NULL, &char_rect); |
| 434 |
width = (width < 0) ? char_rect.width : width; |
| 435 |
height = (height < 0) ? char_rect.height : height; |
| 436 |
gtk_widget_set_size_request(GTK_WIDGET(widget), width + glyph_pad, height + glyph_pad); |
| 437 |
gint xpad = ((allocation.size.width - char_rect.width) / 2); |
| 438 |
gint ypad = ((allocation.size.height - char_rect.height) / 2); |
| 439 |
gint baseline = pango_layout_get_baseline(self->zoom_layout) / PANGO_SCALE; |
| 440 |
GdkRGBA color; |
| 441 |
gtk_widget_get_color(GTK_WIDGET(widget), &color); |
| 442 |
cairo_set_line_width(cr, 0.5); |
| 443 |
cairo_set_source_rgba(cr, color.red, color.green, color.blue, 0.25); |
| 444 |
render_line(cr, 1, baseline + xpad, allocation.size.width - 1, baseline + xpad); |
| 445 |
render_line(cr, 1, PANGO_ASCENT(char_rect) + xpad, allocation.size.width - 1, PANGO_ASCENT(char_rect) + xpad); |
| 446 |
render_line(cr, 1, PANGO_DESCENT(char_rect) + xpad, allocation.size.width - 1, PANGO_DESCENT(char_rect) + xpad); |
| 447 |
render_line(cr, PANGO_LBEARING(char_rect) + ypad, 1, PANGO_LBEARING(char_rect) + ypad, allocation.size.height - 1); |
| 448 |
render_line(cr, PANGO_RBEARING(char_rect) + ypad, 1, PANGO_RBEARING(char_rect) + ypad, allocation.size.height - 1); |
| 449 |
cairo_set_source_rgba(cr, color.red, color.green, color.blue, color.alpha); |
| 450 |
cairo_move_to(cr, char_rect.x + xpad, char_rect.y + ypad); |
| 451 |
pango_cairo_show_layout(cr, self->zoom_layout); |
| 452 |
gtk_popover_present(GTK_POPOVER(get_context_widget(self))); |
| 453 |
return; |
| 454 |
} |
| 455 |
|
| 456 |
*/ |
| 457 |
|
| 458 |
static void |
| 459 |
draw_character_with_metrics (GtkDrawingArea *widget, |
| 460 |
cairo_t *cr, |
| 461 |
gint unused_width, |
| 462 |
gint unused_height, |
| 463 |
gpointer user_data) |
| 464 |
{ |
| 465 |
g_return_if_fail(user_data != NULL); |
| 466 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 467 |
ensure_pango_layout(self); |
| 468 |
GtkStyleContext *ctx = gtk_widget_get_style_context(GTK_WIDGET(widget)); |
| 469 |
g_autofree gchar *cell_text = get_text_for_cell(self, self->active_cell); |
| 470 |
pango_layout_set_text(self->zoom_layout, cell_text, -1); |
| 471 |
PangoRectangle char_rect; |
| 472 |
GtkAllocation alloc; |
| 473 |
gint glyph_pad = 48, width = -1, height = -1; |
| 474 |
pango_layout_get_pixel_size(self->zoom_layout, &width, &height); |
| 475 |
pango_layout_get_pixel_extents(self->zoom_layout, NULL, &char_rect); |
| 476 |
width = width > 0 ? width : char_rect.width; |
| 477 |
height = height > 0 ? height : char_rect.height; |
| 478 |
GtkWidget *parent = gtk_widget_get_parent(GTK_WIDGET(widget)); |
| 479 |
int parent_size = MAX(width + glyph_pad, height + glyph_pad); |
| 480 |
gtk_widget_set_size_request(parent, parent_size, parent_size); |
| 481 |
gtk_widget_set_size_request(GTK_WIDGET(widget), width + glyph_pad, height + glyph_pad); |
| 482 |
gtk_widget_get_allocation(GTK_WIDGET(widget), &alloc); |
| 483 |
gint xpad = ((alloc.width - char_rect.width) / 2); |
| 484 |
gint ypad = ((alloc.height - char_rect.height) / 2); |
| 485 |
gint baseline = pango_layout_get_baseline(self->zoom_layout) / PANGO_SCALE; |
| 486 |
gtk_style_context_save(ctx); |
| 487 |
gtk_style_context_set_state(ctx, GTK_STATE_FLAG_INSENSITIVE); |
| 488 |
gtk_style_context_add_class(ctx, "PangoGlyphMetrics"); |
| 489 |
gtk_render_line(ctx, cr, 1, baseline + xpad, alloc.width - 1, baseline + xpad); |
| 490 |
gtk_render_line(ctx, cr, 1, PANGO_ASCENT(char_rect) + xpad, alloc.width - 1, PANGO_ASCENT(char_rect) + xpad); |
| 491 |
gtk_render_line(ctx, cr, 1, PANGO_DESCENT(char_rect) + xpad, alloc.width - 1, PANGO_DESCENT(char_rect) + xpad); |
| 492 |
gtk_render_line(ctx, cr, PANGO_LBEARING(char_rect) + ypad, 1, PANGO_LBEARING(char_rect) + ypad, alloc.height - 1); |
| 493 |
gtk_render_line(ctx, cr, PANGO_RBEARING(char_rect) + ypad, 1, PANGO_RBEARING(char_rect) + ypad, alloc.height - 1); |
| 494 |
gtk_style_context_restore(ctx); |
| 495 |
gtk_render_layout(ctx, cr, char_rect.x + xpad, char_rect.y + ypad, self->zoom_layout); |
| 496 |
resize_context_widget(self); |
| 497 |
gtk_popover_present(GTK_POPOVER(get_context_widget(self))); |
| 498 |
return; |
| 499 |
} |
| 500 |
|
| 501 |
static void |
| 502 |
draw_character (GtkWidget *widget, |
| 503 |
GtkSnapshot *snapshot, |
| 504 |
GtkStyleContext *ctx, |
| 505 |
graphene_rect_t *rect, |
| 506 |
gint cell) |
| 507 |
{ |
| 508 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(widget); |
| 509 |
g_autofree gchar *text = get_text_for_cell(self, cell); |
| 510 |
pango_layout_set_text(self->pango_layout, text, -1); |
| 511 |
/* Keep the square empty if the font has no glyph for this cell. */ |
| 512 |
if (pango_layout_get_unknown_glyphs_count(self->pango_layout) > 0) |
| 513 |
return; |
| 514 |
gtk_style_context_save(ctx); |
| 515 |
GtkStateFlags _state = GTK_STATE_FLAG_NORMAL; |
| 516 |
if (gtk_widget_has_focus(widget) && cell == self->active_cell) |
| 517 |
_state = GTK_STATE_FLAG_SELECTED | GTK_STATE_FLAG_FOCUSED; |
| 518 |
else if ((gint) cell == self->active_cell) |
| 519 |
_state = GTK_STATE_FLAG_INSENSITIVE | GTK_STATE_FLAG_SELECTED; |
| 520 |
gtk_style_context_set_state(ctx, _state); |
| 521 |
gtk_style_context_add_class(ctx, "CharacterMapGlyph"); |
| 522 |
gint char_width, char_height; |
| 523 |
pango_layout_get_pixel_size(self->pango_layout, &char_width, &char_height); |
| 524 |
gtk_snapshot_render_layout(snapshot, ctx, |
| 525 |
rect->origin.x + (rect->size.width - char_width) / 2, |
| 526 |
rect->origin.y + (rect->size.height - char_height) / 2, |
| 527 |
self->pango_layout); |
| 528 |
gtk_style_context_restore(ctx); |
| 529 |
return; |
| 530 |
} |
| 531 |
|
| 532 |
static void |
| 533 |
draw_square_bg (GtkWidget *widget, |
| 534 |
GtkSnapshot *snapshot, |
| 535 |
GtkStyleContext *ctx, |
| 536 |
graphene_rect_t *rect, |
| 537 |
gint cell) |
| 538 |
{ |
| 539 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(widget); |
| 540 |
gtk_style_context_save(ctx); |
| 541 |
GtkStateFlags _state = GTK_STATE_FLAG_NORMAL; |
| 542 |
if (gtk_widget_has_focus(widget) && cell == self->active_cell) |
| 543 |
_state = GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_SELECTED; |
| 544 |
else if (cell == self->active_cell) |
| 545 |
_state = GTK_STATE_FLAG_SELECTED; |
| 546 |
else if (cell > self->last_cell) |
| 547 |
_state = GTK_STATE_FLAG_INSENSITIVE; |
| 548 |
gtk_style_context_set_state(ctx, _state); |
| 549 |
gtk_style_context_add_class(ctx, "CharacterMapCell"); |
| 550 |
gtk_snapshot_render_background(snapshot, ctx, |
| 551 |
rect->origin.x + 2, rect->origin.y + 2, |
| 552 |
rect->size.width - 5, rect->size.height - 5); |
| 553 |
gtk_style_context_restore(ctx); |
| 554 |
return; |
| 555 |
} |
| 556 |
|
| 557 |
static void |
| 558 |
draw_separators (GtkWidget *widget, GtkStyleContext *ctx, cairo_t *cr) |
| 559 |
{ |
| 560 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(widget); |
| 561 |
gint x, y, col, row; |
| 562 |
GtkAllocation allocation; |
| 563 |
gtk_style_context_save(ctx); |
| 564 |
/* Set insensitive flag so our lines have less contrast */ |
| 565 |
gtk_style_context_set_state(ctx, GTK_STATE_FLAG_INSENSITIVE); |
| 566 |
gtk_style_context_add_class(ctx, "CharacterMapSeparator"); |
| 567 |
gtk_widget_get_allocation(widget, &allocation); |
| 568 |
/* Vertical */ |
| 569 |
gtk_render_line(ctx, cr, 0, 0, 0, allocation.height); |
| 570 |
for (col = 0, x = 0; col < self->columns; col++) { |
| 571 |
x += column_width(self, col); |
| 572 |
gtk_render_line(ctx, cr, x, 0, x, allocation.height); |
| 573 |
} |
| 574 |
/* Horizontal */ |
| 575 |
gtk_render_line(ctx, cr, 0, 0, allocation.width, 0); |
| 576 |
for (row = 0, y = 0; row < self->rows; row++) { |
| 577 |
y += row_height(self, row); |
| 578 |
gtk_render_line(ctx, cr, 0, y, allocation.width, y); |
| 579 |
} |
| 580 |
gtk_style_context_restore(ctx); |
| 581 |
return; |
| 582 |
} |
| 583 |
|
| 584 |
static void |
| 585 |
font_manager_unicode_character_map_snapshot (GtkWidget *widget, GtkSnapshot *snapshot) |
| 586 |
{ |
| 587 |
GtkAllocation allocation; |
| 588 |
GtkStyleContext *ctx = gtk_widget_get_style_context(widget); |
| 589 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(widget); |
| 590 |
ensure_pango_layout(self); |
| 591 |
gtk_widget_get_allocation(widget, &allocation); |
| 592 |
for (int row = self->rows - 1; row >= 0; --row) { |
| 593 |
for (int col = self->columns - 1; col >= 0; --col) { |
| 594 |
graphene_rect_t *cell_rect = graphene_rect_alloc(); |
| 595 |
cell_rect = graphene_rect_init(cell_rect, |
| 596 |
get_x_offset(self, col), |
| 597 |
get_y_offset(self, row), |
| 598 |
column_width(self, col), |
| 599 |
row_height(self, row)); |
| 600 |
gint cell = get_cell_at_rowcol(self, row, col); |
| 601 |
draw_square_bg(widget, snapshot, ctx, cell_rect, cell); |
| 602 |
draw_character(widget, snapshot, ctx, cell_rect, cell); |
| 603 |
graphene_rect_free(cell_rect); |
| 604 |
} |
| 605 |
} |
| 606 |
graphene_rect_t *allocated_rect = graphene_rect_alloc(); |
| 607 |
allocated_rect = graphene_rect_init(allocated_rect, |
| 608 |
allocation.x, allocation.y, |
| 609 |
allocation.width, allocation.height); |
| 610 |
cairo_t *cr = gtk_snapshot_append_cairo(snapshot, allocated_rect); |
| 611 |
draw_separators(widget, ctx, cr); |
| 612 |
graphene_rect_free(allocated_rect); |
| 613 |
cairo_destroy(cr); |
| 614 |
return; |
| 615 |
} |
| 616 |
|
| 617 |
static void |
| 618 |
set_hadjustment (FontManagerUnicodeCharacterMap *self, GtkAdjustment *hadjustment) |
| 619 |
{ |
| 620 |
g_return_if_fail(self != NULL); |
| 621 |
g_set_object(&self->hadjustment, hadjustment); |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
static void |
| 626 |
vadjustment_value_changed_cb (GtkAdjustment *vadjustment, FontManagerUnicodeCharacterMap *self) |
| 627 |
{ |
| 628 |
int row = (int) gtk_adjustment_get_value(vadjustment); |
| 629 |
if (row < 0 ) |
| 630 |
row = 0; |
| 631 |
int first_cell = row * self->columns; |
| 632 |
if (first_cell == self->page_first_cell) |
| 633 |
return; |
| 634 |
gtk_widget_queue_resize(GTK_WIDGET(self)); |
| 635 |
self->page_first_cell = first_cell; |
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
static void |
| 640 |
set_vadjustment (FontManagerUnicodeCharacterMap *self, GtkAdjustment *vadjustment) |
| 641 |
{ |
| 642 |
if (vadjustment) |
| 643 |
g_return_if_fail(GTK_IS_ADJUSTMENT(vadjustment)); |
| 644 |
else |
| 645 |
vadjustment = GTK_ADJUSTMENT(gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)); |
| 646 |
|
| 647 |
if (self->vadjustment) { |
| 648 |
g_signal_handler_disconnect(self->vadjustment, self->vadjustment_changed_handler_id); |
| 649 |
self->vadjustment_changed_handler_id = 0; |
| 650 |
g_clear_object(&self->vadjustment); |
| 651 |
} |
| 652 |
|
| 653 |
g_set_object(&self->vadjustment, vadjustment); |
| 654 |
self->vadjustment_changed_handler_id = g_signal_connect(vadjustment, |
| 655 |
"value-changed", |
| 656 |
G_CALLBACK(vadjustment_value_changed_cb), |
| 657 |
self); |
| 658 |
|
| 659 |
update_scrollbar_adjustment(self); |
| 660 |
return; |
| 661 |
} |
| 662 |
|
| 663 |
gboolean |
| 664 |
is_regional_indicator_filter (GList *filter) |
| 665 |
{ |
| 666 |
if (!filter || g_list_length(filter) != 26) |
| 667 |
return FALSE; |
| 668 |
return (GPOINTER_TO_INT(g_list_nth_data(filter, 0)) == FONT_MANAGER_RIS_START_POINT |
| 669 |
&& GPOINTER_TO_INT(g_list_nth_data(filter, 25)) == FONT_MANAGER_RIS_END_POINT); |
| 670 |
} |
| 671 |
|
| 672 |
static void |
| 673 |
check_for_regional_indicator_symbols (FontManagerUnicodeCharacterMap *self, hb_set_t *charset) |
| 674 |
{ |
| 675 |
self->has_regional_indicator_symbols = FALSE; |
| 676 |
for (guint32 i = FONT_MANAGER_RIS_START_POINT; i < FONT_MANAGER_RIS_END_POINT; i++) |
| 677 |
if (!hb_set_has(charset, i)) |
| 678 |
return; |
| 679 |
self->has_regional_indicator_symbols = TRUE; |
| 680 |
return; |
| 681 |
} |
| 682 |
|
| 683 |
static void |
| 684 |
populate_charset (FontManagerUnicodeCharacterMap *self, const PangoFontDescription *font_desc) |
| 685 |
{ |
| 686 |
g_clear_pointer(&self->charset, g_list_free); |
| 687 |
g_clear_pointer(&self->filter, g_list_free); |
| 688 |
ensure_pango_layout(self); |
| 689 |
PangoContext *context = pango_layout_get_context(self->pango_layout); |
| 690 |
PangoFontMap *font_map = pango_context_get_font_map(context); |
| 691 |
g_autoptr(PangoFont) font = pango_font_map_load_font(font_map, context, font_desc); |
| 692 |
hb_font_t *hb_font = pango_font_get_hb_font(font); |
| 693 |
hb_face_t *face = hb_font_get_face(hb_font); |
| 694 |
hb_set_t *charset = hb_set_create(); |
| 695 |
hb_face_collect_unicodes(face, charset); |
| 696 |
hb_codepoint_t codepoint = HB_SET_VALUE_INVALID; |
| 697 |
while (hb_set_next(charset, &codepoint)) |
| 698 |
if (font_manager_unicode_unichar_isgraph(codepoint)) |
| 699 |
self->charset = g_list_prepend(self->charset, GINT_TO_POINTER(codepoint)); |
| 700 |
self->charset = g_list_reverse(self->charset); |
| 701 |
check_for_regional_indicator_symbols(self, charset); |
| 702 |
hb_set_destroy(charset); |
| 703 |
return; |
| 704 |
} |
| 705 |
|
| 706 |
static void |
| 707 |
set_font_desc_internal (FontManagerUnicodeCharacterMap *self, |
| 708 |
PangoFontDescription *font_desc) |
| 709 |
{ |
| 710 |
g_return_if_fail(font_desc != NULL); |
| 711 |
if (!self->font_desc || !pango_font_description_equal(font_desc, self->font_desc)){ |
| 712 |
g_clear_pointer(&self->font_desc, pango_font_description_free); |
| 713 |
pango_font_description_set_size(font_desc, self->preview_size * PANGO_SCALE); |
| 714 |
self->font_desc = pango_font_description_copy(font_desc); |
| 715 |
populate_charset(self, font_desc); |
| 716 |
g_object_notify(G_OBJECT(self), "font-desc"); |
| 717 |
} |
| 718 |
self->active_cell = 0; |
| 719 |
self->page_first_cell = 0; |
| 720 |
self->last_cell = get_last_index(self); |
| 721 |
clear_pango_layout(self); |
| 722 |
gtk_widget_queue_resize(GTK_WIDGET(self)); |
| 723 |
g_object_notify(G_OBJECT(self), "active-cell"); |
| 724 |
return; |
| 725 |
} |
| 726 |
|
| 727 |
static void |
| 728 |
on_selection_changed (FontManagerUnicodeCharacterMap *self) |
| 729 |
{ |
| 730 |
if (self->charset == NULL) |
| 731 |
return; |
| 732 |
const gchar *name = NULL; |
| 733 |
g_autofree gchar *codepoint = NULL; |
| 734 |
gint last_index = get_last_index(self); |
| 735 |
g_autofree gchar *n_codepoints = g_strdup_printf("%i", (last_index + 1)); |
| 736 |
GSList *codepoints = get_codepoints(self, self->active_cell); |
| 737 |
guint points = g_slist_length(codepoints); |
| 738 |
if (points == 1) { |
| 739 |
gunichar ac = (gunichar) GPOINTER_TO_INT(g_slist_nth_data(codepoints, 0)); |
| 740 |
codepoint = g_markup_printf_escaped("U+%4.4X", ac); |
| 741 |
name = font_manager_unicode_get_codepoint_name(ac); |
| 742 |
} else if (points == 2) { |
| 743 |
gunichar code1 = (gunichar) GPOINTER_TO_INT(g_slist_nth_data(codepoints, 0)); |
| 744 |
gunichar code2 = (gunichar) GPOINTER_TO_INT(g_slist_nth_data(codepoints, 1)); |
| 745 |
int index; |
| 746 |
for (index = 0; index < G_N_ELEMENTS(FontManagerRegionalIndicatorSymbols); index++) |
| 747 |
if (FontManagerRegionalIndicatorSymbols[index].code1 == code1 && FontManagerRegionalIndicatorSymbols[index].code2 == code2) |
| 748 |
break; |
| 749 |
codepoint = g_markup_printf_escaped("U+%4.4X + U+%4.4X", code1, code2); |
| 750 |
name = FontManagerRegionalIndicatorSymbols[index].region; |
| 751 |
} |
| 752 |
g_slist_free(codepoints); |
| 753 |
g_signal_emit(self, signals[SELECTION_CHANGED], 0, codepoint, name, n_codepoints); |
| 754 |
return; |
| 755 |
} |
| 756 |
|
| 757 |
static void |
| 758 |
copy_clipboard (GtkWidget *widget, gpointer user_data) |
| 759 |
{ |
| 760 |
g_return_if_fail(user_data != NULL); |
| 761 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 762 |
g_autofree gchar *text = get_text_for_cell(self, self->active_cell); |
| 763 |
GdkClipboard *clipboard = gtk_widget_get_clipboard(GTK_WIDGET(self)); |
| 764 |
gdk_clipboard_set_text(clipboard, text); |
| 765 |
if (self->context_widget && gtk_widget_get_mapped(self->context_widget)) |
| 766 |
gtk_popover_popdown(GTK_POPOVER(self->context_widget)); |
| 767 |
return; |
| 768 |
} |
| 769 |
|
| 770 |
static void |
| 771 |
copy_shortcut_func (GtkWidget *widget, GVariant *args, gpointer user_data) |
| 772 |
{ |
| 773 |
copy_clipboard(widget, widget); |
| 774 |
return; |
| 775 |
} |
| 776 |
|
| 777 |
static void |
| 778 |
move_cursor_left_right (FontManagerUnicodeCharacterMap *self, int count) |
| 779 |
{ |
| 780 |
gboolean is_rtl = (gtk_widget_get_direction(GTK_WIDGET(self)) == GTK_TEXT_DIR_RTL); |
| 781 |
int offset = is_rtl ? -count : count; |
| 782 |
font_manager_unicode_character_map_set_active_cell(self, self->active_cell + offset); |
| 783 |
return; |
| 784 |
} |
| 785 |
|
| 786 |
static void |
| 787 |
move_cursor_up_down (FontManagerUnicodeCharacterMap *self, int count) |
| 788 |
{ |
| 789 |
font_manager_unicode_character_map_set_active_cell(self, self->active_cell + self->columns * count); |
| 790 |
return; |
| 791 |
} |
| 792 |
|
| 793 |
static void |
| 794 |
move_cursor_page_up_down (FontManagerUnicodeCharacterMap *self, int count) |
| 795 |
{ |
| 796 |
font_manager_unicode_character_map_set_active_cell(self, self->active_cell + self->page_size * count); |
| 797 |
return; |
| 798 |
} |
| 799 |
|
| 800 |
static void |
| 801 |
move_cursor_start_end (FontManagerUnicodeCharacterMap *self, int count) |
| 802 |
{ |
| 803 |
int new_cell = count > 0 ? self->last_cell : 0; |
| 804 |
font_manager_unicode_character_map_set_active_cell(self, new_cell); |
| 805 |
return; |
| 806 |
} |
| 807 |
|
| 808 |
static void |
| 809 |
move_cursor (GtkWidget *widget, GVariant *variant, gpointer user_data) |
| 810 |
{ |
| 811 |
g_return_if_fail(widget != NULL); |
| 812 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(widget); |
| 813 |
gint step, count; |
| 814 |
g_variant_get(variant, "(ii)", &step, &count); |
| 815 |
switch ((GtkMovementStep) step) { |
| 816 |
case GTK_MOVEMENT_LOGICAL_POSITIONS: |
| 817 |
case GTK_MOVEMENT_VISUAL_POSITIONS: |
| 818 |
move_cursor_left_right(self, count); |
| 819 |
break; |
| 820 |
case GTK_MOVEMENT_DISPLAY_LINES: |
| 821 |
move_cursor_up_down(self, count); |
| 822 |
break; |
| 823 |
case GTK_MOVEMENT_PAGES: |
| 824 |
move_cursor_page_up_down(self, count); |
| 825 |
break; |
| 826 |
case GTK_MOVEMENT_BUFFER_ENDS: |
| 827 |
move_cursor_start_end(self, count); |
| 828 |
break; |
| 829 |
default: |
| 830 |
return; |
| 831 |
} |
| 832 |
return; |
| 833 |
} |
| 834 |
|
| 835 |
static GtkWidget * |
| 836 |
get_context_widget (FontManagerUnicodeCharacterMap *self) |
| 837 |
{ |
| 838 |
g_return_val_if_fail(self != NULL, NULL); |
| 839 |
if (self->context_widget != NULL) |
| 840 |
return self->context_widget; |
| 841 |
self->context_widget = gtk_popover_new(); |
| 842 |
gtk_popover_set_autohide(GTK_POPOVER(self->context_widget), TRUE); |
| 843 |
GtkWidget *center_box = gtk_center_box_new(); |
| 844 |
self->zoom_center_box = center_box; |
| 845 |
GtkWidget *glyph = gtk_drawing_area_new(); |
| 846 |
GtkWidget *separator = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL); |
| 847 |
gtk_widget_set_opacity(separator, 0.5); |
| 848 |
gtk_widget_set_margin_top(separator, FONT_MANAGER_DEFAULT_MARGIN); |
| 849 |
gtk_widget_set_margin_bottom(separator, FONT_MANAGER_DEFAULT_MARGIN); |
| 850 |
GtkWidget *copy_button = gtk_button_new_with_label(_("Copy")); |
| 851 |
gtk_widget_set_opacity(copy_button, 0.75); |
| 852 |
gtk_widget_add_css_class(copy_button, "pill-button"); |
| 853 |
font_manager_widget_set_align(copy_button, GTK_ALIGN_CENTER); |
| 854 |
font_manager_widget_set_margin(copy_button, 0); |
| 855 |
gtk_widget_set_focusable(copy_button, FALSE); |
| 856 |
gtk_center_box_set_center_widget(GTK_CENTER_BOX(center_box), glyph); |
| 857 |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 858 |
gtk_box_append(GTK_BOX(box), center_box); |
| 859 |
gtk_box_append(GTK_BOX(box), separator); |
| 860 |
gtk_box_append(GTK_BOX(box), copy_button); |
| 861 |
font_manager_widget_set_expand(glyph, FALSE); |
| 862 |
font_manager_widget_set_expand(separator, FALSE); |
| 863 |
font_manager_widget_set_expand(copy_button, FALSE); |
| 864 |
font_manager_widget_set_expand(box, FALSE); |
| 865 |
font_manager_widget_set_margin(box, 0); |
| 866 |
g_signal_connect(copy_button, "clicked", G_CALLBACK(copy_clipboard), self); |
| 867 |
gtk_drawing_area_set_draw_func(GTK_DRAWING_AREA(glyph), draw_character_with_metrics, self, NULL); |
| 868 |
gtk_popover_set_child(GTK_POPOVER(self->context_widget), box); |
| 869 |
gtk_popover_set_position(GTK_POPOVER(self->context_widget), GTK_POS_TOP); |
| 870 |
gtk_widget_set_parent(self->context_widget, GTK_WIDGET(self)); |
| 871 |
gtk_popover_set_default_widget(GTK_POPOVER(self->context_widget), copy_button); |
| 872 |
return self->context_widget; |
| 873 |
} |
| 874 |
|
| 875 |
static void |
| 876 |
show_context_widget (FontManagerUnicodeCharacterMap *self, gdouble x, gdouble y) |
| 877 |
{ |
| 878 |
g_return_if_fail(self != NULL); |
| 879 |
GtkWidget *popover = get_context_widget(self); |
| 880 |
gint row = (self->active_cell - self->page_first_cell) / self->columns; |
| 881 |
gint col = cell_column(self, self->active_cell); |
| 882 |
if ((row >= 0 && row < self->rows) && (col >= 0 && col < self->columns)) { |
| 883 |
gint x_offset = get_x_offset(self, col); |
| 884 |
gint y_offset = get_y_offset(self, row); |
| 885 |
GdkRectangle rect = { x_offset, y_offset, self->min_cell_width, self->min_cell_height }; |
| 886 |
gtk_popover_set_pointing_to(GTK_POPOVER(popover), &rect); |
| 887 |
} else { |
| 888 |
GdkRectangle rect = { x, y, 1, 1 }; |
| 889 |
gtk_popover_set_pointing_to(GTK_POPOVER(popover), &rect); |
| 890 |
} |
| 891 |
gtk_popover_popup(GTK_POPOVER(popover)); |
| 892 |
return; |
| 893 |
} |
| 894 |
|
| 895 |
void |
| 896 |
on_long_press (G_GNUC_UNUSED GtkGestureLongPress *gesture, gdouble x, gdouble y, gpointer user_data) |
| 897 |
{ |
| 898 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 899 |
font_manager_unicode_character_map_set_active_cell(self, get_cell_at_xy(self, x, y)); |
| 900 |
show_context_widget(self, x, y); |
| 901 |
return; |
| 902 |
} |
| 903 |
|
| 904 |
void |
| 905 |
on_pinch_zoom (G_GNUC_UNUSED GtkGestureZoom *gesture, gdouble scale_factor, gpointer user_data) |
| 906 |
{ |
| 907 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 908 |
gdouble size = nearbyint(self->preview_size * scale_factor); |
| 909 |
font_manager_unicode_character_map_set_preview_size(self, VALID_FONT_SIZE(size)); |
| 910 |
return; |
| 911 |
} |
| 912 |
|
| 913 |
static void |
| 914 |
on_click (GtkGestureClick* click, gint n_press, gdouble x, gdouble y, gpointer user_data) |
| 915 |
{ |
| 916 |
if (n_press > 1) |
| 917 |
return; |
| 918 |
g_return_if_fail(user_data != NULL); |
| 919 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 920 |
gtk_widget_grab_focus(GTK_WIDGET(user_data)); |
| 921 |
font_manager_unicode_character_map_set_active_cell(self, get_cell_at_xy(self, x, y)); |
| 922 |
if (gtk_gesture_single_get_current_button(GTK_GESTURE_SINGLE(click)) == GDK_BUTTON_SECONDARY) |
| 923 |
show_context_widget(self, x, y); |
| 924 |
return; |
| 925 |
} |
| 926 |
|
| 927 |
static GdkContentProvider * |
| 928 |
on_prepare_drag (GtkDragSource *source, double x, double y, gpointer user_data) |
| 929 |
{ |
| 930 |
g_return_val_if_fail(user_data != NULL, NULL); |
| 931 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 932 |
self->drag_cell = get_cell_at_xy(self, x, y); |
| 933 |
g_autofree gchar *text = get_text_for_cell(self, self->drag_cell); |
| 934 |
return gdk_content_provider_new_typed(G_TYPE_STRING, text); |
| 935 |
} |
| 936 |
|
| 937 |
static void |
| 938 |
on_drag_begin (GtkDragSource *source, GdkDrag *drag, gpointer user_data) |
| 939 |
{ |
| 940 |
gint font_size = FONT_MANAGER_LARGE_PREVIEW_SIZE * 1.5; |
| 941 |
GtkWidget *widget = GTK_WIDGET(user_data); |
| 942 |
g_autoptr(GtkSnapshot) snapshot = gtk_snapshot_new(); |
| 943 |
GtkStyleContext *ctx = gtk_widget_get_style_context(widget); |
| 944 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(user_data); |
| 945 |
graphene_rect_t *rect = graphene_rect_alloc(); |
| 946 |
rect = graphene_rect_init(rect, 0, 0, font_size * 3, font_size * 3); |
| 947 |
gtk_style_context_save(ctx); |
| 948 |
gtk_style_context_set_state(ctx, GTK_STATE_FLAG_FOCUSED | GTK_STATE_FLAG_SELECTED); |
| 949 |
gtk_style_context_add_class(ctx, "CharacterMapCell"); |
| 950 |
gtk_style_context_add_class(ctx, "CharacterMapGlyph"); |
| 951 |
gtk_snapshot_render_background(snapshot, ctx, |
| 952 |
rect->origin.x, rect->origin.y, |
| 953 |
rect->size.width, rect->size.height); |
| 954 |
g_autofree gchar *text = get_text_for_cell(self, self->drag_cell); |
| 955 |
g_autoptr(PangoLayout) layout = gtk_widget_create_pango_layout(widget, text); |
| 956 |
PangoAttrList *attrs = pango_attr_list_new(); |
| 957 |
PangoAttribute *size = pango_attr_size_new(font_size * PANGO_SCALE); |
| 958 |
PangoAttribute *font = pango_attr_font_desc_new(self->font_desc); |
| 959 |
pango_attr_list_insert(attrs, font); |
| 960 |
pango_attr_list_insert(attrs, size); |
| 961 |
pango_layout_set_attributes(layout, attrs); |
| 962 |
gint char_width, char_height; |
| 963 |
pango_layout_get_pixel_size(layout, &char_width, &char_height); |
| 964 |
gtk_snapshot_render_layout(snapshot, ctx, |
| 965 |
rect->origin.x + (rect->size.width - char_width) / 2, |
| 966 |
rect->origin.y + (rect->size.height - char_height) / 2, |
| 967 |
layout); |
| 968 |
gtk_style_context_restore(ctx); |
| 969 |
pango_attr_list_unref(attrs); |
| 970 |
gtk_drag_source_set_icon(source, gtk_snapshot_to_paintable(snapshot, NULL), 0, 0); |
| 971 |
gdk_drag_set_hotspot(drag, |
| 972 |
rect->origin.x - ((rect->size.width / 2)), |
| 973 |
rect->origin.y - ((rect->size.height / 2)) - 12); |
| 974 |
graphene_rect_free(rect); |
| 975 |
return; |
| 976 |
} |
| 977 |
|
| 978 |
G_GNUC_END_IGNORE_DEPRECATIONS |
| 979 |
|
| 980 |
static void |
| 981 |
font_manager_unicode_character_map_dispose (GObject *gobject) |
| 982 |
{ |
| 983 |
g_return_if_fail(gobject != NULL); |
| 984 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(gobject); |
| 985 |
g_clear_pointer(&self->font_desc, pango_font_description_free); |
| 986 |
clear_pango_layout(self); |
| 987 |
font_manager_widget_dispose(GTK_WIDGET(self)); |
| 988 |
G_OBJECT_CLASS(font_manager_unicode_character_map_parent_class)->dispose(gobject); |
| 989 |
return; |
| 990 |
} |
| 991 |
|
| 992 |
static void |
| 993 |
font_manager_unicode_character_map_set_property (GObject *gobject, |
| 994 |
guint prop_id, |
| 995 |
const GValue *value, |
| 996 |
GParamSpec *pspec) |
| 997 |
{ |
| 998 |
g_return_if_fail(gobject != NULL); |
| 999 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(gobject); |
| 1000 |
|
| 1001 |
switch (prop_id) { |
| 1002 |
case PROP_HADJUSTMENT: |
| 1003 |
set_hadjustment(self, g_value_get_object(value)); |
| 1004 |
break; |
| 1005 |
case PROP_VADJUSTMENT: |
| 1006 |
set_vadjustment(self, g_value_get_object(value)); |
| 1007 |
break; |
| 1008 |
case PROP_HSCROLL_POLICY: |
| 1009 |
self->hscroll_policy = g_value_get_enum(value); |
| 1010 |
gtk_widget_queue_resize(GTK_WIDGET(self)); |
| 1011 |
break; |
| 1012 |
case PROP_VSCROLL_POLICY: |
| 1013 |
self->vscroll_policy = g_value_get_enum(value); |
| 1014 |
gtk_widget_queue_resize(GTK_WIDGET(self)); |
| 1015 |
break; |
| 1016 |
case PROP_ACTIVE_CELL: |
| 1017 |
self->active_cell = g_value_get_int(value); |
| 1018 |
break; |
| 1019 |
case PROP_FONT_DESC: |
| 1020 |
font_manager_unicode_character_map_set_font_desc(self, g_value_get_boxed(value)); |
| 1021 |
break; |
| 1022 |
case PROP_PREVIEW_SIZE: |
| 1023 |
font_manager_unicode_character_map_set_preview_size(self, g_value_get_double(value)); |
| 1024 |
break; |
| 1025 |
default: |
| 1026 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec); |
| 1027 |
break; |
| 1028 |
} |
| 1029 |
return; |
| 1030 |
} |
| 1031 |
|
| 1032 |
static void |
| 1033 |
font_manager_unicode_character_map_get_property (GObject *gobject, |
| 1034 |
guint prop_id, |
| 1035 |
GValue *value, |
| 1036 |
GParamSpec *pspec) |
| 1037 |
{ |
| 1038 |
g_return_if_fail(gobject != NULL); |
| 1039 |
FontManagerUnicodeCharacterMap *self = FONT_MANAGER_UNICODE_CHARACTER_MAP(gobject); |
| 1040 |
|
| 1041 |
switch (prop_id) { |
| 1042 |
case PROP_HADJUSTMENT: |
| 1043 |
g_value_set_object(value, NULL); |
| 1044 |
break; |
| 1045 |
case PROP_VADJUSTMENT: |
| 1046 |
g_value_set_object(value, self->vadjustment); |
| 1047 |
break; |
| 1048 |
case PROP_HSCROLL_POLICY: |
| 1049 |
g_value_set_enum(value, self->hscroll_policy); |
| 1050 |
break; |
| 1051 |
case PROP_VSCROLL_POLICY: |
| 1052 |
g_value_set_enum(value, self->vscroll_policy); |
| 1053 |
break; |
| 1054 |
case PROP_ACTIVE_CELL: |
| 1055 |
g_value_set_int(value, self->active_cell); |
| 1056 |
break; |
| 1057 |
case PROP_FONT_DESC: |
| 1058 |
g_value_set_boxed(value, font_manager_unicode_character_map_get_font_desc(self)); |
| 1059 |
break; |
| 1060 |
case PROP_PREVIEW_SIZE: |
| 1061 |
g_value_set_double(value, font_manager_unicode_character_map_get_preview_size(self)); |
| 1062 |
break; |
| 1063 |
default: |
| 1064 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec); |
| 1065 |
break; |
| 1066 |
} |
| 1067 |
return; |
| 1068 |
} |
| 1069 |
|
| 1070 |
static void |
| 1071 |
font_manager_unicode_character_map_class_init (FontManagerUnicodeCharacterMapClass *klass) |
| 1072 |
{ |
| 1073 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 1074 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| 1075 |
|
| 1076 |
GTK_DRAWING_AREA_CLASS(widget_class)->resize = font_manager_unicode_character_map_resize; |
| 1077 |
widget_class->snapshot = font_manager_unicode_character_map_snapshot; |
| 1078 |
object_class->dispose = font_manager_unicode_character_map_dispose; |
| 1079 |
object_class->get_property = font_manager_unicode_character_map_get_property; |
| 1080 |
object_class->set_property = font_manager_unicode_character_map_set_property; |
| 1081 |
|
| 1082 |
/* GtkScrollable interface properties */ |
| 1083 |
g_object_class_override_property(object_class, PROP_HADJUSTMENT, "hadjustment"); |
| 1084 |
g_object_class_override_property(object_class, PROP_VADJUSTMENT, "vadjustment"); |
| 1085 |
g_object_class_override_property(object_class, PROP_HSCROLL_POLICY, "hscroll-policy"); |
| 1086 |
g_object_class_override_property(object_class, PROP_VSCROLL_POLICY, "vscroll-policy"); |
| 1087 |
|
| 1088 |
/** |
| 1089 |
* FontManagerUnicodeCharacterMap::selection-changed: |
| 1090 |
* @self: #FontManagerUnicodeCharacterMap |
| 1091 |
* @codepoint: Unicode codepoint as a string |
| 1092 |
* @codepoint_name: Codepoint name |
| 1093 |
* @n_codepoints: Total # of codepoints in current list as a string |
| 1094 |
* |
| 1095 |
* The :selection-changed signal is emitted whenever a new cell is selected. |
| 1096 |
*/ |
| 1097 |
signals[SELECTION_CHANGED] = g_signal_new("selection-changed", |
| 1098 |
FONT_MANAGER_TYPE_UNICODE_CHARACTER_MAP, |
| 1099 |
G_SIGNAL_RUN_FIRST, |
| 1100 |
0, |
| 1101 |
NULL, NULL, NULL, |
| 1102 |
G_TYPE_NONE, |
| 1103 |
3, |
| 1104 |
G_TYPE_STRING, |
| 1105 |
G_TYPE_STRING, |
| 1106 |
G_TYPE_STRING); |
| 1107 |
|
| 1108 |
/** |
| 1109 |
* FontManagerUnicodeCharacterMap:active-cell: |
| 1110 |
* |
| 1111 |
* Active cell in character map |
| 1112 |
*/ |
| 1113 |
g_object_class_install_property(object_class, |
| 1114 |
PROP_ACTIVE_CELL, |
| 1115 |
g_param_spec_int("active-cell", |
| 1116 |
NULL, |
| 1117 |
"Active cell in character map", |
| 1118 |
G_MININT, |
| 1119 |
G_MAXINT, |
| 1120 |
0, |
| 1121 |
G_PARAM_READWRITE | |
| 1122 |
G_PARAM_STATIC_STRINGS)); |
| 1123 |
|
| 1124 |
/** |
| 1125 |
* FontManagerUnicodeCharacterMap:font-desc: |
| 1126 |
* |
| 1127 |
* #PangoFontDescription |
| 1128 |
*/ |
| 1129 |
g_object_class_install_property(object_class, |
| 1130 |
PROP_FONT_DESC, |
| 1131 |
g_param_spec_boxed("font-desc", |
| 1132 |
NULL, |
| 1133 |
"PangoFontDescription", |
| 1134 |
PANGO_TYPE_FONT_DESCRIPTION, |
| 1135 |
G_PARAM_READWRITE | |
| 1136 |
G_PARAM_STATIC_STRINGS)); |
| 1137 |
|
| 1138 |
/** |
| 1139 |
* FontManagerUnicodeCharacterMap:preview-size: |
| 1140 |
* |
| 1141 |
* Character map preview size |
| 1142 |
*/ |
| 1143 |
g_object_class_install_property(object_class, |
| 1144 |
PROP_PREVIEW_SIZE, |
| 1145 |
g_param_spec_double("preview-size", |
| 1146 |
NULL, |
| 1147 |
"Preview size", |
| 1148 |
6, 96, 14, |
| 1149 |
G_PARAM_READWRITE | |
| 1150 |
G_PARAM_STATIC_STRINGS)); |
| 1151 |
|
| 1152 |
|
| 1153 |
/* Clipboard */ |
| 1154 |
GtkShortcutFunc copy_callback = (GtkShortcutFunc) copy_shortcut_func; |
| 1155 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_c, GDK_CONTROL_MASK, copy_callback, NULL); |
| 1156 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Insert, GDK_CONTROL_MASK, copy_callback, NULL); |
| 1157 |
/* Cursor movement */ |
| 1158 |
GtkShortcutFunc move_callback = (GtkShortcutFunc) move_cursor; |
| 1159 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Up, 0, move_callback, "(ii)", GTK_MOVEMENT_DISPLAY_LINES, -1); |
| 1160 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Up, 0, move_callback, "(ii)", GTK_MOVEMENT_DISPLAY_LINES, -1); |
| 1161 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Down, 0, move_callback, "(ii)", GTK_MOVEMENT_DISPLAY_LINES, 1); |
| 1162 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Down, 0, move_callback, "(ii)", GTK_MOVEMENT_DISPLAY_LINES, 1); |
| 1163 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_p, GDK_CONTROL_MASK, move_callback, "(ii)", GTK_MOVEMENT_DISPLAY_LINES, -1); |
| 1164 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_n, GDK_CONTROL_MASK, move_callback, "(ii)", GTK_MOVEMENT_DISPLAY_LINES, 1); |
| 1165 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Home, 0, move_callback, "(ii)", GTK_MOVEMENT_BUFFER_ENDS, -1); |
| 1166 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Home, 0, move_callback, "(ii)", GTK_MOVEMENT_BUFFER_ENDS, -1); |
| 1167 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_End, 0, move_callback, "(ii)", GTK_MOVEMENT_BUFFER_ENDS, 1); |
| 1168 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_End, 0, move_callback, "(ii)", GTK_MOVEMENT_BUFFER_ENDS, 1); |
| 1169 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Page_Up, 0, move_callback, "(ii)", GTK_MOVEMENT_PAGES, -1); |
| 1170 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Page_Up, 0, move_callback, "(ii)", GTK_MOVEMENT_PAGES, -1); |
| 1171 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Page_Down, 0, move_callback, "(ii)", GTK_MOVEMENT_PAGES, 1); |
| 1172 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Page_Down, 0, move_callback, "(ii)", GTK_MOVEMENT_PAGES, 1); |
| 1173 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Left, 0, move_callback, "(ii)", GTK_MOVEMENT_VISUAL_POSITIONS, -1); |
| 1174 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Left, 0, move_callback, "(ii)", GTK_MOVEMENT_VISUAL_POSITIONS, -1); |
| 1175 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_Right, 0, move_callback, "(ii)", GTK_MOVEMENT_VISUAL_POSITIONS, 1); |
| 1176 |
gtk_widget_class_add_binding(widget_class, GDK_KEY_KP_Right, 0, move_callback, "(ii)", GTK_MOVEMENT_VISUAL_POSITIONS, 1); |
| 1177 |
return; |
| 1178 |
} |
| 1179 |
|
| 1180 |
static void |
| 1181 |
font_manager_unicode_character_map_init (FontManagerUnicodeCharacterMap *self) |
| 1182 |
{ |
| 1183 |
self->page_first_cell = 0; |
| 1184 |
self->active_cell = 0; |
| 1185 |
self->rows = 1; |
| 1186 |
self->columns = 1; |
| 1187 |
self->vadjustment = NULL; |
| 1188 |
self->hadjustment = NULL; |
| 1189 |
self->hscroll_policy = GTK_SCROLL_NATURAL; |
| 1190 |
self->vscroll_policy = GTK_SCROLL_NATURAL; |
| 1191 |
self->preview_size = FONT_MANAGER_LARGE_PREVIEW_SIZE; |
| 1192 |
GtkWidget *widget = GTK_WIDGET(self); |
| 1193 |
gtk_widget_set_focusable(widget, TRUE); |
| 1194 |
gtk_widget_add_css_class(widget, FONT_MANAGER_STYLE_CLASS_VIEW); |
| 1195 |
font_manager_widget_set_expand(widget, TRUE); |
| 1196 |
font_manager_widget_set_name(widget, "FontManagerUnicodeCharacterMap"); |
| 1197 |
g_autoptr(PangoFontDescription) font_desc = pango_font_description_from_string(FONT_MANAGER_DEFAULT_FONT); |
| 1198 |
font_manager_unicode_character_map_set_font_desc(self, font_desc); |
| 1199 |
g_signal_connect(self, "notify::active-cell", G_CALLBACK(on_selection_changed), self); |
| 1200 |
/* Mouse events */ |
| 1201 |
GtkGesture *click_gesture = gtk_gesture_click_new(); |
| 1202 |
gtk_gesture_single_set_button(GTK_GESTURE_SINGLE(click_gesture), 0); |
| 1203 |
g_signal_connect(click_gesture, "released", G_CALLBACK(on_click), self); |
| 1204 |
gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(click_gesture)); |
| 1205 |
/* Touch events */ |
| 1206 |
GtkGesture *long_press = gtk_gesture_long_press_new(); |
| 1207 |
g_signal_connect(long_press, "pressed", G_CALLBACK(on_long_press), widget); |
| 1208 |
gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(long_press)); |
| 1209 |
GtkGesture *pinch_zoom = gtk_gesture_zoom_new(); |
| 1210 |
g_signal_connect(pinch_zoom, "scale-changed", G_CALLBACK(on_pinch_zoom), widget); |
| 1211 |
gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(pinch_zoom)); |
| 1212 |
/* Drag and drop */ |
| 1213 |
GtkDragSource *drag_source = gtk_drag_source_new(); |
| 1214 |
g_signal_connect(drag_source, "prepare", G_CALLBACK(on_prepare_drag), self); |
| 1215 |
g_signal_connect(drag_source, "drag-begin", G_CALLBACK(on_drag_begin), self); |
| 1216 |
gtk_widget_add_controller(widget, GTK_EVENT_CONTROLLER(drag_source)); |
| 1217 |
return; |
| 1218 |
} |
| 1219 |
|
| 1220 |
/** |
| 1221 |
* font_manager_unicode_character_map_get_active_cell: |
| 1222 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1223 |
* |
| 1224 |
* Returns: The currently selected cell |
| 1225 |
*/ |
| 1226 |
gint |
| 1227 |
font_manager_unicode_character_map_get_active_cell (FontManagerUnicodeCharacterMap *self) |
| 1228 |
{ |
| 1229 |
g_return_val_if_fail(FONT_MANAGER_IS_UNICODE_CHARACTER_MAP(self), 0); |
| 1230 |
return self->active_cell; |
| 1231 |
} |
| 1232 |
|
| 1233 |
/** |
| 1234 |
* font_manager_unicode_character_map_get_font_desc: |
| 1235 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1236 |
* |
| 1237 |
* Returns: (transfer none) (nullable): |
| 1238 |
* The #PangoFontDescription used to display the character table. |
| 1239 |
* The returned object is owned by the instance and must not be modified or freed. |
| 1240 |
*/ |
| 1241 |
PangoFontDescription * |
| 1242 |
font_manager_unicode_character_map_get_font_desc (FontManagerUnicodeCharacterMap *self) |
| 1243 |
{ |
| 1244 |
g_return_val_if_fail(FONT_MANAGER_IS_UNICODE_CHARACTER_MAP(self), NULL); |
| 1245 |
return self->font_desc; |
| 1246 |
} |
| 1247 |
|
| 1248 |
/** |
| 1249 |
* font_manager_unicode_character_map_get_preview_size: |
| 1250 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1251 |
* |
| 1252 |
* Returns: The current preview size |
| 1253 |
*/ |
| 1254 |
double |
| 1255 |
font_manager_unicode_character_map_get_preview_size (FontManagerUnicodeCharacterMap *self) |
| 1256 |
{ |
| 1257 |
g_return_val_if_fail(FONT_MANAGER_IS_UNICODE_CHARACTER_MAP(self), 0.0); |
| 1258 |
return self->preview_size; |
| 1259 |
} |
| 1260 |
|
| 1261 |
/** |
| 1262 |
* font_manager_unicode_character_map_get_index: |
| 1263 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1264 |
* @codepoints: (element-type uint) (transfer none): #GList of codepoints to get index of |
| 1265 |
* |
| 1266 |
* Returns: index of @codepoints |
| 1267 |
*/ |
| 1268 |
gint |
| 1269 |
font_manager_unicode_character_map_get_index (FontManagerUnicodeCharacterMap *self, |
| 1270 |
GSList *codepoints) |
| 1271 |
{ |
| 1272 |
return get_index(self, codepoints); |
| 1273 |
} |
| 1274 |
|
| 1275 |
/** |
| 1276 |
* font_manager_unicode_character_map_get_last_index: |
| 1277 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1278 |
* |
| 1279 |
* Returns: # of codepoints in character map |
| 1280 |
*/ |
| 1281 |
gint |
| 1282 |
font_manager_unicode_character_map_get_last_index (FontManagerUnicodeCharacterMap *self) |
| 1283 |
{ |
| 1284 |
return get_last_index(self); |
| 1285 |
} |
| 1286 |
|
| 1287 |
/** |
| 1288 |
* font_manager_unicode_character_map_get_codepoints: |
| 1289 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1290 |
* @index: #gint |
| 1291 |
* |
| 1292 |
* Returns: (element-type uint) (transfer full) (nullable): #GList of codepoints for @index |
| 1293 |
*/ |
| 1294 |
GSList * |
| 1295 |
font_manager_unicode_character_map_get_codepoints (FontManagerUnicodeCharacterMap *self, |
| 1296 |
gint index) |
| 1297 |
{ |
| 1298 |
return get_codepoints(self, index); |
| 1299 |
} |
| 1300 |
|
| 1301 |
/** |
| 1302 |
* font_manager_unicode_character_map_set_active_cell: |
| 1303 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1304 |
* @cell: cell to select |
| 1305 |
* |
| 1306 |
* Sets the currently selected cell for @self |
| 1307 |
*/ |
| 1308 |
void |
| 1309 |
font_manager_unicode_character_map_set_active_cell (FontManagerUnicodeCharacterMap *self, |
| 1310 |
gint cell) |
| 1311 |
{ |
| 1312 |
GtkWidget *widget = GTK_WIDGET(self); |
| 1313 |
cell = CLAMP(cell, 0, self->last_cell); |
| 1314 |
int old_active_cell = self->active_cell; |
| 1315 |
int old_page_first_cell = self->page_first_cell; |
| 1316 |
self->active_cell = cell; |
| 1317 |
if (cell < self->page_first_cell || cell >= self->page_first_cell + self->page_size) { |
| 1318 |
int old_row = old_active_cell / self->columns; |
| 1319 |
int new_row = cell / self->columns; |
| 1320 |
int new_page_first_cell = old_page_first_cell + ((new_row - old_row) * self->columns); |
| 1321 |
int last_row = (self->last_cell / self->columns) + 1; |
| 1322 |
int last_page_first_row = last_row - self->rows; |
| 1323 |
int last_page_first_cell = (last_page_first_row * self->columns) + 1; |
| 1324 |
self->page_first_cell = CLAMP(new_page_first_cell, 0, last_page_first_cell); |
| 1325 |
if (self->vadjustment) |
| 1326 |
gtk_adjustment_set_value(self->vadjustment, self->page_first_cell / self->columns); |
| 1327 |
} |
| 1328 |
gtk_widget_queue_draw(widget); |
| 1329 |
g_object_notify(G_OBJECT(self), "active-cell"); |
| 1330 |
return; |
| 1331 |
} |
| 1332 |
|
| 1333 |
/** |
| 1334 |
* font_manager_unicode_character_map_set_filter: |
| 1335 |
* @self: #FontManagerCodepointList |
| 1336 |
* @filter: (element-type uint) (transfer full) (nullable): #GList containing codepoints |
| 1337 |
* |
| 1338 |
* When a filter is set only codepoints which are in the filter and actually present |
| 1339 |
* in the selected font will be displayed. |
| 1340 |
*/ |
| 1341 |
void |
| 1342 |
font_manager_unicode_character_map_set_filter (FontManagerUnicodeCharacterMap *self, GList *filter) |
| 1343 |
{ |
| 1344 |
g_return_if_fail(self != NULL); |
| 1345 |
g_clear_pointer(&self->filter, g_list_free); |
| 1346 |
self->filter = filter; |
| 1347 |
self->is_regional_indicator_filter = is_regional_indicator_filter(filter); |
| 1348 |
self->last_cell = get_last_index(self); |
| 1349 |
gtk_widget_queue_resize(GTK_WIDGET(self)); |
| 1350 |
gtk_widget_queue_draw(GTK_WIDGET(self)); |
| 1351 |
font_manager_unicode_character_map_set_active_cell(self, 0); |
| 1352 |
return; |
| 1353 |
} |
| 1354 |
|
| 1355 |
/** |
| 1356 |
* font_manager_unicode_character_map_set_font_desc: |
| 1357 |
* @self: #FontManagerUnicodeCharacterMap |
| 1358 |
* @font_desc: (transfer none): #PangoFontDescription |
| 1359 |
* |
| 1360 |
* Sets @font_desc as the font to use to display the character table. |
| 1361 |
*/ |
| 1362 |
void |
| 1363 |
font_manager_unicode_character_map_set_font_desc (FontManagerUnicodeCharacterMap *self, |
| 1364 |
PangoFontDescription *font_desc) |
| 1365 |
{ |
| 1366 |
g_return_if_fail(FONT_MANAGER_IS_UNICODE_CHARACTER_MAP(self)); |
| 1367 |
g_return_if_fail(font_desc != NULL); |
| 1368 |
set_font_desc_internal(self, font_desc); |
| 1369 |
return; |
| 1370 |
} |
| 1371 |
|
| 1372 |
/** |
| 1373 |
* font_manager_unicode_character_map_set_preview_size: |
| 1374 |
* @self: a #FontManagerUnicodeCharacterMap |
| 1375 |
* @size: new preview size |
| 1376 |
* |
| 1377 |
* Sets the preview size to @size. |
| 1378 |
*/ |
| 1379 |
void |
| 1380 |
font_manager_unicode_character_map_set_preview_size (FontManagerUnicodeCharacterMap *self, |
| 1381 |
gdouble size) |
| 1382 |
{ |
| 1383 |
g_return_if_fail(FONT_MANAGER_IS_UNICODE_CHARACTER_MAP(self)); |
| 1384 |
self->preview_size = VALID_FONT_SIZE(size); |
| 1385 |
set_font_desc_internal(self, self->font_desc); |
| 1386 |
g_object_notify(G_OBJECT(self), "preview-size"); |
| 1387 |
return; |
| 1388 |
} |
| 1389 |
|
| 1390 |
/** |
| 1391 |
* font_manager_unicode_character_map_new: |
| 1392 |
* |
| 1393 |
* Returns: (transfer full): A newly created #FontManagerUnicodeCharacterMap. |
| 1394 |
* Free the returned object using #g_object_unref(). |
| 1395 |
*/ |
| 1396 |
GtkWidget * |
| 1397 |
font_manager_unicode_character_map_new (void) |
| 1398 |
{ |
| 1399 |
return GTK_WIDGET(g_object_new(FONT_MANAGER_TYPE_UNICODE_CHARACTER_MAP, NULL)); |
| 1400 |
} |
| 1401 |
|
| 1402 |
|