[gspell/wip/entry: 2/3] Entry utils: get list of words



commit 172104d86398093e83a095a7c264777ac3c32271
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Oct 28 12:30:46 2016 +0200

    Entry utils: get list of words

 docs/reference/Makefile.am  |    1 +
 gspell/Makefile.am          |    2 +
 gspell/gspell-entry-utils.c |  117 +++++++++++++++++++++++++++++++++++++++++++
 gspell/gspell-entry-utils.h |   49 ++++++++++++++++++
 4 files changed, 169 insertions(+), 0 deletions(-)
---
diff --git a/docs/reference/Makefile.am b/docs/reference/Makefile.am
index 33b30b1..088c496 100644
--- a/docs/reference/Makefile.am
+++ b/docs/reference/Makefile.am
@@ -31,6 +31,7 @@ EXTRA_HFILES =                                                \
 IGNORE_HFILES =                                        \
        gspell.h                                \
        gspellregion.h                          \
+       gspell-entry-utils.h                    \
        gspell-init.h                           \
        gspell-inline-checker-text-buffer.h     \
        gspell-osx.h                            \
diff --git a/gspell/Makefile.am b/gspell/Makefile.am
index 3cfd0da..253698f 100644
--- a/gspell/Makefile.am
+++ b/gspell/Makefile.am
@@ -45,6 +45,7 @@ gspell_public_c_files =                               \
 gspell_private_headers =                       \
        gconstructor.h                          \
        gspellregion.h                          \
+       gspell-entry-utils.h                    \
        gspell-init.h                           \
        gspell-inline-checker-text-buffer.h     \
        gspell-text-iter.h                      \
@@ -52,6 +53,7 @@ gspell_private_headers =                      \
 
 gspell_private_c_files =                       \
        gspellregion.c                          \
+       gspell-entry-utils.c                    \
        gspell-init.c                           \
        gspell-inline-checker-text-buffer.c     \
        gspell-text-iter.c                      \
diff --git a/gspell/gspell-entry-utils.c b/gspell/gspell-entry-utils.c
new file mode 100644
index 0000000..05488a3
--- /dev/null
+++ b/gspell/gspell-entry-utils.c
@@ -0,0 +1,117 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "gspell-entry-utils.h"
+#include <string.h>
+
+static GspellEntryWord *
+entry_word_new (void)
+{
+       return g_new0 (GspellEntryWord, 1);
+}
+
+void
+_gspell_entry_word_free (gpointer data)
+{
+       GspellEntryWord *word = data;
+
+       if (word != NULL)
+       {
+               g_free (word->word_str);
+               g_free (word);
+       }
+}
+
+/* List elements: GspellEntryWord*.
+ * Free with g_slist_free_full (words, _gspell_entry_word_free);
+ */
+GSList *
+_gspell_entry_utils_get_words (GtkEntry *entry)
+{
+       PangoLayout *layout;
+       const gchar *text;
+       const gchar *cur_text_pos;
+       const gchar *word_start;
+       const PangoLogAttr *attrs;
+       gint n_attrs = 0;
+       gint attr_num;
+       GSList *list = NULL;
+
+       g_return_val_if_fail (GTK_IS_ENTRY (entry), NULL);
+
+       layout = gtk_entry_get_layout (entry);
+       text = gtk_entry_get_text (entry);
+       attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs);
+
+       attr_num = 0;
+       cur_text_pos = text;
+       word_start = NULL;
+
+       while (attr_num < n_attrs)
+       {
+               if (word_start != NULL &&
+                   attrs[attr_num].is_word_end)
+               {
+                       const gchar *word_end;
+                       GspellEntryWord *word;
+
+                       if (cur_text_pos != NULL)
+                       {
+                               word_end = cur_text_pos;
+                       }
+                       else
+                       {
+                               word_end = word_start + strlen (word_start);
+                       }
+
+                       word = entry_word_new ();
+                       word->byte_start = word_start - text;
+                       word->byte_end = word_end - text;
+                       word->word_str = g_strndup (word_start, word_end - word_start);
+
+                       list = g_slist_prepend (list, word);
+
+                       /* Find next word start. */
+                       word_start = NULL;
+               }
+
+               if (word_start == NULL &&
+                   attrs[attr_num].is_word_start)
+               {
+                       word_start = cur_text_pos;
+               }
+
+               if (cur_text_pos == NULL &&
+                   attr_num != n_attrs - 1)
+               {
+                       g_warning ("%s(): problem in loop iteration, attr_num=%d but should be %d.",
+                                  G_STRFUNC,
+                                  attr_num,
+                                  n_attrs - 1);
+                       break;
+               }
+
+               attr_num++;
+               cur_text_pos = g_utf8_find_next_char (cur_text_pos, NULL);
+       }
+
+       return g_slist_reverse (list);
+}
+
+/* ex:set ts=8 noet: */
diff --git a/gspell/gspell-entry-utils.h b/gspell/gspell-entry-utils.h
new file mode 100644
index 0000000..cdac6d6
--- /dev/null
+++ b/gspell/gspell-entry-utils.h
@@ -0,0 +1,49 @@
+/*
+ * This file is part of gspell, a spell-checking library.
+ *
+ * Copyright 2016 - Sébastien Wilmet
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GSPELL_ENTRY_UTILS_H
+#define GSPELL_ENTRY_UTILS_H
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+typedef struct _GspellEntryWord GspellEntryWord;
+struct _GspellEntryWord
+{
+       gchar *word_str;
+
+       /* Position in the GtkEntryBuffer. The character at the byte_end index
+        * is not included, like in #PangoAttribute.
+        */
+       gint byte_start;
+       gint byte_end;
+};
+
+G_GNUC_INTERNAL
+void           _gspell_entry_word_free         (gpointer data);
+
+G_GNUC_INTERNAL
+GSList *       _gspell_entry_utils_get_words   (GtkEntry *entry);
+
+G_END_DECLS
+
+#endif /* GSPELL_ENTRY_UTILS_H */
+
+/* ex:set ts=8 noet: */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]