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>
51 #include <gtk/gtkoptionmenu.h>
52 #include <gtk/gtkmenu.h>
53 #include <gtk/gtkmenuitem.h>
54 #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;
132 gint default_sug_mode;
134 GList *suggestions_list;
136 GtkTextView *gtktext;
140 typedef AspellConfig GtkAspellConfig;
142 /******************************************************************************/
144 static GtkAspellCheckers *gtkaspellcheckers;
146 /* Error message storage */
147 static void gtkaspell_checkers_error_message (gchar *message);
150 static void entry_insert_cb (GtkTextBuffer *textbuf,
154 GtkAspell *gtkaspell);
155 static void entry_delete_cb (GtkTextBuffer *textbuf,
156 GtkTextIter *startiter,
157 GtkTextIter *enditer,
158 GtkAspell *gtkaspell);
159 static gint button_press_intercept_cb (GtkTextView *gtktext,
161 GtkAspell *gtkaspell);
163 /* Checker creation */
164 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
165 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
166 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
167 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
169 /* Checker configuration */
170 static gint set_dictionary (AspellConfig *config,
172 static void set_sug_mode_cb (GtkMenuItem *w,
173 GtkAspell *gtkaspell);
174 static void set_real_sug_mode (GtkAspell *gtkaspell,
175 const char *themode);
177 /* Checker actions */
178 static gboolean check_at (GtkAspell *gtkaspell,
180 static gboolean check_next_prev (GtkAspell *gtkaspell,
182 static GList* misspelled_suggest (GtkAspell *gtkaspell,
184 static void add_word_to_session_cb (GtkWidget *w,
186 static void add_word_to_personal_cb (GtkWidget *w,
188 static void replace_with_create_dialog_cb (GtkWidget *w,
190 static void replace_with_supplied_word_cb (GtkWidget *w,
191 GtkAspell *gtkaspell);
192 static void replace_word_cb (GtkWidget *w,
194 static void replace_real_word (GtkAspell *gtkaspell,
196 static void check_with_alternate_cb (GtkWidget *w,
198 static void use_alternate_dict (GtkAspell *gtkaspell);
199 static void toggle_check_while_typing_cb (GtkWidget *w,
203 static void popup_menu (GtkAspell *gtkaspell,
205 static GtkMenu* make_sug_menu (GtkAspell *gtkaspell);
206 static void populate_submenu (GtkAspell *gtkaspell,
208 static GtkMenu* make_config_menu (GtkAspell *gtkaspell);
209 static void set_menu_pos (GtkMenu *menu,
214 /* Other menu callbacks */
215 static gboolean cancel_menu_cb (GtkMenuShell *w,
217 static void change_dict_cb (GtkWidget *w,
218 GtkAspell *gtkaspell);
219 static void switch_to_alternate_cb (GtkWidget *w,
222 /* Misc. helper functions */
223 static void set_point_continue (GtkAspell *gtkaspell);
224 static void continue_check (gpointer *gtkaspell);
225 static gboolean iswordsep (unsigned char c);
226 static guchar get_text_index_whar (GtkAspell *gtkaspell,
228 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
234 static void allocate_color (GtkAspell *gtkaspell,
236 static void change_color (GtkAspell *gtkaspell,
241 static guchar* convert_to_aspell_encoding (const guchar *encoding);
242 static gint compare_dict (Dictionary *a,
244 static void dictionary_delete (Dictionary *dict);
245 static Dictionary * dictionary_dup (const Dictionary *dict);
246 static void free_suggestions_list (GtkAspell *gtkaspell);
247 static void reset_theword_data (GtkAspell *gtkaspell);
248 static void free_checkers (gpointer elt,
250 static gint find_gtkaspeller (gconstpointer aa,
252 static void gtkaspell_alert_dialog (gchar *message);
253 /* gtkspellconfig - only one config per session */
254 GtkAspellConfig * gtkaspellconfig;
256 /******************************************************************************/
257 static gint get_textview_buffer_charcount(GtkTextView *view);
258 static gint get_textview_buffer_position(GtkTextView *view);
260 static gint get_textview_buffer_charcount(GtkTextView *view)
262 GtkTextBuffer *buffer;
264 g_return_val_if_fail(view, 0);
266 buffer = gtk_text_view_get_buffer(view);
267 g_return_val_if_fail(buffer, 0);
269 return gtk_text_buffer_get_char_count(buffer);
271 static gint get_textview_buffer_offset(GtkTextView *view)
273 GtkTextBuffer * buffer;
277 g_return_val_if_fail(view, 0);
279 buffer = gtk_text_view_get_buffer(view);
280 g_return_val_if_fail(buffer, 0);
282 mark = gtk_text_buffer_get_insert(buffer);
283 g_return_val_if_fail(mark, 0);
285 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
287 return gtk_text_iter_get_offset(&iter);
289 static void set_textview_buffer_offset(GtkTextView *view, gint offset)
291 GtkTextBuffer *buffer;
294 g_return_if_fail(view);
296 buffer = gtk_text_view_get_buffer(view);
297 g_return_if_fail(buffer);
299 gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
300 gtk_text_buffer_place_cursor(buffer, &iter);
302 /******************************************************************************/
304 void gtkaspell_checkers_init(void)
306 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
307 gtkaspellcheckers->checkers = NULL;
308 gtkaspellcheckers->dictionary_list = NULL;
309 gtkaspellcheckers->error_message = NULL;
312 void gtkaspell_checkers_quit(void)
317 if (gtkaspellcheckers == NULL)
320 if ((checkers = gtkaspellcheckers->checkers)) {
321 debug_print("Aspell: number of running checkers to delete %d\n",
322 g_slist_length(checkers));
324 g_slist_foreach(checkers, free_checkers, NULL);
325 g_slist_free(checkers);
328 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
329 debug_print("Aspell: number of dictionaries to delete %d\n",
330 g_slist_length(dict_list));
332 gtkaspell_free_dictionary_list(dict_list);
333 gtkaspellcheckers->dictionary_list = NULL;
336 g_free(gtkaspellcheckers->error_message);
341 static void gtkaspell_checkers_error_message (gchar *message)
344 if (gtkaspellcheckers->error_message) {
345 tmp = g_strdup_printf("%s\n%s",
346 gtkaspellcheckers->error_message,
349 g_free(gtkaspellcheckers->error_message);
350 gtkaspellcheckers->error_message = tmp;
352 gtkaspellcheckers->error_message = message;
355 const char *gtkaspell_checkers_strerror(void)
357 g_return_val_if_fail(gtkaspellcheckers, "");
358 return gtkaspellcheckers->error_message;
361 void gtkaspell_checkers_reset_error(void)
363 g_return_if_fail(gtkaspellcheckers);
365 g_free(gtkaspellcheckers->error_message);
367 gtkaspellcheckers->error_message = NULL;
370 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
371 const gchar *dictionary,
372 const gchar *encoding,
373 gint misspelled_color,
374 gboolean check_while_typing,
375 gboolean use_alternate,
376 GtkTextView *gtktext)
379 GtkAspell *gtkaspell;
380 GtkAspeller *gtkaspeller;
381 GtkTextBuffer *buffer;
383 g_return_val_if_fail(gtktext, NULL);
384 buffer = gtk_text_view_get_buffer(gtktext);
386 dict = g_new0(Dictionary, 1);
387 dict->fullname = g_strdup(dictionary);
388 dict->encoding = g_strdup(encoding);
390 gtkaspeller = gtkaspeller_new(dict);
391 dictionary_delete(dict);
396 gtkaspell = g_new0(GtkAspell, 1);
398 gtkaspell->dictionary_path = g_strdup(dictionary_path);
400 gtkaspell->gtkaspeller = gtkaspeller;
401 gtkaspell->alternate_speller = NULL;
402 gtkaspell->theword[0] = 0x00;
403 gtkaspell->start_pos = 0;
404 gtkaspell->end_pos = 0;
405 gtkaspell->orig_pos = -1;
406 gtkaspell->end_check_pos = -1;
407 gtkaspell->misspelled = -1;
408 gtkaspell->check_while_typing = check_while_typing;
409 gtkaspell->continue_check = NULL;
410 gtkaspell->config_menu = NULL;
411 gtkaspell->popup_config_menu = NULL;
412 gtkaspell->sug_menu = NULL;
413 gtkaspell->replace_entry = NULL;
414 gtkaspell->gtktext = gtktext;
415 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
416 gtkaspell->max_sug = -1;
417 gtkaspell->suggestions_list = NULL;
418 gtkaspell->use_alternate = use_alternate;
420 allocate_color(gtkaspell, misspelled_color);
422 g_signal_connect_after(G_OBJECT(buffer), "insert-text",
423 G_CALLBACK(entry_insert_cb), gtkaspell);
424 g_signal_connect_after(G_OBJECT(buffer), "delete-range",
425 GTK_SIGNAL_FUNC(entry_delete_cb), gtkaspell);
426 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
427 GTK_SIGNAL_FUNC(button_press_intercept_cb),
430 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
435 void gtkaspell_delete(GtkAspell * gtkaspell)
437 GtkTextView *gtktext = gtkaspell->gtktext;
439 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
440 G_CALLBACK(entry_insert_cb),
442 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
443 G_CALLBACK(entry_delete_cb),
445 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
446 GTK_SIGNAL_FUNC(button_press_intercept_cb),
449 gtkaspell_uncheck_all(gtkaspell);
451 gtkaspeller_delete(gtkaspell->gtkaspeller);
453 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
454 gtkaspeller_delete(gtkaspell->alternate_speller);
456 if (gtkaspell->sug_menu)
457 gtk_widget_destroy(gtkaspell->sug_menu);
459 if (gtkaspell->popup_config_menu)
460 gtk_widget_destroy(gtkaspell->popup_config_menu);
462 if (gtkaspell->config_menu)
463 gtk_widget_destroy(gtkaspell->config_menu);
465 if (gtkaspell->suggestions_list)
466 free_suggestions_list(gtkaspell);
468 g_free((gchar *)gtkaspell->dictionary_path);
470 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
477 static void entry_insert_cb(GtkTextBuffer *textbuf,
481 GtkAspell *gtkaspell)
486 g_return_if_fail(gtkaspell->gtkaspeller->checker);
488 if (!gtkaspell->check_while_typing)
491 pos = gtk_text_iter_get_offset(iter);
493 if (iswordsep(newtext[0])) {
494 /* did we just end a word? */
496 check_at(gtkaspell, pos - 2);
498 /* did we just split a word? */
499 if (pos < gtk_text_buffer_get_char_count(textbuf))
500 check_at(gtkaspell, pos + 1);
502 /* check as they type, *except* if they're typing at the end (the most
505 if (pos < gtk_text_buffer_get_char_count(textbuf) &&
506 !iswordsep(get_text_index_whar(gtkaspell, pos))) {
507 check_at(gtkaspell, pos - 1);
511 gtk_text_buffer_get_iter_at_offset(textbuf, iter, pos);
512 gtk_text_buffer_place_cursor(textbuf, iter);
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 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
571 GTK_SIGNAL_FUNC(button_press_intercept_cb),
573 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
575 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-release-event",
577 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
578 GTK_SIGNAL_FUNC(button_press_intercept_cb),
580 gtk_signal_emit_stop_by_name(GTK_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, size);
760 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
762 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
764 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
765 if (dict->encoding) {
768 aspell_enc = convert_to_aspell_encoding (dict->encoding);
769 aspell_config_replace(config, "encoding",
770 (const char *) aspell_enc);
773 RETURN_FALSE_IF_CONFIG_ERROR();
779 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
782 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
783 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
785 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
788 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
793 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
794 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
796 dict = gtkaspell->gtkaspeller->dictionary;
797 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
802 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
803 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
807 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
809 set_real_sug_mode(gtkaspell, themode);
811 if (gtkaspell->config_menu)
812 populate_submenu(gtkaspell, gtkaspell->config_menu);
815 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
818 gint mode = ASPELL_FASTMODE;
819 g_return_if_fail(gtkaspell);
820 g_return_if_fail(gtkaspell->gtkaspeller);
821 g_return_if_fail(themode);
823 if (!strcmp(themode,_("Normal Mode")))
824 mode = ASPELL_NORMALMODE;
825 else if (!strcmp( themode,_("Bad Spellers Mode")))
826 mode = ASPELL_BADSPELLERMODE;
828 result = gtkaspell_set_sug_mode(gtkaspell, mode);
831 debug_print("Aspell: error while changing suggestion mode:%s\n",
832 gtkaspellcheckers->error_message);
833 gtkaspell_checkers_reset_error();
837 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
838 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
840 AspellConfig *config;
842 g_return_val_if_fail(gtkaspell, FALSE);
843 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
844 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
846 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
847 (guint) gtkaspell->gtkaspeller, themode);
849 config = gtkaspell->gtkaspeller->config;
852 case ASPELL_FASTMODE:
853 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
855 case ASPELL_NORMALMODE:
856 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
858 case ASPELL_BADSPELLERMODE:
859 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
863 gtkaspellcheckers->error_message =
864 g_strdup(_("Unknown suggestion mode."));
868 gtkaspell->gtkaspeller->sug_mode = themode;
869 gtkaspell->default_sug_mode = themode;
874 /* misspelled_suggest() - Create a suggestion list for word */
875 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
877 const guchar *newword;
879 const AspellWordList *suggestions;
880 AspellStringEnumeration *elements;
882 g_return_val_if_fail(word, NULL);
884 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
885 free_suggestions_list(gtkaspell);
887 suggestions = aspell_speller_suggest(
888 gtkaspell->gtkaspeller->checker,
889 (const char *)word, -1);
890 elements = aspell_word_list_elements(suggestions);
891 list = g_list_append(list, g_strdup(word));
893 while (newword = aspell_string_enumeration_next(elements))
894 list = g_list_append(list, g_strdup(newword));
896 gtkaspell->max_sug = g_list_length(list) - 1;
897 gtkaspell->suggestions_list = list;
902 free_suggestions_list(gtkaspell);
907 /* misspelled_test() - Just test if word is correctly spelled */
908 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
910 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
915 static gboolean iswordsep(unsigned char c)
917 return !isalpha(c) && c != '\'';
920 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
922 GtkTextView *view = gtkaspell->gtktext;
923 GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
924 GtkTextIter start, end;
925 const gchar *utf8chars;
928 gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
929 gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
931 utf8chars = gtk_text_iter_get_text(&start, &end);
932 a = utf8chars ? utf8chars[0] : 0 ;
937 /* get_word_from_pos () - return the word pointed to. */
938 /* Handles correctly the quotes. */
939 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
940 unsigned char* buf, gint buflen,
941 gint *pstart, gint *pend)
944 /* TODO : when correcting a word into quotes, change the color of */
945 /* the quotes too, as may be they were highlighted before. To do */
946 /* this, we can use two others pointers that points to the whole */
947 /* word including quotes. */
953 GtkTextView *gtktext;
955 gtktext = gtkaspell->gtktext;
956 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
959 /* The apostrophe character is somtimes used for quotes
960 * So include it in the word only if it is not surrounded
961 * by other characters.
964 for (start = pos; start >= 0; --start) {
965 c = get_text_index_whar(gtkaspell, start);
968 if (!isalpha(get_text_index_whar(gtkaspell,
970 /* start_quote = TRUE; */
975 /* start_quote = TRUE; */
979 else if (!isalpha(c))
985 for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
986 c = get_text_index_whar(gtkaspell, end);
988 if (end < get_textview_buffer_charcount(gtktext)) {
989 if (!isalpha(get_text_index_whar(gtkaspell,
991 /* end_quote = TRUE; */
996 /* end_quote = TRUE; */
1000 else if(!isalpha(c))
1010 if (end - start < buflen) {
1011 for (pos = start; pos < end; pos++)
1013 get_text_index_whar(gtkaspell, pos);
1014 buf[pos - start] = 0;
1022 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
1025 unsigned char buf[GTKASPELLWORDSIZE];
1026 GtkTextView *gtktext;
1028 g_return_val_if_fail(from_pos >= 0, FALSE);
1030 gtktext = gtkaspell->gtktext;
1032 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
1036 if (misspelled_test(gtkaspell, buf)) {
1037 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
1038 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
1039 gtkaspell->start_pos = start;
1040 gtkaspell->end_pos = end;
1041 free_suggestions_list(gtkaspell);
1043 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
1046 change_color(gtkaspell, start, end, buf, NULL);
1051 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1057 gboolean misspelled;
1060 maxpos = gtkaspell->end_check_pos;
1068 pos = get_textview_buffer_offset(gtkaspell->gtktext);
1069 gtkaspell->orig_pos = pos;
1070 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1071 pos > minpos && pos <= maxpos)
1073 while (!(misspelled = check_at(gtkaspell, pos)) &&
1074 pos > minpos && pos <= maxpos) {
1076 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1077 pos > minpos && pos <= maxpos)
1080 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1081 pos > minpos && pos <= maxpos)
1085 misspelled_suggest(gtkaspell, gtkaspell->theword);
1088 gtkaspell->orig_pos = gtkaspell->end_pos;
1090 set_textview_buffer_offset(gtkaspell->gtktext,
1091 gtkaspell->end_pos);
1092 /* scroll line to window center */
1093 gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
1094 gtk_text_buffer_get_insert(
1095 gtk_text_view_get_buffer(gtkaspell->gtktext)),
1096 0.0, TRUE, 0.0, 0.5);
1097 /* let textview recalculate coordinates (set_menu_pos) */
1098 while (gtk_events_pending ())
1099 gtk_main_iteration ();
1101 gtk_menu_popup(make_sug_menu(gtkaspell), NULL, NULL,
1102 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1105 reset_theword_data(gtkaspell);
1107 gtkaspell_alert_dialog(_("No misspelled word found."));
1108 set_textview_buffer_offset(gtkaspell->gtktext,
1109 gtkaspell->orig_pos);
1115 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1117 gtkaspell->continue_check = NULL;
1118 gtkaspell->end_check_pos =
1119 get_textview_buffer_charcount(gtkaspell->gtktext);
1120 check_next_prev(gtkaspell, FALSE);
1123 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1126 gtkaspell->continue_check = NULL;
1127 gtkaspell->end_check_pos =
1128 get_textview_buffer_charcount(gtkaspell->gtktext);
1129 check_next_prev(gtkaspell, TRUE);
1132 void gtkaspell_check_all(GtkAspell *gtkaspell)
1134 GtkTextView *gtktext;
1136 GtkTextBuffer *buffer;
1137 GtkTextIter startiter, enditer;
1139 g_return_if_fail(gtkaspell);
1140 g_return_if_fail(gtkaspell->gtktext);
1142 gtktext = gtkaspell->gtktext;
1143 buffer = gtk_text_view_get_buffer(gtktext);
1144 gtk_text_buffer_get_selection_bounds(buffer, &startiter, &enditer);
1145 start = gtk_text_iter_get_offset(&startiter);
1146 end = gtk_text_iter_get_offset(&enditer);
1150 end = gtk_text_buffer_get_char_count(buffer);
1151 } else if (start > end) {
1159 set_textview_buffer_offset(gtktext, start);
1161 gtkaspell->continue_check = continue_check;
1162 gtkaspell->end_check_pos = end;
1164 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1167 static void continue_check(gpointer *data)
1169 GtkAspell *gtkaspell = (GtkAspell *) data;
1170 gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
1171 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1172 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1174 gtkaspell->continue_check = NULL;
1177 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1182 GtkTextView *gtktext;
1184 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1186 gtktext = gtkaspell->gtktext;
1188 len = get_textview_buffer_charcount(gtktext);
1190 origpos = get_textview_buffer_offset(gtktext);
1194 iswordsep(get_text_index_whar(gtkaspell, pos)))
1197 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1200 check_at(gtkaspell, pos - 1);
1202 set_textview_buffer_offset(gtktext, origpos);
1205 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1207 unsigned char *newword;
1208 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1210 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1213 if (strcmp(newword, gtkaspell->theword)) {
1214 replace_real_word(gtkaspell, newword);
1216 if ((e->type == GDK_KEY_PRESS &&
1217 ((GdkEventKey *) e)->state & GDK_MOD1_MASK)) {
1218 aspell_speller_store_replacement(
1219 gtkaspell->gtkaspeller->checker,
1220 gtkaspell->theword, -1,
1223 gtkaspell->replace_entry = NULL;
1228 set_point_continue(gtkaspell);
1232 static void replace_word_cb(GtkWidget *w, gpointer data)
1234 unsigned char *newword;
1235 GtkAspell *gtkaspell = (GtkAspell *) data;
1236 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1238 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
1240 replace_real_word(gtkaspell, newword);
1242 if ((e->type == GDK_KEY_PRESS &&
1243 ((GdkEventKey *) e)->state & GDK_MOD1_MASK) ||
1244 (e->type == GDK_BUTTON_RELEASE &&
1245 ((GdkEventButton *) e)->state & GDK_MOD1_MASK)) {
1246 aspell_speller_store_replacement(
1247 gtkaspell->gtkaspeller->checker,
1248 gtkaspell->theword, -1,
1252 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1254 set_point_continue(gtkaspell);
1257 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1259 int oldlen, newlen, wordlen;
1262 gint start = gtkaspell->start_pos;
1263 GtkTextView *gtktext;
1264 GtkTextBuffer *textbuf;
1265 GtkTextIter startiter, enditer;
1267 if (!newword) return;
1269 gtktext = gtkaspell->gtktext;
1270 textbuf = gtk_text_view_get_buffer(gtktext);
1272 origpos = gtkaspell->orig_pos;
1274 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1275 wordlen = strlen(gtkaspell->theword);
1277 newlen = strlen(newword); /* FIXME: multybyte characters? */
1279 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1280 G_CALLBACK(entry_insert_cb),
1282 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1283 G_CALLBACK(entry_delete_cb),
1286 gtk_text_buffer_get_iter_at_offset(textbuf, &startiter,
1287 gtkaspell->start_pos);
1288 gtk_text_buffer_get_iter_at_offset(textbuf, &enditer,
1289 gtkaspell->end_pos);
1290 g_signal_emit_by_name(G_OBJECT(textbuf), "delete-range",
1291 &startiter, &enditer, gtkaspell);
1292 g_signal_emit_by_name(G_OBJECT(textbuf), "insert-text",
1293 &startiter, newword, newlen, gtkaspell);
1295 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1296 G_CALLBACK(entry_insert_cb),
1298 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1299 G_CALLBACK(entry_delete_cb),
1302 /* Put the point and the position where we clicked with the mouse
1303 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1304 * to let it update correctly the word insertion and then the
1305 * point & position position. If not, SEGV after the first replacement
1306 * If the new word ends before point, put the point at its end.
1309 if (origpos - gtkaspell->start_pos < oldlen &&
1310 origpos - gtkaspell->start_pos >= 0) {
1311 /* Original point was in the word.
1312 * Let it there unless point is going to be outside of the word
1314 if (origpos - gtkaspell->start_pos >= newlen) {
1315 pos = gtkaspell->start_pos + newlen;
1318 else if (origpos >= gtkaspell->end_pos) {
1319 /* move the position according to the change of length */
1320 pos = origpos + newlen - oldlen;
1323 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1325 if (get_textview_buffer_charcount(gtktext) < pos)
1326 pos = get_textview_buffer_charcount(gtktext);
1327 gtkaspell->orig_pos = pos;
1329 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1332 /* Accept this word for this session */
1333 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1336 GtkTextView *gtktext;
1337 GtkAspell *gtkaspell = (GtkAspell *) data;
1338 gtktext = gtkaspell->gtktext;
1340 pos = get_textview_buffer_offset(gtktext);
1342 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1344 strlen(gtkaspell->theword));
1346 check_at(gtkaspell, gtkaspell->start_pos);
1348 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1350 set_point_continue(gtkaspell);
1353 /* add_word_to_personal_cb() - add word to personal dict. */
1354 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1356 GtkAspell *gtkaspell = (GtkAspell *) data;
1357 GtkTextView *gtktext = gtkaspell->gtktext;
1359 aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
1361 strlen(gtkaspell->theword));
1363 check_at(gtkaspell, gtkaspell->start_pos);
1365 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1366 set_point_continue(gtkaspell);
1369 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1371 GtkAspell *gtkaspell = (GtkAspell *) data;
1374 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1376 use_alternate_dict(gtkaspell);
1377 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1379 if (!gtkaspell->continue_check) {
1381 gtkaspell->misspelled = misspelled;
1383 if (gtkaspell->misspelled) {
1385 misspelled_suggest(gtkaspell, gtkaspell->theword);
1387 set_textview_buffer_offset(gtkaspell->gtktext,
1388 gtkaspell->end_pos);
1390 gtk_menu_popup(make_sug_menu(gtkaspell), NULL, NULL,
1391 set_menu_pos, gtkaspell, 0,
1396 gtkaspell->orig_pos = gtkaspell->start_pos;
1398 set_point_continue(gtkaspell);
1401 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1407 GtkWidget *ok_button;
1408 GtkWidget *cancel_button;
1411 GtkAspell *gtkaspell = (GtkAspell *) data;
1413 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1415 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1417 dialog = gtk_dialog_new();
1419 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
1420 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1421 gtk_widget_set_uposition(dialog, xx, yy);
1423 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
1424 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1425 GTK_OBJECT(dialog));
1427 hbox = gtk_hbox_new(FALSE, 0);
1428 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
1430 thelabel = g_strdup_printf(_("Replace \"%s\" with: "),
1431 gtkaspell->theword);
1432 label = gtk_label_new(thelabel);
1434 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1436 entry = gtk_entry_new();
1437 gtkaspell->replace_entry = entry;
1438 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1439 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1440 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1441 GTK_SIGNAL_FUNC(replace_with_supplied_word_cb),
1443 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1444 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1445 GTK_OBJECT(dialog));
1446 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1448 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE,
1450 label = gtk_label_new(_("Holding down MOD1 key while pressing "
1451 "Enter\nwill learn from mistake.\n"));
1452 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1453 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1454 gtk_misc_set_padding(GTK_MISC(label), 8, 0);
1455 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label,
1458 hbox = gtk_hbox_new(TRUE, 0);
1460 ok_button = gtk_button_new_with_label(_("OK"));
1461 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
1462 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
1463 GTK_SIGNAL_FUNC(replace_with_supplied_word_cb),
1465 gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
1466 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1467 GTK_OBJECT(dialog));
1469 cancel_button = gtk_button_new_with_label(_("Cancel"));
1470 gtk_box_pack_start(GTK_BOX(hbox), cancel_button, TRUE, TRUE, 8);
1471 gtk_signal_connect_object(GTK_OBJECT(cancel_button), "clicked",
1472 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1473 GTK_OBJECT(dialog));
1475 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1477 gtk_widget_grab_focus(entry);
1479 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1481 gtk_widget_show_all(dialog);
1484 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1489 GtkTextView *gtktext;
1490 GtkTextBuffer *buffer;
1491 GtkTextIter startiter, enditer;
1493 gtktext = gtkaspell->gtktext;
1495 buffer = gtk_text_view_get_buffer(gtktext);
1496 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
1497 gtk_text_buffer_get_iter_at_offset(buffer, &enditer,
1498 get_textview_buffer_charcount(gtktext)-1);
1499 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
1500 &startiter, &enditer);
1503 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1505 GtkAspell *gtkaspell = (GtkAspell *) data;
1507 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1509 if (!gtkaspell->check_while_typing)
1510 gtkaspell_uncheck_all(gtkaspell);
1512 if (gtkaspell->config_menu)
1513 populate_submenu(gtkaspell, gtkaspell->config_menu);
1516 static GSList *create_empty_dictionary_list(void)
1518 GSList *list = NULL;
1521 dict = g_new0(Dictionary, 1);
1522 dict->fullname = g_strdup(_("None"));
1523 dict->dictname = dict->fullname;
1524 dict->encoding = NULL;
1526 return g_slist_append(list, dict);
1529 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1530 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1534 AspellConfig *config;
1535 AspellDictInfoList *dlist;
1536 AspellDictInfoEnumeration *dels;
1537 const AspellDictInfo *entry;
1539 if (!gtkaspellcheckers)
1540 gtkaspell_checkers_init();
1542 if (gtkaspellcheckers->dictionary_list && !refresh)
1543 return gtkaspellcheckers->dictionary_list;
1545 gtkaspell_free_dictionary_list(
1546 gtkaspellcheckers->dictionary_list);
1549 config = new_aspell_config();
1551 aspell_config_replace(config, "rem-all-word-list-path", "");
1552 if (aspell_config_error_number(config) != 0) {
1553 gtkaspellcheckers->error_message = g_strdup(
1554 aspell_config_error_message(config));
1555 gtkaspellcheckers->dictionary_list =
1556 create_empty_dictionary_list();
1558 return gtkaspellcheckers->dictionary_list;
1561 aspell_config_replace(config, "dict-dir", aspell_path);
1562 if (aspell_config_error_number(config) != 0) {
1563 gtkaspellcheckers->error_message = g_strdup(
1564 aspell_config_error_message(config));
1565 gtkaspellcheckers->dictionary_list =
1566 create_empty_dictionary_list();
1568 return gtkaspellcheckers->dictionary_list;
1571 dlist = get_aspell_dict_info_list(config);
1572 delete_aspell_config(config);
1574 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1575 dels = aspell_dict_info_list_elements(dlist);
1576 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1578 dict = g_new0(Dictionary, 1);
1579 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1581 dict->dictname = dict->fullname + strlen(aspell_path);
1582 dict->encoding = g_strdup(entry->code);
1583 debug_print("Aspell: found dictionary %s %s\n", dict->fullname,
1585 list = g_slist_insert_sorted(list, dict,
1586 (GCompareFunc) compare_dict);
1589 delete_aspell_dict_info_enumeration(dels);
1593 debug_print("Aspell: error when searching for dictionaries: "
1594 "No dictionary found.\n");
1595 list = create_empty_dictionary_list();
1598 gtkaspellcheckers->dictionary_list = list;
1603 void gtkaspell_free_dictionary_list(GSList *list)
1607 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1609 dict = (Dictionary *) walk->data;
1610 dictionary_delete(dict);
1615 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1617 GSList *dict_list, *tmp;
1622 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1623 g_return_val_if_fail(dict_list, NULL);
1625 menu = gtk_menu_new();
1627 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1628 dict = (Dictionary *) tmp->data;
1629 item = gtk_menu_item_new_with_label(dict->dictname);
1630 gtk_object_set_data(GTK_OBJECT(item), "dict_name",
1633 gtk_menu_append(GTK_MENU(menu), item);
1634 gtk_widget_show(item);
1637 gtk_widget_show(menu);
1642 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1644 GtkWidget *menuitem;
1645 gchar *dict_fullname;
1648 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1650 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1651 dict_fullname = (gchar *) gtk_object_get_data(GTK_OBJECT(menuitem),
1653 g_return_val_if_fail(dict_fullname, NULL);
1655 label = g_strdup(dict_fullname);
1661 gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
1662 const gchar *dictionary)
1667 g_return_val_if_fail(menu != NULL, 0);
1668 g_return_val_if_fail(dictionary != NULL, 0);
1669 g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
1672 for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
1673 GTK_OPTION_MENU(menu)))->children;
1674 cur != NULL; cur = cur->next) {
1675 GtkWidget *menuitem;
1678 menuitem = GTK_WIDGET(cur->data);
1679 dict_name = gtk_object_get_data(GTK_OBJECT(menuitem),
1681 if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
1682 gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
1692 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1697 menu = gtk_menu_new();
1698 gtk_widget_show(menu);
1700 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1701 gtk_widget_show(item);
1702 gtk_menu_append(GTK_MENU(menu), item);
1703 gtk_object_set_data(GTK_OBJECT(item), "sugmode",
1704 GINT_TO_POINTER(ASPELL_FASTMODE));
1706 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1707 gtk_widget_show(item);
1708 gtk_menu_append(GTK_MENU(menu), item);
1709 gtk_object_set_data(GTK_OBJECT(item), "sugmode",
1710 GINT_TO_POINTER(ASPELL_NORMALMODE));
1712 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1713 gtk_widget_show(item);
1714 gtk_menu_append(GTK_MENU(menu), item);
1715 gtk_object_set_data(GTK_OBJECT(item), "sugmode",
1716 GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1721 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1723 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1725 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1726 sugmode == ASPELL_NORMALMODE ||
1727 sugmode == ASPELL_BADSPELLERMODE);
1729 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1732 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1737 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1739 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1741 sugmode = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item),
1747 static void use_alternate_dict(GtkAspell *gtkaspell)
1751 tmp = gtkaspell->gtkaspeller;
1752 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1753 gtkaspell->alternate_speller = tmp;
1755 if (gtkaspell->config_menu)
1756 populate_submenu(gtkaspell, gtkaspell->config_menu);
1759 static void popup_menu(GtkAspell *gtkaspell, GdkEventButton *eb)
1761 GtkTextView * gtktext;
1763 gtktext = gtkaspell->gtktext;
1765 gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
1767 if (!(eb->state & GDK_SHIFT_MASK)) {
1768 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
1770 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1772 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
1773 gtk_menu_popup(make_sug_menu(gtkaspell),
1774 NULL, NULL, NULL, NULL,
1775 eb->button, GDK_CURRENT_TIME);
1780 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1783 gtk_menu_popup(make_config_menu(gtkaspell), NULL, NULL, NULL, NULL,
1784 eb->button, GDK_CURRENT_TIME);
1787 /* make_sug_menu() - Add menus to accept this word for this session
1788 * and to add it to personal dictionary
1790 static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
1792 GtkWidget *menu, *item;
1793 unsigned char *caption;
1794 GtkTextView *gtktext;
1795 GtkAccelGroup *accel;
1796 GList *l = gtkaspell->suggestions_list;
1799 gtktext = gtkaspell->gtktext;
1801 accel = gtk_accel_group_new();
1802 menu = gtk_menu_new();
1804 if (gtkaspell->sug_menu)
1805 gtk_widget_destroy(gtkaspell->sug_menu);
1807 gtkaspell->sug_menu = menu;
1809 gtk_signal_connect(GTK_OBJECT(menu), "cancel",
1810 GTK_SIGNAL_FUNC(cancel_menu_cb), gtkaspell);
1812 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1813 (unsigned char*) l->data,
1814 gtkaspell->gtkaspeller->dictionary->dictname);
1815 item = gtk_menu_item_new_with_label(caption);
1816 gtk_widget_show(item);
1817 gtk_menu_append(GTK_MENU(menu), item);
1818 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1821 item = gtk_menu_item_new();
1822 gtk_widget_show(item);
1823 gtk_menu_append(GTK_MENU(menu), item);
1825 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1826 gtk_widget_show(item);
1827 gtk_menu_append(GTK_MENU(menu), item);
1828 gtk_signal_connect(GTK_OBJECT(item), "activate",
1829 GTK_SIGNAL_FUNC(add_word_to_session_cb),
1831 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1833 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1835 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1836 gtk_widget_show(item);
1837 gtk_menu_append(GTK_MENU(menu), item);
1838 gtk_signal_connect(GTK_OBJECT(item), "activate",
1839 GTK_SIGNAL_FUNC(add_word_to_personal_cb),
1841 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1843 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1845 item = gtk_menu_item_new_with_label(_("Replace with..."));
1846 gtk_widget_show(item);
1847 gtk_menu_append(GTK_MENU(menu), item);
1848 gtk_signal_connect(GTK_OBJECT(item), "activate",
1849 GTK_SIGNAL_FUNC(replace_with_create_dialog_cb),
1851 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1852 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1854 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1855 caption = g_strdup_printf(_("Check with %s"),
1856 gtkaspell->alternate_speller->dictionary->dictname);
1857 item = gtk_menu_item_new_with_label(caption);
1859 gtk_widget_show(item);
1860 gtk_menu_append(GTK_MENU(menu), item);
1861 gtk_signal_connect(GTK_OBJECT(item), "activate",
1862 GTK_SIGNAL_FUNC(check_with_alternate_cb),
1864 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1865 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1868 item = gtk_menu_item_new();
1869 gtk_widget_show(item);
1870 gtk_menu_append(GTK_MENU(menu), item);
1874 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1875 gtk_widget_show(item);
1876 gtk_menu_append(GTK_MENU(menu), item);
1878 GtkWidget *curmenu = menu;
1882 if (count == MENUCOUNT) {
1885 item = gtk_menu_item_new_with_label(_("More..."));
1886 gtk_widget_show(item);
1887 gtk_menu_append(GTK_MENU(curmenu), item);
1889 curmenu = gtk_menu_new();
1890 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
1894 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
1895 conv_get_current_charset_str(),
1897 item = gtk_menu_item_new_with_label(utf8buf);
1898 gtk_widget_show(item);
1899 gtk_menu_append(GTK_MENU(curmenu), item);
1900 gtk_signal_connect(GTK_OBJECT(item), "activate",
1901 GTK_SIGNAL_FUNC(replace_word_cb),
1904 if (curmenu == menu && count < MENUCOUNT) {
1905 gtk_widget_add_accelerator(item, "activate",
1910 gtk_widget_add_accelerator(item, "activate",
1919 } while ((l = l->next) != NULL);
1923 #warning GTK2 set accelerators for speller popup
1927 gtk_accel_group_attach(accel, GTK_OBJECT(menu));
1928 gtk_accel_group_unref(accel);
1931 return GTK_MENU(menu);
1934 static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
1936 GtkWidget *item, *submenu;
1938 GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
1940 if (GTK_MENU_SHELL(menu)->children) {
1941 GList *amenu, *alist;
1942 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
1943 alist = amenu->next;
1944 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1949 dictname = g_strdup_printf(_("Dictionary: %s"),
1950 gtkaspeller->dictionary->dictname);
1951 item = gtk_menu_item_new_with_label(dictname);
1952 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1954 gtk_widget_show(item);
1955 gtk_menu_append(GTK_MENU(menu), item);
1957 item = gtk_menu_item_new();
1958 gtk_widget_show(item);
1959 gtk_menu_append(GTK_MENU(menu), item);
1961 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1962 dictname = g_strdup_printf(_("Use alternate (%s)"),
1963 gtkaspell->alternate_speller->dictionary->dictname);
1964 item = gtk_menu_item_new_with_label(dictname);
1966 gtk_signal_connect(GTK_OBJECT(item), "activate",
1967 GTK_SIGNAL_FUNC(switch_to_alternate_cb),
1969 gtk_widget_show(item);
1970 gtk_menu_append(GTK_MENU(menu), item);
1973 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
1974 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
1975 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1976 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1978 gtk_signal_connect(GTK_OBJECT(item), "activate",
1979 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1981 gtk_widget_show(item);
1982 gtk_menu_append(GTK_MENU(menu), item);
1984 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
1985 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
1986 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1987 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1989 gtk_signal_connect(GTK_OBJECT(item), "activate",
1990 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1992 gtk_widget_show(item);
1993 gtk_menu_append(GTK_MENU(menu),item);
1995 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
1996 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
1997 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1998 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2000 gtk_signal_connect(GTK_OBJECT(item), "activate",
2001 GTK_SIGNAL_FUNC(set_sug_mode_cb),
2003 gtk_widget_show(item);
2004 gtk_menu_append(GTK_MENU(menu), item);
2006 item = gtk_menu_item_new();
2007 gtk_widget_show(item);
2008 gtk_menu_append(GTK_MENU(menu), item);
2010 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
2011 if (gtkaspell->check_while_typing)
2012 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2014 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2015 gtk_signal_connect(GTK_OBJECT(item), "activate",
2016 GTK_SIGNAL_FUNC(toggle_check_while_typing_cb),
2018 gtk_widget_show(item);
2019 gtk_menu_append(GTK_MENU(menu), item);
2021 item = gtk_menu_item_new();
2022 gtk_widget_show(item);
2023 gtk_menu_append(GTK_MENU(menu), item);
2025 submenu = gtk_menu_new();
2026 item = gtk_menu_item_new_with_label(_("Change dictionary"));
2027 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
2028 gtk_widget_show(item);
2029 gtk_menu_append(GTK_MENU(menu), item);
2032 if (gtkaspellcheckers->dictionary_list == NULL)
2033 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
2035 GtkWidget * curmenu = submenu;
2039 tmp = gtkaspellcheckers->dictionary_list;
2041 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
2042 tmp = g_slist_next(tmp)) {
2043 if (count == MENUCOUNT) {
2046 newmenu = gtk_menu_new();
2047 item = gtk_menu_item_new_with_label(_("More..."));
2048 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2051 gtk_menu_append(GTK_MENU(curmenu), item);
2052 gtk_widget_show(item);
2056 dict = (Dictionary *) tmp->data;
2057 item = gtk_check_menu_item_new_with_label(dict->dictname);
2058 gtk_object_set_data(GTK_OBJECT(item), "dict_name",
2060 if (strcmp2(dict->fullname,
2061 gtkaspell->gtkaspeller->dictionary->fullname))
2062 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2064 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2065 gtk_widget_set_sensitive(GTK_WIDGET(item),
2068 gtk_signal_connect(GTK_OBJECT(item), "activate",
2069 GTK_SIGNAL_FUNC(change_dict_cb),
2071 gtk_widget_show(item);
2072 gtk_menu_append(GTK_MENU(curmenu), item);
2079 static GtkMenu *make_config_menu(GtkAspell *gtkaspell)
2081 if (!gtkaspell->popup_config_menu)
2082 gtkaspell->popup_config_menu = gtk_menu_new();
2084 debug_print("Aspell: creating/using popup_config_menu %0x\n",
2085 (guint) gtkaspell->popup_config_menu);
2086 populate_submenu(gtkaspell, gtkaspell->popup_config_menu);
2088 return GTK_MENU(gtkaspell->popup_config_menu);
2091 void gtkaspell_populate_submenu(GtkAspell *gtkaspell, GtkWidget *menuitem)
2095 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2097 debug_print("Aspell: using config menu %0x\n",
2098 (guint) gtkaspell->popup_config_menu);
2099 populate_submenu(gtkaspell, menu);
2101 gtkaspell->config_menu = menu;
2105 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
2106 gboolean *push_in, gpointer data)
2108 GtkAspell *gtkaspell = (GtkAspell *) data;
2109 gint xx = 0, yy = 0;
2112 GtkTextView *text = GTK_TEXT_VIEW(gtkaspell->gtktext);
2113 GtkTextBuffer *textbuf;
2118 textbuf = gtk_text_view_get_buffer(gtkaspell->gtktext);
2119 gtk_text_buffer_get_iter_at_mark(textbuf, &iter,
2120 gtk_text_buffer_get_insert(textbuf));
2121 gtk_text_view_get_iter_location(gtkaspell->gtktext, &iter, &rect);
2122 gtk_text_view_buffer_to_window_coords(text, GTK_TEXT_WINDOW_TEXT,
2126 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2128 sx = gdk_screen_width();
2129 sy = gdk_screen_height();
2131 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2137 gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
2139 *y = rect.y + rect.height + yy;
2145 gdk_string_height(gtk_style_get_font(
2146 GTK_WIDGET(text)->style),
2147 gtkaspell->theword);
2150 /* Menu call backs */
2152 static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
2154 GtkAspell *gtkaspell = (GtkAspell *) data;
2156 gtkaspell->continue_check = NULL;
2157 set_point_continue(gtkaspell);
2162 gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
2165 GtkAspeller *gtkaspeller;
2168 g_return_val_if_fail(gtkaspell, FALSE);
2169 g_return_val_if_fail(dictionary, FALSE);
2171 sug_mode = gtkaspell->default_sug_mode;
2173 dict = g_new0(Dictionary, 1);
2174 dict->fullname = g_strdup(dictionary);
2175 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2177 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2178 dict == gtkaspell->alternate_speller->dictionary) {
2179 use_alternate_dict(gtkaspell);
2180 dictionary_delete(dict);
2184 gtkaspeller = gtkaspeller_new(dict);
2188 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2189 gtkaspellcheckers->error_message);
2191 gtkaspell_alert_dialog(message);
2194 if (gtkaspell->use_alternate) {
2195 if (gtkaspell->alternate_speller)
2196 gtkaspeller_delete(gtkaspell->alternate_speller);
2197 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2199 gtkaspeller_delete(gtkaspell->gtkaspeller);
2201 gtkaspell->gtkaspeller = gtkaspeller;
2202 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2205 dictionary_delete(dict);
2207 if (gtkaspell->config_menu)
2208 populate_submenu(gtkaspell, gtkaspell->config_menu);
2213 /* change_dict_cb() - Menu callback : change dict */
2214 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2218 fullname = (gchar *) gtk_object_get_data(GTK_OBJECT(w), "dict_name");
2220 if (!strcmp2(fullname, _("None")))
2223 gtkaspell_change_dict(gtkaspell, fullname);
2226 static void switch_to_alternate_cb(GtkWidget *w,
2229 GtkAspell *gtkaspell = (GtkAspell *) data;
2230 use_alternate_dict(gtkaspell);
2233 /* Misc. helper functions */
2235 static void set_point_continue(GtkAspell *gtkaspell)
2237 GtkTextView *gtktext;
2239 gtktext = gtkaspell->gtktext;
2241 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
2243 if (gtkaspell->continue_check)
2244 gtkaspell->continue_check((gpointer *) gtkaspell);
2247 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2249 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtkaspell->gtktext);
2250 GdkColor *color = &(gtkaspell->highlight);
2252 /* Shameless copy from Sylpheed's gtkutils.c */
2254 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2256 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2258 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2261 gtk_text_buffer_create_tag(buffer, "misspelled",
2262 "foreground-gdk", color, NULL);
2265 static void change_color(GtkAspell * gtkaspell,
2266 gint start, gint end,
2270 GtkTextView *gtktext;
2271 GtkTextBuffer *buffer;
2272 GtkTextIter startiter, enditer;
2274 g_return_if_fail(start < end);
2276 gtktext = gtkaspell->gtktext;
2278 buffer = gtk_text_view_get_buffer(gtktext);
2279 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, start);
2280 gtk_text_buffer_get_iter_at_offset(buffer, &enditer, end);
2282 gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
2283 &startiter, &enditer);
2285 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
2286 &startiter, &enditer);
2289 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2290 * as needed by aspell. Returns an allocated string.
2293 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2295 guchar * aspell_encoding;
2297 if (strstr2(encoding, "ISO-8859-")) {
2298 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2301 if (!strcmp2(encoding, "US-ASCII"))
2302 aspell_encoding = g_strdup("iso8859-1");
2304 aspell_encoding = g_strdup(encoding);
2307 return aspell_encoding;
2310 /* compare_dict () - compare 2 dict names */
2311 static gint compare_dict(Dictionary *a, Dictionary *b)
2313 guint aparts = 0, bparts = 0;
2316 for (i=0; i < strlen(a->dictname); i++)
2317 if (a->dictname[i] == '-')
2319 for (i=0; i < strlen(b->dictname); i++)
2320 if (b->dictname[i] == '-')
2323 if (aparts != bparts)
2324 return (aparts < bparts) ? -1 : +1;
2327 compare = strcmp2(a->dictname, b->dictname);
2329 compare = strcmp2(a->fullname, b->fullname);
2334 static void dictionary_delete(Dictionary *dict)
2336 g_free(dict->fullname);
2337 g_free(dict->encoding);
2341 static Dictionary *dictionary_dup(const Dictionary *dict)
2345 dict2 = g_new(Dictionary, 1);
2347 dict2->fullname = g_strdup(dict->fullname);
2348 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2349 dict2->encoding = g_strdup(dict->encoding);
2354 static void free_suggestions_list(GtkAspell *gtkaspell)
2358 for (list = gtkaspell->suggestions_list; list != NULL;
2364 gtkaspell->max_sug = -1;
2365 gtkaspell->suggestions_list = NULL;
2368 static void reset_theword_data(GtkAspell *gtkaspell)
2370 gtkaspell->start_pos = 0;
2371 gtkaspell->end_pos = 0;
2372 gtkaspell->theword[0] = 0;
2373 gtkaspell->max_sug = -1;
2375 free_suggestions_list(gtkaspell);
2378 static void free_checkers(gpointer elt, gpointer data)
2380 GtkAspeller *gtkaspeller = elt;
2382 g_return_if_fail(gtkaspeller);
2384 gtkaspeller_real_delete(gtkaspeller);
2387 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2389 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2390 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2392 if (a && b && a->fullname && b->fullname &&
2393 strcmp(a->fullname, b->fullname) == 0 &&
2394 a->encoding && b->encoding)
2395 return strcmp(a->encoding, b->encoding);
2400 static void gtkaspell_alert_dialog(gchar *message)
2405 GtkWidget *ok_button;
2407 dialog = gtk_dialog_new();
2408 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
2409 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_MOUSE);
2410 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
2411 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2412 GTK_OBJECT(dialog));
2414 label = gtk_label_new(message);
2415 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
2417 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
2419 hbox = gtk_hbox_new(FALSE, 0);
2421 ok_button = gtk_button_new_with_label(_("OK"));
2422 GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
2423 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
2425 gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
2426 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2427 GTK_OBJECT(dialog));
2428 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
2430 gtk_widget_grab_default(ok_button);
2431 gtk_widget_grab_focus(ok_button);
2432 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
2434 gtk_widget_show_all(dialog);