fixed bug for US-ASCII introduced by the encoding code
[claws.git] / src / gtkspell.c
1 /* gtkpspell - a spell-checking addon for GtkText
2  * Copyright (c) 2000 Evan Martin.
3  *
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.
8  *
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.
13  *
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
17  */
18
19 /*
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
24  */
25  
26 #if defined(HAVE_CONFIG_H)
27 #include "config.h"
28 #endif
29
30 #if USE_PSPELL
31 #include "intl.h"
32
33 #include <gtk/gtk.h>
34 #include <sys/types.h>
35 #include <sys/wait.h>
36 #include <unistd.h>
37 #include <stdio.h>
38 #include <signal.h>
39 #include <ctype.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <sys/time.h>
45 #include <fcntl.h>
46 #include <time.h>
47 #include <prefs_common.h>
48 #include <utils.h>
49
50 #include <dirent.h>
51
52 #include <gtk/gtkoptionmenu.h>
53 #include <gtk/gtkmenu.h>
54 #include <gtk/gtkmenuitem.h>
55
56 #include "gtkxtext.h"
57
58 #include "gtkspell.h"
59
60 #include <pspell/pspell.h>
61
62 /* size of the text buffer used in various word-processing routines. */
63 #define BUFSIZE 1024
64
65 /* number of suggestions to display on each menu. */
66 #define MENUCOUNT 15
67
68 /******************************************************************************/
69
70 /* Function called from menus */
71
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 set_learn_mode              (GtkWidget *w, GtkPspell *gtkpspell);
76 static void check_all                   (GtkWidget *w, GtkPspell *gtkpspell);
77 static void menu_change_dict            (GtkWidget *w, GtkPspell *gtkpspell);
78 static void entry_insert_cb             (GtkXText *gtktext, gchar *newtext, 
79                                          guint len, guint *ppos, 
80                                          GtkPspell *gtkpspell);
81 static gint compare_dict                (Dictionary *a, Dictionary *b);
82 guchar *convert_to_pspell_encoding      (const guchar *encoding);
83
84
85 /* gtkspellconfig - only one config per session */
86 GtkPspellConfig * gtkpspellconfig;
87
88 /* TODO: configurable */
89 static GdkColor highlight = { 0, 255 * 256, 0, 0 };
90
91 /******************************************************************************/
92
93 /* gtkspell_init() - run the first pspell_config from which every
94  * new config is cloned 
95  */
96 GtkPspellConfig * gtkpspell_init()
97 {
98         return new_pspell_config();
99 }
100
101 /* gtkspell_finished() - Finish all. No more spelling. Called when the 
102  * program ends 
103  */
104 void gtkpspell_finished(GtkPspellConfig *gtkpspellconfig)
105 {
106         if (gtkpspellconfig) {
107                 delete_pspell_config(gtkpspellconfig);
108                 gtkpspellconfig = NULL;
109         }
110 }
111
112 /* gtkspell_running - Test if there is a manager running 
113  */
114 int gtkpspell_running(GtkPspell * gtkpspell) 
115 {
116         return (gtkpspell->config!=NULL);
117 }
118
119 /* gtkspell_new() - creates a new session if a gtkpspellconfig exists. The 
120  * settings are defaults.  If no path/dict is set afterwards, the default 
121  * one is used  
122  */
123 GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
124 {
125         GtkPspell *gtkpspell;
126
127         if (gtkpspellconfig == NULL) {
128                 gtkpspellconfig = gtkpspell_init();
129                 if (gtkpspellconfig == NULL) {
130                         debug_print(_("Pspell could not be started."));
131                         prefs_common.enable_pspell=FALSE;
132                         return NULL;
133                 }
134         }
135
136         gtkpspell               = g_new(GtkPspell ,1);
137         gtkpspell->config       = gtkpspellconfig;
138         gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
139         gtkpspell->checker      = NULL;
140
141         if (pspell_error_number(gtkpspell->possible_err) != 0) {
142                 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
143                 delete_pspell_can_have_error( gtkpspell->possible_err );
144         }
145         else {
146                 gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
147         }
148  
149         gtkpspell->dictionary_list = NULL;
150         gtkpspell->path            = NULL;
151         gtkpspell->dict            = NULL;
152         gtkpspell->mode            = PSPELL_FASTMODE;
153         gtkpspell->learn           = TRUE;
154         gtkpspell->gtktext         = NULL;
155         return gtkpspell;
156 }
157
158 /* gtkspell_new_with_config() - Creates a new session and set path/dic 
159  */
160 GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig, 
161                                      guchar *path, guchar *dict, 
162                                      guint mode, const guchar *encoding)
163 {
164         GtkPspell *gtkpspell;
165
166         if (gtkpspellconfig == NULL) {
167                 gtkpspellconfig=gtkpspell_init();
168                 if (gtkpspellconfig == NULL) {
169                         debug_print(_("Pspell could not be started."));
170                         prefs_common.enable_pspell = FALSE;
171                         return NULL;
172                 }
173         }
174
175         gtkpspell                  = g_new( GtkPspell ,1);
176         gtkpspell->path            = NULL;
177         gtkpspell->dict            = NULL;
178         gtkpspell->dictionary_list = NULL;
179         gtkpspell->gtktext         = NULL;
180   
181         gtkpspell->config          = pspell_config_clone(gtkpspellconfig);
182         gtkpspell->mode            = PSPELL_FASTMODE;
183         gtkpspell->learn           = TRUE;
184         
185         if (!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)) {
186                 debug_print(_("Pspell could not be configured."));
187                 gtkpspell = gtkpspell_delete(gtkpspell);
188                 return gtkpspell;
189         }
190         
191         if (encoding) {
192                 char *pspell_encoding;
193                 pspell_encoding = convert_to_pspell_encoding (encoding);
194                 debug_print(_("Pspell encoding: asked: %s changed to: %s\n"), encoding, pspell_encoding);
195                 pspell_config_replace(gtkpspell->config, "encoding", (const char *)pspell_encoding);
196                 if (pspell_config_error_number(gtkpspell->config) !=0 ) {
197                         debug_print(_("Pspell encoding error: %s\nSwitching to iso8859-1 (sorry)\n"), 
198                                     pspell_config_error_message(gtkpspell->config));
199                         pspell_config_replace(gtkpspell->config, "encoding", "iso8859-1");
200                 }
201                 g_free(pspell_encoding);
202         }
203
204         gtkpspell->possible_err = new_pspell_manager(gtkpspell->config);
205         gtkpspell->checker      = NULL;
206
207         if (pspell_error_number(gtkpspell->possible_err) != 0) {
208                 debug_print(_("Pspell error : %s\n"), pspell_error_message(gtkpspell->possible_err));
209                 delete_pspell_can_have_error(gtkpspell->possible_err);
210                 gtkpspell = gtkpspell_delete(gtkpspell);
211         }
212         else {
213                 gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
214         }
215         return gtkpspell;
216 }
217
218 /* gtkspell_delete() - Finishes a session 
219  */
220 GtkPspell *gtkpspell_delete( GtkPspell *gtkpspell )
221 {
222         if ( gtkpspell->checker ) {
223                 /* First save all word lists */
224                 pspell_manager_save_all_word_lists(gtkpspell->checker);
225                 delete_pspell_manager(gtkpspell->checker);
226                 gtkpspell->checker      = NULL;
227                 gtkpspell->possible_err = NULL; /* checker is a cast from possible_err */
228         }
229
230         if (gtkpspell->dictionary_list)
231                 gtkpspell_free_dictionary_list(gtkpspell->dictionary_list);
232
233         g_free(gtkpspell->path);
234         g_free(gtkpspell->dict);
235         gtkpspell->path = NULL;
236         gtkpspell->dict = NULL;
237
238         g_free(gtkpspell);
239         return NULL;
240 }
241   
242 int set_path_and_dict(GtkPspell *gtkpspell, PspellConfig *config,
243                       guchar *path, guchar * dict)
244 {
245         guchar *module   = NULL;
246         guchar *language = NULL;
247         guchar *spelling = NULL;
248         guchar *jargon   = NULL;
249         guchar  buf[BUFSIZE];
250         guchar *end;
251         guchar *temppath;
252         guchar *tempdict;
253
254         /* Change nothing if any of path/dict/config is NULL */
255         g_return_val_if_fail(path, 0);
256         g_return_val_if_fail(dict, 0);
257         g_return_val_if_fail(config, 0);
258         
259         /* This is done, so we can free gtkpspell->path, even if it was
260          * given as an argument of the function */
261         temppath = g_strdup(path);
262         g_free(gtkpspell->path);
263   
264         /* pspell dict name format :                         */
265         /*   <lang>[[-<spelling>[-<jargon>]]-<module>.pwli   */
266         /* Strip off path                                    */
267         
268         if (!strrchr(dict,G_DIR_SEPARATOR)) {
269                 /* plain dict name */
270                 strncpy(buf,dict,BUFSIZE-1);
271         }
272         else { 
273                 /* strip path */
274                 strncpy(buf, strrchr(dict, G_DIR_SEPARATOR)+1, BUFSIZE-1);
275         }
276         
277         g_free(gtkpspell->dict);
278
279         /* Ensure no buffers overflows if the dict is to long */
280         buf[BUFSIZE-1] = 0x00;
281
282         language = buf;
283         if ((module = strrchr(buf, '-')) != NULL) {
284                 module++;
285                 if ((end = strrchr(module, '.')) != NULL)
286                         end[0] = 0x00;
287         }
288
289         /* In tempdict, we have only the dict name, without path nor
290            extension. Useful in the popup menus */
291
292         tempdict = g_strdup(buf);
293
294         /* Probably I am too paranoied... */
295         if (!(language[0] != 0x00 && language[1] != 0x00))
296                 language = NULL;
297         else {
298                 spelling = strchr(language, '-');
299                 if (spelling != NULL) {
300                         spelling[0] = 0x00;
301                         spelling++;
302                 }
303                 if (spelling != module) {
304                         if ((end = strchr(spelling, '-')) != NULL) {
305                                 end[0] = 0x00;
306                                 jargon = end + 1;
307                                 if (jargon != module)
308                                         if ((end = strchr(jargon, '-')) != NULL)
309                                                 end[0] = 0x00;
310                                         else
311                                                 jargon = NULL;
312                                 else
313                                         jargon = NULL;
314                         }
315                         else
316                                 spelling = NULL;
317                 }
318                 else 
319                         spelling = NULL;
320         }
321
322         debug_print(_("Language : %s\nSpelling: %s\nJargon: %s\nModule: %s\n"),
323                     language, spelling, jargon, module);
324         
325         if (language) 
326                 pspell_config_replace(config, "language-tag", language);
327         if (spelling) 
328                 pspell_config_replace(config, "spelling", spelling);
329         if (jargon)
330                 pspell_config_replace(config, "jargon", jargon);
331         if (module)
332                 pspell_config_replace(config, "module", module);
333         if (temppath)
334                 pspell_config_replace(config, "word-list-path", temppath);
335
336         switch(gtkpspell->mode) {
337         case PSPELL_FASTMODE: 
338                 pspell_config_replace(config, "sug_mode", "fast");
339                 break;
340         case PSPELL_NORMALMODE: 
341                 pspell_config_replace(config, "sug_mode", "normal");
342                 break;
343         case PSPELL_BADSPELLERMODE: 
344                 pspell_config_replace(config, "sug_mode", "bad-spellers");
345                 break;
346         }
347   
348         gtkpspell->path = g_strdup(temppath);
349         gtkpspell->dict = g_strdup(tempdict);
350         g_free(temppath);
351         g_free(tempdict);
352
353         return TRUE;
354 }
355
356
357 /* gtkpspell_set_path_and_dict() - Set path and dict. The session is 
358  * resetted. 
359  * FALSE on error, TRUE on success */
360 int gtkpspell_set_path_and_dict(GtkPspell * gtkpspell, guchar * path, 
361                                 guchar * dict)
362 {
363         PspellConfig * config2;
364
365         /* It seems changing an already running config is not the way to go
366          */
367
368         config2 = pspell_config_clone(gtkpspell->config);
369
370         if (gtkpspell->checker) {
371                 pspell_manager_save_all_word_lists(gtkpspell->checker);
372                 delete_pspell_manager(gtkpspell->checker);
373         }
374         
375         gtkpspell->checker      = NULL;
376         gtkpspell->possible_err = NULL;
377
378         if (set_path_and_dict(gtkpspell,config2,path,dict) == 0) {
379                 debug_print(_("Pspell set_path_and_dict error."));
380                 return FALSE;
381         }
382   
383         gtkpspell->possible_err = new_pspell_manager(config2);
384
385         delete_pspell_config(config2);
386         config2 = NULL;
387
388         if (pspell_error_number(gtkpspell->possible_err) != 0) {
389                 debug_print(_("Pspell path & dict. error %s\n"),
390                             pspell_error_message(gtkpspell->possible_err));
391                 delete_pspell_can_have_error(gtkpspell->possible_err);
392                 gtkpspell->possible_err = NULL;
393                 return FALSE;
394         }
395
396         gtkpspell->checker=to_pspell_manager(gtkpspell->possible_err);
397
398         return TRUE;
399 }
400   
401 /* gtkpspell_get_dict() - What dict are we using ? language-spelling-jargon-module format */
402 /* Actually, this function is not used and hence not tested. */
403 /* Returns an allocated string */  
404 guchar *gtkpspell_get_dict(GtkPspell *gtkpspell)
405 {
406         guchar *dict;
407         guchar *language;
408         guchar *spelling;
409         guchar *jargon;
410         guint   len;
411
412         g_return_val_if_fail(gtkpspell->config, NULL);
413   
414         language = g_strdup(pspell_config_retrieve(gtkpspell->config, "language"));
415         spelling = g_strdup(pspell_config_retrieve(gtkpspell->config, "spelling"));
416         jargon   = g_strdup(pspell_config_retrieve(gtkpspell->config, "jargon"  ));
417         len      = strlen(language) + strlen(spelling) + strlen(jargon);
418
419         if (len < BUFSIZE) {
420                 dict = g_new(char,len + 4);
421                 strcpy(dict, language);
422                 if (spelling) {
423                         strcat(dict, "-");
424                         strcat(dict, spelling);
425                         if (jargon) {
426                                 strcat(dict, "-");
427                                 strcat(dict,jargon);
428                         }
429                 }
430         }
431         g_free(language);
432         g_free(spelling);
433         g_free(jargon);
434   
435         return dict;
436 }
437   
438 /* gtkpspell_get_path() - Return the dict path as an allocated string */
439 /* Not used = not tested */
440 guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
441 {
442         guchar * path;
443
444
445         g_return_val_if_fail(gtkpspell->config, NULL);
446
447         path = g_strdup(pspell_config_retrieve(gtkpspell->config,"word-list-path"));
448
449         return path;
450 }
451
452 /* menu_change_dict() - Menu callback : change dict */
453 static void menu_change_dict(GtkWidget *w, GtkPspell *gtkpspell)
454 {
455         guchar *thedict,
456                *thelabel;
457   
458         /* Dict is simply the menu label */
459
460         gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &thelabel);
461         thedict = g_strdup(thelabel);
462
463         /* Set path, dict, (and sug_mode ?) */
464         gtkpspell_set_path_and_dict(gtkpspell, gtkpspell->path, thedict);
465         g_free(thedict);
466 }
467
468 /* set_sug_mode() - Menu callback : Set the suggestion mode */
469 static void set_sug_mode(GtkWidget *w, GtkPspell *gtkpspell)
470 {
471         unsigned char *themode;
472
473         gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &themode);
474
475         if (!strcmp(themode, _("Fast Mode"))) {
476                 gtkpspell_set_sug_mode(gtkpspell, "fast");
477                 gtkpspell->mode = PSPELL_FASTMODE;
478         }
479         if (!strcmp(themode,_("Normal Mode"))) {
480                 gtkpspell_set_sug_mode(gtkpspell, "normal");
481                 gtkpspell->mode = PSPELL_NORMALMODE;
482         }
483         if (!strcmp( themode,_("Bad Spellers Mode"))) {
484                 gtkpspell_set_sug_mode(gtkpspell, "bad-spellers");
485                 gtkpspell->mode = PSPELL_BADSPELLERMODE;
486         }
487 }
488   
489 /* gtkpspell_set_sug_mode() - Set the suggestion mode */
490 /* Actually, the session is resetted and everything is reset. */
491 /* We take the used path/dict pair and create a new config with them */
492 int gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gchar *themode)
493 {
494         PspellConfig *config2;
495         guchar       *path;
496         guchar       *dict;
497
498         pspell_manager_save_all_word_lists(gtkpspell->checker);
499         delete_pspell_manager(gtkpspell->checker);
500
501         gtkpspell->checker = NULL;
502
503         config2 = pspell_config_clone(gtkpspell->config);
504
505         if (!set_path_and_dict(gtkpspell, config2, gtkpspell->path, gtkpspell->dict)) {
506                 debug_print(_("Pspell set_sug_mod could not reset path & dict\n"));
507                 return FALSE;
508         }
509
510         pspell_config_replace(config2, "sug-mode", themode);
511
512         gtkpspell->possible_err = new_pspell_manager(config2);
513         delete_pspell_config(config2);
514         config2 = NULL;
515
516         if (pspell_error_number(gtkpspell->possible_err) != 0) {
517                 debug_print(_("Pspell set sug-mode error %s\n"),
518                             pspell_error_message(gtkpspell->possible_err));
519                 delete_pspell_can_have_error(gtkpspell->possible_err);
520                 gtkpspell->possible_err = NULL;
521                 return FALSE;
522         }
523         gtkpspell->checker = to_pspell_manager(gtkpspell->possible_err);
524         return TRUE;
525 }
526
527 /* set_learn_mode() - menu callback to toggle learn mode */
528 static void set_learn_mode (GtkWidget *w, GtkPspell *gtkpspell)
529 {
530         gtkpspell->learn = gtkpspell->learn == FALSE ;
531 }
532   
533 /* misspelled_suggest() - Create a suggestion list for  word  */
534 static GList *misspelled_suggest(GtkPspell *gtkpspell, guchar *word) 
535 {
536         const guchar          *newword;
537         GList                 *list = NULL;
538         int                    count;
539         const PspellWordList  *suggestions;
540         PspellStringEmulation *elements;
541
542         g_return_val_if_fail(word, NULL);
543
544         if (!pspell_manager_check(gtkpspell->checker, word, -1)) {
545                 suggestions = pspell_manager_suggest(gtkpspell->checker, (const char *)word, -1);
546                 elements    = pspell_word_list_elements(suggestions);
547                 /* First one must be the misspelled (why this ?) */
548                 list        = g_list_append(list, g_strdup(word)); 
549                 
550                 while ((newword = pspell_string_emulation_next(elements)) != NULL)
551                         list = g_list_append(list, g_strdup(newword));
552                 return list;
553         }
554         return NULL;
555 }
556
557 /* misspelled_test() - Just test if word is correctly spelled */  
558 static int misspelled_test(GtkPspell *gtkpspell, unsigned char *word) 
559 {
560         return pspell_manager_check(gtkpspell->checker, word, -1) ? 0 : 1; 
561 }
562
563
564 static gboolean iswordsep(unsigned char c) 
565 {
566         return !isalpha(c) && c != '\'';
567 }
568
569 static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos) 
570 {
571         guchar a;
572         gchar *text;
573         
574         text = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->gtktext), pos, pos + 1);
575         if (text == NULL) 
576                 return 0;
577         a = (guchar) *text;
578         g_free(text);
579         return a;
580 }
581
582 /* get_word_from_pos () - return the word pointed to. */
583 /* Handles correctly the quotes. */
584 static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos, 
585                                   unsigned char* buf,
586                                   int *pstart, int *pend) 
587 {
588
589         /* TODO : when correcting a word into quotes, change the color of */
590         /* the quotes too, as may be they were highlighted before. To do  */
591         /* so, we can use two others pointers that points to the whole    */
592         /* word including quotes. */
593
594         gint      start, 
595                   end;
596         guchar    c;
597         GtkXText *gtktext;
598         
599         gtktext = gtkpspell->gtktext;
600         if (iswordsep(get_text_index_whar(gtkpspell, pos))) 
601                 return FALSE;
602         
603         /* The apostrophe character is somtimes used for quotes 
604          * So include it in the word only if it is not surrounded 
605          * by other characters. 
606          */
607          
608         for (start = pos; start >= 0; --start) {
609                 c = get_text_index_whar(gtkpspell, start);
610                 if (c == '\'') {
611                         if (start > 0) {
612                                 if (!isalpha(get_text_index_whar(gtkpspell, start - 1))) {
613                                         /* start_quote = TRUE; */
614                                         break;
615                                 }
616                         }
617                         else {
618                                 /* start_quote = TRUE; */
619                                 break;
620                         }
621                 }
622                 else
623                         if (!isalpha(c))
624                                 break;
625         }
626         start++;
627
628         for (end = pos; end < gtk_xtext_get_length(gtktext); end++) {
629                 c = get_text_index_whar(gtkpspell, end); 
630                 if (c == '\'') {
631                         if (end < gtk_xtext_get_length(gtktext)) {
632                                 if (!isalpha(get_text_index_whar(gtkpspell, end + 1))) {
633                                         /* end_quote = TRUE; */
634                                         break;
635                                 }
636                         }
637                         else {
638                                 /* end_quote = TRUE; */
639                                 break;
640                         }
641                 }
642                 else
643                         if(!isalpha(c))
644                                 break;
645         }
646                                                 
647         if (buf) {
648                 for (pos = start; pos < end; pos++) 
649                         buf[pos - start] = get_text_index_whar(gtkpspell, pos);
650                 buf[pos - start] = 0;
651         }
652
653         if (pstart) 
654                 *pstart = start;
655         if (pend) 
656                 *pend = end;
657
658         return TRUE;
659 }
660
661 static gboolean get_curword(GtkPspell *gtkpspell, unsigned char* buf,
662                             int *pstart, int *pend) 
663 {
664         int pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
665         return get_word_from_pos(gtkpspell, pos, buf, pstart, pend);
666 }
667
668 static void change_color(GtkPspell * gtkpspell, 
669                          int start, int end, 
670                          GdkColor *color) 
671 {
672         char     *newtext;
673         GtkXText *gtktext;
674
675         g_return_if_fail(start < end);
676     
677         gtktext = gtkpspell->gtktext;
678     
679         gtk_xtext_freeze(gtktext);
680         newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
681         if (newtext) {
682                 gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
683                                                  GTK_SIGNAL_FUNC(entry_insert_cb), 
684                                                  gtkpspell);
685                 gtk_xtext_set_point(gtktext, start);
686                 gtk_xtext_forward_delete(gtktext, end - start);
687
688                 gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
689                 gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
690                                                    GTK_SIGNAL_FUNC(entry_insert_cb), 
691                                                    gtkpspell);
692         }
693         gtk_xtext_thaw(gtktext);
694 }
695
696 static gboolean check_at(GtkPspell *gtkpspell, int from_pos) 
697 {
698         int           start, end;
699         unsigned char buf[BUFSIZE];
700         GtkXText     *gtktext;
701
702         g_return_val_if_fail(from_pos >= 0, FALSE);
703     
704         gtktext = gtkpspell->gtktext;
705
706         if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
707                 return FALSE;
708
709         strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
710         gtkpspell->theword[BUFSIZE - 1] = 0;
711
712         if (misspelled_test(gtkpspell, buf)) {
713                 if (highlight.pixel == 0) {
714                         /* add an entry for the highlight in the color map. */
715                         GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
716                         gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
717                 }
718                 change_color(gtkpspell, start, end, &highlight);
719                 return TRUE;
720         } else {
721                 change_color(gtkpspell, start, end,
722                              &(GTK_WIDGET(gtktext)->style->fg[0]));
723                 return FALSE;
724         }
725 }
726
727
728 static void check_all(GtkWidget *w, GtkPspell *gtkpspell)
729 {
730         gtkpspell_check_all(gtkpspell);
731 }
732
733
734 void gtkpspell_check_all(GtkPspell *gtkpspell) 
735 {
736         guint     origpos;
737         guint     pos = 0;
738         guint     len;
739         float     adj_value;
740         GtkXText *gtktext;
741
742         
743         if (!gtkpspell_running(gtkpspell)) return ;
744         gtktext = gtkpspell->gtktext;
745
746         len = gtk_xtext_get_length(gtktext);
747
748         adj_value = gtktext->vadj->value;
749         gtk_xtext_freeze(gtktext);
750         origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
751         gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
752         while (pos < len) {
753                 while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
754                         pos++;
755                 while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
756                         pos++;
757                 if (pos > 0)
758                         check_at(gtkpspell, pos - 1);
759         }
760         gtk_xtext_thaw(gtktext);
761         gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
762         gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
763 }
764
765 static void entry_insert_cb(GtkXText *gtktext, gchar *newtext, 
766                             guint len, guint *ppos, 
767                             GtkPspell * gtkpspell) 
768 {
769         guint origpos;
770         if (!gtkpspell_running(gtkpspell)) 
771                 return;
772
773         /* We must insert ourself the character to impose the */
774         /* color of the inserted character to be default */
775         /* Never mess with set_insertion when frozen */
776         gtk_xtext_freeze(gtktext);
777         gtk_xtext_backward_delete(GTK_XTEXT(gtktext), len);
778         gtk_xtext_insert(GTK_XTEXT(gtktext), NULL,
779                         &(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len);
780         *ppos = gtk_xtext_get_point(GTK_XTEXT(gtktext));
781                
782         if (iswordsep(newtext[0])) {
783                 /* did we just end a word? */
784                 if (*ppos >= 2) 
785                         check_at(gtkpspell, *ppos - 2);
786
787                 /* did we just split a word? */
788                 if (*ppos < gtk_xtext_get_length(gtktext))
789                         check_at(gtkpspell, *ppos + 1);
790         } else {
791                 /* check as they type, *except* if they're typing at the end (the most
792                  * common case.
793                  */
794                 if (*ppos < gtk_xtext_get_length(gtktext) 
795                 &&  !iswordsep(get_text_index_whar(gtkpspell, *ppos)))
796                         check_at(gtkpspell, *ppos - 1);
797                 }
798         gtk_xtext_thaw(gtktext);
799         gtk_editable_set_position(GTK_EDITABLE(gtktext), *ppos);
800 }
801
802 static void entry_delete_cb(GtkXText *gtktext, gint start, gint end, 
803                             GtkPspell *gtkpspell) 
804 {
805         int origpos;
806     
807         if (!gtkpspell_running(gtkpspell)) 
808                 return;
809
810         origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
811         if (start) {
812                 check_at(gtkpspell, start - 1);
813                 check_at(gtkpspell, start);
814         }
815
816         gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
817         gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
818         /* this is to *UNDO* the selection, in case they were holding shift
819          * while hitting backspace. */
820 }
821
822 static void replace_word(GtkWidget *w, GtkPspell *gtkpspell) 
823 {
824         int             start, end,oldlen, newlen;
825         guint           origpos;
826         unsigned char  *newword;
827         unsigned char   buf[BUFSIZE];
828         guint           pos;
829         GtkXText       *gtktext;
830     
831         gtktext = gtkpspell->gtktext;
832
833         gtk_xtext_freeze(GTK_XTEXT(gtktext));
834         origpos = gtkpspell->orig_pos;
835         pos     = origpos;
836
837         gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
838         newlen = strlen(newword);
839
840         get_curword(gtkpspell, buf, &start, &end);
841         oldlen = end - start;
842
843         gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
844         gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
845         gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
846     
847         if (end-start > 0 && gtkpspell->learn) { 
848                 /* Just be sure the buffer ends somewhere... */
849                 buf[end-start] = 0; 
850                 /* Learn from common misspellings */
851                 pspell_manager_store_replacement(gtkpspell->checker, buf, 
852                                                  end-start, newword, 
853                                                  strlen(newword));
854         }
855
856         /* Put the point and the position where we clicked with the mouse */
857         /* It seems to be a hack, as I must thaw,freeze,thaw the widget   */
858         /* to let it update correctly the word insertion and then the     */
859         /* point & position position. If not, SEGV after the first replacement */
860         /* If the new word ends before point, put the point at its end*/
861     
862         if (origpos-start <= oldlen && origpos-start >= 0) {
863                 /* Original point was in the word. */
864                 /* Put the insertion point in the same location */
865                 /* with respect to the new length */
866                 /* If the original position is still within the word, */
867                 /* then keep the original position. If not, move to the */
868                 /* end of the word */
869                 if (origpos-start > newlen)
870                         pos = start + newlen;
871         }
872         else if (origpos > end) {
873                 /* move the position according to the change of length */
874                 pos = origpos + newlen - oldlen;
875         }
876         gtk_xtext_thaw(GTK_XTEXT(gtktext));
877         gtk_xtext_freeze(GTK_XTEXT(gtktext));
878         if (GTK_XTEXT(gtktext)->text_len < pos)
879                 pos = gtk_xtext_get_length(GTK_XTEXT(gtktext));
880         gtkpspell->orig_pos = pos;
881         gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
882         gtk_xtext_set_point(GTK_XTEXT(gtktext), 
883                             gtk_editable_get_position(GTK_EDITABLE(gtktext)));
884         gtk_xtext_thaw(GTK_XTEXT(gtktext));
885 }
886
887 /* Accept this word for this session */
888
889 static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
890 {
891         guint     pos;
892         GtkXText *gtkxtext;
893     
894         gtkxtext = gtkpspell->gtktext;
895         gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
896         pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
897     
898         pspell_manager_add_to_session(gtkpspell->checker,gtkpspell->theword, strlen(gtkpspell->theword));
899
900         check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
901
902         gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
903         gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
904         gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
905         gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
906         gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
907 }
908
909 /* add_word_to_personal() - add word to personal dict. */
910
911 static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
912 {
913         guint     pos;
914         GtkXText *gtkxtext;
915     
916         gtkxtext = gtkpspell->gtktext;
917
918         gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
919         pos = gtk_editable_get_position(GTK_EDITABLE(gtkxtext));
920     
921         pspell_manager_add_to_personal(gtkpspell->checker,gtkpspell->theword,strlen(gtkpspell->theword));
922     
923         check_at(gtkpspell,gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
924         gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
925         gtk_xtext_freeze(GTK_XTEXT(gtkxtext));
926         gtk_editable_set_position(GTK_EDITABLE(gtkxtext),pos);
927         gtk_xtext_set_point(GTK_XTEXT(gtkxtext), gtk_editable_get_position(GTK_EDITABLE(gtkxtext)));
928         gtk_xtext_thaw(GTK_XTEXT(gtkxtext));
929 }
930
931        
932 static GtkMenu *make_menu_config(GtkPspell *gtkpspell)
933 {
934         GtkWidget *menu, *item, *submenu;
935
936   
937         menu = gtk_menu_new();
938
939         item = gtk_menu_item_new_with_label(_("Spell check all"));
940         gtk_widget_show(item);
941         gtk_menu_append(GTK_MENU(menu), item);
942         gtk_signal_connect(GTK_OBJECT(item),"activate",
943                            GTK_SIGNAL_FUNC(check_all), 
944                            gtkpspell);
945
946
947         item = gtk_menu_item_new();
948         gtk_widget_show(item);
949         gtk_menu_append(GTK_MENU(menu),item);
950
951         submenu = gtk_menu_new();
952         item = gtk_menu_item_new_with_label(_("Change dictionary"));
953         gtk_widget_show(item);
954         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
955         gtk_menu_append(GTK_MENU(menu), item);
956
957         /* Dict list */
958         if (gtkpspell->dictionary_list==NULL)
959                 gtkpspell->dictionary_list = gtkpspell_get_dictionary_list(gtkpspell->path);
960        
961         {
962                 GtkWidget * curmenu = submenu;
963                 int count = 0;
964                 Dictionary *dict;
965                 GSList *tmp;
966                 
967                 tmp = gtkpspell->dictionary_list;
968                 for (tmp = gtkpspell->dictionary_list; tmp != NULL; tmp =g_slist_next(tmp)) {
969                         dict = (Dictionary *) tmp->data;
970                         item = gtk_check_menu_item_new_with_label(dict->name);
971                         if (strcmp2(dict->name, gtkpspell->dict))
972                                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
973                         else {
974                                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
975                                 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
976                         }
977                         gtk_menu_append(GTK_MENU(curmenu), item);
978                         gtk_signal_connect(GTK_OBJECT(item), "activate",
979                                            GTK_SIGNAL_FUNC(menu_change_dict),
980                                            gtkpspell);
981                         gtk_widget_show(item);
982                         count++;
983                         if (count == MENUCOUNT) {
984                                 GtkWidget *newmenu;
985                                 newmenu = gtk_menu_new();
986                                 item = gtk_menu_item_new_with_label(_("More..."));
987                                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), newmenu);
988                                 gtk_menu_append(GTK_MENU(curmenu), item);
989                                 gtk_widget_show(item);
990                                 curmenu = newmenu;
991                                 count = 0;
992                         }
993                 }
994         }  
995
996         item = gtk_menu_item_new();
997         gtk_widget_show(item);
998         gtk_menu_append(GTK_MENU(menu), item);
999
1000         item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
1001         gtk_widget_show(item);
1002         gtk_menu_append(GTK_MENU(menu), item);
1003         if (gtkpspell->mode == PSPELL_FASTMODE) {
1004                 gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
1005                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
1006         }
1007         else
1008                 gtk_signal_connect(GTK_OBJECT(item), "activate",
1009                                    GTK_SIGNAL_FUNC(set_sug_mode),
1010                                    gtkpspell);
1011
1012         item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
1013         gtk_widget_show(item);
1014         gtk_menu_append(GTK_MENU(menu),item);
1015         if (gtkpspell->mode == PSPELL_NORMALMODE) {
1016                 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1017                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1018         }
1019         else
1020                 gtk_signal_connect(GTK_OBJECT(item), "activate",
1021                                    GTK_SIGNAL_FUNC(set_sug_mode),
1022                                    gtkpspell);
1023         
1024         item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
1025         gtk_widget_show(item);
1026         gtk_menu_append(GTK_MENU(menu), item);
1027         if (gtkpspell->mode==PSPELL_BADSPELLERMODE) {
1028                 gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
1029                 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
1030         }
1031         else
1032                 gtk_signal_connect(GTK_OBJECT(item), "activate",
1033                                    GTK_SIGNAL_FUNC(set_sug_mode),
1034                                    gtkpspell);
1035         item = gtk_menu_item_new();
1036         gtk_widget_show(item);
1037         gtk_menu_append(GTK_MENU(menu), item);
1038
1039         item = gtk_check_menu_item_new_with_label(_("Learn from mistakes"));
1040         gtk_widget_show(item);
1041         gtk_menu_append(GTK_MENU(menu), item);
1042         gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), 
1043                                        gtkpspell->learn);
1044         gtk_signal_connect(GTK_OBJECT(item), "activate", 
1045                            GTK_SIGNAL_FUNC(set_learn_mode), gtkpspell);
1046                         
1047         
1048         return GTK_MENU(menu);
1049 }
1050   
1051 /* make_menu() - Add menus to accept this word for this session and to add it to 
1052  * personal dictionary */
1053 static GtkMenu *make_menu(GList *l, GtkPspell *gtkpspell) 
1054 {
1055         GtkWidget *menu, *item;
1056         unsigned char *caption;
1057         GtkXText * gtktext;
1058         
1059         gtktext = gtkpspell->gtktext;
1060
1061         menu = gtk_menu_new(); 
1062         caption = g_strdup_printf(_("Accept `%s' for this session"), (unsigned char*)l->data);
1063         item = gtk_menu_item_new_with_label(caption);
1064         g_free(caption);
1065         gtk_widget_show(item);
1066         gtk_menu_append(GTK_MENU(menu), item);
1067
1068         gtk_signal_connect(GTK_OBJECT(item), "activate",
1069                            GTK_SIGNAL_FUNC(add_word_to_session), 
1070                            gtkpspell);
1071
1072         caption = g_strdup_printf(_("Add `%s' to personal dictionary"), (char*)l->data);
1073         item = gtk_menu_item_new_with_label(caption);
1074         g_free(caption);
1075         gtk_widget_show(item);
1076         gtk_menu_append(GTK_MENU(menu), item);
1077
1078         gtk_signal_connect(GTK_OBJECT(item), "activate",
1079                            GTK_SIGNAL_FUNC(add_word_to_personal), 
1080                            gtkpspell);
1081          
1082         item = gtk_menu_item_new();
1083         gtk_widget_show(item);
1084         gtk_menu_append(GTK_MENU(menu), item);
1085
1086         l = l->next;
1087         if (l == NULL) {
1088                 item = gtk_menu_item_new_with_label(_("(no suggestions)"));
1089                 gtk_widget_show(item);
1090                 gtk_menu_append(GTK_MENU(menu), item);
1091         } else {
1092                 GtkWidget *curmenu = menu;
1093                 int count = 0;
1094                 
1095                 do {
1096                         if (l->data == NULL && l->next != NULL) {
1097                                 count = 0;
1098                                 curmenu = gtk_menu_new();
1099                                 item = gtk_menu_item_new_with_label(_("Others..."));
1100                                 gtk_widget_show(item);
1101                                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1102                                 gtk_menu_append(GTK_MENU(curmenu), item);
1103                                 l = l->next;
1104                         } else if (count > MENUCOUNT) {
1105                                 count -= MENUCOUNT;
1106                                 item = gtk_menu_item_new_with_label(_("More..."));
1107                                 gtk_widget_show(item);
1108                                 gtk_menu_append(GTK_MENU(curmenu), item);
1109                                 curmenu = gtk_menu_new();
1110                                 gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
1111                         }
1112                         item = gtk_menu_item_new_with_label((unsigned char*)l->data);
1113                         gtk_signal_connect(GTK_OBJECT(item), "activate",
1114                                            GTK_SIGNAL_FUNC(replace_word), gtkpspell);
1115                         gtk_widget_show(item);
1116                         gtk_menu_append(GTK_MENU(curmenu), item);
1117                         count++;
1118                 } while ((l = l->next) != NULL);
1119         }
1120         return GTK_MENU(menu);
1121 }
1122
1123 static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb) 
1124 {
1125         unsigned char buf[BUFSIZE];
1126         GList *list, *l;
1127         GtkXText * gtktext;
1128         
1129         gtktext = gtkpspell->gtktext;
1130         gtkpspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1131
1132         if (!(eb->state & GDK_SHIFT_MASK))
1133                 if (get_curword(gtkpspell, buf, NULL, NULL)) {
1134                         if (buf != NULL) {
1135                                 strncpy(gtkpspell->theword, buf, BUFSIZE - 1);
1136                                 gtkpspell->theword[BUFSIZE - 1] = 0;
1137                                 list = misspelled_suggest(gtkpspell, buf);
1138                                 if (list != NULL) {
1139                                         gtk_menu_popup(make_menu(list, gtkpspell), NULL, NULL, NULL, NULL,
1140                                                        eb->button, eb->time);
1141                                         for (l = list; l != NULL; l = l->next)
1142                                                 g_free(l->data);
1143                                         g_list_free(list);
1144                                         return;
1145                                 }
1146                         }
1147                 }
1148         gtk_menu_popup(make_menu_config(gtkpspell),NULL,NULL,NULL,NULL,
1149         eb->button,eb->time);
1150 }
1151
1152 /* ok, this is pretty wacky:
1153  * we need to let the right-mouse-click go through, so it moves the cursor,
1154  * but we *can't* let it go through, because GtkText interprets rightclicks as
1155  * weird selection modifiers.
1156  *
1157  * so what do we do?  forge rightclicks as leftclicks, then popup the menu.
1158  * HACK HACK HACK.
1159  */
1160 static gint button_press_intercept_cb(GtkXText *gtktext, GdkEvent *e, GtkPspell *gtkpspell) 
1161 {
1162         GdkEventButton *eb;
1163         gboolean retval;
1164
1165         if (!gtkpspell_running(gtkpspell)) 
1166                 return FALSE;
1167
1168         if (e->type != GDK_BUTTON_PRESS) 
1169                 return FALSE;
1170         eb = (GdkEventButton*) e;
1171
1172         if (eb->button != 3) 
1173                 return FALSE;
1174
1175         /* forge the leftclick */
1176         eb->button = 1;
1177
1178         gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
1179                                          GTK_SIGNAL_FUNC(button_press_intercept_cb), 
1180                                          gtkpspell);
1181         gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "button-press-event",
1182                                 e, &retval);
1183         gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
1184                                            GTK_SIGNAL_FUNC(button_press_intercept_cb), 
1185                                            gtkpspell);
1186         gtk_signal_emit_stop_by_name(GTK_OBJECT(gtktext), "button-press-event");
1187     
1188         /* now do the menu wackiness */
1189         popup_menu(gtkpspell, eb);
1190         gtk_grab_remove(GTK_WIDGET(gtktext));
1191         return TRUE;
1192 }
1193
1194 void gtkpspell_uncheck_all(GtkPspell * gtkpspell) 
1195 {
1196         int origpos;
1197         unsigned char *text;
1198         float adj_value;
1199         GtkXText *gtktext;
1200         
1201         gtktext=gtkpspell->gtktext;
1202
1203         adj_value = gtktext->vadj->value;
1204         gtk_xtext_freeze(gtktext);
1205         origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
1206         text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
1207         gtk_xtext_set_point(gtktext, 0);
1208         gtk_xtext_forward_delete(gtktext, gtk_xtext_get_length(gtktext));
1209         gtk_xtext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
1210         gtk_xtext_thaw(gtktext);
1211
1212         gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
1213         gtk_adjustment_set_value(gtktext->vadj, adj_value);
1214 }
1215
1216 void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext) 
1217 {
1218         gtkpspell->gtktext=gtktext;
1219         gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
1220                            GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1221         gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
1222                                  GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1223         gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
1224                            GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1225
1226 }
1227
1228 void gtkpspell_detach(GtkPspell * gtkpspell) 
1229 {
1230         GtkXText * gtktext;
1231         
1232         gtktext =gtkpspell->gtktext;
1233 /*    if (prefs_common.auto_makepspell) { */
1234         gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1235                                       GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
1236         gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1237                                       GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
1238 /*    }; */
1239         gtk_signal_disconnect_by_func(GTK_OBJECT(gtktext),
1240                                       GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
1241
1242         gtkpspell_uncheck_all(gtkpspell);
1243 }
1244
1245 /*** Sylpheed (Claws) ***/
1246
1247 static GSList *create_empty_dictionary_list(void)
1248 {
1249         GSList *list = NULL;
1250         Dictionary *dict;
1251         
1252         dict = g_new0(Dictionary, 1);
1253         dict->name = g_strdup(_("None"));
1254         return g_slist_append(list, dict);
1255 }
1256
1257 /* gtkpspell_get_dictionary_list() - returns list of dictionary names */
1258 GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path)
1259 {
1260         GSList *list;
1261         gchar *dict_path, *tmp, *prevdir;
1262         GSList *walk;
1263         Dictionary *dict;
1264         DIR *dir;
1265         struct dirent *ent;
1266
1267         list = NULL;
1268
1269 #ifdef USE_THREADS
1270 #warning TODO: no directory change
1271 #endif
1272         dict_path=g_strdup(pspell_path);
1273         prevdir = g_get_current_dir();
1274         if (chdir(dict_path) <0) {
1275                 FILE_OP_ERROR(dict_path, "chdir");
1276                 g_free(prevdir);
1277                 g_free(dict_path);
1278                 return create_empty_dictionary_list();
1279         }
1280
1281         debug_print(_("Checking for dictionaries in %s\n"), dict_path);
1282
1283         if (NULL != (dir = opendir("."))) {
1284                 while (NULL != (ent = readdir(dir))) {
1285                         /* search for pwli */
1286                         if (NULL != (tmp = strstr(ent->d_name, ".pwli"))) {
1287                                 dict = g_new0(Dictionary, 1);
1288                                 dict->name = g_strndup(ent->d_name, tmp - ent->d_name);
1289                                 debug_print(_("Found dictionary %s\n"), dict->name);
1290                                 list = g_slist_insert_sorted(list, dict, (GCompareFunc) compare_dict);
1291                         }
1292                 }                       
1293                 closedir(dir);
1294         }
1295         else {
1296                 FILE_OP_ERROR(dict_path, "opendir");
1297                 debug_print(_("No dictionary found\n"));
1298                 list = create_empty_dictionary_list();
1299         }
1300         if(list==NULL){
1301           debug_print(_("No dictionary found"));
1302           list = create_empty_dictionary_list();
1303         }
1304         chdir(prevdir);
1305         g_free(dict_path);
1306         g_free(prevdir);
1307         return list;
1308 }
1309
1310 /* compare_dict () - compare 2 dict names */
1311
1312 static gint compare_dict(Dictionary *a, Dictionary *b)
1313 {
1314         guchar *alanguage, *blanguage,
1315                *aspelling, *bspelling,
1316                *ajargon  , *bjargon  ,
1317                *amodule  , *bmodule  ;
1318         guint aparts = 0, bparts = 0, i;
1319
1320         for (i=0; i < strlen(a->name) ; i++)
1321                 if (a->name[i]=='-')
1322                         aparts++;
1323         for (i=0; i < strlen(b->name) ; i++)
1324                 if (b->name[i]=='-')
1325                         bparts++;
1326
1327         if (aparts != bparts) 
1328                 return (aparts < bparts) ? -1 : +1 ;
1329         else 
1330                 return strcmp2(a->name, b->name);
1331 }
1332 void gtkpspell_free_dictionary_list(GSList *list)
1333 {
1334         Dictionary *dict;
1335         GSList *walk;
1336         for (walk = list; walk != NULL; walk = g_slist_next(walk))
1337                 if (walk->data) {
1338                         dict = (Dictionary *) walk->data;
1339                         if (dict->name)
1340                                 g_free(dict->name);
1341                         g_free(dict);
1342                 }                               
1343         g_slist_free(list);
1344 }
1345
1346 static void dictionary_option_menu_item_data_destroy(gpointer data)
1347 {
1348         gchar *str = (gchar *) data;
1349
1350         if (str)
1351                 g_free(str);
1352 }
1353
1354 GtkWidget *gtkpspell_dictionary_option_menu_new(const gchar *pspell_path)
1355 {
1356         GSList *dict_list, *tmp;
1357         GtkWidget *item;
1358         GtkWidget *menu;
1359         Dictionary *dict;
1360
1361         dict_list = gtkpspell_get_dictionary_list(pspell_path);
1362         g_return_val_if_fail(dict_list, NULL);
1363
1364         menu = gtk_menu_new();
1365         
1366         for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
1367                 dict = (Dictionary *) tmp->data;
1368                 item = gtk_menu_item_new_with_label(dict->name);
1369                 if (dict->name)
1370                         gtk_object_set_data_full(GTK_OBJECT(item), "dict_name",
1371                                          g_strdup(dict->name), 
1372                                          dictionary_option_menu_item_data_destroy);
1373                 gtk_menu_append(GTK_MENU(menu), item);                                   
1374                 gtk_widget_show(item);
1375         }
1376
1377         gtk_widget_show(menu);
1378
1379         gtkpspell_free_dictionary_list(dict_list);
1380
1381         return menu;
1382 }
1383
1384
1385
1386 gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
1387 {
1388         GtkWidget *menuitem;
1389         gchar *result;
1390
1391         g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
1392         menuitem = gtk_menu_get_active(GTK_MENU(menu));
1393         result = gtk_object_get_data(GTK_OBJECT(menuitem), "dict_name");
1394         g_return_val_if_fail(result, NULL);
1395         return g_strdup(result);
1396   
1397 }
1398
1399 /* convert_to_pspell_encoding () - converts ISO-8859-* strings to iso8859-* 
1400  * as needed by pspell. Returns an allocated string.
1401  */
1402
1403 guchar *convert_to_pspell_encoding (const guchar *encoding)
1404 {
1405         guchar * pspell_encoding;
1406         /* Beware, strstr2 returns 0 if string is found -1 if not */
1407         if (!strstr2(encoding, "ISO-8859-")) {
1408                 pspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
1409         }
1410         else
1411                 if (!strcmp2(encoding, "US-ASCII"))
1412                         pspell_encoding = g_strdup("iso8859-1");
1413                 else
1414                         pspell_encoding = g_strdup(encoding);
1415         return pspell_encoding;
1416         
1417 }
1418
1419                 
1420                 
1421 #endif