1 /* gtkaspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin (original code for ispell).
3 * Copyright (c) 2002 Melvin Hadasht.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; If not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
21 * Adapted for Sylpheed (Claws) (c) 2001-2002 by Hiroyuki Yamamoto &
22 * The Sylpheed Claws Team.
23 * Adapted for pspell (c) 2001-2002 Melvin Hadasht
24 * Adapted for GNU/aspell (c) 2002 Melvin Hadasht
36 #include <sys/types.h>
48 #include <glib/gi18n.h>
52 #include <gtk/gtkoptionmenu.h>
53 #include <gtk/gtkmenu.h>
54 #include <gtk/gtkmenuitem.h>
55 #include <gdk/gdkkeysyms.h>
61 #include "alertpanel.h"
62 #include "gtkaspell.h"
63 #include "gtk/gtkutils.h"
65 #define ASPELL_FASTMODE 1
66 #define ASPELL_NORMALMODE 2
67 #define ASPELL_BADSPELLERMODE 3
69 #define GTKASPELLWORDSIZE 1024
71 /* size of the text buffer used in various word-processing routines. */
74 /* number of suggestions to display on each menu. */
77 /* 'config' must be defined as a 'AspellConfig *' */
78 #define RETURN_FALSE_IF_CONFIG_ERROR() \
80 if (aspell_config_error_number(config) != 0) { \
81 gtkaspellcheckers->error_message = g_strdup(aspell_config_error_message(config)); \
86 #define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
87 aspell_config_replace(config, option, value); \
88 RETURN_FALSE_IF_CONFIG_ERROR(); \
91 typedef struct _GtkAspellCheckers {
93 GSList *dictionary_list;
97 typedef struct _Dictionary {
103 typedef struct _GtkAspeller {
104 Dictionary *dictionary;
106 AspellConfig *config;
107 AspellSpeller *checker;
110 typedef void (*ContCheckFunc) (gpointer *gtkaspell);
114 GtkAspeller *gtkaspeller;
115 GtkAspeller *alternate_speller;
116 gchar *dictionary_path;
117 gchar theword[GTKASPELLWORDSIZE];
123 gboolean check_while_typing;
124 gboolean use_alternate;
126 ContCheckFunc continue_check;
128 GtkWidget *config_menu;
129 GtkWidget *popup_config_menu;
131 GtkWidget *replace_entry;
132 GtkWidget *parent_window;
134 gint default_sug_mode;
136 GList *suggestions_list;
138 GtkTextView *gtktext;
140 GtkAccelGroup *accel_group;
143 typedef AspellConfig GtkAspellConfig;
145 /******************************************************************************/
147 static GtkAspellCheckers *gtkaspellcheckers;
149 /* Error message storage */
150 static void gtkaspell_checkers_error_message (gchar *message);
153 static void entry_insert_cb (GtkTextBuffer *textbuf,
157 GtkAspell *gtkaspell);
158 static void entry_delete_cb (GtkTextBuffer *textbuf,
159 GtkTextIter *startiter,
160 GtkTextIter *enditer,
161 GtkAspell *gtkaspell);
162 static gint button_press_intercept_cb (GtkTextView *gtktext,
164 GtkAspell *gtkaspell);
166 /* Checker creation */
167 static GtkAspeller* gtkaspeller_new (Dictionary *dict);
168 static GtkAspeller* gtkaspeller_real_new (Dictionary *dict);
169 static GtkAspeller* gtkaspeller_delete (GtkAspeller *gtkaspeller);
170 static GtkAspeller* gtkaspeller_real_delete (GtkAspeller *gtkaspeller);
172 /* Checker configuration */
173 static gint set_dictionary (AspellConfig *config,
175 static void set_sug_mode_cb (GtkMenuItem *w,
176 GtkAspell *gtkaspell);
177 static void set_real_sug_mode (GtkAspell *gtkaspell,
178 const char *themode);
180 /* Checker actions */
181 static gboolean check_at (GtkAspell *gtkaspell,
183 static gboolean check_next_prev (GtkAspell *gtkaspell,
185 static GList* misspelled_suggest (GtkAspell *gtkaspell,
187 static void add_word_to_session_cb (GtkWidget *w,
189 static void add_word_to_personal_cb (GtkWidget *w,
191 static void replace_with_create_dialog_cb (GtkWidget *w,
193 static void replace_with_supplied_word_cb (GtkWidget *w,
194 GtkAspell *gtkaspell);
195 static void replace_word_cb (GtkWidget *w,
197 static void replace_real_word (GtkAspell *gtkaspell,
199 static void check_with_alternate_cb (GtkWidget *w,
201 static void use_alternate_dict (GtkAspell *gtkaspell);
202 static void toggle_check_while_typing_cb (GtkWidget *w,
206 static void popup_menu (GtkAspell *gtkaspell,
208 static GtkMenu* make_sug_menu (GtkAspell *gtkaspell);
209 static void populate_submenu (GtkAspell *gtkaspell,
211 static GtkMenu* make_config_menu (GtkAspell *gtkaspell);
212 static void set_menu_pos (GtkMenu *menu,
217 /* Other menu callbacks */
218 static gboolean cancel_menu_cb (GtkMenuShell *w,
220 static void change_dict_cb (GtkWidget *w,
221 GtkAspell *gtkaspell);
222 static void switch_to_alternate_cb (GtkWidget *w,
225 /* Misc. helper functions */
226 static void set_point_continue (GtkAspell *gtkaspell);
227 static void continue_check (gpointer *gtkaspell);
228 static gboolean iswordsep (unsigned char c);
229 static guchar get_text_index_whar (GtkAspell *gtkaspell,
231 static gboolean get_word_from_pos (GtkAspell *gtkaspell,
237 static void allocate_color (GtkAspell *gtkaspell,
239 static void change_color (GtkAspell *gtkaspell,
244 static guchar* convert_to_aspell_encoding (const guchar *encoding);
245 static gint compare_dict (Dictionary *a,
247 static void dictionary_delete (Dictionary *dict);
248 static Dictionary * dictionary_dup (const Dictionary *dict);
249 static void free_suggestions_list (GtkAspell *gtkaspell);
250 static void reset_theword_data (GtkAspell *gtkaspell);
251 static void free_checkers (gpointer elt,
253 static gint find_gtkaspeller (gconstpointer aa,
255 /* gtkspellconfig - only one config per session */
256 GtkAspellConfig * gtkaspellconfig;
257 static void destroy_menu(GtkWidget *widget, gpointer user_data);
259 /******************************************************************************/
260 static gint get_textview_buffer_charcount(GtkTextView *view);
262 static gint get_textview_buffer_charcount(GtkTextView *view)
264 GtkTextBuffer *buffer;
266 g_return_val_if_fail(view, 0);
268 buffer = gtk_text_view_get_buffer(view);
269 g_return_val_if_fail(buffer, 0);
271 return gtk_text_buffer_get_char_count(buffer);
273 static gint get_textview_buffer_offset(GtkTextView *view)
275 GtkTextBuffer * buffer;
279 g_return_val_if_fail(view, 0);
281 buffer = gtk_text_view_get_buffer(view);
282 g_return_val_if_fail(buffer, 0);
284 mark = gtk_text_buffer_get_insert(buffer);
285 g_return_val_if_fail(mark, 0);
287 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
289 return gtk_text_iter_get_offset(&iter);
291 static void set_textview_buffer_offset(GtkTextView *view, gint offset)
293 GtkTextBuffer *buffer;
296 g_return_if_fail(view);
298 buffer = gtk_text_view_get_buffer(view);
299 g_return_if_fail(buffer);
301 gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
302 gtk_text_buffer_place_cursor(buffer, &iter);
304 /******************************************************************************/
306 void gtkaspell_checkers_init(void)
308 gtkaspellcheckers = g_new(GtkAspellCheckers, 1);
309 gtkaspellcheckers->checkers = NULL;
310 gtkaspellcheckers->dictionary_list = NULL;
311 gtkaspellcheckers->error_message = NULL;
314 void gtkaspell_checkers_quit(void)
319 if (gtkaspellcheckers == NULL)
322 if ((checkers = gtkaspellcheckers->checkers)) {
323 debug_print("Aspell: number of running checkers to delete %d\n",
324 g_slist_length(checkers));
326 g_slist_foreach(checkers, free_checkers, NULL);
327 g_slist_free(checkers);
330 if ((dict_list = gtkaspellcheckers->dictionary_list)) {
331 debug_print("Aspell: number of dictionaries to delete %d\n",
332 g_slist_length(dict_list));
334 gtkaspell_free_dictionary_list(dict_list);
335 gtkaspellcheckers->dictionary_list = NULL;
338 g_free(gtkaspellcheckers->error_message);
343 static void gtkaspell_checkers_error_message (gchar *message)
346 if (gtkaspellcheckers->error_message) {
347 tmp = g_strdup_printf("%s\n%s",
348 gtkaspellcheckers->error_message,
351 g_free(gtkaspellcheckers->error_message);
352 gtkaspellcheckers->error_message = tmp;
354 gtkaspellcheckers->error_message = message;
357 const char *gtkaspell_checkers_strerror(void)
359 g_return_val_if_fail(gtkaspellcheckers, "");
360 return gtkaspellcheckers->error_message;
363 void gtkaspell_checkers_reset_error(void)
365 g_return_if_fail(gtkaspellcheckers);
367 g_free(gtkaspellcheckers->error_message);
369 gtkaspellcheckers->error_message = NULL;
372 GtkAspell *gtkaspell_new(const gchar *dictionary_path,
373 const gchar *dictionary,
374 const gchar *encoding,
375 gint misspelled_color,
376 gboolean check_while_typing,
377 gboolean use_alternate,
378 GtkTextView *gtktext,
379 GtkWindow *parent_win)
382 GtkAspell *gtkaspell;
383 GtkAspeller *gtkaspeller;
384 GtkTextBuffer *buffer;
386 g_return_val_if_fail(gtktext, NULL);
387 buffer = gtk_text_view_get_buffer(gtktext);
389 dict = g_new0(Dictionary, 1);
390 dict->fullname = g_strdup(dictionary);
391 dict->encoding = g_strdup(encoding);
393 gtkaspeller = gtkaspeller_new(dict);
394 dictionary_delete(dict);
399 gtkaspell = g_new0(GtkAspell, 1);
401 gtkaspell->dictionary_path = g_strdup(dictionary_path);
403 gtkaspell->gtkaspeller = gtkaspeller;
404 gtkaspell->alternate_speller = NULL;
405 gtkaspell->theword[0] = 0x00;
406 gtkaspell->start_pos = 0;
407 gtkaspell->end_pos = 0;
408 gtkaspell->orig_pos = -1;
409 gtkaspell->end_check_pos = -1;
410 gtkaspell->misspelled = -1;
411 gtkaspell->check_while_typing = check_while_typing;
412 gtkaspell->continue_check = NULL;
413 gtkaspell->config_menu = NULL;
414 gtkaspell->popup_config_menu = NULL;
415 gtkaspell->sug_menu = NULL;
416 gtkaspell->replace_entry = NULL;
417 gtkaspell->gtktext = gtktext;
418 gtkaspell->default_sug_mode = ASPELL_FASTMODE;
419 gtkaspell->max_sug = -1;
420 gtkaspell->suggestions_list = NULL;
421 gtkaspell->use_alternate = use_alternate;
422 gtkaspell->parent_window = GTK_WIDGET(parent_win);
424 allocate_color(gtkaspell, misspelled_color);
426 g_signal_connect_after(G_OBJECT(buffer), "insert-text",
427 G_CALLBACK(entry_insert_cb), gtkaspell);
428 g_signal_connect_after(G_OBJECT(buffer), "delete-range",
429 G_CALLBACK(entry_delete_cb), gtkaspell);
430 g_signal_connect(G_OBJECT(gtktext), "button-press-event",
431 G_CALLBACK(button_press_intercept_cb),
434 debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
439 void gtkaspell_delete(GtkAspell * gtkaspell)
441 GtkTextView *gtktext = gtkaspell->gtktext;
443 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
444 G_CALLBACK(entry_insert_cb),
446 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
447 G_CALLBACK(entry_delete_cb),
449 g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
450 G_CALLBACK(button_press_intercept_cb),
453 gtkaspell_uncheck_all(gtkaspell);
455 gtkaspeller_delete(gtkaspell->gtkaspeller);
457 if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
458 gtkaspeller_delete(gtkaspell->alternate_speller);
460 if (gtkaspell->sug_menu)
461 gtk_widget_destroy(gtkaspell->sug_menu);
463 if (gtkaspell->popup_config_menu)
464 gtk_widget_destroy(gtkaspell->popup_config_menu);
466 if (gtkaspell->config_menu)
467 gtk_widget_destroy(gtkaspell->config_menu);
469 if (gtkaspell->suggestions_list)
470 free_suggestions_list(gtkaspell);
472 g_free((gchar *)gtkaspell->dictionary_path);
474 debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
481 static void entry_insert_cb(GtkTextBuffer *textbuf,
485 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 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
568 G_CALLBACK(button_press_intercept_cb),
570 g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
573 /* forge the leftclick */
576 g_signal_emit_by_name(G_OBJECT(gtktext), "button-press-event",
578 g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
580 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
581 G_CALLBACK(button_press_intercept_cb),
583 g_signal_stop_emission_by_name(G_OBJECT(gtktext), "button-press-event");
585 /* now do the menu wackiness */
586 popup_menu(gtkaspell, eb);
587 gtk_grab_remove(GTK_WIDGET(gtktext));
591 /* Checker creation */
592 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
595 GtkAspeller *gtkaspeller = NULL;
599 g_return_val_if_fail(gtkaspellcheckers, NULL);
601 g_return_val_if_fail(dictionary, NULL);
603 if (dictionary->fullname == NULL)
604 gtkaspell_checkers_error_message(
605 g_strdup(_("No dictionary selected.")));
607 g_return_val_if_fail(dictionary->fullname, NULL);
609 if (dictionary->dictname == NULL) {
612 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
615 dictionary->dictname = dictionary->fullname;
617 dictionary->dictname = tmp + 1;
620 dict = dictionary_dup(dictionary);
622 tmp = g_new0(GtkAspeller, 1);
623 tmp->dictionary = dict;
625 exist = g_slist_find_custom(gtkaspellcheckers->checkers, tmp,
630 if ((gtkaspeller = gtkaspeller_real_new(dict)) != NULL) {
631 gtkaspellcheckers->checkers = g_slist_append(
632 gtkaspellcheckers->checkers,
635 debug_print("Aspell: Created a new gtkaspeller %0x\n",
638 dictionary_delete(dict);
640 debug_print("Aspell: Could not create spell checker.\n");
643 debug_print("Aspell: number of existing checkers %d\n",
644 g_slist_length(gtkaspellcheckers->checkers));
649 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
651 GtkAspeller *gtkaspeller;
652 AspellConfig *config;
653 AspellCanHaveError *ret;
655 g_return_val_if_fail(gtkaspellcheckers, NULL);
656 g_return_val_if_fail(dict, NULL);
658 gtkaspeller = g_new(GtkAspeller, 1);
660 gtkaspeller->dictionary = dict;
661 gtkaspeller->sug_mode = ASPELL_FASTMODE;
663 config = new_aspell_config();
665 if (!set_dictionary(config, dict))
668 ret = new_aspell_speller(config);
669 delete_aspell_config(config);
671 if (aspell_error_number(ret) != 0) {
672 gtkaspellcheckers->error_message
673 = g_strdup(aspell_error_message(ret));
675 delete_aspell_can_have_error(ret);
680 gtkaspeller->checker = to_aspell_speller(ret);
681 gtkaspeller->config = aspell_speller_config(gtkaspeller->checker);
686 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
688 g_return_val_if_fail(gtkaspellcheckers, NULL);
690 gtkaspellcheckers->checkers =
691 g_slist_remove(gtkaspellcheckers->checkers,
694 debug_print("Aspell: Deleting gtkaspeller %0x.\n",
697 gtkaspeller_real_delete(gtkaspeller);
699 debug_print("Aspell: number of existing checkers %d\n",
700 g_slist_length(gtkaspellcheckers->checkers));
705 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
707 g_return_val_if_fail(gtkaspeller, NULL);
708 g_return_val_if_fail(gtkaspeller->checker, NULL);
710 aspell_speller_save_all_word_lists(gtkaspeller->checker);
712 delete_aspell_speller(gtkaspeller->checker);
714 dictionary_delete(gtkaspeller->dictionary);
716 debug_print("Aspell: gtkaspeller %0x deleted.\n",
724 /*****************************************************************************/
725 /* Checker configuration */
727 static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
729 gchar *language = NULL;
730 gchar *jargon = NULL;
734 g_return_val_if_fail(config, FALSE);
735 g_return_val_if_fail(dict, FALSE);
737 strncpy(buf, dict->fullname, BUFSIZE-1);
738 buf[BUFSIZE-1] = 0x00;
740 buf[dict->dictname - dict->fullname] = 0x00;
742 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
743 debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
745 strncpy(buf, dict->dictname, BUFSIZE-1);
748 if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
753 if ((jargon = strchr(language, '-')) != NULL)
756 if (size != NULL && jargon == size)
759 debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
760 language, jargon ? jargon : "",
764 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
766 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
768 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
769 if (dict->encoding) {
772 aspell_enc = convert_to_aspell_encoding (dict->encoding);
773 aspell_config_replace(config, "encoding",
774 (const char *) aspell_enc);
777 RETURN_FALSE_IF_CONFIG_ERROR();
783 guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
786 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
787 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
789 return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
792 guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
797 g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
798 g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
800 dict = gtkaspell->gtkaspeller->dictionary;
801 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
806 /* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
807 static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
811 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
813 set_real_sug_mode(gtkaspell, themode);
815 if (gtkaspell->config_menu)
816 populate_submenu(gtkaspell, gtkaspell->config_menu);
819 static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
822 gint mode = ASPELL_FASTMODE;
823 g_return_if_fail(gtkaspell);
824 g_return_if_fail(gtkaspell->gtkaspeller);
825 g_return_if_fail(themode);
827 if (!strcmp(themode,_("Normal Mode")))
828 mode = ASPELL_NORMALMODE;
829 else if (!strcmp( themode,_("Bad Spellers Mode")))
830 mode = ASPELL_BADSPELLERMODE;
832 result = gtkaspell_set_sug_mode(gtkaspell, mode);
835 debug_print("Aspell: error while changing suggestion mode:%s\n",
836 gtkaspellcheckers->error_message);
837 gtkaspell_checkers_reset_error();
841 /* gtkaspell_set_sug_mode() - Set the suggestion mode */
842 gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
844 AspellConfig *config;
846 g_return_val_if_fail(gtkaspell, FALSE);
847 g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
848 g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
850 debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
851 (guint) gtkaspell->gtkaspeller, themode);
853 config = gtkaspell->gtkaspeller->config;
856 case ASPELL_FASTMODE:
857 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
859 case ASPELL_NORMALMODE:
860 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
862 case ASPELL_BADSPELLERMODE:
863 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
867 gtkaspellcheckers->error_message =
868 g_strdup(_("Unknown suggestion mode."));
872 gtkaspell->gtkaspeller->sug_mode = themode;
873 gtkaspell->default_sug_mode = themode;
878 /* misspelled_suggest() - Create a suggestion list for word */
879 static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
881 const guchar *newword;
883 const AspellWordList *suggestions;
884 AspellStringEnumeration *elements;
886 g_return_val_if_fail(word, NULL);
888 if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
889 free_suggestions_list(gtkaspell);
891 suggestions = aspell_speller_suggest(
892 gtkaspell->gtkaspeller->checker,
893 (const char *)word, -1);
894 elements = aspell_word_list_elements(suggestions);
895 list = g_list_append(list, g_strdup(word));
897 while (newword = aspell_string_enumeration_next(elements))
898 list = g_list_append(list, g_strdup(newword));
900 gtkaspell->max_sug = g_list_length(list) - 1;
901 gtkaspell->suggestions_list = list;
906 free_suggestions_list(gtkaspell);
911 /* misspelled_test() - Just test if word is correctly spelled */
912 static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
914 return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
919 static gboolean iswordsep(unsigned char c)
921 return !isalpha(c) && c != '\'';
924 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
926 GtkTextView *view = gtkaspell->gtktext;
927 GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
928 GtkTextIter start, end;
929 const gchar *utf8chars;
932 gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
933 gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
935 utf8chars = gtk_text_iter_get_text(&start, &end);
936 if (is_ascii_str(utf8chars)) {
937 a = utf8chars ? utf8chars[0] : '\0' ;
939 gchar *tr = conv_iconv_strdup(utf8chars, CS_UTF_8,
940 gtkaspell->gtkaspeller->dictionary->encoding);
950 /* get_word_from_pos () - return the word pointed to. */
951 /* Handles correctly the quotes. */
952 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
953 unsigned char* buf, gint buflen,
954 gint *pstart, gint *pend)
957 /* TODO : when correcting a word into quotes, change the color of */
958 /* the quotes too, as may be they were highlighted before. To do */
959 /* this, we can use two others pointers that points to the whole */
960 /* word including quotes. */
966 GtkTextView *gtktext;
968 gtktext = gtkaspell->gtktext;
969 if (iswordsep(get_text_index_whar(gtkaspell, pos)))
972 /* The apostrophe character is somtimes used for quotes
973 * So include it in the word only if it is not surrounded
974 * by other characters.
977 for (start = pos; start >= 0; --start) {
978 c = get_text_index_whar(gtkaspell, start);
981 if (!isalpha(get_text_index_whar(gtkaspell,
983 /* start_quote = TRUE; */
988 /* start_quote = TRUE; */
992 else if (!isalpha(c))
998 for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
999 c = get_text_index_whar(gtkaspell, end);
1001 if (end < get_textview_buffer_charcount(gtktext)) {
1002 if (!isalpha(get_text_index_whar(gtkaspell,
1004 /* end_quote = TRUE; */
1009 /* end_quote = TRUE; */
1013 else if(!isalpha(c))
1023 if (end - start < buflen) {
1024 for (pos = start; pos < end; pos++) {
1026 get_text_index_whar(gtkaspell, pos);
1028 buf[pos - start] = 0;
1036 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
1039 unsigned char buf[GTKASPELLWORDSIZE];
1040 GtkTextView *gtktext;
1042 g_return_val_if_fail(from_pos >= 0, FALSE);
1044 gtktext = gtkaspell->gtktext;
1046 if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf),
1050 if (misspelled_test(gtkaspell, buf)) {
1051 strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
1052 gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
1053 gtkaspell->start_pos = start;
1054 gtkaspell->end_pos = end;
1055 free_suggestions_list(gtkaspell);
1057 change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
1060 change_color(gtkaspell, start, end, buf, NULL);
1065 static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
1071 gboolean misspelled;
1074 maxpos = gtkaspell->end_check_pos;
1082 pos = get_textview_buffer_offset(gtkaspell->gtktext);
1083 gtkaspell->orig_pos = pos;
1084 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1085 pos > minpos && pos <= maxpos)
1087 while (!(misspelled = check_at(gtkaspell, pos)) &&
1088 pos > minpos && pos <= maxpos) {
1090 while (!iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1091 pos > minpos && pos <= maxpos)
1094 while (iswordsep(get_text_index_whar(gtkaspell, pos)) &&
1095 pos > minpos && pos <= maxpos)
1099 GtkMenu *menu = NULL;
1100 misspelled_suggest(gtkaspell, gtkaspell->theword);
1103 gtkaspell->orig_pos = gtkaspell->end_pos;
1105 set_textview_buffer_offset(gtkaspell->gtktext,
1106 gtkaspell->end_pos);
1107 /* scroll line to window center */
1108 gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
1109 gtk_text_buffer_get_insert(
1110 gtk_text_view_get_buffer(gtkaspell->gtktext)),
1111 0.0, TRUE, 0.0, 0.5);
1112 /* let textview recalculate coordinates (set_menu_pos) */
1113 while (gtk_events_pending ())
1114 gtk_main_iteration ();
1116 menu = make_sug_menu(gtkaspell);
1117 gtk_menu_popup(menu, NULL, NULL,
1118 set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
1119 g_signal_connect(G_OBJECT(menu), "deactivate",
1120 G_CALLBACK(destroy_menu),
1124 reset_theword_data(gtkaspell);
1126 alertpanel_notice(_("No misspelled word found."));
1127 set_textview_buffer_offset(gtkaspell->gtktext,
1128 gtkaspell->orig_pos);
1134 void gtkaspell_check_backwards(GtkAspell *gtkaspell)
1136 gtkaspell->continue_check = NULL;
1137 gtkaspell->end_check_pos =
1138 get_textview_buffer_charcount(gtkaspell->gtktext);
1139 check_next_prev(gtkaspell, FALSE);
1142 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
1145 gtkaspell->continue_check = NULL;
1146 gtkaspell->end_check_pos =
1147 get_textview_buffer_charcount(gtkaspell->gtktext);
1148 check_next_prev(gtkaspell, TRUE);
1151 void gtkaspell_check_all(GtkAspell *gtkaspell)
1153 GtkTextView *gtktext;
1155 GtkTextBuffer *buffer;
1156 GtkTextIter startiter, enditer;
1158 g_return_if_fail(gtkaspell);
1159 g_return_if_fail(gtkaspell->gtktext);
1161 gtktext = gtkaspell->gtktext;
1162 buffer = gtk_text_view_get_buffer(gtktext);
1163 gtk_text_buffer_get_selection_bounds(buffer, &startiter, &enditer);
1164 start = gtk_text_iter_get_offset(&startiter);
1165 end = gtk_text_iter_get_offset(&enditer);
1169 end = gtk_text_buffer_get_char_count(buffer);
1170 } else if (start > end) {
1178 set_textview_buffer_offset(gtktext, start);
1180 gtkaspell->continue_check = continue_check;
1181 gtkaspell->end_check_pos = end;
1183 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1186 static void continue_check(gpointer *data)
1188 GtkAspell *gtkaspell = (GtkAspell *) data;
1189 gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
1190 if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
1191 gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
1193 gtkaspell->continue_check = NULL;
1196 void gtkaspell_highlight_all(GtkAspell *gtkaspell)
1201 GtkTextView *gtktext;
1203 g_return_if_fail(gtkaspell->gtkaspeller->checker);
1205 gtktext = gtkaspell->gtktext;
1207 len = get_textview_buffer_charcount(gtktext);
1209 origpos = get_textview_buffer_offset(gtktext);
1213 iswordsep(get_text_index_whar(gtkaspell, pos)))
1216 !iswordsep(get_text_index_whar(gtkaspell, pos)))
1219 check_at(gtkaspell, pos - 1);
1221 set_textview_buffer_offset(gtktext, origpos);
1224 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
1226 unsigned char *newword;
1227 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1229 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
1232 if (strcmp(newword, gtkaspell->theword)) {
1233 replace_real_word(gtkaspell, newword);
1235 if ((e->type == GDK_KEY_PRESS &&
1236 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK)) {
1237 aspell_speller_store_replacement(
1238 gtkaspell->gtkaspeller->checker,
1239 gtkaspell->theword, -1,
1242 gtkaspell->replace_entry = NULL;
1247 if (w && GTK_IS_DIALOG(w)) {
1248 gtk_widget_destroy(w);
1251 set_point_continue(gtkaspell);
1255 static void replace_word_cb(GtkWidget *w, gpointer data)
1257 unsigned char *newword;
1258 GtkAspell *gtkaspell = (GtkAspell *) data;
1259 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1261 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
1263 replace_real_word(gtkaspell, newword);
1265 if ((e->type == GDK_KEY_PRESS &&
1266 ((GdkEventKey *) e)->state & GDK_CONTROL_MASK) ||
1267 (e->type == GDK_BUTTON_RELEASE &&
1268 ((GdkEventButton *) e)->state & GDK_CONTROL_MASK)) {
1269 aspell_speller_store_replacement(
1270 gtkaspell->gtkaspeller->checker,
1271 gtkaspell->theword, -1,
1275 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1277 set_point_continue(gtkaspell);
1280 static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
1282 int oldlen, newlen, wordlen;
1285 GtkTextView *gtktext;
1286 GtkTextBuffer *textbuf;
1287 GtkTextIter startiter, enditer;
1289 if (!newword) return;
1291 gtktext = gtkaspell->gtktext;
1292 textbuf = gtk_text_view_get_buffer(gtktext);
1294 origpos = gtkaspell->orig_pos;
1296 oldlen = gtkaspell->end_pos - gtkaspell->start_pos;
1297 wordlen = strlen(gtkaspell->theword);
1299 newlen = strlen(newword); /* FIXME: multybyte characters? */
1301 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1302 G_CALLBACK(entry_insert_cb),
1304 g_signal_handlers_block_by_func(G_OBJECT(gtktext),
1305 G_CALLBACK(entry_delete_cb),
1308 gtk_text_buffer_get_iter_at_offset(textbuf, &startiter,
1309 gtkaspell->start_pos);
1310 gtk_text_buffer_get_iter_at_offset(textbuf, &enditer,
1311 gtkaspell->end_pos);
1312 g_signal_emit_by_name(G_OBJECT(textbuf), "delete-range",
1313 &startiter, &enditer, gtkaspell);
1314 g_signal_emit_by_name(G_OBJECT(textbuf), "insert-text",
1315 &startiter, newword, newlen, gtkaspell);
1317 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1318 G_CALLBACK(entry_insert_cb),
1320 g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
1321 G_CALLBACK(entry_delete_cb),
1324 /* Put the point and the position where we clicked with the mouse
1325 * It seems to be a hack, as I must thaw,freeze,thaw the widget
1326 * to let it update correctly the word insertion and then the
1327 * point & position position. If not, SEGV after the first replacement
1328 * If the new word ends before point, put the point at its end.
1331 if (origpos - gtkaspell->start_pos < oldlen &&
1332 origpos - gtkaspell->start_pos >= 0) {
1333 /* Original point was in the word.
1334 * Let it there unless point is going to be outside of the word
1336 if (origpos - gtkaspell->start_pos >= newlen) {
1337 pos = gtkaspell->start_pos + newlen;
1340 else if (origpos >= gtkaspell->end_pos) {
1341 /* move the position according to the change of length */
1342 pos = origpos + newlen - oldlen;
1345 gtkaspell->end_pos = gtkaspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1347 if (get_textview_buffer_charcount(gtktext) < pos)
1348 pos = get_textview_buffer_charcount(gtktext);
1349 gtkaspell->orig_pos = pos;
1351 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1354 /* Accept this word for this session */
1355 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1358 GtkTextView *gtktext;
1359 GtkAspell *gtkaspell = (GtkAspell *) data;
1360 gtktext = gtkaspell->gtktext;
1362 pos = get_textview_buffer_offset(gtktext);
1364 aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
1366 strlen(gtkaspell->theword));
1368 check_at(gtkaspell, gtkaspell->start_pos);
1370 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1372 set_point_continue(gtkaspell);
1375 /* add_word_to_personal_cb() - add word to personal dict. */
1376 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1378 GtkAspell *gtkaspell = (GtkAspell *) data;
1380 aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
1382 strlen(gtkaspell->theword));
1384 check_at(gtkaspell, gtkaspell->start_pos);
1386 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1387 set_point_continue(gtkaspell);
1390 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
1392 GtkAspell *gtkaspell = (GtkAspell *) data;
1395 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1397 use_alternate_dict(gtkaspell);
1398 misspelled = check_at(gtkaspell, gtkaspell->start_pos);
1400 if (!gtkaspell->continue_check) {
1402 gtkaspell->misspelled = misspelled;
1404 if (gtkaspell->misspelled) {
1406 misspelled_suggest(gtkaspell, gtkaspell->theword);
1408 set_textview_buffer_offset(gtkaspell->gtktext,
1409 gtkaspell->end_pos);
1411 menu = make_sug_menu(gtkaspell);
1412 gtk_menu_popup(menu, NULL, NULL,
1413 set_menu_pos, gtkaspell, 0,
1415 g_signal_connect(G_OBJECT(menu), "deactivate",
1416 G_CALLBACK(destroy_menu),
1421 gtkaspell->orig_pos = gtkaspell->start_pos;
1423 set_point_continue(gtkaspell);
1426 static gboolean replace_key_pressed(GtkWidget *widget,
1428 GtkAspell *gtkaspell)
1430 if (event && event->keyval == GDK_Escape) {
1431 gtk_widget_destroy(widget);
1433 } else if (event && event->keyval == GDK_Return) {
1434 replace_with_supplied_word_cb(widget, gtkaspell);
1440 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1442 static PangoFontDescription *font_desc;
1449 GtkWidget *ok_button;
1450 GtkWidget *cancel_button;
1451 GtkWidget *confirm_area;
1455 GtkAspell *gtkaspell = (GtkAspell *) data;
1457 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1459 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1461 dialog = gtk_dialog_new();
1463 gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
1464 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1465 gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
1466 gtk_widget_set_uposition(dialog, xx, yy);
1468 g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
1469 G_CALLBACK(gtk_widget_destroy),
1472 gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
1473 hbox = gtk_hbox_new (FALSE, 12);
1474 gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
1475 gtk_widget_show (hbox);
1476 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
1479 thelabel = g_strdup_printf(_("<span weight=\"bold\" "
1480 "size=\"larger\">Replace \"%s\" with: </span>"),
1481 gtkaspell->theword);
1482 /* for title label */
1483 w_hbox = gtk_hbox_new(FALSE, 0);
1485 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
1486 GTK_ICON_SIZE_DIALOG);
1487 gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
1488 gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
1490 vbox = gtk_vbox_new (FALSE, 12);
1491 gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
1492 gtk_widget_show (vbox);
1494 label = gtk_label_new(thelabel);
1495 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1496 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1497 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
1498 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1499 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
1503 size = pango_font_description_get_size
1504 (label->style->font_desc);
1505 font_desc = pango_font_description_new();
1506 pango_font_description_set_weight
1507 (font_desc, PANGO_WEIGHT_BOLD);
1508 pango_font_description_set_size
1509 (font_desc, size * PANGO_SCALE_LARGE);
1512 gtk_widget_modify_font(label, font_desc);
1515 entry = gtk_entry_new();
1516 gtkaspell->replace_entry = entry;
1517 gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
1518 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1519 g_signal_connect(G_OBJECT(dialog),
1521 G_CALLBACK(replace_key_pressed), gtkaspell);
1522 gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
1524 label = gtk_label_new(_("Holding down Control key while pressing "
1525 "Enter\nwill learn from mistake.\n"));
1526 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1527 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1528 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1529 gtk_widget_show(label);
1531 hbox = gtk_hbox_new(TRUE, 0);
1533 gtkut_stock_button_set_create(&confirm_area,
1534 &ok_button, GTK_STOCK_OK,
1535 &cancel_button, GTK_STOCK_CANCEL,
1538 gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1539 confirm_area, FALSE, FALSE, 0);
1540 gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
1542 g_signal_connect(G_OBJECT(ok_button), "clicked",
1543 G_CALLBACK(replace_with_supplied_word_cb),
1545 g_signal_connect_swapped(G_OBJECT(ok_button), "clicked",
1546 G_CALLBACK(gtk_widget_destroy),
1549 g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
1550 G_CALLBACK(gtk_widget_destroy),
1553 gtk_widget_grab_focus(entry);
1555 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1557 gtk_widget_show_all(dialog);
1560 void gtkaspell_uncheck_all(GtkAspell * gtkaspell)
1562 GtkTextView *gtktext;
1563 GtkTextBuffer *buffer;
1564 GtkTextIter startiter, enditer;
1566 gtktext = gtkaspell->gtktext;
1568 buffer = gtk_text_view_get_buffer(gtktext);
1569 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, 0);
1570 gtk_text_buffer_get_iter_at_offset(buffer, &enditer,
1571 get_textview_buffer_charcount(gtktext)-1);
1572 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
1573 &startiter, &enditer);
1576 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1578 GtkAspell *gtkaspell = (GtkAspell *) data;
1580 gtkaspell->check_while_typing = gtkaspell->check_while_typing == FALSE;
1582 if (!gtkaspell->check_while_typing)
1583 gtkaspell_uncheck_all(gtkaspell);
1585 if (gtkaspell->config_menu)
1586 populate_submenu(gtkaspell, gtkaspell->config_menu);
1589 static GSList *create_empty_dictionary_list(void)
1591 GSList *list = NULL;
1594 dict = g_new0(Dictionary, 1);
1595 dict->fullname = g_strdup(_("None"));
1596 dict->dictname = dict->fullname;
1597 dict->encoding = NULL;
1599 return g_slist_append(list, dict);
1602 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
1603 GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
1607 AspellConfig *config;
1608 AspellDictInfoList *dlist;
1609 AspellDictInfoEnumeration *dels;
1610 const AspellDictInfo *entry;
1612 if (!gtkaspellcheckers)
1613 gtkaspell_checkers_init();
1615 if (gtkaspellcheckers->dictionary_list && !refresh)
1616 return gtkaspellcheckers->dictionary_list;
1618 gtkaspell_free_dictionary_list(
1619 gtkaspellcheckers->dictionary_list);
1622 config = new_aspell_config();
1624 aspell_config_replace(config, "dict-dir", aspell_path);
1625 if (aspell_config_error_number(config) != 0) {
1626 gtkaspellcheckers->error_message = g_strdup(
1627 aspell_config_error_message(config));
1628 gtkaspellcheckers->dictionary_list =
1629 create_empty_dictionary_list();
1631 return gtkaspellcheckers->dictionary_list;
1634 dlist = get_aspell_dict_info_list(config);
1635 delete_aspell_config(config);
1637 debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
1638 dels = aspell_dict_info_list_elements(dlist);
1639 while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0)
1641 dict = g_new0(Dictionary, 1);
1642 dict->fullname = g_strdup_printf("%s%s", aspell_path,
1644 dict->dictname = dict->fullname + strlen(aspell_path);
1645 dict->encoding = g_strdup(entry->code);
1647 if (g_slist_find_custom(list, dict,
1648 (GCompareFunc) compare_dict) != NULL) {
1649 dictionary_delete(dict);
1653 debug_print("Aspell: found dictionary %s %s %s\n", dict->fullname,
1654 dict->dictname, dict->encoding);
1655 list = g_slist_insert_sorted(list, dict,
1656 (GCompareFunc) compare_dict);
1659 delete_aspell_dict_info_enumeration(dels);
1663 debug_print("Aspell: error when searching for dictionaries: "
1664 "No dictionary found.\n");
1665 list = create_empty_dictionary_list();
1668 gtkaspellcheckers->dictionary_list = list;
1673 void gtkaspell_free_dictionary_list(GSList *list)
1677 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1679 dict = (Dictionary *) walk->data;
1680 dictionary_delete(dict);
1685 GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
1687 GSList *dict_list, *tmp;
1692 dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
1693 g_return_val_if_fail(dict_list, NULL);
1695 menu = gtk_menu_new();
1697 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1698 dict = (Dictionary *) tmp->data;
1699 item = gtk_menu_item_new_with_label(dict->dictname);
1700 g_object_set_data(G_OBJECT(item), "dict_name",
1703 gtk_menu_append(GTK_MENU(menu), item);
1704 gtk_widget_show(item);
1707 gtk_widget_show(menu);
1712 gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
1714 GtkWidget *menuitem;
1715 gchar *dict_fullname;
1718 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1720 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1721 dict_fullname = (gchar *) g_object_get_data(G_OBJECT(menuitem),
1723 g_return_val_if_fail(dict_fullname, NULL);
1725 label = g_strdup(dict_fullname);
1731 gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
1732 const gchar *dictionary)
1737 g_return_val_if_fail(menu != NULL, 0);
1738 g_return_val_if_fail(dictionary != NULL, 0);
1739 g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
1742 for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
1743 GTK_OPTION_MENU(menu)))->children;
1744 cur != NULL; cur = cur->next) {
1745 GtkWidget *menuitem;
1748 menuitem = GTK_WIDGET(cur->data);
1749 dict_name = g_object_get_data(G_OBJECT(menuitem),
1751 if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
1752 gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
1762 GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
1767 menu = gtk_menu_new();
1768 gtk_widget_show(menu);
1770 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1771 gtk_widget_show(item);
1772 gtk_menu_append(GTK_MENU(menu), item);
1773 g_object_set_data(G_OBJECT(item), "sugmode",
1774 GINT_TO_POINTER(ASPELL_FASTMODE));
1776 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1777 gtk_widget_show(item);
1778 gtk_menu_append(GTK_MENU(menu), item);
1779 g_object_set_data(G_OBJECT(item), "sugmode",
1780 GINT_TO_POINTER(ASPELL_NORMALMODE));
1782 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1783 gtk_widget_show(item);
1784 gtk_menu_append(GTK_MENU(menu), item);
1785 g_object_set_data(G_OBJECT(item), "sugmode",
1786 GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
1791 void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1793 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1795 g_return_if_fail(sugmode == ASPELL_FASTMODE ||
1796 sugmode == ASPELL_NORMALMODE ||
1797 sugmode == ASPELL_BADSPELLERMODE);
1799 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1802 gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1807 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1809 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1811 sugmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
1817 static void use_alternate_dict(GtkAspell *gtkaspell)
1821 tmp = gtkaspell->gtkaspeller;
1822 gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
1823 gtkaspell->alternate_speller = tmp;
1825 if (gtkaspell->config_menu)
1826 populate_submenu(gtkaspell, gtkaspell->config_menu);
1829 static void destroy_menu(GtkWidget *widget,
1830 gpointer user_data) {
1832 GtkAspell *gtkaspell = (GtkAspell *)user_data;
1834 if (gtkaspell->accel_group) {
1835 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1836 gtkaspell->accel_group);
1837 gtkaspell->accel_group = NULL;
1841 static void popup_menu(GtkAspell *gtkaspell, GdkEventButton *eb)
1843 GtkTextView * gtktext;
1844 GtkMenu *menu = NULL;
1846 gtktext = gtkaspell->gtktext;
1848 gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
1850 if (!(eb->state & GDK_SHIFT_MASK)) {
1851 if (check_at(gtkaspell, gtkaspell->orig_pos)) {
1853 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1855 if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
1856 menu = make_sug_menu(gtkaspell);
1857 gtk_menu_popup(menu,
1858 NULL, NULL, NULL, NULL,
1859 eb->button, eb->time);
1861 g_signal_connect(G_OBJECT(menu), "deactivate",
1862 G_CALLBACK(destroy_menu),
1867 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
1869 menu = make_config_menu(gtkaspell);
1870 gtk_menu_popup(menu, NULL, NULL, NULL, NULL,
1871 eb->button, eb->time);
1873 g_signal_connect(G_OBJECT(menu), "deactivate",
1874 G_CALLBACK(destroy_menu),
1878 static gboolean aspell_key_pressed(GtkWidget *widget,
1880 GtkAspell *gtkaspell)
1882 if (event && (isascii(event->keyval) || event->keyval == GDK_Return)) {
1883 gtk_accel_groups_activate(
1884 G_OBJECT(gtkaspell->parent_window),
1885 event->keyval, event->state);
1886 } else if (event && event->keyval == GDK_Escape) {
1887 destroy_menu(NULL, gtkaspell);
1892 /* make_sug_menu() - Add menus to accept this word for this session
1893 * and to add it to personal dictionary
1895 static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
1897 GtkWidget *menu, *item;
1898 unsigned char *caption;
1899 GtkTextView *gtktext;
1900 GtkAccelGroup *accel;
1901 GList *l = gtkaspell->suggestions_list;
1904 gtktext = gtkaspell->gtktext;
1906 accel = gtk_accel_group_new();
1907 menu = gtk_menu_new();
1909 if (gtkaspell->sug_menu)
1910 gtk_widget_destroy(gtkaspell->sug_menu);
1912 if (gtkaspell->accel_group) {
1913 gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window),
1914 gtkaspell->accel_group);
1915 gtkaspell->accel_group = NULL;
1918 gtkaspell->sug_menu = menu;
1920 g_signal_connect(G_OBJECT(menu), "cancel",
1921 G_CALLBACK(cancel_menu_cb), gtkaspell);
1923 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1924 (unsigned char*) l->data,
1925 gtkaspell->gtkaspeller->dictionary->dictname);
1926 item = gtk_menu_item_new_with_label(caption);
1927 gtk_widget_show(item);
1928 gtk_menu_append(GTK_MENU(menu), item);
1929 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1932 item = gtk_menu_item_new();
1933 gtk_widget_show(item);
1934 gtk_menu_append(GTK_MENU(menu), item);
1936 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1937 gtk_widget_show(item);
1938 gtk_menu_append(GTK_MENU(menu), item);
1939 g_signal_connect(G_OBJECT(item), "activate",
1940 G_CALLBACK(add_word_to_session_cb),
1942 gtk_widget_add_accelerator(item, "activate", accel, GDK_space,
1944 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1946 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1947 gtk_widget_show(item);
1948 gtk_menu_append(GTK_MENU(menu), item);
1949 g_signal_connect(G_OBJECT(item), "activate",
1950 G_CALLBACK(add_word_to_personal_cb),
1952 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return,
1954 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1956 item = gtk_menu_item_new_with_label(_("Replace with..."));
1957 gtk_widget_show(item);
1958 gtk_menu_append(GTK_MENU(menu), item);
1959 g_signal_connect(G_OBJECT(item), "activate",
1960 G_CALLBACK(replace_with_create_dialog_cb),
1962 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1963 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1964 gtk_widget_add_accelerator(item, "activate", accel, GDK_R,
1968 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
1969 caption = g_strdup_printf(_("Check with %s"),
1970 gtkaspell->alternate_speller->dictionary->dictname);
1971 item = gtk_menu_item_new_with_label(caption);
1973 gtk_widget_show(item);
1974 gtk_menu_append(GTK_MENU(menu), item);
1975 g_signal_connect(G_OBJECT(item), "activate",
1976 G_CALLBACK(check_with_alternate_cb),
1978 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1979 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1980 gtk_widget_add_accelerator(item, "activate", accel, GDK_X,
1985 item = gtk_menu_item_new();
1986 gtk_widget_show(item);
1987 gtk_menu_append(GTK_MENU(menu), item);
1991 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1992 gtk_widget_show(item);
1993 gtk_menu_append(GTK_MENU(menu), item);
1995 GtkWidget *curmenu = menu;
1999 if (count == MENUCOUNT) {
2002 item = gtk_menu_item_new_with_label(_("More..."));
2003 gtk_widget_show(item);
2004 gtk_menu_append(GTK_MENU(curmenu), item);
2006 curmenu = gtk_menu_new();
2007 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2011 utf8buf = conv_codeset_strdup((unsigned char*)l->data,
2012 conv_get_locale_charset_str(),
2014 item = gtk_menu_item_new_with_label(utf8buf);
2015 gtk_widget_show(item);
2016 gtk_menu_append(GTK_MENU(curmenu), item);
2017 g_signal_connect(G_OBJECT(item), "activate",
2018 G_CALLBACK(replace_word_cb),
2021 if (curmenu == menu && count < MENUCOUNT) {
2022 gtk_widget_add_accelerator(item, "activate",
2027 gtk_widget_add_accelerator(item, "activate",
2036 } while ((l = l->next) != NULL);
2039 gtk_window_add_accel_group
2040 (GTK_WINDOW(gtkaspell->parent_window),
2042 gtkaspell->accel_group = accel;
2044 g_signal_connect(G_OBJECT(menu),
2046 G_CALLBACK(aspell_key_pressed), gtkaspell);
2048 return GTK_MENU(menu);
2051 static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
2053 GtkWidget *item, *submenu;
2055 GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
2057 if (GTK_MENU_SHELL(menu)->children) {
2058 GList *amenu, *alist;
2059 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
2060 alist = amenu->next;
2061 gtk_widget_destroy(GTK_WIDGET(amenu->data));
2066 dictname = g_strdup_printf(_("Dictionary: %s"),
2067 gtkaspeller->dictionary->dictname);
2068 item = gtk_menu_item_new_with_label(dictname);
2069 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
2071 gtk_widget_show(item);
2072 gtk_menu_append(GTK_MENU(menu), item);
2074 item = gtk_menu_item_new();
2075 gtk_widget_show(item);
2076 gtk_menu_append(GTK_MENU(menu), item);
2078 if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
2079 dictname = g_strdup_printf(_("Use alternate (%s)"),
2080 gtkaspell->alternate_speller->dictionary->dictname);
2081 item = gtk_menu_item_new_with_label(dictname);
2083 g_signal_connect(G_OBJECT(item), "activate",
2084 G_CALLBACK(switch_to_alternate_cb),
2086 gtk_widget_show(item);
2087 gtk_menu_append(GTK_MENU(menu), item);
2090 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
2091 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
2092 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
2093 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
2095 g_signal_connect(G_OBJECT(item), "activate",
2096 G_CALLBACK(set_sug_mode_cb),
2098 gtk_widget_show(item);
2099 gtk_menu_append(GTK_MENU(menu), item);
2101 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
2102 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
2103 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2104 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2106 g_signal_connect(G_OBJECT(item), "activate",
2107 G_CALLBACK(set_sug_mode_cb),
2109 gtk_widget_show(item);
2110 gtk_menu_append(GTK_MENU(menu),item);
2112 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
2113 if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
2114 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2115 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2117 g_signal_connect(G_OBJECT(item), "activate",
2118 G_CALLBACK(set_sug_mode_cb),
2120 gtk_widget_show(item);
2121 gtk_menu_append(GTK_MENU(menu), item);
2123 item = gtk_menu_item_new();
2124 gtk_widget_show(item);
2125 gtk_menu_append(GTK_MENU(menu), item);
2127 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
2128 if (gtkaspell->check_while_typing)
2129 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2131 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2132 g_signal_connect(G_OBJECT(item), "activate",
2133 G_CALLBACK(toggle_check_while_typing_cb),
2135 gtk_widget_show(item);
2136 gtk_menu_append(GTK_MENU(menu), item);
2138 item = gtk_menu_item_new();
2139 gtk_widget_show(item);
2140 gtk_menu_append(GTK_MENU(menu), item);
2142 submenu = gtk_menu_new();
2143 item = gtk_menu_item_new_with_label(_("Change dictionary"));
2144 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
2145 gtk_widget_show(item);
2146 gtk_menu_append(GTK_MENU(menu), item);
2149 if (gtkaspellcheckers->dictionary_list == NULL)
2150 gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
2152 GtkWidget * curmenu = submenu;
2156 tmp = gtkaspellcheckers->dictionary_list;
2158 for (tmp = gtkaspellcheckers->dictionary_list; tmp != NULL;
2159 tmp = g_slist_next(tmp)) {
2160 if (count == MENUCOUNT) {
2163 newmenu = gtk_menu_new();
2164 item = gtk_menu_item_new_with_label(_("More..."));
2165 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2168 gtk_menu_append(GTK_MENU(curmenu), item);
2169 gtk_widget_show(item);
2173 dict = (Dictionary *) tmp->data;
2174 item = gtk_check_menu_item_new_with_label(dict->dictname);
2175 g_object_set_data(G_OBJECT(item), "dict_name",
2177 if (strcmp2(dict->fullname,
2178 gtkaspell->gtkaspeller->dictionary->fullname))
2179 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2181 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2182 gtk_widget_set_sensitive(GTK_WIDGET(item),
2185 g_signal_connect(G_OBJECT(item), "activate",
2186 G_CALLBACK(change_dict_cb),
2188 gtk_widget_show(item);
2189 gtk_menu_append(GTK_MENU(curmenu), item);
2196 static GtkMenu *make_config_menu(GtkAspell *gtkaspell)
2198 if (!gtkaspell->popup_config_menu)
2199 gtkaspell->popup_config_menu = gtk_menu_new();
2201 debug_print("Aspell: creating/using popup_config_menu %0x\n",
2202 (guint) gtkaspell->popup_config_menu);
2203 populate_submenu(gtkaspell, gtkaspell->popup_config_menu);
2205 return GTK_MENU(gtkaspell->popup_config_menu);
2208 void gtkaspell_populate_submenu(GtkAspell *gtkaspell, GtkWidget *menuitem)
2212 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2214 debug_print("Aspell: using config menu %0x\n",
2215 (guint) gtkaspell->popup_config_menu);
2216 populate_submenu(gtkaspell, menu);
2218 gtkaspell->config_menu = menu;
2222 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
2223 gboolean *push_in, gpointer data)
2225 GtkAspell *gtkaspell = (GtkAspell *) data;
2226 gint xx = 0, yy = 0;
2229 GtkTextView *text = GTK_TEXT_VIEW(gtkaspell->gtktext);
2230 GtkTextBuffer *textbuf;
2235 textbuf = gtk_text_view_get_buffer(gtkaspell->gtktext);
2236 gtk_text_buffer_get_iter_at_mark(textbuf, &iter,
2237 gtk_text_buffer_get_insert(textbuf));
2238 gtk_text_view_get_iter_location(gtkaspell->gtktext, &iter, &rect);
2239 gtk_text_view_buffer_to_window_coords(text, GTK_TEXT_WINDOW_TEXT,
2243 gdk_window_get_origin(GTK_WIDGET(gtkaspell->gtktext)->window, &xx, &yy);
2245 sx = gdk_screen_width();
2246 sy = gdk_screen_height();
2248 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2254 gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
2256 *y = rect.y + rect.height + yy;
2262 gdk_string_height(gtk_style_get_font(
2263 GTK_WIDGET(text)->style),
2264 gtkaspell->theword);
2267 /* Menu call backs */
2269 static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
2271 GtkAspell *gtkaspell = (GtkAspell *) data;
2273 gtkaspell->continue_check = NULL;
2274 set_point_continue(gtkaspell);
2279 gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
2282 GtkAspeller *gtkaspeller;
2285 g_return_val_if_fail(gtkaspell, FALSE);
2286 g_return_val_if_fail(dictionary, FALSE);
2288 sug_mode = gtkaspell->default_sug_mode;
2290 dict = g_new0(Dictionary, 1);
2291 dict->fullname = g_strdup(dictionary);
2292 dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
2294 if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
2295 dict == gtkaspell->alternate_speller->dictionary) {
2296 use_alternate_dict(gtkaspell);
2297 dictionary_delete(dict);
2301 gtkaspeller = gtkaspeller_new(dict);
2305 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2306 gtkaspellcheckers->error_message);
2308 alertpanel_warning(message);
2311 if (gtkaspell->use_alternate) {
2312 if (gtkaspell->alternate_speller)
2313 gtkaspeller_delete(gtkaspell->alternate_speller);
2314 gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
2316 gtkaspeller_delete(gtkaspell->gtkaspeller);
2318 gtkaspell->gtkaspeller = gtkaspeller;
2319 gtkaspell_set_sug_mode(gtkaspell, sug_mode);
2322 dictionary_delete(dict);
2324 if (gtkaspell->config_menu)
2325 populate_submenu(gtkaspell, gtkaspell->config_menu);
2330 /* change_dict_cb() - Menu callback : change dict */
2331 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
2335 fullname = (gchar *) g_object_get_data(G_OBJECT(w), "dict_name");
2337 if (!strcmp2(fullname, _("None")))
2340 gtkaspell_change_dict(gtkaspell, fullname);
2343 static void switch_to_alternate_cb(GtkWidget *w,
2346 GtkAspell *gtkaspell = (GtkAspell *) data;
2347 use_alternate_dict(gtkaspell);
2350 /* Misc. helper functions */
2352 static void set_point_continue(GtkAspell *gtkaspell)
2354 GtkTextView *gtktext;
2356 gtktext = gtkaspell->gtktext;
2358 set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
2360 if (gtkaspell->continue_check)
2361 gtkaspell->continue_check((gpointer *) gtkaspell);
2364 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
2366 GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtkaspell->gtktext);
2367 GdkColor *color = &(gtkaspell->highlight);
2369 /* Shameless copy from Sylpheed's gtkutils.c */
2371 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0)
2373 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0)
2375 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0)
2378 gtk_text_buffer_create_tag(buffer, "misspelled",
2379 "foreground-gdk", color, NULL);
2382 static void change_color(GtkAspell * gtkaspell,
2383 gint start, gint end,
2387 GtkTextView *gtktext;
2388 GtkTextBuffer *buffer;
2389 GtkTextIter startiter, enditer;
2391 g_return_if_fail(start < end);
2393 gtktext = gtkaspell->gtktext;
2395 buffer = gtk_text_view_get_buffer(gtktext);
2396 gtk_text_buffer_get_iter_at_offset(buffer, &startiter, start);
2397 gtk_text_buffer_get_iter_at_offset(buffer, &enditer, end);
2399 gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
2400 &startiter, &enditer);
2402 gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
2403 &startiter, &enditer);
2406 /* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-*
2407 * as needed by aspell. Returns an allocated string.
2410 static guchar *convert_to_aspell_encoding (const guchar *encoding)
2412 guchar * aspell_encoding;
2414 if (strstr2(encoding, "ISO-8859-")) {
2415 aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2418 if (!strcmp2(encoding, "US-ASCII"))
2419 aspell_encoding = g_strdup("iso8859-1");
2421 aspell_encoding = g_strdup(encoding);
2424 return aspell_encoding;
2427 /* compare_dict () - compare 2 dict names */
2428 static gint compare_dict(Dictionary *a, Dictionary *b)
2430 guint aparts = 0, bparts = 0;
2433 for (i=0; i < strlen(a->dictname); i++)
2434 if (a->dictname[i] == '-')
2436 for (i=0; i < strlen(b->dictname); i++)
2437 if (b->dictname[i] == '-')
2440 if (aparts != bparts)
2441 return (aparts < bparts) ? -1 : +1;
2444 compare = strcmp2(a->dictname, b->dictname);
2446 compare = strcmp2(a->fullname, b->fullname);
2451 static void dictionary_delete(Dictionary *dict)
2453 g_free(dict->fullname);
2454 g_free(dict->encoding);
2458 static Dictionary *dictionary_dup(const Dictionary *dict)
2462 dict2 = g_new(Dictionary, 1);
2464 dict2->fullname = g_strdup(dict->fullname);
2465 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2466 dict2->encoding = g_strdup(dict->encoding);
2471 static void free_suggestions_list(GtkAspell *gtkaspell)
2475 for (list = gtkaspell->suggestions_list; list != NULL;
2481 gtkaspell->max_sug = -1;
2482 gtkaspell->suggestions_list = NULL;
2485 static void reset_theword_data(GtkAspell *gtkaspell)
2487 gtkaspell->start_pos = 0;
2488 gtkaspell->end_pos = 0;
2489 gtkaspell->theword[0] = 0;
2490 gtkaspell->max_sug = -1;
2492 free_suggestions_list(gtkaspell);
2495 static void free_checkers(gpointer elt, gpointer data)
2497 GtkAspeller *gtkaspeller = elt;
2499 g_return_if_fail(gtkaspeller);
2501 gtkaspeller_real_delete(gtkaspeller);
2504 static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
2506 Dictionary *a = ((GtkAspeller *) aa)->dictionary;
2507 Dictionary *b = ((GtkAspeller *) bb)->dictionary;
2509 if (a && b && a->fullname && b->fullname &&
2510 strcmp(a->fullname, b->fullname) == 0 &&
2511 a->encoding && b->encoding)
2512 return strcmp(a->encoding, b->encoding);