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>
59 #include "gtk/gtktext.h"
63 #include "gtkaspell.h"
64 #define ASPELL_FASTMODE 1
65 #define ASPELL_NORMALMODE 2
66 #define ASPELL_BADSPELLERMODE 3
68 #define GTKASPELLWORDSIZE 1024
70 /* size of the text buffer used in various word-processing routines. */
73 /* number of suggestions to display on each menu. */
76 /* 'config' must be defined as a 'AspellConfig *' */
77 #define RETURN_FALSE_IF_CONFIG_ERROR() \
79 if (aspell_config_error_number(config) != 0) { \
80 gtkaspellcheckers->error_message = g_strdup(aspell_config_error_message(config)); \
85 #define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
86 aspell_config_replace(config, option, value); \
87 RETURN_FALSE_IF_CONFIG_ERROR(); \
90 typedef struct _GtkAspellCheckers {
92 GSList *dictionary_list;
96 typedef struct _Dictionary {
102 typedef struct _GtkAspeller {
103 Dictionary *dictionary;
105 AspellConfig *config;
106 AspellSpeller *checker;
109 typedef void (*ContCheckFunc) (gpointer *gtkaspell);
113 GtkAspeller *gtkaspeller;
114 GtkAspeller *alternate_speller;
115 gchar *dictionary_path;
116 gchar theword[GTKASPELLWORDSIZE];
122 gboolean check_while_typing;
123 gboolean use_alternate;
125 ContCheckFunc continue_check;
127 GtkWidget *config_menu;
128 GtkWidget *popup_config_menu;
130 GtkWidget *replace_entry;
131 GtkWidget *parent_window;
133 gint default_sug_mode;
135 GList *suggestions_list;
137 GtkTextView *gtktext;
141 typedef AspellConfig GtkAspellConfig;
143 /******************************************************************************/
145 static GtkAspellCheckers *gtkaspellcheckers;
147 /* Error message storage */
148 static void gtkaspell_checkers_error_message (gchar *message);
151 static void entry_insert_cb (GtkTextBuffer *textbuf,
155 GtkAspell *gtkaspell);
156 static void entry_delete_cb (GtkTextBuffer *textbuf,
157 GtkTextIter *startiter,
158 GtkTextIter *enditer,
159 GtkAspell *gtkaspell);
160 static gint button_press_intercept_cb (GtkTextView *gtktext,
162 GtkAspell *gtkaspell);
164 /* Checker creation */
165 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
166 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
167 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
168 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
170 /* Checker configuration */
171 static gint set_dictionary (AspellConfig *config,
173 static void set_sug_mode_cb (GtkMenuItem *w,
174 GtkAspell *gtkaspell);
175 static void set_real_sug_mode (GtkAspell *gtkaspell,
176 const char *themode);
178 /* Checker actions */
179 static gboolean check_at (GtkAspell *gtkaspell,
181 static gboolean check_next_prev (GtkAspell *gtkaspell,
183 static GList* misspelled_suggest (GtkAspell *gtkaspell,
185 static void add_word_to_session_cb (GtkWidget *w,
187 static void add_word_to_personal_cb (GtkWidget *w,
189 static void replace_with_create_dialog_cb (GtkWidget *w,
191 static void replace_with_supplied_word_cb (GtkWidget *w,
192 GtkAspell *gtkaspell);
193 static void replace_word_cb (GtkWidget *w,
195 static void replace_real_word (GtkAspell *gtkaspell,
197 static void check_with_alternate_cb (GtkWidget *w,
199 static void use_alternate_dict (GtkAspell *gtkaspell);
200 static void toggle_check_while_typing_cb (GtkWidget *w,
204 static void popup_menu (GtkAspell *gtkaspell,
206 static GtkMenu* make_sug_menu (GtkAspell *gtkaspell);
207 static void populate_submenu (GtkAspell *gtkaspell,
209 static GtkMenu* make_config_menu (GtkAspell *gtkaspell);
210 static void set_menu_pos (GtkMenu *menu,
215 /* Other menu callbacks */
216 static gboolean cancel_menu_cb (GtkMenuShell *w,
218 static void change_dict_cb (GtkWidget *w,
219 GtkAspell *gtkaspell);
220 static void switch_to_alternate_cb (GtkWidget *w,
223 /* Misc. helper functions */
224 static void set_point_continue (GtkAspell *gtkaspell);
225 static void continue_check (gpointer *gtkaspell);
226 static gboolean iswordsep (unsigned char c);
227 static guchar get_text_index_whar (GtkAspell *gtkaspell,
229 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
235 static void allocate_color (GtkAspell *gtkaspell,
237 static void change_color (GtkAspell *gtkaspell,
242 static guchar* convert_to_aspell_encoding (const guchar *encoding);
243 static gint compare_dict (Dictionary *a,
245 static void dictionary_delete (Dictionary *dict);
246 static Dictionary * dictionary_dup (const Dictionary *dict);
247 static void free_suggestions_list (GtkAspell *gtkaspell);
248 static void reset_theword_data (GtkAspell *gtkaspell);
249 static void free_checkers (gpointer elt,
251 static gint find_gtkaspeller (gconstpointer aa,
253 static void gtkaspell_alert_dialog (gchar *message);
254 /* gtkspellconfig - only one config per session */
255 GtkAspellConfig * gtkaspellconfig;
257 /******************************************************************************/
258 static gint get_textview_buffer_charcount(GtkTextView *view);
259 static gint get_textview_buffer_position(GtkTextView *view);
261 static gint get_textview_buffer_charcount(GtkTextView *view)
263 GtkTextBuffer *buffer;
265 g_return_val_if_fail(view, 0);
267 buffer = gtk_text_view_get_buffer(view);
268 g_return_val_if_fail(buffer, 0);
270 return gtk_text_buffer_get_char_count(buffer);
272 static gint get_textview_buffer_offset(GtkTextView *view)
274 GtkTextBuffer * buffer;
278 g_return_val_if_fail(view, 0);
280 buffer = gtk_text_view_get_buffer(view);
281 g_return_val_if_fail(buffer, 0);
283 mark = gtk_text_buffer_get_insert(buffer);
284 g_return_val_if_fail(mark, 0);
286 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
288 return gtk_text_iter_get_offset(&iter);
290 static void set_textview_buffer_offset(GtkTextView *view, gint offset)
292 GtkTextBuffer *buffer;
295 g_return_if_fail(view);
297 buffer = gtk_text_view_get_buffer(view);
298 g_return_if_fail(buffer);
300 gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
301 gtk_text_buffer_place_cursor(buffer, &iter);
303 /******************************************************************************/
305 void gtkaspell_checkers_init(void)
307 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
308 gtkaspellcheckers->checkers = NULL;
309 gtkaspellcheckers->dictionary_list = NULL;
310 gtkaspellcheckers->error_message = NULL;
313 void gtkaspell_checkers_quit(void)
318 if (gtkaspellcheckers == NULL)
321 if ((checkers = gtkaspellcheckers->checkers)) {
322 debug_print("Aspell: number of running checkers to delete %d\n",
323 g_slist_length(checkers));
325 g_slist_foreach(checkers, free_checkers, NULL);
326 g_slist_free(checkers);
329 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
330 debug_print("Aspell: number of dictionaries to delete %d\n",
331 g_slist_length(dict_list));
333 gtkaspell_free_dictionary_list(dict_list);
334 gtkaspellcheckers->dictionary_list = NULL;
337 g_free(gtkaspellcheckers->error_message);
342 static void gtkaspell_checkers_error_message (gchar *message)
345 if (gtkaspellcheckers->error_message) {
346 tmp = g_strdup_printf("%s\n%s",
347 gtkaspellcheckers->error_message,
350 g_free(gtkaspellcheckers->error_message);
351 gtkaspellcheckers->error_message = tmp;
353 gtkaspellcheckers->error_message = message;
356 const char *gtkaspell_checkers_strerror(void)
358 g_return_val_if_fail(gtkaspellcheckers, "");
359 return gtkaspellcheckers->error_message;
362 void gtkaspell_checkers_reset_error(void)
364 g_return_if_fail(gtkaspellcheckers);
366 g_free(gtkaspellcheckers->error_message);
368 gtkaspellcheckers->error_message = NULL;
371 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
372 const gchar *dictionary,
373 const gchar *encoding,
374 gint misspelled_color,
375 gboolean check_while_typing,
376 gboolean use_alternate,
377 GtkTextView *gtktext,
378 GtkWindow *parent_win)
381 GtkAspell *gtkaspell;
382 GtkAspeller *gtkaspeller;
383 GtkTextBuffer *buffer;
385 g_return_val_if_fail(gtktext, NULL);
386 buffer = gtk_text_view_get_buffer(gtktext);
388 dict = g_new0(Dictionary, 1);
389 dict->fullname = g_strdup(dictionary);
390 dict->encoding = g_strdup(encoding);
392 gtkaspeller = gtkaspeller_new(dict);
393 dictionary_delete(dict);
398 gtkaspell = g_new0(GtkAspell, 1);
400 gtkaspell->dictionary_path = g_strdup(dictionary_path);
402 gtkaspell->gtkaspeller = gtkaspeller;
403 gtkaspell->alternate_speller = NULL;
404 gtkaspell->theword[0] = 0x00;
405 gtkaspell->start_pos = 0;
406 gtkaspell->end_pos = 0;
407 gtkaspell->orig_pos = -1;
408 gtkaspell->end_check_pos = -1;
409 gtkaspell->misspelled = -1;
410 gtkaspell->check_while_typing = check_while_typing;
411 gtkaspell->continue_check = NULL;
412 gtkaspell->config_menu = NULL;
413 gtkaspell->popup_config_menu = NULL;
414 gtkaspell->sug_menu = NULL;
415 gtkaspell->replace_entry = NULL;
416 gtkaspell->gtktext = gtktext;
417 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
418 gtkaspell->max_sug = -1;
419 gtkaspell->suggestions_list = NULL;
420 gtkaspell->use_alternate = use_alternate;
421 gtkaspell->parent_window = GTK_WIDGET(parent_win);
423 allocate_color(gtkaspell, misspelled_color);
425 g_signal_connect_after(G_OBJECT(buffer), "insert-text",
426 G_CALLBACK(entry_insert_cb), gtkaspell);
427 g_signal_connect_after(G_OBJECT(buffer), "delete-range",
428 G_CALLBACK(entry_delete_cb), gtkaspell);
429 g_signal_connect(G_OBJECT(gtktext), "button-press-event",
430 G_CALLBACK(button_press_intercept_cb),
433 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
438 void gtkaspell_delete(GtkAspell * gtkaspell)
440 GtkTextView *gtktext = gtkaspell->gtktext;
442 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
443 G_CALLBACK(entry_insert_cb),
445 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
446 G_CALLBACK(entry_delete_cb),
448 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
449 G_CALLBACK(button_press_intercept_cb),
452 gtkaspell_uncheck_all(gtkaspell);
454 gtkaspeller_delete(gtkaspell->gtkaspeller);
456 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
457 gtkaspeller_delete(gtkaspell->alternate_speller);
459 if (gtkaspell->sug_menu)
460 gtk_widget_destroy(gtkaspell->sug_menu);
462 if (gtkaspell->popup_config_menu)
463 gtk_widget_destroy(gtkaspell->popup_config_menu);
465 if (gtkaspell->config_menu)
466 gtk_widget_destroy(gtkaspell->config_menu);
468 if (gtkaspell->suggestions_list)
469 free_suggestions_list(gtkaspell);
471 g_free((gchar *)gtkaspell->dictionary_path);
473 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
480 static void entry_insert_cb(GtkTextBuffer *textbuf,
484 GtkAspell *gtkaspell)
489 g_return_if_fail(gtkaspell->gtkaspeller->checker);
491 if (!gtkaspell->check_while_typing)
494 pos = gtk_text_iter_get_offset(iter);
496 if (iswordsep(newtext[0])) {
497 /* did we just end a word? */
499 check_at(gtkaspell, pos - 2);
501 /* did we just split a word? */
502 if (pos < gtk_text_buffer_get_char_count(textbuf))
503 check_at(gtkaspell, pos + 1);
505 /* check as they type, *except* if they're typing at the end (the most
508 if (pos < gtk_text_buffer_get_char_count(textbuf) &&
509 !iswordsep(get_text_index_whar(gtkaspell, pos))) {
510 check_at(gtkaspell, pos - 1);
515 static void entry_delete_cb(GtkTextBuffer *textbuf,
516 GtkTextIter *startiter,
517 GtkTextIter *enditer,
518 GtkAspell *gtkaspell)
523 g_return_if_fail(gtkaspell->gtkaspeller->checker);
525 if (!gtkaspell->check_while_typing)
528 start = gtk_text_iter_get_offset(startiter);
529 end = gtk_text_iter_get_offset(enditer);
530 origpos = get_textview_buffer_offset(gtkaspell->gtktext);
532 check_at(gtkaspell, start - 1);
533 check_at(gtkaspell, start);
536 set_textview_buffer_offset(gtkaspell->gtktext, origpos);
537 /* this is to *UNDO* the selection, in case they were holding shift
538 * while hitting backspace. */
539 /* needed with textview ??? */
540 /* gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); */
543 /* ok, this is pretty wacky:
544 * we need to let the right-mouse-click go through, so it moves the cursor,
545 * but we *can't* let it go through, because GtkText interprets rightclicks as
546 * weird selection modifiers.
548 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
551 static gint button_press_intercept_cb(GtkTextView *gtktext,
553 GtkAspell *gtkaspell)
558 g_return_val_if_fail(gtkaspell->gtkaspeller->checker, FALSE);
560 if (e->type != GDK_BUTTON_PRESS)
562 eb = (GdkEventButton*) e;
567 /* forge the leftclick */
570 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
571 G_CALLBACK(button_press_intercept_cb),
573 g_signal_emit_by_name(G_OBJECT(gtktext), "button-press-event",
575 g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
577 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
578 G_CALLBACK(button_press_intercept_cb),
580 g_signal_stop_emission_by_name(G_OBJECT(gtktext), "button-press-event");
582 /* now do the menu wackiness */
583 popup_menu(gtkaspell, eb);
584 gtk_grab_remove(GTK_WIDGET(gtktext));
588 /* Checker creation */
589 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
592 GtkAspeller *gtkaspeller = NULL;
596 g_return_val_if_fail(gtkaspellcheckers, NULL);
598 g_return_val_if_fail(dictionary, NULL);
600 if (dictionary->fullname == NULL)
601 gtkaspell_checkers_error_message(
602 g_strdup(_("No dictionary selected.")));
604 g_return_val_if_fail(dictionary->fullname, NULL);
606 if (dictionary->dictname == NULL) {
609 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
612 dictionary->dictname = dictionary->fullname;
614 dictionary->dictname = tmp + 1;
617 dict = dictionary_dup(dictionary);
619 tmp = g_new0(GtkAspeller, 1);
620 tmp->dictionary = dict;
622 exist = g_slist_find_custom(gtkaspellcheckers->checkers, tmp,
627 if ((gtkaspeller = gtkaspeller_real_new(dict)) != NULL) {
628 gtkaspellcheckers->checkers = g_slist_append(
629 gtkaspellcheckers->checkers,
632 debug_print("Aspell: Created a new gtkaspeller %0x\n",
635 dictionary_delete(dict);
637 debug_print("Aspell: Could not create spell checker.\n");
640 debug_print("Aspell: number of existing checkers %d\n",
641 g_slist_length(gtkaspellcheckers->checkers));
646 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
648 GtkAspeller *gtkaspeller;
649 AspellConfig *config;
650 AspellCanHaveError *ret;
652 g_return_val_if_fail(gtkaspellcheckers, NULL);
653 g_return_val_if_fail(dict, NULL);
655 gtkaspeller = g_new(GtkAspeller, 1);
657 gtkaspeller->dictionary = dict;
658 gtkaspeller->sug_mode = ASPELL_FASTMODE;
660 config = new_aspell_config();
662 if (!set_dictionary(config, dict))
665 ret = new_aspell_speller(config);
666 delete_aspell_config(config);
668 if (aspell_error_number(ret) != 0) {
669 gtkaspellcheckers->error_message
670 = g_strdup(aspell_error_message(ret));
672 delete_aspell_can_have_error(ret);
677 gtkaspeller->checker = to_aspell_speller(ret);
678 gtkaspeller->config = aspell_speller_config(gtkaspeller->checker);
683 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
685 g_return_val_if_fail(gtkaspellcheckers, NULL);
687 gtkaspellcheckers->checkers =
688 g_slist_remove(gtkaspellcheckers->checkers,
691 debug_print("Aspell: Deleting gtkaspeller %0x.\n",
694 gtkaspeller_real_delete(gtkaspeller);
696 debug_print("Aspell: number of existing checkers %d\n",
697 g_slist_length(gtkaspellcheckers->checkers));
702 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
704 g_return_val_if_fail(gtkaspeller, NULL);
705 g_return_val_if_fail(gtkaspeller->checker, NULL);
707 aspell_speller_save_all_word_lists(gtkaspeller->checker);
709 delete_aspell_speller(gtkaspeller->checker);
711 dictionary_delete(gtkaspeller->dictionary);
713 debug_print("Aspell: gtkaspeller %0x deleted.\n",
721 /*****************************************************************************/
722 /* Checker configuration */
724 static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
726 gchar *language = NULL;
727 gchar *jargon = NULL;
731 g_return_val_if_fail(config, FALSE);
732 g_return_val_if_fail(dict, FALSE);
734 strncpy(buf, dict->fullname, BUFSIZE-1);
735 buf[BUFSIZE-1] = 0x00;
737 buf[dict->dictname - dict->fullname] = 0x00;
739 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
740 debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
742 strncpy(buf, dict->dictname, BUFSIZE-1);
745 if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
750 if ((jargon = strchr(language, '-')) != NULL)
753 if (size != NULL && jargon == size)
756 debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
757 language, jargon ? jargon : "",
761 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
763 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
765 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
766 if (dict->encoding) {
769 aspell_enc = convert_to_aspell_encoding (dict->encoding);
770 aspell_config_replace(config, "encoding",
771 (const char *) aspell_enc);
774 RETURN_FALSE_IF_CONFIG_ERROR();
780 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
783 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
784 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
786 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
789 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
794 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
795 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
797 dict = gtkaspell->gtkaspeller->dictionary;
798 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
803 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
804 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
808 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
810 set_real_sug_mode(gtkaspell, themode);
812 if (gtkaspell->config_menu)
813 populate_submenu(gtkaspell, gtkaspell->config_menu);
816 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
819 gint mode = ASPELL_FASTMODE;
820 g_return_if_fail(gtkaspell);
821 g_return_if_fail(gtkaspell->gtkaspeller);
822 g_return_if_fail(themode);
824 if (!strcmp(themode,_("Normal Mode")))
825 mode = ASPELL_NORMALMODE;
826 else if (!strcmp( themode,_("Bad Spellers Mode")))
827 mode = ASPELL_BADSPELLERMODE;
829 result = gtkaspell_set_sug_mode(gtkaspell, mode);
832 debug_print("Aspell: error while changing suggestion mode:%s\n",
833 gtkaspellcheckers->error_message);
834 gtkaspell_checkers_reset_error();
838 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
839 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
841 AspellConfig *config;
843 g_return_val_if_fail(gtkaspell, FALSE);
844 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
845 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
847 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
848 (guint) gtkaspell->gtkaspeller, themode);
850 config = gtkaspell->gtkaspeller->config;
853 case ASPELL_FASTMODE:
854 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
856 case ASPELL_NORMALMODE:
857 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
859 case ASPELL_BADSPELLERMODE:
860 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
864 gtkaspellcheckers->error_message =
865 g_strdup(_("Unknown suggestion mode."));
869 gtkaspell->gtkaspeller->sug_mode = themode;
870 gtkaspell->default_sug_mode = themode;
875 /* misspelled_suggest() - Create a suggestion list for word */
876 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
878 const guchar *newword;
880 const AspellWordList *suggestions;
881 AspellStringEnumeration *elements;
883 g_return_val_if_fail(word, NULL);
885 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
886 free_suggestions_list(gtkaspell);
888 suggestions = aspell_speller_suggest(
889 gtkaspell->gtkaspeller->checker,
890 (const char *)word, -1);
891 elements = aspell_word_list_elements(suggestions);
892 list = g_list_append(list, g_strdup(word));
894 while (newword = aspell_string_enumeration_next(elements))
895 list = g_list_append(list, g_strdup(newword));
897 gtkaspell->max_sug = g_list_length(list) - 1;
898 gtkaspell->suggestions_list = list;
903 free_suggestions_list(gtkaspell);
908 /* misspelled_test() - Just test if word is correctly spelled */
909 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
911 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
916 static gboolean iswordsep(unsigned char c)
918 return !isalpha(c) && c != '\'';
921 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
923 GtkTextView *view = gtkaspell->gtktext;
924 GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
925 GtkTextIter start, end;
926 const gchar *utf8chars;
929 gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
930 gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
932 utf8chars = gtk_text_iter_get_text(&start, &end);
933 if (is_ascii_str(utf8chars)) {
934 a = utf8chars ? utf8chars[0] : '\0' ;
936 gchar *tr = conv_iconv_strdup(utf8chars, CS_UTF_8,
937 gtkaspell->gtkaspeller->dictionary->encoding);
947 /* get_word_from_pos () - return the word pointed to. */
948 /* Handles correctly the quotes. */
949 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
950 unsigned char* buf, gint buflen,
951 gint *pstart, gint *pend)
954 /* TODO : when correcting a word into quotes, change the color of */
955 /* the quotes too, as may be they were highlighted before. To do */
956 /* this, we can use two others pointers that points to the whole */
957 /* word including quotes. */
963 GtkTextView *gtktext;
965 gtktext = gtkaspell->gtktext;
966 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
969 /* The apostrophe character is somtimes used for quotes
970 * So include it in the word only if it is not surrounded
971 * by other characters.
974 for (start = pos; start >= 0; --start) {
975 c = get_text_index_whar(gtkaspell, start);
978 if (!isalpha(get_text_index_whar(gtkaspell,
980 /* start_quote = TRUE; */
985 /* start_quote = TRUE; */
989 else if (!isalpha(c))
995 for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
996 c = get_text_index_whar(gtkaspell, end);
998 if (end < get_textview_buffer_charcount(gtktext)) {
999 if (!isalpha(get_text_index_whar(gtkaspell,
1001 /* end_quote = TRUE; */
1006 /* end_quote = TRUE; */
1010 else if(!isalpha(c))
1020 if (end - start < buflen) {
1021 for (pos = start; pos < end; pos++) {
1023 get_text_index_whar(gtkaspell, pos);
1025 buf[pos - start] = 0;
1033 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
1036 unsigned char buf[GTKASPELLWORDSIZE];
1037 GtkTextView *gtktext;
1039 g_return_val_if_fail(from_pos >= 0, FALSE);
1041 gtktext = gtkaspell->gtktext;
1043 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
1047 if (misspelled_test(gtkaspell, buf)) {
1048 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
1049 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
1050 gtkaspell->start_pos = start;
1051 gtkaspell->end_pos = end;
1052 free_suggestions_list(gtkaspell);
1054 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
1057 change_color(gtkaspell, start, end, buf, NULL);
1062 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1068 gboolean misspelled;
1071 maxpos = gtkaspell->end_check_pos;
1079 pos = get_textview_buffer_offset(gtkaspell->gtktext);
1080 gtkaspell->orig_pos = pos;
1081 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1082 pos > minpos && pos <= maxpos)
1084 while (!(misspelled = check_at(gtkaspell, pos)) &&
1085 pos > minpos && pos <= maxpos) {
1087 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1088 pos > minpos && pos <= maxpos)
1091 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1092 pos > minpos && pos <= maxpos)
1096 misspelled_suggest(gtkaspell, gtkaspell->theword);
1099 gtkaspell->orig_pos = gtkaspell->end_pos;
1101 set_textview_buffer_offset(gtkaspell->gtktext,
1102 gtkaspell->end_pos);
1103 /* scroll line to window center */
1104 gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
1105 gtk_text_buffer_get_insert(
1106 gtk_text_view_get_buffer(gtkaspell->gtktext)),
1107 0.0, TRUE, 0.0, 0.5);
1108 /* let textview recalculate coordinates (set_menu_pos) */
1109 while (gtk_events_pending ())
1110 gtk_main_iteration ();
1112 gtk_menu_popup(make_sug_menu(gtkaspell), NULL, NULL,
1113 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1116 reset_theword_data(gtkaspell);
1118 gtkaspell_alert_dialog(_("No misspelled word found."));
1119 set_textview_buffer_offset(gtkaspell->gtktext,
1120 gtkaspell->orig_pos);
1126 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1128 gtkaspell->continue_check = NULL;
1129 gtkaspell->end_check_pos =
1130 get_textview_buffer_charcount(gtkaspell->gtktext);
1131 check_next_prev(gtkaspell, FALSE);
1134 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1137 gtkaspell->continue_check = NULL;
1138 gtkaspell->end_check_pos =
1139 get_textview_buffer_charcount(gtkaspell->gtktext);
1140 check_next_prev(gtkaspell, TRUE);
1143 void gtkaspell_check_all(GtkAspell *gtkaspell)
1145 GtkTextView *gtktext;
1147 GtkTextBuffer *buffer;
1148 GtkTextIter startiter, enditer;
1150 g_return_if_fail(gtkaspell);
1151 g_return_if_fail(gtkaspell->gtktext);
1153 gtktext = gtkaspell->gtktext;
1154 buffer = gtk_text_view_get_buffer(gtktext);
1155 gtk_text_buffer_get_selection_bounds(buffer, &startiter, &enditer);
1156 start = gtk_text_iter_get_offset(&startiter);
1157 end = gtk_text_iter_get_offset(&enditer);
1161 end = gtk_text_buffer_get_char_count(buffer);
1162 } else if (start > end) {
1170 set_textview_buffer_offset(gtktext, start);
1172 gtkaspell->continue_check = continue_check;
1173 gtkaspell->end_check_pos = end;
1175 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1178 static void continue_check(gpointer *data)
1180 GtkAspell *gtkaspell = (GtkAspell *) data;
1181 gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
1182 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1183 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1185 gtkaspell->continue_check = NULL;
1188 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1193 GtkTextView *gtktext;
1195 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1197 gtktext = gtkaspell->gtktext;
1199 len = get_textview_buffer_charcount(gtktext);
1201 origpos = get_textview_buffer_offset(gtktext);
1205 iswordsep(get_text_index_whar(gtkaspell, pos)))
1208 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1211 check_at(gtkaspell, pos - 1);
1213 set_textview_buffer_offset(gtktext, origpos);
1216 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1218 unsigned char *newword;
1219 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1221 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1224 if (strcmp(newword, gtkaspell->theword)) {
1225 replace_real_word(gtkaspell, newword);
1227 if ((e->type == GDK_KEY_PRESS &&
1228 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK)) {
1229 aspell_speller_store_replacement(
1230 gtkaspell->gtkaspeller->checker,
1231 gtkaspell->theword, -1,
1234 gtkaspell->replace_entry = NULL;
1239 set_point_continue(gtkaspell);
1243 static void replace_word_cb(GtkWidget *w, gpointer data)
1245 unsigned char *newword;
1246 GtkAspell *gtkaspell = (GtkAspell *) data;
1247 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1249 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
1251 replace_real_word(gtkaspell, newword);
1253 if ((e->type == GDK_KEY_PRESS &&
1254 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK) ||
1255 (e->type == GDK_BUTTON_RELEASE &&
1256 ((GdkEventButton *) e)->state & GDK_CONTROL_MASK)) {
1257 aspell_speller_store_replacement(
1258 gtkaspell->gtkaspeller->checker,
1259 gtkaspell->theword, -1,
1263 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1265 set_point_continue(gtkaspell);
1268 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1270 int oldlen, newlen, wordlen;
1273 gint start = gtkaspell->start_pos;
1274 GtkTextView *gtktext;
1275 GtkTextBuffer *textbuf;
1276 GtkTextIter startiter, enditer;
1278 if (!newword) return;
1280 gtktext = gtkaspell->gtktext;
1281 textbuf = gtk_text_view_get_buffer(gtktext);
1283 origpos = gtkaspell->orig_pos;
1285 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1286 wordlen = strlen(gtkaspell->theword);
1288 newlen = strlen(newword); /* FIXME: multybyte characters? */
1290 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1291 G_CALLBACK(entry_insert_cb),
1293 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1294 G_CALLBACK(entry_delete_cb),
1297 gtk_text_buffer_get_iter_at_offset(textbuf, &startiter,
1298 gtkaspell->start_pos);
1299 gtk_text_buffer_get_iter_at_offset(textbuf, &enditer,
1300 gtkaspell->end_pos);
1301 g_signal_emit_by_name(G_OBJECT(textbuf), "delete-range",
1302 &startiter, &enditer, gtkaspell);
1303 g_signal_emit_by_name(G_OBJECT(textbuf), "insert-text",
1304 &startiter, newword, newlen, gtkaspell);
1306 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1307 G_CALLBACK(entry_insert_cb),
1309 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1310 G_CALLBACK(entry_delete_cb),
1313 /* Put the point and the position where we clicked with the mouse
1314 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1315 * to let it update correctly the word insertion and then the
1316 * point & position position. If not, SEGV after the first replacement
1317 * If the new word ends before point, put the point at its end.
1320 if (origpos - gtkaspell->start_pos < oldlen &&
1321 origpos - gtkaspell->start_pos >= 0) {
1322 /* Original point was in the word.
1323 * Let it there unless point is going to be outside of the word
1325 if (origpos - gtkaspell->start_pos >= newlen) {
1326 pos = gtkaspell->start_pos + newlen;
1329 else if (origpos >= gtkaspell->end_pos) {
1330 /* move the position according to the change of length */
1331 pos = origpos + newlen - oldlen;
1334 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1336 if (get_textview_buffer_charcount(gtktext) < pos)
1337 pos = get_textview_buffer_charcount(gtktext);
1338 gtkaspell->orig_pos = pos;
1340 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1343 /* Accept this word for this session */
1344 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1347 GtkTextView *gtktext;
1348 GtkAspell *gtkaspell = (GtkAspell *) data;
1349 gtktext = gtkaspell->gtktext;
1351 pos = get_textview_buffer_offset(gtktext);
1353 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1355 strlen(gtkaspell->theword));
1357 check_at(gtkaspell, gtkaspell->start_pos);
1359 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1361 set_point_continue(gtkaspell);
1364 /* add_word_to_personal_cb() - add word to personal dict. */
1365 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1367 GtkAspell *gtkaspell = (GtkAspell *) data;
1368 GtkTextView *gtktext = gtkaspell->gtktext;
1370 aspell_speller_add_to_personal(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));
1377 set_point_continue(gtkaspell);
1380 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1382 GtkAspell *gtkaspell = (GtkAspell *) data;
1385 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1387 use_alternate_dict(gtkaspell);
1388 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1390 if (!gtkaspell->continue_check) {
1392 gtkaspell->misspelled = misspelled;
1394 if (gtkaspell->misspelled) {
1396 misspelled_suggest(gtkaspell, gtkaspell->theword);
1398 set_textview_buffer_offset(gtkaspell->gtktext,
1399 gtkaspell->end_pos);
1401 gtk_menu_popup(make_sug_menu(gtkaspell), NULL, NULL,
1402 set_menu_pos, gtkaspell, 0,
1407 gtkaspell->orig_pos = gtkaspell->start_pos;
1409 set_point_continue(gtkaspell);
1412 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1418 GtkWidget *ok_button;
1419 GtkWidget *cancel_button;
1422 GtkAspell *gtkaspell = (GtkAspell *) data;
1424 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1426 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1428 dialog = gtk_dialog_new();
1430 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
1431 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1432 gtk_widget_set_uposition(dialog, xx, yy);
1434 g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
1435 G_CALLBACK(gtk_widget_destroy),
1438 hbox = gtk_hbox_new(FALSE, 0);
1439 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
1441 thelabel = g_strdup_printf(_("Replace \"%s\" with: "),
1442 gtkaspell->theword);
1443 label = gtk_label_new(thelabel);
1445 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1447 entry = gtk_entry_new();
1448 gtkaspell->replace_entry = entry;
1449 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1450 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1451 g_signal_connect(G_OBJECT(entry), "activate",
1452 G_CALLBACK(replace_with_supplied_word_cb),
1454 g_signal_connect_swapped(G_OBJECT(entry), "activate",
1455 G_CALLBACK(gtk_widget_destroy),
1457 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1459 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE,
1461 label = gtk_label_new(_("Holding down Control key while pressing "
1462 "Enter\nwill learn from mistake.\n"));
1463 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1464 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1465 gtk_misc_set_padding(GTK_MISC(label), 8, 0);
1466 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label,
1469 hbox = gtk_hbox_new(TRUE, 0);
1471 ok_button = gtk_button_new_with_label(_("OK"));
1472 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
1473 g_signal_connect(G_OBJECT(ok_button), "clicked",
1474 G_CALLBACK(replace_with_supplied_word_cb),
1476 g_signal_connect_swapped(G_OBJECT(ok_button), "clicked",
1477 G_CALLBACK(gtk_widget_destroy),
1480 cancel_button = gtk_button_new_with_label(_("Cancel"));
1481 gtk_box_pack_start(GTK_BOX(hbox), cancel_button, TRUE, TRUE, 8);
1482 g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
1483 G_CALLBACK(gtk_widget_destroy),
1486 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1488 gtk_widget_grab_focus(entry);
1490 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1492 gtk_widget_show_all(dialog);
1495 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1500 GtkTextView *gtktext;
1501 GtkTextBuffer *buffer;
1502 GtkTextIter startiter, enditer;
1504 gtktext = gtkaspell->gtktext;
1506 buffer = gtk_text_view_get_buffer(gtktext);
1507 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
1508 gtk_text_buffer_get_iter_at_offset(buffer, &enditer,
1509 get_textview_buffer_charcount(gtktext)-1);
1510 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
1511 &startiter, &enditer);
1514 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1516 GtkAspell *gtkaspell = (GtkAspell *) data;
1518 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1520 if (!gtkaspell->check_while_typing)
1521 gtkaspell_uncheck_all(gtkaspell);
1523 if (gtkaspell->config_menu)
1524 populate_submenu(gtkaspell, gtkaspell->config_menu);
1527 static GSList *create_empty_dictionary_list(void)
1529 GSList *list = NULL;
1532 dict = g_new0(Dictionary, 1);
1533 dict->fullname = g_strdup(_("None"));
1534 dict->dictname = dict->fullname;
1535 dict->encoding = NULL;
1537 return g_slist_append(list, dict);
1540 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1541 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1545 AspellConfig *config;
1546 AspellDictInfoList *dlist;
1547 AspellDictInfoEnumeration *dels;
1548 const AspellDictInfo *entry;
1550 if (!gtkaspellcheckers)
1551 gtkaspell_checkers_init();
1553 if (gtkaspellcheckers->dictionary_list && !refresh)
1554 return gtkaspellcheckers->dictionary_list;
1556 gtkaspell_free_dictionary_list(
1557 gtkaspellcheckers->dictionary_list);
1560 config = new_aspell_config();
1562 aspell_config_replace(config, "rem-all-word-list-path", "");
1563 if (aspell_config_error_number(config) != 0) {
1564 gtkaspellcheckers->error_message = g_strdup(
1565 aspell_config_error_message(config));
1566 gtkaspellcheckers->dictionary_list =
1567 create_empty_dictionary_list();
1569 return gtkaspellcheckers->dictionary_list;
1572 aspell_config_replace(config, "dict-dir", aspell_path);
1573 if (aspell_config_error_number(config) != 0) {
1574 gtkaspellcheckers->error_message = g_strdup(
1575 aspell_config_error_message(config));
1576 gtkaspellcheckers->dictionary_list =
1577 create_empty_dictionary_list();
1579 return gtkaspellcheckers->dictionary_list;
1582 dlist = get_aspell_dict_info_list(config);
1583 delete_aspell_config(config);
1585 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1586 dels = aspell_dict_info_list_elements(dlist);
1587 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1589 dict = g_new0(Dictionary, 1);
1590 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1592 dict->dictname = dict->fullname + strlen(aspell_path);
1593 dict->encoding = g_strdup(entry->code);
1594 debug_print("Aspell: found dictionary %s %s\n", dict->fullname,
1596 list = g_slist_insert_sorted(list, dict,
1597 (GCompareFunc) compare_dict);
1600 delete_aspell_dict_info_enumeration(dels);
1604 debug_print("Aspell: error when searching for dictionaries: "
1605 "No dictionary found.\n");
1606 list = create_empty_dictionary_list();
1609 gtkaspellcheckers->dictionary_list = list;
1614 void gtkaspell_free_dictionary_list(GSList *list)
1618 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1620 dict = (Dictionary *) walk->data;
1621 dictionary_delete(dict);
1626 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1628 GSList *dict_list, *tmp;
1633 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1634 g_return_val_if_fail(dict_list, NULL);
1636 menu = gtk_menu_new();
1638 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1639 dict = (Dictionary *) tmp->data;
1640 item = gtk_menu_item_new_with_label(dict->dictname);
1641 g_object_set_data(G_OBJECT(item), "dict_name",
1644 gtk_menu_append(GTK_MENU(menu), item);
1645 gtk_widget_show(item);
1648 gtk_widget_show(menu);
1653 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1655 GtkWidget *menuitem;
1656 gchar *dict_fullname;
1659 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1661 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1662 dict_fullname = (gchar *) g_object_get_data(G_OBJECT(menuitem),
1664 g_return_val_if_fail(dict_fullname, NULL);
1666 label = g_strdup(dict_fullname);
1672 gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
1673 const gchar *dictionary)
1678 g_return_val_if_fail(menu != NULL, 0);
1679 g_return_val_if_fail(dictionary != NULL, 0);
1680 g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
1683 for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
1684 GTK_OPTION_MENU(menu)))->children;
1685 cur != NULL; cur = cur->next) {
1686 GtkWidget *menuitem;
1689 menuitem = GTK_WIDGET(cur->data);
1690 dict_name = g_object_get_data(G_OBJECT(menuitem),
1692 if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
1693 gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
1703 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1708 menu = gtk_menu_new();
1709 gtk_widget_show(menu);
1711 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1712 gtk_widget_show(item);
1713 gtk_menu_append(GTK_MENU(menu), item);
1714 g_object_set_data(G_OBJECT(item), "sugmode",
1715 GINT_TO_POINTER(ASPELL_FASTMODE));
1717 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1718 gtk_widget_show(item);
1719 gtk_menu_append(GTK_MENU(menu), item);
1720 g_object_set_data(G_OBJECT(item), "sugmode",
1721 GINT_TO_POINTER(ASPELL_NORMALMODE));
1723 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1724 gtk_widget_show(item);
1725 gtk_menu_append(GTK_MENU(menu), item);
1726 g_object_set_data(G_OBJECT(item), "sugmode",
1727 GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1732 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1734 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1736 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1737 sugmode == ASPELL_NORMALMODE ||
1738 sugmode == ASPELL_BADSPELLERMODE);
1740 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1743 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1748 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1750 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1752 sugmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
1758 static void use_alternate_dict(GtkAspell *gtkaspell)
1762 tmp = gtkaspell->gtkaspeller;
1763 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1764 gtkaspell->alternate_speller = tmp;
1766 if (gtkaspell->config_menu)
1767 populate_submenu(gtkaspell, gtkaspell->config_menu);
1770 static void popup_menu(GtkAspell *gtkaspell, GdkEventButton *eb)
1772 GtkTextView * gtktext;
1774 gtktext = gtkaspell->gtktext;
1776 gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
1778 if (!(eb->state & GDK_SHIFT_MASK)) {
1779 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
1781 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1783 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
1784 gtk_menu_popup(make_sug_menu(gtkaspell),
1785 NULL, NULL, NULL, NULL,
1786 eb->button, GDK_CURRENT_TIME);
1791 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1794 gtk_menu_popup(make_config_menu(gtkaspell), NULL, NULL, NULL, NULL,
1795 eb->button, GDK_CURRENT_TIME);
1798 static gboolean aspell_key_pressed(GtkWidget *widget,
1800 GtkAspell *gtkaspell)
1802 if (event && isascii(event->keyval)) {
1803 gtk_accel_groups_activate(gtkaspell->parent_window,
1804 event->keyval, event->state);
1809 /* make_sug_menu() - Add menus to accept this word for this session
1810 * and to add it to personal dictionary
1812 static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
1814 GtkWidget *menu, *item;
1815 unsigned char *caption;
1816 GtkTextView *gtktext;
1817 GtkAccelGroup *accel;
1818 GList *l = gtkaspell->suggestions_list;
1821 gtktext = gtkaspell->gtktext;
1823 accel = gtk_accel_group_new();
1824 menu = gtk_menu_new();
1826 if (gtkaspell->sug_menu)
1827 gtk_widget_destroy(gtkaspell->sug_menu);
1829 gtkaspell->sug_menu = menu;
1831 g_signal_connect(G_OBJECT(menu), "cancel",
1832 G_CALLBACK(cancel_menu_cb), gtkaspell);
1834 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1835 (unsigned char*) l->data,
1836 gtkaspell->gtkaspeller->dictionary->dictname);
1837 item = gtk_menu_item_new_with_label(caption);
1838 gtk_widget_show(item);
1839 gtk_menu_append(GTK_MENU(menu), item);
1840 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1843 item = gtk_menu_item_new();
1844 gtk_widget_show(item);
1845 gtk_menu_append(GTK_MENU(menu), item);
1847 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1848 gtk_widget_show(item);
1849 gtk_menu_append(GTK_MENU(menu), item);
1850 g_signal_connect(G_OBJECT(item), "activate",
1851 G_CALLBACK(add_word_to_session_cb),
1853 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1855 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1857 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1858 gtk_widget_show(item);
1859 gtk_menu_append(GTK_MENU(menu), item);
1860 g_signal_connect(G_OBJECT(item), "activate",
1861 G_CALLBACK(add_word_to_personal_cb),
1863 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1865 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1867 item = gtk_menu_item_new_with_label(_("Replace with..."));
1868 gtk_widget_show(item);
1869 gtk_menu_append(GTK_MENU(menu), item);
1870 g_signal_connect(G_OBJECT(item), "activate",
1871 G_CALLBACK(replace_with_create_dialog_cb),
1873 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1874 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1876 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1877 caption = g_strdup_printf(_("Check with %s"),
1878 gtkaspell->alternate_speller->dictionary->dictname);
1879 item = gtk_menu_item_new_with_label(caption);
1881 gtk_widget_show(item);
1882 gtk_menu_append(GTK_MENU(menu), item);
1883 g_signal_connect(G_OBJECT(item), "activate",
1884 G_CALLBACK(check_with_alternate_cb),
1886 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1887 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1890 item = gtk_menu_item_new();
1891 gtk_widget_show(item);
1892 gtk_menu_append(GTK_MENU(menu), item);
1896 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1897 gtk_widget_show(item);
1898 gtk_menu_append(GTK_MENU(menu), item);
1900 GtkWidget *curmenu = menu;
1904 if (count == MENUCOUNT) {
1907 item = gtk_menu_item_new_with_label(_("More..."));
1908 gtk_widget_show(item);
1909 gtk_menu_append(GTK_MENU(curmenu), item);
1911 curmenu = gtk_menu_new();
1912 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
1916 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
1917 conv_get_locale_charset_str(),
1919 item = gtk_menu_item_new_with_label(utf8buf);
1920 gtk_widget_show(item);
1921 gtk_menu_append(GTK_MENU(curmenu), item);
1922 g_signal_connect(G_OBJECT(item), "activate",
1923 G_CALLBACK(replace_word_cb),
1926 if (curmenu == menu && count < MENUCOUNT) {
1927 gtk_widget_add_accelerator(item, "activate",
1932 gtk_widget_add_accelerator(item, "activate",
1941 } while ((l = l->next) != NULL);
1944 gtk_window_add_accel_group
1945 (GTK_WINDOW(gtkaspell->parent_window),
1947 g_signal_connect(G_OBJECT(menu),
1949 G_CALLBACK(aspell_key_pressed), gtkaspell);
1951 return GTK_MENU(menu);
1954 static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
1956 GtkWidget *item, *submenu;
1958 GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
1960 if (GTK_MENU_SHELL(menu)->children) {
1961 GList *amenu, *alist;
1962 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
1963 alist = amenu->next;
1964 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1969 dictname = g_strdup_printf(_("Dictionary: %s"),
1970 gtkaspeller->dictionary->dictname);
1971 item = gtk_menu_item_new_with_label(dictname);
1972 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1974 gtk_widget_show(item);
1975 gtk_menu_append(GTK_MENU(menu), item);
1977 item = gtk_menu_item_new();
1978 gtk_widget_show(item);
1979 gtk_menu_append(GTK_MENU(menu), item);
1981 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1982 dictname = g_strdup_printf(_("Use alternate (%s)"),
1983 gtkaspell->alternate_speller->dictionary->dictname);
1984 item = gtk_menu_item_new_with_label(dictname);
1986 g_signal_connect(G_OBJECT(item), "activate",
1987 G_CALLBACK(switch_to_alternate_cb),
1989 gtk_widget_show(item);
1990 gtk_menu_append(GTK_MENU(menu), item);
1993 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
1994 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
1995 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1996 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1998 g_signal_connect(G_OBJECT(item), "activate",
1999 G_CALLBACK(set_sug_mode_cb),
2001 gtk_widget_show(item);
2002 gtk_menu_append(GTK_MENU(menu), item);
2004 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
2005 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
2006 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2007 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2009 g_signal_connect(G_OBJECT(item), "activate",
2010 G_CALLBACK(set_sug_mode_cb),
2012 gtk_widget_show(item);
2013 gtk_menu_append(GTK_MENU(menu),item);
2015 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
2016 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
2017 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2018 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2020 g_signal_connect(G_OBJECT(item), "activate",
2021 G_CALLBACK(set_sug_mode_cb),
2023 gtk_widget_show(item);
2024 gtk_menu_append(GTK_MENU(menu), item);
2026 item = gtk_menu_item_new();
2027 gtk_widget_show(item);
2028 gtk_menu_append(GTK_MENU(menu), item);
2030 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
2031 if (gtkaspell->check_while_typing)
2032 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2034 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2035 g_signal_connect(G_OBJECT(item), "activate",
2036 G_CALLBACK(toggle_check_while_typing_cb),
2038 gtk_widget_show(item);
2039 gtk_menu_append(GTK_MENU(menu), item);
2041 item = gtk_menu_item_new();
2042 gtk_widget_show(item);
2043 gtk_menu_append(GTK_MENU(menu), item);
2045 submenu = gtk_menu_new();
2046 item = gtk_menu_item_new_with_label(_("Change dictionary"));
2047 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
2048 gtk_widget_show(item);
2049 gtk_menu_append(GTK_MENU(menu), item);
2052 if (gtkaspellcheckers->dictionary_list == NULL)
2053 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
2055 GtkWidget * curmenu = submenu;
2059 tmp = gtkaspellcheckers->dictionary_list;
2061 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
2062 tmp = g_slist_next(tmp)) {
2063 if (count == MENUCOUNT) {
2066 newmenu = gtk_menu_new();
2067 item = gtk_menu_item_new_with_label(_("More..."));
2068 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2071 gtk_menu_append(GTK_MENU(curmenu), item);
2072 gtk_widget_show(item);
2076 dict = (Dictionary *) tmp->data;
2077 item = gtk_check_menu_item_new_with_label(dict->dictname);
2078 g_object_set_data(G_OBJECT(item), "dict_name",
2080 if (strcmp2(dict->fullname,
2081 gtkaspell->gtkaspeller->dictionary->fullname))
2082 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2084 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2085 gtk_widget_set_sensitive(GTK_WIDGET(item),
2088 g_signal_connect(G_OBJECT(item), "activate",
2089 G_CALLBACK(change_dict_cb),
2091 gtk_widget_show(item);
2092 gtk_menu_append(GTK_MENU(curmenu), item);
2099 static GtkMenu *make_config_menu(GtkAspell *gtkaspell)
2101 if (!gtkaspell->popup_config_menu)
2102 gtkaspell->popup_config_menu = gtk_menu_new();
2104 debug_print("Aspell: creating/using popup_config_menu %0x\n",
2105 (guint) gtkaspell->popup_config_menu);
2106 populate_submenu(gtkaspell, gtkaspell->popup_config_menu);
2108 return GTK_MENU(gtkaspell->popup_config_menu);
2111 void gtkaspell_populate_submenu(GtkAspell *gtkaspell, GtkWidget *menuitem)
2115 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2117 debug_print("Aspell: using config menu %0x\n",
2118 (guint) gtkaspell->popup_config_menu);
2119 populate_submenu(gtkaspell, menu);
2121 gtkaspell->config_menu = menu;
2125 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
2126 gboolean *push_in, gpointer data)
2128 GtkAspell *gtkaspell = (GtkAspell *) data;
2129 gint xx = 0, yy = 0;
2132 GtkTextView *text = GTK_TEXT_VIEW(gtkaspell->gtktext);
2133 GtkTextBuffer *textbuf;
2138 textbuf = gtk_text_view_get_buffer(gtkaspell->gtktext);
2139 gtk_text_buffer_get_iter_at_mark(textbuf, &iter,
2140 gtk_text_buffer_get_insert(textbuf));
2141 gtk_text_view_get_iter_location(gtkaspell->gtktext, &iter, &rect);
2142 gtk_text_view_buffer_to_window_coords(text, GTK_TEXT_WINDOW_TEXT,
2146 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2148 sx = gdk_screen_width();
2149 sy = gdk_screen_height();
2151 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2157 gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
2159 *y = rect.y + rect.height + yy;
2165 gdk_string_height(gtk_style_get_font(
2166 GTK_WIDGET(text)->style),
2167 gtkaspell->theword);
2170 /* Menu call backs */
2172 static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
2174 GtkAspell *gtkaspell = (GtkAspell *) data;
2176 gtkaspell->continue_check = NULL;
2177 set_point_continue(gtkaspell);
2182 gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
2185 GtkAspeller *gtkaspeller;
2188 g_return_val_if_fail(gtkaspell, FALSE);
2189 g_return_val_if_fail(dictionary, FALSE);
2191 sug_mode = gtkaspell->default_sug_mode;
2193 dict = g_new0(Dictionary, 1);
2194 dict->fullname = g_strdup(dictionary);
2195 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2197 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2198 dict == gtkaspell->alternate_speller->dictionary) {
2199 use_alternate_dict(gtkaspell);
2200 dictionary_delete(dict);
2204 gtkaspeller = gtkaspeller_new(dict);
2208 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2209 gtkaspellcheckers->error_message);
2211 gtkaspell_alert_dialog(message);
2214 if (gtkaspell->use_alternate) {
2215 if (gtkaspell->alternate_speller)
2216 gtkaspeller_delete(gtkaspell->alternate_speller);
2217 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2219 gtkaspeller_delete(gtkaspell->gtkaspeller);
2221 gtkaspell->gtkaspeller = gtkaspeller;
2222 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2225 dictionary_delete(dict);
2227 if (gtkaspell->config_menu)
2228 populate_submenu(gtkaspell, gtkaspell->config_menu);
2233 /* change_dict_cb() - Menu callback : change dict */
2234 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2238 fullname = (gchar *) g_object_get_data(G_OBJECT(w), "dict_name");
2240 if (!strcmp2(fullname, _("None")))
2243 gtkaspell_change_dict(gtkaspell, fullname);
2246 static void switch_to_alternate_cb(GtkWidget *w,
2249 GtkAspell *gtkaspell = (GtkAspell *) data;
2250 use_alternate_dict(gtkaspell);
2253 /* Misc. helper functions */
2255 static void set_point_continue(GtkAspell *gtkaspell)
2257 GtkTextView *gtktext;
2259 gtktext = gtkaspell->gtktext;
2261 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
2263 if (gtkaspell->continue_check)
2264 gtkaspell->continue_check((gpointer *) gtkaspell);
2267 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2269 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtkaspell->gtktext);
2270 GdkColor *color = &(gtkaspell->highlight);
2272 /* Shameless copy from Sylpheed's gtkutils.c */
2274 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2276 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2278 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2281 gtk_text_buffer_create_tag(buffer, "misspelled",
2282 "foreground-gdk", color, NULL);
2285 static void change_color(GtkAspell * gtkaspell,
2286 gint start, gint end,
2290 GtkTextView *gtktext;
2291 GtkTextBuffer *buffer;
2292 GtkTextIter startiter, enditer;
2294 g_return_if_fail(start < end);
2296 gtktext = gtkaspell->gtktext;
2298 buffer = gtk_text_view_get_buffer(gtktext);
2299 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, start);
2300 gtk_text_buffer_get_iter_at_offset(buffer, &enditer, end);
2302 gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
2303 &startiter, &enditer);
2305 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
2306 &startiter, &enditer);
2309 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2310 * as needed by aspell. Returns an allocated string.
2313 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2315 guchar * aspell_encoding;
2317 if (strstr2(encoding, "ISO-8859-")) {
2318 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2321 if (!strcmp2(encoding, "US-ASCII"))
2322 aspell_encoding = g_strdup("iso8859-1");
2324 aspell_encoding = g_strdup(encoding);
2327 return aspell_encoding;
2330 /* compare_dict () - compare 2 dict names */
2331 static gint compare_dict(Dictionary *a, Dictionary *b)
2333 guint aparts = 0, bparts = 0;
2336 for (i=0; i < strlen(a->dictname); i++)
2337 if (a->dictname[i] == '-')
2339 for (i=0; i < strlen(b->dictname); i++)
2340 if (b->dictname[i] == '-')
2343 if (aparts != bparts)
2344 return (aparts < bparts) ? -1 : +1;
2347 compare = strcmp2(a->dictname, b->dictname);
2349 compare = strcmp2(a->fullname, b->fullname);
2354 static void dictionary_delete(Dictionary *dict)
2356 g_free(dict->fullname);
2357 g_free(dict->encoding);
2361 static Dictionary *dictionary_dup(const Dictionary *dict)
2365 dict2 = g_new(Dictionary, 1);
2367 dict2->fullname = g_strdup(dict->fullname);
2368 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2369 dict2->encoding = g_strdup(dict->encoding);
2374 static void free_suggestions_list(GtkAspell *gtkaspell)
2378 for (list = gtkaspell->suggestions_list; list != NULL;
2384 gtkaspell->max_sug = -1;
2385 gtkaspell->suggestions_list = NULL;
2388 static void reset_theword_data(GtkAspell *gtkaspell)
2390 gtkaspell->start_pos = 0;
2391 gtkaspell->end_pos = 0;
2392 gtkaspell->theword[0] = 0;
2393 gtkaspell->max_sug = -1;
2395 free_suggestions_list(gtkaspell);
2398 static void free_checkers(gpointer elt, gpointer data)
2400 GtkAspeller *gtkaspeller = elt;
2402 g_return_if_fail(gtkaspeller);
2404 gtkaspeller_real_delete(gtkaspeller);
2407 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2409 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2410 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2412 if (a && b && a->fullname && b->fullname &&
2413 strcmp(a->fullname, b->fullname) == 0 &&
2414 a->encoding && b->encoding)
2415 return strcmp(a->encoding, b->encoding);
2420 static void gtkaspell_alert_dialog(gchar *message)
2425 GtkWidget *ok_button;
2427 dialog = gtk_dialog_new();
2428 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
2429 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
2430 g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
2431 G_CALLBACK(gtk_widget_destroy),
2434 label = gtk_label_new(message);
2435 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
2437 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
2439 hbox = gtk_hbox_new(FALSE, 0);
2441 ok_button = gtk_button_new_with_label(_("OK"));
2442 GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
2443 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
2445 g_signal_connect_swapped(G_OBJECT(ok_button), "clicked",
2446 G_CALLBACK(gtk_widget_destroy),
2448 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
2450 gtk_widget_grab_default(ok_button);
2451 gtk_widget_grab_focus(ok_button);
2452 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
2454 gtk_widget_show_all(dialog);