summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2021-12-28 12:46:15 +0100
committerChristian Grothoff <christian@grothoff.org>2021-12-28 12:46:15 +0100
commit987f3f2f2d4f7fd61c115323fb17e6889f1e1120 (patch)
tree35b1b41725dbc9903bec896e3030f1349992907a
parentc7ece85320454c809b84798b6b5847f22fea5dcc (diff)
downloadanastasis-gtk-987f3f2f2d4f7fd61c115323fb17e6889f1e1120.tar.gz
anastasis-gtk-987f3f2f2d4f7fd61c115323fb17e6889f1e1120.tar.bz2
anastasis-gtk-987f3f2f2d4f7fd61c115323fb17e6889f1e1120.zip
fix #7086
-rw-r--r--contrib/anastasis_gtk_main_window.glade4
-rw-r--r--src/anastasis/Makefile.am1
-rw-r--r--src/anastasis/anastasis-gtk_autocomplete.c143
3 files changed, 148 insertions, 0 deletions
diff --git a/contrib/anastasis_gtk_main_window.glade b/contrib/anastasis_gtk_main_window.glade
index 1b1ab4e..30bbd49 100644
--- a/contrib/anastasis_gtk_main_window.glade
+++ b/contrib/anastasis_gtk_main_window.glade
@@ -1230,6 +1230,8 @@ Author: Christian Grothoff, Dennis Neufeld
<property name="placeholder-text" translatable="yes">756.1222.3425.89</property>
<property name="input-purpose">number</property>
<signal name="changed" handler="AG_identity_changed" swapped="no"/>
+ <signal name="delete-text" handler="anastasis_gtk_autocomplete_delete_text" swapped="no"/>
+ <signal name="insert-text" handler="anastasis_gtk_autocomplete_insert_text" swapped="no"/>
<signal name="style-updated" handler="gtk_widget_queue_draw" swapped="no"/>
</object>
<packing>
@@ -1565,6 +1567,8 @@ Author: Christian Grothoff, Dennis Neufeld
<property name="max-width-chars">11</property>
<property name="placeholder-text" translatable="yes">000-00-0000</property>
<signal name="changed" handler="AG_identity_changed" swapped="no"/>
+ <signal name="delete-text" handler="anastasis_gtk_autocomplete_delete_text" swapped="no"/>
+ <signal name="insert-text" handler="anastasis_gtk_autocomplete_insert_text" swapped="no"/>
<signal name="style-updated" handler="gtk_widget_queue_draw" swapped="no"/>
</object>
<packing>
diff --git a/src/anastasis/Makefile.am b/src/anastasis/Makefile.am
index fe908df..1e80236 100644
--- a/src/anastasis/Makefile.am
+++ b/src/anastasis/Makefile.am
@@ -14,6 +14,7 @@ anastasis_gtk_SOURCES = \
anastasis-gtk.c anastasis-gtk.h \
anastasis-gtk_action.c anastasis-gtk_action.h \
anastasis-gtk_attributes.c anastasis-gtk_attributes.h \
+ anastasis-gtk_autocomplete.c \
anastasis-gtk_dispatch.c anastasis-gtk_dispatch.h \
anastasis-gtk_io.c \
anastasis-gtk_handle-auth-delete-button-clicked.c \
diff --git a/src/anastasis/anastasis-gtk_autocomplete.c b/src/anastasis/anastasis-gtk_autocomplete.c
new file mode 100644
index 0000000..1e3da34
--- /dev/null
+++ b/src/anastasis/anastasis-gtk_autocomplete.c
@@ -0,0 +1,143 @@
+/*
+ This file is part of anastasis-gtk.
+ Copyright (C) 2021 Anastasis SARL
+
+ Anastasis is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published
+ by the Free Software Foundation; either version 3, or (at your
+ option) any later version.
+
+ Anastasis is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Anastasis; see the file COPYING. If not, write to the
+ Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA.
+*/
+/**
+ * @file src/anastasis/anastasis-gtk_autocomplete.c
+ * @brief Autocomplete logic for GtkEntry widgets
+ * @author Christian Grothoff
+ */
+#include <gnunet/platform.h>
+#include <gnunet/gnunet_util_lib.h>
+#include "anastasis-gtk_attributes.h"
+#include "anastasis-gtk_dispatch.h"
+#include "anastasis-gtk_helper.h"
+#include <jansson.h>
+
+
+/**
+ * Lookup autocompletion string for @a editable widget.
+ *
+ * @param editable widget to lookup
+ * @return NULL if not found
+ */
+static const char *
+lookup_autocomplete (GtkEditable *editable)
+{
+ json_t *a;
+ size_t index;
+ json_t *ra = json_object_get (AG_redux_state,
+ "required_attributes");
+ if (NULL == ra)
+ {
+ GNUNET_break (0);
+ return NULL;
+ }
+ json_array_foreach (ra, index, a)
+ {
+ const char *widget_name = json_string_value (json_object_get (a,
+ "widget"));
+ if (editable ==
+ GTK_EDITABLE (GCG_get_main_window_object (ai[i].widget_name)))
+ return json_string_value (json_object_get (a,
+ "autocomplete"));
+ }
+ GNUNET_break (0);
+ return NULL;
+}
+
+
+/**
+ * Function called from an auto-completed entry widget up on upon text
+ * deletion.
+ *
+ * @param editable the widget
+ * @param start_pos starting position
+ * @param end_pos end position
+ * @param user_data unused
+ */
+void
+anastasis_gtk_autocomplete_delete_text (GtkEditable *editable,
+ int start_pos,
+ int end_pos,
+ void *user_data)
+{
+ const char *ac;
+ (void) user_data;
+
+ ac = lookup_autocomplete (editable);
+ if (NULL == ac)
+ return;
+ if (0 == start_pos)
+ return;
+#if CONFUSING_UX_DESIRED
+ if (ac[start_pos] != '?')
+ {
+ g_signal_stop_emission_by_name (editable,
+ "delete-text");
+ gtk_editable_delete_text (editable,
+ start_pos - 1,
+ end_pos);
+ }
+#endif
+}
+
+
+/**
+ * Function called from an auto-completed widget upon text insertion.
+ *
+ * @param editable the widget
+ * @param new_text inserted text
+ * @param new_text_length number of bytes in @a new_text
+ * @param position insertion position
+ * @param user_data unused
+ */
+void
+anastasis_gtk_autocomplete_insert_text (GtkEditable *editable,
+ const char *new_text,
+ int new_text_length,
+ gpointer position,
+ void *user_data)
+{
+ const char *ac;
+ (void) user_data;
+ gint pos;
+ char *replacement;
+
+ ac = lookup_autocomplete (editable);
+ if (NULL == ac)
+ return;
+ pos = gtk_editable_get_position (editable);
+ if (strlen (ac) <= pos + new_text_length)
+ return;
+ if (ac[pos+new_text_length] != '?')
+ {
+ g_signal_stop_emission_by_name (editable,
+ "insert-text");
+ GNUNET_asprintf (&replacement,
+ "%.*s%c",
+ new_text_length,
+ new_text,
+ ac[pos+new_text_length]);
+ gtk_editable_insert_text (editable,
+ replacement,
+ -1,
+ position);
+ GNUNET_free (replacement);
+ }
+}