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