| 1 |
/* font-manager-character-map.c |
| 2 |
* |
| 3 |
* Copyright (C) 2009-2023 Jerry Casiano |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. |
| 17 |
* |
| 18 |
* If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "font-manager-character-map.h" |
| 22 |
|
| 23 |
/** |
| 24 |
* SECTION: font-manager-character-map |
| 25 |
* @short_description: Browse available characters |
| 26 |
* @title: Character Map |
| 27 |
* @include: font-manager-character-map.h |
| 28 |
* |
| 29 |
* Widget which displays available characters in the selected font. |
| 30 |
* It also provides basic information about the selected character and the |
| 31 |
* ability to search for a specific character based on codepoint, name or |
| 32 |
* other details. |
| 33 |
*/ |
| 34 |
|
| 35 |
struct _FontManagerCharacterMap |
| 36 |
{ |
| 37 |
GtkBox parent; |
| 38 |
|
| 39 |
gint active_cell; |
| 40 |
gdouble preview_size; |
| 41 |
|
| 42 |
GtkWidget *action_area; |
| 43 |
GtkWidget *character_map; |
| 44 |
GtkWidget *character_info; |
| 45 |
GtkWidget *fontscale; |
| 46 |
GtkWidget *search; |
| 47 |
|
| 48 |
FontManagerFont *font; |
| 49 |
}; |
| 50 |
|
| 51 |
G_DEFINE_TYPE(FontManagerCharacterMap, font_manager_character_map, GTK_TYPE_WIDGET) |
| 52 |
|
| 53 |
enum |
| 54 |
{ |
| 55 |
PROP_RESERVED, |
| 56 |
PROP_FONT, |
| 57 |
PROP_ACTIVE_CELL, |
| 58 |
PROP_PREVIEW_SIZE, |
| 59 |
PROP_SEARCH_MODE, |
| 60 |
N_PROPERTIES |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
static GParamSpec *obj_properties[N_PROPERTIES] = { NULL, }; |
| 65 |
|
| 66 |
void |
| 67 |
font_manager_character_map_set_active_cell (FontManagerCharacterMap *self, |
| 68 |
gint cell) |
| 69 |
{ |
| 70 |
g_return_if_fail(self != NULL); |
| 71 |
self->active_cell = cell; |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
static void |
| 76 |
font_manager_character_map_dispose (GObject *gobject) |
| 77 |
{ |
| 78 |
g_return_if_fail(gobject != NULL); |
| 79 |
FontManagerCharacterMap *self = FONT_MANAGER_CHARACTER_MAP(gobject); |
| 80 |
g_clear_pointer(&self->font, g_object_unref); |
| 81 |
font_manager_widget_dispose(GTK_WIDGET(self)); |
| 82 |
G_OBJECT_CLASS(font_manager_character_map_parent_class)->dispose(gobject); |
| 83 |
return; |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
font_manager_character_map_get_property (GObject *gobject, |
| 88 |
guint property_id, |
| 89 |
GValue *value, |
| 90 |
GParamSpec *pspec) |
| 91 |
{ |
| 92 |
g_return_if_fail(gobject != NULL); |
| 93 |
FontManagerCharacterMap *self = FONT_MANAGER_CHARACTER_MAP(gobject); |
| 94 |
GtkWidget *child = NULL; |
| 95 |
switch (property_id) { |
| 96 |
case PROP_FONT: |
| 97 |
g_value_set_object(value, self->font); |
| 98 |
break; |
| 99 |
case PROP_ACTIVE_CELL: |
| 100 |
g_value_set_int(value, self->active_cell); |
| 101 |
break; |
| 102 |
case PROP_SEARCH_MODE: |
| 103 |
child = gtk_stack_get_visible_child(GTK_STACK(self->action_area)); |
| 104 |
g_value_set_boolean(value, child == self->search); |
| 105 |
break; |
| 106 |
case PROP_PREVIEW_SIZE: |
| 107 |
g_value_set_double(value, self->preview_size); |
| 108 |
break; |
| 109 |
default: |
| 110 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 111 |
} |
| 112 |
return; |
| 113 |
} |
| 114 |
|
| 115 |
static void |
| 116 |
font_manager_character_map_set_property (GObject *gobject, |
| 117 |
guint property_id, |
| 118 |
const GValue *value, |
| 119 |
GParamSpec *pspec) |
| 120 |
{ |
| 121 |
g_return_if_fail(gobject != NULL); |
| 122 |
FontManagerCharacterMap *self = FONT_MANAGER_CHARACTER_MAP(gobject); |
| 123 |
GtkWidget *child = NULL; |
| 124 |
switch (property_id) { |
| 125 |
case PROP_FONT: |
| 126 |
font_manager_character_map_set_font(self, g_value_get_object(value)); |
| 127 |
break; |
| 128 |
case PROP_ACTIVE_CELL: |
| 129 |
font_manager_character_map_set_active_cell(self, g_value_get_int(value)); |
| 130 |
break; |
| 131 |
case PROP_SEARCH_MODE: |
| 132 |
child = g_value_get_boolean(value) ? self->search : self->fontscale; |
| 133 |
gtk_stack_set_visible_child(GTK_STACK(self->action_area), child); |
| 134 |
break; |
| 135 |
case PROP_PREVIEW_SIZE: |
| 136 |
self->preview_size = g_value_get_double(value); |
| 137 |
break; |
| 138 |
default: |
| 139 |
G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, property_id, pspec); |
| 140 |
} |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
static void |
| 146 |
font_manager_character_map_class_init (FontManagerCharacterMapClass *klass) |
| 147 |
{ |
| 148 |
GObjectClass *object_class = G_OBJECT_CLASS(klass); |
| 149 |
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass); |
| 150 |
|
| 151 |
object_class->dispose = font_manager_character_map_dispose; |
| 152 |
object_class->get_property = font_manager_character_map_get_property; |
| 153 |
object_class->set_property = font_manager_character_map_set_property; |
| 154 |
gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BIN_LAYOUT); |
| 155 |
|
| 156 |
/** |
| 157 |
* FontManagerCharacterMap:font: |
| 158 |
* |
| 159 |
* #FontManagerFont |
| 160 |
*/ |
| 161 |
obj_properties[PROP_FONT] = g_param_spec_object("font", |
| 162 |
NULL, |
| 163 |
"FontManagerFont", |
| 164 |
FONT_MANAGER_TYPE_FONT, |
| 165 |
G_PARAM_READWRITE | |
| 166 |
G_PARAM_STATIC_STRINGS); |
| 167 |
|
| 168 |
/** |
| 169 |
* FontManagerCharacterMap:active-cell: |
| 170 |
* |
| 171 |
* Currently selected cell in character map |
| 172 |
*/ |
| 173 |
obj_properties[PROP_ACTIVE_CELL] = g_param_spec_int("active-cell", |
| 174 |
NULL, |
| 175 |
"Active cell in character map", |
| 176 |
G_MININT, |
| 177 |
G_MAXINT, |
| 178 |
0, |
| 179 |
G_PARAM_READWRITE | |
| 180 |
G_PARAM_STATIC_STRINGS); |
| 181 |
|
| 182 |
/** |
| 183 |
* FontManagerCharacterMap:preview-size: |
| 184 |
* |
| 185 |
* Font preview size |
| 186 |
*/ |
| 187 |
obj_properties[PROP_PREVIEW_SIZE] = g_param_spec_double("preview-size", |
| 188 |
NULL, |
| 189 |
"Preview size", |
| 190 |
FONT_MANAGER_MIN_FONT_SIZE, |
| 191 |
FONT_MANAGER_MAX_FONT_SIZE, |
| 192 |
FONT_MANAGER_LARGE_PREVIEW_SIZE, |
| 193 |
G_PARAM_STATIC_STRINGS | |
| 194 |
G_PARAM_READWRITE); |
| 195 |
|
| 196 |
/** |
| 197 |
* FontManagerCharacterMap:search-mode: |
| 198 |
* |
| 199 |
* Whether search mode is active or not |
| 200 |
*/ |
| 201 |
obj_properties[PROP_SEARCH_MODE] = g_param_spec_boolean("search-mode", |
| 202 |
NULL, |
| 203 |
"Whether search mode is active or not", |
| 204 |
FALSE, |
| 205 |
G_PARAM_STATIC_STRINGS | |
| 206 |
G_PARAM_READWRITE); |
| 207 |
|
| 208 |
g_object_class_install_properties(object_class, N_PROPERTIES, obj_properties); |
| 209 |
return; |
| 210 |
} |
| 211 |
|
| 212 |
GtkWidget * |
| 213 |
create_action_area (FontManagerCharacterMap *self) |
| 214 |
{ |
| 215 |
self->action_area = gtk_stack_new(); |
| 216 |
self->fontscale = font_manager_font_scale_new(); |
| 217 |
self->search = font_manager_unicode_search_bar_new(); |
| 218 |
gtk_stack_add_named(GTK_STACK(self->action_area), self->fontscale, gtk_widget_get_name(self->fontscale)); |
| 219 |
gtk_stack_add_named(GTK_STACK(self->action_area), self->search, gtk_widget_get_name(self->search)); |
| 220 |
gtk_stack_set_visible_child(GTK_STACK(self->action_area), self->fontscale); |
| 221 |
gtk_stack_set_transition_type(GTK_STACK(self->action_area), GTK_STACK_TRANSITION_TYPE_CROSSFADE); |
| 222 |
gtk_stack_set_vhomogeneous(GTK_STACK(self->action_area), FALSE); |
| 223 |
return self->action_area; |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
font_manager_character_map_init (FontManagerCharacterMap *self) |
| 228 |
{ |
| 229 |
g_return_if_fail(self != NULL); |
| 230 |
font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerCharacterMap"); |
| 231 |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); |
| 232 |
self->character_map = font_manager_unicode_character_map_new(); |
| 233 |
GtkWidget *info_widget = font_manager_unicode_character_info_new(); |
| 234 |
FontManagerUnicodeCharacterMap *charmap = FONT_MANAGER_UNICODE_CHARACTER_MAP(self->character_map); |
| 235 |
FontManagerUnicodeCharacterInfo *charinfo = FONT_MANAGER_UNICODE_CHARACTER_INFO(info_widget); |
| 236 |
font_manager_unicode_character_info_set_character_map(charinfo, charmap); |
| 237 |
gtk_box_append(GTK_BOX(box), info_widget); |
| 238 |
GtkWidget *scroll = gtk_scrolled_window_new(); |
| 239 |
gtk_scrolled_window_set_child(GTK_SCROLLED_WINDOW(scroll), self->character_map); |
| 240 |
gtk_box_append(GTK_BOX(box), scroll); |
| 241 |
GtkWidget *action_area = create_action_area(self); |
| 242 |
gtk_box_append(GTK_BOX(box), action_area); |
| 243 |
FontManagerUnicodeSearchBar *search_bar = FONT_MANAGER_UNICODE_SEARCH_BAR(self->search); |
| 244 |
font_manager_unicode_search_bar_set_character_map(search_bar, charmap); |
| 245 |
font_manager_font_scale_set_default_size(FONT_MANAGER_FONT_SCALE(self->fontscale), |
| 246 |
FONT_MANAGER_LARGE_PREVIEW_SIZE); |
| 247 |
self->preview_size = FONT_MANAGER_LARGE_PREVIEW_SIZE; |
| 248 |
gtk_widget_set_parent(box, GTK_WIDGET(self)); |
| 249 |
font_manager_widget_set_expand(GTK_WIDGET(box), TRUE); |
| 250 |
font_manager_widget_set_expand(GTK_WIDGET(charmap), TRUE); |
| 251 |
font_manager_widget_set_expand(GTK_WIDGET(scroll), TRUE); |
| 252 |
font_manager_widget_set_expand(GTK_WIDGET(self), TRUE); |
| 253 |
gtk_widget_set_valign(info_widget, GTK_ALIGN_START); |
| 254 |
gtk_widget_set_valign(action_area, GTK_ALIGN_END); |
| 255 |
GBindingFlags flags = G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL; |
| 256 |
g_object_bind_property(self, "preview-size", self->fontscale, "value", flags); |
| 257 |
g_object_bind_property(self, "preview-size", self->character_map, "preview-size", flags); |
| 258 |
g_object_bind_property(self, "active-cell", self->character_map, "active-cell", flags); |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* font_manager_character_map_set_font: |
| 264 |
* @self: #FontManagerCharacterMap |
| 265 |
* @font: (transfer none): #FontManagerFont |
| 266 |
*/ |
| 267 |
void |
| 268 |
font_manager_character_map_set_font (FontManagerCharacterMap *self, |
| 269 |
FontManagerFont *font) |
| 270 |
{ |
| 271 |
g_return_if_fail(self != NULL); |
| 272 |
g_set_object(&self->font, font); |
| 273 |
FontManagerUnicodeCharacterMap *charmap = FONT_MANAGER_UNICODE_CHARACTER_MAP(self->character_map); |
| 274 |
g_autofree gchar *description = NULL; |
| 275 |
if (font) |
| 276 |
g_object_get(font, "description", &description, NULL); |
| 277 |
else |
| 278 |
description = g_strdup(FONT_MANAGER_DEFAULT_FONT); |
| 279 |
g_autoptr(PangoFontDescription) font_desc = pango_font_description_from_string(description); |
| 280 |
font_manager_unicode_character_map_set_font_desc(charmap, font_desc); |
| 281 |
return; |
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* font_manager_character_map_set_filter: |
| 286 |
* @self: #FontManagerCharacterMap |
| 287 |
* @filter:(element-type uint) (transfer full) (nullable): #GList of codepoints |
| 288 |
*/ |
| 289 |
void |
| 290 |
font_manager_character_map_set_filter (FontManagerCharacterMap *self, GList *filter) |
| 291 |
{ |
| 292 |
g_return_if_fail(self != NULL); |
| 293 |
FontManagerUnicodeCharacterMap *charmap = FONT_MANAGER_UNICODE_CHARACTER_MAP(self->character_map); |
| 294 |
font_manager_unicode_character_map_set_filter(charmap, filter); |
| 295 |
return; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* font_manager_character_map_restore_state: |
| 300 |
* @self: #FontManagerCharacterMap |
| 301 |
* @settings: #GSettings |
| 302 |
* |
| 303 |
* Applies the values in @settings to @self and also binds those settings to their |
| 304 |
* respective properties so that they are updated when any changes take place. |
| 305 |
* |
| 306 |
* The following keys MUST be present in @settings: |
| 307 |
* |
| 308 |
* - charmap-font-size |
| 309 |
*/ |
| 310 |
void |
| 311 |
font_manager_character_map_restore_state (FontManagerCharacterMap *self, GSettings *settings) |
| 312 |
{ |
| 313 |
g_return_if_fail(self != NULL); |
| 314 |
g_return_if_fail(settings != NULL); |
| 315 |
g_settings_bind(settings, "charmap-font-size", self, "preview-size", G_SETTINGS_BIND_DEFAULT); |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
/** |
| 320 |
* font_manager_character_map_new: |
| 321 |
* |
| 322 |
* Returns: (transfer full): A newly created #FontManagerCharacterMap. |
| 323 |
* Free the returned object using #g_object_unref(). |
| 324 |
*/ |
| 325 |
GtkWidget * |
| 326 |
font_manager_character_map_new () |
| 327 |
{ |
| 328 |
return g_object_new(FONT_MANAGER_TYPE_CHARACTER_MAP, NULL); |
| 329 |
} |
| 330 |
|