2010-10-17 [colin] 3.7.6cvs56
[claws.git] / src / gtk / gtkaspell.c
index 7144718f845d1ffa4bd88093d00d0b202d0739a3..91195cd8f8eda83850edf86701cdff851c1c098c 100644 (file)
@@ -5,7 +5,7 @@
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
- * version 2 of the License, or (at your option) any later version.
+ * version 3 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; If not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 /*
  * Stuphead: (C) 2000,2001 Grigroy Bakunov, Sergey Pinaev
  * Adapted for Sylpheed (Claws) (c) 2001-2002 by Hiroyuki Yamamoto & 
- * The Sylpheed Claws Team.
+ * The Claws Mail Team.
  * Adapted for pspell (c) 2001-2002 Melvin Hadasht
  * Adapted for GNU/aspell (c) 2002 Melvin Hadasht
  */
@@ -28,7 +27,7 @@
 #  include "config.h"
 #endif
 
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
 
 #include <stdio.h>
 #include <stdlib.h>
 
 #include <gtk/gtk.h>
 #include <gdk/gdk.h>
-#include <gtk/gtkoptionmenu.h>
-#include <gtk/gtkmenu.h>
-#include <gtk/gtkmenuitem.h>
 #include <gdk/gdkkeysyms.h>
 
-#include <aspell.h>
-
 #include "utils.h"
-#include "codeconv.h"
 #include "alertpanel.h"
 #include "gtkaspell.h"
 #include "gtk/gtkutils.h"
+#include "gtk/combobox.h"
 
 #define ASPELL_FASTMODE       1
 #define ASPELL_NORMALMODE     2
 #define ASPELL_BADSPELLERMODE 3
 
-#define GTKASPELLWORDSIZE 1024
-
 /* size of the text buffer used in various word-processing routines. */
 #define BUFSIZE 1024
 
 /* number of suggestions to display on each menu. */
 #define MENUCOUNT 15
 
