| 1 |
/* unicode-search-bar.c |
| 2 |
* |
| 3 |
* Originally a part of Gucharmap |
| 4 |
* |
| 5 |
* Copyright (C) 2017-2023 Jerry Casiano |
| 6 |
* |
| 7 |
* |
| 8 |
* Copyright © 2004 Noah Levitt |
| 9 |
* |
| 10 |
* This program is free software: you can redistribute it and/or modify |
| 11 |
* it under the terms of the GNU General Public License as published by |
| 12 |
* the Free Software Foundation, either version 3 of the License, or |
| 13 |
* (at your option) any later version. |
| 14 |
* |
| 15 |
* This program is distributed in the hope that it will be useful, |
| 16 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
* GNU General Public License for more details. |
| 19 |
* |
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with this program. |
| 22 |
* |
| 23 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "unicode-search-bar.h" |
| 27 |
|
| 28 |
/** |
| 29 |
* SECTION: unicode-search-bar |
| 30 |
* @short_description: Search for unicode codepoints within a font |
| 31 |
* @title: Unicode Search Bar |
| 32 |
* @include: unicode-search-bar.h |
| 33 |
* |
| 34 |
* This widget provides a way to search for characters by name, value or |
| 35 |
* other information. |
| 36 |
*/ |
| 37 |
|
| 38 |
typedef enum |
| 39 |
{ |
| 40 |
UNICODE_SEARCH_DIRECTION_BACKWARD = -1, |
| 41 |
UNICODE_SEARCH_DIRECTION_FORWARD = 1 |
| 42 |
} |
| 43 |
UnicodeSearchDirection; |
| 44 |
|
| 45 |
typedef struct _UnicodeSearchState UnicodeSearchState; |
| 46 |
|
| 47 |
struct _UnicodeSearchState |
| 48 |
{ |
| 49 |
UnicodeSearchDirection direction; |
| 50 |
FontManagerUnicodeCharacterMap *character_map; |
| 51 |
gint start_index; |
| 52 |
gint curr_index; |
| 53 |
gint match; /* index of the found character */ |
| 54 |
gint search_string_value; |
| 55 |
gint search_index_nfc; |
| 56 |
gint search_index_nfd; |
| 57 |
gint search_string_nfc_len; |
| 58 |
gint search_string_nfd_len; |
| 59 |
gboolean searching; |
| 60 |
/* true if there are known to be no matches, |
| 61 |
* or there is known to be exactly one match and it has been found */ |
| 62 |
gboolean search_complete; |
| 63 |
gboolean prepped; |
| 64 |
gchar *search_string; |
| 65 |
gchar *search_string_nfd; |
| 66 |
gchar *search_string_nfc; |
| 67 |
}; |
| 68 |
|
| 69 |
struct _FontManagerUnicodeSearchBar |
| 70 |
{ |
| 71 |
GtkWidget parent; |
| 72 |
|
| 73 |
GtkWidget *entry; |
| 74 |
GtkWidget *next_button; |
| 75 |
GtkWidget *prev_button; |
| 76 |
|
| 77 |
FontManagerUnicodeCharacterMap *character_map; |
| 78 |
UnicodeSearchState *search_state; |
| 79 |
}; |
| 80 |
|
| 81 |
G_DEFINE_TYPE(FontManagerUnicodeSearchBar, font_manager_unicode_search_bar, GTK_TYPE_WIDGET) |
| 82 |
|
| 83 |
enum |
| 84 |
{ |
| 85 |
PROP_RESERVED, |
| 86 |
PROP_CHARMAP, |
| 87 |
N_PROPERTIES |
| 88 |
}; |
| 89 |
|
| 90 |
static GParamSpec *obj_properties[N_PROPERTIES] = {0}; |
| 91 |
|
| 92 |
static const gchar * |
| 93 |
utf8_strcasestr (const gchar *haystack, const gchar *needle) |
| 94 |
{ |
| 95 |
const gchar *p, *q, *r; |
| 96 |
gint needle_len = strlen(needle); |
| 97 |
gint haystack_len = strlen(haystack); |
| 98 |
|
| 99 |
for (p = haystack; p + needle_len <= haystack + haystack_len; p = g_utf8_next_char(p)) { |
| 100 |
|
| 101 |
gboolean match = TRUE; |
| 102 |
|
| 103 |
for (q = needle, r = p; *q && *r; q = g_utf8_next_char(q), r = g_utf8_next_char(r)) { |
| 104 |
gunichar lc0 = g_unichar_tolower (g_utf8_get_char(r)); |
| 105 |
gunichar lc1 = g_unichar_tolower (g_utf8_get_char(q)); |
| 106 |
if (lc0 != lc1) { |
| 107 |
match = FALSE; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
if (match) |
| 113 |
return p; |
| 114 |
|
| 115 |
} |
| 116 |
|
| 117 |
return NULL; |
| 118 |
} |
| 119 |
|
| 120 |
static gboolean |
| 121 |
found_in_array (const gchar **haystack_arr, const gchar *search_string_nfd) |
| 122 |
{ |
| 123 |
gboolean matched = FALSE; |
| 124 |
if (haystack_arr) { |
| 125 |
for (gint i = 0; haystack_arr[i] != NULL; i++) { |
| 126 |
g_autofree gchar *haystack_nfd = g_utf8_normalize(haystack_arr[i], -1, G_NORMALIZE_NFD); |
| 127 |
matched = utf8_strcasestr(haystack_nfd, search_string_nfd) != NULL; |
| 128 |
if (matched) |
| 129 |
break; |
| 130 |
} |
| 131 |
g_free(haystack_arr); |
| 132 |
} |
| 133 |
return matched; |
| 134 |
} |
| 135 |
|
| 136 |
static gboolean |
| 137 |
matches (gunichar wc, const gchar *search_string_nfd) |
| 138 |
{ |
| 139 |
const gchar *haystack; |
| 140 |
gboolean matched = FALSE; |
| 141 |
|
| 142 |
haystack = font_manager_unicode_get_codepoint_data_name(wc); |
| 143 |
if (haystack) { |
| 144 |
/* character names are ascii, so are nfd */ |
| 145 |
gchar *haystack_nfd = (gchar *) haystack; |
| 146 |
matched = utf8_strcasestr(haystack_nfd, search_string_nfd) != NULL; |
| 147 |
} |
| 148 |
|
| 149 |
if (!matched) { |
| 150 |
haystack = font_manager_unicode_get_unicode_kDefinition(wc); |
| 151 |
if (haystack) { |
| 152 |
g_autofree gchar *haystack_nfd = g_utf8_normalize(haystack, -1, G_NORMALIZE_NFD); |
| 153 |
matched = utf8_strcasestr(haystack_nfd, search_string_nfd) != NULL; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if (!matched) |
| 158 |
matched = found_in_array(font_manager_unicode_get_nameslist_equals(wc), search_string_nfd); |
| 159 |
|
| 160 |
if (!matched) |
| 161 |
matched = found_in_array(font_manager_unicode_get_nameslist_stars(wc), search_string_nfd); |
| 162 |
|
| 163 |
if (!matched) |
| 164 |
matched = found_in_array(font_manager_unicode_get_nameslist_colons(wc), search_string_nfd); |
| 165 |
|
| 166 |
if (!matched) |
| 167 |
matched = found_in_array(font_manager_unicode_get_nameslist_pounds(wc), search_string_nfd); |
| 168 |
|
| 169 |
return matched; |
| 170 |
} |
| 171 |
|
| 172 |
static gint |
| 173 |
check_for_explicit_codepoint (FontManagerUnicodeCharacterMap *character_map, const gchar *string) |
| 174 |
{ |
| 175 |
/* Default to hex */ |
| 176 |
gint base = 16; |
| 177 |
gint index = -1, offset = 0; |
| 178 |
|
| 179 |
const gchar *nptr; |
| 180 |
gchar *endptr; |
| 181 |
gunichar wc; |
| 182 |
|
| 183 |
/* Check for explicit decimal codepoint */ |
| 184 |
if (*string == '#') { |
| 185 |
offset = 1; |
| 186 |
base = 10; |
| 187 |
} else if (g_ascii_strncasecmp(string, "&#", 2) == 0) { |
| 188 |
offset = 2; |
| 189 |
base = 10; |
| 190 |
/* Check for explicit hex codepoint */ |
| 191 |
} else if (g_ascii_strncasecmp(string, "&#x", 3) == 0) { |
| 192 |
offset = 3; |
| 193 |
} else if (g_ascii_strncasecmp(string, "U+", 2) == 0 || g_ascii_strncasecmp(string, "0x", 2) == 0) { |
| 194 |
offset = 2; |
| 195 |
} |
| 196 |
|
| 197 |
nptr = string + offset; |
| 198 |
|
| 199 |
if (nptr != string) { |
| 200 |
wc = strtoul(nptr, &endptr, base); |
| 201 |
if (endptr != nptr) { |
| 202 |
GSList *codepoints = NULL; |
| 203 |
codepoints = g_slist_append(codepoints, GINT_TO_POINTER(wc)); |
| 204 |
index = font_manager_unicode_character_map_get_index(character_map, codepoints); |
| 205 |
g_slist_free(codepoints); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/* Check for hex codepoint without any prefix. */ |
| 210 |
if (index < 0 && base > 10) { |
| 211 |
wc = strtoul(string, &endptr, base); |
| 212 |
if (endptr-3 >= string) { |
| 213 |
GSList *codepoints = NULL; |
| 214 |
codepoints = g_slist_append(codepoints, GINT_TO_POINTER(wc)); |
| 215 |
index = font_manager_unicode_character_map_get_index(character_map, codepoints); |
| 216 |
g_slist_free(codepoints); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return index; |
| 221 |
} |
| 222 |
|
| 223 |
static gboolean |
| 224 |
quick_checks_before (UnicodeSearchState *search_state) |
| 225 |
{ |
| 226 |
if (search_state->search_complete) |
| 227 |
return TRUE; |
| 228 |
|
| 229 |
if (search_state->prepped) |
| 230 |
return FALSE; |
| 231 |
|
| 232 |
search_state->prepped = TRUE; |
| 233 |
|
| 234 |
g_return_val_if_fail(search_state->search_string_nfd != NULL, FALSE); |
| 235 |
g_return_val_if_fail(search_state->search_string_nfc != NULL, FALSE); |
| 236 |
|
| 237 |
if (search_state->search_string_nfd[0] == '\0') { |
| 238 |
search_state->search_complete = TRUE; |
| 239 |
return TRUE; |
| 240 |
} |
| 241 |
|
| 242 |
/* if NFD of the search string is a single character, jump to that */ |
| 243 |
if (search_state->search_string_nfd_len == 1 && search_state->search_index_nfd != -1) { |
| 244 |
search_state->match = search_state->curr_index = search_state->search_index_nfd; |
| 245 |
search_state->search_complete = TRUE; |
| 246 |
return TRUE; |
| 247 |
} |
| 248 |
|
| 249 |
/* if NFC of the search string is a single character, jump to that */ |
| 250 |
if (search_state->search_string_nfc_len == 1 && search_state->search_index_nfc != -1) { |
| 251 |
search_state->match = search_state->curr_index = search_state->search_index_nfc; |
| 252 |
search_state->search_complete = TRUE; |
| 253 |
return TRUE; |
| 254 |
} |
| 255 |
|
| 256 |
return FALSE; |
| 257 |
} |
| 258 |
|
| 259 |
static gboolean |
| 260 |
quick_checks_after (UnicodeSearchState *search_state) |
| 261 |
{ |
| 262 |
/* jump to the first nonspace character unless it’s plain ascii */ |
| 263 |
if (search_state->search_string_nfd[0] < 0x20 || search_state->search_string_nfd[0] > 0x7e) { |
| 264 |
GSList *codepoints = NULL; |
| 265 |
codepoints = g_slist_append(codepoints, GINT_TO_POINTER(g_utf8_get_char(search_state->search_string_nfd))); |
| 266 |
gint index = font_manager_unicode_character_map_get_index (search_state->character_map, codepoints); |
| 267 |
g_slist_free(codepoints); |
| 268 |
if (index != -1) { |
| 269 |
search_state->match = index; |
| 270 |
search_state->search_complete = TRUE; |
| 271 |
return TRUE; |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
return FALSE; |
| 276 |
} |
| 277 |
|
| 278 |
static gboolean |
| 279 |
idle_search (FontManagerUnicodeSearchBar *self) |
| 280 |
{ |
| 281 |
g_return_val_if_fail(self != NULL, FALSE); |
| 282 |
gunichar wc; |
| 283 |
GTimer *timer; |
| 284 |
|
| 285 |
if (quick_checks_before (self->search_state)) |
| 286 |
return FALSE; |
| 287 |
|
| 288 |
timer = g_timer_new (); |
| 289 |
|
| 290 |
gint n_chars = font_manager_unicode_character_map_get_last_index(self->search_state->character_map); |
| 291 |
|
| 292 |
do { |
| 293 |
self->search_state->curr_index = (self->search_state->curr_index + self->search_state->direction + n_chars) % n_chars; |
| 294 |
|
| 295 |
/* check for explicit codepoint */ |
| 296 |
if (self->search_state->search_string_value != -1 && self->search_state->curr_index == self->search_state->search_string_value) { |
| 297 |
self->search_state->match = self->search_state->curr_index; |
| 298 |
self->search_state->search_complete = TRUE; |
| 299 |
g_timer_destroy (timer); |
| 300 |
return FALSE; |
| 301 |
} |
| 302 |
|
| 303 |
GSList *codepoints = font_manager_unicode_character_map_get_codepoints(self->search_state->character_map, self->search_state->curr_index); |
| 304 |
|
| 305 |
for (GSList *iter = codepoints; iter != NULL; iter = iter->next) { |
| 306 |
|
| 307 |
wc = (gunichar) GPOINTER_TO_INT(iter->data); |
| 308 |
|
| 309 |
if (!font_manager_unicode_unichar_validate (wc)) |
| 310 |
continue; |
| 311 |
|
| 312 |
/* check for other matches */ |
| 313 |
if (matches(wc, self->search_state->search_string_nfd)) { |
| 314 |
self->search_state->match = self->search_state->curr_index; |
| 315 |
g_timer_destroy (timer); |
| 316 |
return FALSE; |
| 317 |
} |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
g_slist_free(codepoints); |
| 322 |
|
| 323 |
if (g_timer_elapsed (timer, NULL) > 0.050) { |
| 324 |
g_timer_destroy (timer); |
| 325 |
return TRUE; |
| 326 |
} |
| 327 |
|
| 328 |
} while (self->search_state->curr_index != self->search_state->start_index); |
| 329 |
|
| 330 |
g_timer_destroy (timer); |
| 331 |
|
| 332 |
if (quick_checks_after (self->search_state)) |
| 333 |
return FALSE; |
| 334 |
|
| 335 |
self->search_state->search_complete = TRUE; |
| 336 |
|
| 337 |
return FALSE; |
| 338 |
} |
| 339 |
|
| 340 |
/** |
| 341 |
* unicode_search_state_free: |
| 342 |
* @search_state: #UnicodeSearchState |
| 343 |
**/ |
| 344 |
static void |
| 345 |
unicode_search_state_free (UnicodeSearchState *search_state) |
| 346 |
{ |
| 347 |
g_object_unref(search_state->character_map); |
| 348 |
g_free(search_state->search_string); |
| 349 |
g_free(search_state->search_string_nfd); |
| 350 |
g_free(search_state->search_string_nfc); |
| 351 |
g_slice_free(UnicodeSearchState, search_state); |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* unicode_search_state_new: |
| 356 |
* @character_map: a #FontManagerUnicodeCodepointList to be searched |
| 357 |
* @search_string: the text to search for |
| 358 |
* @start_index: the starting point within @character_map |
| 359 |
* @direction: forward or backward |
| 360 |
* |
| 361 |
* Initializes a #UnicodeSearchState to search for the next character in |
| 362 |
* the codepoint list that matches @search_string. Assumes input is valid. |
| 363 |
* |
| 364 |
* Return value: A newly created #UnicodeSearchState. |
| 365 |
* Free the returned object using #unicode_search_state_free(). |
| 366 |
**/ |
| 367 |
static UnicodeSearchState * |
| 368 |
unicode_search_state_new (FontManagerUnicodeCharacterMap *character_map, |
| 369 |
const gchar *search_string, |
| 370 |
gint start_index, |
| 371 |
UnicodeSearchDirection direction) |
| 372 |
{ |
| 373 |
UnicodeSearchState *search_state = g_slice_new(UnicodeSearchState); |
| 374 |
search_state->character_map = g_object_ref(character_map); |
| 375 |
search_state->direction = direction; |
| 376 |
search_state->prepped = FALSE; |
| 377 |
search_state->match = -1; |
| 378 |
search_state->search_complete = FALSE; |
| 379 |
search_state->start_index = start_index; |
| 380 |
search_state->curr_index = start_index; |
| 381 |
search_state->search_string = g_strstrip(g_strdup(search_string)); |
| 382 |
|
| 383 |
/* NFD */ |
| 384 |
search_state->search_string_nfd = g_utf8_normalize(search_string, -1, G_NORMALIZE_NFD); |
| 385 |
search_state->search_string_nfd_len = g_utf8_strlen(search_state->search_string_nfd, -1); |
| 386 |
|
| 387 |
if (search_state->search_string_nfd_len == 1) { |
| 388 |
GSList *codepoints = NULL; |
| 389 |
codepoints = g_slist_append(codepoints, GINT_TO_POINTER(g_utf8_get_char(search_state->search_string_nfd))); |
| 390 |
search_state->search_index_nfd = font_manager_unicode_character_map_get_index (search_state->character_map, codepoints); |
| 391 |
g_slist_free(codepoints); |
| 392 |
} else |
| 393 |
search_state->search_index_nfd = -1; |
| 394 |
|
| 395 |
/* NFC */ |
| 396 |
search_state->search_string_nfc = g_utf8_normalize(search_state->search_string_nfd, -1, G_NORMALIZE_NFC); |
| 397 |
search_state->search_string_nfc_len = g_utf8_strlen (search_state->search_string_nfc, -1); |
| 398 |
|
| 399 |
if (search_state->search_string_nfc_len == 1) { |
| 400 |
GSList *codepoints = NULL; |
| 401 |
codepoints = g_slist_append(codepoints, GINT_TO_POINTER(g_utf8_get_char(search_state->search_string_nfc))); |
| 402 |
search_state->search_index_nfc = font_manager_unicode_character_map_get_index(search_state->character_map, codepoints); |
| 403 |
g_slist_free(codepoints); |
| 404 |
} else |
| 405 |
search_state->search_index_nfc = -1; |
| 406 |
|
| 407 |
/* INDEX */ |
| 408 |
search_state->search_string_value = check_for_explicit_codepoint(search_state->character_map, search_state->search_string_nfd); |
| 409 |
search_state->searching = FALSE; |
| 410 |
return search_state; |
| 411 |
} |
| 412 |
|
| 413 |
static void |
| 414 |
set_action_visibility (FontManagerUnicodeSearchBar *self, gboolean visible) |
| 415 |
{ |
| 416 |
g_return_if_fail(self != NULL); |
| 417 |
gtk_widget_set_visible(GTK_WIDGET(self->prev_button), visible); |
| 418 |
gtk_widget_set_visible(GTK_WIDGET(self->next_button), visible); |
| 419 |
return; |
| 420 |
} |
| 421 |
|
| 422 |
static void |
| 423 |
search_completed (FontManagerUnicodeSearchBar *self) |
| 424 |
{ |
| 425 |
g_return_if_fail(self != NULL && self->character_map != NULL); |
| 426 |
UnicodeSearchState *search_state = self->search_state; |
| 427 |
gint index = search_state->match >= 0 ? search_state->match : -1; |
| 428 |
search_state->searching = FALSE; |
| 429 |
font_manager_unicode_character_map_set_active_cell(self->character_map, index); |
| 430 |
set_action_visibility(self, !search_state->search_complete); |
| 431 |
return; |
| 432 |
} |
| 433 |
|
| 434 |
static void |
| 435 |
unicode_search_start (FontManagerUnicodeSearchBar *self, UnicodeSearchDirection direction) |
| 436 |
{ |
| 437 |
g_return_if_fail(self != NULL && self->character_map != NULL); |
| 438 |
|
| 439 |
gint start_index; |
| 440 |
|
| 441 |
if (self->search_state && self->search_state->searching) /* Already searching */ |
| 442 |
return; |
| 443 |
|
| 444 |
if (self->search_state == NULL |
| 445 |
|| self->character_map != self->search_state->character_map |
| 446 |
|| strcmp (self->search_state->search_string, gtk_editable_get_text(GTK_EDITABLE(self->entry))) != 0 ) { |
| 447 |
|
| 448 |
g_clear_pointer(&self->search_state, unicode_search_state_free); |
| 449 |
|
| 450 |
start_index = font_manager_unicode_character_map_get_active_cell(self->character_map); |
| 451 |
self->search_state = unicode_search_state_new(self->character_map, |
| 452 |
gtk_editable_get_text(GTK_EDITABLE(self->entry)), |
| 453 |
start_index, direction ); |
| 454 |
} else { |
| 455 |
self->search_state->start_index = font_manager_unicode_character_map_get_active_cell(self->character_map); |
| 456 |
self->search_state->curr_index = self->search_state->start_index; |
| 457 |
self->search_state->direction = direction; |
| 458 |
} |
| 459 |
|
| 460 |
self->search_state->searching = TRUE; |
| 461 |
g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, (GSourceFunc) idle_search, self, (GDestroyNotify) search_completed); |
| 462 |
return; |
| 463 |
} |
| 464 |
|
| 465 |
static void |
| 466 |
on_prev_button_clicked (FontManagerUnicodeSearchBar *self, G_GNUC_UNUSED GtkWidget *widget) { |
| 467 |
unicode_search_start(self, UNICODE_SEARCH_DIRECTION_BACKWARD); |
| 468 |
return; |
| 469 |
} |
| 470 |
|
| 471 |
static void |
| 472 |
on_next_button_clicked (FontManagerUnicodeSearchBar *self, G_GNUC_UNUSED GtkWidget *widget) { |
| 473 |
unicode_search_start(self, UNICODE_SEARCH_DIRECTION_FORWARD); |
| 474 |
return; |
| 475 |
} |
| 476 |
|
| 477 |
static void |
| 478 |
on_map_event (FontManagerUnicodeSearchBar *self, |
| 479 |
G_GNUC_UNUSED GdkEvent *event, |
| 480 |
G_GNUC_UNUSED GtkWidget *widget) |
| 481 |
{ |
| 482 |
if (gtk_widget_get_mapped(GTK_WIDGET(self))) |
| 483 |
gtk_widget_grab_focus(GTK_WIDGET(self->entry)); |
| 484 |
return; |
| 485 |
} |
| 486 |
|
| 487 |
static guint search_timeout = 0; |
| 488 |
|
| 489 |
static gboolean |
| 490 |
_entry_changed (FontManagerUnicodeSearchBar *self) |
| 491 |
{ |
| 492 |
unicode_search_start(self, UNICODE_SEARCH_DIRECTION_FORWARD); |
| 493 |
search_timeout = 0; |
| 494 |
return FALSE; |
| 495 |
} |
| 496 |
|
| 497 |
static void |
| 498 |
entry_changed (FontManagerUnicodeSearchBar *self, G_GNUC_UNUSED GtkWidget *widget) |
| 499 |
{ |
| 500 |
g_return_if_fail(self != NULL && self->character_map != NULL); |
| 501 |
set_action_visibility(self, FALSE); |
| 502 |
g_autofree gchar *entry_text = g_strstrip(g_strdup(gtk_editable_get_text(GTK_EDITABLE(self->entry)))); |
| 503 |
|
| 504 |
if (strlen(entry_text) != 0) { |
| 505 |
if (search_timeout > 0) { |
| 506 |
g_source_remove(search_timeout); |
| 507 |
search_timeout = 0; |
| 508 |
} |
| 509 |
search_timeout = g_timeout_add(500, (GSourceFunc) _entry_changed, self); |
| 510 |
} else { |
| 511 |
font_manager_unicode_character_map_set_active_cell(self->character_map, 0); |
| 512 |
} |
| 513 |
|
| 514 |
return; |
| 515 |
} |
| 516 |
|
| 517 |
static void |
| 518 |
font_manager_unicode_search_bar_set_property (GObject *gobject, |
| 519 |
guint prop_id, |
| 520 |
const GValue *value, |
| 521 |
GParamSpec *pspec) |
| 522 |
{ |
| 523 |
g_return_if_fail(gobject != NULL); |
| 524 |
FontManagerUnicodeSearchBar *self = FONT_MANAGER_UNICODE_SEARCH_BAR(gobject); |
| 525 |
switch (prop_id) { |
| 526 |
case PROP_CHARMAP: |
| 527 |
font_manager_unicode_search_bar_set_character_map(self, g_value_get_object(value)); |
| 528 |
break; |
| 529 |
default: |
| 530 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec); |
| 531 |
break; |
| 532 |
} |
| 533 |
return; |
| 534 |
} |
| 535 |
|
| 536 |
static void |
| 537 |
font_manager_unicode_search_bar_get_property (GObject *gobject, |
| 538 |
guint prop_id, |
| 539 |
GValue *value, |
| 540 |
GParamSpec *pspec) |
| 541 |
{ |
| 542 |
g_return_if_fail(gobject != NULL); |
| 543 |
FontManagerUnicodeSearchBar *self = FONT_MANAGER_UNICODE_SEARCH_BAR(gobject); |
| 544 |
switch (prop_id) { |
| 545 |
case PROP_CHARMAP: |
| 546 |
g_value_set_object(value, self->character_map); |
| 547 |
break; |
| 548 |
default: |
| 549 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec); |
| 550 |
break; |
| 551 |
} |
| 552 |
return; |
| 553 |
} |
| 554 |
|
| 555 |
static void |
| 556 |
font_manager_unicode_search_bar_dispose (GObject *gobject) |
| 557 |
{ |
| 558 |
g_return_if_fail(gobject != NULL); |
| 559 |
FontManagerUnicodeSearchBar *self = FONT_MANAGER_UNICODE_SEARCH_BAR(gobject); |
| 560 |
g_clear_pointer(&self->search_state, unicode_search_state_free); |
| 561 |
g_clear_object(&self->character_map); |
| 562 |
font_manager_widget_dispose(GTK_WIDGET(gobject)); |
| 563 |
G_OBJECT_CLASS(font_manager_unicode_search_bar_parent_class)->dispose(gobject); |
| 564 |
return; |
| 565 |
} |
| 566 |
|
| 567 |
static void |
| 568 |
font_manager_unicode_search_bar_constructed (GObject *gobject) |
| 569 |
{ |
| 570 |
g_return_if_fail(gobject != NULL); |
| 571 |
FontManagerUnicodeSearchBar *self = FONT_MANAGER_UNICODE_SEARCH_BAR(gobject); |
| 572 |
set_action_visibility(self, FALSE); |
| 573 |
g_signal_connect_swapped(self->entry, "search-changed", G_CALLBACK(entry_changed), self); |
| 574 |
g_signal_connect_swapped(self->entry, "previous-match", G_CALLBACK(on_prev_button_clicked), self); |
| 575 |
g_signal_connect_swapped(self->entry, "next-match", G_CALLBACK(on_next_button_clicked), self); |
| 576 |
g_signal_connect_swapped(self->entry, "map", G_CALLBACK(on_map_event), self); |
| 577 |
g_signal_connect_swapped(self->prev_button, "clicked", G_CALLBACK(on_prev_button_clicked), self); |
| 578 |
g_signal_connect_swapped(self->next_button, "clicked", G_CALLBACK(on_next_button_clicked), self); |
| 579 |
G_OBJECT_CLASS(font_manager_unicode_search_bar_parent_class)->constructed(gobject); |
| 580 |
return; |
| 581 |
} |
| 582 |
|
| 583 |
static void |
| 584 |
font_manager_unicode_search_bar_class_init (FontManagerUnicodeSearchBarClass *klass) |
| 585 |
{ |
| 586 |
g_return_if_fail(klass != NULL); |
| 587 |
|
| 588 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 589 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| 590 |
|
| 591 |
object_class->constructed = font_manager_unicode_search_bar_constructed; |
| 592 |
object_class->dispose = font_manager_unicode_search_bar_dispose; |
| 593 |
object_class->get_property = font_manager_unicode_search_bar_get_property; |
| 594 |
object_class->set_property = font_manager_unicode_search_bar_set_property; |
| 595 |
|
| 596 |
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BOX_LAYOUT); |
| 597 |
|
| 598 |
obj_properties[PROP_CHARMAP] = g_param_spec_object("character-map", |
| 599 |
NULL, |
| 600 |
"FontManagerUnicodeCharacterMap", |
| 601 |
G_TYPE_OBJECT, |
| 602 |
G_PARAM_STATIC_STRINGS | |
| 603 |
G_PARAM_READWRITE | |
| 604 |
G_PARAM_EXPLICIT_NOTIFY); |
| 605 |
|
| 606 |
g_object_class_install_property(object_class, PROP_CHARMAP, obj_properties[PROP_CHARMAP]); |
| 607 |
|
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
static void |
| 612 |
font_manager_unicode_search_bar_init (FontManagerUnicodeSearchBar *self) |
| 613 |
{ |
| 614 |
g_return_if_fail(self != NULL); |
| 615 |
self->character_map = NULL; |
| 616 |
GtkWidget *search_bar = gtk_search_bar_new(); |
| 617 |
GtkWidget *center_box = gtk_center_box_new(); |
| 618 |
self->prev_button = gtk_button_new_from_icon_name("go-previous-symbolic"); |
| 619 |
self->entry = gtk_search_entry_new(); |
| 620 |
self->next_button = gtk_button_new_from_icon_name("go-next-symbolic"); |
| 621 |
gtk_center_box_set_start_widget(GTK_CENTER_BOX(center_box), self->prev_button); |
| 622 |
gtk_widget_set_margin_start(self->prev_button, 3); |
| 623 |
gtk_widget_set_margin_end(self->prev_button, 3); |
| 624 |
gtk_center_box_set_center_widget(GTK_CENTER_BOX(center_box), self->entry); |
| 625 |
gtk_center_box_set_end_widget(GTK_CENTER_BOX(center_box), self->next_button); |
| 626 |
gtk_widget_set_margin_start(self->next_button, 3); |
| 627 |
gtk_widget_set_margin_end(self->next_button, 3); |
| 628 |
gtk_search_bar_set_child(GTK_SEARCH_BAR(search_bar), center_box); |
| 629 |
gtk_search_bar_set_search_mode(GTK_SEARCH_BAR(search_bar), TRUE); |
| 630 |
gtk_widget_set_parent(search_bar, GTK_WIDGET(self)); |
| 631 |
font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerUnicodeSearchBar"); |
| 632 |
gtk_widget_set_opacity(self->prev_button, 0.75); |
| 633 |
gtk_widget_set_opacity(self->next_button, 0.75); |
| 634 |
font_manager_widget_set_expand(GTK_WIDGET(search_bar), TRUE); |
| 635 |
gtk_widget_set_hexpand(GTK_WIDGET(self), TRUE); |
| 636 |
gtk_widget_set_vexpand(GTK_WIDGET(self), FALSE); |
| 637 |
return; |
| 638 |
} |
| 639 |
|
| 640 |
static void |
| 641 |
reset_search (G_GNUC_UNUSED FontManagerUnicodeCharacterMap *charmap, |
| 642 |
G_GNUC_UNUSED const gchar *status_message, |
| 643 |
FontManagerUnicodeSearchBar *self) |
| 644 |
{ |
| 645 |
g_return_if_fail(self != NULL); |
| 646 |
if (gtk_widget_get_mapped(GTK_WIDGET(self->entry)) && |
| 647 |
strlen(gtk_editable_get_text(GTK_EDITABLE(self->entry))) > 0) { |
| 648 |
g_autofree gchar *previous_text = g_strdup(gtk_editable_get_text(GTK_EDITABLE(self->entry))); |
| 649 |
gtk_editable_set_text(GTK_EDITABLE(self->entry), " "); |
| 650 |
gtk_editable_set_text(GTK_EDITABLE(self->entry), previous_text); |
| 651 |
} |
| 652 |
return; |
| 653 |
} |
| 654 |
|
| 655 |
/** |
| 656 |
* font_manager_unicode_search_bar_set_character_map: |
| 657 |
* @self: #FontManagerUnicodeSearchBar |
| 658 |
* @character_map: (transfer none) (nullable): #FontManagerUnicodeCharacterMap |
| 659 |
*/ |
| 660 |
void |
| 661 |
font_manager_unicode_search_bar_set_character_map (FontManagerUnicodeSearchBar *self, |
| 662 |
FontManagerUnicodeCharacterMap *character_map) |
| 663 |
{ |
| 664 |
g_return_if_fail(self != NULL); |
| 665 |
if (self->character_map) |
| 666 |
g_signal_handlers_disconnect_by_func(self->character_map, G_CALLBACK(reset_search), self); |
| 667 |
if (g_set_object(&self->character_map, character_map)) |
| 668 |
g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_CHARMAP]); |
| 669 |
if (self->character_map) { |
| 670 |
g_signal_connect_after(self->character_map, "notify::font-desc", G_CALLBACK(reset_search), self); |
| 671 |
g_signal_connect_after(self->character_map, "notify::codepoint-list", G_CALLBACK(reset_search), self); |
| 672 |
} |
| 673 |
return; |
| 674 |
} |
| 675 |
|
| 676 |
/** |
| 677 |
* font_manager_unicode_search_bar_new: |
| 678 |
* |
| 679 |
* Returns: (transfer full): A newly created #FontManagerUnicodeSearchBar. |
| 680 |
* Free the returned object using #g_object_unref(). |
| 681 |
*/ |
| 682 |
GtkWidget * |
| 683 |
font_manager_unicode_search_bar_new (void) |
| 684 |
{ |
| 685 |
return g_object_new(FONT_MANAGER_TYPE_UNICODE_SEARCH_BAR, NULL); |
| 686 |
} |