Debian Codegraph @

lib/gtk/unicode/unicode-character-info.c

raw
1 /* unicode-character-info.c
2 *
3 * Copyright (C) 2020-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 "unicode-character-info.h"
22
23 /**
24 * SECTION: unicode-character-info
25 * @short_description: Character info widget
26 * @title: Unicode Character Info
27 * @include: unicode-character-info.h
28 *
29 * Widget which displays information such as the codepoint and name of the
30 * currently selected character as well as how many codepoints are currently
31 * being displayed in the attached #UnicodeCharacterMap.
32 */
33
34 struct _FontManagerUnicodeCharacterInfo
35 {
36 GtkWidget parent;
37
38 GtkWidget *codepoint;
39 GtkWidget *name;
40 GtkWidget *n_codepoints;
41
42 FontManagerUnicodeCharacterMap *cmap;
43 };
44
45 G_DEFINE_TYPE(FontManagerUnicodeCharacterInfo, font_manager_unicode_character_info, GTK_TYPE_WIDGET)
46
47 enum
48 {
49 PROP_RESERVED,
50 PROP_CMAP,
51 N_PROPERTIES
52 };
53
54 static GParamSpec *obj_properties[N_PROPERTIES] = {0};
55
56 static void
57 font_manager_unicode_character_info_dispose (GObject *gobject)
58 {
59 g_return_if_fail(gobject != NULL);
60 FontManagerUnicodeCharacterInfo *self = FONT_MANAGER_UNICODE_CHARACTER_INFO(gobject);
61 g_clear_object(&self->cmap);
62 font_manager_widget_dispose(GTK_WIDGET(gobject));
63 G_OBJECT_CLASS(font_manager_unicode_character_info_parent_class)->dispose(gobject);
64 return;
65 }
66
67 static void
68 font_manager_unicode_character_info_get_property (GObject *gobject,
69 guint prop_id,
70 GValue *value,
71 GParamSpec *pspec)
72 {
73 g_return_if_fail(gobject != NULL);
74 FontManagerUnicodeCharacterInfo *self = FONT_MANAGER_UNICODE_CHARACTER_INFO(gobject);
75 switch (prop_id) {
76 case PROP_CMAP:
77 g_value_set_object(value, self->cmap);
78 break;
79 default:
80 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
81 break;
82 }
83 return;
84 }
85
86 static void
87 font_manager_unicode_character_info_set_property (GObject *gobject,
88 guint prop_id,
89 const GValue *value,
90 GParamSpec *pspec)
91 {
92 g_return_if_fail(gobject != NULL);
93 FontManagerUnicodeCharacterInfo *self = FONT_MANAGER_UNICODE_CHARACTER_INFO(gobject);
94 switch (prop_id) {
95 case PROP_CMAP:
96 font_manager_unicode_character_info_set_character_map(self, g_value_get_object(value));
97 break;
98 default:
99 G_OBJECT_WARN_INVALID_PROPERTY_ID(gobject, prop_id, pspec);
100 break;
101 }
102 return;
103 }
104
105 static void
106 font_manager_unicode_character_info_class_init (FontManagerUnicodeCharacterInfoClass *klass)
107 {
108 g_return_if_fail(klass != NULL);
109
110 GObjectClass *object_class = G_OBJECT_CLASS(klass);
111 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS(klass);
112
113 object_class->dispose = font_manager_unicode_character_info_dispose;
114 object_class->get_property = font_manager_unicode_character_info_get_property;
115 object_class->set_property = font_manager_unicode_character_info_set_property;
116
117 gtk_widget_class_set_layout_manager_type(widget_class, GTK_TYPE_BOX_LAYOUT);
118
119 /**
120 * FontManagerUnicodeCharacterInfo:character-map:
121 *
122 * #FontManagerUnicodeCharacterMap
123 */
124 obj_properties[PROP_CMAP] = g_param_spec_object("character-map",
125 NULL,
126 "FontManagerUnicodeCharacterMap",
127 G_TYPE_OBJECT,
128 G_PARAM_STATIC_STRINGS |
129 G_PARAM_READWRITE |
130 G_PARAM_EXPLICIT_NOTIFY);
131
132 g_object_class_install_property(object_class, PROP_CMAP, obj_properties[PROP_CMAP]);
133
134 return;
135 }
136
137 static void
138 font_manager_unicode_character_info_init (FontManagerUnicodeCharacterInfo *self)
139 {
140 g_return_if_fail(self != NULL);
141 self->cmap = NULL;
142 self->codepoint = gtk_label_new(NULL);
143 gtk_widget_set_can_focus(self->codepoint, FALSE);
144 gtk_label_set_selectable(GTK_LABEL(self->codepoint), TRUE);
145 self->name = gtk_label_new(NULL);
146 self->n_codepoints = gtk_label_new(NULL);
147 GtkWidget *center_box = gtk_center_box_new();
148 gtk_center_box_set_start_widget(GTK_CENTER_BOX(center_box), self->codepoint);
149 gtk_center_box_set_center_widget(GTK_CENTER_BOX(center_box), self->name);
150 gtk_widget_set_opacity(self->codepoint, 0.9);
151 gtk_widget_set_opacity(self->name, 0.9);
152 gtk_widget_set_hexpand(self->name, TRUE);
153 gtk_widget_set_vexpand(self->name, FALSE);
154 gtk_center_box_set_end_widget(GTK_CENTER_BOX(center_box), self->n_codepoints);
155 gtk_widget_set_hexpand(center_box, TRUE);
156 gtk_widget_set_vexpand(center_box, FALSE);
157 gtk_widget_set_parent(center_box, GTK_WIDGET(self));
158 gtk_widget_add_css_class(self->n_codepoints, "CharacterMapCount");
159 font_manager_widget_set_name(GTK_WIDGET(self), "FontManagerUnicodeCharacterInfo");
160 gtk_widget_set_hexpand(GTK_WIDGET(self), TRUE);
161 gtk_widget_set_vexpand(GTK_WIDGET(self), FALSE);
162 font_manager_widget_set_margin(center_box, 6);
163 gtk_widget_set_margin_start(center_box, 12);
164 gtk_widget_set_margin_end(center_box, 12);
165 gtk_widget_add_css_class(GTK_WIDGET(self), FONT_MANAGER_STYLE_CLASS_VIEW);
166 return;
167 }
168
169 static void
170 on_selection_changed (FontManagerUnicodeCharacterInfo *self,
171 const gchar *codepoint,
172 const gchar *name,
173 const gchar *n_codepoints)
174 {
175 g_return_if_fail(self != NULL);
176 gtk_label_set_label(GTK_LABEL(self->codepoint), codepoint);
177 gtk_label_set_label(GTK_LABEL(self->name), name);
178 gtk_label_set_label(GTK_LABEL(self->n_codepoints), n_codepoints);
179 return;
180 }
181
182 /**
183 * font_manager_unicode_character_info_set_character_map:
184 * @self: #FontManagerUnicodeCharacterInfo
185 * @character_map: (transfer none) (nullable): #FontManagerUnicodeCharacterMap
186 */
187 void
188 font_manager_unicode_character_info_set_character_map (FontManagerUnicodeCharacterInfo *self,
189 FontManagerUnicodeCharacterMap *character_map)
190 {
191 g_return_if_fail(self != NULL);
192 if (self->cmap)
193 g_signal_handlers_disconnect_by_func(self->cmap, G_CALLBACK(on_selection_changed), self);
194 if (g_set_object(&self->cmap, character_map))
195 g_object_notify_by_pspec(G_OBJECT(self), obj_properties[PROP_CMAP]);
196 if (self->cmap)
197 g_signal_connect_swapped(self->cmap, "selection-changed", G_CALLBACK(on_selection_changed), self);
198 /* Trigger first update */
199 gint active_cell = font_manager_unicode_character_map_get_active_cell(character_map);
200 font_manager_unicode_character_map_set_active_cell(character_map, active_cell != 0 ? 0 : 1);
201 font_manager_unicode_character_map_set_active_cell(character_map, active_cell);
202 return;
203 }
204
205 /**
206 * font_manager_unicode_character_info_new:
207 *
208 * Returns: (transfer full): A newly created #FontManagerUnicodeCharacterInfo.
209 * Free the returned object using #g_object_unref().
210 */
211 GtkWidget *
212 font_manager_unicode_character_info_new (void)
213 {
214 return g_object_new(FONT_MANAGER_TYPE_UNICODE_CHARACTER_INFO, NULL);
215 }