-/* 'config' must be defined as a 'AspellConfig *' */
-#define RETURN_FALSE_IF_CONFIG_ERROR() \
-{ \
-       if (aspell_config_error_number(config) != 0) { \
-               gtkaspellcheckers->error_message = g_strdup(aspell_config_error_message(config)); \
-               return FALSE; \
-       } \
-}
-
-#define CONFIG_REPLACE_RETURN_FALSE_IF_FAIL(option, value) { \
-       aspell_config_replace(config, option, value);        \
-       RETURN_FALSE_IF_CONFIG_ERROR();                      \
-       }
+enum {
+       SET_GTKASPELL_NAME      = 0,
+       SET_GTKASPELL_FULLNAME  = 1,
+       SET_GTKASPELL_SIZE
+};
 
 typedef struct _GtkAspellCheckers {
        GSList          *checkers;
@@ -94,60 +78,12 @@ typedef struct _GtkAspellCheckers {
        gchar           *error_message;
 } GtkAspellCheckers;
 
-typedef struct _Dictionary {
-       gchar *fullname;
-       gchar *dictname;
-       gchar *encoding;
-} Dictionary;
-
-typedef struct _GtkAspeller {
-       Dictionary      *dictionary;
-       gint             sug_mode;
-       AspellConfig    *config;
-       AspellSpeller   *checker;
-} GtkAspeller;
-
-typedef void (*ContCheckFunc) (gpointer *gtkaspell);
-
-struct _GtkAspell
-{
-       GtkAspeller     *gtkaspeller;
-       GtkAspeller     *alternate_speller;
-       gchar           *dictionary_path;
-       gchar            theword[GTKASPELLWORDSIZE];
-       gint             start_pos;
-       gint             end_pos;
-        gint            orig_pos;
-       gint             end_check_pos;
-       gboolean         misspelled;
-       gboolean         check_while_typing;
-       gboolean         use_alternate;
-
-       ContCheckFunc    continue_check; 
-
-       GtkWidget       *config_menu;
-       GtkWidget       *popup_config_menu;
-       GtkWidget       *sug_menu;
-       GtkWidget       *replace_entry;
-       GtkWidget       *parent_window;
-
-       gint             default_sug_mode;
-       gint             max_sug;
-       GList           *suggestions_list;
-
-       GtkTextView     *gtktext;
-       GdkColor         highlight;
-       GtkAccelGroup   *accel_group;
-};
-
-typedef AspellConfig GtkAspellConfig;
-
 /******************************************************************************/
 
 static GtkAspellCheckers *gtkaspellcheckers;
 
 /* Error message storage */
-static void gtkaspell_checkers_error_message   (gchar          *message);
+static void gtkaspell_checkers_error_message   (gchar  *message);
 
 /* Callbacks */
 static void entry_insert_cb                    (GtkTextBuffer  *textbuf,
@@ -159,10 +95,13 @@ static void entry_delete_cb                        (GtkTextBuffer  *textbuf,
                                                 GtkTextIter    *startiter,
                                                 GtkTextIter    *enditer,
                                                 GtkAspell      *gtkaspell);
-static gint button_press_intercept_cb          (GtkTextView    *gtktext,
+/*static gint button_press_intercept_cb                (GtkTextView    *gtktext,
                                                 GdkEvent       *e, 
                                                 GtkAspell      *gtkaspell);
-
+*/
+static void button_press_intercept_cb(GtkTextView *gtktext,
+                       GtkMenu *menu, GtkAspell *gtkaspell);
+                       
 /* Checker creation */
 static GtkAspeller* gtkaspeller_new            (Dictionary     *dict);
 static GtkAspeller* gtkaspeller_real_new       (Dictionary     *dict);
@@ -170,20 +109,19 @@ static GtkAspeller* gtkaspeller_delete            (GtkAspeller    *gtkaspeller);
 static GtkAspeller* gtkaspeller_real_delete    (GtkAspeller    *gtkaspeller);
 
 /* Checker configuration */
-static gint            set_dictionary                  (AspellConfig *config
+static EnchantDict     *set_dictionary                 (EnchantBroker *broker
                                                         Dictionary *dict);
-static void            set_sug_mode_cb                 (GtkMenuItem *w, 
+static void            set_use_both_cb                 (GtkMenuItem *w, 
                                                         GtkAspell *gtkaspell);
-static void            set_real_sug_mode               (GtkAspell *gtkaspell, 
-                                                        const char *themode);
 
 /* Checker actions */
 static gboolean check_at                       (GtkAspell      *gtkaspell, 
                                                 int             from_pos);
-static gboolean        check_next_prev                 (GtkAspell      *gtkaspell, 
-                                                gboolean        forward);
+static gboolean check_at_cb                    (gpointer       data);
 static GList* misspelled_suggest               (GtkAspell      *gtkaspell, 
-                                                guchar         *word);
+                                                gchar          *word);
+static gboolean find_misspelled_cb             (gpointer       data,
+                                                gboolean       forward);
 static void add_word_to_session_cb             (GtkWidget      *w, 
                                                 gpointer        data);
 static void add_word_to_personal_cb            (GtkWidget      *w, 
@@ -195,28 +133,27 @@ static void replace_with_supplied_word_cb (GtkWidget      *w,
 static void replace_word_cb                    (GtkWidget      *w, 
                                                 gpointer       data); 
 static void replace_real_word                  (GtkAspell      *gtkaspell, 
-                                                gchar          *newword);
+                                                const gchar    *newword);
+static void replace_real_word_cb               (gpointer       data,
+                                               const gchar     *newword);
 static void check_with_alternate_cb            (GtkWidget      *w,
                                                 gpointer        data);
-static void use_alternate_dict                 (GtkAspell      *gtkaspell);
 static void toggle_check_while_typing_cb       (GtkWidget      *w, 
                                                 gpointer        data);
 
 /* Menu creation */
-static void popup_menu                         (GtkAspell      *gtkaspell, 
-                                                GdkEventButton *eb);
-static GtkMenu*        make_sug_menu                   (GtkAspell      *gtkaspell);
-static void populate_submenu                   (GtkAspell      *gtkaspell, 
-                                                GtkWidget      *menu);
-static GtkMenu*        make_config_menu                (GtkAspell      *gtkaspell);
+static GSList* make_sug_menu                   (GtkAspell      *gtkaspell);
+static GSList * populate_submenu               (GtkAspell      *gtkaspell);
+GSList*        gtkaspell_make_config_menu              (GtkAspell      *gtkaspell);
 static void set_menu_pos                       (GtkMenu        *menu, 
                                                 gint           *x, 
                                                 gint           *y, 
                                                 gboolean       *push_in,
                                                 gpointer        data);
 /* Other menu callbacks */
-static gboolean cancel_menu_cb                 (GtkMenuShell   *w,
-                                                gpointer        data);
+static gboolean aspell_key_pressed             (GtkWidget *widget,
+                                                GdkEventKey *event,
+                                                GtkAspell *gtkaspell);
 static void change_dict_cb                     (GtkWidget      *w, 
                                                 GtkAspell      *gtkaspell);
 static void switch_to_alternate_cb             (GtkWidget      *w, 
@@ -225,12 +162,12 @@ static void switch_to_alternate_cb                (GtkWidget      *w,
 /* Misc. helper functions */
 static void            set_point_continue              (GtkAspell *gtkaspell);
 static void            continue_check                  (gpointer *gtkaspell);
-static gboolean        iswordsep                       (unsigned char c);
-static guchar          get_text_index_whar             (GtkAspell *gtkaspell, 
+static gboolean        iswordsep                       (gunichar c);
+static gunichar                get_text_index_whar             (GtkAspell *gtkaspell, 
                                                         int pos);
 static gboolean        get_word_from_pos               (GtkAspell *gtkaspell, 
                                                         gint pos, 
-                                                        unsigned char* buf,
+                                                        char* buf,
                                                         gint buflen,
                                                         gint *pstart, 
                                                         gint *pend);
@@ -241,32 +178,34 @@ static void               change_color                    (GtkAspell *gtkaspell,
                                                         gint end, 
                                                         gchar *newtext,
                                                         GdkColor *color);
-static guchar*         convert_to_aspell_encoding      (const guchar *encoding);
 static gint            compare_dict                    (Dictionary *a, 
                                                         Dictionary *b);
 static void            dictionary_delete               (Dictionary *dict);
 static Dictionary *    dictionary_dup                  (const Dictionary *dict);
-static void            free_suggestions_list           (GtkAspell *gtkaspell);
 static void            reset_theword_data              (GtkAspell *gtkaspell);
 static void            free_checkers                   (gpointer elt, 
                                                         gpointer data);
 static gint            find_gtkaspeller                (gconstpointer aa, 
                                                         gconstpointer bb);
-/* gtkspellconfig - only one config per session */
-GtkAspellConfig * gtkaspellconfig;
+
 static void destroy_menu(GtkWidget *widget, gpointer user_data);       
 
 /******************************************************************************/
 static gint get_textview_buffer_charcount(GtkTextView *view);
 
+static void            gtkaspell_free_dictionary_list  (GSList *list);
+static GSList*         gtkaspell_get_dictionary_list   (gint refresh);
+
+static void            gtkaspell_uncheck_all           (GtkAspell *gtkaspell);
+
 static gint get_textview_buffer_charcount(GtkTextView *view)
 {
        GtkTextBuffer *buffer;
 
-       g_return_val_if_fail(view, 0);
+       cm_return_val_if_fail(view, 0);
 
        buffer = gtk_text_view_get_buffer(view);
-       g_return_val_if_fail(buffer, 0);
+       cm_return_val_if_fail(buffer, 0);
 
        return gtk_text_buffer_get_char_count(buffer);
 }
@@ -276,13 +215,13 @@ static gint get_textview_buffer_offset(GtkTextView *view)
        GtkTextMark * mark;
        GtkTextIter iter;
 
-       g_return_val_if_fail(view, 0);
+       cm_return_val_if_fail(view, 0);
 
        buffer = gtk_text_view_get_buffer(view);
-       g_return_val_if_fail(buffer, 0);
+       cm_return_val_if_fail(buffer, 0);
 
        mark = gtk_text_buffer_get_insert(buffer);
-       g_return_val_if_fail(mark, 0);
+       cm_return_val_if_fail(mark, 0);
 
        gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
 
@@ -293,10 +232,10 @@ static void set_textview_buffer_offset(GtkTextView *view, gint offset)
        GtkTextBuffer *buffer;
        GtkTextIter iter;
 
-       g_return_if_fail(view);
+       cm_return_if_fail(view);
 
        buffer = gtk_text_view_get_buffer(view);
-       g_return_if_fail(buffer);
+       cm_return_if_fail(buffer);
 
        gtk_text_buffer_get_iter_at_offset(buffer, &iter, offset);
        gtk_text_buffer_place_cursor(buffer, &iter);
@@ -353,56 +292,110 @@ static void gtkaspell_checkers_error_message (gchar *message)
                gtkaspellcheckers->error_message = tmp;
        } else 
                gtkaspellcheckers->error_message = message;
+       
+       
 }
 
 const char *gtkaspell_checkers_strerror(void)
 {
-       g_return_val_if_fail(gtkaspellcheckers, "");
+       cm_return_val_if_fail(gtkaspellcheckers, "");
        return gtkaspellcheckers->error_message;
 }
 
 void gtkaspell_checkers_reset_error(void)
 {
-       g_return_if_fail(gtkaspellcheckers);
+       cm_return_if_fail(gtkaspellcheckers);
        
        g_free(gtkaspellcheckers->error_message);
        
        gtkaspellcheckers->error_message = NULL;
 }
 
-GtkAspell *gtkaspell_new(const gchar *dictionary_path,
-                        const gchar *dictionary, 
-                        const gchar *encoding,
+GtkAspell *gtkaspell_new(const gchar *dictionary
+                        const gchar *alt_dictionary, 
+                        const gchar *encoding, /* unused */
                         gint  misspelled_color,
                         gboolean check_while_typing,
+                        gboolean recheck_when_changing_dict,
                         gboolean use_alternate,
+                        gboolean use_both_dicts,
                         GtkTextView *gtktext,
-                        GtkWindow *parent_win)
+                        GtkWindow *parent_win,
+                        void (dict_changed_cb)(void *data),
+                        void (*spell_menu_cb)(void *data),
+                        void *data)
 {
        Dictionary      *dict;
        GtkAspell       *gtkaspell;
        GtkAspeller     *gtkaspeller;
        GtkTextBuffer *buffer;
 
-       g_return_val_if_fail(gtktext, NULL);
+       cm_return_val_if_fail(gtktext, NULL);
+       if (!dictionary || !*dictionary) {
+               gtkaspell_checkers_error_message(
+                               g_strdup(_("No dictionary selected.")));
+               return NULL;
+       }
+
        buffer = gtk_text_view_get_buffer(gtktext);
        
        dict           = g_new0(Dictionary, 1);
-       dict->fullname = g_strdup(dictionary);
-       dict->encoding = g_strdup(encoding);
+       if (strrchr(dictionary, '/')) {
+               dict->fullname = g_strdup(strrchr(dictionary, '/')+1);
+               dict->dictname = g_strdup(strrchr(dictionary, '/')+1);
+       } else {
+               dict->fullname = g_strdup(dictionary);
+               dict->dictname = g_strdup(dictionary);
+       }
 
+       if (strchr(dict->fullname, '-')) {
+               *(strchr(dict->fullname, '-')) = '\0';
+               *(strchr(dict->dictname, '-')) = '\0';
+       }
        gtkaspeller    = gtkaspeller_new(dict); 
        dictionary_delete(dict);
 
-       if (!gtkaspeller)
+       if (!gtkaspeller) {
+               gtkaspell_checkers_error_message(
+                               g_strdup_printf(_("Couldn't initialize %s speller."), dictionary));
                return NULL;
+       }
        
        gtkaspell = g_new0(GtkAspell, 1);
 
-       gtkaspell->dictionary_path    = g_strdup(dictionary_path);
-
        gtkaspell->gtkaspeller        = gtkaspeller;
-       gtkaspell->alternate_speller  = NULL;
+
+       if (use_alternate && alt_dictionary && *alt_dictionary) {
+               Dictionary      *alt_dict;
+               GtkAspeller     *alt_gtkaspeller;
+
+               alt_dict               = g_new0(Dictionary, 1);
+               if (strrchr(alt_dictionary, '/')) {
+                       alt_dict->fullname = g_strdup(strrchr(alt_dictionary, '/')+1);
+                       alt_dict->dictname = g_strdup(strrchr(alt_dictionary, '/')+1);
+               } else {
+                       alt_dict->fullname = g_strdup(alt_dictionary);
+                       alt_dict->dictname = g_strdup(alt_dictionary);
+               }
+               if (strchr(alt_dict->fullname, '-')) {
+                       *(strchr(alt_dict->fullname, '-')) = '\0';
+                       *(strchr(alt_dict->dictname, '-')) = '\0';
+               }
+
+               alt_gtkaspeller    = gtkaspeller_new(alt_dict);
+               dictionary_delete(alt_dict);
+
+               if (!alt_gtkaspeller) {
+                       gtkaspell_checkers_error_message(
+                               g_strdup_printf(_("Couldn't initialize %s speller."), dictionary));
+                       return NULL;
+               }
+
+               gtkaspell->alternate_speller  = alt_gtkaspeller;
+       } else {
+               gtkaspell->alternate_speller  = NULL;
+       }
+
        gtkaspell->theword[0]         = 0x00;
        gtkaspell->start_pos          = 0;
        gtkaspell->end_pos            = 0;
@@ -410,76 +403,76 @@ GtkAspell *gtkaspell_new(const gchar *dictionary_path,
        gtkaspell->end_check_pos      = -1;
        gtkaspell->misspelled         = -1;
        gtkaspell->check_while_typing = check_while_typing;
+       gtkaspell->recheck_when_changing_dict = recheck_when_changing_dict;
        gtkaspell->continue_check     = NULL;
-       gtkaspell->config_menu        = NULL;
-       gtkaspell->popup_config_menu  = NULL;
-       gtkaspell->sug_menu           = NULL;
        gtkaspell->replace_entry      = NULL;
        gtkaspell->gtktext            = gtktext;
-       gtkaspell->default_sug_mode   = ASPELL_FASTMODE;
        gtkaspell->max_sug            = -1;
        gtkaspell->suggestions_list   = NULL;
        gtkaspell->use_alternate      = use_alternate;
+       gtkaspell->use_both_dicts     = use_both_dicts;
        gtkaspell->parent_window      = GTK_WIDGET(parent_win);
-       
+       gtkaspell->dict_changed_cb = dict_changed_cb;
+       gtkaspell->menu_changed_cb = spell_menu_cb;
+       gtkaspell->menu_changed_data = data;
+
        allocate_color(gtkaspell, misspelled_color);
 
        g_signal_connect_after(G_OBJECT(buffer), "insert-text",
                               G_CALLBACK(entry_insert_cb), gtkaspell);
        g_signal_connect_after(G_OBJECT(buffer), "delete-range",
                               G_CALLBACK(entry_delete_cb), gtkaspell);
-       g_signal_connect(G_OBJECT(gtktext), "button-press-event",
+       /*g_signal_connect(G_OBJECT(gtktext), "button-press-event",
                         G_CALLBACK(button_press_intercept_cb),
-                        gtkaspell);
+                        gtkaspell);*/
+       g_signal_connect(G_OBJECT(gtktext), "populate-popup",
+                        G_CALLBACK(button_press_intercept_cb), gtkaspell);
        
-       debug_print("Aspell: created gtkaspell %0x\n", (guint) gtkaspell);
+       debug_print("Aspell: created gtkaspell %p\n", gtkaspell);
 
        return gtkaspell;
 }
 
-void gtkaspell_delete(GtkAspell * gtkaspell) 
+void gtkaspell_delete(GtkAspell *gtkaspell) 
 {
        GtkTextView *gtktext = gtkaspell->gtktext;
        
         g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
                                             G_CALLBACK(entry_insert_cb),
-                                     gtkaspell);
+                                            gtkaspell);
        g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
                                             G_CALLBACK(entry_delete_cb),
-                                            gtkaspell);
+                                            gtkaspell);
        g_signal_handlers_disconnect_by_func(G_OBJECT(gtktext),
-                                            G_CALLBACK(button_press_intercept_cb),
-                                            gtkaspell);
+                                            G_CALLBACK(button_press_intercept_cb),
+                                            gtkaspell);
 
        gtkaspell_uncheck_all(gtkaspell);
        
        gtkaspeller_delete(gtkaspell->gtkaspeller);
 
-       if (gtkaspell->use_alternate && gtkaspell->alternate_speller)
+       if (gtkaspell->alternate_speller)
                gtkaspeller_delete(gtkaspell->alternate_speller);
 
-       if (gtkaspell->sug_menu)
-               gtk_widget_destroy(gtkaspell->sug_menu);
-
-       if (gtkaspell->popup_config_menu)
-               gtk_widget_destroy(gtkaspell->popup_config_menu);
-
-       if (gtkaspell->config_menu)
-               gtk_widget_destroy(gtkaspell->config_menu);
-
        if (gtkaspell->suggestions_list)
-               free_suggestions_list(gtkaspell);
+               gtkaspell_free_suggestions_list(gtkaspell);
 
-       g_free((gchar *)gtkaspell->dictionary_path);
-       gtkaspell->dictionary_path = NULL;
-
-       debug_print("Aspell: deleting gtkaspell %0x\n", (guint) gtkaspell);
+       debug_print("Aspell: deleting gtkaspell %p\n", gtkaspell);
 
        g_free(gtkaspell);
 
        gtkaspell = NULL;
 }
 
+void gtkaspell_dict_changed(GtkAspell *gtkaspell)
+{
+       if(!gtkaspell || !gtkaspell->dict_changed_cb ||
+                       !gtkaspell->menu_changed_data)
+               return;
+
+       gtkaspell->dict_changed_cb(gtkaspell->menu_changed_data);
+}
+
 static void entry_insert_cb(GtkTextBuffer *textbuf,
                            GtkTextIter *iter,
                            gchar *newtext,
@@ -488,14 +481,14 @@ static void entry_insert_cb(GtkTextBuffer *textbuf,
 {
        guint pos;
 
-       g_return_if_fail(gtkaspell->gtkaspeller->checker);
+       cm_return_if_fail(gtkaspell->gtkaspeller->speller);
 
        if (!gtkaspell->check_while_typing)
                return;
 
        pos = gtk_text_iter_get_offset(iter);
        
-       if (iswordsep(newtext[0])) {
+       if (iswordsep(g_utf8_get_char(newtext))) {
                /* did we just end a word? */
                if (pos >= 2)
                        check_at(gtkaspell, pos - 2);
@@ -522,7 +515,7 @@ static void entry_delete_cb(GtkTextBuffer *textbuf,
        int origpos;
        gint start, end;
     
-       g_return_if_fail(gtkaspell->gtkaspeller->checker);
+       cm_return_if_fail(gtkaspell->gtkaspeller->speller);
 
        if (!gtkaspell->check_while_typing)
                return;
@@ -542,54 +535,70 @@ static void entry_delete_cb(GtkTextBuffer *textbuf,
        /* gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos); */
 }
 
-/* ok, this is pretty wacky:
- * we need to let the right-mouse-click go through, so it moves the cursor,
- * but we *can't* let it go through, because GtkText interprets rightclicks as
- * weird selection modifiers.
- *
- * so what do we do?  forge rightclicks as leftclicks, then popup the menu.
- * HACK HACK HACK.
- */
-static gint button_press_intercept_cb(GtkTextView *gtktext,
-                                     GdkEvent *e, 
-                                     GtkAspell *gtkaspell)
+void gtkaspell_make_context_menu(GtkMenu *menu, GtkAspell *gtkaspell)
 {
-       GdkEventButton *eb;
-       gboolean retval;
+       GtkMenuItem *menuitem;
+       GSList *spell_menu = NULL;
+       GSList *items;
+       gboolean suggest = FALSE;
 
-       g_return_val_if_fail(gtkaspell->gtkaspeller->checker, FALSE);
+       if (gtkaspell->misspelled && 
+           misspelled_suggest(gtkaspell, gtkaspell->theword)) {
+               spell_menu = make_sug_menu(gtkaspell);
+               suggest = TRUE;
+       } 
+       if (!spell_menu) 
+               spell_menu = gtkaspell_make_config_menu(gtkaspell);
+       
+       menuitem = GTK_MENU_ITEM(gtk_separator_menu_item_new());
+       gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), GTK_WIDGET(menuitem));
+       gtk_widget_show(GTK_WIDGET(menuitem));
+
+       spell_menu = g_slist_reverse(spell_menu);
+       for (items = spell_menu;
+            items; items = items->next) {
+               menuitem = GTK_MENU_ITEM(items->data);
+               gtk_menu_shell_prepend(GTK_MENU_SHELL(menu), GTK_WIDGET(menuitem));
+               gtk_widget_show(GTK_WIDGET(menuitem));
+       }
+       g_slist_free(spell_menu);
+       
+       g_signal_connect(G_OBJECT(menu), "deactivate",
+                                G_CALLBACK(destroy_menu),
+                                gtkaspell);
+       if (suggest)
+               g_signal_connect(G_OBJECT(menu),
+                       "key_press_event",
+                       G_CALLBACK(aspell_key_pressed),
+                       gtkaspell);
+}
 
-       if (e->type != GDK_BUTTON_PRESS) 
-               return FALSE;
-       eb = (GdkEventButton*) e;
+static void set_position_cb(gpointer data, gint pos)
+{
+       GtkAspell *gtkaspell = (GtkAspell *) data;
+       set_textview_buffer_offset(gtkaspell->gtktext, pos);
+}
 
-       if (eb->button != 3) 
-               return FALSE;
+void gtkaspell_context_set(GtkAspell *gtkaspell)
+{
+       gtkaspell->ctx.set_position     = set_position_cb;
+       gtkaspell->ctx.set_menu_pos     = set_menu_pos;
+       gtkaspell->ctx.find_misspelled  = find_misspelled_cb;
+       gtkaspell->ctx.check_word       = check_at_cb;
+       gtkaspell->ctx.replace_word     = replace_real_word_cb;
+       gtkaspell->ctx.data             = (gpointer) gtkaspell;
+}
 
-        g_signal_handlers_block_by_func(G_OBJECT(gtktext),
-                                       G_CALLBACK(button_press_intercept_cb), 
-                                       gtkaspell);
-       g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
-                             e, &retval);
+static void button_press_intercept_cb(GtkTextView *gtktext,
+                       GtkMenu *menu, GtkAspell *gtkaspell)
+{
+       gtktext = gtkaspell->gtktext;
+       gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
+       gtkaspell->misspelled = check_at(gtkaspell, gtkaspell->orig_pos);
 
-       /* forge the leftclick */
-       eb->button = 1;
-       
-       g_signal_emit_by_name(G_OBJECT(gtktext), "button-press-event",
-                             e, &retval);
-       g_signal_emit_by_name(G_OBJECT(gtktext), "button-release-event",
-                             e, &retval);
-       g_signal_handlers_unblock_by_func(G_OBJECT(gtktext),
-                                         G_CALLBACK(button_press_intercept_cb), 
-                                          gtkaspell);
-       g_signal_stop_emission_by_name(G_OBJECT(gtktext), "button-press-event");
-    
-       /* now do the menu wackiness */
-       popup_menu(gtkaspell, eb);
-       gtk_grab_remove(GTK_WIDGET(gtktext));
-       return FALSE;
+       gtkaspell_context_set(gtkaspell);       
+       gtkaspell_make_context_menu(menu, gtkaspell);
 }
-
 /* Checker creation */
 static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
 {
@@ -598,26 +607,15 @@ static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
        GtkAspeller     *tmp;
        Dictionary      *dict;
 
-       g_return_val_if_fail(gtkaspellcheckers, NULL);
+       cm_return_val_if_fail(gtkaspellcheckers, NULL);
 
-       g_return_val_if_fail(dictionary, NULL);
+       cm_return_val_if_fail(dictionary, NULL);
 
-       if (dictionary->fullname == NULL)
+       if (dictionary->dictname == NULL)
                gtkaspell_checkers_error_message(
                                g_strdup(_("No dictionary selected.")));
        
-       g_return_val_if_fail(dictionary->fullname, NULL);
-       
-       if (dictionary->dictname == NULL) {
-               gchar *tmp;
-
-               tmp = strrchr(dictionary->fullname, G_DIR_SEPARATOR);
-
-               if (tmp == NULL)
-                       dictionary->dictname = dictionary->fullname;
-               else
-                       dictionary->dictname = tmp + 1;
-       }
+       cm_return_val_if_fail(dictionary->fullname, NULL);
 
        dict = dictionary_dup(dictionary);
 
@@ -634,8 +632,8 @@ static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
                                gtkaspellcheckers->checkers,
                                gtkaspeller);
 
-               debug_print("Aspell: Created a new gtkaspeller %0x\n",
-                               (gint) gtkaspeller);
+               debug_print("Aspell: Created a new gtkaspeller %p\n",
+                               gtkaspeller);
        } else {
                dictionary_delete(dict);
 
@@ -651,50 +649,46 @@ static GtkAspeller *gtkaspeller_new(Dictionary *dictionary)
 static GtkAspeller *gtkaspeller_real_new(Dictionary *dict)
 {
        GtkAspeller             *gtkaspeller;
-       AspellConfig            *config;
-       AspellCanHaveError      *ret;
+       EnchantBroker           *broker;
+       EnchantDict             *speller;
        
-       g_return_val_if_fail(gtkaspellcheckers, NULL);
-       g_return_val_if_fail(dict, NULL);
+       cm_return_val_if_fail(gtkaspellcheckers, NULL);
+       cm_return_val_if_fail(dict, NULL);
 
        gtkaspeller = g_new(GtkAspeller, 1);
        
        gtkaspeller->dictionary = dict;
-       gtkaspeller->sug_mode   = ASPELL_FASTMODE;
 
-       config = new_aspell_config();
+       broker = enchant_broker_init();
 
-       if (!set_dictionary(config, dict))
+       if (!broker) {
+               gtkaspell_checkers_error_message(
+                               g_strdup(_("Couldn't initialize Enchant broker.")));
                return NULL;
-       
-       ret = new_aspell_speller(config);
-       delete_aspell_config(config);
-
-       if (aspell_error_number(ret) != 0) {
-               gtkaspellcheckers->error_message
-                       = g_strdup(aspell_error_message(ret));
-               
-               delete_aspell_can_have_error(ret);
-               
+       }
+       if ((speller = set_dictionary(broker, dict)) == NULL) {
+               gtkaspell_checkers_error_message(
+                               g_strdup_printf(_("Couldn't initialize %s dictionary:"), dict->fullname));
+               gtkaspell_checkers_error_message(
+                               g_strdup(enchant_broker_get_error(broker)));
                return NULL;
        }
-
-       gtkaspeller->checker = to_aspell_speller(ret);
-       gtkaspeller->config  = aspell_speller_config(gtkaspeller->checker);
+       gtkaspeller->speller = speller;
+       gtkaspeller->broker = broker;
 
        return gtkaspeller;
 }
 
 static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
 {
-       g_return_val_if_fail(gtkaspellcheckers, NULL);
+       cm_return_val_if_fail(gtkaspellcheckers, NULL);
        
        gtkaspellcheckers->checkers = 
                g_slist_remove(gtkaspellcheckers->checkers, 
                                gtkaspeller);
 
-       debug_print("Aspell: Deleting gtkaspeller %0x.\n", 
-                       (gint) gtkaspeller);
+       debug_print("Aspell: Deleting gtkaspeller %p.\n", 
+                       gtkaspeller);
 
        gtkaspeller_real_delete(gtkaspeller);
 
@@ -706,17 +700,16 @@ static GtkAspeller *gtkaspeller_delete(GtkAspeller *gtkaspeller)
 
 static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
 {
-       g_return_val_if_fail(gtkaspeller,          NULL);
-       g_return_val_if_fail(gtkaspeller->checker, NULL);
-
-       aspell_speller_save_all_word_lists(gtkaspeller->checker);
+       cm_return_val_if_fail(gtkaspeller,          NULL);
+       cm_return_val_if_fail(gtkaspeller->speller, NULL);
 
-       delete_aspell_speller(gtkaspeller->checker);
+       enchant_broker_free_dict(gtkaspeller->broker, gtkaspeller->speller);
+       enchant_broker_free(gtkaspeller->broker);
 
        dictionary_delete(gtkaspeller->dictionary);
 
-       debug_print("Aspell: gtkaspeller %0x deleted.\n", 
-                   (gint) gtkaspeller);
+       debug_print("Aspell: gtkaspeller %p deleted.\n", 
+                   gtkaspeller);
 
        g_free(gtkaspeller);
 
@@ -726,227 +719,92 @@ static GtkAspeller *gtkaspeller_real_delete(GtkAspeller *gtkaspeller)
 /*****************************************************************************/
 /* Checker configuration */
 
-static gboolean set_dictionary(AspellConfig *config, Dictionary *dict)
+static EnchantDict *set_dictionary(EnchantBroker *broker, Dictionary *dict)
 {
-       gchar *language = NULL;
-       gchar *jargon = NULL;
-       gchar *size   = NULL;
-       gchar  buf[BUFSIZE];
-       
-       g_return_val_if_fail(config, FALSE);
-       g_return_val_if_fail(dict,   FALSE);
-
-       strncpy(buf, dict->fullname, BUFSIZE-1);
-       buf[BUFSIZE-1] = 0x00;
-
-       buf[dict->dictname - dict->fullname] = 0x00;
-
-       CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("dict-dir", buf);
-       debug_print("Aspell: looking for dictionaries in path %s.\n", buf);
-
-       strncpy(buf, dict->dictname, BUFSIZE-1);
-       language = buf;
-       
-       if ((size = strrchr(buf, '-')) && isdigit((int) size[1]))
-               *size++ = 0x00;
-       else
-               size = NULL;
-                               
-       if ((jargon = strchr(language, '-')) != NULL) 
-               *jargon++ = 0x00;
-       
-       if (size != NULL && jargon == size)
-               jargon = NULL;
-
-       debug_print("Aspell: language: %s, jargon: %s, size: %s\n",
-                   language, jargon ? jargon : "",
-                    size ? size : "");
-       
-       if (language)
-               CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("lang", language);
-       if (jargon)
-               CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("jargon", jargon);
-       if (size)
-               CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("size", size);
-       if (dict->encoding) {
-               gchar *aspell_enc;
-       
-               aspell_enc = convert_to_aspell_encoding (dict->encoding);
-               aspell_config_replace(config, "encoding",
-                                     (const char *) aspell_enc);
-               g_free(aspell_enc);
+       cm_return_val_if_fail(broker, FALSE);
+       cm_return_val_if_fail(dict,   FALSE);
 
-               RETURN_FALSE_IF_CONFIG_ERROR();
-       }
-       
-       return TRUE;
+       return enchant_broker_request_dict(broker, dict->dictname );
 }
 
-guchar *gtkaspell_get_dict(GtkAspell *gtkaspell)
+static void set_use_both_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
 {
+       gtkaspell->use_both_dicts = gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(w));
+       gtkaspell_dict_changed(gtkaspell);
 
-       g_return_val_if_fail(gtkaspell->gtkaspeller->config,     NULL);
-       g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
-       
-       return g_strdup(gtkaspell->gtkaspeller->dictionary->dictname);
+       if (gtkaspell->menu_changed_cb)
+               gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
 }
   
-guchar *gtkaspell_get_path(GtkAspell *gtkaspell)
-{
-       guchar *path;
-       Dictionary *dict;
-
-       g_return_val_if_fail(gtkaspell->gtkaspeller->config, NULL);
-       g_return_val_if_fail(gtkaspell->gtkaspeller->dictionary, NULL);
-
-       dict = gtkaspell->gtkaspeller->dictionary;
-       path = g_strndup(dict->fullname, dict->dictname - dict->fullname);
-
-       return path;
-}
-
-/* set_sug_mode_cb() - Menu callback: Set the suggestion mode */
-static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
-{
-       char *themode;
-       
-       themode = (char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
-       themode = g_strdup(themode);
-       
-       set_real_sug_mode(gtkaspell, themode);
-       g_free(themode);
-
-       if (gtkaspell->config_menu)
-               populate_submenu(gtkaspell, gtkaspell->config_menu);
-}
-
-static void set_real_sug_mode(GtkAspell *gtkaspell, const char *themode)
-{
-       gint result;
-       gint mode = ASPELL_FASTMODE;
-       g_return_if_fail(gtkaspell);
-       g_return_if_fail(gtkaspell->gtkaspeller);
-       g_return_if_fail(themode);
-
-       if (!strcmp(themode,_("Normal Mode")))
-               mode = ASPELL_NORMALMODE;
-       else if (!strcmp( themode,_("Bad Spellers Mode")))
-               mode = ASPELL_BADSPELLERMODE;
-
-       result = gtkaspell_set_sug_mode(gtkaspell, mode);
-
-       if(!result) {
-               debug_print("Aspell: error while changing suggestion mode:%s\n",
-                           gtkaspellcheckers->error_message);
-               gtkaspell_checkers_reset_error();
-       }
-}
-  
-/* gtkaspell_set_sug_mode() - Set the suggestion mode */
-gboolean gtkaspell_set_sug_mode(GtkAspell *gtkaspell, gint themode)
-{
-       AspellConfig *config;
-
-       g_return_val_if_fail(gtkaspell, FALSE);
-       g_return_val_if_fail(gtkaspell->gtkaspeller, FALSE);
-       g_return_val_if_fail(gtkaspell->gtkaspeller->config, FALSE);
-
-       debug_print("Aspell: setting sug mode of gtkaspeller %0x to %d\n",
-                       (guint) gtkaspell->gtkaspeller, themode);
-
-       config = gtkaspell->gtkaspeller->config;
-
-       switch (themode) {
-               case ASPELL_FASTMODE: 
-                       CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "fast");
-                       break;
-               case ASPELL_NORMALMODE: 
-                       CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", "normal");
-                       break;
-               case ASPELL_BADSPELLERMODE: 
-                       CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("sug-mode", 
-                                                           "bad-spellers");
-                       break;
-               default: 
-                       gtkaspellcheckers->error_message = 
-                               g_strdup(_("Unknown suggestion mode."));
-                       return FALSE;
-               }
-
-       gtkaspell->gtkaspeller->sug_mode = themode;
-       gtkaspell->default_sug_mode      = themode;
-
-       return TRUE;
-}
-
 /* misspelled_suggest() - Create a suggestion list for  word  */
-static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word) 
+static GList *misspelled_suggest(GtkAspell *gtkaspell, gchar *word) 
 {
-       const guchar          *newword;
        GList                 *list = NULL;
-       const AspellWordList  *suggestions;
-       AspellStringEnumeration *elements;
+       char **suggestions;
+       size_t num_sug, i;
+       cm_return_val_if_fail(word, NULL);
 
-       g_return_val_if_fail(word, NULL);
-
-       if (!aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)) {
-               free_suggestions_list(gtkaspell);
+       if (*word == 0)
+               return NULL;
 
-               suggestions = aspell_speller_suggest(
-                               gtkaspell->gtkaspeller->checker,
-                                                    (const char *)word, -1);
-               elements    = aspell_word_list_elements(suggestions);
-               list        = g_list_append(list, g_strdup(word)); 
-               
-               while (newword = aspell_string_enumeration_next(elements))
-                       list = g_list_append(list, g_strdup(newword));
+       gtkaspell_free_suggestions_list(gtkaspell);
 
-               gtkaspell->max_sug          = g_list_length(list) - 1;
+       suggestions = enchant_dict_suggest(gtkaspell->gtkaspeller->speller, word, strlen(word), &num_sug);
+       list = g_list_append(list, g_strdup(word)); 
+       if (suggestions == NULL || num_sug == 0) {
+               gtkaspell->max_sug          = -1;
                gtkaspell->suggestions_list = list;
-
                return list;
        }
+       for (i = 0; i < num_sug; i++)
+               list = g_list_append(list, g_strdup((gchar *)suggestions[i]));
 
-       free_suggestions_list(gtkaspell);
-
-       return NULL;
+       gtkaspell->max_sug          = num_sug - 1;
+       gtkaspell->suggestions_list = list;
+       enchant_dict_free_string_list(gtkaspell->gtkaspeller->speller, suggestions);
+       return list;
 }
 
 /* misspelled_test() - Just test if word is correctly spelled */  
-static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word) 
+int gtkaspell_misspelled_test(GtkAspell *gtkaspell, char *word) 
 {
-       return aspell_speller_check(gtkaspell->gtkaspeller->checker, word, -1)
-                                   ? 0 : 1;
+       gint result = 0;
+       cm_return_val_if_fail(word, 0);
+
+       if (*word == 0)
+               return 0;
+
+       result = enchant_dict_check(gtkaspell->gtkaspeller->speller, word, strlen(word));
+
+       if (result && gtkaspell->use_both_dicts && gtkaspell->alternate_speller) {
+               gtkaspell_use_alternate_dict(gtkaspell);
+               result = enchant_dict_check(gtkaspell->gtkaspeller->speller, word, strlen(word));
+               gtkaspell_use_alternate_dict(gtkaspell);
+       }
+       return (result && strcasecmp(word, "sylpheed") && 
+               strcasecmp(word, "claws-mail"));
 }
 
 
-static gboolean iswordsep(unsigned char c) 
+static gboolean iswordsep(gunichar c) 
 {
-       return !isalpha(c) && c != '\'';
+       return (g_unichar_isspace(c) || g_unichar_ispunct(c)) && c != (gunichar)'\'';
 }
 
-static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos) 
+static gunichar get_text_index_whar(GtkAspell *gtkaspell, int pos) 
 {
        GtkTextView *view = gtkaspell->gtktext;
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(view);
        GtkTextIter start, end;
-       const gchar *utf8chars;
-       guchar a = '\0';
+       gchar *utf8chars;
+       gunichar a = '\0';
 
        gtk_text_buffer_get_iter_at_offset(buffer, &start, pos);
        gtk_text_buffer_get_iter_at_offset(buffer, &end, pos+1);
 
        utf8chars = gtk_text_iter_get_text(&start, &end);
-       if (is_ascii_str(utf8chars)) {
-               a = utf8chars ? utf8chars[0] : '\0' ;
-       } else {
-               gchar *tr = conv_iconv_strdup(utf8chars, CS_UTF_8, 
-                               gtkaspell->gtkaspeller->dictionary->encoding);
-               if (tr) {
-                       a = tr[0];
-                       g_free(tr);
-               }
-       }
+       a = g_utf8_get_char(utf8chars);
+       g_free(utf8chars);
 
        return a;
 }
@@ -954,7 +812,7 @@ static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos)
 /* get_word_from_pos () - return the word pointed to. */
 /* Handles correctly the quotes. */
 static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos, 
-                                  unsigned char* buf, gint buflen,
+                                  char* buf, gint buflen,
                                   gint *pstart, gint *pend) 
 {
 
@@ -966,7 +824,7 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
        gint start;
        gint end;
                  
-       guchar c;
+       gunichar c;
        GtkTextView *gtktext;
        
        gtktext = gtkaspell->gtktext;
@@ -980,9 +838,13 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
         
        for (start = pos; start >= 0; --start) {
                c = get_text_index_whar(gtkaspell, start);
-               if (c == '\'') {
+               if (c == (gunichar)'\'') {
                        if (start > 0) {
-                               if (!isalpha(get_text_index_whar(gtkaspell,
+                               if (g_unichar_isspace(get_text_index_whar(gtkaspell,
+                                                                start - 1))
+                               ||  g_unichar_ispunct(get_text_index_whar(gtkaspell,
+                                                                start - 1))
+                               ||  g_unichar_isdigit(get_text_index_whar(gtkaspell,
                                                                 start - 1))) {
                                        /* start_quote = TRUE; */
                                        break;
@@ -993,7 +855,7 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
                                break;
                        }
                }
-               else if (!isalpha(c))
+               else if (g_unichar_isspace(c) || g_unichar_ispunct(c) || g_unichar_isdigit(c))
                                break;
        }
 
@@ -1001,9 +863,13 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
 
        for (end = pos; end < get_textview_buffer_charcount(gtktext); end++) {
                c = get_text_index_whar(gtkaspell, end); 
-               if (c == '\'') {
+               if (c == (gunichar)'\'') {
                        if (end < get_textview_buffer_charcount(gtktext)) {
-                               if (!isalpha(get_text_index_whar(gtkaspell,
+                               if (g_unichar_isspace(get_text_index_whar(gtkaspell,
+                                                                end + 1))
+                               ||  g_unichar_ispunct(get_text_index_whar(gtkaspell,
+                                                                end + 1))
+                               ||  g_unichar_isdigit(get_text_index_whar(gtkaspell,
                                                                 end + 1))) {
                                        /* end_quote = TRUE; */
                                        break;
@@ -1014,7 +880,7 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
                                break;
                        }
                }
-               else if(!isalpha(c))
+               else if (g_unichar_isspace(c) || g_unichar_ispunct(c) || g_unichar_isdigit(c))
                                break;
        }
                                                
@@ -1025,11 +891,15 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
 
        if (buf) {
                if (end - start < buflen) {
-                       for (pos = start; pos < end; pos++) {
-                               buf[pos - start] =
-                                       get_text_index_whar(gtkaspell, pos);
-                       }
-                       buf[pos - start] = 0;
+                       GtkTextIter iterstart, iterend;
+                       gchar *tmp;
+                       GtkTextBuffer *buffer = gtk_text_view_get_buffer(gtktext);
+                       gtk_text_buffer_get_iter_at_offset(buffer, &iterstart, start);
+                       gtk_text_buffer_get_iter_at_offset(buffer, &iterend, end);
+                       tmp = gtk_text_buffer_get_text(buffer, &iterstart, &iterend, FALSE);
+                       strncpy(buf, tmp, buflen-1);
+                       buf[buflen-1]='\0';
+                       g_free(tmp);
                } else
                        return FALSE;
        }
@@ -1040,10 +910,10 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
 static gboolean check_at(GtkAspell *gtkaspell, gint from_pos) 
 {
        gint          start, end;
-       unsigned char buf[GTKASPELLWORDSIZE];
+       char buf[GTKASPELLWORDSIZE];
        GtkTextView     *gtktext;
 
-       g_return_val_if_fail(from_pos >= 0, FALSE);
+       cm_return_val_if_fail(from_pos >= 0, FALSE);
     
        gtktext = gtkaspell->gtktext;
 
@@ -1051,28 +921,35 @@ static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
                               &start, &end))
                return FALSE;
 
-       if (misspelled_test(gtkaspell, buf)) {
-               strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
+       if (gtkaspell_misspelled_test(gtkaspell, buf)) {
+               strncpy(gtkaspell->theword, (gchar *)buf, GTKASPELLWORDSIZE - 1);
                gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
                gtkaspell->start_pos  = start;
                gtkaspell->end_pos    = end;
-               free_suggestions_list(gtkaspell);
+               gtkaspell_free_suggestions_list(gtkaspell);
 
-               change_color(gtkaspell, start, end, buf, &(gtkaspell->highlight));
+               change_color(gtkaspell, start, end, (gchar *)buf, &(gtkaspell->highlight));
                return TRUE;
        } else {
-               change_color(gtkaspell, start, end, buf, NULL);
+               change_color(gtkaspell, start, end, (gchar *)buf, NULL);
                return FALSE;
        }
 }
 
-static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
+static gboolean check_at_cb(gpointer data)
+{
+       GtkAspell *gtkaspell = (GtkAspell *)data;
+       return check_at(gtkaspell, gtkaspell->start_pos);
+}
+
+static gboolean find_misspelled_cb(gpointer data, gboolean forward)
 {
+       GtkAspell *gtkaspell = (GtkAspell *)data;
+       gboolean misspelled;
        gint pos;
        gint minpos;
        gint maxpos;
        gint direc = -1;
-       gboolean misspelled;
        
        minpos = 0;
        maxpos = gtkaspell->end_check_pos;
@@ -1099,37 +976,60 @@ static gboolean check_next_prev(GtkAspell *gtkaspell, gboolean forward)
                       pos > minpos && pos <= maxpos)
                        pos += direc;
        }
+       
+       return misspelled;
+}
+
+gboolean gtkaspell_check_next_prev(GtkAspell *gtkaspell, gboolean forward)
+{
+       gboolean misspelled = gtkaspell->ctx.find_misspelled(gtkaspell->ctx.data,
+                                                       forward);
        if (misspelled) {
-               GtkMenu *menu = NULL;
+               GSList *list, *cur;
+               GtkWidget *menu;
                misspelled_suggest(gtkaspell, gtkaspell->theword);
 
                if (forward)
                        gtkaspell->orig_pos = gtkaspell->end_pos;
 
-               set_textview_buffer_offset(gtkaspell->gtktext,
-                               gtkaspell->end_pos);
-               /* scroll line to window center */
-               gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
-                       gtk_text_buffer_get_insert(
-                               gtk_text_view_get_buffer(gtkaspell->gtktext)),
-                       0.0, TRUE, 0.0, 0.5);
-               /* let textview recalculate coordinates (set_menu_pos) */
-               while (gtk_events_pending ())
-                       gtk_main_iteration ();
-
-               menu = make_sug_menu(gtkaspell);
-               gtk_menu_popup(menu, NULL, NULL,
-                               set_menu_pos, gtkaspell, 0, GDK_CURRENT_TIME);
+               gtkaspell->ctx.set_position(gtkaspell->ctx.data, gtkaspell->end_pos);
+               
+               /* only execute when in textview context */
+               if (gtkaspell == (GtkAspell *)gtkaspell->ctx.data) {
+                       /* scroll line to window center */
+                       gtk_text_view_scroll_to_mark(gtkaspell->gtktext,
+                               gtk_text_buffer_get_insert(
+                                       gtk_text_view_get_buffer(gtkaspell->gtktext)),
+                                       0.0, TRUE, 0.0, 0.5);
+                       /* let textview recalculate coordinates (set_menu_pos) */
+                       while (gtk_events_pending ())
+                               gtk_main_iteration ();
+               }
+
+               list = make_sug_menu(gtkaspell);
+               menu = gtk_menu_new();
+               for (cur = list; cur; cur = cur->next)
+                       gtk_menu_shell_append(GTK_MENU_SHELL(menu), GTK_WIDGET(cur->data));
+               g_slist_free(list);
+               gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
+                               gtkaspell->ctx.set_menu_pos,
+                               gtkaspell->ctx.data,
+                               0, GDK_CURRENT_TIME);
                g_signal_connect(G_OBJECT(menu), "deactivate",
-                                        G_CALLBACK(destroy_menu), 
+                                        G_CALLBACK(destroy_menu),
                                         gtkaspell);
+               g_signal_connect(G_OBJECT(menu),
+                       "key_press_event",
+                       G_CALLBACK(aspell_key_pressed),
+                       gtkaspell);
+
 
        } else {
                reset_theword_data(gtkaspell);
 
                alertpanel_notice(_("No misspelled word found."));
-               set_textview_buffer_offset(gtkaspell->gtktext,
-                                         gtkaspell->orig_pos);
+               gtkaspell->ctx.set_position(gtkaspell->ctx.data,
+                                       gtkaspell->orig_pos);
        }
 
        return misspelled;
@@ -1140,7 +1040,8 @@ void gtkaspell_check_backwards(GtkAspell *gtkaspell)
        gtkaspell->continue_check = NULL;
        gtkaspell->end_check_pos =
                get_textview_buffer_charcount(gtkaspell->gtktext);
-       check_next_prev(gtkaspell, FALSE);
+       gtkaspell_context_set(gtkaspell);
+       gtkaspell_check_next_prev(gtkaspell, FALSE);
 }
 
 void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
@@ -1149,7 +1050,8 @@ void gtkaspell_check_forwards_go(GtkAspell *gtkaspell)
        gtkaspell->continue_check = NULL;
        gtkaspell->end_check_pos =
                get_textview_buffer_charcount(gtkaspell->gtktext);
-       check_next_prev(gtkaspell, TRUE);
+       gtkaspell_context_set(gtkaspell);
+       gtkaspell_check_next_prev(gtkaspell, TRUE);
 }
 
 void gtkaspell_check_all(GtkAspell *gtkaspell)
@@ -1159,8 +1061,8 @@ void gtkaspell_check_all(GtkAspell *gtkaspell)
        GtkTextBuffer *buffer;
        GtkTextIter startiter, enditer;
 
-       g_return_if_fail(gtkaspell);
-       g_return_if_fail(gtkaspell->gtktext);
+       cm_return_if_fail(gtkaspell);
+       cm_return_if_fail(gtkaspell->gtktext);
 
        gtktext = gtkaspell->gtktext;
        buffer = gtk_text_view_get_buffer(gtktext);
@@ -1184,7 +1086,8 @@ void gtkaspell_check_all(GtkAspell *gtkaspell)
        gtkaspell->continue_check = continue_check;
        gtkaspell->end_check_pos  = end;
 
-       gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
+       gtkaspell_context_set(gtkaspell);
+       gtkaspell->misspelled = gtkaspell_check_next_prev(gtkaspell, TRUE);
 }      
 
 static void continue_check(gpointer *data)
@@ -1192,7 +1095,7 @@ static void continue_check(gpointer *data)
        GtkAspell *gtkaspell = (GtkAspell *) data;
        gint pos = get_textview_buffer_offset(gtkaspell->gtktext);
        if (pos < gtkaspell->end_check_pos && gtkaspell->misspelled)
-               gtkaspell->misspelled = check_next_prev(gtkaspell, TRUE);
+               gtkaspell->misspelled = gtkaspell_check_next_prev(gtkaspell, TRUE);
        else
                gtkaspell->continue_check = NULL;
 }
@@ -1204,7 +1107,7 @@ void gtkaspell_highlight_all(GtkAspell *gtkaspell)
        guint     len;
        GtkTextView *gtktext;
 
-       g_return_if_fail(gtkaspell->gtkaspeller->checker);      
+       cm_return_if_fail(gtkaspell->gtkaspeller->speller);     
 
        gtktext = gtkaspell->gtktext;
 
@@ -1227,21 +1130,20 @@ void gtkaspell_highlight_all(GtkAspell *gtkaspell)
 
 static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell) 
 {
-       unsigned char *newword;
+       char *newword;
        GdkEvent *e= (GdkEvent *) gtk_get_current_event();
 
        newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
                                         0, -1);
 
        if (strcmp(newword, gtkaspell->theword)) {
-               replace_real_word(gtkaspell, newword);
+               gtkaspell->ctx.replace_word(gtkaspell->ctx.data, newword);
 
                if ((e->type == GDK_KEY_PRESS &&
                    ((GdkEventKey *) e)->state & GDK_CONTROL_MASK)) {
-                       aspell_speller_store_replacement(
-                                       gtkaspell->gtkaspeller->checker,
-                                        gtkaspell->theword, -1,
-                                        newword, -1);
+                       enchant_dict_store_replacement(gtkaspell->gtkaspeller->speller, 
+                                       gtkaspell->theword, strlen(gtkaspell->theword),
+                                       newword, strlen(newword));
                }
                gtkaspell->replace_entry = NULL;
        }
@@ -1258,32 +1160,29 @@ static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
 
 static void replace_word_cb(GtkWidget *w, gpointer data)
 {
-       unsigned char *newword;
+       const char *newword;
        GtkAspell *gtkaspell = (GtkAspell *) data;
        GdkEvent *e= (GdkEvent *) gtk_get_current_event();
 
-       newword = (unsigned char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
-       newword = g_strdup(newword);
+       newword = gtk_label_get_text(GTK_LABEL(gtk_bin_get_child(GTK_BIN((w)))));
 
-       replace_real_word(gtkaspell, newword);
+       gtkaspell->ctx.replace_word(gtkaspell->ctx.data, newword);
 
        if ((e->type == GDK_KEY_PRESS && 
            ((GdkEventKey *) e)->state & GDK_CONTROL_MASK) ||
            (e->type == GDK_BUTTON_RELEASE && 
             ((GdkEventButton *) e)->state & GDK_CONTROL_MASK)) {
-               aspell_speller_store_replacement(
-                               gtkaspell->gtkaspeller->checker,
-                                                gtkaspell->theword, -1, 
-                                                newword, -1);
+               enchant_dict_store_replacement(gtkaspell->gtkaspeller->speller, 
+                               gtkaspell->theword, strlen(gtkaspell->theword),
+                               newword, strlen(newword));
        }
 
        gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
 
        set_point_continue(gtkaspell);
-       g_free(newword);
 }
 
-static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
+static void replace_real_word(GtkAspell *gtkaspell, const gchar *newword)
 {
        int             oldlen, newlen, wordlen;
        gint            origpos;
@@ -1357,21 +1256,23 @@ static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
        set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
 }
 
+static void replace_real_word_cb(gpointer data, const gchar *newword)
+{
+       replace_real_word((GtkAspell *)data, newword);
+}
+
 /* Accept this word for this session */
 static void add_word_to_session_cb(GtkWidget *w, gpointer data)
 {
-       guint     pos;
        GtkTextView *gtktext;
        GtkAspell *gtkaspell = (GtkAspell *) data; 
        gtktext = gtkaspell->gtktext;
 
-       pos = get_textview_buffer_offset(gtktext);
 
-       aspell_speller_add_to_session(gtkaspell->gtkaspeller->checker,
-                                     gtkaspell->theword,
-                                     strlen(gtkaspell->theword));
+       enchant_dict_add_to_session(gtkaspell->gtkaspeller->speller, gtkaspell->theword, strlen(gtkaspell->theword));
 
-       check_at(gtkaspell, gtkaspell->start_pos);
+       gtkaspell->ctx.check_word(gtkaspell->ctx.data);
+       gtkaspell_dict_changed(gtkaspell);
 
        gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
 
@@ -1383,44 +1284,53 @@ static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
 {
        GtkAspell *gtkaspell = (GtkAspell *) data; 
 
-       aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
-                                      gtkaspell->theword,
-                                      strlen(gtkaspell->theword));
-
-       check_at(gtkaspell, gtkaspell->start_pos);
+       enchant_dict_add_to_pwl(gtkaspell->gtkaspeller->speller, gtkaspell->theword, strlen(gtkaspell->theword));
 
+       gtkaspell->ctx.check_word(gtkaspell->ctx.data);
+       gtkaspell_dict_changed(gtkaspell);
+       
        gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
        set_point_continue(gtkaspell);
 }
 
 static void check_with_alternate_cb(GtkWidget *w, gpointer data)
 {
-       GtkAspell *gtkaspell = (GtkAspell *) data;
+       GtkAspell *gtkaspell = (GtkAspell *)data;
        gint misspelled;
 
        gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
 
-       use_alternate_dict(gtkaspell);
-       misspelled = check_at(gtkaspell, gtkaspell->start_pos);
+       gtkaspell_use_alternate_dict(gtkaspell);
+       misspelled = gtkaspell->ctx.check_word(gtkaspell->ctx.data);
 
        if (!gtkaspell->continue_check) {
 
                gtkaspell->misspelled = misspelled;
 
                if (gtkaspell->misspelled) {
-                       GtkMenu *menu;
+                       GtkWidget *menu;
+                       GSList *list, *cur;
                        misspelled_suggest(gtkaspell, gtkaspell->theword);
 
-                       set_textview_buffer_offset(gtkaspell->gtktext,
+                       gtkaspell->ctx.set_position(gtkaspell->ctx.data,
                                            gtkaspell->end_pos);
 
-                       menu = make_sug_menu(gtkaspell);
-                       gtk_menu_popup(menu, NULL, NULL,
-                                      set_menu_pos, gtkaspell, 0,
+                       list = make_sug_menu(gtkaspell);
+                       menu = gtk_menu_new();
+                       for (cur = list; cur; cur = cur->next)
+                               gtk_menu_shell_append(GTK_MENU_SHELL(menu), GTK_WIDGET(cur->data));
+                       g_slist_free(list);
+                       gtk_menu_popup(GTK_MENU(menu), NULL, NULL,
+                                      gtkaspell->ctx.set_menu_pos,
+                                      gtkaspell->ctx.data, 0,
                                       GDK_CURRENT_TIME);
                        g_signal_connect(G_OBJECT(menu), "deactivate",
-                                        G_CALLBACK(destroy_menu), 
+                                        G_CALLBACK(destroy_menu),
                                         gtkaspell);
+                       g_signal_connect(G_OBJECT(menu),
+                               "key_press_event",
+                               G_CALLBACK(aspell_key_pressed),
+                               gtkaspell);
                        return;
                }
        } else
@@ -1456,7 +1366,7 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
        GtkWidget *cancel_button;
        GtkWidget *confirm_area;
        GtkWidget *icon;
-       gchar *thelabel;
+       gchar *utf8buf, *thelabel;
        gint xx, yy;
        GtkAspell *gtkaspell = (GtkAspell *) data;
 
@@ -1468,8 +1378,7 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
 
        gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
        gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
-       gtk_dialog_set_has_separator (GTK_DIALOG (dialog), FALSE);
-       gtk_widget_set_uposition(dialog, xx, yy);
+       gtk_window_move(GTK_WINDOW(dialog), xx, yy);
 
        g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
                                 G_CALLBACK(gtk_widget_destroy), 
@@ -1482,9 +1391,11 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
        gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
                            FALSE, FALSE, 0);
 
+       utf8buf  = g_strdup(gtkaspell->theword);
+
        thelabel = g_strdup_printf(_("<span weight=\"bold\" "
                                        "size=\"larger\">Replace \"%s\" with: </span>"), 
-                                  gtkaspell->theword);
+                                  utf8buf);
        /* for title label */
        w_hbox = gtk_hbox_new(FALSE, 0);
        
@@ -1520,12 +1431,13 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
        
        entry = gtk_entry_new();
        gtkaspell->replace_entry = entry;
-       gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
+       gtk_entry_set_text(GTK_ENTRY(entry), utf8buf);
        gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
        g_signal_connect(G_OBJECT(dialog),
                        "key_press_event",
                        G_CALLBACK(replace_key_pressed), gtkaspell);
        gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
+       g_free(utf8buf);  
 
        label = gtk_label_new(_("Holding down Control key while pressing "
                                "Enter\nwill learn from mistake.\n"));
@@ -1537,8 +1449,8 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
        hbox = gtk_hbox_new(TRUE, 0);
 
        gtkut_stock_button_set_create(&confirm_area,
-                                     &ok_button, GTK_STOCK_OK,
                                      &cancel_button, GTK_STOCK_CANCEL,
+                                     &ok_button, GTK_STOCK_OK,
                                      NULL, NULL);
 
        gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
@@ -1563,7 +1475,7 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
        gtk_widget_show_all(dialog);
 }
 
-void gtkaspell_uncheck_all(GtkAspell * gtkaspell) 
+static void gtkaspell_uncheck_all(GtkAspell * gtkaspell) 
 {
        GtkTextView *gtktext;
        GtkTextBuffer *buffer;
@@ -1587,9 +1499,8 @@ static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
 
        if (!gtkaspell->check_while_typing)
                gtkaspell_uncheck_all(gtkaspell);
-
-       if (gtkaspell->config_menu)
-               populate_submenu(gtkaspell, gtkaspell->config_menu);
+       if (gtkaspell->menu_changed_cb)
+               gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
 }
 
 static GSList *create_empty_dictionary_list(void)
@@ -1599,21 +1510,38 @@ static GSList *create_empty_dictionary_list(void)
 
        dict = g_new0(Dictionary, 1);
        dict->fullname = g_strdup(_("None"));
-       dict->dictname = dict->fullname;
-       dict->encoding = NULL;
+       dict->dictname = NULL;
 
        return g_slist_append(list, dict);
 }
 
+static void list_dict_cb(const char * const lang_tag,
+                const char * const provider_name,
+                const char * const provider_desc,
+                const char * const provider_file,
+                void * data)
+{
+       GSList **list = (GSList **)data;
+       Dictionary *dict = g_new0(Dictionary, 1);
+       dict->fullname = g_strdup(lang_tag);
+       dict->dictname = g_strdup(lang_tag);
+
+       if (g_slist_find_custom(*list, dict, 
+                       (GCompareFunc) compare_dict) == NULL) {
+               debug_print("Aspell: found dictionary %s %s\n", dict->fullname,
+                               dict->dictname);
+               *list = g_slist_insert_sorted(*list, dict,
+                               (GCompareFunc) compare_dict);
+       } else {
+               dictionary_delete(dict);
+       }
+}
+
 /* gtkaspell_get_dictionary_list() - returns list of dictionary names */
-GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
+static GSList *gtkaspell_get_dictionary_list(gint refresh)
 {
        GSList *list;
-       Dictionary *dict;
-       AspellConfig *config;
-       AspellDictInfoList *dlist;
-       AspellDictInfoEnumeration *dels;
-       const AspellDictInfo *entry;
+       EnchantBroker *broker;
 
        if (!gtkaspellcheckers)
                gtkaspell_checkers_init();
@@ -1625,46 +1553,13 @@ GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
                                gtkaspellcheckers->dictionary_list);
        list = NULL;
 
-       config = new_aspell_config();
+       broker = enchant_broker_init();
 
-       aspell_config_replace(config, "dict-dir", aspell_path);
-       if (aspell_config_error_number(config) != 0) {
-               gtkaspellcheckers->error_message = g_strdup(
-                               aspell_config_error_message(config));
-               gtkaspellcheckers->dictionary_list =
-                       create_empty_dictionary_list();
+       enchant_broker_list_dicts(broker, list_dict_cb, &list);
 
-               return gtkaspellcheckers->dictionary_list; 
-       }
+       enchant_broker_free(broker);
 
-       dlist = get_aspell_dict_info_list(config);
-       delete_aspell_config(config);
-
-       debug_print("Aspell: checking for dictionaries in %s\n", aspell_path);
-       dels = aspell_dict_info_list_elements(dlist);
-       while ( (entry = aspell_dict_info_enumeration_next(dels)) != 0) 
-       {
-               dict = g_new0(Dictionary, 1);
-               dict->fullname = g_strdup_printf("%s%s", aspell_path, 
-                               entry->name);
-               dict->dictname = dict->fullname + strlen(aspell_path);
-               dict->encoding = g_strdup(entry->code);
-               
-               if (g_slist_find_custom(list, dict, 
-                               (GCompareFunc) compare_dict) != NULL) {
-                       dictionary_delete(dict);
-                       continue;       
-               }
-               
-               debug_print("Aspell: found dictionary %s %s %s\n", dict->fullname,
-                               dict->dictname, dict->encoding);
-               list = g_slist_insert_sorted(list, dict,
-                               (GCompareFunc) compare_dict);
-       }
-
-       delete_aspell_dict_info_enumeration(dels);
-       
-        if(list==NULL){
+        if (list == NULL){
                
                debug_print("Aspell: error when searching for dictionaries: "
                              "No dictionary found.\n");
@@ -1676,7 +1571,7 @@ GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
        return list;
 }
 
-void gtkaspell_free_dictionary_list(GSList *list)
+static void gtkaspell_free_dictionary_list(GSList *list)
 {
        Dictionary *dict;
        GSList *walk;
@@ -1688,153 +1583,123 @@ void gtkaspell_free_dictionary_list(GSList *list)
        g_slist_free(list);
 }
 
-GtkWidget *gtkaspell_dictionary_option_menu_new(const gchar *aspell_path)
+GtkTreeModel *gtkaspell_dictionary_store_new_with_refresh(gboolean refresh)
 {
        GSList *dict_list, *tmp;
-       GtkWidget *item;
-       GtkWidget *menu;
+       GtkListStore *store;
+       GtkTreeIter iter;
        Dictionary *dict;
 
-       dict_list = gtkaspell_get_dictionary_list(aspell_path, TRUE);
-       g_return_val_if_fail(dict_list, NULL);
+       dict_list = gtkaspell_get_dictionary_list(refresh);
+       cm_return_val_if_fail(dict_list, NULL);
 
-       menu = gtk_menu_new();
+       store = gtk_list_store_new(SET_GTKASPELL_SIZE,
+                                  G_TYPE_STRING,
+                                  G_TYPE_STRING,
+                                  -1);
        
        for (tmp = dict_list; tmp != NULL; tmp = g_slist_next(tmp)) {
                dict = (Dictionary *) tmp->data;
-               item = gtk_menu_item_new_with_label(dict->dictname);
-               g_object_set_data(G_OBJECT(item), "dict_name",
-                                 dict->fullname); 
-                                        
-               gtk_menu_append(GTK_MENU(menu), item);                                   
-               gtk_widget_show(item);
+               
+               gtk_list_store_append(store, &iter);
+               gtk_list_store_set(store, &iter,
+                                  SET_GTKASPELL_NAME, dict->dictname,
+                                  SET_GTKASPELL_FULLNAME, dict->fullname,
+                                  -1);
        }
 
-       gtk_widget_show(menu);
-
-       return menu;
+       return GTK_TREE_MODEL(store);
 }
 
-gchar *gtkaspell_get_dictionary_menu_active_item(GtkWidget *menu)
+GtkTreeModel *gtkaspell_dictionary_store_new(void)
 {
-       GtkWidget *menuitem;
-       gchar *dict_fullname;
-       gchar *label;
-
-       g_return_val_if_fail(GTK_IS_MENU(menu), NULL);
-
-       menuitem = gtk_menu_get_active(GTK_MENU(menu));
-        dict_fullname = (gchar *) g_object_get_data(G_OBJECT(menuitem), 
-                                                   "dict_name");
-        g_return_val_if_fail(dict_fullname, NULL);
-
-       label = g_strdup(dict_fullname);
-
-        return label;
-  
+       return gtkaspell_dictionary_store_new_with_refresh
+               (TRUE);
 }
 
-gint gtkaspell_set_dictionary_menu_active_item(GtkWidget *menu,
-                                              const gchar *dictionary)
+GtkWidget *gtkaspell_dictionary_combo_new(const gboolean refresh)
 {
-       GList *cur;
-       gint n;
-
-       g_return_val_if_fail(menu != NULL, 0);
-       g_return_val_if_fail(dictionary != NULL, 0);
-       g_return_val_if_fail(GTK_IS_OPTION_MENU(menu), 0);
-
-       n = 0;
-       for (cur = GTK_MENU_SHELL(gtk_option_menu_get_menu(
-                                       GTK_OPTION_MENU(menu)))->children;
-            cur != NULL; cur = cur->next) {
-               GtkWidget *menuitem;
-               gchar *dict_name;
-
-               menuitem = GTK_WIDGET(cur->data);
-               dict_name = g_object_get_data(G_OBJECT(menuitem), 
-                                             "dict_name");
-               if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
-                       gtk_option_menu_set_history(GTK_OPTION_MENU(menu), n);
-
-                       return 1;
-               }
-               n++;
-       }
+       GtkWidget *combo;
+       GtkCellRenderer *renderer;
 
-       return 0;
-}
+       combo = gtk_combo_box_new_with_model(
+                       gtkaspell_dictionary_store_new_with_refresh(refresh));
+       gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);   
+       gtk_widget_show(combo);
+       
+       renderer = gtk_cell_renderer_text_new();
+       gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(combo), renderer, TRUE);
+       gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(combo),renderer,
+                                       "text", SET_GTKASPELL_NAME, NULL);
+       
+       return combo;
+} 
 
-GtkWidget *gtkaspell_sugmode_option_menu_new(gint sugmode)
+gchar *gtkaspell_get_dictionary_menu_active_item(GtkComboBox *combo)
 {
-       GtkWidget *menu;
-       GtkWidget *item;
-
-       menu = gtk_menu_new();
-       gtk_widget_show(menu);
-
-       item = gtk_menu_item_new_with_label(_("Fast Mode"));
-        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
-       g_object_set_data(G_OBJECT(item), "sugmode",
-                         GINT_TO_POINTER(ASPELL_FASTMODE));
-
-       item = gtk_menu_item_new_with_label(_("Normal Mode"));
-        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
-       g_object_set_data(G_OBJECT(item), "sugmode",
-                         GINT_TO_POINTER(ASPELL_NORMALMODE));
+       GtkTreeIter iter;
+       GtkTreeModel *model;
+       gchar *dict_fullname = NULL;
        
-       item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
-        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
-       g_object_set_data(G_OBJECT(item), "sugmode",
-                         GINT_TO_POINTER(ASPELL_BADSPELLERMODE));
-
-       return menu;
-}
+       cm_return_val_if_fail(GTK_IS_COMBO_BOX(combo), NULL);
+       cm_return_val_if_fail(gtk_combo_box_get_active_iter(combo, &iter), NULL);
        
-void gtkaspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
-{
-       g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
-
-       g_return_if_fail(sugmode == ASPELL_FASTMODE ||
-                        sugmode == ASPELL_NORMALMODE ||
-                        sugmode == ASPELL_BADSPELLERMODE);
+       model = gtk_combo_box_get_model(combo);
+       if(model == NULL)
+               return NULL;
+       
+       gtk_tree_model_get(model, &iter,
+                          SET_GTKASPELL_FULLNAME, &dict_fullname,
+                          -1);
 
-       gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
+        return dict_fullname;
 }
 
-gint gtkaspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
+gint gtkaspell_set_dictionary_menu_active_item(GtkComboBox *combo,
+                                              const gchar *dictionary)
 {
-       gint sugmode;
-       GtkWidget *item;
+       GtkTreeModel *model;
+       GtkTreeIter iter;
+       gchar *dict_name = NULL;
        
-       g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
-
-       item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
+       cm_return_val_if_fail(combo != NULL, 0);
+       cm_return_val_if_fail(dictionary != NULL, 0);
+       cm_return_val_if_fail(GTK_IS_COMBO_BOX(combo), 0);
+
+       if((model = gtk_combo_box_get_model(combo)) == NULL)
+               return 0;
+       if((gtk_tree_model_get_iter_first(model, &iter)) == FALSE)
+               return 0;
        
-       sugmode = GPOINTER_TO_INT(g_object_get_data(G_OBJECT(item),
-                                                   "sugmode"));
-
-       return sugmode;
+       do {
+               gtk_tree_model_get(model, &iter,
+                                  SET_GTKASPELL_FULLNAME, &dict_name,
+                                  -1);
+               
+               if ((dict_name != NULL) && !strcmp2(dict_name, dictionary)) {
+                       gtk_combo_box_set_active_iter(combo, &iter);
+                       g_free(dict_name);
+                       return 1;
+               }
+               
+               g_free(dict_name);
+               
+       } while ((gtk_tree_model_iter_next(model, &iter)) == TRUE);
+       
+       return 0;
 }
 
-static void use_alternate_dict(GtkAspell *gtkaspell)
+void gtkaspell_use_alternate_dict(GtkAspell *gtkaspell)
 {
        GtkAspeller *tmp;
 
        tmp = gtkaspell->gtkaspeller;
        gtkaspell->gtkaspeller = gtkaspell->alternate_speller;
        gtkaspell->alternate_speller = tmp;
-
-       if (gtkaspell->config_menu)
-               populate_submenu(gtkaspell, gtkaspell->config_menu);
 }
 
 static void destroy_menu(GtkWidget *widget,
                             gpointer user_data) {
-
        GtkAspell *gtkaspell = (GtkAspell *)user_data;
 
        if (gtkaspell->accel_group) {
@@ -1844,43 +1709,6 @@ static void destroy_menu(GtkWidget *widget,
        }
 }
 
-static void popup_menu(GtkAspell *gtkaspell, GdkEventButton *eb) 
-{
-       GtkTextView * gtktext;
-       GtkMenu *menu = NULL;
-       
-       gtktext = gtkaspell->gtktext;
-
-       gtkaspell->orig_pos = get_textview_buffer_offset(gtktext);
-
-       if (!(eb->state & GDK_SHIFT_MASK)) {
-               if (check_at(gtkaspell, gtkaspell->orig_pos)) {
-
-                       set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
-
-                       if (misspelled_suggest(gtkaspell, gtkaspell->theword)) {
-                               menu = make_sug_menu(gtkaspell);
-                               gtk_menu_popup(menu,
-                                              NULL, NULL, NULL, NULL,
-                                              eb->button, eb->time);
-
-                               g_signal_connect(G_OBJECT(menu), "deactivate",
-                                                        G_CALLBACK(destroy_menu), 
-                                                        gtkaspell);
-                               return;
-                       }
-               } else
-                       set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
-       }
-       menu = make_config_menu(gtkaspell);
-       gtk_menu_popup(menu, NULL, NULL, NULL, NULL,
-                      eb->button, eb->time);
-
-       g_signal_connect(G_OBJECT(menu), "deactivate",
-                                G_CALLBACK(destroy_menu), 
-                                gtkaspell);
-}
-
 static gboolean aspell_key_pressed(GtkWidget *widget,
                                   GdkEventKey *event,
                                   GtkAspell *gtkaspell)
@@ -1898,22 +1726,21 @@ static gboolean aspell_key_pressed(GtkWidget *widget,
 /* make_sug_menu() - Add menus to accept this word for this session 
  * and to add it to personal dictionary 
  */
-static GtkMenu *make_sug_menu(GtkAspell *gtkaspell) 
+static GSList *make_sug_menu(GtkAspell *gtkaspell) 
 {
-       GtkWidget       *menu, *item;
-       unsigned char   *caption;
+       GtkWidget       *item;
+       char    *caption;
        GtkTextView     *gtktext;
        GtkAccelGroup   *accel;
        GList           *l = gtkaspell->suggestions_list;
        gchar           *utf8buf;
-
+       GSList *list = NULL;
        gtktext = gtkaspell->gtktext;
 
-       accel = gtk_accel_group_new();
-       menu = gtk_menu_new(); 
+       if (l == NULL)
+               return NULL;
 
-       if (gtkaspell->sug_menu)
-               gtk_widget_destroy(gtkaspell->sug_menu);
+       accel = gtk_accel_group_new();
 
        if (gtkaspell->accel_group) {
                gtk_window_remove_accel_group(GTK_WINDOW(gtkaspell->parent_window), 
@@ -1921,31 +1748,24 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
                gtkaspell->accel_group = NULL;
        }
 
-       gtkaspell->sug_menu = menu;     
-
-       g_signal_connect(G_OBJECT(menu), "cancel",
-                        G_CALLBACK(cancel_menu_cb), gtkaspell);
-
-       utf8buf  = conv_codeset_strdup((unsigned char*)l->data,
-                               conv_get_locale_charset_str(),
-                               CS_UTF_8);
+       utf8buf  = g_strdup(l->data);
        caption = g_strdup_printf(_("\"%s\" unknown in %s"), 
                                  utf8buf, 
                                  gtkaspell->gtkaspeller->dictionary->dictname);
        item = gtk_menu_item_new_with_label(caption);
        g_free(utf8buf);
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
-       gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
+       list = g_slist_append(list, item);
+       gtk_misc_set_alignment(GTK_MISC(gtk_bin_get_child(GTK_BIN((item)))), 0.5, 0.5);
        g_free(caption);
 
        item = gtk_menu_item_new();
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
 
        item = gtk_menu_item_new_with_label(_("Accept in this session"));
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
         g_signal_connect(G_OBJECT(item), "activate",
                         G_CALLBACK(add_word_to_session_cb), 
                         gtkaspell);
@@ -1955,7 +1775,7 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
 
        item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
         g_signal_connect(G_OBJECT(item), "activate",
                         G_CALLBACK(add_word_to_personal_cb), 
                         gtkaspell);
@@ -1965,7 +1785,7 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
 
         item = gtk_menu_item_new_with_label(_("Replace with..."));
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
         g_signal_connect(G_OBJECT(item), "activate",
                         G_CALLBACK(replace_with_create_dialog_cb), 
                         gtkaspell);
@@ -1981,7 +1801,7 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
                item = gtk_menu_item_new_with_label(caption);
                g_free(caption);
                gtk_widget_show(item);
-               gtk_menu_append(GTK_MENU(menu), item);
+               list = g_slist_append(list, item);
                g_signal_connect(G_OBJECT(item), "activate",
                                 G_CALLBACK(check_with_alternate_cb),
                                 gtkaspell);
@@ -1994,15 +1814,15 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
 
        item = gtk_menu_item_new();
         gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu), item);
+        list = g_slist_append(list, item);
 
        l = l->next;
         if (l == NULL) {
                item = gtk_menu_item_new_with_label(_("(no suggestions)"));
                gtk_widget_show(item);
-               gtk_menu_append(GTK_MENU(menu), item);
+               list = g_slist_append(list, item);
         } else {
-               GtkWidget *curmenu = menu;
+               GtkWidget *curmenu = NULL;
                gint count = 0;
                
                do {
@@ -2011,25 +1831,31 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
 
                                item = gtk_menu_item_new_with_label(_("More..."));
                                gtk_widget_show(item);
-                               gtk_menu_append(GTK_MENU(curmenu), item);
+                               if (curmenu)
+                                       gtk_menu_shell_append(GTK_MENU_SHELL(curmenu), item);
+                               else 
+                                       list = g_slist_append(list, item);
 
                                curmenu = gtk_menu_new();
                                gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),
                                                          curmenu);
                        }
 
-                       utf8buf  = conv_codeset_strdup((unsigned char*)l->data,
-                                                       conv_get_locale_charset_str(),
-                                                       CS_UTF_8);
+                       utf8buf  = g_strdup(l->data);
+
                        item = gtk_menu_item_new_with_label(utf8buf);
                        g_free(utf8buf);
                        gtk_widget_show(item);
-                       gtk_menu_append(GTK_MENU(curmenu), item);
+                       if (curmenu == NULL) {
+                               list = g_slist_append(list, item);
+                       } else {
+                               gtk_menu_shell_append(GTK_MENU_SHELL(curmenu), item);
+                       }
                        g_signal_connect(G_OBJECT(item), "activate",
                                         G_CALLBACK(replace_word_cb),
                                         gtkaspell);
 
-                       if (curmenu == menu && count < MENUCOUNT) {
+                       if (curmenu == NULL && count < MENUCOUNT) {
                                gtk_widget_add_accelerator(item, "activate",
                                                           accel,
                                                           GDK_A + count, 0,
@@ -2052,39 +1878,31 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
                 accel);
        gtkaspell->accel_group = accel;
 
-       g_signal_connect(G_OBJECT(menu),
-                       "key_press_event",
-                       G_CALLBACK(aspell_key_pressed), gtkaspell);
-       
-       return GTK_MENU(menu);
+       return list;
 }
 
-static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
+static GSList *populate_submenu(GtkAspell *gtkaspell)
 {
        GtkWidget *item, *submenu;
        gchar *dictname;
-       GtkAspeller *gtkaspeller = gtkaspell->gtkaspeller;
-
-       if (GTK_MENU_SHELL(menu)->children) {
-               GList *amenu, *alist;
-               for (amenu = (GTK_MENU_SHELL(menu)->children); amenu; ) {
-                       alist = amenu->next;
-                       gtk_widget_destroy(GTK_WIDGET(amenu->data));
-                       amenu = alist;
-               }
-       }
-       
+       GtkAspeller *gtkaspeller = NULL;
+       GSList *list = NULL;
+
+       if (!gtkaspell)
+               return NULL;
+
+       gtkaspeller = gtkaspell->gtkaspeller;
        dictname = g_strdup_printf(_("Dictionary: %s"),
                                   gtkaspeller->dictionary->dictname);
        item = gtk_menu_item_new_with_label(dictname);
-       gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
+       gtk_misc_set_alignment(GTK_MISC(gtk_bin_get_child(GTK_BIN((item)))), 0.5, 0.5);
        g_free(dictname);
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
 
        item = gtk_menu_item_new();
         gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu), item);
+        list = g_slist_append(list, item);
                
        if (gtkaspell->use_alternate && gtkaspell->alternate_speller) {
                dictname = g_strdup_printf(_("Use alternate (%s)"), 
@@ -2095,45 +1913,22 @@ static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
                                 G_CALLBACK(switch_to_alternate_cb),
                                 gtkaspell);
                gtk_widget_show(item);
-               gtk_menu_append(GTK_MENU(menu), item);
+               list = g_slist_append(list, item);
        }
 
-       item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
-       if (gtkaspell->gtkaspeller->sug_mode == ASPELL_FASTMODE) {
-               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
-               gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
-       } else
-               g_signal_connect(G_OBJECT(item), "activate",
-                                G_CALLBACK(set_sug_mode_cb),
-                                gtkaspell);
-       gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
-
-       item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
-       if (gtkaspell->gtkaspeller->sug_mode == ASPELL_NORMALMODE) {
-               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
+       item = gtk_check_menu_item_new_with_label(_("Use both dictionaries"));
+       if (gtkaspell->use_both_dicts) {
                gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
-       } else
-               g_signal_connect(G_OBJECT(item), "activate",
-                                G_CALLBACK(set_sug_mode_cb),
-                                gtkaspell);
-       gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu),item);
-
-       item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
-       if (gtkaspell->gtkaspeller->sug_mode == ASPELL_BADSPELLERMODE) {
-               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
-               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
-       } else
-               g_signal_connect(G_OBJECT(item), "activate",
-                                G_CALLBACK(set_sug_mode_cb),
-                                gtkaspell);
+       } 
+       g_signal_connect(G_OBJECT(item), "activate",
+                        G_CALLBACK(set_use_both_cb),
+                        gtkaspell);
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
        
        item = gtk_menu_item_new();
         gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu), item);
