7fc6eb106602fb0027950ab3a0da54cd59606d1d
[claws.git] / src / gtk / spell_entry.c
1 /*
2  * @file libsexy/sexy-icon-entry.c Entry widget
3  *
4  * @Copyright (C) 2004-2006 Christian Hammond.
5  * Some of this code is from gtkspell, Copyright (C) 2002 Evan Martin.
6  * Adapted for Claws Mail (c) 2009 Pawel Pekala and the Claws Mail team
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  * 
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #ifdef USE_ENCHANT
28
29 #include <glib.h>
30 #include <glib/gi18n.h>
31
32 #include <gdk/gdk.h>
33 #include <gdk/gdkkeysyms.h>
34
35 #include <string.h>
36 #include <glib.h>
37 #include <gtk/gtk.h>
38
39 #include "spell_entry.h"
40 #include "prefs_common.h"
41 #include "codeconv.h"
42 #include "defs.h"
43
44 static void claws_spell_entry_init              (ClawsSpellEntry *entry);
45 static void claws_spell_entry_editable_init     (GtkEditableClass *iface);
46 static void claws_spell_entry_finalize          (GObject *object);
47 static void claws_spell_entry_destroy           (GtkObject *object);
48 static gint claws_spell_entry_expose            (GtkWidget *widget,
49                                                  GdkEventExpose *event);
50 static gint claws_spell_entry_button_press      (GtkWidget *widget,
51                                                  GdkEventButton *event);
52 static gboolean claws_spell_entry_popup_menu    (GtkWidget *widget,
53                                                  ClawsSpellEntry *entry);
54 static void claws_spell_entry_populate_popup    (ClawsSpellEntry *entry,
55                                                  GtkMenu *menu,
56                                                  gpointer data);
57 static void claws_spell_entry_changed           (GtkEditable *editable,
58                                                  gpointer data);
59
60 struct _ClawsSpellEntryPriv
61 {
62         PangoAttrList        *attr_list;
63         gint                  mark_character;
64         gchar               **words;
65         gint                 *word_starts;
66         gint                 *word_ends;
67 };
68
69 static GtkEntryClass *parent_class = NULL;
70
71
72 G_DEFINE_TYPE_EXTENDED(ClawsSpellEntry, claws_spell_entry, GTK_TYPE_ENTRY, 0, G_IMPLEMENT_INTERFACE(GTK_TYPE_EDITABLE, claws_spell_entry_editable_init)); 
73
74
75 static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass)
76 {
77         GObjectClass    *g_object_class;
78         GtkObjectClass  *gtk_object_class;
79         GtkWidgetClass  *widget_class;
80         
81         parent_class = g_type_class_peek_parent(klass);
82         
83         g_object_class = G_OBJECT_CLASS(klass);
84         g_object_class->finalize = claws_spell_entry_finalize;
85         
86         gtk_object_class = GTK_OBJECT_CLASS(klass);
87         gtk_object_class->destroy = claws_spell_entry_destroy;
88         
89         widget_class = GTK_WIDGET_CLASS(klass);
90         widget_class->expose_event = claws_spell_entry_expose;
91         widget_class->button_press_event = claws_spell_entry_button_press;
92         
93         g_type_class_add_private(g_object_class,
94                         sizeof(struct _ClawsSpellEntryPriv));
95 }
96
97 static void claws_spell_entry_init(ClawsSpellEntry *entry)
98 {
99         entry->gtkaspell = NULL;
100         
101         entry->priv = g_new0(ClawsSpellEntryPriv, 1);
102         entry->priv->attr_list = pango_attr_list_new();
103                                         
104         g_signal_connect(G_OBJECT(entry), "popup-menu",
105                         G_CALLBACK(claws_spell_entry_popup_menu), entry);
106         g_signal_connect(G_OBJECT(entry), "populate-popup",
107                         G_CALLBACK(claws_spell_entry_populate_popup), NULL);
108         g_signal_connect(G_OBJECT(entry), "changed",
109                         G_CALLBACK(claws_spell_entry_changed), NULL);
110 }
111
112 static void claws_spell_entry_editable_init (GtkEditableClass *iface) {}
113
114 static void claws_spell_entry_finalize(GObject *object)
115 {
116         ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(object);
117
118         if (entry->priv->attr_list)
119                 pango_attr_list_unref(entry->priv->attr_list);
120         if (entry->priv->words)
121                 g_strfreev(entry->priv->words);
122
123         g_free(entry->priv->word_starts);
124         g_free(entry->priv->word_ends);
125         g_free(entry->priv);
126         entry->priv = NULL;
127         
128         G_OBJECT_CLASS(parent_class)->finalize(object);
129 }
130
131 static void claws_spell_entry_destroy(GtkObject *object)
132 {
133         GTK_OBJECT_CLASS(parent_class)->destroy(object);
134 }
135
136 GtkWidget *claws_spell_entry_new(void)
137 {
138         return GTK_WIDGET( g_object_new(CLAWS_TYPE_SPELL_ENTRY, NULL) );
139 }
140
141 void claws_spell_entry_set_gtkaspell(ClawsSpellEntry *entry, GtkAspell *gtkaspell)
142 {
143         cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
144
145         entry->gtkaspell = gtkaspell;
146 }
147
148 static gint gtk_entry_find_position (GtkEntry *entry, gint x)
149 {
150         PangoLayout *layout;
151         PangoLayoutLine *line;
152         const gchar *text;
153         gint cursor_index;
154         gint index;
155         gint pos;
156         gboolean trailing;
157
158         x = x + entry->scroll_offset;
159
160         layout = gtk_entry_get_layout(entry);
161         text = pango_layout_get_text(layout);
162         cursor_index = g_utf8_offset_to_pointer(text, entry->current_pos) - text;
163
164         line = pango_layout_get_lines(layout)->data;
165         pango_layout_line_x_to_index(line, x * PANGO_SCALE, &index, &trailing);
166
167         if (index >= cursor_index && entry->preedit_length) {
168                 if (index >= cursor_index + entry->preedit_length) {
169                         index -= entry->preedit_length;
170                 } else {
171                         index = cursor_index;
172                         trailing = FALSE;
173                 }
174         }
175
176         pos = g_utf8_pointer_to_offset (text, text + index);
177         pos += trailing;
178
179         return pos;
180 }
181
182 static void get_word_extents_from_position(ClawsSpellEntry *entry, gint *start,
183                                            gint *end, guint position)
184 {
185         const gchar *text;
186         gint i, bytes_pos;
187
188         *start = -1;
189         *end = -1;
190
191         if (entry->priv->words == NULL)
192                 return;
193
194         text = gtk_entry_get_text(GTK_ENTRY(entry));
195         bytes_pos = (gint) (g_utf8_offset_to_pointer(text, position) - text);
196
197         for (i = 0; entry->priv->words[i]; i++) {
198                 if (bytes_pos >= entry->priv->word_starts[i] &&
199                     bytes_pos <= entry->priv->word_ends[i]) {
200                         *start = entry->priv->word_starts[i];
201                         *end   = entry->priv->word_ends[i];
202                         return;
203                 }
204         }
205 }
206
207 static gchar *get_word(ClawsSpellEntry *entry, const int start, const int end)
208 {
209         const gchar *text;
210         gchar *word;
211         
212         if (start >= end)
213                 return NULL;
214
215         text = gtk_entry_get_text(GTK_ENTRY(entry));
216         word = g_new0(gchar, end - start + 2);
217         g_strlcpy(word, text + start, end - start + 1);
218
219         return word;
220 }
221
222 static void replace_word(ClawsSpellEntry *entry, const gchar *newword)
223 {
224         gint cursor, start_pos, end_pos;
225         const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
226
227         start_pos = entry->gtkaspell->start_pos;
228         end_pos = entry->gtkaspell->end_pos;
229                 
230         cursor = gtk_editable_get_position(GTK_EDITABLE(entry));
231         /* is the cursor at the end? If so, restore it there */
232         if (g_utf8_strlen(text, -1) == cursor)
233                 cursor = -1;
234         else if(cursor < entry->priv->mark_character ||
235                 cursor > entry->priv->mark_character)
236                         cursor = entry->priv->mark_character;
237
238         gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, end_pos);
239         gtk_editable_insert_text(GTK_EDITABLE(entry), newword, strlen(newword),
240                                                          &start_pos);
241         gtk_editable_set_position(GTK_EDITABLE(entry), cursor);
242 }
243
244
245 static gboolean word_misspelled(ClawsSpellEntry *entry, int start, int end)
246 {
247         gchar *word;
248         gboolean ret;
249
250         word = get_word(entry, start, end);
251         if (word == NULL)
252                 return FALSE;
253                 
254         ret = gtkaspell_misspelled_test(entry->gtkaspell, word);
255
256         g_free(word);
257         return ret;
258 }
259
260 static void entry_strsplit_utf8(GtkEntry *entry, gchar ***set, gint **starts, gint **ends)
261 {
262         PangoLayout   *layout;
263         PangoLogAttr  *log_attrs;
264         const gchar   *text;
265         gint           n_attrs, n_strings, i, j;
266
267         layout = gtk_entry_get_layout(GTK_ENTRY(entry));
268         text = gtk_entry_get_text(GTK_ENTRY(entry));
269         pango_layout_get_log_attrs(layout, &log_attrs, &n_attrs);
270
271         /* Find how many words we have */
272         n_strings = 0;
273         for (i = 0; i < n_attrs; i++)
274                 if (log_attrs[i].is_word_start)
275                         n_strings++;
276
277         *set    = g_new0(gchar *, n_strings + 1);
278         *starts = g_new0(gint, n_strings);
279         *ends   = g_new0(gint, n_strings);
280
281         /* Copy out strings */
282         for (i = 0, j = 0; i < n_attrs; i++) {
283                 if (log_attrs[i].is_word_start) {
284                         gint cend, bytes;
285                         gchar *start;
286
287                         /* Find the end of this string */
288                         cend = i;
289                         while (!(log_attrs[cend].is_word_end))
290                                 cend++;
291
292                         /* Copy sub-string */
293                         start = g_utf8_offset_to_pointer(text, i);
294                         bytes = (gint) (g_utf8_offset_to_pointer(text, cend) - start);
295                         (*set)[j]    = g_new0(gchar, bytes + 1);
296                         (*starts)[j] = (gint) (start - text);
297                         (*ends)[j]   = (gint) (start - text + bytes);
298                         g_utf8_strncpy((*set)[j], start, cend - i);
299
300                         /* Move on to the next word */
301                         j++;
302                 }
303         }
304
305         g_free (log_attrs);
306 }
307
308 static void insert_misspelled_marker(ClawsSpellEntry *entry, guint start, guint end)
309 {
310         guint16 red   = (guint16) (((gdouble)((prefs_common.misspelled_col & 
311                                         0xff0000) >> 16) / 255.0) * 65535.0);
312         guint16 green = (guint16) (((gdouble)((prefs_common.misspelled_col & 
313                                         0x00ff00) >> 8) / 255.0) * 65535.0);
314         guint16 blue  = (guint16) (((gdouble) (prefs_common.misspelled_col & 
315                                         0x0000ff) / 255.0) * 65535.0);
316         PangoAttribute *fcolor, *ucolor, *unline;
317         
318         if(prefs_common.misspelled_col != 0) {
319                 fcolor = pango_attr_foreground_new(red, green, blue);
320                 fcolor->start_index = start;
321                 fcolor->end_index = end;
322                 
323                 pango_attr_list_insert(entry->priv->attr_list, fcolor);
324         } else {
325                 ucolor = pango_attr_underline_color_new (65535, 0, 0);
326                 unline = pango_attr_underline_new (PANGO_UNDERLINE_ERROR);
327
328                 ucolor->start_index = start;
329                 unline->start_index = start;
330
331                 ucolor->end_index = end;
332                 unline->end_index = end;
333
334                 pango_attr_list_insert (entry->priv->attr_list, ucolor);
335                 pango_attr_list_insert (entry->priv->attr_list, unline);
336         }
337 }
338
339 static gboolean check_word(ClawsSpellEntry *entry, int start, int end)
340 {
341         GtkAspell *gtkaspell = entry->gtkaspell;
342         PangoAttrIterator *it;
343         gint s, e;
344         gboolean misspelled;
345         gchar *text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
346         gchar *word = NULL;
347
348         /* Check to see if we've got any attributes at this position.
349          * If so, free them, since we'll readd it if the word is misspelled */
350         it = pango_attr_list_get_iterator(entry->priv->attr_list);
351         if (it == NULL)
352                 return FALSE;
353         do {
354                 pango_attr_iterator_range(it, &s, &e);
355                 if (s == start) {
356                         GSList *attrs = pango_attr_iterator_get_attrs(it);
357                         g_slist_foreach(attrs, (GFunc) pango_attribute_destroy, NULL);
358                         g_slist_free(attrs);
359                 }
360         } while (pango_attr_iterator_next(it));
361         pango_attr_iterator_destroy(it);
362
363         if ((misspelled = word_misspelled(entry, start, end))) {
364                 insert_misspelled_marker(entry, start, end);
365         
366                 word = get_word(entry, start, end);
367                 strncpy(gtkaspell->theword, (gchar *)word, GTKASPELLWORDSIZE - 1);
368                 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
369                 gtkaspell->start_pos  = g_utf8_pointer_to_offset(text, (text+start));
370                 gtkaspell->end_pos    = g_utf8_pointer_to_offset(text, (text+end));
371                 gtkaspell_free_suggestions_list(gtkaspell);
372                 g_free(word);
373         }
374         
375         g_free(text);
376         
377         return misspelled;
378 }
379
380 void claws_spell_entry_recheck_all(ClawsSpellEntry *entry)
381 {
382         GdkRectangle rect;
383         PangoLayout *layout;
384         int length, i;
385
386         cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
387         cm_return_if_fail(entry->gtkaspell != NULL);
388
389         if (entry->priv->words == NULL)
390                 return;
391
392         /* Remove all existing pango attributes.  These will get readded as we check */
393         pango_attr_list_unref(entry->priv->attr_list);
394         entry->priv->attr_list = pango_attr_list_new();
395
396         /* Loop through words */
397         for (i = 0; entry->priv->words[i]; i++) {
398                 length = strlen(entry->priv->words[i]);
399                 if (length == 0)
400                         continue;
401                 check_word(entry, entry->priv->word_starts[i], entry->priv->word_ends[i]);
402         }
403
404         layout = gtk_entry_get_layout(GTK_ENTRY(entry));
405         pango_layout_set_attributes(layout, entry->priv->attr_list);
406
407         if (GTK_WIDGET_REALIZED(GTK_WIDGET(entry))) {
408                 rect.x = 0; rect.y = 0;
409                 rect.width  = GTK_WIDGET(entry)->allocation.width;
410                 rect.height = GTK_WIDGET(entry)->allocation.height;
411                 gdk_window_invalidate_rect(GTK_WIDGET(entry)->window, &rect, TRUE);
412         }
413 }
414
415 static gint claws_spell_entry_expose(GtkWidget *widget, GdkEventExpose *event)
416 {
417         ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
418         GtkEntry *gtk_entry = GTK_ENTRY(widget);
419         PangoLayout *layout;
420
421         if (entry->gtkaspell != NULL) {
422                 layout = gtk_entry_get_layout(gtk_entry);
423                 pango_layout_set_attributes(layout, entry->priv->attr_list);
424         }
425
426         return GTK_WIDGET_CLASS(parent_class)->expose_event (widget, event);
427 }
428
429 static gint claws_spell_entry_button_press(GtkWidget *widget, GdkEventButton *event)
430 {
431         ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
432         GtkEntry *gtk_entry = GTK_ENTRY(widget);
433         gint pos;
434
435         pos = gtk_entry_find_position(gtk_entry, event->x);
436         entry->priv->mark_character = pos;
437
438         return GTK_WIDGET_CLASS(parent_class)->button_press_event (widget, event);
439 }
440
441 static gboolean claws_spell_entry_popup_menu(GtkWidget *widget, ClawsSpellEntry *entry)
442 {
443         entry->priv->mark_character = gtk_editable_get_position (GTK_EDITABLE (entry));
444         return FALSE;
445 }
446
447 static void set_position(gpointer data, gint pos)
448 {
449         gtk_editable_set_position(GTK_EDITABLE(data), pos);
450 }
451
452 static gboolean find_misspelled_cb(gpointer data, gboolean forward)
453 {
454         ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
455         GtkAspell *gtkaspell = entry->gtkaspell;
456         gboolean misspelled = FALSE;
457         gint cursor, minpos, maxpos, i, words_len = 0;
458         gint start, end;
459         gchar *text;
460         
461         if (entry->priv->words == NULL)
462                 return FALSE;
463
464         gtkaspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
465         text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
466         cursor = g_utf8_offset_to_pointer(text, gtkaspell->orig_pos) - text;
467
468         if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &start, &end)) {
469                 minpos = g_utf8_offset_to_pointer(text, start) - text;
470                 maxpos = g_utf8_offset_to_pointer(text, end) - text;
471         } else {
472                 minpos = forward ? cursor : 0;
473                 maxpos = forward ? strlen(text)-1 : cursor;
474         }
475         g_free(text);
476
477         while(entry->priv->words[words_len])
478                 words_len++;
479
480         if (forward) {
481                 for(i=0; i < words_len; i++)
482                         if (entry->priv->word_ends[i] > minpos &&
483                             (misspelled = check_word(entry,
484                                         entry->priv->word_starts[i],
485                                         entry->priv->word_ends[i])))
486                                 break;
487         } else {
488                 for(i=words_len-1; i >= 0; i--)
489                         if (entry->priv->word_starts[i] < maxpos &&
490                             (misspelled = check_word(entry,
491                                         entry->priv->word_starts[i],
492                                         entry->priv->word_ends[i])))
493                                 break;
494         }
495         
496         return misspelled;
497 }
498
499 static gboolean check_word_cb(gpointer data)
500 {
501         ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
502         gint start, end;
503         
504         get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character);
505         return check_word(entry, start, end);
506 }
507
508 static void replace_word_cb(gpointer data, const gchar *newword)
509 {
510         replace_word((ClawsSpellEntry *) data, newword);
511 }
512
513 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y, 
514                          gboolean *push_in, gpointer data)
515 {
516         ClawsSpellEntry *entry = (ClawsSpellEntry *) data;
517         GtkAspell *gtkaspell = entry->gtkaspell;
518         gint pango_offset, win_x, win_y, scr_x, scr_y, text_index, entry_x;
519         gchar *text;
520         GtkRequisition subject_rq;
521         PangoLayout *layout = gtk_entry_get_layout(GTK_ENTRY(entry));
522         PangoLayoutLine *line = pango_layout_get_lines(layout)->data;
523
524         gtk_widget_get_child_requisition(GTK_WIDGET(entry), &subject_rq);
525         
526         /* screen -> compose window coords */
527         gdk_window_get_origin(GTK_WIDGET(gtkaspell->parent_window)->window,
528                                 &scr_x, &scr_y);
529
530         /* compose window -> subject entry coords */
531         gtk_widget_translate_coordinates(GTK_WIDGET(entry),
532                         gtkaspell->parent_window, 0, 0, &win_x, &win_y);
533
534         text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
535         text_index = g_utf8_offset_to_pointer(text, gtkaspell->end_pos) - text;
536         g_free(text);
537
538         pango_offset = gtk_entry_text_index_to_layout_index(GTK_ENTRY(entry),
539                                         text_index);
540         pango_layout_line_index_to_x(line, pango_offset, TRUE, &entry_x);
541
542         *x = scr_x + win_x + PANGO_PIXELS(entry_x) + 8;
543         *y = scr_y + win_y + subject_rq.height;
544 }
545
546 void claws_spell_entry_context_set(ClawsSpellEntry *entry)
547 {
548         cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
549         cm_return_if_fail(entry->gtkaspell != NULL);
550
551         entry->gtkaspell->ctx.set_position      = set_position;
552         entry->gtkaspell->ctx.set_menu_pos      = set_menu_pos;
553         entry->gtkaspell->ctx.find_misspelled   = find_misspelled_cb;
554         entry->gtkaspell->ctx.check_word        = check_word_cb;
555         entry->gtkaspell->ctx.replace_word      = replace_word_cb;
556         entry->gtkaspell->ctx.data              = (gpointer) entry;
557 }
558
559 static void claws_spell_entry_populate_popup(ClawsSpellEntry *entry, GtkMenu *menu,
560                                                 gpointer data)
561 {
562         GtkAspell *gtkaspell = entry->gtkaspell;
563         gint start, end;
564         gchar *word, *text;
565         
566         if (gtkaspell == NULL)
567                 return;
568         
569         get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character);
570
571         if ((word = get_word(entry, start, end)) != NULL) {
572                 strncpy(gtkaspell->theword, word, GTKASPELLWORDSIZE - 1);
573                 g_free(word);
574         }
575
576         gtkaspell->misspelled = word_misspelled(entry, start, end);
577
578         text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
579         gtkaspell->start_pos  = g_utf8_pointer_to_offset(text, (text+start));
580         gtkaspell->end_pos    = g_utf8_pointer_to_offset(text, (text+end));
581         g_free(text);
582
583         claws_spell_entry_context_set(entry);
584         gtkaspell_make_context_menu(menu, gtkaspell);
585 }
586
587 static void claws_spell_entry_changed(GtkEditable *editable, gpointer data)
588 {
589         ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(editable);
590
591         if (entry->gtkaspell == NULL)
592                 return;
593
594         if (entry->priv->words) {
595                 g_strfreev(entry->priv->words);
596                 g_free(entry->priv->word_starts);
597                 g_free(entry->priv->word_ends);
598         }
599         entry_strsplit_utf8(GTK_ENTRY(entry), &entry->priv->words, 
600                         &entry->priv->word_starts, &entry->priv->word_ends);
601         if(entry->gtkaspell->check_while_typing == TRUE)
602                 claws_spell_entry_recheck_all(entry);
603 }
604
605 static void continue_check(gpointer *data)
606 {
607         ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
608         GtkAspell *gtkaspell = entry->gtkaspell;
609         gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
610         
611         if (gtkaspell->misspelled && pos < gtkaspell->end_check_pos)
612                 gtkaspell->misspelled = gtkaspell_check_next_prev(gtkaspell, TRUE);
613         else
614                 gtkaspell->continue_check = NULL;
615 }
616
617 void claws_spell_entry_check_all(ClawsSpellEntry *entry)
618 {
619         gint start, end;
620         gchar *text;
621         
622         cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
623         cm_return_if_fail(entry->gtkaspell != NULL);
624         
625         if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &start, &end)) {
626                 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);      
627                 
628                 start = 0;
629                 end = g_utf8_strlen(text, -1) - 1;
630                 
631                 g_free(text);
632         }
633
634         gtk_editable_set_position(GTK_EDITABLE(entry), start);
635         entry->gtkaspell->continue_check = continue_check;
636         entry->gtkaspell->end_check_pos  = end;
637
638         claws_spell_entry_context_set(entry);
639         entry->gtkaspell->misspelled = 
640                         gtkaspell_check_next_prev(entry->gtkaspell, TRUE);
641 }
642
643 void claws_spell_entry_check_backwards(ClawsSpellEntry *entry)
644 {
645         cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
646         cm_return_if_fail(entry->gtkaspell != NULL);
647         
648         entry->gtkaspell->continue_check = NULL;
649         claws_spell_entry_context_set(entry);
650         gtkaspell_check_next_prev(entry->gtkaspell, FALSE);
651 }
652
653 void claws_spell_entry_check_forwards_go(ClawsSpellEntry *entry)
654 {
655         cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
656         cm_return_if_fail(entry->gtkaspell != NULL);
657
658         entry->gtkaspell->continue_check = NULL;
659         claws_spell_entry_context_set(entry);
660         gtkaspell_check_next_prev(entry->gtkaspell, TRUE);
661 }
662
663 #endif  /* USE_ENCHANT */