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