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