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