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 use_alternate;
126 ContCheckFunc continue_check;
128 GtkWidget *config_menu;
129 GtkWidget *popup_config_menu;
131 GtkWidget *replace_entry;
132 GtkWidget *parent_window;
134 gint default_sug_mode;
136 GList *suggestions_list;
138 GtkTextView *gtktext;
140 GtkAccelGroup *accel_group;
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 /* Checker creation */
167 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
168 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
169 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
170 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
172 /* Checker configuration */
173 static gint set_dictionary (AspellConfig *config,
175 static void set_sug_mode_cb (GtkMenuItem *w,
176 GtkAspell *gtkaspell);
177 static void set_real_sug_mode (GtkAspell *gtkaspell,
178 const char *themode);
180 /* Checker actions */
181 static gboolean check_at (GtkAspell *gtkaspell,
183 static gboolean check_next_prev (GtkAspell *gtkaspell,
185 static GList* misspelled_suggest (GtkAspell *gtkaspell,
187 static void add_word_to_session_cb (GtkWidget *w,
189 static void add_word_to_personal_cb (GtkWidget *w,
191 static void replace_with_create_dialog_cb (GtkWidget *w,
193 static void replace_with_supplied_word_cb (GtkWidget *w,
194 GtkAspell *gtkaspell);
195 static void replace_word_cb (GtkWidget *w,
197 static void replace_real_word (GtkAspell *gtkaspell,
199 static void check_with_alternate_cb (GtkWidget *w,
201 static void use_alternate_dict (GtkAspell *gtkaspell);
202 static void toggle_check_while_typing_cb (GtkWidget *w,
206 static void popup_menu (GtkAspell *gtkaspell,
208 static GtkMenu* make_sug_menu (GtkAspell *gtkaspell);
209 static void populate_submenu (GtkAspell *gtkaspell,
211 static GtkMenu* make_config_menu (GtkAspell *gtkaspell);
212 static void set_menu_pos (GtkMenu *menu,
217 /* Other menu callbacks */
218 static gboolean cancel_menu_cb (GtkMenuShell *w,
220 static void change_dict_cb (GtkWidget *w,
221 GtkAspell *gtkaspell);
222 static void switch_to_alternate_cb (GtkWidget *w,
225 /* Misc. helper functions */
226 static void set_point_continue (GtkAspell *gtkaspell);
227 static void continue_check (gpointer *gtkaspell);
228 static gboolean iswordsep (unsigned char c);
229 static guchar get_text_index_whar (GtkAspell *gtkaspell,
231 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
237 static void allocate_color (GtkAspell *gtkaspell,
239 static void change_color (GtkAspell *gtkaspell,
244 static guchar* convert_to_aspell_encoding (const guchar *encoding);
245 static gint compare_dict (Dictionary *a,
247 static void dictionary_delete (Dictionary *dict);
248 static Dictionary * dictionary_dup (const Dictionary *dict);
249 static void free_suggestions_list (GtkAspell *gtkaspell);
250 static void reset_theword_data (GtkAspell *gtkaspell);
251 static void free_checkers (gpointer elt,
253 static gint find_gtkaspeller (gconstpointer aa,
255 /* gtkspellconfig - only one config per session */
256 GtkAspellConfig * gtkaspellconfig;
257 static void destroy_menu(GtkWidget *widget, gpointer user_data);
259 /******************************************************************************/
260 static gint get_textview_buffer_charcount(GtkTextView *view);
262 static gint get_textview_buffer_charcount(GtkTextView *view)
264 GtkTextBuffer *buffer;
266 g_return_val_if_fail(view, 0);
268 buffer = gtk_text_view_get_buffer(view);
269 g_return_val_if_fail(buffer, 0);
271 return gtk_text_buffer_get_char_count(buffer);
273 static gint get_textview_buffer_offset(GtkTextView *view)
275 GtkTextBuffer * buffer;
279 g_return_val_if_fail(view, 0);
281 buffer = gtk_text_view_get_buffer(view);
282 g_return_val_if_fail(buffer, 0);
284 mark = gtk_text_buffer_get_insert(buffer);
285 g_return_val_if_fail(mark, 0);
287 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
289 return gtk_text_iter_get_offset(&iter);
291 static void set_textview_buffer_offset(GtkTextView *view, gint offset)
293 GtkTextBuffer *buffer;
296 g_return_if_fail(view);
298 buffer = gtk_text_view_get_buffer(view);
299 g_return_if_fail(buffer);
301 gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
302 gtk_text_buffer_place_cursor(buffer, &iter);
304 /******************************************************************************/
306 void gtkaspell_checkers_init(void)
308 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
309 gtkaspellcheckers->checkers = NULL;
310 gtkaspellcheckers->dictionary_list = NULL;
311 gtkaspellcheckers->error_message = NULL;
314 void gtkaspell_checkers_quit(void)
319 if (gtkaspellcheckers == NULL)
322 if ((checkers = gtkaspellcheckers->checkers)) {
323 debug_print("Aspell: number of running checkers to delete %d\n",
324 g_slist_length(checkers));
326 g_slist_foreach(checkers, free_checkers, NULL);
327 g_slist_free(checkers);
328 gtkaspellcheckers->checkers = NULL;
331 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
332 debug_print("Aspell: number of dictionaries to delete %d\n",
333 g_slist_length(dict_list));
335 gtkaspell_free_dictionary_list(dict_list);
336 gtkaspellcheckers->dictionary_list = NULL;
339 g_free(gtkaspellcheckers->error_message);
340 gtkaspellcheckers->error_message = NULL;
344 static void gtkaspell_checkers_error_message (gchar *message)
347 if (gtkaspellcheckers->error_message) {
348 tmp = g_strdup_printf("%s\n%s",
349 gtkaspellcheckers->error_message,
352 g_free(gtkaspellcheckers->error_message);
353 gtkaspellcheckers->error_message = tmp;
355 gtkaspellcheckers->error_message = message;
358 const char *gtkaspell_checkers_strerror(void)
360 g_return_val_if_fail(gtkaspellcheckers, "");
361 return gtkaspellcheckers->error_message;
364 void gtkaspell_checkers_reset_error(void)
366 g_return_if_fail(gtkaspellcheckers);
368 g_free(gtkaspellcheckers->error_message);
370 gtkaspellcheckers->error_message = NULL;
373 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
374 const gchar *dictionary,
375 const gchar *encoding,
376 gint misspelled_color,
377 gboolean check_while_typing,
378 gboolean use_alternate,
379 GtkTextView *gtktext,
380 GtkWindow *parent_win)
383 GtkAspell *gtkaspell;
384 GtkAspeller *gtkaspeller;
385 GtkTextBuffer *buffer;
387 g_return_val_if_fail(gtktext, NULL);
388 buffer = gtk_text_view_get_buffer(gtktext);
390 dict = g_new0(Dictionary, 1);
391 dict->fullname = g_strdup(dictionary);
392 dict->encoding = g_strdup(encoding);
394 gtkaspeller = gtkaspeller_new(dict);
395 dictionary_delete(dict);
400 gtkaspell = g_new0(GtkAspell, 1);
402 gtkaspell->dictionary_path = g_strdup(dictionary_path);
404 gtkaspell->gtkaspeller = gtkaspeller;
405 gtkaspell->alternate_speller = NULL;
406 gtkaspell->theword[0] = 0x00;
407 gtkaspell->start_pos = 0;
408 gtkaspell->end_pos = 0;
409 gtkaspell->orig_pos = -1;
410 gtkaspell->end_check_pos = -1;
411 gtkaspell->misspelled = -1;
412 gtkaspell->check_while_typing = check_while_typing;
413 gtkaspell->continue_check = NULL;
414 gtkaspell->config_menu = NULL;
415 gtkaspell->popup_config_menu = NULL;
416 gtkaspell->sug_menu = NULL;
417 gtkaspell->replace_entry = NULL;
418 gtkaspell->gtktext = gtktext;
419 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
420 gtkaspell->max_sug = -1;
421 gtkaspell->suggestions_list = NULL;
422 gtkaspell->use_alternate = use_alternate;
423 gtkaspell->parent_window = GTK_WIDGET(parent_win);
425 allocate_color(gtkaspell, misspelled_color);
427 g_signal_connect_after(G_OBJECT(buffer), "insert-text",
428 G_CALLBACK(entry_insert_cb), gtkaspell);
429 g_signal_connect_after(G_OBJECT(buffer), "delete-range",
430 G_CALLBACK(entry_delete_cb), gtkaspell);
431 g_signal_connect(G_OBJECT(gtktext), "button-press-event",
432 G_CALLBACK(button_press_intercept_cb),
435 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
440 void gtkaspell_delete(GtkAspell *gtkaspell)
442 GtkTextView *gtktext = gtkaspell->gtktext;
444 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
445 G_CALLBACK(entry_insert_cb),
447 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
448 G_CALLBACK(entry_delete_cb),
450 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
451 G_CALLBACK(button_press_intercept_cb),
454 gtkaspell_uncheck_all(gtkaspell);
456 gtkaspeller_delete(gtkaspell->gtkaspeller);
458 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
459 gtkaspeller_delete(gtkaspell->alternate_speller);
461 if (gtkaspell->sug_menu)
462 gtk_widget_destroy(gtkaspell->sug_menu);
464 if (gtkaspell->popup_config_menu)
465 gtk_widget_destroy(gtkaspell->popup_config_menu);
467 if (gtkaspell->config_menu)
468 gtk_widget_destroy(gtkaspell->config_menu);
470 if (gtkaspell->suggestions_list)
471 free_suggestions_list(gtkaspell);
473 g_free((gchar *)gtkaspell->dictionary_path);
474 gtkaspell->dictionary_path = NULL;
476 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
483 static void entry_insert_cb(GtkTextBuffer *textbuf,
487 GtkAspell *gtkaspell)
491 g_return_if_fail(gtkaspell->gtkaspeller->checker);
493 if (!gtkaspell->check_while_typing)
496 pos = gtk_text_iter_get_offset(iter);
498 if (iswordsep(newtext[0])) {
499 /* did we just end a word? */
501 check_at(gtkaspell, pos - 2);
503 /* did we just split a word? */
504 if (pos < gtk_text_buffer_get_char_count(textbuf))
505 check_at(gtkaspell, pos + 1);
507 /* check as they type, *except* if they're typing at the end (the most
510 if (pos < gtk_text_buffer_get_char_count(textbuf) &&
511 !iswordsep(get_text_index_whar(gtkaspell, pos))) {
512 check_at(gtkaspell, pos - 1);
517 static void entry_delete_cb(GtkTextBuffer *textbuf,
518 GtkTextIter *startiter,
519 GtkTextIter *enditer,
520 GtkAspell *gtkaspell)
525 g_return_if_fail(gtkaspell->gtkaspeller->checker);
527 if (!gtkaspell->check_while_typing)
530 start = gtk_text_iter_get_offset(startiter);
531 end = gtk_text_iter_get_offset(enditer);
532 origpos = get_textview_buffer_offset(gtkaspell->gtktext);
534 check_at(gtkaspell, start - 1);
535 check_at(gtkaspell, start);
538 set_textview_buffer_offset(gtkaspell->gtktext, origpos);
539 /* this is to *UNDO* the selection, in case they were holding shift
540 * while hitting backspace. */
541 /* needed with textview ??? */
542 /* gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); */
545 /* ok, this is pretty wacky:
546 * we need to let the right-mouse-click go through, so it moves the cursor,
547 * but we *can't* let it go through, because GtkText interprets rightclicks as
548 * weird selection modifiers.
550 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
553 static gint button_press_intercept_cb(GtkTextView *gtktext,
555 GtkAspell *gtkaspell)
560 g_return_val_if_fail(gtkaspell->gtkaspeller->checker, FALSE);
562 if (e->type != GDK_BUTTON_PRESS)
564 eb = (GdkEventButton*) e;
569 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
570 G_CALLBACK(button_press_intercept_cb),
572 g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
575 /* forge the leftclick */
578 g_signal_emit_by_name(G_OBJECT(gtktext), "button-press-event",
580 g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
582 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
583 G_CALLBACK(button_press_intercept_cb),
585 g_signal_stop_emission_by_name(G_OBJECT(gtktext), "button-press-event");
587 /* now do the menu wackiness */
588 popup_menu(gtkaspell, eb);
589 gtk_grab_remove(GTK_WIDGET(gtktext));
593 /* Checker creation */
594 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
597 GtkAspeller *gtkaspeller = NULL;
601 g_return_val_if_fail(gtkaspellcheckers, NULL);
603 g_return_val_if_fail(dictionary, NULL);
605 if (dictionary->fullname == NULL)
606 gtkaspell_checkers_error_message(
607 g_strdup(_("No dictionary selected.")));
609 g_return_val_if_fail(dictionary->fullname, NULL);
611 if (dictionary->dictname == NULL) {
614 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
617 dictionary->dictname = dictionary->fullname;
619 dictionary->dictname = tmp + 1;
622 dict = dictionary_dup(dictionary);
624 tmp = g_new0(GtkAspeller, 1);
625 tmp->dictionary = dict;
627 exist = g_slist_find_custom(gtkaspellcheckers->checkers, tmp,
632 if ((gtkaspeller = gtkaspeller_real_new(dict)) != NULL) {
633 gtkaspellcheckers->checkers = g_slist_append(
634 gtkaspellcheckers->checkers,
637 debug_print("Aspell: Created a new gtkaspeller %0x\n",
640 dictionary_delete(dict);
642 debug_print("Aspell: Could not create spell checker.\n");
645 debug_print("Aspell: number of existing checkers %d\n",
646 g_slist_length(gtkaspellcheckers->checkers));
651 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
653 GtkAspeller *gtkaspeller;
654 AspellConfig *config;
655 AspellCanHaveError *ret;
657 g_return_val_if_fail(gtkaspellcheckers, NULL);
658 g_return_val_if_fail(dict, NULL);
660 gtkaspeller = g_new(GtkAspeller, 1);
662 gtkaspeller->dictionary = dict;
663 gtkaspeller->sug_mode = ASPELL_FASTMODE;
665 config = new_aspell_config();
667 if (!set_dictionary(config, dict))
670 ret = new_aspell_speller(config);
671 delete_aspell_config(config);
673 if (aspell_error_number(ret) != 0) {
674 gtkaspellcheckers->error_message
675 = g_strdup(aspell_error_message(ret));
677 delete_aspell_can_have_error(ret);
682 gtkaspeller->checker = to_aspell_speller(ret);
683 gtkaspeller->config = aspell_speller_config(gtkaspeller->checker);
688 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
690 g_return_val_if_fail(gtkaspellcheckers, NULL);
692 gtkaspellcheckers->checkers =
693 g_slist_remove(gtkaspellcheckers->checkers,
696 debug_print("Aspell: Deleting gtkaspeller %0x.\n",
699 gtkaspeller_real_delete(gtkaspeller);
701 debug_print("Aspell: number of existing checkers %d\n",
702 g_slist_length(gtkaspellcheckers->checkers));
707 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
709 g_return_val_if_fail(gtkaspeller, NULL);
710 g_return_val_if_fail(gtkaspeller->checker, NULL);
712 aspell_speller_save_all_word_lists(gtkaspeller->checker);
714 delete_aspell_speller(gtkaspeller->checker);
716 dictionary_delete(gtkaspeller->dictionary);
718 debug_print("Aspell: gtkaspeller %0x deleted.\n",
726 /*****************************************************************************/
727 /* Checker configuration */
729 static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
731 gchar *language = NULL;
732 gchar *jargon = NULL;
736 g_return_val_if_fail(config, FALSE);
737 g_return_val_if_fail(dict, FALSE);
739 strncpy(buf, dict->fullname, BUFSIZE-1);
740 buf[BUFSIZE-1] = 0x00;
742 buf[dict->dictname - dict->fullname] = 0x00;
744 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
745 debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
747 strncpy(buf, dict->dictname, BUFSIZE-1);
750 if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
755 if ((jargon = strchr(language, '-')) != NULL)
758 if (size != NULL && jargon == size)
761 debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
762 language, jargon ? jargon : "",
766 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
768 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
770 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
771 if (dict->encoding) {
774 aspell_enc = convert_to_aspell_encoding (dict->encoding);
775 aspell_config_replace(config, "encoding",
776 (const char *) aspell_enc);
779 RETURN_FALSE_IF_CONFIG_ERROR();
785 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
788 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
789 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
791 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
794 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
799 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
800 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
802 dict = gtkaspell->gtkaspeller->dictionary;
803 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
808 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
809 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
813 themode = (char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
814 themode = g_strdup(themode);
816 set_real_sug_mode(gtkaspell, themode);
819 if (gtkaspell->config_menu)
820 populate_submenu(gtkaspell, gtkaspell->config_menu);
823 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
826 gint mode = ASPELL_FASTMODE;
827 g_return_if_fail(gtkaspell);
828 g_return_if_fail(gtkaspell->gtkaspeller);
829 g_return_if_fail(themode);
831 if (!strcmp(themode,_("Normal Mode")))
832 mode = ASPELL_NORMALMODE;
833 else if (!strcmp( themode,_("Bad Spellers Mode")))
834 mode = ASPELL_BADSPELLERMODE;
836 result = gtkaspell_set_sug_mode(gtkaspell, mode);
839 debug_print("Aspell: error while changing suggestion mode:%s\n",
840 gtkaspellcheckers->error_message);
841 gtkaspell_checkers_reset_error();
845 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
846 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
848 AspellConfig *config;
850 g_return_val_if_fail(gtkaspell, FALSE);
851 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
852 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
854 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
855 (guint) gtkaspell->gtkaspeller, themode);
857 config = gtkaspell->gtkaspeller->config;
860 case ASPELL_FASTMODE:
861 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
863 case ASPELL_NORMALMODE:
864 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
866 case ASPELL_BADSPELLERMODE:
867 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
871 gtkaspellcheckers->error_message =
872 g_strdup(_("Unknown suggestion mode."));
876 gtkaspell->gtkaspeller->sug_mode = themode;
877 gtkaspell->default_sug_mode = themode;
882 /* misspelled_suggest() - Create a suggestion list for word */
883 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
885 const guchar *newword;
887 const AspellWordList *suggestions;
888 AspellStringEnumeration *elements;
890 g_return_val_if_fail(word, NULL);
892 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
893 free_suggestions_list(gtkaspell);
895 suggestions = aspell_speller_suggest(
896 gtkaspell->gtkaspeller->checker,
897 (const char *)word, -1);
898 elements = aspell_word_list_elements(suggestions);
899 list = g_list_append(list, g_strdup(word));
901 while ((newword = aspell_string_enumeration_next(elements)) != NULL)
902 list = g_list_append(list, g_strdup(newword));
904 gtkaspell->max_sug = g_list_length(list) - 1;
905 gtkaspell->suggestions_list = list;
910 free_suggestions_list(gtkaspell);
915 /* misspelled_test() - Just test if word is correctly spelled */
916 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
918 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
923 static gboolean iswordsep(unsigned char c)
925 return !isalpha(c) && c != '\'';
928 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
930 GtkTextView *view = gtkaspell->gtktext;
931 GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
932 GtkTextIter start, end;
933 const gchar *utf8chars;
936 gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
937 gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
939 utf8chars = gtk_text_iter_get_text(&start, &end);
940 if (is_ascii_str(utf8chars)) {
941 a = utf8chars ? utf8chars[0] : '\0' ;
943 gchar *tr = conv_iconv_strdup(utf8chars, CS_UTF_8,
944 gtkaspell->gtkaspeller->dictionary->encoding);
954 /* get_word_from_pos () - return the word pointed to. */
955 /* Handles correctly the quotes. */
956 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
957 unsigned char* buf, gint buflen,
958 gint *pstart, gint *pend)
961 /* TODO : when correcting a word into quotes, change the color of */
962 /* the quotes too, as may be they were highlighted before. To do */
963 /* this, we can use two others pointers that points to the whole */
964 /* word including quotes. */
970 GtkTextView *gtktext;
972 gtktext = gtkaspell->gtktext;
973 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
976 /* The apostrophe character is somtimes used for quotes
977 * So include it in the word only if it is not surrounded
978 * by other characters.
981 for (start = pos; start >= 0; --start) {
982 c = get_text_index_whar(gtkaspell, start);
985 if (!isalpha(get_text_index_whar(gtkaspell,
987 /* start_quote = TRUE; */
992 /* start_quote = TRUE; */
996 else if (!isalpha(c))
1002 for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
1003 c = get_text_index_whar(gtkaspell, end);
1005 if (end < get_textview_buffer_charcount(gtktext)) {
1006 if (!isalpha(get_text_index_whar(gtkaspell,
1008 /* end_quote = TRUE; */
1013 /* end_quote = TRUE; */
1017 else if(!isalpha(c))
1027 if (end - start < buflen) {
1028 for (pos = start; pos < end; pos++) {
1030 get_text_index_whar(gtkaspell, pos);
1032 buf[pos - start] = 0;
1040 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
1043 unsigned char buf[GTKASPELLWORDSIZE];
1044 GtkTextView *gtktext;
1046 g_return_val_if_fail(from_pos >= 0, FALSE);
1048 gtktext = gtkaspell->gtktext;
1050 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
1054 if (misspelled_test(gtkaspell, buf)) {
1055 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
1056 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
1057 gtkaspell->start_pos = start;
1058 gtkaspell->end_pos = end;
1059 free_suggestions_list(gtkaspell);
1061 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
1064 change_color(gtkaspell, start, end, buf, NULL);
1069 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1075 gboolean misspelled;
1078 maxpos = gtkaspell->end_check_pos;
1086 pos = get_textview_buffer_offset(gtkaspell->gtktext);
1087 gtkaspell->orig_pos = pos;
1088 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1089 pos > minpos && pos <= maxpos)
1091 while (!(misspelled = check_at(gtkaspell, pos)) &&
1092 pos > minpos && pos <= maxpos) {
1094 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1095 pos > minpos && pos <= maxpos)
1098 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1099 pos > minpos && pos <= maxpos)
1103 GtkMenu *menu = NULL;
1104 misspelled_suggest(gtkaspell, gtkaspell->theword);
1107 gtkaspell->orig_pos = gtkaspell->end_pos;
1109 set_textview_buffer_offset(gtkaspell->gtktext,
1110 gtkaspell->end_pos);
1111 /* scroll line to window center */
1112 gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
1113 gtk_text_buffer_get_insert(
1114 gtk_text_view_get_buffer(gtkaspell->gtktext)),
1115 0.0, TRUE, 0.0, 0.5);
1116 /* let textview recalculate coordinates (set_menu_pos) */
1117 while (gtk_events_pending ())
1118 gtk_main_iteration ();
1120 menu = make_sug_menu(gtkaspell);
1121 gtk_menu_popup(menu, NULL, NULL,
1122 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1123 g_signal_connect(G_OBJECT(menu), "deactivate",
1124 G_CALLBACK(destroy_menu),
1128 reset_theword_data(gtkaspell);
1130 alertpanel_notice(_("No misspelled word found."));
1131 set_textview_buffer_offset(gtkaspell->gtktext,
1132 gtkaspell->orig_pos);
1138 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1140 gtkaspell->continue_check = NULL;
1141 gtkaspell->end_check_pos =
1142 get_textview_buffer_charcount(gtkaspell->gtktext);
1143 check_next_prev(gtkaspell, FALSE);
1146 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1149 gtkaspell->continue_check = NULL;
1150 gtkaspell->end_check_pos =
1151 get_textview_buffer_charcount(gtkaspell->gtktext);
1152 check_next_prev(gtkaspell, TRUE);
1155 void gtkaspell_check_all(GtkAspell *gtkaspell)
1157 GtkTextView *gtktext;
1159 GtkTextBuffer *buffer;
1160 GtkTextIter startiter, enditer;
1162 g_return_if_fail(gtkaspell);
1163 g_return_if_fail(gtkaspell->gtktext);
1165 gtktext = gtkaspell->gtktext;
1166 buffer = gtk_text_view_get_buffer(gtktext);
1167 gtk_text_buffer_get_selection_bounds(buffer, &startiter, &enditer);
1168 start = gtk_text_iter_get_offset(&startiter);
1169 end = gtk_text_iter_get_offset(&enditer);
1173 end = gtk_text_buffer_get_char_count(buffer);
1174 } else if (start > end) {
1182 set_textview_buffer_offset(gtktext, start);
1184 gtkaspell->continue_check = continue_check;
1185 gtkaspell->end_check_pos = end;
1187 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1190 static void continue_check(gpointer *data)
1192 GtkAspell *gtkaspell = (GtkAspell *) data;
1193 gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
1194 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1195 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1197 gtkaspell->continue_check = NULL;
1200 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1205 GtkTextView *gtktext;
1207 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1209 gtktext = gtkaspell->gtktext;
1211 len = get_textview_buffer_charcount(gtktext);
1213 origpos = get_textview_buffer_offset(gtktext);
1217 iswordsep(get_text_index_whar(gtkaspell, pos)))
1220 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1223 check_at(gtkaspell, pos - 1);
1225 set_textview_buffer_offset(gtktext, origpos);
1228 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1230 unsigned char *newword;
1231 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1233 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1236 if (strcmp(newword, gtkaspell->theword)) {
1237 replace_real_word(gtkaspell, newword);
1239 if ((e->type == GDK_KEY_PRESS &&
1240 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK)) {
1241 aspell_speller_store_replacement(
1242 gtkaspell->gtkaspeller->checker,
1243 gtkaspell->theword, -1,
1246 gtkaspell->replace_entry = NULL;
1251 if (w && GTK_IS_DIALOG(w)) {
1252 gtk_widget_destroy(w);
1255 set_point_continue(gtkaspell);
1259 static void replace_word_cb(GtkWidget *w, gpointer data)
1261 unsigned char *newword;
1262 GtkAspell *gtkaspell = (GtkAspell *) data;
1263 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1265 newword = (unsigned char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
1266 newword = g_strdup(newword);
1268 replace_real_word(gtkaspell, newword);
1270 if ((e->type == GDK_KEY_PRESS &&
1271 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK) ||
1272 (e->type == GDK_BUTTON_RELEASE &&
1273 ((GdkEventButton *) e)->state & GDK_CONTROL_MASK)) {
1274 aspell_speller_store_replacement(
1275 gtkaspell->gtkaspeller->checker,
1276 gtkaspell->theword, -1,
1280 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1282 set_point_continue(gtkaspell);
1286 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1288 int oldlen, newlen, wordlen;
1291 GtkTextView *gtktext;
1292 GtkTextBuffer *textbuf;
1293 GtkTextIter startiter, enditer;
1295 if (!newword) return;
1297 gtktext = gtkaspell->gtktext;
1298 textbuf = gtk_text_view_get_buffer(gtktext);
1300 origpos = gtkaspell->orig_pos;
1302 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1303 wordlen = strlen(gtkaspell->theword);
1305 newlen = strlen(newword); /* FIXME: multybyte characters? */
1307 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1308 G_CALLBACK(entry_insert_cb),
1310 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1311 G_CALLBACK(entry_delete_cb),
1314 gtk_text_buffer_get_iter_at_offset(textbuf, &startiter,
1315 gtkaspell->start_pos);
1316 gtk_text_buffer_get_iter_at_offset(textbuf, &enditer,
1317 gtkaspell->end_pos);
1318 g_signal_emit_by_name(G_OBJECT(textbuf), "delete-range",
1319 &startiter, &enditer, gtkaspell);
1320 g_signal_emit_by_name(G_OBJECT(textbuf), "insert-text",
1321 &startiter, newword, newlen, gtkaspell);
1323 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1324 G_CALLBACK(entry_insert_cb),
1326 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1327 G_CALLBACK(entry_delete_cb),
1330 /* Put the point and the position where we clicked with the mouse
1331 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1332 * to let it update correctly the word insertion and then the
1333 * point & position position. If not, SEGV after the first replacement
1334 * If the new word ends before point, put the point at its end.
1337 if (origpos - gtkaspell->start_pos < oldlen &&
1338 origpos - gtkaspell->start_pos >= 0) {
1339 /* Original point was in the word.
1340 * Let it there unless point is going to be outside of the word
1342 if (origpos - gtkaspell->start_pos >= newlen) {
1343 pos = gtkaspell->start_pos + newlen;
1346 else if (origpos >= gtkaspell->end_pos) {
1347 /* move the position according to the change of length */
1348 pos = origpos + newlen - oldlen;
1351 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1353 if (get_textview_buffer_charcount(gtktext) < pos)
1354 pos = get_textview_buffer_charcount(gtktext);
1355 gtkaspell->orig_pos = pos;
1357 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1360 /* Accept this word for this session */
1361 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1364 GtkTextView *gtktext;
1365 GtkAspell *gtkaspell = (GtkAspell *) data;
1366 gtktext = gtkaspell->gtktext;
1368 pos = get_textview_buffer_offset(gtktext);
1370 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1372 strlen(gtkaspell->theword));
1374 check_at(gtkaspell, gtkaspell->start_pos);
1376 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1378 set_point_continue(gtkaspell);
1381 /* add_word_to_personal_cb() - add word to personal dict. */
1382 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1384 GtkAspell *gtkaspell = (GtkAspell *) data;
1386 aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
1388 strlen(gtkaspell->theword));
1390 check_at(gtkaspell, gtkaspell->start_pos);
1392 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1393 set_point_continue(gtkaspell);
1396 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1398 GtkAspell *gtkaspell = (GtkAspell *) data;
1401 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1403 use_alternate_dict(gtkaspell);
1404 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1406 if (!gtkaspell->continue_check) {
1408 gtkaspell->misspelled = misspelled;
1410 if (gtkaspell->misspelled) {
1412 misspelled_suggest(gtkaspell, gtkaspell->theword);
1414 set_textview_buffer_offset(gtkaspell->gtktext,
1415 gtkaspell->end_pos);
1417 menu = make_sug_menu(gtkaspell);
1418 gtk_menu_popup(menu, NULL, NULL,
1419 set_menu_pos, gtkaspell, 0,
1421 g_signal_connect(G_OBJECT(menu), "deactivate",
1422 G_CALLBACK(destroy_menu),
1427 gtkaspell->orig_pos = gtkaspell->start_pos;
1429 set_point_continue(gtkaspell);
1432 static gboolean replace_key_pressed(GtkWidget *widget,
1434 GtkAspell *gtkaspell)
1436 if (event && event->keyval == GDK_Escape) {
1437 gtk_widget_destroy(widget);
1439 } else if (event && event->keyval == GDK_Return) {
1440 replace_with_supplied_word_cb(widget, gtkaspell);
1446 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1448 static PangoFontDescription *font_desc;
1455 GtkWidget *ok_button;
1456 GtkWidget *cancel_button;
1457 GtkWidget *confirm_area;
1461 GtkAspell *gtkaspell = (GtkAspell *) data;
1463 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1465 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1467 dialog = gtk_dialog_new();
1469 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
1470 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1471 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1472 gtk_widget_set_uposition(dialog, xx, yy);
1474 g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
1475 G_CALLBACK(gtk_widget_destroy),
1478 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
1479 hbox = gtk_hbox_new (FALSE, 12);
1480 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1481 gtk_widget_show (hbox);
1482 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
1485 thelabel = g_strdup_printf(_("<span weight=\"bold\" "
1486 "size=\"larger\">Replace \"%s\" with: </span>"),
1487 gtkaspell->theword);
1488 /* for title label */
1489 w_hbox = gtk_hbox_new(FALSE, 0);
1491 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
1492 GTK_ICON_SIZE_DIALOG);
1493 gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
1494 gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
1496 vbox = gtk_vbox_new (FALSE, 12);
1497 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1498 gtk_widget_show (vbox);
1500 label = gtk_label_new(thelabel);
1501 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1502 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1503 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1504 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1505 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1509 size = pango_font_description_get_size
1510 (label->style->font_desc);
1511 font_desc = pango_font_description_new();
1512 pango_font_description_set_weight
1513 (font_desc, PANGO_WEIGHT_BOLD);
1514 pango_font_description_set_size
1515 (font_desc, size * PANGO_SCALE_LARGE);
1518 gtk_widget_modify_font(label, font_desc);
1521 entry = gtk_entry_new();
1522 gtkaspell->replace_entry = entry;
1523 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1524 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1525 g_signal_connect(G_OBJECT(dialog),
1527 G_CALLBACK(replace_key_pressed), gtkaspell);
1528 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
1530 label = gtk_label_new(_("Holding down Control key while pressing "
1531 "Enter\nwill learn from mistake.\n"));
1532 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1533 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1534 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1535 gtk_widget_show(label);
1537 hbox = gtk_hbox_new(TRUE, 0);
1539 gtkut_stock_button_set_create(&confirm_area,
1540 &ok_button, GTK_STOCK_OK,
1541 &cancel_button, GTK_STOCK_CANCEL,
1544 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1545 confirm_area, FALSE, FALSE, 0);
1546 gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
1548 g_signal_connect(G_OBJECT(ok_button), "clicked",
1549 G_CALLBACK(replace_with_supplied_word_cb),
1551 g_signal_connect_swapped(G_OBJECT(ok_button), "clicked",
1552 G_CALLBACK(gtk_widget_destroy),
1555 g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
1556 G_CALLBACK(gtk_widget_destroy),
1559 gtk_widget_grab_focus(entry);
1561 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1563 gtk_widget_show_all(dialog);
1566 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1568 GtkTextView *gtktext;
1569 GtkTextBuffer *buffer;
1570 GtkTextIter startiter, enditer;
1572 gtktext = gtkaspell->gtktext;
1574 buffer = gtk_text_view_get_buffer(gtktext);
1575 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
1576 gtk_text_buffer_get_iter_at_offset(buffer, &enditer,
1577 get_textview_buffer_charcount(gtktext)-1);
1578 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
1579 &startiter, &enditer);
1582 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1584 GtkAspell *gtkaspell = (GtkAspell *) data;
1586 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1588 if (!gtkaspell->check_while_typing)
1589 gtkaspell_uncheck_all(gtkaspell);
1591 if (gtkaspell->config_menu)
1592 populate_submenu(gtkaspell, gtkaspell->config_menu);
1595 static GSList *create_empty_dictionary_list(void)
1597 GSList *list = NULL;
1600 dict = g_new0(Dictionary, 1);
1601 dict->fullname = g_strdup(_("None"));
1602 dict->dictname = dict->fullname;
1603 dict->encoding = NULL;
1605 return g_slist_append(list, dict);
1608 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1609 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1613 AspellConfig *config;
1614 AspellDictInfoList *dlist;
1615 AspellDictInfoEnumeration *dels;
1616 const AspellDictInfo *entry;
1618 if (!gtkaspellcheckers)
1619 gtkaspell_checkers_init();
1621 if (gtkaspellcheckers->dictionary_list && !refresh)
1622 return gtkaspellcheckers->dictionary_list;
1624 gtkaspell_free_dictionary_list(
1625 gtkaspellcheckers->dictionary_list);
1628 config = new_aspell_config();
1630 aspell_config_replace(config, "dict-dir", aspell_path);
1631 if (aspell_config_error_number(config) != 0) {
1632 gtkaspellcheckers->error_message = g_strdup(
1633 aspell_config_error_message(config));
1634 gtkaspellcheckers->dictionary_list =
1635 create_empty_dictionary_list();
1637 return gtkaspellcheckers->dictionary_list;
1640 dlist = get_aspell_dict_info_list(config);
1641 delete_aspell_config(config);
1643 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1644 dels = aspell_dict_info_list_elements(dlist);
1645 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1647 dict = g_new0(Dictionary, 1);
1648 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1650 dict->dictname = dict->fullname + strlen(aspell_path);
1651 dict->encoding = g_strdup(entry->code);
1653 if (g_slist_find_custom(list, dict,
1654 (GCompareFunc) compare_dict) != NULL) {
1655 dictionary_delete(dict);
1659 debug_print("Aspell: found dictionary %s %s %s\n", dict->fullname,
1660 dict->dictname, dict->encoding);
1661 list = g_slist_insert_sorted(list, dict,
1662 (GCompareFunc) compare_dict);
1665 delete_aspell_dict_info_enumeration(dels);
1669 debug_print("Aspell: error when searching for dictionaries: "
1670 "No dictionary found.\n");
1671 list = create_empty_dictionary_list();
1674 gtkaspellcheckers->dictionary_list = list;
1679 void gtkaspell_free_dictionary_list(GSList *list)
1683 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1685 dict = (Dictionary *) walk->data;
1686 dictionary_delete(dict);
1691 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1693 GSList *dict_list, *tmp;
1698 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1699 g_return_val_if_fail(dict_list, NULL);
1701 menu = gtk_menu_new();
1703 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1704 dict = (Dictionary *) tmp->data;
1705 item = gtk_menu_item_new_with_label(dict->dictname);
1706 g_object_set_data(G_OBJECT(item), "dict_name",
1709 gtk_menu_append(GTK_MENU(menu), item);
1710 gtk_widget_show(item);
1713 gtk_widget_show(menu);
1718 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1720 GtkWidget *menuitem;
1721 gchar *dict_fullname;
1724 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1726 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1727 dict_fullname = (gchar *) g_object_get_data(G_OBJECT(menuitem),
1729 g_return_val_if_fail(dict_fullname, NULL);
1731 label = g_strdup(dict_fullname);
1737 gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
1738 const gchar *dictionary)
1743 g_return_val_if_fail(menu != NULL, 0);
1744 g_return_val_if_fail(dictionary != NULL, 0);
1745 g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
1748 for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
1749 GTK_OPTION_MENU(menu)))->children;
1750 cur != NULL; cur = cur->next) {
1751 GtkWidget *menuitem;
1754 menuitem = GTK_WIDGET(cur->data);
1755 dict_name = g_object_get_data(G_OBJECT(menuitem),
1757 if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
1758 gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
1768 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1773 menu = gtk_menu_new();
1774 gtk_widget_show(menu);
1776 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1777 gtk_widget_show(item);
1778 gtk_menu_append(GTK_MENU(menu), item);
1779 g_object_set_data(G_OBJECT(item), "sugmode",
1780 GINT_TO_POINTER(ASPELL_FASTMODE));
1782 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1783 gtk_widget_show(item);
1784 gtk_menu_append(GTK_MENU(menu), item);
1785 g_object_set_data(G_OBJECT(item), "sugmode",
1786 GINT_TO_POINTER(ASPELL_NORMALMODE));
1788 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1789 gtk_widget_show(item);
1790 gtk_menu_append(GTK_MENU(menu), item);
1791 g_object_set_data(G_OBJECT(item), "sugmode",
1792 GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1797 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1799 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1801 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1802 sugmode == ASPELL_NORMALMODE ||
1803 sugmode == ASPELL_BADSPELLERMODE);
1805 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1808 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1813 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1815 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1817 sugmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
1823 static void use_alternate_dict(GtkAspell *gtkaspell)
1827 tmp = gtkaspell->gtkaspeller;
1828 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1829 gtkaspell->alternate_speller = tmp;
1831 if (gtkaspell->config_menu)
1832 populate_submenu(gtkaspell, gtkaspell->config_menu);
1835 static void destroy_menu(GtkWidget *widget,
1836 gpointer user_data) {
1838 GtkAspell *gtkaspell = (GtkAspell *)user_data;
1840 if (gtkaspell->accel_group) {
1841 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1842 gtkaspell->accel_group);
1843 gtkaspell->accel_group = NULL;
1847 static void popup_menu(GtkAspell *gtkaspell, GdkEventButton *eb)
1849 GtkTextView * gtktext;
1850 GtkMenu *menu = NULL;
1852 gtktext = gtkaspell->gtktext;
1854 gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
1856 if (!(eb->state & GDK_SHIFT_MASK)) {
1857 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
1859 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1861 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
1862 menu = make_sug_menu(gtkaspell);
1863 gtk_menu_popup(menu,
1864 NULL, NULL, NULL, NULL,
1865 eb->button, eb->time);
1867 g_signal_connect(G_OBJECT(menu), "deactivate",
1868 G_CALLBACK(destroy_menu),
1873 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1875 menu = make_config_menu(gtkaspell);
1876 gtk_menu_popup(menu, NULL, NULL, NULL, NULL,
1877 eb->button, eb->time);
1879 g_signal_connect(G_OBJECT(menu), "deactivate",
1880 G_CALLBACK(destroy_menu),
1884 static gboolean aspell_key_pressed(GtkWidget *widget,
1886 GtkAspell *gtkaspell)
1888 if (event && (isascii(event->keyval) || event->keyval == GDK_Return)) {
1889 gtk_accel_groups_activate(
1890 G_OBJECT(gtkaspell->parent_window),
1891 event->keyval, event->state);
1892 } else if (event && event->keyval == GDK_Escape) {
1893 destroy_menu(NULL, gtkaspell);
1898 /* make_sug_menu() - Add menus to accept this word for this session
1899 * and to add it to personal dictionary
1901 static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
1903 GtkWidget *menu, *item;
1904 unsigned char *caption;
1905 GtkTextView *gtktext;
1906 GtkAccelGroup *accel;
1907 GList *l = gtkaspell->suggestions_list;
1910 gtktext = gtkaspell->gtktext;
1912 accel = gtk_accel_group_new();
1913 menu = gtk_menu_new();
1915 if (gtkaspell->sug_menu)
1916 gtk_widget_destroy(gtkaspell->sug_menu);
1918 if (gtkaspell->accel_group) {
1919 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1920 gtkaspell->accel_group);
1921 gtkaspell->accel_group = NULL;
1924 gtkaspell->sug_menu = menu;
1926 g_signal_connect(G_OBJECT(menu), "cancel",
1927 G_CALLBACK(cancel_menu_cb), gtkaspell);
1929 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
1930 conv_get_locale_charset_str(),
1932 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1934 gtkaspell->gtkaspeller->dictionary->dictname);
1935 item = gtk_menu_item_new_with_label(caption);
1937 gtk_widget_show(item);
1938 gtk_menu_append(GTK_MENU(menu), item);
1939 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1942 item = gtk_menu_item_new();
1943 gtk_widget_show(item);
1944 gtk_menu_append(GTK_MENU(menu), item);
1946 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1947 gtk_widget_show(item);
1948 gtk_menu_append(GTK_MENU(menu), item);
1949 g_signal_connect(G_OBJECT(item), "activate",
1950 G_CALLBACK(add_word_to_session_cb),
1952 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1954 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1956 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1957 gtk_widget_show(item);
1958 gtk_menu_append(GTK_MENU(menu), item);
1959 g_signal_connect(G_OBJECT(item), "activate",
1960 G_CALLBACK(add_word_to_personal_cb),
1962 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1964 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1966 item = gtk_menu_item_new_with_label(_("Replace with..."));
1967 gtk_widget_show(item);
1968 gtk_menu_append(GTK_MENU(menu), item);
1969 g_signal_connect(G_OBJECT(item), "activate",
1970 G_CALLBACK(replace_with_create_dialog_cb),
1972 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1973 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1974 gtk_widget_add_accelerator(item, "activate", accel, GDK_R,
1978 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1979 caption = g_strdup_printf(_("Check with %s"),
1980 gtkaspell->alternate_speller->dictionary->dictname);
1981 item = gtk_menu_item_new_with_label(caption);
1983 gtk_widget_show(item);
1984 gtk_menu_append(GTK_MENU(menu), item);
1985 g_signal_connect(G_OBJECT(item), "activate",
1986 G_CALLBACK(check_with_alternate_cb),
1988 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1989 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1990 gtk_widget_add_accelerator(item, "activate", accel, GDK_X,
1995 item = gtk_menu_item_new();
1996 gtk_widget_show(item);
1997 gtk_menu_append(GTK_MENU(menu), item);
2001 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
2002 gtk_widget_show(item);
2003 gtk_menu_append(GTK_MENU(menu), item);
2005 GtkWidget *curmenu = menu;
2009 if (count == MENUCOUNT) {
2012 item = gtk_menu_item_new_with_label(_("More..."));
2013 gtk_widget_show(item);
2014 gtk_menu_append(GTK_MENU(curmenu), item);
2016 curmenu = gtk_menu_new();
2017 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2021 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
2022 conv_get_locale_charset_str(),
2024 item = gtk_menu_item_new_with_label(utf8buf);
2026 gtk_widget_show(item);
2027 gtk_menu_append(GTK_MENU(curmenu), item);
2028 g_signal_connect(G_OBJECT(item), "activate",
2029 G_CALLBACK(replace_word_cb),
2032 if (curmenu == menu && count < MENUCOUNT) {
2033 gtk_widget_add_accelerator(item, "activate",
2038 gtk_widget_add_accelerator(item, "activate",
2047 } while ((l = l->next) != NULL);
2050 gtk_window_add_accel_group
2051 (GTK_WINDOW(gtkaspell->parent_window),
2053 gtkaspell->accel_group = accel;
2055 g_signal_connect(G_OBJECT(menu),
2057 G_CALLBACK(aspell_key_pressed), gtkaspell);
2059 return GTK_MENU(menu);
2062 static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
2064 GtkWidget *item, *submenu;
2066 GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
2068 if (GTK_MENU_SHELL(menu)->children) {
2069 GList *amenu, *alist;
2070 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
2071 alist = amenu->next;
2072 gtk_widget_destroy(GTK_WIDGET(amenu->data));
2077 dictname = g_strdup_printf(_("Dictionary: %s"),
2078 gtkaspeller->dictionary->dictname);
2079 item = gtk_menu_item_new_with_label(dictname);
2080 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
2082 gtk_widget_show(item);
2083 gtk_menu_append(GTK_MENU(menu), item);
2085 item = gtk_menu_item_new();
2086 gtk_widget_show(item);
2087 gtk_menu_append(GTK_MENU(menu), item);
2089 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
2090 dictname = g_strdup_printf(_("Use alternate (%s)"),
2091 gtkaspell->alternate_speller->dictionary->dictname);
2092 item = gtk_menu_item_new_with_label(dictname);
2094 g_signal_connect(G_OBJECT(item), "activate",
2095 G_CALLBACK(switch_to_alternate_cb),
2097 gtk_widget_show(item);
2098 gtk_menu_append(GTK_MENU(menu), item);
2101 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
2102 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
2103 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
2104 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
2106 g_signal_connect(G_OBJECT(item), "activate",
2107 G_CALLBACK(set_sug_mode_cb),
2109 gtk_widget_show(item);
2110 gtk_menu_append(GTK_MENU(menu), item);
2112 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
2113 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
2114 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2115 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2117 g_signal_connect(G_OBJECT(item), "activate",
2118 G_CALLBACK(set_sug_mode_cb),
2120 gtk_widget_show(item);
2121 gtk_menu_append(GTK_MENU(menu),item);
2123 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
2124 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
2125 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2126 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2128 g_signal_connect(G_OBJECT(item), "activate",
2129 G_CALLBACK(set_sug_mode_cb),
2131 gtk_widget_show(item);
2132 gtk_menu_append(GTK_MENU(menu), item);
2134 item = gtk_menu_item_new();
2135 gtk_widget_show(item);
2136 gtk_menu_append(GTK_MENU(menu), item);
2138 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
2139 if (gtkaspell->check_while_typing)
2140 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2142 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2143 g_signal_connect(G_OBJECT(item), "activate",
2144 G_CALLBACK(toggle_check_while_typing_cb),
2146 gtk_widget_show(item);
2147 gtk_menu_append(GTK_MENU(menu), item);
2149 item = gtk_menu_item_new();
2150 gtk_widget_show(item);
2151 gtk_menu_append(GTK_MENU(menu), item);
2153 submenu = gtk_menu_new();
2154 item = gtk_menu_item_new_with_label(_("Change dictionary"));
2155 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
2156 gtk_widget_show(item);
2157 gtk_menu_append(GTK_MENU(menu), item);
2160 if (gtkaspellcheckers->dictionary_list == NULL)
2161 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
2163 GtkWidget * curmenu = submenu;
2167 tmp = gtkaspellcheckers->dictionary_list;
2169 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
2170 tmp = g_slist_next(tmp)) {
2171 if (count == MENUCOUNT) {
2174 newmenu = gtk_menu_new();
2175 item = gtk_menu_item_new_with_label(_("More..."));
2176 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2179 gtk_menu_append(GTK_MENU(curmenu), item);
2180 gtk_widget_show(item);
2184 dict = (Dictionary *) tmp->data;
2185 item = gtk_check_menu_item_new_with_label(dict->dictname);
2186 g_object_set_data(G_OBJECT(item), "dict_name",
2188 if (strcmp2(dict->fullname,
2189 gtkaspell->gtkaspeller->dictionary->fullname))
2190 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2192 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2193 gtk_widget_set_sensitive(GTK_WIDGET(item),
2196 g_signal_connect(G_OBJECT(item), "activate",
2197 G_CALLBACK(change_dict_cb),
2199 gtk_widget_show(item);
2200 gtk_menu_append(GTK_MENU(curmenu), item);
2207 static GtkMenu *make_config_menu(GtkAspell *gtkaspell)
2209 if (!gtkaspell->popup_config_menu)
2210 gtkaspell->popup_config_menu = gtk_menu_new();
2212 debug_print("Aspell: creating/using popup_config_menu %0x\n",
2213 (guint) gtkaspell->popup_config_menu);
2214 populate_submenu(gtkaspell, gtkaspell->popup_config_menu);
2216 return GTK_MENU(gtkaspell->popup_config_menu);
2219 void gtkaspell_populate_submenu(GtkAspell *gtkaspell, GtkWidget *menuitem)
2223 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2225 debug_print("Aspell: using config menu %0x\n",
2226 (guint) gtkaspell->popup_config_menu);
2227 populate_submenu(gtkaspell, menu);
2229 gtkaspell->config_menu = menu;
2233 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
2234 gboolean *push_in, gpointer data)
2236 GtkAspell *gtkaspell = (GtkAspell *) data;
2237 gint xx = 0, yy = 0;
2240 GtkTextView *text = GTK_TEXT_VIEW(gtkaspell->gtktext);
2241 GtkTextBuffer *textbuf;
2246 textbuf = gtk_text_view_get_buffer(gtkaspell->gtktext);
2247 gtk_text_buffer_get_iter_at_mark(textbuf, &iter,
2248 gtk_text_buffer_get_insert(textbuf));
2249 gtk_text_view_get_iter_location(gtkaspell->gtktext, &iter, &rect);
2250 gtk_text_view_buffer_to_window_coords(text, GTK_TEXT_WINDOW_TEXT,
2254 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2256 sx = gdk_screen_width();
2257 sy = gdk_screen_height();
2259 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2265 gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
2267 *y = rect.y + rect.height + yy;
2273 gdk_string_height(gtk_style_get_font(
2274 GTK_WIDGET(text)->style),
2275 gtkaspell->theword);
2278 /* Menu call backs */
2280 static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
2282 GtkAspell *gtkaspell = (GtkAspell *) data;
2284 gtkaspell->continue_check = NULL;
2285 set_point_continue(gtkaspell);
2290 gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
2293 GtkAspeller *gtkaspeller;
2296 g_return_val_if_fail(gtkaspell, FALSE);
2297 g_return_val_if_fail(dictionary, FALSE);
2299 sug_mode = gtkaspell->default_sug_mode;
2301 dict = g_new0(Dictionary, 1);
2302 dict->fullname = g_strdup(dictionary);
2303 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2305 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2306 dict == gtkaspell->alternate_speller->dictionary) {
2307 use_alternate_dict(gtkaspell);
2308 dictionary_delete(dict);
2309 gtkaspell->alternate_speller->dictionary = NULL;
2313 gtkaspeller = gtkaspeller_new(dict);
2317 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2318 gtkaspellcheckers->error_message);
2320 alertpanel_warning(message);
2323 if (gtkaspell->use_alternate) {
2324 if (gtkaspell->alternate_speller)
2325 gtkaspeller_delete(gtkaspell->alternate_speller);
2326 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2328 gtkaspeller_delete(gtkaspell->gtkaspeller);
2330 gtkaspell->gtkaspeller = gtkaspeller;
2331 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2334 dictionary_delete(dict);
2336 if (gtkaspell->config_menu)
2337 populate_submenu(gtkaspell, gtkaspell->config_menu);
2342 /* change_dict_cb() - Menu callback : change dict */
2343 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2347 fullname = (gchar *) g_object_get_data(G_OBJECT(w), "dict_name");
2349 if (!strcmp2(fullname, _("None")))
2352 gtkaspell_change_dict(gtkaspell, fullname);
2355 static void switch_to_alternate_cb(GtkWidget *w,
2358 GtkAspell *gtkaspell = (GtkAspell *) data;
2359 use_alternate_dict(gtkaspell);
2362 /* Misc. helper functions */
2364 static void set_point_continue(GtkAspell *gtkaspell)
2366 GtkTextView *gtktext;
2368 gtktext = gtkaspell->gtktext;
2370 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
2372 if (gtkaspell->continue_check)
2373 gtkaspell->continue_check((gpointer *) gtkaspell);
2376 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2378 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtkaspell->gtktext);
2379 GdkColor *color = &(gtkaspell->highlight);
2381 /* Shameless copy from Sylpheed's gtkutils.c */
2383 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2385 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2387 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2390 gtk_text_buffer_create_tag(buffer, "misspelled",
2391 "foreground-gdk", color, NULL);
2394 static void change_color(GtkAspell * gtkaspell,
2395 gint start, gint end,
2399 GtkTextView *gtktext;
2400 GtkTextBuffer *buffer;
2401 GtkTextIter startiter, enditer;
2403 g_return_if_fail(start < end);
2405 gtktext = gtkaspell->gtktext;
2407 buffer = gtk_text_view_get_buffer(gtktext);
2408 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, start);
2409 gtk_text_buffer_get_iter_at_offset(buffer, &enditer, end);
2411 gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
2412 &startiter, &enditer);
2414 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
2415 &startiter, &enditer);
2418 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2419 * as needed by aspell. Returns an allocated string.
2422 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2424 guchar * aspell_encoding;
2426 if (strstr2(encoding, "ISO-8859-")) {
2427 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2430 if (!strcmp2(encoding, "US-ASCII"))
2431 aspell_encoding = g_strdup("iso8859-1");
2433 aspell_encoding = g_strdup(encoding);
2436 return aspell_encoding;
2439 /* compare_dict () - compare 2 dict names */
2440 static gint compare_dict(Dictionary *a, Dictionary *b)
2442 guint aparts = 0, bparts = 0;
2445 for (i=0; i < strlen(a->dictname); i++)
2446 if (a->dictname[i] == '-')
2448 for (i=0; i < strlen(b->dictname); i++)
2449 if (b->dictname[i] == '-')
2452 if (aparts != bparts)
2453 return (aparts < bparts) ? -1 : +1;
2456 compare = strcmp2(a->dictname, b->dictname);
2458 compare = strcmp2(a->fullname, b->fullname);
2463 static void dictionary_delete(Dictionary *dict)
2465 g_free(dict->fullname);
2466 g_free(dict->encoding);
2470 static Dictionary *dictionary_dup(const Dictionary *dict)
2474 dict2 = g_new(Dictionary, 1);
2476 dict2->fullname = g_strdup(dict->fullname);
2477 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2478 dict2->encoding = g_strdup(dict->encoding);
2483 static void free_suggestions_list(GtkAspell *gtkaspell)
2487 for (list = gtkaspell->suggestions_list; list != NULL;
2491 g_list_free(gtkaspell->suggestions_list);
2493 gtkaspell->max_sug = -1;
2494 gtkaspell->suggestions_list = NULL;
2497 static void reset_theword_data(GtkAspell *gtkaspell)
2499 gtkaspell->start_pos = 0;
2500 gtkaspell->end_pos = 0;
2501 gtkaspell->theword[0] = 0;
2502 gtkaspell->max_sug = -1;
2504 free_suggestions_list(gtkaspell);
2507 static void free_checkers(gpointer elt, gpointer data)
2509 GtkAspeller *gtkaspeller = elt;
2511 g_return_if_fail(gtkaspeller);
2513 gtkaspeller_real_delete(gtkaspeller);
2516 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2518 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2519 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2521 if (a && b && a->fullname && b->fullname &&
2522 strcmp(a->fullname, b->fullname) == 0 &&
2523 a->encoding && b->encoding)
2524 return strcmp(a->encoding, b->encoding);