1 /* gtkpspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin.
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
26 #if defined(HAVE_CONFIG_H)
35 #include <sys/types.h>
48 #include <prefs_common.h>
53 #include <gtk/gtkoptionmenu.h>
54 #include <gtk/gtkmenu.h>
55 #include <gtk/gtkmenuitem.h>
56 #include <gdk/gdkkeysyms.h>
62 #include <pspell/pspell.h>
63 /* size of the text buffer used in various word-processing routines. */
66 /* number of suggestions to display on each menu. */
69 /* 'config' must be defined as a 'PspellConfig *' */
70 #define RETURN_FALSE_IF_CONFIG_ERROR() \
71 if (pspell_config_error_number(config) != 0) { \
72 gtkpspellcheckers->error_message = g_strdup(pspell_config_error_message(config)); \
76 #define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
77 pspell_config_replace(config, option, value); \
78 RETURN_FALSE_IF_CONFIG_ERROR(); \
81 /******************************************************************************/
83 GtkPspellCheckers *gtkpspellcheckers;
85 /* Error message storage */
86 static void gtkpspell_checkers_error_message(gchar *message);
89 static void entry_insert_cb (GtkSText *gtktext,
93 GtkPspell *gtkpspell);
94 static void entry_delete_cb (GtkSText *gtktext,
97 GtkPspell *gtkpspell);
98 static gint button_press_intercept_cb (GtkSText *gtktext,
100 GtkPspell *gtkpspell);
102 /* Checker creation */
103 static GtkPspeller * gtkpspeller_new (Dictionary *dict);
104 static GtkPspeller * gtkpspeller_real_new (Dictionary *dict);
105 static GtkPspeller * gtkpspeller_delete (GtkPspeller *gtkpspeller);
106 static GtkPspeller * gtkpspeller_real_delete (GtkPspeller *gtkpspeller);
108 /* Checker configuration */
109 static gint set_dictionary (PspellConfig *config,
111 static void set_sug_mode_cb (GtkMenuItem *w,
112 GtkPspell *gtkpspell);
113 static void set_real_sug_mode (GtkPspell *gtkpspell,
114 const char *themode);
116 /* Checker actions */
117 static gboolean check_at (GtkPspell *gtkpspell,
119 static gboolean check_next_prev (GtkPspell *gtkpspell,
121 static GList* misspelled_suggest (GtkPspell *gtkpspell,
123 static void add_word_to_session_cb (GtkWidget *w,
125 static void add_word_to_personal_cb (GtkWidget *w,
127 static void replace_with_create_dialog_cb (GtkWidget *w,
129 static void replace_with_supplied_word_cb (GtkWidget *w,
130 GtkPspell *gtkpspell);
131 static void replace_word_cb (GtkWidget *w,
133 static void replace_real_word (GtkPspell *gtkpspell,
135 static void check_with_alternate_cb (GtkWidget *w,
137 static void use_alternate_dict (GtkPspell *gtkpspell);
138 static void toggle_check_while_typing_cb (GtkWidget *w,
142 static void popup_menu (GtkPspell *gtkpspell,
144 static GtkMenu* make_sug_menu (GtkPspell *gtkpspell);
145 static void populate_submenu (GtkPspell *gtkpspell,
147 static GtkMenu* make_config_menu (GtkPspell *gtkpspell);
148 static void set_menu_pos (GtkMenu *menu,
152 /* Other menu callbacks */
153 static gboolean cancel_menu_cb (GtkMenuShell *w,
155 static void change_dict_cb (GtkWidget *w,
156 GtkPspell *gtkpspell);
157 static void switch_to_alternate_cb (GtkWidget *w,
160 /* Misc. helper functions */
161 static void set_point_continue (GtkPspell *gtkpspell);
162 static void continue_check (gpointer *gtkpspell);
163 static gboolean iswordsep (unsigned char c);
164 static guchar get_text_index_whar (GtkPspell *gtkpspell,
166 static gboolean get_word_from_pos (GtkPspell *gtkpspell,
172 static void allocate_color (GtkPspell *gtkpspell,
174 static void change_color (GtkPspell *gtkpspell,
179 static guchar* convert_to_pspell_encoding (const guchar *encoding);
180 static gint compare_dict (Dictionary *a,
182 static void dictionary_delete (Dictionary *dict);
183 static Dictionary * dictionary_dup (const Dictionary *dict);
184 static void free_suggestions_list (GtkPspell *gtkpspell);
185 static void reset_theword_data (GtkPspell *gtkpspell);
186 static void free_checkers (gpointer elt,
188 static gint find_gtkpspeller (gconstpointer aa,
190 static void gtkpspell_alert_dialog (gchar *message);
191 /* gtkspellconfig - only one config per session */
192 GtkPspellConfig * gtkpspellconfig;
194 /******************************************************************************/
196 GtkPspellCheckers * gtkpspell_checkers_new()
198 GtkPspellCheckers *gtkpspellcheckers;
200 gtkpspellcheckers = g_new(GtkPspellCheckers, 1);
202 gtkpspellcheckers->checkers = NULL;
204 gtkpspellcheckers->dictionary_list = NULL;
206 gtkpspellcheckers->error_message = NULL;
208 return gtkpspellcheckers;
211 GtkPspellCheckers *gtkpspell_checkers_delete()
216 if (gtkpspellcheckers == NULL) return NULL;
218 if ((checkers = gtkpspellcheckers->checkers)) {
219 debug_print(_("Pspell: number of running checkers to delete %d\n"),
220 g_slist_length(checkers));
222 g_slist_foreach(checkers, free_checkers, NULL);
223 g_slist_free(checkers);
226 if ((dict_list = gtkpspellcheckers->dictionary_list)) {
227 debug_print(_("Pspell: number of dictionaries to delete %d\n"),
228 g_slist_length(dict_list));
230 gtkpspell_free_dictionary_list(dict_list);
231 gtkpspellcheckers->dictionary_list = NULL;
234 g_free(gtkpspellcheckers->error_message);
239 static void gtkpspell_checkers_error_message (gchar *message)
242 if (gtkpspellcheckers->error_message) {
243 tmp = g_strdup_printf("%s\n%s",
244 gtkpspellcheckers->error_message, message);
246 g_free(gtkpspellcheckers->error_message);
247 gtkpspellcheckers->error_message = tmp;
248 } else gtkpspellcheckers->error_message = message;
251 void gtkpspell_checkers_reset_error()
253 g_return_if_fail(gtkpspellcheckers);
255 g_free(gtkpspellcheckers->error_message);
257 gtkpspellcheckers->error_message = NULL;
260 GtkPspell *gtkpspell_new(const gchar *dictionary,
261 const gchar *encoding,
262 gint misspelled_color,
263 gboolean check_while_typing,
264 gboolean use_alternate,
267 Dictionary *dict = g_new0(Dictionary, 1);
268 GtkPspell *gtkpspell;
269 GtkPspeller *gtkpspeller;
271 g_return_val_if_fail(gtktext, NULL);
273 dict->fullname = g_strdup(dictionary);
274 dict->encoding = g_strdup(encoding);
275 gtkpspeller = gtkpspeller_new(dict);
276 dictionary_delete(dict);
281 gtkpspell = g_new(GtkPspell, 1);
283 gtkpspell->gtkpspeller = gtkpspeller;
284 gtkpspell->alternate_speller = NULL;
285 gtkpspell->theword[0] = 0x00;
286 gtkpspell->start_pos = 0;
287 gtkpspell->end_pos = 0;
288 gtkpspell->orig_pos = -1;
289 gtkpspell->end_check_pos = -1;
290 gtkpspell->misspelled = -1;
291 gtkpspell->check_while_typing = check_while_typing;
292 gtkpspell->continue_check = NULL;
293 gtkpspell->config_menu = NULL;
294 gtkpspell->popup_config_menu = NULL;
295 gtkpspell->sug_menu = NULL;
296 gtkpspell->replace_entry = NULL;
297 gtkpspell->gtktext = gtktext;
298 gtkpspell->default_sug_mode = PSPELL_FASTMODE;
299 gtkpspell->max_sug = -1;
300 gtkpspell->suggestions_list = NULL;
301 gtkpspell->use_alternate = use_alternate;
303 allocate_color(gtkpspell, misspelled_color);
305 gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
306 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
307 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
308 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
309 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
310 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
312 debug_print("Pspell: created gtkpspell %0x\n", gtkpspell);
317 void gtkpspell_delete(GtkPspell * gtkpspell)
321 gtktext = gtkpspell->gtktext;
323 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
324 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
325 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
326 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
327 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
328 GTK_SIGNAL_FUNC(button_press_intercept_cb),
331 gtkpspell_uncheck_all(gtkpspell);
333 gtkpspeller_delete(gtkpspell->gtkpspeller);
335 if (gtkpspell->use_alternate && gtkpspell->alternate_speller)
336 gtkpspeller_delete(gtkpspell->alternate_speller);
338 if (gtkpspell->sug_menu)
339 gtk_widget_destroy(gtkpspell->sug_menu);
340 if (gtkpspell->popup_config_menu)
341 gtk_widget_destroy(gtkpspell->popup_config_menu);
342 if (gtkpspell->config_menu)
343 gtk_widget_destroy(gtkpspell->config_menu);
344 if (gtkpspell->suggestions_list)
345 free_suggestions_list(gtkpspell);
347 debug_print("Pspell: deleting gtkpspell %0x\n", gtkpspell);
354 static void entry_insert_cb(GtkSText *gtktext, gchar *newtext,
355 guint len, guint *ppos,
356 GtkPspell * gtkpspell)
358 g_return_if_fail(gtkpspell->gtkpspeller->checker);
360 if (!gtkpspell->check_while_typing)
363 /* We must insert ourself the character to impose the */
364 /* color of the inserted character to be default */
365 /* Never mess with set_insertion when frozen */
366 gtk_stext_freeze(gtktext);
367 gtk_stext_backward_delete(GTK_STEXT(gtktext), len);
368 gtk_stext_insert(GTK_STEXT(gtktext), NULL, NULL, NULL, newtext, len);
369 *ppos = gtk_stext_get_point(GTK_STEXT(gtktext));
371 if (iswordsep(newtext[0])) {
372 /* did we just end a word? */
374 check_at(gtkpspell, *ppos - 2);
376 /* did we just split a word? */
377 if (*ppos < gtk_stext_get_length(gtktext))
378 check_at(gtkpspell, *ppos + 1);
380 /* check as they type, *except* if they're typing at the end (the most
383 if (*ppos < gtk_stext_get_length(gtktext)
384 && !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
385 check_at(gtkpspell, *ppos - 1);
387 gtk_stext_thaw(gtktext);
388 gtk_editable_set_position(GTK_EDITABLE(gtktext), *ppos);
391 static void entry_delete_cb(GtkSText *gtktext, gint start, gint end,
392 GtkPspell *gtkpspell)
396 g_return_if_fail(gtkpspell->gtkpspeller->checker);
398 if (!gtkpspell->check_while_typing)
401 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
403 check_at(gtkpspell, start - 1);
404 check_at(gtkpspell, start);
407 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
408 gtk_stext_set_point(gtktext, origpos);
409 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
410 /* this is to *UNDO* the selection, in case they were holding shift
411 * while hitting backspace. */
414 /* ok, this is pretty wacky:
415 * we need to let the right-mouse-click go through, so it moves the cursor,
416 * but we *can't* let it go through, because GtkText interprets rightclicks as
417 * weird selection modifiers.
419 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
422 static gint button_press_intercept_cb(GtkSText *gtktext, GdkEvent *e, GtkPspell *gtkpspell)
427 g_return_val_if_fail(gtkpspell->gtkpspeller->checker, FALSE);
429 if (e->type != GDK_BUTTON_PRESS)
431 eb = (GdkEventButton*) e;
436 /* forge the leftclick */
439 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
440 GTK_SIGNAL_FUNC(button_press_intercept_cb),
442 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
444 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
445 GTK_SIGNAL_FUNC(button_press_intercept_cb),
447 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
449 /* now do the menu wackiness */
450 popup_menu(gtkpspell, eb);
451 gtk_grab_remove(GTK_WIDGET(gtktext));
455 /*****************************************************************************/
456 /* Checker creation */
457 static GtkPspeller *gtkpspeller_new(Dictionary *dictionary)
460 GtkPspeller *gtkpspeller = NULL;
461 GtkPspeller *g = g_new0(GtkPspeller, 1);
465 g_return_val_if_fail(gtkpspellcheckers, NULL);
467 g_return_val_if_fail(dictionary, NULL);
469 if (dictionary->fullname == NULL)
470 gtkpspell_checkers_error_message(g_strdup(_("No dictionary selected.")));
472 g_return_val_if_fail(dictionary->fullname, NULL);
474 if (dictionary->dictname == NULL) {
477 tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
480 dictionary->dictname = dictionary->fullname;
482 dictionary->dictname = tmp + 1;
485 dict = dictionary_dup(dictionary);
487 ispell = (strstr2(dict->dictname, "-ispell") != NULL);
489 g->dictionary = dict;
491 exist = g_slist_find_custom(gtkpspellcheckers->checkers, g,
496 if (exist && ispell) {
497 gtkpspeller = (GtkPspeller *) exist->data;
498 dictionary_delete(dict);
500 debug_print(_("Pspell: Using existing ispell checker %0x\n"),
503 if ((gtkpspeller = gtkpspeller_real_new(dict)) != NULL) {
504 gtkpspellcheckers->checkers = g_slist_append(
505 gtkpspellcheckers->checkers,
508 debug_print(_("Pspell: Created a new gtkpspeller %0x\n"),
511 dictionary_delete(dict);
513 debug_print(_("Pspell: Could not create spell checker.\n"));
517 debug_print(_("Pspell: number of existing checkers %d\n"),
518 g_slist_length(gtkpspellcheckers->checkers));
523 static GtkPspeller *gtkpspeller_real_new(Dictionary *dict)
525 GtkPspeller *gtkpspeller;
526 PspellConfig *config;
527 PspellCanHaveError *ret;
529 g_return_val_if_fail(gtkpspellcheckers, NULL);
530 g_return_val_if_fail(dict, NULL);
532 gtkpspeller = g_new(GtkPspeller, 1);
534 gtkpspeller->dictionary = dict;
535 gtkpspeller->sug_mode = PSPELL_FASTMODE;
537 config = new_pspell_config();
539 if (!set_dictionary(config, dict))
542 ret = new_pspell_manager(config);
543 delete_pspell_config(config);
545 if (pspell_error_number(ret) != 0) {
546 gtkpspellcheckers->error_message = g_strdup(pspell_error_message(ret));
548 delete_pspell_can_have_error(ret);
553 gtkpspeller->checker = to_pspell_manager(ret);
554 gtkpspeller->config = pspell_manager_config(gtkpspeller->checker);
555 gtkpspeller->ispell = (strstr2(dict->fullname, "-ispell") != NULL);
560 static GtkPspeller *gtkpspeller_delete(GtkPspeller *gtkpspeller)
562 g_return_val_if_fail(gtkpspellcheckers, NULL);
564 if (gtkpspeller->ispell)
565 debug_print(_("Pspell: Won't remove existing ispell checker %0x.\n"),
568 gtkpspellcheckers->checkers = g_slist_remove(gtkpspellcheckers->checkers,
571 debug_print(_("Pspell: Deleting gtkpspeller %0x.\n"),
574 gtkpspeller_real_delete(gtkpspeller);
577 debug_print(_("Pspell: number of existing checkers %d\n"),
578 g_slist_length(gtkpspellcheckers->checkers));
583 static GtkPspeller *gtkpspeller_real_delete(GtkPspeller *gtkpspeller)
585 g_return_val_if_fail(gtkpspeller, NULL);
586 g_return_val_if_fail(gtkpspeller->checker, NULL);
588 pspell_manager_save_all_word_lists(gtkpspeller->checker);
590 delete_pspell_manager(gtkpspeller->checker);
592 dictionary_delete(gtkpspeller->dictionary);
594 debug_print(_("Pspell: gtkpspeller %0x deleted.\n"),
601 /*****************************************************************************/
602 /* Checker configuration */
604 static gboolean set_dictionary(PspellConfig *config, Dictionary *dict)
606 gchar *language = NULL;
607 gchar *spelling = NULL;
608 gchar *jargon = NULL;
609 gchar *module = NULL;
613 g_return_val_if_fail(config, FALSE);
614 g_return_val_if_fail(dict, FALSE);
616 strncpy(buf, dict->fullname, BUFSIZE-1);
617 buf[BUFSIZE-1] = 0x00;
619 buf[dict->dictname - dict->fullname] = 0x00;
621 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("rem-all-word-list-path", "");
622 debug_print(_("Pspell: removed all paths.\n"));
624 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("add-word-list-path", buf);
625 debug_print(_("Pspell: added path %s.\n"), buf);
627 strncpy(buf, dict->dictname, BUFSIZE-1);
630 if ((module = strrchr(buf, '-')) != NULL) {
633 if ((spelling = strchr(language, '-')) != NULL)
635 if (spelling != module) {
636 if ((end = strchr(spelling, '-')) != NULL) {
639 if (jargon != module)
640 if ((end = strchr(jargon, '-')) != NULL)
653 debug_print(_("Pspell: Language: %s, spelling: %s, jargon: %s, module: %s\n"),
654 language, spelling, jargon, module);
657 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("language-tag", language);
660 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("spelling", spelling);
663 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
666 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("rem-all-module-search-order", "");
667 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("add-module-search-order", module);
670 if (dict->encoding) {
673 pspell_enc = convert_to_pspell_encoding (dict->encoding);
674 pspell_config_replace(config, "encoding", (const char *) pspell_enc);
677 RETURN_FALSE_IF_CONFIG_ERROR();
683 guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
686 g_return_val_if_fail(gtkpspell->gtkpspeller->config, NULL);
687 g_return_val_if_fail(gtkpspell->gtkpspeller->dictionary, NULL);
689 return g_strdup(gtkpspell->gtkpspeller->dictionary->dictname);
692 guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
697 g_return_val_if_fail(gtkpspell->gtkpspeller->config, NULL);
698 g_return_val_if_fail(gtkpspell->gtkpspeller->dictionary, NULL);
700 dict = gtkpspell->gtkpspeller->dictionary;
701 path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
706 /* set_sug_mode_cb() - Menu callback : Set the suggestion mode */
707 static void set_sug_mode_cb(GtkMenuItem *w, GtkPspell *gtkpspell)
711 if (gtkpspell->gtkpspeller->ispell) return;
713 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
715 set_real_sug_mode(gtkpspell, themode);
717 if (gtkpspell->config_menu)
718 populate_submenu(gtkpspell, gtkpspell->config_menu);
720 static void set_real_sug_mode(GtkPspell *gtkpspell, const char *themode)
723 gint mode = PSPELL_FASTMODE;
724 g_return_if_fail(gtkpspell);
725 g_return_if_fail(gtkpspell->gtkpspeller);
726 g_return_if_fail(themode);
728 if (!strcmp(themode,_("Normal Mode")))
729 mode = PSPELL_NORMALMODE;
730 else if (!strcmp( themode,_("Bad Spellers Mode")))
731 mode = PSPELL_BADSPELLERMODE;
733 result = gtkpspell_set_sug_mode(gtkpspell, mode);
736 debug_print(_("Pspell: error while changing suggestion mode:%s\n"),
737 gtkpspellcheckers->error_message);
738 gtkpspell_checkers_reset_error();
742 /* gtkpspell_set_sug_mode() - Set the suggestion mode */
743 gboolean gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gint themode)
745 PspellConfig *config;
747 g_return_val_if_fail(gtkpspell, FALSE);
748 g_return_val_if_fail(gtkpspell->gtkpspeller, FALSE);
749 g_return_val_if_fail(gtkpspell->gtkpspeller->config, FALSE);
751 debug_print("Pspell: setting sug mode of gtkpspeller %0x to %d\n",
752 gtkpspell->gtkpspeller, themode);
753 config = gtkpspell->gtkpspeller->config;
755 case PSPELL_FASTMODE:
756 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
758 case PSPELL_NORMALMODE:
759 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
761 case PSPELL_BADSPELLERMODE:
762 CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode",
766 gtkpspellcheckers->error_message =
767 g_strdup(_("Unknown suggestion mode."));
771 gtkpspell->gtkpspeller->sug_mode = themode;
772 gtkpspell->default_sug_mode = themode;
777 /* misspelled_suggest() - Create a suggestion list for word */
778 static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word)
780 const guchar *newword;
782 const PspellWordList *suggestions;
783 PspellStringEmulation *elements;
785 g_return_val_if_fail(word, NULL);
787 if (!pspell_manager_check(gtkpspell->gtkpspeller->checker, word, -1)) {
788 free_suggestions_list(gtkpspell);
790 suggestions = pspell_manager_suggest(gtkpspell->gtkpspeller->checker,
791 (const char *)word, -1);
792 elements = pspell_word_list_elements(suggestions);
793 list = g_list_append(list, g_strdup(word));
795 while ((newword = pspell_string_emulation_next(elements)) != NULL)
796 list = g_list_append(list, g_strdup(newword));
798 gtkpspell->max_sug = g_list_length(list) - 1;
799 gtkpspell->suggestions_list = list;
804 free_suggestions_list(gtkpspell);
809 /* misspelled_test() - Just test if word is correctly spelled */
810 static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word)
812 return pspell_manager_check(gtkpspell->gtkpspeller->checker, word, -1) ? 0 : 1;
816 static gboolean iswordsep(unsigned char c)
818 return !isalpha(c) && c != '\'';
821 static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos)
826 text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
834 /* get_word_from_pos () - return the word pointed to. */
835 /* Handles correctly the quotes. */
836 static gboolean get_word_from_pos(GtkPspell *gtkpspell, gint pos,
837 unsigned char* buf, gint buflen,
838 gint *pstart, gint *pend)
841 /* TODO : when correcting a word into quotes, change the color of */
842 /* the quotes too, as may be they were highlighted before. To do */
843 /* this, we can use two others pointers that points to the whole */
844 /* word including quotes. */
851 gtktext = gtkpspell->gtktext;
852 if (iswordsep(get_text_index_whar(gtkpspell, pos)))
855 /* The apostrophe character is somtimes used for quotes
856 * So include it in the word only if it is not surrounded
857 * by other characters.
860 for (start = pos; start >= 0; --start) {
861 c = get_text_index_whar(gtkpspell, start);
864 if (!isalpha(get_text_index_whar(gtkpspell, start - 1))) {
865 /* start_quote = TRUE; */
870 /* start_quote = TRUE; */
880 for (end = pos; end < gtk_stext_get_length(gtktext); end++) {
881 c = get_text_index_whar(gtkpspell, end);
883 if (end < gtk_stext_get_length(gtktext)) {
884 if (!isalpha(get_text_index_whar(gtkpspell, end + 1))) {
885 /* end_quote = TRUE; */
890 /* end_quote = TRUE; */
905 if (end - start < buflen) {
906 for (pos = start; pos < end; pos++)
907 buf[pos - start] = get_text_index_whar(gtkpspell, pos);
908 buf[pos - start] = 0;
915 static gboolean check_at(GtkPspell *gtkpspell, gint from_pos)
918 unsigned char buf[GTKPSPELLWORDSIZE];
921 g_return_val_if_fail(from_pos >= 0, FALSE);
923 gtktext = gtkpspell->gtktext;
925 if (!get_word_from_pos(gtkpspell, from_pos, buf, sizeof(buf),
929 if (misspelled_test(gtkpspell, buf)) {
930 strncpy(gtkpspell->theword, buf, GTKPSPELLWORDSIZE - 1);
931 gtkpspell->theword[GTKPSPELLWORDSIZE - 1] = 0;
932 gtkpspell->start_pos = start;
933 gtkpspell->end_pos = end;
934 free_suggestions_list(gtkpspell);
936 change_color(gtkpspell, start, end, buf, &(gtkpspell->highlight));
939 change_color(gtkpspell, start, end, buf, NULL);
944 static gboolean check_next_prev(GtkPspell *gtkpspell, gboolean forward)
953 maxpos = gtkpspell->end_check_pos;
961 pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
962 gtkpspell->orig_pos = pos;
963 while (iswordsep(get_text_index_whar(gtkpspell, pos)) && pos > minpos && pos <= maxpos)
965 while (!(misspelled = check_at(gtkpspell, pos)) && pos > minpos && pos <= maxpos)
967 while (!iswordsep(get_text_index_whar(gtkpspell, pos)) && pos > minpos && pos <= maxpos)
970 while (iswordsep(get_text_index_whar(gtkpspell, pos)) && pos > minpos && pos <= maxpos)
974 misspelled_suggest(gtkpspell, gtkpspell->theword);
976 gtkpspell->orig_pos = gtkpspell->end_pos;
977 gtk_stext_set_point(GTK_STEXT(gtkpspell->gtktext),
979 gtk_editable_set_position(GTK_EDITABLE(gtkpspell->gtktext),
981 gtk_menu_popup(make_sug_menu(gtkpspell), NULL, NULL,
982 set_menu_pos, gtkpspell, 0, GDK_CURRENT_TIME);
984 reset_theword_data(gtkpspell);
986 gtkpspell_alert_dialog(_("No misspelled word found."));
987 gtk_stext_set_point(GTK_STEXT(gtkpspell->gtktext), gtkpspell->orig_pos);
988 gtk_editable_set_position(GTK_EDITABLE(gtkpspell->gtktext), gtkpspell->orig_pos);
993 void gtkpspell_check_backwards(GtkPspell *gtkpspell)
995 gtkpspell->continue_check = NULL;
996 gtkpspell->end_check_pos = gtk_stext_get_length(GTK_STEXT(gtkpspell->gtktext));
997 check_next_prev(gtkpspell, FALSE);
1000 void gtkpspell_check_forwards_go(GtkPspell *gtkpspell)
1003 gtkpspell->continue_check = NULL;
1004 gtkpspell->end_check_pos = gtk_stext_get_length(GTK_STEXT(gtkpspell->gtktext));
1005 check_next_prev(gtkpspell, TRUE);
1008 void gtkpspell_check_all(GtkPspell *gtkpspell)
1014 g_return_if_fail(gtkpspell);
1015 g_return_if_fail(gtkpspell->gtktext);
1017 gtktext = (GtkWidget *) gtkpspell->gtktext;
1020 end = gtk_stext_get_length(GTK_STEXT(gtktext));
1022 if (GTK_EDITABLE(gtktext)->has_selection) {
1023 start = GTK_EDITABLE(gtktext)->selection_start_pos;
1024 end = GTK_EDITABLE(gtktext)->selection_end_pos;
1035 gtk_editable_set_position(GTK_EDITABLE(gtktext), start);
1036 gtk_stext_set_point(GTK_STEXT(gtktext), start);
1038 gtkpspell->continue_check = continue_check;
1039 gtkpspell->end_check_pos = end;
1041 gtkpspell->misspelled = check_next_prev(gtkpspell, TRUE);
1045 static void continue_check(gpointer *data)
1047 GtkPspell *gtkpspell = (GtkPspell *) data;
1048 gint pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
1049 if (pos < gtkpspell->end_check_pos && gtkpspell->misspelled)
1050 gtkpspell->misspelled = check_next_prev(gtkpspell, TRUE);
1052 gtkpspell->continue_check = NULL;
1056 void gtkpspell_highlight_all(GtkPspell *gtkpspell)
1064 g_return_if_fail(gtkpspell->gtkpspeller->checker);
1066 gtktext = gtkpspell->gtktext;
1068 adj_value = gtktext->vadj->value;
1070 len = gtk_stext_get_length(gtktext);
1072 gtk_stext_freeze(gtktext);
1074 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1076 /* gtk_editable_set_position(GTK_EDITABLE(gtktext), 0);*/
1080 iswordsep(get_text_index_whar(gtkpspell, pos)))
1083 !iswordsep(get_text_index_whar(gtkpspell, pos)))
1086 check_at(gtkpspell, pos - 1);
1088 gtk_stext_thaw(gtktext);
1089 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1090 gtk_stext_set_point(GTK_STEXT(gtktext), origpos);
1091 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1092 /*gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);*/
1095 static void replace_with_supplied_word_cb(GtkWidget *w, GtkPspell *gtkpspell)
1097 unsigned char *newword;
1098 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1100 newword = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->replace_entry), 0, -1);
1102 if (strcmp(newword, gtkpspell->theword)) {
1103 replace_real_word(gtkpspell, newword);
1105 if ((e->type == GDK_KEY_PRESS &&
1106 ((GdkEventKey *) e)->state & GDK_MOD1_MASK)) {
1107 pspell_manager_store_replacement(gtkpspell->gtkpspeller->checker,
1108 gtkpspell->theword, -1,
1111 gtkpspell->replace_entry = NULL;
1116 set_point_continue(gtkpspell);
1120 static void replace_word_cb(GtkWidget *w, gpointer data)
1122 unsigned char *newword;
1123 GtkPspell *gtkpspell = (GtkPspell *) data;
1124 GdkEvent *e= (GdkEvent *) gtk_get_current_event();
1127 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
1129 replace_real_word(gtkpspell, newword);
1131 if ((e->type == GDK_KEY_PRESS &&
1132 ((GdkEventKey *) e)->state & GDK_MOD1_MASK) ||
1133 (e->type == GDK_BUTTON_RELEASE &&
1134 ((GdkEventButton *) e)->state & GDK_MOD1_MASK)) {
1135 pspell_manager_store_replacement(gtkpspell->gtkpspeller->checker,
1136 gtkpspell->theword, -1,
1140 gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
1142 set_point_continue(gtkpspell);
1145 static void replace_real_word(GtkPspell *gtkpspell, gchar *newword)
1147 int oldlen, newlen, wordlen;
1150 gint start = gtkpspell->start_pos;
1153 if (!newword) return;
1155 gtktext = gtkpspell->gtktext;
1157 gtk_stext_freeze(GTK_STEXT(gtktext));
1158 origpos = gtkpspell->orig_pos;
1160 oldlen = gtkpspell->end_pos - gtkpspell->start_pos;
1161 wordlen = strlen(gtkpspell->theword);
1163 newlen = strlen(newword); /* FIXME: multybyte characters? */
1165 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1166 GTK_SIGNAL_FUNC(entry_insert_cb),
1168 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1169 GTK_SIGNAL_FUNC(entry_delete_cb),
1172 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "delete-text",
1173 gtkpspell->start_pos, gtkpspell->end_pos);
1174 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "insert-text",
1175 newword, newlen, &start);
1177 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1178 GTK_SIGNAL_FUNC(entry_insert_cb),
1180 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1181 GTK_SIGNAL_FUNC(entry_delete_cb),
1184 /* Put the point and the position where we clicked with the mouse */
1185 /* It seems to be a hack, as I must thaw,freeze,thaw the widget */
1186 /* to let it update correctly the word insertion and then the */
1187 /* point & position position. If not, SEGV after the first replacement */
1188 /* If the new word ends before point, put the point at its end*/
1190 if (origpos - gtkpspell->start_pos < oldlen &&
1191 origpos - gtkpspell->start_pos >= 0) {
1192 /* Original point was in the word. */
1193 /* Let it there unless point is going to be outside of the word */
1194 if (origpos - gtkpspell->start_pos >= newlen) {
1195 pos = gtkpspell->start_pos + newlen;
1198 else if (origpos >= gtkpspell->end_pos) {
1199 /* move the position according to the change of length */
1200 pos = origpos + newlen - oldlen;
1203 gtkpspell->end_pos = gtkpspell->start_pos + strlen(newword); /* FIXME: multibyte characters? */
1205 gtk_stext_thaw(GTK_STEXT(gtktext));
1206 gtk_stext_freeze(GTK_STEXT(gtktext));
1208 if (GTK_STEXT(gtktext)->text_len < pos)
1209 pos = gtk_stext_get_length(GTK_STEXT(gtktext));
1211 gtkpspell->orig_pos = pos;
1213 gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
1214 gtk_stext_set_point(GTK_STEXT(gtktext),
1215 gtk_editable_get_position(GTK_EDITABLE(gtktext)));
1217 gtk_stext_thaw(GTK_STEXT(gtktext));
1220 /* Accept this word for this session */
1221 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
1225 GtkPspell *gtkpspell = (GtkPspell *) data;
1226 gtktext = gtkpspell->gtktext;
1228 gtk_stext_freeze(GTK_STEXT(gtktext));
1230 pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1232 pspell_manager_add_to_session(gtkpspell->gtkpspeller->checker,
1234 strlen(gtkpspell->theword));
1236 check_at(gtkpspell, gtkpspell->start_pos);
1238 gtk_stext_thaw(gtkpspell->gtktext);
1240 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1242 set_point_continue(gtkpspell);
1245 /* add_word_to_personal_cb() - add word to personal dict. */
1246 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
1249 GtkPspell *gtkpspell = (GtkPspell *) data;
1250 gtktext = gtkpspell->gtktext;
1252 gtk_stext_freeze(GTK_STEXT(gtktext));
1254 pspell_manager_add_to_personal(gtkpspell->gtkpspeller->checker,
1256 strlen(gtkpspell->theword));
1258 check_at(gtkpspell, gtkpspell->start_pos);
1260 gtk_stext_thaw(gtkpspell->gtktext);
1262 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1263 set_point_continue(gtkpspell);
1266 static void deactivate_sug_menu_cb(GtkObject *w, gpointer data)
1268 GtkMenuShell *menu_shell = GTK_MENU_SHELL(data);
1269 debug_print("Destroying %0x\n", w);
1270 gtk_menu_shell_deactivate(menu_shell);
1273 static void check_with_alternate_cb(GtkWidget *w,
1276 GtkPspell *gtkpspell = (GtkPspell *) data;
1278 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1280 use_alternate_dict(gtkpspell);
1281 misspelled = check_at(gtkpspell, gtkpspell->start_pos);
1283 if (!gtkpspell->continue_check) {
1284 gtkpspell->misspelled = misspelled;
1285 if (gtkpspell->misspelled) {
1286 misspelled_suggest(gtkpspell, gtkpspell->theword);
1287 gtk_stext_set_point(GTK_STEXT(gtkpspell->gtktext),
1288 gtkpspell->end_pos);
1289 gtk_editable_set_position(GTK_EDITABLE(gtkpspell->gtktext),
1290 gtkpspell->end_pos);
1291 gtk_menu_popup(make_sug_menu(gtkpspell), NULL, NULL,
1292 set_menu_pos, gtkpspell, 0,
1297 gtkpspell->orig_pos = gtkpspell->start_pos;
1299 set_point_continue(gtkpspell);
1302 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
1308 GtkWidget *ok_button;
1309 GtkWidget *cancel_button;
1312 GtkPspell *gtkpspell = (GtkPspell *) data;
1314 gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
1316 gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
1318 dialog = gtk_dialog_new();
1319 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
1320 gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
1321 gtk_widget_set_uposition(dialog, xx, yy);
1323 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
1324 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1325 GTK_OBJECT(dialog));
1327 hbox = gtk_hbox_new(FALSE, 0);
1328 gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
1330 thelabel = g_strdup_printf(_("Replace \"%s\" with: "),
1331 gtkpspell->theword);
1332 label = gtk_label_new(thelabel);
1334 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1336 entry = gtk_entry_new();
1337 gtkpspell->replace_entry = entry;
1338 gtk_entry_set_text(GTK_ENTRY(entry), gtkpspell->theword);
1339 gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
1340 gtk_signal_connect(GTK_OBJECT(entry), "activate",
1341 GTK_SIGNAL_FUNC(replace_with_supplied_word_cb),
1343 gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
1344 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1345 GTK_OBJECT(dialog));
1346 gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1348 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE,
1350 if (!gtkpspell->gtkpspeller->ispell) {
1351 label = gtk_label_new(_("Holding down MOD1 key while pressing Enter\nwill learn from mistake.\n"));
1352 gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
1353 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1354 gtk_misc_set_padding(GTK_MISC(label), 8, 0);
1355 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label,
1359 hbox = gtk_hbox_new(TRUE, 0);
1361 ok_button = gtk_button_new_with_label(_("OK"));
1362 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
1363 gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
1364 GTK_SIGNAL_FUNC(replace_with_supplied_word_cb),
1366 gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
1367 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1368 GTK_OBJECT(dialog));
1370 cancel_button = gtk_button_new_with_label(_("Cancel"));
1371 gtk_box_pack_start(GTK_BOX(hbox), cancel_button, TRUE, TRUE, 8);
1372 gtk_signal_connect_object(GTK_OBJECT(cancel_button), "clicked",
1373 GTK_SIGNAL_FUNC(gtk_widget_destroy),
1374 GTK_OBJECT(dialog));
1376 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
1378 gtk_widget_grab_focus(entry);
1380 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
1382 gtk_widget_show_all(dialog);
1385 void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
1392 gtktext = gtkpspell->gtktext;
1394 adj_value = gtktext->vadj->value;
1396 gtk_stext_freeze(gtktext);
1398 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1400 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1402 gtk_stext_set_point(gtktext, 0);
1403 gtk_stext_forward_delete(gtktext, gtk_stext_get_length(gtktext));
1404 gtk_stext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1406 gtk_stext_thaw(gtktext);
1408 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1409 gtk_stext_set_point(gtktext, origpos);
1410 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1416 static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
1418 GtkPspell *gtkpspell = (GtkPspell *) data;
1420 gtkpspell->check_while_typing = gtkpspell->check_while_typing == FALSE;
1421 if (!gtkpspell->check_while_typing)
1422 gtkpspell_uncheck_all(gtkpspell);
1424 if (gtkpspell->config_menu)
1425 populate_submenu(gtkpspell, gtkpspell->config_menu);
1428 static GSList *create_empty_dictionary_list(void)
1430 GSList *list = NULL;
1433 dict = g_new0(Dictionary, 1);
1434 dict->fullname = g_strdup(_("None"));
1435 dict->dictname = dict->fullname;
1436 dict->encoding = NULL;
1437 return g_slist_append(list, dict);
1440 /* gtkpspell_get_dictionary_list() - returns list of dictionary names */
1441 GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path, gint refresh)
1444 gchar *dict_path, *tmp, *prevdir;
1445 gchar tmpname[BUFSIZE];
1450 if (!gtkpspellcheckers)
1451 gtkpspellcheckers = gtkpspell_checkers_new();
1453 if (gtkpspellcheckers->dictionary_list && !refresh)
1454 return gtkpspellcheckers->dictionary_list;
1456 gtkpspell_free_dictionary_list(gtkpspellcheckers->dictionary_list);
1461 #warning TODO: no directory change
1463 dict_path = g_strdup(pspell_path);
1464 prevdir = g_get_current_dir();
1465 if (chdir(dict_path) <0) {
1466 debug_print(_("Pspell: error when searching for dictionaries:\n%s\n"),
1470 gtkpspellcheckers->dictionary_list = create_empty_dictionary_list();
1471 return gtkpspellcheckers->dictionary_list;
1474 debug_print(_("Pspell: checking for dictionaries in %s\n"), dict_path);
1476 if (NULL != (dir = opendir("."))) {
1477 while (NULL != (ent = readdir(dir))) {
1478 /* search for pwli */
1479 if ((NULL != (tmp = strstr2(ent->d_name, ".pwli"))) &&
1481 g_snprintf(tmpname, BUFSIZE, "%s%s",
1482 G_DIR_SEPARATOR_S, ent->d_name);
1483 tmpname[MIN(tmp - ent->d_name + 1, BUFSIZE-1)] = 0x00;
1484 dict = g_new0(Dictionary, 1);
1485 dict->fullname = g_strdup_printf("%s%s",
1488 dict->dictname = strrchr(dict->fullname,
1489 G_DIR_SEPARATOR) + 1;
1490 dict->encoding = NULL;
1491 debug_print(_("Pspell: found dictionary %s %s\n"),
1492 dict->fullname, dict->dictname);
1493 list = g_slist_insert_sorted(list, dict,
1494 (GCompareFunc) compare_dict);
1500 debug_print(_("Pspell: error when searching for dictionaries.\nNo dictionary found.\n(%s)"),
1502 list = create_empty_dictionary_list();
1506 debug_print(_("Pspell: error when searching for dictionaries.\nNo dictionary found."));
1507 list = create_empty_dictionary_list();
1512 gtkpspellcheckers->dictionary_list = list;
1516 void gtkpspell_free_dictionary_list(GSList *list)
1520 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1522 dict = (Dictionary *) walk->data;
1523 dictionary_delete(dict);
1529 static void dictionary_option_menu_item_data_destroy(gpointer data)
1531 Dictionary *dict = (Dictionary *) data;
1534 dictionary_delete(dict);
1538 GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
1540 GSList *dict_list, *tmp;
1545 dict_list = gtkpspell_get_dictionary_list(pspell_path, TRUE);
1546 g_return_val_if_fail(dict_list, NULL);
1548 menu = gtk_menu_new();
1550 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1551 dict = (Dictionary *) tmp->data;
1552 item = gtk_menu_item_new_with_label(dict->dictname);
1553 gtk_object_set_data(GTK_OBJECT(item), "dict_name",
1556 gtk_menu_append(GTK_MENU(menu), item);
1557 gtk_widget_show(item);
1560 gtk_widget_show(menu);
1565 gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
1567 GtkWidget *menuitem;
1568 gchar *dict_fullname;
1571 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1572 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1573 dict_fullname = (gchar *) gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
1574 g_return_val_if_fail(dict_fullname, NULL);
1575 label = g_strdup(dict_fullname);
1580 GtkWidget *gtkpspell_sugmode_option_menu_new(gint sugmode)
1586 menu = gtk_menu_new();
1587 gtk_widget_show(menu);
1589 item = gtk_menu_item_new_with_label(_("Fast Mode"));
1590 gtk_widget_show(item);
1591 gtk_menu_append(GTK_MENU(menu), item);
1592 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_FASTMODE));
1594 item = gtk_menu_item_new_with_label(_("Normal Mode"));
1595 gtk_widget_show(item);
1596 gtk_menu_append(GTK_MENU(menu), item);
1597 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_NORMALMODE));
1599 item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
1600 gtk_widget_show(item);
1601 gtk_menu_append(GTK_MENU(menu), item);
1602 gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_BADSPELLERMODE));
1607 void gtkpspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
1609 g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
1611 g_return_if_fail(sugmode == PSPELL_FASTMODE ||
1612 sugmode == PSPELL_NORMALMODE ||
1613 sugmode == PSPELL_BADSPELLERMODE);
1615 gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
1618 gint gtkpspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
1623 g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
1625 item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
1627 sugmode = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "sugmode"));
1633 static void use_alternate_dict(GtkPspell *gtkpspell)
1637 tmp = gtkpspell->gtkpspeller;
1638 gtkpspell->gtkpspeller = gtkpspell->alternate_speller;
1639 gtkpspell->alternate_speller = tmp;
1641 if (gtkpspell->config_menu)
1642 populate_submenu(gtkpspell, gtkpspell->config_menu);
1646 #if 0 /* Experimenal */
1647 void gtkpspell_set_primary_dict(GtkPspell *gtkpspell, gchar *dict, gchar *encoding)
1649 g_return_if_fail(gtkpspell);
1650 if (gtkpspell->dict1)
1651 dictionary_delete(gtkpspell->dict1);
1652 gtkpspell->dict1 = g_new(Dictionary, 1);
1653 gtkpspell->dict1->fullname = g_strdup(dict);
1654 gtkpspell->dict1->encoding = g_strdup(encoding);
1657 void gtkpspell_set_secondary_dict(GtkPspell *gtkpspell, gchar *dict, gchar *encoding)
1659 g_return_if_fail(gtkpspell);
1660 if (gtkpspell->dict2)
1661 dictionary_delete(gtkpspell->dict2);
1662 gtkpspell->dict2 = g_new(Dictionary, 1);
1663 gtkpspell->dict2->fullname = g_strdup(dict);
1664 gtkpspell->dict2->encoding = g_strdup(encoding);
1667 gboolean gtkpspell_use_dict(GtkPspell *gtkpspell, Dictionary *dict)
1672 gboolean gtkpspell_use_dictionary(GtkPspell *gtkpspell, gchar *dictpath, gchar *encoding)
1677 g_return_val_if_fail(gtkpspell, FALSE);
1678 g_return_val_if_fail(dict, FALSE);
1679 g_return_val_if_fail(encoding, FALSE);
1681 dict = g_new(Dictionary, 1);
1683 dict->fullname = g_strdup(dictpath);
1684 dict->encoding = NULL; /* To be continued */
1686 retval = gtkpspell_use_dict(gtkpspell, dict);
1688 dictionary_delete(dict);
1694 gboolean gtkpspell_use_primary(GtkPspell *gtkpspell)
1696 g_return_val_if_fail(gtkpspell, FALSE);
1698 return gtkpspell_use_dict(gtkpspell, gtkpspell->dict1);
1701 gboolean gtkpspell_use_secondary(GtkPspell *gtkpspell)
1703 g_return_val_if_fail(gtkpspell, FALSE);
1705 return gtkpspell_use_dict(gtkpspell, gtkpspell->dict2);
1709 /*****************************************************************************/
1711 static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
1715 gtktext = gtkpspell->gtktext;
1716 gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1718 if (!(eb->state & GDK_SHIFT_MASK)) {
1719 if (check_at(gtkpspell, gtkpspell->orig_pos)) {
1721 gtk_editable_set_position(GTK_EDITABLE(gtktext),
1722 gtkpspell->orig_pos);
1723 gtk_stext_set_point(gtktext, gtkpspell->orig_pos);
1725 if (misspelled_suggest(gtkpspell, gtkpspell->theword)) {
1726 gtk_menu_popup(make_sug_menu(gtkpspell),
1727 NULL, NULL, NULL, NULL,
1728 eb->button, GDK_CURRENT_TIME);
1733 gtk_editable_set_position(GTK_EDITABLE(gtktext),
1734 gtkpspell->orig_pos);
1735 gtk_stext_set_point(gtktext, gtkpspell->orig_pos);
1739 gtk_menu_popup(make_config_menu(gtkpspell),NULL,NULL,NULL,NULL,
1740 eb->button, GDK_CURRENT_TIME);
1743 /* make_sug_menu() - Add menus to accept this word for this session and to add it to
1744 * personal dictionary */
1745 static GtkMenu *make_sug_menu(GtkPspell *gtkpspell)
1747 GtkWidget *menu, *item;
1748 unsigned char *caption;
1750 GList *l = gtkpspell->suggestions_list;
1751 GtkAccelGroup *accel;
1754 gtktext = gtkpspell->gtktext;
1756 accel = gtk_accel_group_new();
1757 menu = gtk_menu_new();
1759 if (gtkpspell->sug_menu)
1760 gtk_widget_destroy(gtkpspell->sug_menu);
1762 gtkpspell->sug_menu = menu;
1764 gtk_signal_connect(GTK_OBJECT(menu), "cancel",
1765 GTK_SIGNAL_FUNC(cancel_menu_cb), gtkpspell);
1767 caption = g_strdup_printf(_("\"%s\" unknown in %s"),
1768 (unsigned char*) l->data,
1769 gtkpspell->gtkpspeller->dictionary->dictname);
1770 item = gtk_menu_item_new_with_label(caption);
1771 gtk_widget_show(item);
1772 gtk_menu_append(GTK_MENU(menu), item);
1773 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1776 item = gtk_menu_item_new();
1777 gtk_widget_show(item);
1778 gtk_menu_append(GTK_MENU(menu), item);
1780 item = gtk_menu_item_new_with_label(_("Accept in this session"));
1781 gtk_widget_show(item);
1782 gtk_menu_append(GTK_MENU(menu), item);
1783 gtk_signal_connect(GTK_OBJECT(item), "activate",
1784 GTK_SIGNAL_FUNC(add_word_to_session_cb),
1786 gtk_widget_add_accelerator(item, "activate", accel, GDK_space, GDK_MOD1_MASK,
1787 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1789 item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
1790 gtk_widget_show(item);
1791 gtk_menu_append(GTK_MENU(menu), item);
1792 gtk_signal_connect(GTK_OBJECT(item), "activate",
1793 GTK_SIGNAL_FUNC(add_word_to_personal_cb),
1795 gtk_widget_add_accelerator(item, "activate", accel, GDK_Return, GDK_MOD1_MASK,
1796 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1798 item = gtk_menu_item_new_with_label(_("Replace with..."));
1799 gtk_widget_show(item);
1800 gtk_menu_append(GTK_MENU(menu), item);
1801 gtk_signal_connect(GTK_OBJECT(item), "activate",
1802 GTK_SIGNAL_FUNC(replace_with_create_dialog_cb),
1804 gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
1805 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1807 if (gtkpspell->use_alternate && gtkpspell->alternate_speller) {
1808 caption = g_strdup_printf(_("Check with %s"),
1809 gtkpspell->alternate_speller->dictionary->dictname);
1810 item = gtk_menu_item_new_with_label(caption);
1812 gtk_widget_show(item);
1813 gtk_menu_append(GTK_MENU(menu), item);
1814 gtk_signal_connect(GTK_OBJECT(item), "activate",
1815 GTK_SIGNAL_FUNC(check_with_alternate_cb),
1817 gtk_widget_add_accelerator(item, "activate", accel, GDK_X, 0,
1818 GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
1820 item = gtk_menu_item_new();
1821 gtk_widget_show(item);
1822 gtk_menu_append(GTK_MENU(menu), item);
1826 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1827 gtk_widget_show(item);
1828 gtk_menu_append(GTK_MENU(menu), item);
1830 GtkWidget *curmenu = menu;
1834 if (l->data == NULL && l->next != NULL) {
1836 curmenu = gtk_menu_new();
1837 item = gtk_menu_item_new_with_label(_("Others..."));
1838 gtk_widget_show(item);
1839 gtk_menu_append(GTK_MENU(curmenu), item);
1840 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1843 } else if (count > MENUCOUNT) {
1846 item = gtk_menu_item_new_with_label(_("More..."));
1847 gtk_widget_show(item);
1848 gtk_menu_append(GTK_MENU(curmenu), item);
1850 curmenu = gtk_menu_new();
1851 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1854 item = gtk_menu_item_new_with_label((unsigned char*)l->data);
1855 gtk_widget_show(item);
1856 gtk_menu_append(GTK_MENU(curmenu), item);
1857 gtk_signal_connect(GTK_OBJECT(item), "activate",
1858 GTK_SIGNAL_FUNC(replace_word_cb), gtkpspell);
1860 if (curmenu == menu && count < MENUCOUNT) {
1861 gtk_widget_add_accelerator(item, "activate",
1866 gtk_widget_add_accelerator(item, "activate",
1875 } while ((l = l->next) != NULL);
1878 gtk_accel_group_attach(accel, GTK_OBJECT(menu));
1879 gtk_accel_group_unref(accel);
1881 return GTK_MENU(menu);
1884 static void populate_submenu(GtkPspell *gtkpspell, GtkWidget *menu)
1886 GtkWidget *item, *submenu;
1887 GtkPspeller *gtkpspeller = gtkpspell->gtkpspeller;
1889 gint ispell = gtkpspeller->ispell;
1891 if (GTK_MENU_SHELL(menu)->children) {
1892 GList *amenu, *alist;
1893 for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
1894 alist = amenu->next;
1895 gtk_widget_destroy(GTK_WIDGET(amenu->data));
1900 dictname = g_strdup_printf(_("Dictionary: %s"), gtkpspeller->dictionary->dictname);
1901 item = gtk_menu_item_new_with_label(dictname);
1902 gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
1904 gtk_widget_show(item);
1905 gtk_menu_append(GTK_MENU(menu), item);
1907 item = gtk_menu_item_new();
1908 gtk_widget_show(item);
1909 gtk_menu_append(GTK_MENU(menu), item);
1911 if (gtkpspell->use_alternate && gtkpspell->alternate_speller) {
1912 dictname = g_strdup_printf(_("Use alternate (%s)"),
1913 gtkpspell->alternate_speller->dictionary->dictname);
1914 item = gtk_menu_item_new_with_label(dictname);
1916 gtk_signal_connect(GTK_OBJECT(item), "activate",
1917 GTK_SIGNAL_FUNC(switch_to_alternate_cb),
1919 gtk_widget_show(item);
1920 gtk_menu_append(GTK_MENU(menu), item);
1923 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
1924 if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_FASTMODE)
1925 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1926 if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_FASTMODE)
1927 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1929 gtk_signal_connect(GTK_OBJECT(item), "activate",
1930 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1932 gtk_widget_show(item);
1933 gtk_menu_append(GTK_MENU(menu), item);
1935 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
1936 if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_NORMALMODE)
1937 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1938 if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_NORMALMODE)
1939 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1941 gtk_signal_connect(GTK_OBJECT(item), "activate",
1942 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1944 gtk_widget_show(item);
1945 gtk_menu_append(GTK_MENU(menu),item);
1947 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
1948 if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_BADSPELLERMODE)
1949 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1950 if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_BADSPELLERMODE)
1951 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1953 gtk_signal_connect(GTK_OBJECT(item), "activate",
1954 GTK_SIGNAL_FUNC(set_sug_mode_cb),
1956 gtk_widget_show(item);
1957 gtk_menu_append(GTK_MENU(menu), item);
1959 item = gtk_menu_item_new();
1960 gtk_widget_show(item);
1961 gtk_menu_append(GTK_MENU(menu), item);
1963 item = gtk_check_menu_item_new_with_label(_("Check while typing"));
1964 if (gtkpspell->check_while_typing)
1965 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1967 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
1968 gtk_signal_connect(GTK_OBJECT(item), "activate",
1969 GTK_SIGNAL_FUNC(toggle_check_while_typing_cb),
1971 gtk_widget_show(item);
1972 gtk_menu_append(GTK_MENU(menu), item);
1974 item = gtk_menu_item_new();
1975 gtk_widget_show(item);
1976 gtk_menu_append(GTK_MENU(menu), item);
1978 submenu = gtk_menu_new();
1979 item = gtk_menu_item_new_with_label(_("Change dictionary"));
1980 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
1981 gtk_widget_show(item);
1982 gtk_menu_append(GTK_MENU(menu), item);
1985 if (gtkpspellcheckers->dictionary_list == NULL)
1986 gtkpspell_get_dictionary_list(prefs_common.pspell_path, FALSE);
1988 GtkWidget * curmenu = submenu;
1992 tmp = gtkpspellcheckers->dictionary_list;
1994 for (tmp = gtkpspellcheckers->dictionary_list; tmp != NULL;
1995 tmp = g_slist_next(tmp)) {
1996 dict = (Dictionary *) tmp->data;
1997 item = gtk_check_menu_item_new_with_label(dict->dictname);
1998 gtk_object_set_data(GTK_OBJECT(item), "dict_name", dict->fullname);
1999 if (strcmp2(dict->fullname, gtkpspell->gtkpspeller->dictionary->fullname))
2000 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
2002 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
2003 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
2005 gtk_signal_connect(GTK_OBJECT(item), "activate",
2006 GTK_SIGNAL_FUNC(change_dict_cb),
2008 gtk_widget_show(item);
2009 gtk_menu_append(GTK_MENU(curmenu), item);
2013 if (count == MENUCOUNT) {
2016 newmenu = gtk_menu_new();
2017 item = gtk_menu_item_new_with_label(_("More..."));
2018 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
2021 gtk_menu_append(GTK_MENU(curmenu), item);
2022 gtk_widget_show(item);
2034 static GtkMenu *make_config_menu(GtkPspell *gtkpspell)
2036 if (!gtkpspell->popup_config_menu)
2037 gtkpspell->popup_config_menu = gtk_menu_new();
2039 debug_print("Pspell: creating/using popup_config_menu %0x\n",
2040 gtkpspell->popup_config_menu);
2041 populate_submenu(gtkpspell, gtkpspell->popup_config_menu);
2043 return GTK_MENU(gtkpspell->popup_config_menu);
2046 void gtkpspell_populate_submenu(GtkPspell *gtkpspell, GtkWidget *menuitem)
2050 menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
2052 debug_print("Pspell: using config menu %0x\n",
2053 gtkpspell->popup_config_menu);
2054 populate_submenu(gtkpspell, menu);
2056 gtkpspell->config_menu = menu;
2060 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y, gpointer data)
2062 GtkPspell *gtkpspell = (GtkPspell *) data;
2063 gint xx = 0, yy = 0;
2066 GtkSText *text = GTK_STEXT(gtkpspell->gtktext);
2069 gdk_window_get_origin(GTK_WIDGET(gtkpspell->gtktext)->window, &xx, &yy);
2071 sx = gdk_screen_width();
2072 sy = gdk_screen_height();
2074 gtk_widget_get_child_requisition(GTK_WIDGET(menu), &r);
2079 *x = gtkpspell->gtktext->cursor_pos_x + xx +
2080 gdk_char_width(GTK_WIDGET(text)->style->font, ' ');
2081 *y = gtkpspell->gtktext->cursor_pos_y + yy;
2087 gdk_string_height((GTK_WIDGET(gtkpspell->gtktext))->style->font,
2088 gtkpspell->theword);
2092 /*************************************************************************/
2093 /* Menu call backs */
2095 static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
2097 GtkPspell *gtkpspell = (GtkPspell *) data;
2098 gtkpspell->continue_check = NULL;
2099 set_point_continue(gtkpspell);
2103 /* change_dict_cb() - Menu callback : change dict */
2104 static void change_dict_cb(GtkWidget *w, GtkPspell *gtkpspell)
2108 GtkPspeller *gtkpspeller;
2111 fullname = (gchar *) gtk_object_get_data(GTK_OBJECT(w), "dict_name");
2113 if (!strcmp2(fullname, _("None")))
2116 sug_mode = gtkpspell->default_sug_mode;
2118 dict = g_new0(Dictionary, 1);
2119 dict->fullname = g_strdup(fullname);
2120 dict->encoding = g_strdup(gtkpspell->gtkpspeller->dictionary->encoding);
2122 if (gtkpspell->use_alternate && gtkpspell->alternate_speller &&
2123 dict == gtkpspell->alternate_speller->dictionary) {
2124 use_alternate_dict(gtkpspell);
2125 dictionary_delete(dict);
2129 gtkpspeller = gtkpspeller_new(dict);
2133 message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"),
2134 gtkpspellcheckers->error_message);
2136 gtkpspell_alert_dialog(message);
2139 if (gtkpspell->use_alternate) {
2140 if (gtkpspell->alternate_speller)
2141 gtkpspeller_delete(gtkpspell->alternate_speller);
2142 gtkpspell->alternate_speller = gtkpspell->gtkpspeller;
2144 gtkpspeller_delete(gtkpspell->gtkpspeller);
2146 gtkpspell->gtkpspeller = gtkpspeller;
2147 gtkpspell_set_sug_mode(gtkpspell, sug_mode);
2150 dictionary_delete(dict);
2152 if (gtkpspell->config_menu)
2153 populate_submenu(gtkpspell, gtkpspell->config_menu);
2157 static void switch_to_alternate_cb(GtkWidget *w,
2160 GtkPspell *gtkpspell = (GtkPspell *) data;
2161 use_alternate_dict(gtkpspell);
2164 /******************************************************************************/
2165 /* Misc. helper functions */
2167 static void set_point_continue(GtkPspell *gtkpspell)
2171 gtktext = gtkpspell->gtktext;
2173 gtk_stext_freeze(gtktext);
2174 gtk_editable_set_position(GTK_EDITABLE(gtktext),gtkpspell->orig_pos);
2175 gtk_stext_set_point(gtktext, gtkpspell->orig_pos);
2176 gtk_stext_thaw(gtktext);
2178 if (gtkpspell->continue_check)
2179 gtkpspell->continue_check((gpointer *) gtkpspell);
2182 static void allocate_color(GtkPspell *gtkpspell, gint rgbvalue)
2185 GdkColor *color = &(gtkpspell->highlight);
2187 gc = gtk_widget_get_colormap(GTK_WIDGET(gtkpspell->gtktext));
2189 if (gtkpspell->highlight.pixel)
2190 gdk_colormap_free_colors(gc, &(gtkpspell->highlight), 1);
2192 /* Shameless copy from Sylpheed's gtkutils.c */
2194 color->red = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
2195 color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >> 8) / 255.0) * 65535.0);
2196 color->blue = (int) (((gdouble) (rgbvalue & 0x0000ff) / 255.0) * 65535.0);
2198 gdk_colormap_alloc_color(gc, &(gtkpspell->highlight), FALSE, TRUE);
2201 static void change_color(GtkPspell * gtkpspell,
2202 gint start, gint end,
2208 g_return_if_fail(start < end);
2210 gtktext = gtkpspell->gtktext;
2212 gtk_stext_freeze(gtktext);
2214 gtk_stext_set_point(gtktext, start);
2215 gtk_stext_forward_delete(gtktext, end - start);
2216 gtk_stext_insert(gtktext, NULL, color, NULL, newtext, end - start);
2218 gtk_stext_thaw(gtktext);
2221 /* convert_to_pspell_encoding () - converts ISO-8859-* strings to iso8859-*
2222 * as needed by pspell. Returns an allocated string.
2225 static guchar *convert_to_pspell_encoding (const guchar *encoding)
2227 guchar * pspell_encoding;
2229 if (strstr2(encoding, "ISO-8859-")) {
2230 pspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
2233 if (!strcmp2(encoding, "US-ASCII"))
2234 pspell_encoding = g_strdup("iso8859-1");
2236 pspell_encoding = g_strdup(encoding);
2238 return pspell_encoding;
2241 /* compare_dict () - compare 2 dict names */
2242 static gint compare_dict(Dictionary *a, Dictionary *b)
2244 guint aparts = 0, bparts = 0;
2247 for (i=0; i < strlen(a->dictname); i++)
2248 if (a->dictname[i] == '-')
2250 for (i=0; i < strlen(b->dictname); i++)
2251 if (b->dictname[i] == '-')
2254 if (aparts != bparts)
2255 return (aparts < bparts) ? -1 : +1;
2258 compare = strcmp2(a->dictname, b->dictname);
2260 compare = strcmp2(a->fullname, b->fullname);
2266 static void dictionary_delete(Dictionary *dict)
2268 g_free(dict->fullname);
2269 g_free(dict->encoding);
2273 static Dictionary *dictionary_dup(const Dictionary *dict)
2277 dict2 = g_new(Dictionary, 1);
2279 dict2->fullname = g_strdup(dict->fullname);
2280 dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
2281 dict2->encoding = g_strdup(dict->encoding);
2286 static void free_suggestions_list(GtkPspell *gtkpspell)
2290 for (list = gtkpspell->suggestions_list; list != NULL; list = list->next)
2294 gtkpspell->max_sug = -1;
2295 gtkpspell->suggestions_list = NULL;
2299 static void reset_theword_data(GtkPspell *gtkpspell)
2301 gtkpspell->start_pos = 0;
2302 gtkpspell->end_pos = 0;
2303 gtkpspell->theword[0] = 0;
2304 gtkpspell->max_sug = -1;
2306 free_suggestions_list(gtkpspell);
2309 static void free_checkers(gpointer elt, gpointer data)
2311 GtkPspeller *gtkpspeller = elt;
2313 g_return_if_fail(gtkpspeller);
2315 gtkpspeller_real_delete(gtkpspeller);
2318 static gint find_gtkpspeller(gconstpointer aa, gconstpointer bb)
2320 Dictionary *a = ((GtkPspeller *) aa)->dictionary;
2321 Dictionary *b = ((GtkPspeller *) bb)->dictionary;
2323 if (a && b && a->fullname && b->fullname &&
2324 strcmp(a->fullname, b->fullname) == 0 &&
2325 a->encoding && b->encoding)
2326 return strcmp(a->encoding, b->encoding);
2331 static void gtkpspell_alert_dialog(gchar *message)
2336 GtkWidget *ok_button;
2338 dialog = gtk_dialog_new();
2339 gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
2340 gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_MOUSE);
2341 gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
2342 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2343 GTK_OBJECT(dialog));
2345 label = gtk_label_new(message);
2346 gtk_misc_set_padding(GTK_MISC(label), 8, 8);
2348 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
2350 hbox = gtk_hbox_new(FALSE, 0);
2352 ok_button = gtk_button_new_with_label(_("OK"));
2353 GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
2354 gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
2356 gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
2357 GTK_SIGNAL_FUNC(gtk_widget_destroy),
2358 GTK_OBJECT(dialog));
2359 gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
2361 gtk_widget_grab_default(ok_button);
2362 gtk_widget_grab_focus(ok_button);
2363 gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
2365 gtk_widget_show_all(dialog);