2 * @file libsexy/sexy-icon-entry.c Entry widget
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-2012 Pawel Pekala and the Claws Mail team
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.
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.
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/>.
25 #include "claws-features.h"
31 #include <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
40 #include "spell_entry.h"
41 #include "prefs_common.h"
46 static void claws_spell_entry_init (ClawsSpellEntry *entry);
47 static void claws_spell_entry_finalize (GObject *object);
48 #if !GTK_CHECK_VERSION(3, 0, 0)
49 static void claws_spell_entry_destroy (GtkObject *object);
50 static gint claws_spell_entry_expose (GtkWidget *widget,
51 GdkEventExpose *event);
53 static void claws_spell_entry_destroy (GtkWidget *object);
54 static gint claws_spell_entry_expose (GtkWidget *widget,
57 static gint claws_spell_entry_button_press (GtkWidget *widget,
58 GdkEventButton *event);
59 static gboolean claws_spell_entry_popup_menu (GtkWidget *widget,
60 ClawsSpellEntry *entry);
61 static void claws_spell_entry_populate_popup (ClawsSpellEntry *entry,
64 static void claws_spell_entry_changed (GtkEditable *editable,
66 static void claws_spell_entry_preedit_changed (GtkEntry *entry,
70 struct _ClawsSpellEntryPriv
72 PangoAttrList *attr_list;
80 static GtkEntryClass *parent_class = NULL;
83 G_DEFINE_TYPE(ClawsSpellEntry, claws_spell_entry, GTK_TYPE_ENTRY)
86 static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass)
88 GObjectClass *g_object_class;
89 #if !GTK_CHECK_VERSION(3, 0, 0)
90 GtkObjectClass *gtk_object_class;
92 GtkWidgetClass *widget_class;
94 parent_class = g_type_class_peek_parent(klass);
96 g_object_class = G_OBJECT_CLASS(klass);
97 g_object_class->finalize = claws_spell_entry_finalize;
99 #if !GTK_CHECK_VERSION(3, 0, 0)
100 gtk_object_class = GTK_OBJECT_CLASS(klass);
101 gtk_object_class->destroy = claws_spell_entry_destroy;
104 widget_class = GTK_WIDGET_CLASS(klass);
105 widget_class->button_press_event = claws_spell_entry_button_press;
106 #if !GTK_CHECK_VERSION(3, 0, 0)
107 widget_class->expose_event = claws_spell_entry_expose;
109 widget_class->draw = claws_spell_entry_expose;
110 widget_class->destroy = claws_spell_entry_destroy;
113 g_type_class_add_private(g_object_class,
114 sizeof(struct _ClawsSpellEntryPriv));
117 static void claws_spell_entry_init(ClawsSpellEntry *entry)
119 entry->gtkaspell = NULL;
121 entry->priv = g_new0(ClawsSpellEntryPriv, 1);
122 entry->priv->attr_list = pango_attr_list_new();
123 entry->priv->preedit_length = 0;
125 g_signal_connect(G_OBJECT(entry), "popup-menu",
126 G_CALLBACK(claws_spell_entry_popup_menu), entry);
127 g_signal_connect(G_OBJECT(entry), "populate-popup",
128 G_CALLBACK(claws_spell_entry_populate_popup), NULL);
129 g_signal_connect(G_OBJECT(entry), "changed",
130 G_CALLBACK(claws_spell_entry_changed), NULL);
131 g_signal_connect(G_OBJECT(entry), "preedit-changed",
132 G_CALLBACK(claws_spell_entry_preedit_changed), NULL);
135 static void claws_spell_entry_finalize(GObject *object)
137 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(object);
139 if (entry->priv->attr_list)
140 pango_attr_list_unref(entry->priv->attr_list);
141 if (entry->priv->words)
142 g_strfreev(entry->priv->words);
144 g_free(entry->priv->word_starts);
145 g_free(entry->priv->word_ends);
149 G_OBJECT_CLASS(parent_class)->finalize(object);
152 #if !GTK_CHECK_VERSION(3, 0, 0)
153 static void claws_spell_entry_destroy(GtkObject *object)
155 GTK_OBJECT_CLASS(parent_class)->destroy(object);
158 static void claws_spell_entry_destroy(GtkWidget *object)
160 GTK_WIDGET_CLASS(parent_class)->destroy(object);
164 GtkWidget *claws_spell_entry_new(void)
166 return GTK_WIDGET( g_object_new(CLAWS_TYPE_SPELL_ENTRY, NULL) );
169 void claws_spell_entry_set_gtkaspell(ClawsSpellEntry *entry, GtkAspell *gtkaspell)
171 cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
173 entry->gtkaspell = gtkaspell;
176 static gint claws_spell_entry_find_position (ClawsSpellEntry *_entry, gint x)
179 PangoLayoutLine *line;
183 gint pos, current_pos;
186 GtkEntry *entry = GTK_ENTRY(_entry);
188 g_object_get(entry, "scroll-offset", &scroll_offset, NULL);
189 x = x + scroll_offset;
191 layout = gtk_entry_get_layout(entry);
192 text = pango_layout_get_text(layout);
193 g_object_get(entry, "cursor-position", ¤t_pos, NULL);
194 cursor_index = g_utf8_offset_to_pointer(text, current_pos) - text;
196 line = pango_layout_get_lines(layout)->data;
197 pango_layout_line_x_to_index(line, x * PANGO_SCALE, &index, &trailing);
199 if (index >= cursor_index && _entry->priv->preedit_length) {
200 if (index >= cursor_index + _entry->priv->preedit_length) {
201 index -= _entry->priv->preedit_length;
203 index = cursor_index;
208 pos = g_utf8_pointer_to_offset (text, text + index);
214 static void get_word_extents_from_position(ClawsSpellEntry *entry, gint *start,
215 gint *end, guint position)
223 if (entry->priv->words == NULL)
226 text = gtk_entry_get_text(GTK_ENTRY(entry));
227 bytes_pos = (gint) (g_utf8_offset_to_pointer(text, position) - text);
229 for (i = 0; entry->priv->words[i]; i++) {
230 if (bytes_pos >= entry->priv->word_starts[i] &&
231 bytes_pos <= entry->priv->word_ends[i]) {
232 *start = entry->priv->word_starts[i];
233 *end = entry->priv->word_ends[i];
239 static gchar *get_word(ClawsSpellEntry *entry, const int start, const int end)
247 text = gtk_entry_get_text(GTK_ENTRY(entry));
248 word = g_new0(gchar, end - start + 2);
249 g_strlcpy(word, text + start, end - start + 1);
254 static void replace_word(ClawsSpellEntry *entry, const gchar *newword)
256 gint cursor, start_pos, end_pos;
257 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
259 start_pos = entry->gtkaspell->start_pos;
260 end_pos = entry->gtkaspell->end_pos;
262 cursor = gtk_editable_get_position(GTK_EDITABLE(entry));
263 /* is the cursor at the end? If so, restore it there */
264 if (g_utf8_strlen(text, -1) == cursor)
266 else if(cursor < entry->priv->mark_character ||
267 cursor > entry->priv->mark_character)
268 cursor = entry->priv->mark_character;
270 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, end_pos);
271 gtk_editable_insert_text(GTK_EDITABLE(entry), newword, strlen(newword),
273 gtk_editable_set_position(GTK_EDITABLE(entry), cursor);
277 static gboolean word_misspelled(ClawsSpellEntry *entry, int start, int end)
282 word = get_word(entry, start, end);
283 if (word == NULL || g_unichar_isdigit(word[0]))
286 ret = gtkaspell_misspelled_test(entry->gtkaspell, word);
292 static gboolean is_word_end (GtkEntry *entry, const int offset)
294 gchar *p = gtk_editable_get_chars(GTK_EDITABLE(entry), offset, offset+1);
297 ch = g_utf8_get_char(p);
304 p = gtk_editable_get_chars(GTK_EDITABLE(entry), offset+1, offset+2);
305 ch = g_utf8_get_char(p);
308 return (g_unichar_isspace(ch) || g_unichar_ispunct(ch)
309 || g_unichar_isdigit(ch));
312 return (g_unichar_isspace(ch) || g_unichar_ispunct(ch));
315 static void entry_strsplit_utf8(GtkEntry *entry, gchar ***set, gint **starts, gint **ends)
318 PangoLogAttr *log_attrs;
320 gint n_attrs, n_strings, i, j;
322 layout = gtk_entry_get_layout(GTK_ENTRY(entry));
323 text = gtk_entry_get_text(GTK_ENTRY(entry));
324 pango_layout_get_log_attrs(layout, &log_attrs, &n_attrs);
326 /* Find how many words we have */
328 for (i = 0; i < n_attrs; i++)
329 if (log_attrs[i].is_word_start)
332 *set = g_new0(gchar *, n_strings + 1);
333 *starts = g_new0(gint, n_strings);
334 *ends = g_new0(gint, n_strings);
336 /* Copy out strings */
337 for (i = 0, j = 0; i < n_attrs; i++) {
338 if (log_attrs[i].is_word_start) {
342 /* Find the end of this string */
344 while (!is_word_end(entry, cend))
347 /* Copy sub-string */
348 start = g_utf8_offset_to_pointer(text, i);
349 bytes = (gint) (g_utf8_offset_to_pointer(text, cend) - start);
350 (*set)[j] = g_new0(gchar, bytes + 1);
351 (*starts)[j] = (gint) (start - text);
352 (*ends)[j] = (gint) (start - text + bytes);
353 g_utf8_strncpy((*set)[j], start, cend - i);
355 /* Move on to the next word */
363 static void insert_misspelled_marker(ClawsSpellEntry *entry, guint start, guint end)
365 guint16 red = (guint16) (((gdouble)((prefs_common.color[COL_MISSPELLED] &
366 0xff0000) >> 16) / 255.0) * 65535.0);
367 guint16 green = (guint16) (((gdouble)((prefs_common.color[COL_MISSPELLED] &
368 0x00ff00) >> 8) / 255.0) * 65535.0);
369 guint16 blue = (guint16) (((gdouble) (prefs_common.color[COL_MISSPELLED] &
370 0x0000ff) / 255.0) * 65535.0);
371 PangoAttribute *fcolor, *ucolor, *unline;
373 if(prefs_common.color[COL_MISSPELLED] != 0) {
374 fcolor = pango_attr_foreground_new(red, green, blue);
375 fcolor->start_index = start;
376 fcolor->end_index = end;
378 pango_attr_list_insert(entry->priv->attr_list, fcolor);
380 ucolor = pango_attr_underline_color_new (65535, 0, 0);
381 unline = pango_attr_underline_new (PANGO_UNDERLINE_ERROR);
383 ucolor->start_index = start;
384 unline->start_index = start;
386 ucolor->end_index = end;
387 unline->end_index = end;
389 pango_attr_list_insert (entry->priv->attr_list, ucolor);
390 pango_attr_list_insert (entry->priv->attr_list, unline);
394 static gboolean check_word(ClawsSpellEntry *entry, int start, int end)
396 GtkAspell *gtkaspell = entry->gtkaspell;
397 PangoAttrIterator *it;
400 gchar *text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
403 /* Check to see if we've got any attributes at this position.
404 * If so, free them, since we'll readd it if the word is misspelled */
405 it = pango_attr_list_get_iterator(entry->priv->attr_list);
409 pango_attr_iterator_range(it, &s, &e);
411 GSList *attrs = pango_attr_iterator_get_attrs(it);
412 g_slist_foreach(attrs, (GFunc) pango_attribute_destroy, NULL);
415 } while (pango_attr_iterator_next(it));
416 pango_attr_iterator_destroy(it);
418 if ((misspelled = word_misspelled(entry, start, end))) {
419 insert_misspelled_marker(entry, start, end);
421 word = get_word(entry, start, end);
422 strncpy(gtkaspell->theword, (gchar *)word, GTKASPELLWORDSIZE - 1);
423 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
424 gtkaspell->start_pos = g_utf8_pointer_to_offset(text, (text+start));
425 gtkaspell->end_pos = g_utf8_pointer_to_offset(text, (text+end));
426 gtkaspell_free_suggestions_list(gtkaspell);
435 void claws_spell_entry_recheck_all(ClawsSpellEntry *entry)
437 GtkAllocation allocation;
442 cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
443 cm_return_if_fail(entry->gtkaspell != NULL);
445 if (entry->priv->words == NULL)
448 /* Remove all existing pango attributes. These will get readded as we check */
449 pango_attr_list_unref(entry->priv->attr_list);
450 entry->priv->attr_list = pango_attr_list_new();
452 /* Loop through words */
453 for (i = 0; entry->priv->words[i]; i++) {
454 length = strlen(entry->priv->words[i]);
457 check_word(entry, entry->priv->word_starts[i], entry->priv->word_ends[i]);
460 layout = gtk_entry_get_layout(GTK_ENTRY(entry));
461 pango_layout_set_attributes(layout, entry->priv->attr_list);
463 if (gtk_widget_get_realized(GTK_WIDGET(entry))) {
464 rect.x = 0; rect.y = 0;
465 gtk_widget_get_allocation(GTK_WIDGET(entry), &allocation);
466 rect.width = allocation.width;
467 rect.height = allocation.height;
468 gdk_window_invalidate_rect(gtk_widget_get_window(GTK_WIDGET(entry)),
473 #if !GTK_CHECK_VERSION(3, 0, 0)
474 static gint claws_spell_entry_expose(GtkWidget *widget, GdkEventExpose *event)
476 static gint claws_spell_entry_expose(GtkWidget *widget, cairo_t *cr)
479 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
480 GtkEntry *gtk_entry = GTK_ENTRY(widget);
483 if (entry->gtkaspell != NULL) {
484 layout = gtk_entry_get_layout(gtk_entry);
485 pango_layout_set_attributes(layout, entry->priv->attr_list);
488 #if !GTK_CHECK_VERSION(3, 0, 0)
489 return GTK_WIDGET_CLASS(parent_class)->expose_event (widget, event);
491 return GTK_WIDGET_CLASS(parent_class)->draw (widget, cr);
495 static gint claws_spell_entry_button_press(GtkWidget *widget, GdkEventButton *event)
497 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
500 pos = claws_spell_entry_find_position(entry, event->x);
501 entry->priv->mark_character = pos;
503 return GTK_WIDGET_CLASS(parent_class)->button_press_event (widget, event);
506 static gboolean claws_spell_entry_popup_menu(GtkWidget *widget, ClawsSpellEntry *entry)
508 entry->priv->mark_character = gtk_editable_get_position (GTK_EDITABLE (entry));
512 static void set_position(gpointer data, gint pos)
514 gtk_editable_set_position(GTK_EDITABLE(data), pos);
517 static gboolean find_misspelled_cb(gpointer data, gboolean forward)
519 ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
520 GtkAspell *gtkaspell = entry->gtkaspell;
521 gboolean misspelled = FALSE;
522 gint cursor, minpos, maxpos, i, words_len = 0;
526 if (entry->priv->words == NULL)
529 gtkaspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
530 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
531 cursor = g_utf8_offset_to_pointer(text, gtkaspell->orig_pos) - text;
533 if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &start, &end)) {
534 minpos = g_utf8_offset_to_pointer(text, start) - text;
535 maxpos = g_utf8_offset_to_pointer(text, end) - text;
537 minpos = forward ? cursor : 0;
538 maxpos = forward ? strlen(text)-1 : cursor;
542 while(entry->priv->words[words_len])
546 for(i=0; i < words_len; i++)
547 if (entry->priv->word_ends[i] > minpos &&
548 (misspelled = check_word(entry,
549 entry->priv->word_starts[i],
550 entry->priv->word_ends[i])))
553 for(i=words_len-1; i >= 0; i--)
554 if (entry->priv->word_starts[i] < maxpos &&
555 (misspelled = check_word(entry,
556 entry->priv->word_starts[i],
557 entry->priv->word_ends[i])))
564 static gboolean check_word_cb(gpointer data)
566 ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
569 get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character);
570 return check_word(entry, start, end);
573 static void replace_word_cb(gpointer data, const gchar *newword)
575 replace_word((ClawsSpellEntry *) data, newword);
578 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
579 gboolean *push_in, gpointer data)
581 ClawsSpellEntry *entry = (ClawsSpellEntry *) data;
582 GtkAspell *gtkaspell = entry->gtkaspell;
583 gint pango_offset, win_x, win_y, scr_x, scr_y, text_index, entry_x;
585 GtkRequisition subject_rq;
586 PangoLayout *layout = gtk_entry_get_layout(GTK_ENTRY(entry));
587 PangoLayoutLine *line = pango_layout_get_lines(layout)->data;
589 gtk_widget_get_child_requisition(GTK_WIDGET(entry), &subject_rq);
591 /* screen -> compose window coords */
592 gdk_window_get_origin(gtk_widget_get_window(GTK_WIDGET(gtkaspell->parent_window)),
595 /* compose window -> subject entry coords */
596 gtk_widget_translate_coordinates(GTK_WIDGET(entry),
597 gtkaspell->parent_window, 0, 0, &win_x, &win_y);
599 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
600 text_index = g_utf8_offset_to_pointer(text, gtkaspell->end_pos) - text;
603 pango_offset = gtk_entry_text_index_to_layout_index(GTK_ENTRY(entry),
605 pango_layout_line_index_to_x(line, pango_offset, TRUE, &entry_x);
607 *x = scr_x + win_x + PANGO_PIXELS(entry_x) + 8;
608 *y = scr_y + win_y + subject_rq.height;
611 void claws_spell_entry_context_set(ClawsSpellEntry *entry)
613 cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
614 cm_return_if_fail(entry->gtkaspell != NULL);
616 entry->gtkaspell->ctx.set_position = set_position;
617 entry->gtkaspell->ctx.set_menu_pos = set_menu_pos;
618 entry->gtkaspell->ctx.find_misspelled = find_misspelled_cb;
619 entry->gtkaspell->ctx.check_word = check_word_cb;
620 entry->gtkaspell->ctx.replace_word = replace_word_cb;
621 entry->gtkaspell->ctx.data = (gpointer) entry;
624 static void claws_spell_entry_populate_popup(ClawsSpellEntry *entry, GtkMenu *menu,
627 GtkAspell *gtkaspell = entry->gtkaspell;
631 if (gtkaspell == NULL)
634 get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character);
636 if ((word = get_word(entry, start, end)) != NULL) {
637 strncpy(gtkaspell->theword, word, GTKASPELLWORDSIZE - 1);
641 gtkaspell->misspelled = word_misspelled(entry, start, end);
643 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
644 gtkaspell->start_pos = g_utf8_pointer_to_offset(text, (text+start));
645 gtkaspell->end_pos = g_utf8_pointer_to_offset(text, (text+end));
648 claws_spell_entry_context_set(entry);
649 gtkaspell_make_context_menu(menu, gtkaspell);
652 static void claws_spell_entry_changed(GtkEditable *editable, gpointer data)
654 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(editable);
656 if (entry->gtkaspell == NULL)
659 if (entry->priv->words) {
660 g_strfreev(entry->priv->words);
661 g_free(entry->priv->word_starts);
662 g_free(entry->priv->word_ends);
664 entry_strsplit_utf8(GTK_ENTRY(entry), &entry->priv->words,
665 &entry->priv->word_starts, &entry->priv->word_ends);
666 if(entry->gtkaspell->check_while_typing == TRUE)
667 claws_spell_entry_recheck_all(entry);
670 static void claws_spell_entry_preedit_changed (GtkEntry *_entry,
674 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(_entry);
676 entry->priv->preedit_length = preedit != NULL ? strlen(preedit) : 0;
679 static void continue_check(gpointer *data)
681 ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
682 GtkAspell *gtkaspell = entry->gtkaspell;
683 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
685 if (gtkaspell->misspelled && pos < gtkaspell->end_check_pos)
686 gtkaspell->misspelled = gtkaspell_check_next_prev(gtkaspell, TRUE);
688 gtkaspell->continue_check = NULL;
691 void claws_spell_entry_check_all(ClawsSpellEntry *entry)
696 cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
697 cm_return_if_fail(entry->gtkaspell != NULL);
699 if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &start, &end)) {
700 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
703 end = g_utf8_strlen(text, -1) - 1;
708 gtk_editable_set_position(GTK_EDITABLE(entry), start);
709 entry->gtkaspell->continue_check = continue_check;
710 entry->gtkaspell->end_check_pos = end;
712 claws_spell_entry_context_set(entry);
713 entry->gtkaspell->misspelled =
714 gtkaspell_check_next_prev(entry->gtkaspell, TRUE);
717 void claws_spell_entry_check_backwards(ClawsSpellEntry *entry)
719 cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
720 cm_return_if_fail(entry->gtkaspell != NULL);
722 entry->gtkaspell->continue_check = NULL;
723 claws_spell_entry_context_set(entry);
724 gtkaspell_check_next_prev(entry->gtkaspell, FALSE);
727 void claws_spell_entry_check_forwards_go(ClawsSpellEntry *entry)
729 cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
730 cm_return_if_fail(entry->gtkaspell != NULL);
732 entry->gtkaspell->continue_check = NULL;
733 claws_spell_entry_context_set(entry);
734 gtkaspell_check_next_prev(entry->gtkaspell, TRUE);
737 #endif /* USE_ENCHANT */