+        list = g_slist_append(list, item);
        
        item = gtk_check_menu_item_new_with_label(_("Check while typing"));
        if (gtkaspell->check_while_typing)
@@ -2144,21 +1939,21 @@ static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
                         G_CALLBACK(toggle_check_while_typing_cb),
                         gtkaspell);
        gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       list = g_slist_append(list, item);
 
        item = gtk_menu_item_new();
         gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu), item);
+        list = g_slist_append(list, item);
 
        submenu = gtk_menu_new();
         item = gtk_menu_item_new_with_label(_("Change dictionary"));
         gtk_menu_item_set_submenu(GTK_MENU_ITEM(item),submenu);
         gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu), item);
+        list = g_slist_append(list, item);
 
        /* Dict list */
         if (gtkaspellcheckers->dictionary_list == NULL)
-               gtkaspell_get_dictionary_list(gtkaspell->dictionary_path, FALSE);
+               gtkaspell_get_dictionary_list(FALSE);
         {
                GtkWidget * curmenu = submenu;
                int count = 0;
@@ -2176,7 +1971,7 @@ static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
                                gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), 
                                                          newmenu);
                                
-                               gtk_menu_append(GTK_MENU(curmenu), item);
+                               gtk_menu_shell_append(GTK_MENU_SHELL(curmenu), item);
                                gtk_widget_show(item);
                                curmenu = newmenu;
                                count = 0;
