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.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 Adapted for Claws Mail (c) 2009 Pawel Pekala
34 #include <glib/gi18n.h>
37 #include <gdk/gdkkeysyms.h>
43 #include "spell_entry.h"
44 #include "prefs_common.h"
48 static void claws_spell_entry_init (ClawsSpellEntry *entry);
49 static void claws_spell_entry_editable_init (GtkEditableClass *iface);
50 static void claws_spell_entry_finalize (GObject *object);
51 static void claws_spell_entry_destroy (GtkObject *object);
52 static gint claws_spell_entry_expose (GtkWidget *widget,
53 GdkEventExpose *event);
54 static gint claws_spell_entry_button_press (GtkWidget *widget,
55 GdkEventButton *event);
56 static gboolean claws_spell_entry_popup_menu (GtkWidget *widget,
57 ClawsSpellEntry *entry);
58 static void claws_spell_entry_populate_popup (ClawsSpellEntry *entry,
61 static void claws_spell_entry_changed (GtkEditable *editable,
64 struct _ClawsSpellEntryPriv
66 PangoAttrList *attr_list;
73 static GtkEntryClass *parent_class = NULL;
76 G_DEFINE_TYPE_EXTENDED(ClawsSpellEntry, claws_spell_entry, GTK_TYPE_ENTRY, 0, G_IMPLEMENT_INTERFACE(GTK_TYPE_EDITABLE, claws_spell_entry_editable_init));
79 static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass)
81 GObjectClass *g_object_class;
82 GtkObjectClass *gtk_object_class;
83 GtkWidgetClass *widget_class;
85 parent_class = g_type_class_peek_parent(klass);
87 g_object_class = G_OBJECT_CLASS(klass);
88 g_object_class->finalize = claws_spell_entry_finalize;
90 gtk_object_class = GTK_OBJECT_CLASS(klass);
91 gtk_object_class->destroy = claws_spell_entry_destroy;
93 widget_class = GTK_WIDGET_CLASS(klass);
94 widget_class->expose_event = claws_spell_entry_expose;
95 widget_class->button_press_event = claws_spell_entry_button_press;
97 g_type_class_add_private(g_object_class,
98 sizeof(struct _ClawsSpellEntryPriv));
101 static void claws_spell_entry_init(ClawsSpellEntry *entry)
103 entry->gtkaspell = NULL;
105 entry->priv = g_new0(ClawsSpellEntryPriv, 1);
106 entry->priv->attr_list = pango_attr_list_new();
108 g_signal_connect(G_OBJECT(entry), "popup-menu",
109 G_CALLBACK(claws_spell_entry_popup_menu), entry);
110 g_signal_connect(G_OBJECT(entry), "populate-popup",
111 G_CALLBACK(claws_spell_entry_populate_popup), NULL);
112 g_signal_connect(G_OBJECT(entry), "changed",
113 G_CALLBACK(claws_spell_entry_changed), NULL);
116 static void claws_spell_entry_editable_init (GtkEditableClass *iface) {}
118 static void claws_spell_entry_finalize(GObject *object)
120 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(object);
122 if (entry->priv->attr_list)
123 pango_attr_list_unref(entry->priv->attr_list);
124 if (entry->priv->words)
125 g_strfreev(entry->priv->words);
127 g_free(entry->priv->word_starts);
128 g_free(entry->priv->word_ends);
132 G_OBJECT_CLASS(parent_class)->finalize(object);
135 static void claws_spell_entry_destroy(GtkObject *object)
137 GTK_OBJECT_CLASS(parent_class)->destroy(object);
140 GtkWidget *claws_spell_entry_new(void)
142 return GTK_WIDGET( g_object_new(CLAWS_TYPE_SPELL_ENTRY, NULL) );
145 void claws_spell_entry_set_gtkaspell(ClawsSpellEntry *entry, GtkAspell *gtkaspell)
147 g_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
148 g_return_if_fail(gtkaspell != NULL);
150 entry->gtkaspell = gtkaspell;
153 static gint gtk_entry_find_position (GtkEntry *entry, gint x)
156 PangoLayoutLine *line;
163 x = x + entry->scroll_offset;
165 layout = gtk_entry_get_layout(entry);
166 text = pango_layout_get_text(layout);
167 cursor_index = g_utf8_offset_to_pointer(text, entry->current_pos) - text;
169 line = pango_layout_get_lines(layout)->data;
170 pango_layout_line_x_to_index(line, x * PANGO_SCALE, &index, &trailing);
172 if (index >= cursor_index && entry->preedit_length) {
173 if (index >= cursor_index + entry->preedit_length) {
174 index -= entry->preedit_length;
176 index = cursor_index;
181 pos = g_utf8_pointer_to_offset (text, text + index);
187 static void get_word_extents_from_position(ClawsSpellEntry *entry, gint *start,
188 gint *end, guint position)
196 if (entry->priv->words == NULL)
199 text = gtk_entry_get_text(GTK_ENTRY(entry));
200 bytes_pos = (gint) (g_utf8_offset_to_pointer(text, position) - text);
202 for (i = 0; entry->priv->words[i]; i++) {
203 if (bytes_pos >= entry->priv->word_starts[i] &&
204 bytes_pos <= entry->priv->word_ends[i]) {
205 *start = entry->priv->word_starts[i];
206 *end = entry->priv->word_ends[i];
212 static gchar *get_word(ClawsSpellEntry *entry, const int start, const int end)
220 text = gtk_entry_get_text(GTK_ENTRY(entry));
221 word = g_new0(gchar, end - start + 2);
222 g_strlcpy(word, text + start, end - start + 1);
227 static void replace_word(ClawsSpellEntry *entry, const gchar *newword)
229 gint cursor, start_pos, end_pos;
230 const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
232 start_pos = entry->gtkaspell->start_pos;
233 end_pos = entry->gtkaspell->end_pos;
235 cursor = gtk_editable_get_position(GTK_EDITABLE(entry));
236 /* is the cursor at the end? If so, restore it there */
237 if (g_utf8_strlen(text, -1) == cursor)
239 else if(cursor < entry->priv->mark_character ||
240 cursor > entry->priv->mark_character)
241 cursor = entry->priv->mark_character;
243 gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, end_pos);
244 gtk_editable_insert_text(GTK_EDITABLE(entry), newword, strlen(newword),
246 gtk_editable_set_position(GTK_EDITABLE(entry), cursor);
250 static gboolean word_misspelled(ClawsSpellEntry *entry, int start, int end)
255 word = get_word(entry, start, end);
259 ret = gtkaspell_misspelled_test(entry->gtkaspell, word);
265 static void entry_strsplit_utf8(GtkEntry *entry, gchar ***set, gint **starts, gint **ends)
268 PangoLogAttr *log_attrs;
270 gint n_attrs, n_strings, i, j;
272 layout = gtk_entry_get_layout(GTK_ENTRY(entry));
273 text = gtk_entry_get_text(GTK_ENTRY(entry));
274 pango_layout_get_log_attrs(layout, &log_attrs, &n_attrs);
276 /* Find how many words we have */
278 for (i = 0; i < n_attrs; i++)
279 if (log_attrs[i].is_word_start)
282 *set = g_new0(gchar *, n_strings + 1);
283 *starts = g_new0(gint, n_strings);
284 *ends = g_new0(gint, n_strings);
286 /* Copy out strings */
287 for (i = 0, j = 0; i < n_attrs; i++) {
288 if (log_attrs[i].is_word_start) {
292 /* Find the end of this string */
294 while (!(log_attrs[cend].is_word_end))
297 /* Copy sub-string */
298 start = g_utf8_offset_to_pointer(text, i);
299 bytes = (gint) (g_utf8_offset_to_pointer(text, cend) - start);
300 (*set)[j] = g_new0(gchar, bytes + 1);
301 (*starts)[j] = (gint) (start - text);
302 (*ends)[j] = (gint) (start - text + bytes);
303 g_utf8_strncpy((*set)[j], start, cend - i);
305 /* Move on to the next word */
313 static void insert_misspelled_marker(ClawsSpellEntry *entry, guint start, guint end)
315 guint16 red = (guint16) (((gdouble)((prefs_common.misspelled_col &
316 0xff0000) >> 16) / 255.0) * 65535.0);
317 guint16 green = (guint16) (((gdouble)((prefs_common.misspelled_col &
318 0x00ff00) >> 8) / 255.0) * 65535.0);
319 guint16 blue = (guint16) (((gdouble) (prefs_common.misspelled_col &
320 0x0000ff) / 255.0) * 65535.0);
321 PangoAttribute *fcolor, *ucolor, *unline;
323 if(prefs_common.misspelled_col != 0) {
324 fcolor = pango_attr_foreground_new(red, green, blue);
325 fcolor->start_index = start;
326 fcolor->end_index = end;
328 pango_attr_list_insert(entry->priv->attr_list, fcolor);
330 ucolor = pango_attr_underline_color_new (65535, 0, 0);
331 unline = pango_attr_underline_new (PANGO_UNDERLINE_ERROR);
333 ucolor->start_index = start;
334 unline->start_index = start;
336 ucolor->end_index = end;
337 unline->end_index = end;
339 pango_attr_list_insert (entry->priv->attr_list, ucolor);
340 pango_attr_list_insert (entry->priv->attr_list, unline);
344 static gboolean check_word(ClawsSpellEntry *entry, int start, int end)
346 GtkAspell *gtkaspell = entry->gtkaspell;
347 PangoAttrIterator *it;
350 gchar *text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
353 /* Check to see if we've got any attributes at this position.
354 * If so, free them, since we'll readd it if the word is misspelled */
355 it = pango_attr_list_get_iterator(entry->priv->attr_list);
359 pango_attr_iterator_range(it, &s, &e);
361 GSList *attrs = pango_attr_iterator_get_attrs(it);
362 g_slist_foreach(attrs, (GFunc) pango_attribute_destroy, NULL);
365 } while (pango_attr_iterator_next(it));
366 pango_attr_iterator_destroy(it);
368 if ((misspelled = word_misspelled(entry, start, end))) {
369 insert_misspelled_marker(entry, start, end);
371 word = get_word(entry, start, end);
372 strncpy(gtkaspell->theword, (gchar *)word, GTKASPELLWORDSIZE - 1);
373 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
374 gtkaspell->start_pos = g_utf8_pointer_to_offset(text, (text+start));
375 gtkaspell->end_pos = g_utf8_pointer_to_offset(text, (text+end));
376 gtkaspell_free_suggestions_list(gtkaspell);
385 void claws_spell_entry_recheck_all(ClawsSpellEntry *entry)
391 g_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
392 g_return_if_fail(entry->gtkaspell != NULL);
394 if (entry->priv->words == NULL)
397 /* Remove all existing pango attributes. These will get readded as we check */
398 pango_attr_list_unref(entry->priv->attr_list);
399 entry->priv->attr_list = pango_attr_list_new();
401 /* Loop through words */
402 for (i = 0; entry->priv->words[i]; i++) {
403 length = strlen(entry->priv->words[i]);
406 check_word(entry, entry->priv->word_starts[i], entry->priv->word_ends[i]);
409 layout = gtk_entry_get_layout(GTK_ENTRY(entry));
410 pango_layout_set_attributes(layout, entry->priv->attr_list);
412 if (GTK_WIDGET_REALIZED(GTK_WIDGET(entry))) {
413 rect.x = 0; rect.y = 0;
414 rect.width = GTK_WIDGET(entry)->allocation.width;
415 rect.height = GTK_WIDGET(entry)->allocation.height;
416 gdk_window_invalidate_rect(GTK_WIDGET(entry)->window, &rect, TRUE);
420 static gint claws_spell_entry_expose(GtkWidget *widget, GdkEventExpose *event)
422 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
423 GtkEntry *gtk_entry = GTK_ENTRY(widget);
426 if (entry->gtkaspell != NULL) {
427 layout = gtk_entry_get_layout(gtk_entry);
428 pango_layout_set_attributes(layout, entry->priv->attr_list);
431 return GTK_WIDGET_CLASS(parent_class)->expose_event (widget, event);
434 static gint claws_spell_entry_button_press(GtkWidget *widget, GdkEventButton *event)
436 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
437 GtkEntry *gtk_entry = GTK_ENTRY(widget);
440 pos = gtk_entry_find_position(gtk_entry, event->x);
441 entry->priv->mark_character = pos;
443 return GTK_WIDGET_CLASS(parent_class)->button_press_event (widget, event);
446 static gboolean claws_spell_entry_popup_menu(GtkWidget *widget, ClawsSpellEntry *entry)
448 entry->priv->mark_character = gtk_editable_get_position (GTK_EDITABLE (entry));
452 static void set_position(gpointer data, gint pos)
454 gtk_editable_set_position(GTK_EDITABLE(data), pos);
457 static gboolean find_misspelled_cb(gpointer data, gboolean forward)
459 ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
460 GtkAspell *gtkaspell = entry->gtkaspell;
461 gboolean misspelled = FALSE;
462 gint cursor, minpos, maxpos, i, words_len = 0;
466 if (entry->priv->words == NULL)
469 gtkaspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(entry));
470 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
471 cursor = g_utf8_offset_to_pointer(text, gtkaspell->orig_pos) - text;
473 if (gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &start, &end)) {
474 minpos = g_utf8_offset_to_pointer(text, start) - text;
475 maxpos = g_utf8_offset_to_pointer(text, end) - text;
477 minpos = forward ? cursor : 0;
478 maxpos = forward ? strlen(text)-1 : cursor;
482 while(entry->priv->words[words_len])
486 for(i=0; i < words_len; i++)
487 if (entry->priv->word_ends[i] > minpos &&
488 (misspelled = check_word(entry,
489 entry->priv->word_starts[i],
490 entry->priv->word_ends[i])))
493 for(i=words_len-1; i >= 0; i--)
494 if (entry->priv->word_starts[i] < maxpos &&
495 (misspelled = check_word(entry,
496 entry->priv->word_starts[i],
497 entry->priv->word_ends[i])))
504 static gboolean check_word_cb(gpointer data)
506 ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
509 get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character);
510 return check_word(entry, start, end);
513 static void replace_word_cb(gpointer data, const gchar *newword)
515 replace_word((ClawsSpellEntry *) data, newword);
518 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
519 gboolean *push_in, gpointer data)
521 ClawsSpellEntry *entry = (ClawsSpellEntry *) data;
522 GtkAspell *gtkaspell = entry->gtkaspell;
523 gint pango_offset, win_x, win_y, scr_x, scr_y, text_index, entry_x;
525 GtkRequisition subject_rq;
526 PangoLayout *layout = gtk_entry_get_layout(GTK_ENTRY(entry));
527 PangoLayoutLine *line = pango_layout_get_lines(layout)->data;
529 gtk_widget_get_child_requisition(GTK_WIDGET(entry), &subject_rq);
531 /* screen -> compose window coords */
532 gdk_window_get_origin(GTK_WIDGET(gtkaspell->parent_window)->window,
535 /* compose window -> subject entry coords */
536 gtk_widget_translate_coordinates(GTK_WIDGET(entry),
537 gtkaspell->parent_window, 0, 0, &win_x, &win_y);
539 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
540 text_index = g_utf8_offset_to_pointer(text, gtkaspell->end_pos) - text;
543 pango_offset = gtk_entry_text_index_to_layout_index(GTK_ENTRY(entry),
545 pango_layout_line_index_to_x(line, pango_offset, TRUE, &entry_x);
547 *x = scr_x + win_x + PANGO_PIXELS(entry_x) + 8;
548 *y = scr_y + win_y + subject_rq.height;
551 void claws_spell_entry_context_set(ClawsSpellEntry *entry)
553 g_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
554 g_return_if_fail(entry->gtkaspell != NULL);
556 entry->gtkaspell->ctx.set_position = set_position;
557 entry->gtkaspell->ctx.set_menu_pos = set_menu_pos;
558 entry->gtkaspell->ctx.find_misspelled = find_misspelled_cb;
559 entry->gtkaspell->ctx.check_word = check_word_cb;
560 entry->gtkaspell->ctx.replace_word = replace_word_cb;
561 entry->gtkaspell->ctx.data = (gpointer) entry;
564 static void claws_spell_entry_populate_popup(ClawsSpellEntry *entry, GtkMenu *menu,
567 GtkAspell *gtkaspell = entry->gtkaspell;
571 if (gtkaspell == NULL)
574 get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character);
576 if ((word = get_word(entry, start, end)) != NULL) {
577 strncpy(gtkaspell->theword, word, GTKASPELLWORDSIZE - 1);
581 gtkaspell->misspelled = word_misspelled(entry, start, end);
583 claws_spell_entry_context_set(entry);
584 gtkaspell_make_context_menu(menu, gtkaspell);
587 static void claws_spell_entry_changed(GtkEditable *editable, gpointer data)
589 ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(editable);
591 if (entry->gtkaspell == NULL)
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);
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);
605 static void continue_check(gpointer *data)
607 ClawsSpellEntry *entry = (ClawsSpellEntry *)data;
608 GtkAspell *gtkaspell = entry->gtkaspell;
609 gint pos = gtk_editable_get_position(GTK_EDITABLE(entry));
611 if (gtkaspell->misspelled && pos < gtkaspell->end_check_pos)
612 gtkaspell->misspelled = gtkaspell_check_next_prev(gtkaspell, TRUE);
614 gtkaspell->continue_check = NULL;
617 void claws_spell_entry_check_all(ClawsSpellEntry *entry)
622 g_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
623 g_return_if_fail(entry->gtkaspell != NULL);
625 if (!gtk_editable_get_selection_bounds(GTK_EDITABLE(entry), &start, &end)) {
626 text = gtk_editable_get_chars(GTK_EDITABLE(entry), 0, -1);
629 end = g_utf8_strlen(text, -1) - 1;
634 gtk_editable_set_position(GTK_EDITABLE(entry), start);
635 entry->gtkaspell->continue_check = continue_check;
636 entry->gtkaspell->end_check_pos = end;
638 claws_spell_entry_context_set(entry);
639 entry->gtkaspell->misspelled =
640 gtkaspell_check_next_prev(entry->gtkaspell, TRUE);
643 void claws_spell_entry_check_backwards(ClawsSpellEntry *entry)
645 g_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
646 g_return_if_fail(entry->gtkaspell != NULL);
648 entry->gtkaspell->continue_check = NULL;
649 claws_spell_entry_context_set(entry);
650 gtkaspell_check_next_prev(entry->gtkaspell, FALSE);
653 void claws_spell_entry_check_forwards_go(ClawsSpellEntry *entry)
655 g_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
656 g_return_if_fail(entry->gtkaspell != NULL);
658 entry->gtkaspell->continue_check = NULL;
659 claws_spell_entry_context_set(entry);
660 gtkaspell_check_next_prev(entry->gtkaspell, TRUE);
663 #endif /* USE_ENCHANT */