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
19 Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
23 * Adapted for Sylpheed (Claws) (c) 2001 by Hiroyuki Yamamoto &
24 * The Sylpheed Claws Team.
28 * Adapted for pspell (c) 2001 Melvin Hadasht
31 #if defined(HAVE_CONFIG_H)
39 #include <sys/types.h>
52 #include <prefs_common.h>
57 #include <gtk/gtkoptionmenu.h>
58 #include <gtk/gtkmenu.h>
59 #include <gtk/gtkmenuitem.h>
65 #include <pspell/pspell.h>
67 /* size of the text buffer used in various word-processing routines. */
70 /* number of suggestions to display on each menu. */
74 /* Only one config per session */
76 GtkPspellConfig * gtkpspellconfig;
78 /* Function called from menus */
80 static void add_word_to_session(GtkWidget *w, GtkPspell * d);
81 static void add_word_to_personal(GtkWidget *w, GtkPspell * d);
82 static void set_sug_mode(GtkWidget *w, GtkPspell * gtkpspell);
83 static void check_all(GtkWidget *w, GtkPspell * gtkpspell);
84 static void menu_change_dict(GtkWidget *w, GtkPspell * gtkpspell);
86 static GdkColor highlight = { 0, 255 * 256, 0, 0 };
88 static void entry_insert_cb(GtkXText *gtktext,
89 gchar *newtext, guint len, guint *ppos,
90 GtkPspell *gtkpspell);
92 /* Run the first pspell_config from which every new config is */
95 GtkPspellConfig * gtkpspell_init(){
96 return new_pspell_config();
99 /* Finish all. No more spelling. Called when the program ends */
101 void gtkpspell_finished(GtkPspellConfig * gtkpspellconfig){
103 delete_pspell_config( gtkpspellconfig );
104 gtkpspellconfig = NULL;
109 /* Test if there is a manager running */
111 int gtkpspell_running(GtkPspell * gtkpspell)
113 return (gtkpspell->config!=NULL);
116 /* Creates a new session if a gtkpspellconfig exists */
117 /* The settings are defaults. If no path/dict is set afterwards, the default one is used */
119 GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig){
120 GtkPspell *gtkpspell;
122 if( gtkpspellconfig == NULL ){
123 gtkpspellconfig=gtkpspell_init();
124 if(gtkpspellconfig==NULL){
125 debug_print(_("Pspell could not be started."));
126 prefs_common.enable_pspell=FALSE;
131 gtkpspell = g_new( GtkPspell ,1);
132 gtkpspell->config = gtkpspellconfig;
133 gtkpspell->possible_err = new_pspell_manager( gtkpspell->config );
134 gtkpspell->checker = NULL;
136 if( pspell_error_number( gtkpspell->possible_err ) != 0){
137 debug_print(_("Pspell error : %s\n"), pspell_error_message( gtkpspell->possible_err ));
138 delete_pspell_can_have_error( gtkpspell->possible_err );
141 gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
144 gtkpspell->dictionary_list = NULL;
145 gtkpspell->path = NULL;
146 gtkpspell->dict = NULL;
147 gtkpspell->mode = PSPELL_FASTMODE;
148 gtkpspell->gtktext = NULL;
152 /* Creates a new session and set path/dic */
154 GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig,
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;
178 if(!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)){
179 debug_print(_("Pspell could not be configured."));
180 gtkpspell=gtkpspell_delete(gtkpspell);
184 pspell_config_replace(gtkpspell->config,"encoding",encoding);
186 gtkpspell->possible_err = new_pspell_manager( gtkpspell->config );
187 gtkpspell->checker = NULL;
189 if( pspell_error_number( gtkpspell->possible_err ) != 0){
190 debug_print(_("Pspell error : %s\n"), pspell_error_message( gtkpspell->possible_err ));
191 delete_pspell_can_have_error( gtkpspell->possible_err );
192 gtkpspell=gtkpspell_delete(gtkpspell);
195 gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
201 /* Finishes a session */
203 GtkPspell * gtkpspell_delete( GtkPspell *gtkpspell ){
205 if( gtkpspell->checker ){
206 delete_pspell_manager( gtkpspell->checker );
207 gtkpspell->checker = NULL;
208 gtkpspell->possible_err = NULL; /* checker is a cast from
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;
225 int set_path_and_dict( GtkPspell *gtkpspell,
226 PspellConfig *config,
230 guchar *module = NULL;
231 guchar *language = NULL;
232 guchar *spelling = NULL;
233 guchar *jargon = NULL;
240 /* Change nothing if any of path/dict/config is NULL */
241 if( path==NULL || dict==NULL || config==NULL)
244 /* This is done, so we can free gtkpspell->path, even if it was
245 given as an argument of the function */
246 temppath = g_strdup( path );
247 g_free( gtkpspell->path );
249 /* pspell dict name format : */
250 /* <lang>[[-<spelling>[-<jargon>]]-<module>.pwli */
253 if(!strrchr(dict,G_DIR_SEPARATOR)){ /* plain dict name */
254 strncpy(buf,dict,BUFSIZE-1);
256 else{ /* strip path */
257 strncpy(buf,strrchr(dict,G_DIR_SEPARATOR)+1,BUFSIZE-1);
259 g_free(gtkpspell->dict);
261 /* Ensure no buffers overflows if the dict is to long */
262 buf[BUFSIZE-1] = 0x00;
265 if(( module=strrchr(buf,'-'))!=NULL){
267 if(( end=strrchr(module,'.'))!=NULL)
271 /* In tempdict, we have only the dict name, without path nor
272 extension. Useful in the popup menus */
274 tempdict=g_strdup(buf);
276 /* Probably I am too paranoied... */
277 if(!( language[0]!=0x00 && language[1]!=0x00))
280 spelling = strchr(language,'-');
281 if( spelling!=NULL ){
285 if( spelling!=module ){
286 if( ( end=strchr( spelling,'-' ) )!=NULL ){
290 if( ( end=strchr(jargon,'-') )!=NULL )
304 debug_print( _("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),language,spelling,jargon,module);
307 pspell_config_replace(config,"language-tag",language);
309 pspell_config_replace(config,"spelling",spelling);
311 pspell_config_replace(config,"jargon",jargon);
313 pspell_config_replace(config,"module",module);
315 pspell_config_replace(config,"word-list-path",temppath);
317 switch(gtkpspell->mode){
318 case PSPELL_FASTMODE: pspell_config_replace(config,"sug_mode","fast");
320 case PSPELL_NORMALMODE: pspell_config_replace(config,"sug_mode","normal");
322 case PSPELL_BADSPELLERMODE: pspell_config_replace(config,"sug_mode","bad-spellers");
326 gtkpspell->path = g_strdup(temppath);
327 gtkpspell->dict = g_strdup(tempdict);
334 /* Set path and dict. The session is resetted. */
335 /* 0 on error, -1 on success */
336 int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path, guchar * dict){
338 PspellConfig * config2;
340 /* It seems changing an already running config is not the way to go
343 config2 = pspell_config_clone( gtkpspell->config );
345 if(gtkpspell->checker){
346 delete_pspell_manager(gtkpspell->checker);
348 gtkpspell->checker = NULL;
349 gtkpspell->possible_err = NULL;
351 if(set_path_and_dict(gtkpspell,config2,path,dict)==0){
352 debug_print("Pspell set_path_and_dict error.");
356 gtkpspell->possible_err = new_pspell_manager( config2 );
358 delete_pspell_config(config2);
361 if(pspell_error_number(gtkpspell->possible_err)!=0){
362 debug_print("Pspell set path&dict error %s\n",
363 pspell_error_message(gtkpspell->possible_err));
364 delete_pspell_can_have_error(gtkpspell->possible_err);
365 gtkpspell->possible_err = NULL;
369 gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
374 /* What dict are we using ? language-spelling-jargon-module format */
375 /* Actually, this function is not used and hence not tested. */
376 /* Returns an allocated string */
377 guchar * gtkpspell_get_dict( GtkPspell *gtkpspell ){
385 if(gtkpspell->config==NULL)
388 language = g_strdup(pspell_config_retrieve(gtkpspell->config,"language"));
389 spelling = g_strdup(pspell_config_retrieve(gtkpspell->config,"spelling"));
390 jargon = g_strdup(pspell_config_retrieve(gtkpspell->config,"jargon" ));
391 len = strlen(language) + strlen(spelling) + strlen(jargon);
394 dict = g_new(char,len+4);
395 strcpy(dict,language);
398 strcat(dict,spelling);
412 /* Return the dict path as an allocated string */
413 /* Not used = not tested */
415 guchar * gtkpspell_get_path(GtkPspell * gtkpspell){
419 if( gtkpspell->config=NULL )
422 path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
427 /* Menu callback : change dict */
429 static void menu_change_dict(GtkWidget *w, GtkPspell * gtkpspell){
431 guchar *thedict,*thelabel;
433 /* Dict is simply the menu label */
435 gtk_label_get( GTK_LABEL(GTK_BIN(w)->child),(gchar **) &thelabel );
436 thedict=g_strdup(thelabel);
438 /* Set path, dict, (and sug_mode ?) */
439 gtkpspell_set_path_and_dict(gtkpspell,gtkpspell->path,thedict);
444 /* Menu callback : Set the suggestion mode */
446 static void set_sug_mode(GtkWidget *w, GtkPspell * gtkpspell){
448 unsigned char * themode;
450 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child),(gchar **) &themode);
452 if( !strcmp(themode,_("Fast Mode")) ){
453 gtkpspell_set_sug_mode( gtkpspell,"fast" );
454 gtkpspell->mode = PSPELL_FASTMODE;
456 if( !strcmp(themode,_("Normal Mode")) ){
457 gtkpspell_set_sug_mode( gtkpspell,"normal" );
458 gtkpspell->mode = PSPELL_NORMALMODE;
460 if(!strcmp( themode,_("Bad Spellers Mode")) ){
461 gtkpspell_set_sug_mode( gtkpspell,"bad-spellers" );
462 gtkpspell->mode = PSPELL_BADSPELLERMODE;
466 /* Set the suggestion mode */
467 /* Actually, the session is resetted and everything is reset. */
468 /* We take the used path/dict pair and create a new config with them
471 int gtkpspell_set_sug_mode( GtkPspell * gtkpspell, gchar * themode ){
472 PspellConfig *config2;
476 delete_pspell_manager( gtkpspell->checker );
477 gtkpspell->checker = NULL;
479 config2 = pspell_config_clone(gtkpspell->config);
481 if(!set_path_and_dict( gtkpspell, config2, gtkpspell->path, gtkpspell->dict )){
482 debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
486 pspell_config_replace( config2, "sug-mode", themode );
488 gtkpspell->possible_err = new_pspell_manager( config2 );
489 delete_pspell_config( config2 );
492 if( pspell_error_number( gtkpspell->possible_err )!=0 ){
493 debug_print( _("Pspell set sug-mode error %s\n"),
494 pspell_error_message( gtkpspell->possible_err ) );
495 delete_pspell_can_have_error( gtkpspell->possible_err );
496 gtkpspell->possible_err = NULL;
499 gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
504 /* Create a suggestion list for word */
506 static GList* misspelled_suggest( GtkPspell * gtkpspell, guchar *word )
508 const guchar *newword;
511 const PspellWordList *suggestions ;
512 PspellStringEmulation *elements ;
514 if( word==NULL ) return NULL;
516 if( !pspell_manager_check( gtkpspell->checker,word,-1 ) ){
517 suggestions = pspell_manager_suggest( gtkpspell->checker,(const char *) word,-1 );
518 elements = pspell_word_list_elements( suggestions );
519 /* First one must be the misspelled (why this ?) */
520 l = g_list_append( l,g_strdup(word) );
521 while( ( newword=pspell_string_emulation_next( elements ) )!=NULL )
522 l = g_list_append( l, g_strdup( newword ) );
529 /* Just test if word is correctly spelled */
531 static int misspelled_test(GtkPspell * gtkpspell, unsigned char *word)
533 if( pspell_manager_check( gtkpspell->checker, word, -1) )
539 static gboolean iswordsep(unsigned char c)
541 return !isalpha(c) && c != '\'';
544 static guchar get_text_index_whar(GtkPspell * gtkpspell, int pos)
548 text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
549 if (text == NULL) return 0;
555 static gboolean get_word_from_pos(GtkPspell * gtkpspell, int pos,
557 int *pstart, int *pend)
561 gtktext=gtkpspell->gtktext;
562 if (iswordsep(get_text_index_whar(gtkpspell, pos))) return FALSE;
564 for (start = pos; start >= 0; --start) {
565 if (iswordsep(get_text_index_whar(gtkpspell, start))) break;
569 for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
570 if (iswordsep(get_text_index_whar(gtkpspell, end))) break;
574 for (pos = start; pos < end; pos++) buf[pos - start] = get_text_index_whar(gtkpspell, pos);
575 buf[pos - start] = 0;
578 if (pstart) *pstart = start;
579 if (pend) *pend = end;
584 static gboolean get_curword(GtkPspell * gtkpspell, unsigned char* buf,
585 int *pstart, int *pend)
587 int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
588 return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
591 static void change_color(GtkPspell * gtkpspell, int start, int end,
596 gtktext=gtkpspell->gtktext;
600 gtk_xtext_freeze(gtktext);
601 newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
603 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
604 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
605 gtk_xtext_set_point(gtktext, start);
606 gtk_xtext_forward_delete(gtktext, end - start);
608 gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
609 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
610 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
612 gtk_xtext_thaw(gtktext);
615 static gboolean check_at(GtkPspell * gtkpspell, int from_pos)
618 unsigned char buf[BUFSIZE];
620 gtktext=gtkpspell->gtktext;
621 if (from_pos < 0) return FALSE;
622 if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end)) {
625 strncpy(gtkpspell->theword,buf,BUFSIZE-1);
626 gtkpspell->theword[BUFSIZE-1]=0x00;
628 if (misspelled_test(gtkpspell, buf)) {
629 if (highlight.pixel == 0) {
630 /* add an entry for the highlight in the color map. */
631 GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
632 gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
635 change_color(gtkpspell, start, end, &highlight);
638 change_color(gtkpspell, start, end,
639 &(GTK_WIDGET(gtktext)->style->fg[0]));
645 static void check_all(GtkWidget *w, GtkPspell * gtkpspell){
646 gtkpspell_check_all(gtkpspell);
650 void gtkpspell_check_all(GtkPspell * gtkpspell)
657 gtktext=gtkpspell->gtktext;
659 if (!gtkpspell_running(gtkpspell)) return ;
661 len = gtk_xtext_get_length(gtktext);
663 adj_value = gtktext->vadj->value;
664 gtk_xtext_freeze(gtktext);
665 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
666 gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
668 while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
670 while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
673 check_at(gtkpspell, pos - 1);
675 gtk_xtext_thaw(gtktext);
676 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
677 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
681 static void entry_insert_cb(GtkXText *gtktext,
682 gchar *newtext, guint len, guint *ppos,
683 GtkPspell * gtkpspell)
685 if (!gtkpspell_running(gtkpspell)) return ;
687 if (iswordsep(newtext[0])) {
688 /* did we just end a word? */
689 if (*ppos >= 2) check_at(gtkpspell, *ppos - 2);
691 /* did we just split a word? */
692 if (*ppos < gtk_xtext_get_length(gtktext))
693 check_at(gtkpspell, *ppos + 1);
695 /* check as they type, *except* if they're typing at the end (the most
698 if (*ppos < gtk_xtext_get_length(gtktext) &&
699 !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
700 check_at(gtkpspell, *ppos - 1);
705 static void entry_delete_cb(GtkXText *gtktext,
706 gint start, gint end, GtkPspell *gtkpspell)
709 if (!gtkpspell_running(gtkpspell)) return ;
711 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
712 check_at(gtkpspell, start - 1);
713 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
714 gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
715 /* this is to *UNDO* the selection, in case they were holding shift
716 * while hitting backspace. */
719 static void replace_word(GtkWidget *w, GtkPspell * gtkpspell)
721 int start, end,oldlen, newlen;
723 unsigned char *newword;
724 unsigned char buf[BUFSIZE];
727 gtktext=gtkpspell->gtktext;
729 gtk_xtext_freeze(GTK_XTEXT(gtktext));
730 origpos = gtkpspell->orig_pos;
733 gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
734 newlen = strlen(newword);
736 get_curword(gtkpspell, buf, &start, &end);
739 gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
740 gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
741 gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
744 if(end-start>0 /*&& gtkpspell->learn*/){
745 buf[end-start]=0x00; /* Just be sure the buffer is correct... */
746 /* Learn from common misspellings */
747 pspell_manager_store_replacement(gtkpspell->checker,buf,end-start,
748 newword,strlen(newword));
751 /* Put the point and the position where we clicked with the mouse */
752 /* It seems to be a hack, as I must thaw,freeze,thaw the widget */
753 /* to let it update correctly the word insertion and then the */
754 /* point & position position. If not, SEGV after the first replacement */
755 /* If the new word ends before point, put the point at its end*/
757 if(origpos-start<=oldlen && origpos-start>=0) {
758 /* Original point was in the word. */
759 /* Put the insertion point in the same location */
760 /* with respect to the new length */
761 /* If the original position is still within the word, */
762 /* then keep the original position. If not, move to the */
763 /* end of the word */
764 if(origpos-start>newlen)
769 /* move the position according to the change of length */
770 pos=origpos+newlen-oldlen;
772 gtk_xtext_thaw(GTK_XTEXT(gtktext));
773 gtk_xtext_freeze(GTK_XTEXT(gtktext));
774 if(GTK_XTEXT(gtktext)->text_len<pos)
775 pos=gtk_xtext_get_length(GTK_XTEXT(gtktext));
776 gtkpspell->orig_pos=pos;
777 gtk_editable_set_position(GTK_EDITABLE(gtktext),gtkpspell->orig_pos);
778 gtk_xtext_set_point(GTK_XTEXT(gtktext),
779 gtk_editable_get_position(GTK_EDITABLE(gtktext)));
780 gtk_xtext_thaw(GTK_XTEXT(gtktext));
783 /* Accept this word for this session */
785 static void add_word_to_session(GtkWidget *w, GtkPspell * gtkpspell){
788 gtkxtext=gtkpspell->gtktext;
790 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
791 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
793 pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
795 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
797 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
798 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
799 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
800 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
801 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
804 /* Add word to personal dict. Save the list at every addition (may be
807 static void add_word_to_personal(GtkWidget *w, GtkPspell * gtkpspell){
810 gtkxtext=gtkpspell->gtktext;
812 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
813 pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
815 pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
816 pspell_manager_save_all_word_lists(gtkpspell->checker);
818 check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
819 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
820 gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
821 gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
822 gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
823 gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
827 static GtkMenu *make_menu_config(GtkPspell *gtkpspell){
828 GtkWidget *menu, *item, *submenu;
831 menu = gtk_menu_new();
833 item = gtk_menu_item_new_with_label(_("Spell check all"));
834 gtk_widget_show(item);
835 gtk_menu_append(GTK_MENU(menu), item);
836 gtk_signal_connect(GTK_OBJECT(item),"activate",
837 GTK_SIGNAL_FUNC(check_all),gtkpspell);
840 item = gtk_menu_item_new();
841 gtk_widget_show(item);
842 gtk_menu_append(GTK_MENU(menu),item);
844 submenu = gtk_menu_new();
845 item = gtk_menu_item_new_with_label(_("Change dictionary"));
846 gtk_widget_show(item);
847 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
848 gtk_menu_append(GTK_MENU(menu), item);
850 if(gtkpspell->dictionary_list==NULL)
851 gtkpspell->dictionary_list=gtkpspell_get_dictionary_list(gtkpspell->path);
855 GtkWidget * curmenu=submenu;
859 tmp = gtkpspell->dictionary_list;
860 for (tmp = gtkpspell->dictionary_list;tmp!=NULL;tmp=g_slist_next(tmp)){
861 dict= (Dictionary *) tmp->data;
862 item = gtk_check_menu_item_new_with_label(dict->name);
863 if(strcmp2(dict->name,gtkpspell->dict))
864 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),FALSE);
866 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
867 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
869 gtk_menu_append(GTK_MENU(curmenu),item);
870 gtk_signal_connect(GTK_OBJECT(item),"activate",
871 GTK_SIGNAL_FUNC(menu_change_dict),gtkpspell);
872 gtk_widget_show(item);
874 if(count==MENUCOUNT){
876 newmenu=gtk_menu_new();
877 item=gtk_menu_item_new_with_label(_("More..."));
878 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),newmenu);
879 gtk_menu_append(GTK_MENU(curmenu),item);
880 gtk_widget_show(item);
887 item = gtk_menu_item_new();
888 gtk_widget_show(item);
889 gtk_menu_append(GTK_MENU(menu),item);
891 item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
892 gtk_widget_show(item);
893 gtk_menu_append(GTK_MENU(menu),item);
894 if(gtkpspell->mode==PSPELL_FASTMODE){
895 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
896 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
899 gtk_signal_connect(GTK_OBJECT(item),"activate",
900 GTK_SIGNAL_FUNC(set_sug_mode),gtkpspell);
904 item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
905 gtk_widget_show(item);
906 gtk_menu_append(GTK_MENU(menu),item);
907 if(gtkpspell->mode==PSPELL_NORMALMODE){
908 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
909 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
912 gtk_signal_connect(GTK_OBJECT(item),"activate",
913 GTK_SIGNAL_FUNC(set_sug_mode),gtkpspell);
915 item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
916 gtk_widget_show(item);
917 gtk_menu_append(GTK_MENU(menu),item);
918 if(gtkpspell->mode==PSPELL_BADSPELLERMODE){
919 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
920 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
923 gtk_signal_connect(GTK_OBJECT(item),"activate",
924 GTK_SIGNAL_FUNC(set_sug_mode),gtkpspell);
928 return GTK_MENU(menu);
931 /* Add menus to accept this word for this session and to add it to personal
933 static GtkMenu *make_menu(GList *l, GtkPspell * gtkpspell)
935 GtkWidget *menu, *item;
936 unsigned char *caption;
938 gtktext=gtkpspell->gtktext;
940 menu = gtk_menu_new();
941 caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
942 item = gtk_menu_item_new_with_label(caption);
944 gtk_widget_show(item);
945 gtk_menu_append(GTK_MENU(menu), item);
947 gtk_signal_connect(GTK_OBJECT(item), "activate",
948 GTK_SIGNAL_FUNC(add_word_to_session), gtkpspell);
950 caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
951 item = gtk_menu_item_new_with_label(caption);
953 gtk_widget_show(item);
954 gtk_menu_append(GTK_MENU(menu), item);
956 gtk_signal_connect(GTK_OBJECT(item), "activate",
957 GTK_SIGNAL_FUNC(add_word_to_personal), gtkpspell);
960 item = gtk_menu_item_new();
961 gtk_widget_show(item);
962 gtk_menu_append(GTK_MENU(menu), item);
966 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
967 gtk_widget_show(item);
968 gtk_menu_append(GTK_MENU(menu), item);
970 GtkWidget *curmenu = menu;
973 if (l->data == NULL && l->next != NULL) {
975 curmenu = gtk_menu_new();
976 item = gtk_menu_item_new_with_label(_("Others..."));
977 gtk_widget_show(item);
978 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
979 gtk_menu_append(GTK_MENU(curmenu), item);
981 } else if (count > MENUCOUNT) {
983 item = gtk_menu_item_new_with_label(_("More..."));
984 gtk_widget_show(item);
985 gtk_menu_append(GTK_MENU(curmenu), item);
986 curmenu = gtk_menu_new();
987 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
989 item = gtk_menu_item_new_with_label((unsigned char*)l->data);
990 gtk_signal_connect(GTK_OBJECT(item), "activate",
991 GTK_SIGNAL_FUNC(replace_word), gtkpspell);
992 gtk_widget_show(item);
993 gtk_menu_append(GTK_MENU(curmenu), item);
995 } while ((l = l->next) != NULL);
997 return GTK_MENU(menu);
1000 static void popup_menu(GtkPspell * gtkpspell, GdkEventButton *eb)
1002 unsigned char buf[BUFSIZE];
1005 gtktext=gtkpspell->gtktext;
1006 gtkpspell->orig_pos=gtk_editable_get_position(GTK_EDITABLE(gtktext));
1008 if( !(eb->state & GDK_SHIFT_MASK) )
1009 if (get_curword(gtkpspell, buf, NULL, NULL)){
1011 strncpy(gtkpspell->theword,buf,BUFSIZE-1);
1012 gtkpspell->theword[BUFSIZE-1]=0x00;
1013 list = misspelled_suggest(gtkpspell, buf);
1015 gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
1016 eb->button, eb->time);
1017 for (l = list; l != NULL; l = l->next)
1024 gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
1025 eb->button,eb->time);
1029 /* ok, this is pretty wacky:
1030 * we need to let the right-mouse-click go through, so it moves the cursor,
1031 * but we *can't* let it go through, because GtkText interprets rightclicks as
1032 * weird selection modifiers.
1034 * so what do we do? forge rightclicks as leftclicks, then popup the menu.
1037 static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell * gtkpspell)
1043 if (!gtkpspell_running(gtkpspell)) return FALSE;
1045 if (e->type != GDK_BUTTON_PRESS) return FALSE;
1046 eb = (GdkEventButton*) e;
1048 if (eb->button != 3) return FALSE;
1050 /* forge the leftclick */
1053 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1054 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1055 gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
1057 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1058 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1059 gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
1061 /* now do the menu wackiness */
1062 popup_menu(gtkpspell, eb);
1063 gtk_grab_remove(GTK_WIDGET(gtktext));
1067 void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
1070 unsigned char *text;
1073 gtktext=gtkpspell->gtktext;
1075 adj_value = gtktext->vadj->value;
1076 gtk_xtext_freeze(gtktext);
1077 origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1078 text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1079 gtk_xtext_set_point(gtktext, 0);
1080 gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
1081 gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1082 gtk_xtext_thaw(gtktext);
1084 gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1085 gtk_adjustment_set_value(gtktext->vadj, adj_value);
1088 void gtkpspell_attach(GtkPspell * gtkpspell, GtkXText *gtktext)
1090 gtkpspell->gtktext=gtktext;
1091 // if (prefs_common.auto_makepspell) {
1092 gtk_signal_connect(GTK_OBJECT(gtktext), "insert-text",
1093 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1094 gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
1095 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1097 gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
1098 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1102 void gtkpspell_detach(GtkPspell * gtkpspell)
1105 gtktext=gtkpspell->gtktext;
1106 // if (prefs_common.auto_makepspell) {
1107 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1108 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1109 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1110 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1112 gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1113 GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1115 gtkpspell_uncheck_all(gtkpspell);
1118 /*** Sylpheed (Claws) ***/
1120 static GSList *create_empty_dictionary_list(void)
1122 GSList *list = NULL;
1125 dict = g_new0(Dictionary, 1);
1126 dict->name = g_strdup(_("None"));
1127 return g_slist_append(list, dict);
1130 /* gtkpspell_get_dictionary_list() - returns list of dictionary names */
1131 GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
1134 gchar *dict_path, *tmp, *prevdir;
1143 #warning TODO: no directory change
1145 dict_path=g_strdup(pspell_path);
1146 prevdir = g_get_current_dir();
1147 if (chdir(dict_path) <0) {
1148 FILE_OP_ERROR(dict_path, "chdir");
1151 return create_empty_dictionary_list();
1154 debug_print(_("Checking for dictionaries in %s\n"), dict_path);
1156 if (NULL != (dir = opendir("."))) {
1157 while (NULL != (ent = readdir(dir))) {
1158 /* search for pwli */
1159 if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
1160 dict = g_new0(Dictionary, 1);
1161 dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
1162 debug_print(_("Found dictionary %s\n"), dict->name);
1163 list = g_slist_append(list, dict);
1169 FILE_OP_ERROR(dict_path, "opendir");
1170 debug_print(_("No dictionary found\n"));
1171 list = create_empty_dictionary_list();
1174 debug_print(_("No dictionary found"));
1175 list = create_empty_dictionary_list();
1183 void gtkpspell_free_dictionary_list(GSList *list)
1187 for (walk = list; walk != NULL; walk = g_slist_next(walk))
1189 dict = (Dictionary *) walk->data;
1197 static void dictionary_option_menu_item_data_destroy(gpointer data)
1199 gchar *str = (gchar *) data;
1205 GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
1207 GSList *dict_list, *tmp;
1212 dict_list = gtkpspell_get_dictionary_list(pspell_path);
1213 g_return_val_if_fail(dict_list, NULL);
1215 menu = gtk_menu_new();
1217 for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1218 dict = (Dictionary *) tmp->data;
1219 item = gtk_menu_item_new_with_label(dict->name);
1221 gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
1222 g_strdup(dict->name),
1223 dictionary_option_menu_item_data_destroy);
1224 gtk_menu_append(GTK_MENU(menu), item);
1225 gtk_widget_show(item);
1228 gtk_widget_show(menu);
1230 gtkpspell_free_dictionary_list(dict_list);
1237 gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
1239 GtkWidget *menuitem;
1242 g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1243 menuitem = gtk_menu_get_active(GTK_MENU(menu));
1244 result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
1245 g_return_val_if_fail(result, NULL);
1246 return g_strdup(result);