@@ -2197,37 +1992,17 @@ static void populate_submenu(GtkAspell *gtkaspell, GtkWidget *menu)
                                         G_CALLBACK(change_dict_cb),
                                         gtkaspell);
                        gtk_widget_show(item);
-                       gtk_menu_append(GTK_MENU(curmenu), item);
+                       gtk_menu_shell_append(GTK_MENU_SHELL(curmenu), item);
                        
                        count++;
                }
         }  
+       return list;
 }
 
-static GtkMenu *make_config_menu(GtkAspell *gtkaspell)
-{
-       if (!gtkaspell->popup_config_menu)
-               gtkaspell->popup_config_menu = gtk_menu_new();
-
-       debug_print("Aspell: creating/using popup_config_menu %0x\n", 
-                       (guint) gtkaspell->popup_config_menu);
-       populate_submenu(gtkaspell, gtkaspell->popup_config_menu);
-
-        return GTK_MENU(gtkaspell->popup_config_menu);
-}
-
-void gtkaspell_populate_submenu(GtkAspell *gtkaspell, GtkWidget *menuitem)
+GSList *gtkaspell_make_config_menu(GtkAspell *gtkaspell)
 {
-       GtkWidget *menu;
-
-       menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
-       
-       debug_print("Aspell: using config menu %0x\n", 
-                       (guint) gtkaspell->popup_config_menu);
-       populate_submenu(gtkaspell, menu);
-       
-       gtkaspell->config_menu = menu;
-       
+       return populate_submenu(gtkaspell);
 }
 
 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y, 
