1 /* gtkaspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin (original code for ispell).
3 * Copyright (c) 2002 Melvin Hadasht.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
21 * Adapted for Sylpheed (Claws) (c) 2001-2002 by Hiroyuki Yamamoto &
22 * The Sylpheed Claws Team.
23 * Adapted for pspell (c) 2001-2002 Melvin Hadasht
24 * Adapted for GNU/aspell (c) 2002 Melvin Hadasht
36 #include <sys/types.h>
48 #include <glib/gi18n.h>
52 #include <gtk/gtkoptionmenu.h>
53 #include <gtk/gtkmenu.h>
54 #include <gtk/gtkmenuitem.h>
55 #include <gdk/gdkkeysyms.h>
61 #include "alertpanel.h"
62 #include "gtkaspell.h"
63 #include "gtk/gtkutils.h"
65 #define ASPELL_FASTMODE 1
66 #define ASPELL_NORMALMODE 2
67 #define ASPELL_BADSPELLERMODE 3
69 #define GTKASPELLWORDSIZE 1024
71 /* size of the text buffer used in various word-processing routines. */
74 /* number of suggestions to display on each menu. */
77 /* 'config' must be defined as a 'AspellConfig *' */
78 #define RETURN_FALSE_IF_CONFIG_ERROR() \
80 if (aspell_config_error_number(config) != 0) { \
81 gtkaspellcheckers->error_message = g_strdup(aspell_config_error_message(config)); \
86 #define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
87 aspell_config_replace(config, option, value); \
88 RETURN_FALSE_IF_CONFIG_ERROR(); \
91 typedef struct _GtkAspellCheckers {
93 GSList *dictionary_list;
97 typedef struct _Dictionary {
103 typedef struct _GtkAspeller {
104 Dictionary *dictionary;
106 AspellConfig *config;
107 AspellSpeller *checker;
110 typedef void (*ContCheckFunc) (gpointer *gtkaspell);
114 GtkAspeller *gtkaspeller;
115 GtkAspeller *alternate_speller;
116 gchar *dictionary_path;
117 gchar theword[GTKASPELLWORDSIZE];
123 gboolean check_while_typing;
124 gboolean recheck_when_changing_dict;
125 gboolean use_alternate;
127 ContCheckFunc continue_check;
129 GtkWidget *replace_entry;
130 GtkWidget *parent_window;
132 gint default_sug_mode;
134 GList *suggestions_list;
136 GtkTextView *gtktext;
138 GtkAccelGroup *accel_group;
139 void (*menu_changed_cb)(void *data);
140 void *menu_changed_data;
143 typedef AspellConfig GtkAspellConfig;
145 /******************************************************************************/
147 static GtkAspellCheckers *gtkaspellcheckers;
149 /* Error message storage */
150 static void gtkaspell_checkers_error_message (gchar *message);
153 static void entry_insert_cb (GtkTextBuffer *textbuf,
157 GtkAspell *gtkaspell);
158 static void entry_delete_cb (GtkTextBuffer *textbuf,
159 GtkTextIter *startiter,
160 GtkTextIter *enditer,
161 GtkAspell *gtkaspell);
162 /*static gint button_press_intercept_cb (GtkTextView *gtktext,
164 GtkAspell *gtkaspell);
166 static void button_press_intercept_cb(GtkTextView *gtktext,
167 GtkMenu *menu, GtkAspell *gtkaspell);
169 /* Checker creation */
170 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
171 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
172 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
173 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
175 /* Checker configuration */
176 static gint set_dictionary (AspellConfig *config,
178 static void set_sug_mode_cb (GtkMenuItem *w,
179 GtkAspell *gtkaspell);
180 static void set_real_sug_mode (GtkAspell *gtkaspell,
181 const char *themode);
183 /* Checker actions */
184 static gboolean check_at (GtkAspell *gtkaspell,
186 static gboolean check_next_prev (GtkAspell *gtkaspell,
188 static GList* misspelled_suggest (GtkAspell *gtkaspell,
190 static void add_word_to_session_cb (GtkWidget *w,
192 static void add_word_to_personal_cb (GtkWidget *w,
194 static void replace_with_create_dialog_cb (GtkWidget *w,
196 static void replace_with_supplied_word_cb (GtkWidget *w,
197 GtkAspell *gtkaspell);
198 static void replace_word_cb (GtkWidget *w,
200 static void replace_real_word (GtkAspell *gtkaspell,
202 static void check_with_alternate_cb (GtkWidget *w,
204 static void use_alternate_dict (GtkAspell *gtkaspell);
205 static void toggle_check_while_typing_cb (GtkWidget *w,
209 static GSList* make_sug_menu (GtkAspell *gtkaspell);
210 static GSList * populate_submenu (GtkAspell *gtkaspell);
211 GSList* gtkaspell_make_config_menu (GtkAspell *gtkaspell);
212 static void set_menu_pos (GtkMenu *menu,
217 /* Other menu callbacks */
218 static gboolean aspell_key_pressed (GtkWidget *widget,
220 GtkAspell *gtkaspell);
221 static void change_dict_cb (GtkWidget *w,
222 GtkAspell *gtkaspell);
223 static void switch_to_alternate_cb (GtkWidget *w,
226 /* Misc. helper functions */
227 static void set_point_continue (GtkAspell *gtkaspell);
228 static void continue_check (gpointer *gtkaspell);
229 static gboolean iswordsep (unsigned char c);
230 static guchar get_text_index_whar (GtkAspell *gtkaspell,
232 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
238 static void allocate_color (GtkAspell *gtkaspell,
240 static void change_color (GtkAspell *gtkaspell,
245 static guchar* convert_to_aspell_encoding (const guchar *encoding);
246 static gint compare_dict (Dictionary *a,
248 static void dictionary_delete (Dictionary *dict);
249 static Dictionary * dictionary_dup (const Dictionary *dict);
250 static void free_suggestions_list (GtkAspell *gtkaspell);
251 static void reset_theword_data (GtkAspell *gtkaspell);
252 static void free_checkers (gpointer elt,
254 static gint find_gtkaspeller (gconstpointer aa,
256 /* gtkspellconfig - only one config per session */
257 GtkAspellConfig * gtkaspellconfig;
258 static void destroy_menu(GtkWidget *widget, gpointer user_data);
260 /******************************************************************************/
261 static gint get_textview_buffer_charcount(GtkTextView *view);
263 static gint get_textview_buffer_charcount(GtkTextView *view)
265 GtkTextBuffer *buffer;
267 g_return_val_if_fail(view, 0);
269 buffer = gtk_text_view_get_buffer(view);
270 g_return_val_if_fail(buffer, 0);
272 return gtk_text_buffer_get_char_count(buffer);
274 static gint get_textview_buffer_offset(GtkTextView *view)
276 GtkTextBuffer * buffer;
280 g_return_val_if_fail(view, 0);
282 buffer = gtk_text_view_get_buffer(view);
283 g_return_val_if_fail(buffer, 0);
285 mark = gtk_text_buffer_get_insert(buffer);
286 g_return_val_if_fail(mark, 0);
288 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
290 return gtk_text_iter_get_offset(&iter);
292 static void set_textview_buffer_offset(GtkTextView *view, gint offset)
294 GtkTextBuffer *buffer;
297 g_return_if_fail(view);
299 buffer = gtk_text_view_get_buffer(view);
300 g_return_if_fail(buffer);
302 gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
303 gtk_text_buffer_place_cursor(buffer, &iter);
305 /******************************************************************************/
307 void gtkaspell_checkers_init(void)
309 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
310 gtkaspellcheckers->checkers = NULL;
311 gtkaspellcheckers->dictionary_list = NULL;
312 gtkaspellcheckers->error_message = NULL;
315 void gtkaspell_checkers_quit(void)
320 if (gtkaspellcheckers == NULL)
323 if ((checkers = gtkaspellcheckers->checkers)) {
324 debug_print("Aspell: number of running checkers to delete %d\n",
325 g_slist_length(checkers));
327 g_slist_foreach(checkers, free_checkers, NULL);
328 g_slist_free(checkers);
329 gtkaspellcheckers->checkers = NULL;
332 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
333 debug_print("Aspell: number of dictionaries to delete %d\n",
334 g_slist_length(dict_list));
336 gtkaspell_free_dictionary_list(dict_list);
337 gtkaspellcheckers->dictionary_list = NULL;
340 g_free(gtkaspellcheckers->error_message);
341 gtkaspellcheckers->error_message = NULL;
345 static void gtkaspell_checkers_error_message (gchar *message)
348 if (gtkaspellcheckers->error_message) {
349 tmp = g_strdup_printf("%s\n%s",
350 gtkaspellcheckers->error_message,
353 g_free(gtkaspellcheckers->error_message);
354 gtkaspellcheckers->error_message = tmp;
356 gtkaspellcheckers->error_message = message;
359 const char *gtkaspell_checkers_strerror(void)
361 g_return_val_if_fail(gtkaspellcheckers, "");
362 return gtkaspellcheckers->error_message;
365 void gtkaspell_checkers_reset_error(void)
367 g_return_if_fail(gtkaspellcheckers);
369 g_free(gtkaspellcheckers->error_message);
371 gtkaspellcheckers->error_message = NULL;
374 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
375 const gchar *dictionary,
376 const gchar *encoding,
377 gint misspelled_color,
378 gboolean check_while_typing,
379 gboolean recheck_when_changing_dict,
380 gboolean use_alternate,
381 GtkTextView *gtktext,
382 GtkWindow *parent_win,
383 void (*spell_menu_cb)(void *data),
387 GtkAspell *gtkaspell;
388 GtkAspeller *gtkaspeller;
389 GtkTextBuffer *buffer;
391 g_return_val_if_fail(gtktext, NULL);
392 g_return_val_if_fail(dictionary && strlen(dictionary) > 0,
394 g_return_val_if_fail(dictionary_path && strlen(dictionary_path) > 0,
397 buffer = gtk_text_view_get_buffer(gtktext);
399 dict = g_new0(Dictionary, 1);
400 dict->fullname = g_strdup(dictionary);
401 dict->encoding = g_strdup(encoding);
403 gtkaspeller = gtkaspeller_new(dict);
404 dictionary_delete(dict);
409 gtkaspell = g_new0(GtkAspell, 1);
411 gtkaspell->dictionary_path = g_strdup(dictionary_path);
413 gtkaspell->gtkaspeller = gtkaspeller;
414 gtkaspell->alternate_speller = NULL;
415 gtkaspell->theword[0] = 0x00;
416 gtkaspell->start_pos = 0;
417 gtkaspell->end_pos = 0;
418 gtkaspell->orig_pos = -1;
419 gtkaspell->end_check_pos = -1;
420 gtkaspell->misspelled = -1;
421 gtkaspell->check_while_typing = check_while_typing;
422 gtkaspell->recheck_when_changing_dict = recheck_when_changing_dict;
423 gtkaspell->continue_check = NULL;
424 gtkaspell->replace_entry = NULL;
425 gtkaspell->gtktext = gtktext;
426 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
427 gtkaspell->max_sug = -1;
428 gtkaspell->suggestions_list = NULL;
429 gtkaspell->use_alternate = use_alternate;
430 gtkaspell->parent_window = GTK_WIDGET(parent_win);
431 gtkaspell->menu_changed_cb = spell_menu_cb;
432 gtkaspell->menu_changed_data = data;
434 allocate_color(gtkaspell, misspelled_color);
436 g_signal_connect_after(G_OBJECT(buffer), "insert-text",
437 G_CALLBACK(entry_insert_cb), gtkaspell);
438 g_signal_connect_after(G_OBJECT(buffer), "delete-range",
439 G_CALLBACK(entry_delete_cb), gtkaspell);
440 /*g_signal_connect(G_OBJECT(gtktext), "button-press-event",
441 G_CALLBACK(button_press_intercept_cb),
443 g_signal_connect(G_OBJECT(gtktext), "populate-popup",
444 G_CALLBACK(button_press_intercept_cb), gtkaspell);
446 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
451 void gtkaspell_delete(GtkAspell *gtkaspell)
453 GtkTextView *gtktext = gtkaspell->gtktext;
455 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
456 G_CALLBACK(entry_insert_cb),
458 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
459 G_CALLBACK(entry_delete_cb),
461 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
462 G_CALLBACK(button_press_intercept_cb),
465 gtkaspell_uncheck_all(gtkaspell);
467 gtkaspeller_delete(gtkaspell->gtkaspeller);
469 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
470 gtkaspeller_delete(gtkaspell->alternate_speller);
472 if (gtkaspell->suggestions_list)
473 free_suggestions_list(gtkaspell);
475 g_free((gchar *)gtkaspell->dictionary_path);
476 gtkaspell->dictionary_path = NULL;
478 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
485 static void entry_insert_cb(GtkTextBuffer *textbuf,
489 GtkAspell *gtkaspell)
493 g_return_if_fail(gtkaspell->gtkaspeller->checker);
495 if (!gtkaspell->check_while_typing)
498 pos = gtk_text_iter_get_offset(iter);
500 if (iswordsep(newtext[0])) {
501 /* did we just end a word? */
503 check_at(gtkaspell, pos - 2);
505 /* did we just split a word? */
506 if (pos < gtk_text_buffer_get_char_count(textbuf))
507 check_at(gtkaspell, pos + 1);
509 /* check as they type, *except* if they're typing at the end (the most
512 if (pos < gtk_text_buffer_get_char_count(textbuf) &&
513 !iswordsep(get_text_index_whar(gtkaspell, pos))) {
514 check_at(gtkaspell, pos - 1);
519 static void entry_delete_cb(GtkTextBuffer *textbuf,
520 GtkTextIter *startiter,
521 GtkTextIter *enditer,
522 GtkAspell *gtkaspell)
527 g_return_if_fail(gtkaspell->gtkaspeller->checker);
529 if (!gtkaspell->check_while_typing)
532 start = gtk_text_iter_get_offset(startiter);
533 end = gtk_text_iter_get_offset(enditer);
534 origpos = get_textview_buffer_offset(gtkaspell->gtktext);
536 check_at(gtkaspell, start - 1);
537 check_at(gtkaspell, start);
540 set_textview_buffer_offset(gtkaspell->gtktext, origpos);
541 /* this is to *UNDO* the selection, in case they were holding shift
542 * while hitting backspace. */
543 /* needed with textview ??? */
544 /* gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); */
547 static void button_press_intercept_cb(GtkTextView *gtktext,
548 GtkMenu *menu, GtkAspell *gtkaspell)
550 GtkMenuItem *menuitem;
551 GSList *spell_menu = NULL;
553 gboolean suggest = FALSE;
555 menuitem = GTK_MENU_ITEM(gtk_separator_menu_item_new());
556 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), GTK_WIDGET(menuitem));
557 gtk_widget_show(GTK_WIDGET(menuitem));
559 gtktext = gtkaspell->gtktext;
561 gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
563 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
565 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
566 spell_menu = make_sug_menu(gtkaspell);
571 spell_menu = gtkaspell_make_config_menu(gtkaspell);
573 spell_menu = g_slist_reverse(spell_menu);
574 for (items = spell_menu;
575 items; items = items->next) {
576 menuitem = GTK_MENU_ITEM(items->data);
577 gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), GTK_WIDGET(menuitem));
578 gtk_widget_show(GTK_WIDGET(menuitem));
580 g_slist_free(spell_menu);
582 g_signal_connect(G_OBJECT(menu), "deactivate",
583 G_CALLBACK(destroy_menu),
586 g_signal_connect(G_OBJECT(menu),
588 G_CALLBACK(aspell_key_pressed), gtkaspell);
592 /* Checker creation */
593 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
596 GtkAspeller *gtkaspeller = NULL;
600 g_return_val_if_fail(gtkaspellcheckers, NULL);
602 g_return_val_if_fail(dictionary, NULL);
604 if (dictionary->fullname == NULL)
605 gtkaspell_checkers_error_message(
606 g_strdup(_("No dictionary selected.")));
608 g_return_val_if_fail(dictionary->fullname, NULL);
610 if (dictionary->dictname == NULL) {
613 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
616 dictionary->dictname = dictionary->fullname;
618 dictionary->dictname = tmp + 1;
621 dict = dictionary_dup(dictionary);
623 tmp = g_new0(GtkAspeller, 1);
624 tmp->dictionary = dict;
626 exist = g_slist_find_custom(gtkaspellcheckers->checkers, tmp,
631 if ((gtkaspeller = gtkaspeller_real_new(dict)) != NULL) {
632 gtkaspellcheckers->checkers = g_slist_append(
633 gtkaspellcheckers->checkers,
636 debug_print("Aspell: Created a new gtkaspeller %0x\n",
639 dictionary_delete(dict);
641 debug_print("Aspell: Could not create spell checker.\n");
644 debug_print("Aspell: number of existing checkers %d\n",
645 g_slist_length(gtkaspellcheckers->checkers));
650 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
652 GtkAspeller *gtkaspeller;
653 AspellConfig *config;
654 AspellCanHaveError *ret;
656 g_return_val_if_fail(gtkaspellcheckers, NULL);
657 g_return_val_if_fail(dict, NULL);
659 gtkaspeller = g_new(GtkAspeller, 1);
661 gtkaspeller->dictionary = dict;
662 gtkaspeller->sug_mode = ASPELL_FASTMODE;
664 config = new_aspell_config();
666 if (!set_dictionary(config, dict))
669 ret = new_aspell_speller(config);
670 delete_aspell_config(config);
672 if (aspell_error_number(ret) != 0) {
673 gtkaspellcheckers->error_message
674 = g_strdup(aspell_error_message(ret));
676 delete_aspell_can_have_error(ret);
681 gtkaspeller->checker = to_aspell_speller(ret);
682 gtkaspeller->config = aspell_speller_config(gtkaspeller->checker);
687 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
689 g_return_val_if_fail(gtkaspellcheckers, NULL);
691 gtkaspellcheckers->checkers =
692 g_slist_remove(gtkaspellcheckers->checkers,
695 debug_print("Aspell: Deleting gtkaspeller %0x.\n",
698 gtkaspeller_real_delete(gtkaspeller);
700 debug_print("Aspell: number of existing checkers %d\n",
701 g_slist_length(gtkaspellcheckers->checkers));
706 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
708 g_return_val_if_fail(gtkaspeller, NULL);
709 g_return_val_if_fail(gtkaspeller->checker, NULL);
711 aspell_speller_save_all_word_lists(gtkaspeller->checker);
713 delete_aspell_speller(gtkaspeller->checker);
715 dictionary_delete(gtkaspeller->dictionary);
717 debug_print("Aspell: gtkaspeller %0x deleted.\n",
725 /*****************************************************************************/
726 /* Checker configuration */
728 static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
730 gchar *language = NULL;
731 gchar *jargon = NULL;
735 g_return_val_if_fail(config, FALSE);
736 g_return_val_if_fail(dict, FALSE);
738 strncpy(buf, dict->fullname, BUFSIZE-1);
739 buf[BUFSIZE-1] = 0x00;
741 buf[dict->dictname - dict->fullname] = 0x00;
743 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
744 debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
746 strncpy(buf, dict->dictname, BUFSIZE-1);
749 if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
754 if ((jargon = strchr(language, '-')) != NULL)
757 if (size != NULL && jargon == size)
760 debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
761 language, jargon ? jargon : "",
765 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
767 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
769 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
770 if (dict->encoding) {
773 aspell_enc = convert_to_aspell_encoding (dict->encoding);
774 aspell_config_replace(config, "encoding",
775 (const char *) aspell_enc);
778 RETURN_FALSE_IF_CONFIG_ERROR();
784 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
787 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
788 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
790 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
793 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
798 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
799 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
801 dict = gtkaspell->gtkaspeller->dictionary;
802 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
807 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
808 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
812 themode = (char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
813 themode = g_strdup(themode);
815 set_real_sug_mode(gtkaspell, themode);
817 if (gtkaspell->menu_changed_cb)
818 gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
821 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
824 gint mode = ASPELL_FASTMODE;
825 g_return_if_fail(gtkaspell);
826 g_return_if_fail(gtkaspell->gtkaspeller);
827 g_return_if_fail(themode);
829 if (!strcmp(themode,_("Normal Mode")))
830 mode = ASPELL_NORMALMODE;
831 else if (!strcmp( themode,_("Bad Spellers Mode")))
832 mode = ASPELL_BADSPELLERMODE;
834 result = gtkaspell_set_sug_mode(gtkaspell, mode);
837 debug_print("Aspell: error while changing suggestion mode:%s\n",
838 gtkaspellcheckers->error_message);
839 gtkaspell_checkers_reset_error();
843 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
844 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
846 AspellConfig *config;
848 g_return_val_if_fail(gtkaspell, FALSE);
849 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
850 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
852 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
853 (guint) gtkaspell->gtkaspeller, themode);
855 config = gtkaspell->gtkaspeller->config;
858 case ASPELL_FASTMODE:
859 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
861 case ASPELL_NORMALMODE:
862 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
864 case ASPELL_BADSPELLERMODE:
865 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
869 gtkaspellcheckers->error_message =
870 g_strdup(_("Unknown suggestion mode."));
874 gtkaspell->gtkaspeller->sug_mode = themode;
875 gtkaspell->default_sug_mode = themode;
880 /* misspelled_suggest() - Create a suggestion list for word */
881 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
883 const guchar *newword;
885 const AspellWordList *suggestions;
886 AspellStringEnumeration *elements;
888 g_return_val_if_fail(word, NULL);
890 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
891 free_suggestions_list(gtkaspell);
893 suggestions = aspell_speller_suggest(
894 gtkaspell->gtkaspeller->checker,
895 (const char *)word, -1);
896 elements = aspell_word_list_elements(suggestions);
897 list = g_list_append(list, g_strdup(word));
899 while ((newword = aspell_string_enumeration_next(elements)) != NULL)
900 list = g_list_append(list, g_strdup(newword));
902 gtkaspell->max_sug = g_list_length(list) - 1;
903 gtkaspell->suggestions_list = list;
908 free_suggestions_list(gtkaspell);
913 /* misspelled_test() - Just test if word is correctly spelled */
914 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
916 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
921 static gboolean iswordsep(unsigned char c)
923 return (isspace(c) || ispunct(c)) && c != '\'';
926 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
928 GtkTextView *view = gtkaspell->gtktext;
929 GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
930 GtkTextIter start, end;
934 gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
935 gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
937 utf8chars = gtk_text_iter_get_text(&start, &end);
938 if (is_ascii_str(utf8chars)) {
939 a = utf8chars ? utf8chars[0] : '\0' ;
941 gchar *tr = conv_iconv_strdup(utf8chars, CS_UTF_8,
942 gtkaspell->gtkaspeller->dictionary->encoding);
953 /* get_word_from_pos () - return the word pointed to. */
954 /* Handles correctly the quotes. */
955 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
956 unsigned char* buf, gint buflen,
957 gint *pstart, gint *pend)
960 /* TODO : when correcting a word into quotes, change the color of */
961 /* the quotes too, as may be they were highlighted before. To do */
962 /* this, we can use two others pointers that points to the whole */
963 /* word including quotes. */
969 GtkTextView *gtktext;
971 gtktext = gtkaspell->gtktext;
972 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
975 /* The apostrophe character is somtimes used for quotes
976 * So include it in the word only if it is not surrounded
977 * by other characters.
980 for (start = pos; start >= 0; --start) {
981 c = get_text_index_whar(gtkaspell, start);
984 if (isspace(get_text_index_whar(gtkaspell,
986 || ispunct(get_text_index_whar(gtkaspell,
988 || isdigit(get_text_index_whar(gtkaspell,
990 /* start_quote = TRUE; */
995 /* start_quote = TRUE; */
999 else if (isspace(c) || ispunct(c) || isdigit(c))
1005 for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
1006 c = get_text_index_whar(gtkaspell, end);
1008 if (end < get_textview_buffer_charcount(gtktext)) {
1009 if (isspace(get_text_index_whar(gtkaspell,
1011 || ispunct(get_text_index_whar(gtkaspell,
1013 || isdigit(get_text_index_whar(gtkaspell,
1015 /* end_quote = TRUE; */
1020 /* end_quote = TRUE; */
1024 else if (isspace(c) || ispunct(c) || isdigit(c))
1034 if (end - start < buflen) {
1035 GtkTextIter iterstart, iterend;
1037 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtktext);
1038 gtk_text_buffer_get_iter_at_offset(buffer, &iterstart, start);
1039 gtk_text_buffer_get_iter_at_offset(buffer, &iterend, end);
1040 tmp = gtk_text_buffer_get_text(buffer, &iterstart, &iterend, FALSE);
1041 conv = conv_iconv_strdup(tmp, CS_UTF_8,
1042 gtkaspell->gtkaspeller->dictionary->encoding);
1044 strncpy(buf, conv, buflen-1);
1054 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
1057 unsigned char buf[GTKASPELLWORDSIZE];
1058 GtkTextView *gtktext;
1060 g_return_val_if_fail(from_pos >= 0, FALSE);
1062 gtktext = gtkaspell->gtktext;
1064 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
1068 if (misspelled_test(gtkaspell, buf)) {
1069 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
1070 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
1071 gtkaspell->start_pos = start;
1072 gtkaspell->end_pos = end;
1073 free_suggestions_list(gtkaspell);
1075 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
1078 change_color(gtkaspell, start, end, buf, NULL);
1083 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1089 gboolean misspelled;
1092 maxpos = gtkaspell->end_check_pos;
1100 pos = get_textview_buffer_offset(gtkaspell->gtktext);
1101 gtkaspell->orig_pos = pos;
1102 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1103 pos > minpos && pos <= maxpos)
1105 while (!(misspelled = check_at(gtkaspell, pos)) &&
1106 pos > minpos && pos <= maxpos) {
1108 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1109 pos > minpos && pos <= maxpos)
1112 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1113 pos > minpos && pos <= maxpos)
1119 misspelled_suggest(gtkaspell, gtkaspell->theword);
1122 gtkaspell->orig_pos = gtkaspell->end_pos;
1124 set_textview_buffer_offset(gtkaspell->gtktext,
1125 gtkaspell->end_pos);
1126 /* scroll line to window center */
1127 gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
1128 gtk_text_buffer_get_insert(
1129 gtk_text_view_get_buffer(gtkaspell->gtktext)),
1130 0.0, TRUE, 0.0, 0.5);
1131 /* let textview recalculate coordinates (set_menu_pos) */
1132 while (gtk_events_pending ())
1133 gtk_main_iteration ();
1135 list = make_sug_menu(gtkaspell);
1136 menu = gtk_menu_new();
1137 for (cur = list; cur; cur = cur->next)
1138 gtk_menu_append(menu, GTK_WIDGET(cur->data));
1140 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
1141 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1142 g_signal_connect(G_OBJECT(menu), "deactivate",
1143 G_CALLBACK(destroy_menu),
1145 g_signal_connect(G_OBJECT(menu),
1147 G_CALLBACK(aspell_key_pressed), gtkaspell);
1151 reset_theword_data(gtkaspell);
1153 alertpanel_notice(_("No misspelled word found."));
1154 set_textview_buffer_offset(gtkaspell->gtktext,
1155 gtkaspell->orig_pos);
1161 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1163 gtkaspell->continue_check = NULL;
1164 gtkaspell->end_check_pos =
1165 get_textview_buffer_charcount(gtkaspell->gtktext);
1166 check_next_prev(gtkaspell, FALSE);
1169 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1172 gtkaspell->continue_check = NULL;
1173 gtkaspell->end_check_pos =
1174 get_textview_buffer_charcount(gtkaspell->gtktext);
1175 check_next_prev(gtkaspell, TRUE);
1178 void gtkaspell_check_all(GtkAspell *gtkaspell)
1180 GtkTextView *gtktext;
1182 GtkTextBuffer *buffer;
1183 GtkTextIter startiter, enditer;
1185 g_return_if_fail(gtkaspell);
1186 g_return_if_fail(gtkaspell->gtktext);
1188 gtktext = gtkaspell->gtktext;
1189 buffer = gtk_text_view_get_buffer(gtktext);
1190 gtk_text_buffer_get_selection_bounds(buffer, &startiter, &enditer);
1191 start = gtk_text_iter_get_offset(&startiter);
1192 end = gtk_text_iter_get_offset(&enditer);
1196 end = gtk_text_buffer_get_char_count(buffer);
1197 } else if (start > end) {
1205 set_textview_buffer_offset(gtktext, start);
1207 gtkaspell->continue_check = continue_check;
1208 gtkaspell->end_check_pos = end;
1210 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1213 static void continue_check(gpointer *data)
1215 GtkAspell *gtkaspell = (GtkAspell *) data;
1216 gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
1217 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1218 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1220 gtkaspell->continue_check = NULL;
1223 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1228 GtkTextView *gtktext;
1230 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1232 gtktext = gtkaspell->gtktext;
1234 len = get_textview_buffer_charcount(gtktext);
1236 origpos = get_textview_buffer_offset(gtktext);
1240 iswordsep(get_text_index_whar(gtkaspell, pos)))
1243 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1246 check_at(gtkaspell, pos - 1);
1248 set_textview_buffer_offset(gtktext, origpos);
1251 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1253 unsigned char *newword;
1254 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1256 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1259 if (strcmp(newword, gtkaspell->theword)) {
1260 replace_real_word(gtkaspell, newword);
1262 if ((e->type == GDK_KEY_PRESS &&
1263 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK)) {
1264 aspell_speller_store_replacement(
1265 gtkaspell->gtkaspeller->checker,
1266 gtkaspell->theword, -1,
1269 gtkaspell->replace_entry = NULL;
1274 if (w && GTK_IS_DIALOG(w)) {
1275 gtk_widget_destroy(w);
1278 set_point_continue(gtkaspell);
1282 static void replace_word_cb(GtkWidget *w, gpointer data)
1284 unsigned char *newword;
1285 GtkAspell *gtkaspell = (GtkAspell *) data;
1286 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1288 newword = (unsigned char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
1289 newword = g_strdup(newword);
1291 replace_real_word(gtkaspell, newword);
1293 if ((e->type == GDK_KEY_PRESS &&
1294 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK) ||
1295 (e->type == GDK_BUTTON_RELEASE &&
1296 ((GdkEventButton *) e)->state & GDK_CONTROL_MASK)) {
1297 aspell_speller_store_replacement(
1298 gtkaspell->gtkaspeller->checker,
1299 gtkaspell->theword, -1,
1303 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1305 set_point_continue(gtkaspell);
1309 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1311 int oldlen, newlen, wordlen;
1314 GtkTextView *gtktext;
1315 GtkTextBuffer *textbuf;
1316 GtkTextIter startiter, enditer;
1318 if (!newword) return;
1320 gtktext = gtkaspell->gtktext;
1321 textbuf = gtk_text_view_get_buffer(gtktext);
1323 origpos = gtkaspell->orig_pos;
1325 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1326 wordlen = strlen(gtkaspell->theword);
1328 newlen = strlen(newword); /* FIXME: multybyte characters? */
1330 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1331 G_CALLBACK(entry_insert_cb),
1333 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1334 G_CALLBACK(entry_delete_cb),
1337 gtk_text_buffer_get_iter_at_offset(textbuf, &startiter,
1338 gtkaspell->start_pos);
1339 gtk_text_buffer_get_iter_at_offset(textbuf, &enditer,
1340 gtkaspell->end_pos);
1341 g_signal_emit_by_name(G_OBJECT(textbuf), "delete-range",
1342 &startiter, &enditer, gtkaspell);
1343 g_signal_emit_by_name(G_OBJECT(textbuf), "insert-text",
1344 &startiter, newword, newlen, gtkaspell);
1346 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1347 G_CALLBACK(entry_insert_cb),
1349 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1350 G_CALLBACK(entry_delete_cb),
1353 /* Put the point and the position where we clicked with the mouse
1354 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1355 * to let it update correctly the word insertion and then the
1356 * point & position position. If not, SEGV after the first replacement
1357 * If the new word ends before point, put the point at its end.
1360 if (origpos - gtkaspell->start_pos < oldlen &&
1361 origpos - gtkaspell->start_pos >= 0) {
1362 /* Original point was in the word.
1363 * Let it there unless point is going to be outside of the word
1365 if (origpos - gtkaspell->start_pos >= newlen) {
1366 pos = gtkaspell->start_pos + newlen;
1369 else if (origpos >= gtkaspell->end_pos) {
1370 /* move the position according to the change of length */
1371 pos = origpos + newlen - oldlen;
1374 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1376 if (get_textview_buffer_charcount(gtktext) < pos)
1377 pos = get_textview_buffer_charcount(gtktext);
1378 gtkaspell->orig_pos = pos;
1380 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1383 /* Accept this word for this session */
1384 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1387 GtkTextView *gtktext;
1388 GtkAspell *gtkaspell = (GtkAspell *) data;
1389 gtktext = gtkaspell->gtktext;
1391 pos = get_textview_buffer_offset(gtktext);
1393 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1395 strlen(gtkaspell->theword));
1397 check_at(gtkaspell, gtkaspell->start_pos);
1399 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1401 set_point_continue(gtkaspell);
1404 /* add_word_to_personal_cb() - add word to personal dict. */
1405 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1407 GtkAspell *gtkaspell = (GtkAspell *) data;
1409 aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
1411 strlen(gtkaspell->theword));
1413 check_at(gtkaspell, gtkaspell->start_pos);
1415 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1416 set_point_continue(gtkaspell);
1419 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1421 GtkAspell *gtkaspell = (GtkAspell *) data;
1424 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1426 use_alternate_dict(gtkaspell);
1427 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1429 if (!gtkaspell->continue_check) {
1431 gtkaspell->misspelled = misspelled;
1433 if (gtkaspell->misspelled) {
1436 misspelled_suggest(gtkaspell, gtkaspell->theword);
1438 set_textview_buffer_offset(gtkaspell->gtktext,
1439 gtkaspell->end_pos);
1441 list = make_sug_menu(gtkaspell);
1442 menu = gtk_menu_new();
1443 for (cur = list; cur; cur = cur->next)
1444 gtk_menu_append(menu, GTK_WIDGET(cur->data));
1446 gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
1447 set_menu_pos, gtkaspell, 0,
1449 g_signal_connect(G_OBJECT(menu), "deactivate",
1450 G_CALLBACK(destroy_menu),
1452 g_signal_connect(G_OBJECT(menu),
1454 G_CALLBACK(aspell_key_pressed), gtkaspell);
1458 gtkaspell->orig_pos = gtkaspell->start_pos;
1460 set_point_continue(gtkaspell);
1463 static gboolean replace_key_pressed(GtkWidget *widget,
1465 GtkAspell *gtkaspell)
1467 if (event && event->keyval == GDK_Escape) {
1468 gtk_widget_destroy(widget);
1470 } else if (event && event->keyval == GDK_Return) {
1471 replace_with_supplied_word_cb(widget, gtkaspell);
1477 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1479 static PangoFontDescription *font_desc;
1486 GtkWidget *ok_button;
1487 GtkWidget *cancel_button;
1488 GtkWidget *confirm_area;
1492 GtkAspell *gtkaspell = (GtkAspell *) data;
1494 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1496 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1498 dialog = gtk_dialog_new();
1500 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
1501 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1502 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1503 gtk_widget_set_uposition(dialog, xx, yy);
1505 g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
1506 G_CALLBACK(gtk_widget_destroy),
1509 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
1510 hbox = gtk_hbox_new (FALSE, 12);
1511 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1512 gtk_widget_show (hbox);
1513 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
1516 thelabel = g_strdup_printf(_("<span weight=\"bold\" "
1517 "size=\"larger\">Replace \"%s\" with: </span>"),
1518 gtkaspell->theword);
1519 /* for title label */
1520 w_hbox = gtk_hbox_new(FALSE, 0);
1522 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
1523 GTK_ICON_SIZE_DIALOG);
1524 gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
1525 gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
1527 vbox = gtk_vbox_new (FALSE, 12);
1528 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1529 gtk_widget_show (vbox);
1531 label = gtk_label_new(thelabel);
1532 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1533 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1534 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1535 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1536 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1540 size = pango_font_description_get_size
1541 (label->style->font_desc);
1542 font_desc = pango_font_description_new();
1543 pango_font_description_set_weight
1544 (font_desc, PANGO_WEIGHT_BOLD);
1545 pango_font_description_set_size
1546 (font_desc, size * PANGO_SCALE_LARGE);
1549 gtk_widget_modify_font(label, font_desc);
1552 entry = gtk_entry_new();
1553 gtkaspell->replace_entry = entry;
1554 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1555 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1556 g_signal_connect(G_OBJECT(dialog),
1558 G_CALLBACK(replace_key_pressed), gtkaspell);
1559 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
1561 label = gtk_label_new(_("Holding down Control key while pressing "
1562 "Enter\nwill learn from mistake.\n"));
1563 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1564 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1565 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1566 gtk_widget_show(label);
1568 hbox = gtk_hbox_new(TRUE, 0);
1570 gtkut_stock_button_set_create(&confirm_area,
1571 &cancel_button, GTK_STOCK_CANCEL,
1572 &ok_button, GTK_STOCK_OK,
1575 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1576 confirm_area, FALSE, FALSE, 0);
1577 gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
1579 g_signal_connect(G_OBJECT(ok_button), "clicked",
1580 G_CALLBACK(replace_with_supplied_word_cb),
1582 g_signal_connect_swapped(G_OBJECT(ok_button), "clicked",
1583 G_CALLBACK(gtk_widget_destroy),
1586 g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
1587 G_CALLBACK(gtk_widget_destroy),
1590 gtk_widget_grab_focus(entry);
1592 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1594 gtk_widget_show_all(dialog);
1597 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1599 GtkTextView *gtktext;
1600 GtkTextBuffer *buffer;
1601 GtkTextIter startiter, enditer;
1603 gtktext = gtkaspell->gtktext;
1605 buffer = gtk_text_view_get_buffer(gtktext);
1606 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
1607 gtk_text_buffer_get_iter_at_offset(buffer, &enditer,
1608 get_textview_buffer_charcount(gtktext)-1);
1609 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
1610 &startiter, &enditer);
1613 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1615 GtkAspell *gtkaspell = (GtkAspell *) data;
1617 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1619 if (!gtkaspell->check_while_typing)
1620 gtkaspell_uncheck_all(gtkaspell);
1621 if (gtkaspell->menu_changed_cb)
1622 gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
1625 static GSList *create_empty_dictionary_list(void)
1627 GSList *list = NULL;
1630 dict = g_new0(Dictionary, 1);
1631 dict->fullname = g_strdup(_("None"));
1632 dict->dictname = dict->fullname;
1633 dict->encoding = NULL;
1635 return g_slist_append(list, dict);
1638 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1639 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1643 AspellConfig *config;
1644 AspellDictInfoList *dlist;
1645 AspellDictInfoEnumeration *dels;
1646 const AspellDictInfo *entry;
1648 if (!gtkaspellcheckers)
1649 gtkaspell_checkers_init();
1651 if (gtkaspellcheckers->dictionary_list && !refresh)
1652 return gtkaspellcheckers->dictionary_list;
1654 gtkaspell_free_dictionary_list(
1655 gtkaspellcheckers->dictionary_list);
1658 config = new_aspell_config();
1660 aspell_config_replace(config, "dict-dir", aspell_path);
1661 if (aspell_config_error_number(config) != 0) {
1662 gtkaspellcheckers->error_message = g_strdup(
1663 aspell_config_error_message(config));
1664 gtkaspellcheckers->dictionary_list =
1665 create_empty_dictionary_list();
1667 return gtkaspellcheckers->dictionary_list;
1670 dlist = get_aspell_dict_info_list(config);
1671 delete_aspell_config(config);
1673 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1674 dels = aspell_dict_info_list_elements(dlist);
1675 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1677 dict = g_new0(Dictionary, 1);
1678 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1680 dict->dictname = dict->fullname + strlen(aspell_path);
1681 dict->encoding = g_strdup(entry->code);
1683 if (g_slist_find_custom(list, dict,
1684 (GCompareFunc) compare_dict) != NULL) {
1685 dictionary_delete(dict);
1689 debug_print("Aspell: found dictionary %s %s %s\n", dict->fullname,
1690 dict->dictname, dict->encoding);
1691 list = g_slist_insert_sorted(list, dict,
1692 (GCompareFunc) compare_dict);
1695 delete_aspell_dict_info_enumeration(dels);
1699 debug_print("Aspell: error when searching for dictionaries: "
1700 "No dictionary found.\n");
1701 list = create_empty_dictionary_list();
1704 gtkaspellcheckers->dictionary_list = list;
1709 void gtkaspell_free_dictionary_list(GSList *list)
1713 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1715 dict = (Dictionary *) walk->data;
1716 dictionary_delete(dict);
1722 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1724 GSList *dict_list, *tmp;
1729 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1730 g_return_val_if_fail(dict_list, NULL);
1732 menu = gtk_menu_new();
1734 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1735 dict = (Dictionary *) tmp->data;
1736 item = gtk_menu_item_new_with_label(dict->dictname);
1737 g_object_set_data(G_OBJECT(item), "dict_name",
1740 gtk_menu_append(GTK_MENU(menu), item);
1741 gtk_widget_show(item);
1744 gtk_widget_show(menu);
1749 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1751 GtkWidget *menuitem;
1752 gchar *dict_fullname;
1755 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1757 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1758 dict_fullname = (gchar *) g_object_get_data(G_OBJECT(menuitem),
1760 g_return_val_if_fail(dict_fullname, NULL);
1762 label = g_strdup(dict_fullname);
1768 gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
1769 const gchar *dictionary)
1774 g_return_val_if_fail(menu != NULL, 0);
1775 g_return_val_if_fail(dictionary != NULL, 0);
1776 g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
1779 for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
1780 GTK_OPTION_MENU(menu)))->children;
1781 cur != NULL; cur = cur->next) {
1782 GtkWidget *menuitem;
1785 menuitem = GTK_WIDGET(cur->data);
1786 dict_name = g_object_get_data(G_OBJECT(menuitem),
1788 if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
1789 gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
1800 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1805 menu = gtk_menu_new();
1806 gtk_widget_show(menu);
1808 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1809 gtk_widget_show(item);
1810 gtk_menu_append(GTK_MENU(menu), item);
1811 g_object_set_data(G_OBJECT(item), "sugmode",
1812 GINT_TO_POINTER(ASPELL_FASTMODE));
1814 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1815 gtk_widget_show(item);
1816 gtk_menu_append(GTK_MENU(menu), item);
1817 g_object_set_data(G_OBJECT(item), "sugmode",
1818 GINT_TO_POINTER(ASPELL_NORMALMODE));
1820 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1821 gtk_widget_show(item);
1822 gtk_menu_append(GTK_MENU(menu), item);
1823 g_object_set_data(G_OBJECT(item), "sugmode",
1824 GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1829 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1831 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1833 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1834 sugmode == ASPELL_NORMALMODE ||
1835 sugmode == ASPELL_BADSPELLERMODE);
1837 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1840 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1845 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1847 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1849 sugmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
1855 static void use_alternate_dict(GtkAspell *gtkaspell)
1859 tmp = gtkaspell->gtkaspeller;
1860 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1861 gtkaspell->alternate_speller = tmp;
1864 static void destroy_menu(GtkWidget *widget,
1865 gpointer user_data) {
1867 GtkAspell *gtkaspell = (GtkAspell *)user_data;
1869 if (gtkaspell->accel_group) {
1870 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1871 gtkaspell->accel_group);
1872 gtkaspell->accel_group = NULL;
1876 static gboolean aspell_key_pressed(GtkWidget *widget,
1878 GtkAspell *gtkaspell)
1880 if (event && (isascii(event->keyval) || event->keyval == GDK_Return)) {
1881 gtk_accel_groups_activate(
1882 G_OBJECT(gtkaspell->parent_window),
1883 event->keyval, event->state);
1884 } else if (event && event->keyval == GDK_Escape) {
1885 destroy_menu(NULL, gtkaspell);
1890 /* make_sug_menu() - Add menus to accept this word for this session
1891 * and to add it to personal dictionary
1893 static GSList *make_sug_menu(GtkAspell *gtkaspell)
1896 unsigned char *caption;
1897 GtkTextView *gtktext;
1898 GtkAccelGroup *accel;
1899 GList *l = gtkaspell->suggestions_list;
1901 GSList *list = NULL;
1902 gtktext = gtkaspell->gtktext;
1904 accel = gtk_accel_group_new();
1906 if (gtkaspell->accel_group) {
1907 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1908 gtkaspell->accel_group);
1909 gtkaspell->accel_group = NULL;
1912 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
1913 conv_get_locale_charset_str(),
1915 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1917 gtkaspell->gtkaspeller->dictionary->dictname);
1918 item = gtk_menu_item_new_with_label(caption);
1920 gtk_widget_show(item);
1921 list = g_slist_append(list, item);
1922 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1925 item = gtk_menu_item_new();
1926 gtk_widget_show(item);
1927 list = g_slist_append(list, item);
1929 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1930 gtk_widget_show(item);
1931 list = g_slist_append(list, item);
1932 g_signal_connect(G_OBJECT(item), "activate",
1933 G_CALLBACK(add_word_to_session_cb),
1935 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1937 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1939 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1940 gtk_widget_show(item);
1941 list = g_slist_append(list, item);
1942 g_signal_connect(G_OBJECT(item), "activate",
1943 G_CALLBACK(add_word_to_personal_cb),
1945 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1947 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1949 item = gtk_menu_item_new_with_label(_("Replace with..."));
1950 gtk_widget_show(item);
1951 list = g_slist_append(list, item);
1952 g_signal_connect(G_OBJECT(item), "activate",
1953 G_CALLBACK(replace_with_create_dialog_cb),
1955 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1956 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1957 gtk_widget_add_accelerator(item, "activate", accel, GDK_R,
1961 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1962 caption = g_strdup_printf(_("Check with %s"),
1963 gtkaspell->alternate_speller->dictionary->dictname);
1964 item = gtk_menu_item_new_with_label(caption);
1966 gtk_widget_show(item);
1967 list = g_slist_append(list, item);
1968 g_signal_connect(G_OBJECT(item), "activate",
1969 G_CALLBACK(check_with_alternate_cb),
1971 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1972 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1973 gtk_widget_add_accelerator(item, "activate", accel, GDK_X,
1978 item = gtk_menu_item_new();
1979 gtk_widget_show(item);
1980 list = g_slist_append(list, item);
1984 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1985 gtk_widget_show(item);
1986 list = g_slist_append(list, item);
1988 GtkWidget *curmenu = NULL;
1992 if (count == MENUCOUNT) {
1995 item = gtk_menu_item_new_with_label(_("More..."));
1996 gtk_widget_show(item);
1998 gtk_menu_append(GTK_MENU(curmenu), item);
2000 list = g_slist_append(list, item);
2002 curmenu = gtk_menu_new();
2003 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2007 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
2008 conv_get_locale_charset_str(),
2010 item = gtk_menu_item_new_with_label(utf8buf);
2012 gtk_widget_show(item);
2013 if (curmenu == NULL) {
2014 list = g_slist_append(list, item);
2016 gtk_menu_append(GTK_MENU(curmenu), item);
2018 g_signal_connect(G_OBJECT(item), "activate",
2019 G_CALLBACK(replace_word_cb),
2022 if (curmenu == NULL && count < MENUCOUNT) {
2023 gtk_widget_add_accelerator(item, "activate",
2028 gtk_widget_add_accelerator(item, "activate",
2037 } while ((l = l->next) != NULL);
2040 gtk_window_add_accel_group
2041 (GTK_WINDOW(gtkaspell->parent_window),
2043 gtkaspell->accel_group = accel;
2048 static GSList *populate_submenu(GtkAspell *gtkaspell)
2050 GtkWidget *item, *submenu;
2052 GtkAspeller *gtkaspeller = NULL;
2053 GSList *list = NULL;
2058 gtkaspeller = gtkaspell->gtkaspeller;
2059 dictname = g_strdup_printf(_("Dictionary: %s"),
2060 gtkaspeller->dictionary->dictname);
2061 item = gtk_menu_item_new_with_label(dictname);
2062 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
2064 gtk_widget_show(item);
2065 list = g_slist_append(list, item);
2067 item = gtk_menu_item_new();
2068 gtk_widget_show(item);
2069 list = g_slist_append(list, item);
2071 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
2072 dictname = g_strdup_printf(_("Use alternate (%s)"),
2073 gtkaspell->alternate_speller->dictionary->dictname);
2074 item = gtk_menu_item_new_with_label(dictname);
2076 g_signal_connect(G_OBJECT(item), "activate",
2077 G_CALLBACK(switch_to_alternate_cb),
2079 gtk_widget_show(item);
2080 list = g_slist_append(list, item);
2083 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
2084 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
2085 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
2086 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
2088 g_signal_connect(G_OBJECT(item), "activate",
2089 G_CALLBACK(set_sug_mode_cb),
2091 gtk_widget_show(item);
2092 list = g_slist_append(list, item);
2094 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
2095 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
2096 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2097 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2099 g_signal_connect(G_OBJECT(item), "activate",
2100 G_CALLBACK(set_sug_mode_cb),
2102 gtk_widget_show(item);
2103 list = g_slist_append(list, item);
2105 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
2106 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
2107 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2108 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2110 g_signal_connect(G_OBJECT(item), "activate",
2111 G_CALLBACK(set_sug_mode_cb),
2113 gtk_widget_show(item);
2114 list = g_slist_append(list, item);
2116 item = gtk_menu_item_new();
2117 gtk_widget_show(item);
2118 list = g_slist_append(list, item);
2120 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
2121 if (gtkaspell->check_while_typing)
2122 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2124 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2125 g_signal_connect(G_OBJECT(item), "activate",
2126 G_CALLBACK(toggle_check_while_typing_cb),
2128 gtk_widget_show(item);
2129 list = g_slist_append(list, item);
2131 item = gtk_menu_item_new();
2132 gtk_widget_show(item);
2133 list = g_slist_append(list, item);
2135 submenu = gtk_menu_new();
2136 item = gtk_menu_item_new_with_label(_("Change dictionary"));
2137 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
2138 gtk_widget_show(item);
2139 list = g_slist_append(list, item);
2142 if (gtkaspellcheckers->dictionary_list == NULL)
2143 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
2145 GtkWidget * curmenu = submenu;
2149 tmp = gtkaspellcheckers->dictionary_list;
2151 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
2152 tmp = g_slist_next(tmp)) {
2153 if (count == MENUCOUNT) {
2156 newmenu = gtk_menu_new();
2157 item = gtk_menu_item_new_with_label(_("More..."));
2158 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2161 gtk_menu_append(GTK_MENU(curmenu), item);
2162 gtk_widget_show(item);
2166 dict = (Dictionary *) tmp->data;
2167 item = gtk_check_menu_item_new_with_label(dict->dictname);
2168 g_object_set_data(G_OBJECT(item), "dict_name",
2170 if (strcmp2(dict->fullname,
2171 gtkaspell->gtkaspeller->dictionary->fullname))
2172 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2174 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2175 gtk_widget_set_sensitive(GTK_WIDGET(item),
2178 g_signal_connect(G_OBJECT(item), "activate",
2179 G_CALLBACK(change_dict_cb),
2181 gtk_widget_show(item);
2182 gtk_menu_append(GTK_MENU(curmenu), item);
2190 GSList *gtkaspell_make_config_menu(GtkAspell *gtkaspell)
2192 return populate_submenu(gtkaspell);
2195 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
2196 gboolean *push_in, gpointer data)
2198 GtkAspell *gtkaspell = (GtkAspell *) data;
2199 gint xx = 0, yy = 0;
2202 GtkTextView *text = GTK_TEXT_VIEW(gtkaspell->gtktext);
2203 GtkTextBuffer *textbuf;
2208 textbuf = gtk_text_view_get_buffer(gtkaspell->gtktext);
2209 gtk_text_buffer_get_iter_at_mark(textbuf, &iter,
2210 gtk_text_buffer_get_insert(textbuf));
2211 gtk_text_view_get_iter_location(gtkaspell->gtktext, &iter, &rect);
2212 gtk_text_view_buffer_to_window_coords(text, GTK_TEXT_WINDOW_TEXT,
2216 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2218 sx = gdk_screen_width();
2219 sy = gdk_screen_height();
2221 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2227 gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
2229 *y = rect.y + rect.height + yy;
2235 gdk_string_height(gtk_style_get_font(
2236 GTK_WIDGET(text)->style),
2237 gtkaspell->theword);
2240 /* Menu call backs */
2242 gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
2245 GtkAspeller *gtkaspeller;
2248 g_return_val_if_fail(gtkaspell, FALSE);
2249 g_return_val_if_fail(dictionary, FALSE);
2251 sug_mode = gtkaspell->default_sug_mode;
2253 dict = g_new0(Dictionary, 1);
2254 dict->fullname = g_strdup(dictionary);
2255 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2257 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2258 dict == gtkaspell->alternate_speller->dictionary) {
2259 use_alternate_dict(gtkaspell);
2260 dictionary_delete(dict);
2261 gtkaspell->alternate_speller->dictionary = NULL;
2265 gtkaspeller = gtkaspeller_new(dict);
2269 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2270 gtkaspellcheckers->error_message);
2272 alertpanel_warning(message);
2275 if (gtkaspell->use_alternate) {
2276 if (gtkaspell->alternate_speller)
2277 gtkaspeller_delete(gtkaspell->alternate_speller);
2278 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2280 gtkaspeller_delete(gtkaspell->gtkaspeller);
2282 gtkaspell->gtkaspeller = gtkaspeller;
2283 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2286 dictionary_delete(dict);
2291 /* change_dict_cb() - Menu callback : change dict */
2292 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2296 fullname = (gchar *) g_object_get_data(G_OBJECT(w), "dict_name");
2298 if (!strcmp2(fullname, _("None")))
2301 gtkaspell_change_dict(gtkaspell, fullname);
2302 if (gtkaspell->recheck_when_changing_dict) {
2303 gtkaspell_highlight_all(gtkaspell);
2305 if (gtkaspell->menu_changed_cb)
2306 gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
2309 static void switch_to_alternate_cb(GtkWidget *w,
2312 GtkAspell *gtkaspell = (GtkAspell *) data;
2313 use_alternate_dict(gtkaspell);
2314 if (gtkaspell->menu_changed_cb)
2315 gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
2318 /* Misc. helper functions */
2320 static void set_point_continue(GtkAspell *gtkaspell)
2322 GtkTextView *gtktext;
2324 gtktext = gtkaspell->gtktext;
2326 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
2328 if (gtkaspell->continue_check)
2329 gtkaspell->continue_check((gpointer *) gtkaspell);
2332 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2334 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtkaspell->gtktext);
2335 GdkColor *color = &(gtkaspell->highlight);
2337 /* Shameless copy from Sylpheed's gtkutils.c */
2339 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2341 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2343 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2347 gtk_text_buffer_create_tag(buffer, "misspelled",
2348 "foreground-gdk", color, NULL);
2350 gtk_text_buffer_create_tag(buffer, "misspelled",
2351 "underline", PANGO_UNDERLINE_ERROR, NULL);
2355 static void change_color(GtkAspell * gtkaspell,
2356 gint start, gint end,
2360 GtkTextView *gtktext;
2361 GtkTextBuffer *buffer;
2362 GtkTextIter startiter, enditer;
2367 gtktext = gtkaspell->gtktext;
2369 buffer = gtk_text_view_get_buffer(gtktext);
2370 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, start);
2371 gtk_text_buffer_get_iter_at_offset(buffer, &enditer, end);
2373 gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
2374 &startiter, &enditer);
2376 gtk_text_iter_forward_char(&enditer);
2377 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
2378 &startiter, &enditer);
2382 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2383 * as needed by aspell. Returns an allocated string.
2386 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2388 guchar * aspell_encoding;
2390 if (strstr2(encoding, "ISO-8859-")) {
2391 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2394 if (!strcmp2(encoding, "US-ASCII"))
2395 aspell_encoding = g_strdup("iso8859-1");
2397 aspell_encoding = g_strdup(encoding);
2400 return aspell_encoding;
2403 /* compare_dict () - compare 2 dict names */
2404 static gint compare_dict(Dictionary *a, Dictionary *b)
2406 guint aparts = 0, bparts = 0;
2409 for (i=0; i < strlen(a->dictname); i++)
2410 if (a->dictname[i] == '-')
2412 for (i=0; i < strlen(b->dictname); i++)
2413 if (b->dictname[i] == '-')
2416 if (aparts != bparts)
2417 return (aparts < bparts) ? -1 : +1;
2420 compare = strcmp2(a->dictname, b->dictname);
2422 compare = strcmp2(a->fullname, b->fullname);
2427 static void dictionary_delete(Dictionary *dict)
2429 g_free(dict->fullname);
2430 g_free(dict->encoding);
2434 static Dictionary *dictionary_dup(const Dictionary *dict)
2438 dict2 = g_new(Dictionary, 1);
2440 dict2->fullname = g_strdup(dict->fullname);
2441 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2442 dict2->encoding = g_strdup(dict->encoding);
2447 static void free_suggestions_list(GtkAspell *gtkaspell)
2451 for (list = gtkaspell->suggestions_list; list != NULL;
2455 g_list_free(gtkaspell->suggestions_list);
2457 gtkaspell->max_sug = -1;
2458 gtkaspell->suggestions_list = NULL;
2461 static void reset_theword_data(GtkAspell *gtkaspell)
2463 gtkaspell->start_pos = 0;
2464 gtkaspell->end_pos = 0;
2465 gtkaspell->theword[0] = 0;
2466 gtkaspell->max_sug = -1;
2468 free_suggestions_list(gtkaspell);
2471 static void free_checkers(gpointer elt, gpointer data)
2473 GtkAspeller *gtkaspeller = elt;
2475 g_return_if_fail(gtkaspeller);
2477 gtkaspeller_real_delete(gtkaspeller);
2480 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2482 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2483 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2485 if (a && b && a->fullname && b->fullname &&
2486 strcmp(a->fullname, b->fullname) == 0 &&
2487 a->encoding && b->encoding)
2488 return strcmp(a->encoding, b->encoding);