diff options
Diffstat (limited to 'src/ui/picker.c')
-rw-r--r-- | src/ui/picker.c | 251 |
1 files changed, 251 insertions, 0 deletions
diff --git a/src/ui/picker.c b/src/ui/picker.c new file mode 100644 index 0000000..f6c89a6 --- /dev/null +++ b/src/ui/picker.c | |||
@@ -0,0 +1,251 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2021 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /* | ||
21 | * @author Tobias Frisch | ||
22 | * @file ui/picker.c | ||
23 | */ | ||
24 | |||
25 | #include "picker.h" | ||
26 | |||
27 | #include "../application.h" | ||
28 | |||
29 | #include <emoji.h> | ||
30 | |||
31 | static void | ||
32 | handle_emoji_button_click(GtkButton *button, | ||
33 | gpointer user_data) | ||
34 | { | ||
35 | GtkTextView *text_view = GTK_TEXT_VIEW(user_data); | ||
36 | GtkTextBuffer *text_buffer = gtk_text_view_get_buffer(text_view); | ||
37 | |||
38 | const gchar *label = gtk_button_get_label(button); | ||
39 | |||
40 | if (label) | ||
41 | gtk_text_buffer_insert_at_cursor(text_buffer, label, strlen(label)); | ||
42 | } | ||
43 | |||
44 | static void | ||
45 | _add_emoji_buttons(GtkFlowBox *flow_box, | ||
46 | GtkTextView *text_view, | ||
47 | size_t characters_count, | ||
48 | const uint32_t *characters) | ||
49 | { | ||
50 | for (size_t i = 0; i < characters_count; i++) { | ||
51 | GString *string = g_string_new(""); | ||
52 | g_string_append_unichar(string, (gunichar) characters[i]); | ||
53 | |||
54 | GtkButton *emoji_button = GTK_BUTTON( | ||
55 | gtk_button_new_with_label(string->str) | ||
56 | ); | ||
57 | |||
58 | gtk_button_set_relief(emoji_button, GTK_RELIEF_NONE); | ||
59 | |||
60 | g_signal_connect( | ||
61 | emoji_button, | ||
62 | "clicked", | ||
63 | G_CALLBACK(handle_emoji_button_click), | ||
64 | text_view | ||
65 | ); | ||
66 | |||
67 | gtk_flow_box_insert(flow_box, GTK_WIDGET(emoji_button), -1); | ||
68 | gtk_widget_show(GTK_WIDGET(emoji_button)); | ||
69 | |||
70 | g_string_free(string, TRUE); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | static void | ||
75 | handle_search_button_click(UNUSED GtkButton *button, | ||
76 | gpointer user_data) | ||
77 | { | ||
78 | UI_PICKER_Handle *handle = (UI_PICKER_Handle*) user_data; | ||
79 | |||
80 | const gchar* picked = gtk_stack_get_visible_child_name(handle->picker_stack); | ||
81 | |||
82 | HdySearchBar *search_bar = NULL; | ||
83 | |||
84 | if (0 == g_strcmp0(picked, "emoji")) | ||
85 | search_bar = handle->emoji_search_bar; | ||
86 | |||
87 | if (search_bar) | ||
88 | hdy_search_bar_set_search_mode( | ||
89 | search_bar, | ||
90 | !hdy_search_bar_get_search_mode(search_bar) | ||
91 | ); | ||
92 | } | ||
93 | |||
94 | UI_PICKER_Handle* | ||
95 | ui_picker_new(UNUSED MESSENGER_Application *app, | ||
96 | UI_CHAT_Handle *chat) | ||
97 | { | ||
98 | UI_PICKER_Handle *handle = g_malloc(sizeof(UI_PICKER_Handle)); | ||
99 | |||
100 | handle->builder = gtk_builder_new_from_file( | ||
101 | "resources/ui/picker.ui" | ||
102 | ); | ||
103 | |||
104 | handle->picker_box = GTK_WIDGET( | ||
105 | gtk_builder_get_object(handle->builder, "picker_box") | ||
106 | ); | ||
107 | |||
108 | handle->picker_stack = GTK_STACK( | ||
109 | gtk_builder_get_object(handle->builder, "picker_stack") | ||
110 | ); | ||
111 | |||
112 | handle->emoji_stack = GTK_STACK( | ||
113 | gtk_builder_get_object(handle->builder, "emoji_stack") | ||
114 | ); | ||
115 | |||
116 | handle->picker_switcher_bar = HDY_VIEW_SWITCHER_BAR( | ||
117 | gtk_builder_get_object(handle->builder, "picker_switcher_bar") | ||
118 | ); | ||
119 | |||
120 | handle->emoji_switcher_bar = HDY_VIEW_SWITCHER_BAR( | ||
121 | gtk_builder_get_object(handle->builder, "emoji_switcher_bar") | ||
122 | ); | ||
123 | |||
124 | handle->recent_flow_box = GTK_FLOW_BOX( | ||
125 | gtk_builder_get_object(handle->builder, "recent_flow_box") | ||
126 | ); | ||
127 | |||
128 | handle->people_flow_box = GTK_FLOW_BOX( | ||
129 | gtk_builder_get_object(handle->builder, "people_flow_box") | ||
130 | ); | ||
131 | |||
132 | _add_emoji_buttons( | ||
133 | handle->people_flow_box, | ||
134 | chat->send_text_view, | ||
135 | EMOJI_SMILEYS_CHARACTER_COUNT, | ||
136 | emoji_smileys_characters | ||
137 | ); | ||
138 | |||
139 | handle->nature_flow_box = GTK_FLOW_BOX( | ||
140 | gtk_builder_get_object(handle->builder, "nature_flow_box") | ||
141 | ); | ||
142 | |||
143 | _add_emoji_buttons( | ||
144 | handle->nature_flow_box, | ||
145 | chat->send_text_view, | ||
146 | EMOJI_ANIMALS_CHARACTER_COUNT, | ||
147 | emoji_animals_characters | ||
148 | ); | ||
149 | |||
150 | handle->food_flow_box = GTK_FLOW_BOX( | ||
151 | gtk_builder_get_object(handle->builder, "food_flow_box") | ||
152 | ); | ||
153 | |||
154 | _add_emoji_buttons( | ||
155 | handle->food_flow_box, | ||
156 | chat->send_text_view, | ||
157 | EMOJI_FOOD_CHARACTER_COUNT, | ||
158 | emoji_food_characters | ||
159 | ); | ||
160 | |||
161 | handle->activities_flow_box = GTK_FLOW_BOX( | ||
162 | gtk_builder_get_object(handle->builder, "activities_flow_box") | ||
163 | ); | ||
164 | |||
165 | _add_emoji_buttons( | ||
166 | handle->activities_flow_box, | ||
167 | chat->send_text_view, | ||
168 | EMOJI_ACTIVITIES_CHARACTER_COUNT, | ||
169 | emoji_activities_characters | ||
170 | ); | ||
171 | |||
172 | handle->travel_flow_box = GTK_FLOW_BOX( | ||
173 | gtk_builder_get_object(handle->builder, "travel_flow_box") | ||
174 | ); | ||
175 | |||
176 | _add_emoji_buttons( | ||
177 | handle->travel_flow_box, | ||
178 | chat->send_text_view, | ||
179 | EMOJI_TRAVEL_CHARACTER_COUNT, | ||
180 | emoji_travel_characters | ||
181 | ); | ||
182 | |||
183 | handle->objects_flow_box = GTK_FLOW_BOX( | ||
184 | gtk_builder_get_object(handle->builder, "objects_flow_box") | ||
185 | ); | ||
186 | |||
187 | _add_emoji_buttons( | ||
188 | handle->objects_flow_box, | ||
189 | chat->send_text_view, | ||
190 | EMOJI_OBJECTS_CHARACTER_COUNT, | ||
191 | emoji_objects_characters | ||
192 | ); | ||
193 | |||
194 | handle->symbols_flow_box = GTK_FLOW_BOX( | ||
195 | gtk_builder_get_object(handle->builder, "symbols_flow_box") | ||
196 | ); | ||
197 | |||
198 | _add_emoji_buttons( | ||
199 | handle->symbols_flow_box, | ||
200 | chat->send_text_view, | ||
201 | EMOJI_SYMBOLS_CHARACTER_COUNT, | ||
202 | emoji_symbols_characters | ||
203 | ); | ||
204 | |||
205 | handle->flags_flow_box = GTK_FLOW_BOX( | ||
206 | gtk_builder_get_object(handle->builder, "flags_flow_box") | ||
207 | ); | ||
208 | |||
209 | _add_emoji_buttons( | ||
210 | handle->flags_flow_box, | ||
211 | chat->send_text_view, | ||
212 | EMOJI_FLAGS_CHARACTER_COUNT, | ||
213 | emoji_flags_characters | ||
214 | ); | ||
215 | |||
216 | handle->emoji_search_bar = HDY_SEARCH_BAR( | ||
217 | gtk_builder_get_object(handle->builder, "emoji_search_bar") | ||
218 | ); | ||
219 | |||
220 | handle->emoji_search_entry = GTK_SEARCH_ENTRY( | ||
221 | gtk_builder_get_object(handle->builder, "emoji_search_entry") | ||
222 | ); | ||
223 | |||
224 | handle->search_button = GTK_BUTTON( | ||
225 | gtk_builder_get_object(handle->builder, "search_button") | ||
226 | ); | ||
227 | |||
228 | g_signal_connect( | ||
229 | handle->search_button, | ||
230 | "clicked", | ||
231 | G_CALLBACK(handle_search_button_click), | ||
232 | handle | ||
233 | ); | ||
234 | |||
235 | handle->settings_button = GTK_BUTTON( | ||
236 | gtk_builder_get_object(handle->builder, "settings_button") | ||
237 | ); | ||
238 | |||
239 | return handle; | ||
240 | } | ||
241 | |||
242 | void | ||
243 | ui_picker_delete(UI_PICKER_Handle *handle) | ||
244 | { | ||
245 | hdy_view_switcher_bar_set_stack(handle->picker_switcher_bar, NULL); | ||
246 | hdy_view_switcher_bar_set_stack(handle->emoji_switcher_bar, NULL); | ||
247 | |||
248 | g_object_unref(handle->builder); | ||
249 | |||
250 | g_free(handle); | ||
251 | } | ||