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