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



commit 6dd27870a30e8583e15bf231d7527e92bd6be3c1
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Fri Oct 28 11:20:33 2016 +0200

    Entry: get list of words

 gspell/gspell-entry.c |   90 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 90 insertions(+), 0 deletions(-)
---
diff --git a/gspell/gspell-entry.c b/gspell/gspell-entry.c
index 53d4340..b0a41b9 100644
--- a/gspell/gspell-entry.c
+++ b/gspell/gspell-entry.c
@@ -43,6 +43,16 @@ struct _GspellEntry
        guint inline_spell_checking : 1;
 };
 
+typedef struct _EntryWord EntryWord;
+struct _EntryWord
+{
+       gchar *word_str;
+
+       /* Position in the GtkEntryBuffer. */
+       gint start;
+       gint end;
+};
+
 enum
 {
        PROP_0,
@@ -54,6 +64,86 @@ enum
 
 G_DEFINE_TYPE (GspellEntry, gspell_entry, G_TYPE_OBJECT)
 
+static EntryWord *
+entry_word_new (void)
+{
+       return g_new0 (EntryWord, 1);
+}
+
+static void
+entry_word_free (gpointer data)
+{
+       EntryWord *word = data;
+
+       if (word != NULL)
+       {
+               g_free (word->word_str);
+               g_free (word);
+       }
+}
+
+/* List elements: EntryWord*.
+ * Free with g_slist_free_full (words, entry_word_free);
+ */
+static GSList *
+get_words (GspellEntry *gspell_entry)
+{
+       PangoLayout *layout;
+       const gchar *text;
+       const PangoLogAttr *log_attrs;
+       gint n_attrs = 0;
+       gint attr_num;
+       GSList *list;
+
+       layout = gtk_entry_get_layout (gspell_entry->entry);
+       text = gtk_entry_get_text (gspell_entry->entry);
+       log_attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs);
+
+       for (attr_num = 0; attr_num < n_attrs; attr_num++)
+       {
+               const PangoLogAttr cur_attr = log_attrs[attr_num];
+               gint word_start;
+               gint word_end;
+               gint bytes;
+               gchar *str;
+               EntryWord *word;
+
+               if (!cur_attr.is_word_start)
+               {
+                       continue;
+               }
+
+               word_start = attr_num;
+
+               /* Find the end of this word. */
+               for (word_end = word_start; word_end < n_attrs; word_end++)
+               {
+                       cur_attr = log_attrs[word_end];
+
+                       if (cur_attr.is_word_end)
+                       {
+                               break;
+                       }
+               }
+
+               /* Safety net in case is_word_end was not found. */
+               word_end = MIN (word_end, n_attrs - 1);
+
+               word = entry_word_new ();
+
+               start = g_utf8_offset_to_pointer(text, i);
+               bytes = (gint) (g_utf8_offset_to_pointer(text, cend) - start);
+               (*set)[j] = g_new0(gchar, bytes + 1);
+               (*starts)[j] = (gint) (start - text);
+               (*ends)[j] = (gint) (start - text + bytes);
+               g_utf8_strncpy((*set)[j], start, cend - i);
+
+               list = g_slist_prepend (list, word);
+       }
+
+       return g_slist_reverse (list);
+}
+
 static void
 apply_underline (GspellEntry *gspell_entry,
                 guint        start,


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