@@ -2261,84 +2036,126 @@ static void set_menu_pos(GtkMenu *menu, gint *x, gint *y,
        wx =  r.width;
        wy =  r.height;
 
-       *x = rect.x + xx +
-            gdk_char_width(gtk_style_get_font(GTK_WIDGET(text)->style), ' ');
+       *x = rect.x + xx + 8;
 
        *y = rect.y + rect.height + yy;
 
        if (*x + wx > sx)
                *x = sx - wx;
        if (*y + wy > sy)
-               *y = *y - wy -
-                    gdk_string_height(gtk_style_get_font(
-                                               GTK_WIDGET(text)->style),
-                                      gtkaspell->theword);
+               *y = *y - wy - 10;
 }
 
-/* Menu call backs */
-
-static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
-{
-       GtkAspell *gtkaspell = (GtkAspell *) data;
-
-       gtkaspell->continue_check = NULL;
-       set_point_continue(gtkaspell);
-
-       return FALSE;
-}
-
-gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
+/* change the current dictionary of gtkaspell
+   - if always_set_alt_dict is set, the alternate dict is unconditionally set to the former
+     current dictionary (common use: from menu callbacks)
+   - if always_set_alt_dict is NOT set, the alternate dict will be set to the former
+     current dictionary only if there is no alternate dictionary already set
+     (this is when we need to set the current dictionary then the alternate one
+     when creating a compose window, from the account and folder settings)
+*/
+gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary,
+                                                        gboolean always_set_alt_dict)
 {
        Dictionary      *dict;       
        GtkAspeller     *gtkaspeller;
-       gint             sug_mode;
 
-       g_return_val_if_fail(gtkaspell, FALSE);
-       g_return_val_if_fail(dictionary, FALSE);
+       cm_return_val_if_fail(gtkaspell, FALSE);
+       cm_return_val_if_fail(dictionary, FALSE);
   
-       sug_mode  = gtkaspell->default_sug_mode;
-
        dict = g_new0(Dictionary, 1);
-       dict->fullname = g_strdup(dictionary);
-       dict->encoding = g_strdup(gtkaspell->gtkaspeller->dictionary->encoding);
+       
+       if (strrchr(dictionary, '/')) {
+               dict->fullname = g_strdup(strrchr(dictionary, '/')+1);
+               dict->dictname = g_strdup(strrchr(dictionary, '/')+1);
+       } else {
+               dict->fullname = g_strdup(dictionary);
+               dict->dictname = g_strdup(dictionary);
+       }
+
+       if (dict->fullname && strchr(dict->fullname, '-')) {
+               *(strchr(dict->fullname, '-')) = '\0';
+               *(strchr(dict->dictname, '-')) = '\0';
+       }
 
-       if (gtkaspell->use_alternate && gtkaspell->alternate_speller &&
-           dict == gtkaspell->alternate_speller->dictionary) {
-               use_alternate_dict(gtkaspell);
+       if (!dict->fullname || !(*dict->fullname)) {
                dictionary_delete(dict);
-               gtkaspell->alternate_speller->dictionary = NULL;
-               return TRUE;
+               return FALSE;
        }
-       
        gtkaspeller = gtkaspeller_new(dict);
 
        if (!gtkaspeller) {
-               gchar *message;
-               message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"), 
+               alertpanel_warning(_("The spell checker could not change dictionary.\n%s"), 
                                          gtkaspellcheckers->error_message);
-
-               alertpanel_warning(message); 
-               g_free(message);
        } else {
                if (gtkaspell->use_alternate) {
-                       if (gtkaspell->alternate_speller)
-                               gtkaspeller_delete(gtkaspell->alternate_speller);
-                       gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
+                       if (gtkaspell->alternate_speller) {
+                               if (always_set_alt_dict) {
+                                       gtkaspeller_delete(gtkaspell->alternate_speller);
+                                       gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
+                               } else
+                                       gtkaspeller_delete(gtkaspell->gtkaspeller);
+                       } else
+                               /* should never be reached as the dicts are always set
+                                  to a default value */
+                               gtkaspell->alternate_speller = gtkaspell->gtkaspeller;
                } else
                        gtkaspeller_delete(gtkaspell->gtkaspeller);
 
                gtkaspell->gtkaspeller = gtkaspeller;
-               gtkaspell_set_sug_mode(gtkaspell, sug_mode);
        }
        
        dictionary_delete(dict);
 
-       if (gtkaspell->config_menu)
-               populate_submenu(gtkaspell, gtkaspell->config_menu);
+       return TRUE;    
+}
+
+/* change the alternate dictionary of gtkaspell (doesn't affect the default dictionary) */
+gboolean gtkaspell_change_alt_dict(GtkAspell *gtkaspell, const gchar *alt_dictionary)
+{
+       Dictionary      *dict;       
+       GtkAspeller     *gtkaspeller;
+
+       cm_return_val_if_fail(gtkaspell, FALSE);
+       cm_return_val_if_fail(alt_dictionary, FALSE);
+  
+       dict = g_new0(Dictionary, 1);
+       if (strrchr(alt_dictionary, '/')) {
+               dict->fullname = g_strdup(strrchr(alt_dictionary, '/')+1);
+               dict->dictname = g_strdup(strrchr(alt_dictionary, '/')+1);
+       } else {
+               dict->fullname = g_strdup(alt_dictionary);
+               dict->dictname = g_strdup(alt_dictionary);
+       }
+
+       if (dict->fullname && strchr(dict->fullname, '-')) {
+               *(strchr(dict->fullname, '-')) = '\0';
+               *(strchr(dict->dictname, '-')) = '\0';
+       }
+
+       if (!dict->fullname || !(*dict->fullname)) {
+               dictionary_delete(dict);
+               return FALSE;
+       }
+
+       gtkaspeller = gtkaspeller_new(dict);
+
+       if (!gtkaspeller) {
+               alertpanel_warning(_("The spell checker could not change the alternate dictionary.\n%s"), 
+                                         gtkaspellcheckers->error_message);
+       } else {
+               if (gtkaspell->alternate_speller)
+                       gtkaspeller_delete(gtkaspell->alternate_speller);
+               gtkaspell->alternate_speller = gtkaspeller;
+       }
+       
+       dictionary_delete(dict);
 
        return TRUE;    
 }
 
+/* Menu call backs */
+
 /* change_dict_cb() - Menu callback : change dict */
 static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
 {
@@ -2349,14 +2166,22 @@ static void change_dict_cb(GtkWidget *w, GtkAspell *gtkaspell)
        if (!strcmp2(fullname, _("None")))
                return;
 
-       gtkaspell_change_dict(gtkaspell, fullname);
+       gtkaspell_change_dict(gtkaspell, fullname, TRUE);
+       gtkaspell_dict_changed(gtkaspell);
+
+       if (gtkaspell->menu_changed_cb)
+               gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
 }
 
 static void switch_to_alternate_cb(GtkWidget *w,
                                   gpointer data)
 {
        GtkAspell *gtkaspell = (GtkAspell *) data;
-       use_alternate_dict(gtkaspell);
+       gtkaspell_use_alternate_dict(gtkaspell);
+       gtkaspell_dict_changed(gtkaspell);
+       
+       if (gtkaspell->menu_changed_cb)
+               gtkaspell->menu_changed_cb(gtkaspell->menu_changed_data);
 }
 
 /* Misc. helper functions */
@@ -2367,10 +2192,10 @@ static void set_point_continue(GtkAspell *gtkaspell)
 
        gtktext = gtkaspell->gtktext;
 
-       set_textview_buffer_offset(gtktext, gtkaspell->orig_pos);
+       gtkaspell->ctx.set_position(gtkaspell->ctx.data, gtkaspell->orig_pos);
 
        if (gtkaspell->continue_check)
-               gtkaspell->continue_check((gpointer *) gtkaspell);
+               gtkaspell->continue_check((gpointer *) gtkaspell->ctx.data);
 }
 
 static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
