1 /* gtkpspell - a spell-checking addon for GtkText
2 * Copyright (c) 2000 Evan Martin.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * 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 by Hiroyuki Yamamoto &
22 * The Sylpheed Claws Team.
23 * Adapted for pspell (c) 2001 Melvin Hadasht
26 #if defined(HAVE_CONFIG_H)
34 #include <sys/types.h>
47 #include <prefs_common.h>
52 #include <gtk/gtkoptionmenu.h>
53 #include <gtk/gtkmenu.h>
54 #include <gtk/gtkmenuitem.h>
60 #include <pspell/pspell.h>
62 /* size of the text buffer used in various word-processing routines. */
65 /* number of suggestions to display on each menu. */
68 /******************************************************************************/
70 /* Function called from menus */
72 static void add_word_to_session (GtkWidget *w, GtkPspell *d);
73 static void add_word_to_personal (GtkWidget *w, GtkPspell *d);
74 static void set_sug_mode (GtkWidget *w, GtkPspell *gtkpspell);
75 static void check_all (GtkWidget *w, GtkPspell *gtkpspell);
76 static void menu_change_dict (GtkWidget *w, GtkPspell *gtkpspell);
77 static void entry_insert_cb (GtkXText *gtktext, gchar *newtext,
78 guint len, guint *ppos,
79 GtkPspell *gtkpspell);
81 /* gtkspellconfig - only one config per session */
82 GtkPspellConfig * gtkpspellconfig;
84 /* TODO: configurable */
85 static GdkColor highlight = { 0, 255 * 256, 0, 0 };
87 /******************************************************************************/
89 /* gtkspell_init() - run the first pspell_config from which every
90 * new config is cloned
92 GtkPspellConfig * gtkpspell_init()
94 return new_pspell_config();
97 /* gtkspell_finished() - Finish all. No more spelling. Called when the
100 void gtkpspell_finished(GtkPspellConfig *gtkpspellconfig)
102 if (gtkpspellconfig) {
103 delete_pspell_config(gtkpspellconfig);
104 gtkpspellconfig = NULL;
108 /* gtkspell_running - Test if there is a manager running
110 int gtkpspell_running(GtkPspell * gtkpspell)
112 return (gtkpspell->config!=NULL);
115 /* gtkspell_new() - creates a new session if a gtkpspellconfig exists. The
116 * settings are defaults. If no path/dict is set afterwards, the default
119 GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
121 GtkPspell *gtkpspell;
123 if (gtkpspellconfig == NULL) {
124 gtkpspellconfig = gtkpspell_init();
125 if (gtkpspellconfig == NULL) {
126 debug_print(_("Pspell could not be started."));
127 prefs_common.enable_pspell=FALSE;
132 gtkpspell = g_new(GtkPspell ,1);
133 gtkpspell->config = gtkpspellconfig;
134 gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
135 gtkpspell->checker = NULL;
137 if (pspell_error_number(gtkpspell->possible_err) != 0) {
138 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
139 delete_pspell_can_have_error( gtkpspell->possible_err );
142 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
145 gtkpspell->dictionary_list = NULL;
146 gtkpspell->path = NULL;
147 gtkpspell->dict = NULL;
148 gtkpspell->mode = PSPELL_FASTMODE;
149 gtkpspell->gtktext = NULL;
153 /* gtkspell_new_with_config() - Creates a new session and set path/dic
155 GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig,
156 guchar *path, guchar *dict,
157 guint mode, guchar *encoding)
159 GtkPspell *gtkpspell;
161 if (gtkpspellconfig == NULL) {
162 gtkpspellconfig=gtkpspell_init();
163 if (gtkpspellconfig == NULL) {
164 debug_print(_("Pspell could not be started."));
165 prefs_common.enable_pspell = FALSE;
170 gtkpspell = g_new( GtkPspell ,1);
171 gtkpspell->path = NULL;
172 gtkpspell->dict = NULL;
173 gtkpspell->dictionary_list = NULL;
174 gtkpspell->gtktext = NULL;
176 gtkpspell->config = pspell_config_clone(gtkpspellconfig);
177 gtkpspell->mode = PSPELL_FASTMODE;
179 if (!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)) {
180 debug_print(_("Pspell could not be configured."));
181 gtkpspell = gtkpspell_delete(gtkpspell);
186 pspell_config_replace(gtkpspell->config,"encoding",encoding);
188 gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
189 gtkpspell->checker = NULL;
191 if (pspell_error_number(gtkpspell->possible_err) != 0) {
192 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
193 delete_pspell_can_have_error(gtkpspell->possible_err);
194 gtkpspell = gtkpspell_delete(gtkpspell);
197 gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
202 /* gtkspell_delete() - Finishes a session
204 GtkPspell *gtkpspell_delete( GtkPspell *gtkpspell )
206 if ( gtkpspell->checker ) {
207 delete_pspell_manager(gtkpspell->checker);
208 gtkpspell->checker = NULL;
209 gtkpspell->possible_err = NULL; /* checker is a cast from possible_err */
212 if (gtkpspell->dictionary_list)
213 gtkpspell_free_dictionary_list(gtkpspell->dictionary_list);
215 g_free(gtkpspell->path);
216 g_free(gtkpspell->dict);
217 gtkpspell->path = NULL;
218 gtkpspell->dict = NULL;
224 int set_path_and_dict(GtkPspell *gtkpspell, PspellConfig *config,
225 guchar *path, guchar * dict)
227 guchar *module = NULL;
228 guchar *language = NULL;
229 guchar *spelling = NULL;
230 guchar *jargon = NULL;
236 /* Change nothing if any of path/dict/config is NULL */
237 g_return_val_if_fail(path, 0);
238 g_return_val_if_fail(dict, 0);
239 g_return_val_if_fail(config, 0);
241 /* This is done, so we can free gtkpspell->path, even if it was
242 * given as an argument of the function */
243 temppath = g_strdup(path);
244 g_free(gtkpspell->path);
246 /* pspell dict name format : */
247 /* <lang>[[-<spelling>[-<jargon>]]-<module>.pwli */
250 if (!strrchr(dict, G_DIR_SEPARATOR)) { /* plain dict name */
251 strncpy(buf, dict, BUFSIZE - 1);
253 else { /* strip path */
254 strncpy(buf, strrchr(dict, G_DIR_SEPARATOR) + 1, BUFSIZE - 1);
256 g_free(gtkpspell->dict);
258 /* Ensure no buffers overflows if the dict is to long */
259 buf[BUFSIZE - 1] = 0x00;
262 if ((module = strrchr(buf,'-')) != NULL) {
264 if ((end = strrchr(module,'.')) != NULL)
268 /* In tempdict, we have only the dict name, without path nor
269 * extension. Useful in the popup menus */
271 tempdict = g_strdup(buf);
273 /* Probably I am too paranoied... */
274 if (!( language[0] != 0x00 && language[1] != 0x00))
277 spelling = strchr(language,'-');
278 if (spelling!=NULL) {
282 if (spelling != module) {
283 if ((end = strchr(spelling, '-' )) != NULL) {
286 if (jargon != module)
287 if ((end = strchr(jargon, '-')) != NULL)
301 debug_print(_("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),
302 language, spelling, jargon, module);
305 pspell_config_replace(config, "language-tag", language);
307 pspell_config_replace(config, "spelling", spelling);
309 pspell_config_replace(config, "jargon", jargon);
311 pspell_config_replace(config, "module", module);
313 pspell_config_replace(config, "word-list-path", temppath);
315 switch(gtkpspell->mode) {
316 case PSPELL_FASTMODE:
317 pspell_config_replace(config, "sug_mode", "fast");
319 case PSPELL_NORMALMODE:
320 pspell_config_replace(config, "sug_mode", "normal");
322 case PSPELL_BADSPELLERMODE:
323 pspell_config_replace(config, "sug_mode", "bad-spellers");
327 gtkpspell->path = g_strdup(temppath);
328 gtkpspell->dict = g_strdup(tempdict);
335 /* gtkpspell_set_path_and_dict() - Set path and dict. The session is
337 * 0 on error, -1 on success */
338 int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path,
341 PspellConfig * config2;
343 /* It seems changing an already running config is not the way to go
346 config2 = pspell_config_clone(gtkpspell->config);
348 if (gtkpspell->checker)
349 delete_pspell_manager(gtkpspell->checker);
351 gtkpspell->checker = NULL;
352 gtkpspell->possible_err = NULL;
354 if (set_path_and_dict(gtkpspell,config2,path,dict) == 0) {
355 debug_print(_("Pspell set_path_and_dict error."));
359 gtkpspell->possible_err = new_pspell_manager(config2);
361 delete_pspell_config(config2);
364 if (pspell_error_number(gtkpspell->possible_err) != 0) {
365 debug_print(_("Pspell path & dict. error %s\n"),
366 pspell_error_message(gtkpspell->possible_err));
367 delete_pspell_can_have_error(gtkpspell->possible_err);
368 gtkpspell->possible_err = NULL;
372 gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
377 /* gtkpspell_get_dict() - What dict are we using ? language-spelling-jargon-module format */
378 /* Actually, this function is not used and hence not tested. */
379 /* Returns an allocated string */
380 guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
388 g_return_val_if_fail(gtkpspell->config, NULL);
390 language = g_strdup(pspell_config_retrieve(gtkpspell->config, "language"));
391 spelling = g_strdup(pspell_config_retrieve(gtkpspell->config, "spelling"));
392 jargon = g_strdup(pspell_config_retrieve(gtkpspell->config, "jargon" ));
393 len = strlen(language) + strlen(spelling) + strlen(jargon);
396 dict = g_new(char,len + 4);
397 strcpy(dict, language);
400 strcat(dict, spelling);
414 /* gtkpspell_get_path() - Return the dict path as an allocated string */
415 /* Not used = not tested */
416 guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
421 g_return_val_if_fail(gtkpspell->config, NULL);
423 path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
428 /* menu_change_dict() - Menu callback : change dict */
429 static void menu_change_dict(GtkWidget *w, GtkPspell *gtkpspell)
434 /* Dict is simply the menu label */
436 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &thelabel);
437 thedict = g_strdup(thelabel);
439 /* Set path, dict, (and sug_mode ?) */
440 gtkpspell_set_path_and_dict(gtkpspell, gtkpspell->path, thedict);
444 /* set_sug_mode() - Menu callback : Set the suggestion mode */
445 static void set_sug_mode(GtkWidget *w, GtkPspell *gtkpspell)
447 unsigned char *themode;
449 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
451 if (!strcmp(themode, _("Fast Mode"))) {
452 gtkpspell_set_sug_mode(gtkpspell, "fast");
453 gtkpspell->mode = PSPELL_FASTMODE;
455 if (!strcmp(themode,_("Normal Mode"))) {
456 gtkpspell_set_sug_mode(gtkpspell, "normal");
457 gtkpspell->mode = PSPELL_NORMALMODE;
459 if (!strcmp( themode,_("Bad Spellers Mode"))) {
460 gtkpspell_set_sug_mode(gtkpspell, "bad-spellers");
461 gtkpspell->mode = PSPELL_BADSPELLERMODE;
465 /* gtkpspell_set_sug_mode() - Set the suggestion mode */
466 /* Actually, the session is resetted and everything is reset. */
467 /* We take the used path/dict pair and create a new config with them */
468 int gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gchar *themode)
470 PspellConfig *config2;
474 delete_pspell_manager(gtkpspell->checker);
475 gtkpspell->checker = NULL;
477 config2 = pspell_config_clone(gtkpspell->config);
479 if (!set_path_and_dict(gtkpspell, config2, gtkpspell->path, gtkpspell->dict)) {
480 debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
484 pspell_config_replace(config2, "sug-mode", themode);
486 gtkpspell->possible_err = new_pspell_manager(config2);
487 delete_pspell_config(config2);
490 if (pspell_error_number(gtkpspell->possible_err) != 0) {
491 debug_print(_("Pspell set sug-mode error %s\n"),
492 pspell_error_message(gtkpspell->possible_err));
493 delete_pspell_can_have_error(gtkpspell->possible_err);
494 gtkpspell->possible_err = NULL;
497 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
501 /* misspelled_suggest() - Create a suggestion list for word */
502 static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word)
504 const guchar *newword;
507 const PspellWordList *suggestions;
508 PspellStringEmulation *elements;
510 g_return_val_if_fail(word, NULL);
512 if (!pspell_manager_check(gtkpspell->checker, word, -1)) {
513 suggestions = pspell_manager_suggest(gtkpspell->checker, (const char *)word, -1);
514 elements = pspell_word_list_elements(suggestions);
515 /* First one must be the misspelled (why this ?) */
516 list = g_list_append(list, g_strdup(word));
518 while ((newword = pspell_string_emulation_next(elements)) != NULL)
519 list = g_list_append(list, g_strdup(newword));
525 /* misspelled_test() - Just test if word is correctly spelled */
526 static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word)
528 return pspell_manager_check(gtkpspell->checker, word, -1) ? 0 : 1;
532 static gboolean iswordsep(unsigned char c)
534 return !isalpha(c) && c != '\'';
537 static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos)
542 text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
550 static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos,
552 int *pstart, int *pend)
557 gtktext = gtkpspell->gtktext;
558 if (iswordsep(get_text_index_whar(gtkpspell, pos)))
561 for (start = pos; start >= 0; --start) {
562 if (iswordsep(get_text_index_whar(gtkpspell, start)))
567 for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
568 if (iswordsep(get_text_index_whar(gtkpspell, end)))
573 for (pos = start; pos < end; pos++)
574 buf[pos - start] = get_text_index_whar(gtkpspell, pos);
575 buf[pos - start] = 0;
586 static gboolean get_curword(GtkPspell *gtkpspell, unsigned char* buf,
587 int *pstart, int *pend)
589 int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
590 return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
593 static void change_color(GtkPspell * gtkpspell,
600 g_return_if_fail(start < end);
602 gtktext = gtkpspell->gtktext;
604 gtk_xtext_freeze(gtktext);
605 newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
607 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
608 GTK_SIGNAL_FUNC(entry_insert_cb),
610 gtk_xtext_set_point(gtktext, start);
611 gtk_xtext_forward_delete(gtktext, end - start);
613 gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
614 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
615 GTK_SIGNAL_FUNC(entry_insert_cb),
618 gtk_xtext_thaw(gtktext);
621 static gboolean check_at(GtkPspell *gtkpspell, int from_pos)
624 unsigned char buf[BUFSIZE];
627 g_return_val_if_fail(from_pos >= 0, FALSE);
629 gtktext = gtkpspell->gtktext;
630 if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
632 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
633 gtkpspell->theword[BUFSIZE - 1] = 0;
635 if (misspelled_test(gtkpspell, buf)) {
636 if (highlight.pixel == 0) {
637 /* add an entry for the highlight in the color map. */
638 GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
639 gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
641 change_color(gtkpspell, start, end, &highlight);
644 change_color(gtkpspell, start, end,
645 &(GTK_WIDGET(gtktext)->style->fg[0]));
651 static void check_all(GtkWidget *w, GtkPspell *gtkpspell)
653 gtkpspell_check_all(gtkpspell);
657 void gtkpspell_check_all(GtkPspell *gtkpspell)
666 if (!gtkpspell_running(gtkpspell)) return ;
667 gtktext = gtkpspell->gtktext;
669 len = gtk_xtext_get_length(gtktext);
671 adj_value = gtktext->vadj->value;
672 gtk_xtext_freeze(gtktext);
673 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
674 gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
676 while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
678 while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
681 check_at(gtkpspell, pos - 1);
683 gtk_xtext_thaw(gtktext);
684 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
685 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
688 static void entry_insert_cb(GtkXText *gtktext, gchar *newtext,
689 guint len, guint *ppos,
690 GtkPspell * gtkpspell)
692 if (!gtkpspell_running(gtkpspell))
695 if (iswordsep(newtext[0])) {
696 /* did we just end a word? */
698 check_at(gtkpspell, *ppos - 2);
700 /* did we just split a word? */
701 if (*ppos < gtk_xtext_get_length(gtktext))
702 check_at(gtkpspell, *ppos + 1);
704 /* check as they type, *except* if they're typing at the end (the most
707 if (*ppos < gtk_xtext_get_length(gtktext)
708 && !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
709 check_at(gtkpspell, *ppos - 1);
714 static void entry_delete_cb(GtkXText *gtktext, gint start, gint end,
715 GtkPspell *gtkpspell)
719 if (!gtkpspell_running(gtkpspell))
722 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
723 check_at(gtkpspell, start - 1);
724 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
725 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
726 /* this is to *UNDO* the selection, in case they were holding shift
727 * while hitting backspace. */
730 static void replace_word(GtkWidget *w, GtkPspell *gtkpspell)
732 int start, end,oldlen, newlen;
734 unsigned char *newword;
735 unsigned char buf[BUFSIZE];
739 gtktext = gtkpspell->gtktext;
741 gtk_xtext_freeze(GTK_XTEXT(gtktext));
742 origpos = gtkpspell->orig_pos;
745 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
746 newlen = strlen(newword);
748 get_curword(gtkpspell, buf, &start, &end);
749 oldlen = end - start;
751 gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
752 gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
753 gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
756 if(end-start>0 /*&& gtkpspell->learn*/){
757 buf[end-start]=0x00; /* Just be sure the buffer is correct... */
758 /* Learn from common misspellings */
759 pspell_manager_store_replacement(gtkpspell->checker,buf,end-start,
760 newword,strlen(newword));
764 /* Put the point and the position where we clicked with the mouse */
765 /* It seems to be a hack, as I must thaw,freeze,thaw the widget */
766 /* to let it update correctly the word insertion and then the */
767 /* point & position position. If not, SEGV after the first replacement */
768 /* If the new word ends before point, put the point at its end*/
770 if (origpos-start <= oldlen && origpos-start >= 0) {
771 /* Original point was in the word. */
772 /* Put the insertion point in the same location */
773 /* with respect to the new length */
774 /* If the original position is still within the word, */
775 /* then keep the original position. If not, move to the */
776 /* end of the word */
777 if (origpos-start > newlen)
778 pos = start + newlen;
780 else if (origpos > end) {
781 /* move the position according to the change of length */
782 pos = origpos + newlen - oldlen;
784 gtk_xtext_thaw(GTK_XTEXT(gtktext));
785 gtk_xtext_freeze(GTK_XTEXT(gtktext));
786 if (GTK_XTEXT(gtktext)->text_len < pos)
787 pos = gtk_xtext_get_length(GTK_XTEXT(gtktext));
788 gtkpspell->orig_pos = pos;
789 gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
790 gtk_xtext_set_point(GTK_XTEXT(gtktext),
791 gtk_editable_get_position(GTK_EDITABLE(gtktext)));
792 gtk_xtext_thaw(GTK_XTEXT(gtktext));
795 /* Accept this word for this session */
797 static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
802 gtkxtext = gtkpspell->gtktext;
803 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
804 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
806 pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword, strlen(gtkpspell->theword));
808 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
810 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
811 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
812 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
813 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
814 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
817 /* add_word_to_personal() - add word to personal dict. Save the list at every addition (may be
818 * not efficient...) */
820 static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
825 gtkxtext = gtkpspell->gtktext;
827 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
828 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
830 pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
831 pspell_manager_save_all_word_lists(gtkpspell->checker);
833 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
834 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
835 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
836 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
837 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
838 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
842 static GtkMenu *make_menu_config(GtkPspell *gtkpspell)
844 GtkWidget *menu, *item, *submenu;
847 menu = gtk_menu_new();
849 item = gtk_menu_item_new_with_label(_("Spell check all"));
850 gtk_widget_show(item);
851 gtk_menu_append(GTK_MENU(menu), item);
852 gtk_signal_connect(GTK_OBJECT(item),"activate",
853 GTK_SIGNAL_FUNC(check_all),
857 item = gtk_menu_item_new();
858 gtk_widget_show(item);
859 gtk_menu_append(GTK_MENU(menu),item);
861 submenu = gtk_menu_new();
862 item = gtk_menu_item_new_with_label(_("Change dictionary"));
863 gtk_widget_show(item);
864 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
865 gtk_menu_append(GTK_MENU(menu), item);
868 if (gtkpspell->dictionary_list==NULL)
869 gtkpspell->dictionary_list = gtkpspell_get_dictionary_list(gtkpspell->path);
872 GtkWidget * curmenu = submenu;
877 tmp = gtkpspell->dictionary_list;
878 for (tmp = gtkpspell->dictionary_list; tmp != NULL; tmp =g_slist_next(tmp)) {
879 dict = (Dictionary *) tmp->data;
880 item = gtk_check_menu_item_new_with_label(dict->name);
881 if (strcmp2(dict->name, gtkpspell->dict))
882 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
884 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
885 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
887 gtk_menu_append(GTK_MENU(curmenu), item);
888 gtk_signal_connect(GTK_OBJECT(item), "activate",
889 GTK_SIGNAL_FUNC(menu_change_dict),
891 gtk_widget_show(item);
893 if (count == MENUCOUNT) {
895 newmenu = gtk_menu_new();
896 item = gtk_menu_item_new_with_label(_("More..."));
897 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), newmenu);
898 gtk_menu_append(GTK_MENU(curmenu), item);
899 gtk_widget_show(item);
906 item = gtk_menu_item_new();
907 gtk_widget_show(item);
908 gtk_menu_append(GTK_MENU(menu), item);
910 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
911 gtk_widget_show(item);
912 gtk_menu_append(GTK_MENU(menu), item);
913 if (gtkpspell->mode == PSPELL_FASTMODE) {
914 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
915 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
918 gtk_signal_connect(GTK_OBJECT(item),"activate",
919 GTK_SIGNAL_FUNC(set_sug_mode),
922 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
923 gtk_widget_show(item);
924 gtk_menu_append(GTK_MENU(menu),item);
925 if (gtkpspell->mode == PSPELL_NORMALMODE) {
926 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
927 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
930 gtk_signal_connect(GTK_OBJECT(item), "activate",
931 GTK_SIGNAL_FUNC(set_sug_mode),
934 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
935 gtk_widget_show(item);
936 gtk_menu_append(GTK_MENU(menu),item);
937 if (gtkpspell->mode==PSPELL_BADSPELLERMODE) {
938 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
939 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
942 gtk_signal_connect(GTK_OBJECT(item),"activate",
943 GTK_SIGNAL_FUNC(set_sug_mode),
946 return GTK_MENU(menu);
949 /* make_menu() - Add menus to accept this word for this session and to add it to
950 * personal dictionary */
951 static GtkMenu *make_menu(GList *l, GtkPspell *gtkpspell)
953 GtkWidget *menu, *item;
954 unsigned char *caption;
957 gtktext = gtkpspell->gtktext;
959 menu = gtk_menu_new();
960 caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
961 item = gtk_menu_item_new_with_label(caption);
963 gtk_widget_show(item);
964 gtk_menu_append(GTK_MENU(menu), item);
966 gtk_signal_connect(GTK_OBJECT(item), "activate",
967 GTK_SIGNAL_FUNC(add_word_to_session),
970 caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
971 item = gtk_menu_item_new_with_label(caption);
973 gtk_widget_show(item);
974 gtk_menu_append(GTK_MENU(menu), item);
976 gtk_signal_connect(GTK_OBJECT(item), "activate",
977 GTK_SIGNAL_FUNC(add_word_to_personal),
980 item = gtk_menu_item_new();
981 gtk_widget_show(item);
982 gtk_menu_append(GTK_MENU(menu), item);
986 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
987 gtk_widget_show(item);
988 gtk_menu_append(GTK_MENU(menu), item);
990 GtkWidget *curmenu = menu;
994 if (l->data == NULL && l->next != NULL) {
996 curmenu = gtk_menu_new();
997 item = gtk_menu_item_new_with_label(_("Others..."));
998 gtk_widget_show(item);
999 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1000 gtk_menu_append(GTK_MENU(curmenu), item);
1002 } else if (count > MENUCOUNT) {
1004 item = gtk_menu_item_new_with_label(_("More..."));
1005 gtk_widget_show(item);
1006 gtk_menu_append(GTK_MENU(curmenu), item);
1007 curmenu = gtk_menu_new();
1008 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1010 item = gtk_menu_item_new_with_label((unsigned char*)l->data);
1011 gtk_signal_connect(GTK_OBJECT(item), "activate",
1012 GTK_SIGNAL_FUNC(replace_word), gtkpspell);
1013 gtk_widget_show(item);
1014 gtk_menu_append(GTK_MENU(curmenu), item);
1016 } while ((l = l->next) != NULL);
1018 return GTK_MENU(menu);
1021 static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
1023 unsigned char buf[BUFSIZE];
1027 gtktext = gtkpspell->gtktext;
1028 gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1030 if (!(eb->state & GDK_SHIFT_MASK))
1031 if (get_curword(gtkpspell, buf, NULL, NULL)) {
1033 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
1034 gtkpspell->theword[BUFSIZE - 1] = 0;
1035 list = misspelled_suggest(gtkpspell, buf);
1037 gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
1038 eb->button, eb->time);
1039 for (l = list; l != NULL; l = l->next)
1046 gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
1047 eb->button,eb->time);
1050 /* ok, this is pretty wacky:
1051 * we need to let the right-mouse-click go through, so it moves the cursor,
1052 * but we *can't* let it go through, because GtkText interprets rightclicks as
1053 * weird selection modifiers.
1055 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
1058 static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell *gtkpspell)
1063 if (!gtkpspell_running(gtkpspell))
1066 if (e->type != GDK_BUTTON_PRESS)
1068 eb = (GdkEventButton*) e;
1070 if (eb->button != 3)
1073 /* forge the leftclick */
1076 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1077 GTK_SIGNAL_FUNC(button_press_intercept_cb),
1079 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
1081 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1082 GTK_SIGNAL_FUNC(button_press_intercept_cb),
1084 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
1086 /* now do the menu wackiness */
1087 popup_menu(gtkpspell, eb);
1088 gtk_grab_remove(GTK_WIDGET(gtktext));
1092 void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
1095 unsigned char *text;
1099 gtktext=gtkpspell->gtktext;
1101 adj_value = gtktext->vadj->value;
1102 gtk_xtext_freeze(gtktext);
1103 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1104 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1105 gtk_xtext_set_point(gtktext, 0);
1106 gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
1107 gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1108 gtk_xtext_thaw(gtktext);
1110 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1111 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1114 void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext)
1116 gtkpspell->gtktext=gtktext;
1117 /* if (prefs_common.auto_makepspell) { */
1118 gtk_signal_connect(GTK_OBJECT(gtktext), "insert-text",
1119 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1120 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
1121 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1123 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
1124 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1128 void gtkpspell_detach(GtkPspell * gtkpspell)
1132 gtktext =gtkpspell->gtktext;
1133 /* if (prefs_common.auto_makepspell) { */
1134 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1135 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1136 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1137 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1139 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1140 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1142 gtkpspell_uncheck_all(gtkpspell);
1145 /*** Sylpheed (Claws) ***/
1147 static GSList *create_empty_dictionary_list(void)
1149 GSList *list = NULL;
1152 dict = g_new0(Dictionary, 1);
1153 dict->name = g_strdup(_("None"));
1154 return g_slist_append(list, dict);
1157 /* gtkpspell_get_dictionary_list() - returns list of dictionary names */
1158 GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
1161 gchar *dict_path, *tmp, *prevdir;
1170 #warning TODO: no directory change
1172 dict_path=g_strdup(pspell_path);
1173 prevdir = g_get_current_dir();
1174 if (chdir(dict_path) <0) {
1175 FILE_OP_ERROR(dict_path, "chdir");
1178 return create_empty_dictionary_list();
1181 debug_print(_("Checking for dictionaries in %s\n"), dict_path);
1183 if (NULL != (dir = opendir("."))) {
1184 while (NULL != (ent = readdir(dir))) {
1185 /* search for pwli */
1186 if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
1187 dict = g_new0(Dictionary, 1);
1188 dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
1189 debug_print(_("Found dictionary %s\n"), dict->name);
1190 list = g_slist_append(list, dict);
1196 FILE_OP_ERROR(dict_path, "opendir");
1197 debug_print(_("No dictionary found\n"));
1198 list = create_empty_dictionary_list();
1201 debug_print(_("No dictionary found"));
1202 list = create_empty_dictionary_list();
1210 void gtkpspell_free_dictionary_list(GSList *list)
1214 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1216 dict = (Dictionary *) walk->data;
1224 static void dictionary_option_menu_item_data_destroy(gpointer data)
1226 gchar *str = (gchar *) data;
1232 GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
1234 GSList *dict_list, *tmp;
1239 dict_list = gtkpspell_get_dictionary_list(pspell_path);
1240 g_return_val_if_fail(dict_list, NULL);
1242 menu = gtk_menu_new();
1244 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1245 dict = (Dictionary *) tmp->data;
1246 item = gtk_menu_item_new_with_label(dict->name);
1248 gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
1249 g_strdup(dict->name),
1250 dictionary_option_menu_item_data_destroy);
1251 gtk_menu_append(GTK_MENU(menu), item);
1252 gtk_widget_show(item);
1255 gtk_widget_show(menu);
1257 gtkpspell_free_dictionary_list(dict_list);
1264 gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
1266 GtkWidget *menuitem;
1269 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1270 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1271 result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
1272 g_return_val_if_fail(result, NULL);
1273 return g_strdup(result);