@@ -2387,8 +2212,13 @@ static void allocate_color(GtkAspell *gtkaspell, gint rgbvalue)
        color->blue  = (int) (((gdouble) (rgbvalue & 0x0000ff)        / 255.0)
                        * 65535.0);
 
-       gtk_text_buffer_create_tag(buffer, "misspelled",
+       if (rgbvalue != 0)
+               gtk_text_buffer_create_tag(buffer, "misspelled",
                                   "foreground-gdk", color, NULL);
+       else
+               gtk_text_buffer_create_tag(buffer, "misspelled",
+                                  "underline", PANGO_UNDERLINE_ERROR, NULL);
+
 }
 
 static void change_color(GtkAspell * gtkaspell, 
@@ -2400,7 +2230,8 @@ static void change_color(GtkAspell * gtkaspell,
        GtkTextBuffer *buffer;
        GtkTextIter startiter, enditer;
 
-       g_return_if_fail(start < end);
+       if (start > end)
+               return;
     
        gtktext = gtkaspell->gtktext;
     
@@ -2410,30 +2241,11 @@ static void change_color(GtkAspell * gtkaspell,
        if (color)
                gtk_text_buffer_apply_tag_by_name(buffer, "misspelled",
                                                  &startiter, &enditer);
-       else
+       else {
+               gtk_text_iter_forward_char(&enditer);
                gtk_text_buffer_remove_tag_by_name(buffer, "misspelled",
                                                   &startiter, &enditer);
-}
-
-/* convert_to_aspell_encoding () - converts ISO-8859-* strings to iso8859-* 
- * as needed by aspell. Returns an allocated string.
- */
-
-static guchar *convert_to_aspell_encoding (const guchar *encoding)
-{
-       guchar * aspell_encoding;
-
-       if (strstr2(encoding, "ISO-8859-")) {
-               aspell_encoding = g_strdup_printf("iso8859%s", encoding+8);
        }
-       else {
-               if (!strcmp2(encoding, "US-ASCII"))
-                       aspell_encoding = g_strdup("iso8859-1");
-               else
-                       aspell_encoding = g_strdup(encoding);
-       }
-
-       return aspell_encoding;
 }
 
 /* compare_dict () - compare 2 dict names */
@@ -2463,7 +2275,7 @@ static gint compare_dict(Dictionary *a, Dictionary *b)
 static void dictionary_delete(Dictionary *dict)
 {
        g_free(dict->fullname);
-       g_free(dict->encoding);
+       g_free(dict->dictname);
        g_free(dict);
 }
 
@@ -2474,13 +2286,12 @@ static Dictionary *dictionary_dup(const Dictionary *dict)
        dict2 = g_new(Dictionary, 1); 
 
        dict2->fullname = g_strdup(dict->fullname);
-       dict2->dictname = dict->dictname - dict->fullname + dict2->fullname;
-       dict2->encoding = g_strdup(dict->encoding);
+       dict2->dictname = g_strdup(dict->dictname);
 
        return dict2;
 }
 
-static void free_suggestions_list(GtkAspell *gtkaspell)
+void gtkaspell_free_suggestions_list(GtkAspell *gtkaspell)
 {
        GList *list;
 
@@ -2501,14 +2312,14 @@ static void reset_theword_data(GtkAspell *gtkaspell)
        gtkaspell->theword[0]    =  0;
        gtkaspell->max_sug       = -1;
 
-       free_suggestions_list(gtkaspell);
+       gtkaspell_free_suggestions_list(gtkaspell);
 }
 
 static void free_checkers(gpointer elt, gpointer data)
 {
        GtkAspeller *gtkaspeller = elt;
 
-       g_return_if_fail(gtkaspeller);
+       cm_return_if_fail(gtkaspeller);
 
        gtkaspeller_real_delete(gtkaspeller);
 }
@@ -2518,11 +2329,18 @@ static gint find_gtkaspeller(gconstpointer aa, gconstpointer bb)
        Dictionary *a = ((GtkAspeller *) aa)->dictionary;
        Dictionary *b = ((GtkAspeller *) bb)->dictionary;
 
-       if (a && b && a->fullname && b->fullname  &&
-           strcmp(a->fullname, b->fullname) == 0 &&
-           a->encoding && b->encoding)
-               return strcmp(a->encoding, b->encoding);
+       if (a && b && a->fullname && b->fullname)
+               return strcmp(a->fullname, b->fullname);
 
        return 1;
 }
+
+gchar *gtkaspell_get_default_dictionary(GtkAspell *gtkaspell)
+{
+       if (gtkaspell && gtkaspell->gtkaspeller &&
+                       gtkaspell->gtkaspeller->dictionary)
+               return gtkaspell->gtkaspeller->dictionary->dictname;
+       else
+               return NULL;
+}
 #endif