Various bug fixes and enhancements, see ChangeLog.claws 2002-02-03
[claws.git] / src / gtkspell.c
index 611e48f5df9cdfb8b7135a07a80744e431207822..09e3bff83b0534c81e53769746b8e34c91dd39a7 100644 (file)
@@ -108,7 +108,7 @@ static GtkPspeller *        gtkpspeller_real_delete         (GtkPspeller *gtkpspeller);
 /* Checker configuration */
 static gint            set_dictionary                  (PspellConfig *config, 
                                                         Dictionary *dict);
-static void            set_sug_mode_cb                 (GtkWidget *w, 
+static void            set_sug_mode_cb                 (GtkMenuItem *w, 
                                                         GtkPspell *gtkpspell);
 static void            set_real_sug_mode               (GtkPspell *gtkpspell, 
                                                         const char *themode);
@@ -116,49 +116,60 @@ static void               set_real_sug_mode               (GtkPspell *gtkpspell,
 /* Checker actions */
 static gboolean        check_at                        (GtkPspell *gtkpspell, 
                                                         int from_pos);
-static void            check_next_prev                 (GtkPspell *gtkpspell, 
+static gboolean                check_next_prev                 (GtkPspell *gtkpspell, 
                                                         gboolean forward);
-
 static GList*          misspelled_suggest              (GtkPspell *gtkpspell, 
                                                         guchar *word);
-static void            add_word_to_session             (GtkWidget *w, 
-                                                        GtkPspell *d);
-static void            add_word_to_personal            (GtkWidget *w, 
-                                                        GtkPspell *d);
-static void            replace_word                    (GtkWidget *w, 
-                                                        GtkPspell *gtkpspell); 
+static void            add_word_to_session_cb          (GtkWidget *w, 
+                                                        gpointer data);
+static void            add_word_to_personal_cb         (GtkWidget *w, 
+                                                        gpointer data);
+static void            replace_with_create_dialog_cb   (GtkWidget *w,
+                                                        gpointer data);
+static void            replace_with_supplied_word_cb   (GtkWidget *w, 
+                                                        GtkPspell *gtkpspell);
+static void            replace_word_cb                 (GtkWidget *w, 
+                                                        gpointer data); 
 static void            replace_real_word               (GtkPspell *gtkpspell, 
                                                         gchar *newword);
-static void            check_all_cb                    (GtkWidget *w, 
-                                                        GtkPspell *gtkpspell);
+static void            toggle_check_while_typing_cb    (GtkWidget *w, 
+                                                        gpointer data);
 
 /* Menu creation */
 static void            popup_menu                      (GtkPspell *gtkpspell, 
                                                         GdkEventButton *eb);
 static GtkMenu*                make_sug_menu                   (GtkPspell *gtkpspell);
+static void            populate_submenu                (GtkPspell *gtkpspell, 
+                                                        GtkWidget *menu);
 static GtkMenu*                make_config_menu                (GtkPspell *gtkpspell);
 static void            set_menu_pos                    (GtkMenu *menu, 
                                                         gint *x, 
                                                         gint *y, 
                                                         gpointer data);
 /* Other menu callbacks */
-static gboolean        deactivate_menu_cb              (GtkWidget *w, 
-                                                        gpointer *data);
+static gboolean        cancel_menu_cb                  (GtkMenuShell *w,
+                                                        gpointer data);
 static void            change_dict_cb                  (GtkWidget *w, 
                                                         GtkPspell *gtkpspell);
 
 /* Misc. helper functions */
+static void            set_point_continue              (GtkPspell *gtkpspell);
+static void            continue_check                  (gpointer *gtkpspell);
 static gboolean        iswordsep                       (unsigned char c);
 static guchar          get_text_index_whar             (GtkPspell *gtkpspell, 
                                                         int pos);
 static gboolean        get_word_from_pos               (GtkPspell *gtkpspell, 
-                                                        int pos, 
+                                                        gint pos, 
                                                         unsigned char* buf,
-                                                        int *pstart, int *pend);
-static void            allocate_color                  (GtkPspell *gtkpspell);
+                                                        gint buflen,
+                                                        gint *pstart, 
+                                                        gint *pend);
+static void            allocate_color                  (GtkPspell *gtkpspell,
+                                                        gint rgbvalue);
 static void            change_color                    (GtkPspell *gtkpspell, 
-                                                        int start, 
-                                                        int end, 
+                                                        gint start, 
+                                                        gint end, 
+                                                        gchar *newtext,
                                                         GdkColor *color);
 static guchar*         convert_to_pspell_encoding      (const guchar *encoding);
 static gint            compare_dict                    (Dictionary *a, 
@@ -232,7 +243,7 @@ static void gtkpspell_checkers_error_message (gchar *message)
        } else gtkpspellcheckers->error_message = message;
 }
 
-void gtkpspell_checkers_reset()
+void gtkpspell_checkers_reset_error()
 {
        g_return_if_fail(gtkpspellcheckers);
        
@@ -242,7 +253,9 @@ void gtkpspell_checkers_reset()
 }
 
 GtkPspell *gtkpspell_new(const gchar *dictionary, 
-                        const gchar *encoding, 
+                        const gchar *encoding,
+                        gint  misspelled_color,
+                        gboolean check_while_typing,
                         GtkSText *gtktext)
 {
        Dictionary      *dict = g_new0(Dictionary, 1);
@@ -253,7 +266,7 @@ GtkPspell *gtkpspell_new(const gchar *dictionary,
        
        dict->fullname = g_strdup(dictionary);
        dict->encoding = g_strdup(encoding);
-       gtkpspeller = gtkpspeller_new(dict); 
+       gtkpspeller    = gtkpspeller_new(dict); 
        dictionary_delete(dict);
 
        if (!gtkpspeller)
@@ -261,29 +274,35 @@ GtkPspell *gtkpspell_new(const gchar *dictionary,
        
        gtkpspell = g_new(GtkPspell, 1);
 
-       gtkpspell->gtkpspeller = gtkpspeller;
-       gtkpspell->theword[0]  = 0x00;
-       gtkpspell->start_pos   = 0;
-       gtkpspell->end_pos     = 0;
-       gtkpspell->orig_pos    = -1;
-
-       gtkpspell->gtktext     = gtktext;
-
+       gtkpspell->gtkpspeller        = gtkpspeller;
+       gtkpspell->theword[0]         = 0x00;
+       gtkpspell->start_pos          = 0;
+       gtkpspell->end_pos            = 0;
+       gtkpspell->orig_pos           = -1;
+       gtkpspell->end_check_pos      = -1;
+       gtkpspell->misspelled         = -1;
+       gtkpspell->check_while_typing = check_while_typing;
+       gtkpspell->continue_check     = NULL;
+       gtkpspell->config_menu        = NULL;
+       gtkpspell->popup_config_menu  = NULL;
+       gtkpspell->sug_menu           = NULL;
+       gtkpspell->replace_entry      = NULL;
+       gtkpspell->gtktext            = gtktext;
        gtkpspell->default_sug_mode   = PSPELL_FASTMODE;
-       gtkpspell->max_sug            = -1;
+       gtkpspell->max_sug            = -1;
        gtkpspell->suggestions_list   = NULL;
 
+       allocate_color(gtkpspell, misspelled_color);
+
        gtk_signal_connect_after(GTK_OBJECT(gtktext), "insert-text",
                                 GTK_SIGNAL_FUNC(entry_insert_cb), gtkpspell);
-
        gtk_signal_connect_after(GTK_OBJECT(gtktext), "delete-text",
                                 GTK_SIGNAL_FUNC(entry_delete_cb), gtkpspell);
-       
        gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
                           GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
        
-       allocate_color(gtkpspell);
-       
+       debug_print("Pspell: created gtkpspell %0x\n", gtkpspell);
+
        return gtkpspell;
 }
 
@@ -305,7 +324,19 @@ void gtkpspell_delete(GtkPspell * gtkpspell)
        
        gtkpspeller_delete(gtkpspell->gtkpspeller);
 
+       if (gtkpspell->sug_menu)
+               gtk_widget_destroy(gtkpspell->sug_menu);
+       if (gtkpspell->popup_config_menu)
+               gtk_widget_destroy(gtkpspell->popup_config_menu);
+       if (gtkpspell->config_menu)
+               gtk_widget_destroy(gtkpspell->config_menu);
+       if (gtkpspell->suggestions_list)
+               free_suggestions_list(gtkpspell);
+       
+       debug_print("Pspell: deleting gtkpspell %0x\n", gtkpspell);
+
        g_free(gtkpspell);
+
        gtkpspell = NULL;
 }
 
@@ -314,7 +345,10 @@ static void entry_insert_cb(GtkSText *gtktext, gchar *newtext,
                             GtkPspell * gtkpspell) 
 {
        g_return_if_fail(gtkpspell->gtkpspeller->checker);
-                       
+
+       if (!gtkpspell->check_while_typing)
+               return;
+       
        /* We must insert ourself the character to impose the */
        /* color of the inserted character to be default */
        /* Never mess with set_insertion when frozen */
@@ -350,6 +384,9 @@ static void entry_delete_cb(GtkSText *gtktext, gint start, gint end,
     
        g_return_if_fail(gtkpspell->gtkpspeller->checker);
 
+       if (!gtkpspell->check_while_typing)
+               return;
+
        origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
        if (start) {
                check_at(gtkpspell, start - 1);
@@ -408,12 +445,11 @@ static gint button_press_intercept_cb(GtkSText *gtktext, GdkEvent *e, GtkPspell
 /* Checker creation */
 static GtkPspeller *gtkpspeller_new(Dictionary *dictionary)
 {
-       GSList          *checkers, *exist;
+       GSList          *exist;
        GtkPspeller     *gtkpspeller = NULL;
-       GtkPspeller     *g           = g_new(GtkPspeller, 1);
+       GtkPspeller     *g           = g_new0(GtkPspeller, 1);
        Dictionary      *dict;
        gint            ispell;
-       gboolean        free_dict = TRUE;
                
        g_return_val_if_fail(gtkpspellcheckers, NULL);
 
@@ -441,37 +477,34 @@ static GtkPspeller *gtkpspeller_new(Dictionary *dictionary)
 
        g->dictionary = dict;
 
-       checkers = gtkpspellcheckers->checkers;
-
-       exist = g_slist_find_custom(checkers, g, find_gtkpspeller);
+       exist = g_slist_find_custom(gtkpspellcheckers->checkers, g, 
+                                   find_gtkpspeller);
        
        g_free(g);
 
-       if (exist)
+       if (exist && ispell) {
                gtkpspeller = (GtkPspeller *) exist->data;
+               dictionary_delete(dict);
 
-       if (exist && ispell)
                debug_print(_("Pspell: Using existing ispell checker %0x\n"),
                            (gint) gtkpspeller);
-       else {
+       else {
                if ((gtkpspeller = gtkpspeller_real_new(dict)) != NULL) {
-                       checkers = g_slist_append(checkers, gtkpspeller);
-                       
+                       gtkpspellcheckers->checkers = g_slist_append(
+                                               gtkpspellcheckers->checkers,
+                                               gtkpspeller);
+
                        debug_print(_("Pspell: Created a new gtkpspeller %0x\n"),
                                    (gint) gtkpspeller);
-                       
-                       free_dict = FALSE;
-               } else 
+               } else {
+                       dictionary_delete(dict);
+
                        debug_print(_("Pspell: Could not create spell checker.\n"));
+               }
        }
 
        debug_print(_("Pspell: number of existing checkers %d\n"), 
-                   g_slist_length(checkers));
-
-       if (free_dict)
-               dictionary_delete(dict);
-
-       gtkpspellcheckers->checkers = checkers;
+                   g_slist_length(gtkpspellcheckers->checkers));
 
        return gtkpspeller;
 }
@@ -515,20 +548,15 @@ static GtkPspeller *gtkpspeller_real_new(Dictionary *dict)
 
 static GtkPspeller *gtkpspeller_delete(GtkPspeller *gtkpspeller)
 {
-       GSList *checkers;
-
        g_return_val_if_fail(gtkpspellcheckers, NULL);
        
-       checkers = gtkpspellcheckers->checkers;
-       
        if (gtkpspeller->ispell) 
                debug_print(_("Pspell: Won't remove existing ispell checker %0x.\n"), 
                            (gint) gtkpspeller);
        else {
-               checkers = g_slist_remove(checkers, gtkpspeller);
+               gtkpspellcheckers->checkers = g_slist_remove(gtkpspellcheckers->checkers, 
+                                               gtkpspeller);
 
-               gtkpspellcheckers->checkers = checkers;
-               
                debug_print(_("Pspell: Deleting gtkpspeller %0x.\n"), 
                            (gint) gtkpspeller);
                
@@ -536,7 +564,7 @@ static GtkPspeller *gtkpspeller_delete(GtkPspeller *gtkpspeller)
        }
 
        debug_print(_("Pspell: number of existing checkers %d\n"), 
-                   g_slist_length(checkers));
+                   g_slist_length(gtkpspellcheckers->checkers));
 
        return gtkpspeller;
 }
@@ -549,11 +577,11 @@ static GtkPspeller *gtkpspeller_real_delete(GtkPspeller *gtkpspeller)
        pspell_manager_save_all_word_lists(gtkpspeller->checker);
 
        delete_pspell_manager(gtkpspeller->checker);
-       
+
        dictionary_delete(gtkpspeller->dictionary);
 
        g_free(gtkpspeller);
-       
+
        return NULL;
 }
 
@@ -578,10 +606,10 @@ static gboolean set_dictionary(PspellConfig *config, Dictionary *dict)
        buf[dict->dictname - dict->fullname] = 0x00;
 
        CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("rem-all-word-list-path", "");
-       debug_print(_("Pspell: removing all paths.\n"));
+       debug_print(_("Pspell: removed all paths.\n"));
 
        CONFIG_REPLACE_RETURN_FALSE_IF_FAIL("add-word-list-path", buf);
-       debug_print(_("Pspell: adding path (%s).\n"), buf);
+       debug_print(_("Pspell: added path %s.\n"), buf);
 
        strncpy(buf, dict->dictname, BUFSIZE-1);
        language = buf;
@@ -663,9 +691,9 @@ guchar *gtkpspell_get_path(GtkPspell *gtkpspell)
 }
 
 /* set_sug_mode_cb() - Menu callback : Set the suggestion mode */
-static void set_sug_mode_cb(GtkWidget *w, GtkPspell *gtkpspell)
+static void set_sug_mode_cb(GtkMenuItem *w, GtkPspell *gtkpspell)
 {
-       unsigned char *themode;
+       char *themode;
        
        if (gtkpspell->gtkpspeller->ispell) return;
 
@@ -673,26 +701,28 @@ static void set_sug_mode_cb(GtkWidget *w, GtkPspell *gtkpspell)
        
        set_real_sug_mode(gtkpspell, themode);
 
+       if (gtkpspell->config_menu)
+               populate_submenu(gtkpspell, gtkpspell->config_menu);
 }
 static void set_real_sug_mode(GtkPspell *gtkpspell, const char *themode)
 {
        gint result;
+       gint mode = PSPELL_FASTMODE;
        g_return_if_fail(gtkpspell);
        g_return_if_fail(gtkpspell->gtkpspeller);
        g_return_if_fail(themode);
 
-       if (!strcmp(themode, _("Fast Mode"))) {
-               result = gtkpspell_set_sug_mode(gtkpspell, PSPELL_FASTMODE);
-       } else if (!strcmp(themode,_("Normal Mode"))) {
-               result = gtkpspell_set_sug_mode(gtkpspell, PSPELL_NORMALMODE);
-       } else if (!strcmp( themode,_("Bad Spellers Mode"))) {
-               result = gtkpspell_set_sug_mode(gtkpspell, PSPELL_BADSPELLERMODE);
-       }
+       if (!strcmp(themode,_("Normal Mode")))
+               mode = PSPELL_NORMALMODE;
+       else if (!strcmp( themode,_("Bad Spellers Mode")))
+               mode = PSPELL_BADSPELLERMODE;
+
+       result = gtkpspell_set_sug_mode(gtkpspell, mode);
 
        if(!result) {
                debug_print(_("Pspell: error while changing suggestion mode:%s\n"),
                            gtkpspellcheckers->error_message);
-               gtkpspell_checkers_reset();
+               gtkpspell_checkers_reset_error();
        }
 }
   
@@ -700,10 +730,13 @@ static void set_real_sug_mode(GtkPspell *gtkpspell, const char *themode)
 gboolean gtkpspell_set_sug_mode(GtkPspell *gtkpspell, gint themode)
 {
        PspellConfig *config;
+
        g_return_val_if_fail(gtkpspell, FALSE);
        g_return_val_if_fail(gtkpspell->gtkpspeller, FALSE);
        g_return_val_if_fail(gtkpspell->gtkpspeller->config, FALSE);
 
+       debug_print("Pspell: setting sug mode of gtkpspeller %0x to %d\n",
+                       gtkpspell->gtkpspeller, themode);
        config = gtkpspell->gtkpspeller->config;
        switch (themode) {
                case PSPELL_FASTMODE: 
@@ -787,9 +820,9 @@ static guchar get_text_index_whar(GtkPspell *gtkpspell, int pos)
 
 /* get_word_from_pos () - return the word pointed to. */
 /* Handles correctly the quotes. */
-static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos, 
-                                  unsigned char* buf,
-                                  int *pstart, int *pend) 
+static gboolean get_word_from_pos(GtkPspell *gtkpspell, gint pos, 
+                                  unsigned char* buf, gint buflen,
+                                  gint *pstart, gint *pend) 
 {
 
        /* TODO : when correcting a word into quotes, change the color of */
@@ -850,106 +883,68 @@ static gboolean get_word_from_pos(GtkPspell *gtkpspell, int pos,
                                break;
        }
                                                
-       if (buf) {
-               for (pos = start; pos < end; pos++) 
-                       buf[pos - start] = get_text_index_whar(gtkpspell, pos);
-               buf[pos - start] = 0;
-       }
-
        if (pstart) 
                *pstart = start;
        if (pend) 
                *pend = end;
 
+       if (buf) {
+               if (end - start < buflen) {
+                       for (pos = start; pos < end; pos++) 
+                               buf[pos - start] = get_text_index_whar(gtkpspell, pos);
+                       buf[pos - start] = 0;
+               } else return FALSE;
+       }
+
        return TRUE;
 }
 
-static gboolean check_at(GtkPspell *gtkpspell, int from_pos) 
+static gboolean check_at(GtkPspell *gtkpspell, gint from_pos) 
 {
-       int           start, end;
-       unsigned char buf[BUFSIZE];
+       gint          start, end;
+       unsigned char buf[GTKPSPELLWORDSIZE];
        GtkSText     *gtktext;
 
        g_return_val_if_fail(from_pos >= 0, FALSE);
     
        gtktext = gtkpspell->gtktext;
 
-       if (!get_word_from_pos(gtkpspell, from_pos, buf, &start, &end))
+       if (!get_word_from_pos(gtkpspell, from_pos, buf, sizeof(buf), 
+                              &start, &end))
                return FALSE;
 
-
        if (misspelled_test(gtkpspell, buf)) {
                strncpy(gtkpspell->theword, buf, GTKPSPELLWORDSIZE - 1);
                gtkpspell->theword[GTKPSPELLWORDSIZE - 1] = 0;
                gtkpspell->start_pos  = start;
                gtkpspell->end_pos    = end;
-               gtkpspell->newword[0] = 0;
                free_suggestions_list(gtkpspell);
 
-               change_color(gtkpspell, start, end, &(gtkpspell->highlight));
-               
+               change_color(gtkpspell, start, end, buf, &(gtkpspell->highlight));
                return TRUE;
        } else {
-               change_color(gtkpspell, start, end, NULL);
+               change_color(gtkpspell, start, end, buf, NULL);
                return FALSE;
        }
 }
 
-static void check_all_cb(GtkWidget *w, GtkPspell *gtkpspell)
-{
-       gtkpspell_check_all(gtkpspell);
-}
-
-void gtkpspell_check_all(GtkPspell *gtkpspell) 
-{
-       guint     origpos;
-       guint     pos = 0;
-       guint     len;
-       float     adj_value;
-       GtkSText *gtktext;
-
-       g_return_if_fail(gtkpspell->gtkpspeller->checker);      
-
-       gtktext = gtkpspell->gtktext;
-
-       len = gtk_stext_get_length(gtktext);
-
-       adj_value = gtktext->vadj->value;
-       gtk_stext_freeze(gtktext);
-       origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
-       gtk_editable_set_position(GTK_EDITABLE(gtktext),0);
-       while (pos < len) {
-               while (pos < len && iswordsep(get_text_index_whar(gtkpspell, pos)))
-                       pos++;
-               while (pos < len && !iswordsep(get_text_index_whar(gtkpspell, pos)))
-                       pos++;
-               if (pos > 0)
-                       check_at(gtkpspell, pos - 1);
-       }
-       gtk_stext_thaw(gtktext);
-       gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
-       gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);
-}
-
-static void check_next_prev(GtkPspell *gtkpspell, gboolean forward)
+static gboolean check_next_prev(GtkPspell *gtkpspell, gboolean forward)
 {
        gint pos;
        gint minpos;
        gint maxpos;
        gint direc = -1;
        gboolean misspelled;
-       GdkEvent *event= (GdkEvent *) gtk_get_current_event();
        
        minpos = 0;
-       maxpos = gtk_stext_get_length(gtkpspell->gtktext);
+       maxpos = gtkpspell->end_check_pos;
 
        if (forward) {
                minpos = -1;
-               direc = -direc;
+               direc = 1;
                maxpos--;
        } 
 
-
        pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
        gtkpspell->orig_pos = pos;
        while (iswordsep(get_text_index_whar(gtkpspell, pos)) && pos > minpos && pos <= maxpos) 
@@ -971,41 +966,168 @@ static void check_next_prev(GtkPspell *gtkpspell, gboolean forward)
                gtk_editable_set_position(GTK_EDITABLE(gtkpspell->gtktext),
                                gtkpspell->end_pos);
                gtk_menu_popup(make_sug_menu(gtkpspell), NULL, NULL, 
-                               set_menu_pos, gtkpspell, 0, gdk_event_get_time(event));
+                               set_menu_pos, gtkpspell, 0, GDK_CURRENT_TIME);
        } else {
                reset_theword_data(gtkpspell);
 
+               gtkpspell_alert_dialog(_("No misspelled word found."));
                gtk_stext_set_point(GTK_STEXT(gtkpspell->gtktext), gtkpspell->orig_pos);
                gtk_editable_set_position(GTK_EDITABLE(gtkpspell->gtktext), gtkpspell->orig_pos);
        }
+       return misspelled;
 }
 
 void gtkpspell_check_backwards(GtkPspell *gtkpspell)
 {
+       gtkpspell->continue_check = NULL;
+       gtkpspell->end_check_pos = gtk_stext_get_length(GTK_STEXT(gtkpspell->gtktext));
        check_next_prev(gtkpspell, FALSE);
 }
 
 void gtkpspell_check_forwards_go(GtkPspell *gtkpspell)
 {
+
+       gtkpspell->continue_check = NULL;
+       gtkpspell->end_check_pos = gtk_stext_get_length(GTK_STEXT(gtkpspell->gtktext));
        check_next_prev(gtkpspell, TRUE);
 }
 
+void gtkpspell_check_all(GtkPspell *gtkpspell)
+{      
+       GtkWidget *gtktext;
+       
+       gint start, end;
+
+       g_return_if_fail(gtkpspell);
+       g_return_if_fail(gtkpspell->gtktext);
+
+       gtktext = (GtkWidget *) gtkpspell->gtktext;
+
+       start = 0;      
+       end   = gtk_stext_get_length(GTK_STEXT(gtktext));
+
+       if (GTK_EDITABLE(gtktext)->has_selection) {
+               start = GTK_EDITABLE(gtktext)->selection_start_pos;
+               end   = GTK_EDITABLE(gtktext)->selection_end_pos;
+       }
+
+       if (start > end) {
+               gint tmp;
+               tmp   = start;
+               start = end;
+               end   = tmp;
+       }
+               
+       
+       gtk_editable_set_position(GTK_EDITABLE(gtktext), start);
+       gtk_stext_set_point(GTK_STEXT(gtktext), start);
+
+       gtkpspell->continue_check = continue_check;
+       gtkpspell->end_check_pos = end;
+
+       gtkpspell->misspelled = check_next_prev(gtkpspell, TRUE);
+
+}      
+
+static void continue_check(gpointer *data)
+{
+       GtkPspell *gtkpspell = (GtkPspell *) data;
+       gint pos = gtk_editable_get_position(GTK_EDITABLE(gtkpspell->gtktext));
+       
+       if (pos < gtkpspell->end_check_pos && gtkpspell->misspelled)
+               gtkpspell->misspelled = check_next_prev(gtkpspell, TRUE);
+       else
+               gtkpspell->continue_check = NULL;
+               
+}
+
+void gtkpspell_highlight_all(GtkPspell *gtkpspell) 
+{
+       guint     origpos;
+       guint     pos = 0;
+       guint     len;
+       GtkSText *gtktext;
+       gfloat    adj_value;
+
+       g_return_if_fail(gtkpspell->gtkpspeller->checker);      
+
+       gtktext = gtkpspell->gtktext;
+
+       adj_value = gtktext->vadj->value;
+
+       len = gtk_stext_get_length(gtktext);
+
+       gtk_stext_freeze(gtktext);
+
+       origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+
+/*     gtk_editable_set_position(GTK_EDITABLE(gtktext), 0);*/
+
+       while (pos < len) {
+               while (pos < len && 
+                      iswordsep(get_text_index_whar(gtkpspell, pos)))
+                       pos++;
+               while (pos < len &&
+                      !iswordsep(get_text_index_whar(gtkpspell, pos)))
+                       pos++;
+               if (pos > 0)
+                       check_at(gtkpspell, pos - 1);
+       }
+       gtk_stext_thaw(gtktext);
+       gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
+       gtk_stext_set_point(GTK_STEXT(gtktext), origpos);
+       gtk_adjustment_set_value(gtktext->vadj, adj_value);
+       /*gtk_editable_select_region(GTK_EDITABLE(gtktext), origpos, origpos);*/
+}
+
+static void replace_with_supplied_word_cb(GtkWidget *w, GtkPspell *gtkpspell) 
+{
+       unsigned char *newword;
+       GdkEvent *e= (GdkEvent *) gtk_get_current_event();
+       
+       newword = gtk_editable_get_chars(GTK_EDITABLE(gtkpspell->replace_entry), 0, -1);
+       
+       if (strcmp(newword, gtkpspell->theword)) {
+               replace_real_word(gtkpspell, newword);
+
+               if ((e->type == GDK_KEY_PRESS && 
+                   ((GdkEventKey *) e)->state & GDK_MOD1_MASK)) {
+                       pspell_manager_store_replacement(gtkpspell->gtkpspeller->checker, 
+                                                        gtkpspell->theword, -1, 
+                                                        newword, -1);
+               }
+               gtkpspell->replace_entry = NULL;
+       }
+
+       g_free(newword);
+
+       set_point_continue(gtkpspell);
+}
+
 
-static void replace_word(GtkWidget *w, GtkPspell *gtkpspell) 
+static void replace_word_cb(GtkWidget *w, gpointer data)
 {
        unsigned char *newword;
+       GtkPspell *gtkpspell = (GtkPspell *) data;
        GdkEvent *e= (GdkEvent *) gtk_get_current_event();
 
+
        gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
+
        replace_real_word(gtkpspell, newword);
-       if ((e->type == GDK_KEY_PRESS && ((GdkEventKey *) e)->state & GDK_MOD1_MASK) ||
-           (e->type == GDK_BUTTON_RELEASE && ((GdkEventButton *) e)->state & GDK_MOD1_MASK)) {
+
+       if ((e->type == GDK_KEY_PRESS && 
+           ((GdkEventKey *) e)->state & GDK_MOD1_MASK) ||
+           (e->type == GDK_BUTTON_RELEASE && 
+            ((GdkEventButton *) e)->state & GDK_MOD1_MASK)) {
                pspell_manager_store_replacement(gtkpspell->gtkpspeller->checker, 
                                                 gtkpspell->theword, -1, 
                                                 newword, -1);
        }
-       gtkpspell->newword[0] = 0x00;
+
        gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
+
+       set_point_continue(gtkpspell);
 }
 
 static void replace_real_word(GtkPspell *gtkpspell, gchar *newword)
@@ -1026,27 +1148,26 @@ static void replace_real_word(GtkPspell *gtkpspell, gchar *newword)
        oldlen  = gtkpspell->end_pos - gtkpspell->start_pos;
        wordlen = strlen(gtkpspell->theword);
 
-       strncpy(gtkpspell->newword, newword, GTKPSPELLWORDSIZE - 1);
-       gtkpspell->newword[GTKPSPELLWORDSIZE-1] = 0x00;
        newlen = strlen(newword); /* FIXME: multybyte characters? */
 
        gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
-                       GTK_SIGNAL_FUNC(entry_insert_cb), 
-                       gtkpspell);
+                                        GTK_SIGNAL_FUNC(entry_insert_cb), 
+                                        gtkpspell);
        gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
-                       GTK_SIGNAL_FUNC(entry_delete_cb), 
-                       gtkpspell);
+                                        GTK_SIGNAL_FUNC(entry_delete_cb), 
+                                        gtkpspell);
+
        gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "delete-text", 
                                gtkpspell->start_pos, gtkpspell->end_pos);
        gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "insert-text", 
                                newword, newlen, &start);
        
        gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
-                       GTK_SIGNAL_FUNC(entry_insert_cb), 
-                       gtkpspell);
+                                          GTK_SIGNAL_FUNC(entry_insert_cb), 
+                                          gtkpspell);
        gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
-                       GTK_SIGNAL_FUNC(entry_delete_cb), 
-                       gtkpspell);
+                                          GTK_SIGNAL_FUNC(entry_delete_cb), 
+                                          gtkpspell);
        
        /* Put the point and the position where we clicked with the mouse */
        /* It seems to be a hack, as I must thaw,freeze,thaw the widget   */
@@ -1054,7 +1175,8 @@ static void replace_real_word(GtkPspell *gtkpspell, gchar *newword)
        /* point & position position. If not, SEGV after the first replacement */
        /* If the new word ends before point, put the point at its end*/
     
-       if (origpos - gtkpspell->start_pos < oldlen && origpos - gtkpspell->start_pos >= 0) {
+       if (origpos - gtkpspell->start_pos < oldlen && 
+           origpos - gtkpspell->start_pos >= 0) {
                /* Original point was in the word. */
                /* Let it there unless point is going to be outside of the word */
                if (origpos - gtkpspell->start_pos >= newlen) {
@@ -1070,75 +1192,196 @@ static void replace_real_word(GtkPspell *gtkpspell, gchar *newword)
        
        gtk_stext_thaw(GTK_STEXT(gtktext));
        gtk_stext_freeze(GTK_STEXT(gtktext));
+
        if (GTK_STEXT(gtktext)->text_len < pos)
                pos = gtk_stext_get_length(GTK_STEXT(gtktext));
+
        gtkpspell->orig_pos = pos;
+
        gtk_editable_set_position(GTK_EDITABLE(gtktext), gtkpspell->orig_pos);
        gtk_stext_set_point(GTK_STEXT(gtktext), 
                            gtk_editable_get_position(GTK_EDITABLE(gtktext)));
+
        gtk_stext_thaw(GTK_STEXT(gtktext));
 }
 
 /* Accept this word for this session */
-
-static void add_word_to_session(GtkWidget *w, GtkPspell *gtkpspell)
+static void add_word_to_session_cb(GtkWidget *w, gpointer data)
 {
        guint     pos;
        GtkSText *gtktext;
-    
+       GtkPspell *gtkpspell = (GtkPspell *) data; 
        gtktext = gtkpspell->gtktext;
 
        gtk_stext_freeze(GTK_STEXT(gtktext));
 
        pos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
     
-       pspell_manager_add_to_session(gtkpspell->gtkpspeller->checker,gtkpspell->theword, strlen(gtkpspell->theword));
+       pspell_manager_add_to_session(gtkpspell->gtkpspeller->checker,
+                                     gtkpspell->theword, 
+                                     strlen(gtkpspell->theword));
 
        check_at(gtkpspell, gtkpspell->start_pos);
 
        gtk_stext_thaw(gtkpspell->gtktext);
-       gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
+
+       gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
+
+       set_point_continue(gtkpspell);
 }
 
-/* add_word_to_personal() - add word to personal dict. */
-static void add_word_to_personal(GtkWidget *w, GtkPspell *gtkpspell)
+/* add_word_to_personal_cb() - add word to personal dict. */
+static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
 {
        GtkSText *gtktext;
-    
+       GtkPspell *gtkpspell = (GtkPspell *) data; 
        gtktext = gtkpspell->gtktext;
 
        gtk_stext_freeze(GTK_STEXT(gtktext));
     
-       pspell_manager_add_to_personal(gtkpspell->gtkpspeller->checker,gtkpspell->theword, strlen(gtkpspell->theword));
+       pspell_manager_add_to_personal(gtkpspell->gtkpspeller->checker,
+                                      gtkpspell->theword,
+                                      strlen(gtkpspell->theword));
     
        check_at(gtkpspell, gtkpspell->start_pos);
 
        gtk_stext_thaw(gtkpspell->gtktext);
-       gtk_menu_shell_deactivate(GTK_MENU_SHELL(w->parent));
+
+       gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
+       set_point_continue(gtkpspell);
+}
+
+static void deactivate_sug_menu_cb(GtkObject *w, gpointer data)
+{
+       GtkMenuShell *menu_shell = GTK_MENU_SHELL(data);
+       debug_print("Destroying %0x\n", w);
+       gtk_menu_shell_deactivate(menu_shell);
+}
+
+static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
+{
+       GtkWidget *dialog;
+       GtkWidget *label;
+       GtkWidget *hbox;
+       GtkWidget *entry;
+       GtkWidget *ok_button;
+       GtkWidget *cancel_button;
+       gchar *thelabel;
+       gint xx, yy;
+       GtkPspell *gtkpspell = (GtkPspell *) data;
+
+       gdk_window_get_origin((GTK_WIDGET(w)->parent)->window, &xx, &yy);
+
+       gtk_menu_shell_deactivate(GTK_MENU_SHELL(GTK_WIDGET(w)->parent));
+
+       dialog = gtk_dialog_new();
+       gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
+       gtk_window_set_title(GTK_WINDOW(dialog),_("Replace unknown word"));
+       gtk_widget_set_uposition(dialog, xx, yy);
+
+       gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
+                                 GTK_SIGNAL_FUNC(gtk_widget_destroy), 
+                                 GTK_OBJECT(dialog));
+
+       hbox = gtk_hbox_new(FALSE, 0);
+       gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
+
+       thelabel = g_strdup_printf(_("Replace \"%s\" with: "), 
+                                  gtkpspell->theword);
+       label = gtk_label_new(thelabel);
+       g_free(thelabel);
+       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
+
+       entry = gtk_entry_new();
+       gtkpspell->replace_entry = entry;
+       gtk_entry_set_text(GTK_ENTRY(entry), gtkpspell->theword);
+       gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
+       gtk_signal_connect(GTK_OBJECT(entry), "activate",
+                          GTK_SIGNAL_FUNC(replace_with_supplied_word_cb), 
+                          gtkpspell);
+       gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
+                          GTK_SIGNAL_FUNC(gtk_widget_destroy), 
+                          GTK_OBJECT(dialog));
+       gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
+
+       gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, 
+                          TRUE, 0);
+       if (!gtkpspell->gtkpspeller->ispell) {
+               label = gtk_label_new(_("Holding down MOD1 key while pressing Enter\nwill learn from mistake.\n"));
+               gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
+               gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
+               gtk_misc_set_padding(GTK_MISC(label), 8, 0);
+               gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), label, 
+                                  TRUE, TRUE, 0);
+       }
+
+       hbox = gtk_hbox_new(TRUE, 0);
+
+       ok_button = gtk_button_new_with_label(_("OK"));
+       gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);
+       gtk_signal_connect(GTK_OBJECT(ok_button), "clicked",
+                          GTK_SIGNAL_FUNC(replace_with_supplied_word_cb), 
+                          gtkpspell);
+       gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
+                                 GTK_SIGNAL_FUNC(gtk_widget_destroy), 
+                                 GTK_OBJECT(dialog));
+
+       cancel_button = gtk_button_new_with_label(_("Cancel"));
+       gtk_box_pack_start(GTK_BOX(hbox), cancel_button, TRUE, TRUE, 8);
+       gtk_signal_connect_object(GTK_OBJECT(cancel_button), "clicked",
+                                 GTK_SIGNAL_FUNC(gtk_widget_destroy), 
+                                 GTK_OBJECT(dialog));
+
+       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
+
+       gtk_widget_grab_focus(entry);
+
+       gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
+
+       gtk_widget_show_all(dialog);
 }
 
 void gtkpspell_uncheck_all(GtkPspell * gtkpspell) 
 {
-       int origpos;
-       unsigned char *text;
-       float adj_value;
+       gint      origpos;
+       gchar    *text;
+       gfloat    adj_value;
        GtkSText *gtktext;
        
-       gtktext=gtkpspell->gtktext;
+       gtktext = gtkpspell->gtktext;
 
        adj_value = gtktext->vadj->value;
+
        gtk_stext_freeze(gtktext);
+
        origpos = gtk_editable_get_position(GTK_EDITABLE(gtktext));
+
        text = gtk_editable_get_chars(GTK_EDITABLE(gtktext), 0, -1);
+
        gtk_stext_set_point(gtktext, 0);
        gtk_stext_forward_delete(gtktext, gtk_stext_get_length(gtktext));
        gtk_stext_insert(gtktext, NULL, NULL, NULL, text, strlen(text));
-       gtk_stext_thaw(gtktext);
 
-       g_free(text);
+       gtk_stext_thaw(gtktext);
 
        gtk_editable_set_position(GTK_EDITABLE(gtktext), origpos);
+       gtk_stext_set_point(gtktext, origpos);
        gtk_adjustment_set_value(gtktext->vadj, adj_value);
+
+       g_free(text);
+
+}
+
+static void toggle_check_while_typing_cb(GtkWidget *w, gpointer data)
+{
+       GtkPspell *gtkpspell = (GtkPspell *) data;
+
+       gtkpspell->check_while_typing = gtkpspell->check_while_typing == FALSE;
+       if (!gtkpspell->check_while_typing)
+               gtkpspell_uncheck_all(gtkpspell);
+
+       if (gtkpspell->config_menu)
+               populate_submenu(gtkpspell, gtkpspell->config_menu);
 }
 
 static GSList *create_empty_dictionary_list(void)
@@ -1146,8 +1389,8 @@ static GSList *create_empty_dictionary_list(void)
        GSList *list = NULL;
        Dictionary *dict;
 
-       dict = g_new0(Dictionary, 1);/*printf("N %08x            dictionary\n", dict);*/
-       dict->fullname = g_strdup(_("None"));/*printf("N %08x            fullname\n", dict->fullname);*/
+       dict = g_new0(Dictionary, 1);
+       dict->fullname = g_strdup(_("None"));
        dict->dictname = dict->fullname;
        dict->encoding = NULL;
        return g_slist_append(list, dict);
@@ -1177,42 +1420,49 @@ GSList *gtkpspell_get_dictionary_list(const gchar *pspell_path, gint refresh)
 #warning TODO: no directory change
 #endif
        dict_path = g_strdup(pspell_path);
-       prevdir = g_get_current_dir();
+       prevdir   = g_get_current_dir();
        if (chdir(dict_path) <0) {
-               debug_print(_("Error when searching for dictionaries:\n%s\n"),
-                                         g_strerror(errno));
+               debug_print(_("Pspell: error when searching for dictionaries:\n%s\n"),
+                           g_strerror(errno));
                g_free(prevdir);
                g_free(dict_path);
                gtkpspellcheckers->dictionary_list = create_empty_dictionary_list();
                return gtkpspellcheckers->dictionary_list; 
        }
 
-       debug_print(_("Checking for dictionaries in %s\n"), dict_path);
+       debug_print(_("Pspell: checking for dictionaries in %s\n"), dict_path);
 
        if (NULL != (dir = opendir("."))) {
                while (NULL != (ent = readdir(dir))) {
                        /* search for pwli */
-                       if ((NULL != (tmp = strstr2(ent->d_name, ".pwli"))) && (tmp[5] == 0x00)) {
-                               g_snprintf(tmpname, BUFSIZE, "%s%s", G_DIR_SEPARATOR_S, ent->d_name);
+                       if ((NULL != (tmp = strstr2(ent->d_name, ".pwli"))) && 
+                           (tmp[5] == 0x00)) {
+                               g_snprintf(tmpname, BUFSIZE, "%s%s", 
+                                          G_DIR_SEPARATOR_S, ent->d_name);
                                tmpname[MIN(tmp - ent->d_name + 1, BUFSIZE-1)] = 0x00;
-                               dict = g_new0(Dictionary, 1);/*printf("N %08x            dictionary\n", dict);*/
-                               dict->fullname = g_strdup_printf("%s%s", dict_path, tmpname);/*printf("N %08x            fullname\n", dict->fullname);*/
-                               dict->dictname = strrchr(dict->fullname, G_DIR_SEPARATOR) + 1;
+                               dict = g_new0(Dictionary, 1);
+                               dict->fullname = g_strdup_printf("%s%s", 
+                                                                dict_path, 
+                                                                tmpname);
+                               dict->dictname = strrchr(dict->fullname, 
+                                                        G_DIR_SEPARATOR) + 1;
                                dict->encoding = NULL;
-                               debug_print(_("Found dictionary %s %s\n"), dict->fullname, dict->dictname);
-                               list = g_slist_insert_sorted(list, dict, (GCompareFunc) compare_dict);
+                               debug_print(_("Pspell: found dictionary %s %s\n"),
+                                           dict->fullname, dict->dictname);
+                               list = g_slist_insert_sorted(list, dict,
+                                               (GCompareFunc) compare_dict);
                        }
                }                       
                closedir(dir);
        }
        else {
-               debug_print(_("Error when searching for dictionaries.\nNo dictionary found.\n(%s)"), 
-                                         g_strerror(errno));
+               debug_print(_("Pspell: error when searching for dictionaries.\nNo dictionary found.\n(%s)"), 
+                           g_strerror(errno));
                list = create_empty_dictionary_list();
        }
         if(list==NULL){
                
-               debug_print(_("Error when searching for dictionaries.\nNo dictionary found."));
+               debug_print(_("Pspell: error when searching for dictionaries.\nNo dictionary found."));
                list = create_empty_dictionary_list();
        }
        chdir(prevdir);
@@ -1338,7 +1588,7 @@ gint gtkpspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
        return sugmode;
        
 }
-
+#if 0 /* Experimenal */
 void gtkpspell_set_primary_dict(GtkPspell *gtkpspell, gchar *dict, gchar *encoding)
 {
        g_return_if_fail(gtkpspell);
@@ -1399,6 +1649,7 @@ gboolean gtkpspell_use_secondary(GtkPspell *gtkpspell)
 
        return gtkpspell_use_dict(gtkpspell, gtkpspell->dict2);
 }
+#endif
 
 /*****************************************************************************/
 
@@ -1419,7 +1670,7 @@ static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
                        if (misspelled_suggest(gtkpspell, gtkpspell->theword)) {
                                gtk_menu_popup(make_sug_menu(gtkpspell), 
                                               NULL, NULL, NULL, NULL,
-                                              eb->button, eb->time);
+                                              eb->button, GDK_CURRENT_TIME);
                                
                                return;
                        }
@@ -1431,7 +1682,7 @@ static void popup_menu(GtkPspell *gtkpspell, GdkEventButton *eb)
        }
 
        gtk_menu_popup(make_config_menu(gtkpspell),NULL,NULL,NULL,NULL,
-                      eb->button,eb->time);
+                      eb->button, GDK_CURRENT_TIME);
 }
 
 /* make_sug_menu() - Add menus to accept this word for this session and to add it to 
@@ -1449,9 +1700,14 @@ static GtkMenu *make_sug_menu(GtkPspell *gtkpspell)
 
        accel = gtk_accel_group_new();
        menu = gtk_menu_new(); 
-       
-       gtk_signal_connect(GTK_OBJECT(menu), "deactivate",
-               GTK_SIGNAL_FUNC(deactivate_menu_cb), gtkpspell);
+
+       if (gtkpspell->sug_menu)
+               gtk_widget_destroy(gtkpspell->sug_menu);
+
+       gtkpspell->sug_menu = menu;     
+
+       gtk_signal_connect(GTK_OBJECT(menu), "cancel",
+               GTK_SIGNAL_FUNC(cancel_menu_cb), gtkpspell);
 
        caption = g_strdup_printf(_("Unknown word: \"%s\""), 
                                  (unsigned char*) l->data);
@@ -1469,27 +1725,34 @@ static GtkMenu *make_sug_menu(GtkPspell *gtkpspell)
        gtk_widget_show(item);
        gtk_menu_append(GTK_MENU(menu), item);
         gtk_signal_connect(GTK_OBJECT(item), "activate",
-                          GTK_SIGNAL_FUNC(add_word_to_session), 
+                          GTK_SIGNAL_FUNC(add_word_to_session_cb), 
                           gtkpspell);
-       gtk_accel_group_add(accel, GDK_space, GDK_MOD1_MASK, 
-                           GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE, 
-                           GTK_OBJECT(item), "activate");
+       gtk_widget_add_accelerator(item, "activate", accel, GDK_space, GDK_MOD1_MASK,
+                                  GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
 
        item = gtk_menu_item_new_with_label(_("Add to personal dictionary"));
        gtk_widget_show(item);
        gtk_menu_append(GTK_MENU(menu), item);
         gtk_signal_connect(GTK_OBJECT(item), "activate",
-                          GTK_SIGNAL_FUNC(add_word_to_personal), 
+                          GTK_SIGNAL_FUNC(add_word_to_personal_cb), 
                           gtkpspell);
-       gtk_accel_group_add(accel, GDK_Return, GDK_MOD1_MASK, 
-                           GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE, 
-                           GTK_OBJECT(item), "activate");
-       
-        item = gtk_menu_item_new();
+       gtk_widget_add_accelerator(item, "activate", accel, GDK_Return, GDK_MOD1_MASK,
+                                  GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
+
+        item = gtk_menu_item_new_with_label(_("Replace with..."));
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+        gtk_signal_connect(GTK_OBJECT(item), "activate",
+                          GTK_SIGNAL_FUNC(replace_with_create_dialog_cb), 
+                          gtkpspell);
+       gtk_widget_add_accelerator(item, "activate", accel, GDK_R, 0,
+                                  GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE);
+
+       item = gtk_menu_item_new();
         gtk_widget_show(item);
         gtk_menu_append(GTK_MENU(menu), item);
 
-        l = l->next;
+       l = l->next;
         if (l == NULL) {
                item = gtk_menu_item_new_with_label(_("(no suggestions)"));
                gtk_widget_show(item);
@@ -1510,56 +1773,128 @@ static GtkMenu *make_sug_menu(GtkPspell *gtkpspell)
                                l = l->next;
                        } else if (count > MENUCOUNT) {
                                count -= MENUCOUNT;
+
                                item = gtk_menu_item_new_with_label(_("More..."));
                                gtk_widget_show(item);
                                gtk_menu_append(GTK_MENU(curmenu), item);
+
                                curmenu = gtk_menu_new();
                                gtk_menu_item_set_submenu(GTK_MENU_ITEM(item), curmenu);
                        }
+
                        item = gtk_menu_item_new_with_label((unsigned char*)l->data);
                        gtk_widget_show(item);
                        gtk_menu_append(GTK_MENU(curmenu), item);
                        gtk_signal_connect(GTK_OBJECT(item), "activate",
-                                          GTK_SIGNAL_FUNC(replace_word), gtkpspell);
-                       if (count <= MENUCOUNT) {
-                               gtk_accel_group_add(accel, 'a' + count, 0, 
-                                       GTK_ACCEL_LOCKED | GTK_ACCEL_VISIBLE, 
-                                       GTK_OBJECT(item), "activate");
-                               gtk_accel_group_add(accel, 'a' + count, 
-                                       GDK_MOD1_MASK, GTK_ACCEL_LOCKED, 
-                                       GTK_OBJECT(item), "activate");
+                                          GTK_SIGNAL_FUNC(replace_word_cb), gtkpspell);
+
+                       if (curmenu == menu && count < MENUCOUNT) {
+                               gtk_widget_add_accelerator(item, "activate",
+                                                          accel,
+                                                          GDK_A + count, 0,
+                                                          GTK_ACCEL_LOCKED | 
+                                                          GTK_ACCEL_VISIBLE);
+                               gtk_widget_add_accelerator(item, "activate", 
+                                                          accel,
+                                                          GDK_A + count, 
+                                                          GDK_MOD1_MASK,
+                                                          GTK_ACCEL_LOCKED);
                                }
 
-
                        count++;
+
                } while ((l = l->next) != NULL);
        }
-       
+
        gtk_accel_group_attach(accel, GTK_OBJECT(menu));
+       gtk_accel_group_unref(accel);
        
        return GTK_MENU(menu);
 }
 
-static GtkMenu *make_config_menu(GtkPspell *gtkpspell)
+static void populate_submenu(GtkPspell *gtkpspell, GtkWidget *menu)
 {
-       GtkWidget *menu, *item, *submenu;
-       gint ispell = gtkpspell->gtkpspeller->ispell;
-  
-       menu = gtk_menu_new();
+       GtkWidget *item, *submenu;
+       GtkPspeller *gtkpspeller = gtkpspell->gtkpspeller;
+       gchar *dictname;
+       gint ispell = gtkpspeller->ispell;
 
-        item = gtk_menu_item_new_with_label(_("Spell check all"));
-        gtk_signal_connect(GTK_OBJECT(item),"activate",
-                          GTK_SIGNAL_FUNC(check_all_cb), 
-                          gtkpspell);
+       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;
+               }
+       }
+       
+       dictname = g_strdup_printf(_("Dictionary: %s"), gtkpspeller->dictionary->dictname);
+       item = gtk_menu_item_new_with_label(dictname);
+       gtk_misc_set_alignment(GTK_MISC(GTK_BIN(item)->child), 0.5, 0.5);
+       g_free(dictname);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+
+       item = gtk_menu_item_new();
         gtk_widget_show(item);
         gtk_menu_append(GTK_MENU(menu), item);
+               
+       item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
+       if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_FASTMODE)
+               gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
+       if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_FASTMODE)
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
+       else
+               gtk_signal_connect(GTK_OBJECT(item), "activate",
+                                  GTK_SIGNAL_FUNC(set_sug_mode_cb),
+                                  gtkpspell);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
 
+       item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
+       if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_NORMALMODE)
+               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
+       if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_NORMALMODE) 
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+       else
+               gtk_signal_connect(GTK_OBJECT(item), "activate",
+                                  GTK_SIGNAL_FUNC(set_sug_mode_cb),
+                                  gtkpspell);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu),item);
+
+       item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
+       if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_BADSPELLERMODE)
+               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
+       if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_BADSPELLERMODE)
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+       else
+               gtk_signal_connect(GTK_OBJECT(item), "activate",
+                                  GTK_SIGNAL_FUNC(set_sug_mode_cb),
+                                  gtkpspell);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+       
+       item = gtk_menu_item_new();
+        gtk_widget_show(item);
+        gtk_menu_append(GTK_MENU(menu), item);
+       
+       item = gtk_check_menu_item_new_with_label(_("Check while typing"));
+       if (gtkpspell->check_while_typing)
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
+       else    
+               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), FALSE);
+       gtk_signal_connect(GTK_OBJECT(item), "activate",
+                          GTK_SIGNAL_FUNC(toggle_check_while_typing_cb),
+                          gtkpspell);
+       gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
 
-        item = gtk_menu_item_new();
+       item = gtk_menu_item_new();
         gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu),item);
+        gtk_menu_append(GTK_MENU(menu), item);
 
-       submenu = gtk_menu_new();
+       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);
@@ -1610,47 +1945,35 @@ static GtkMenu *make_config_menu(GtkPspell *gtkpspell)
                }
         }  
 
-        item = gtk_menu_item_new();
-        gtk_widget_show(item);
-        gtk_menu_append(GTK_MENU(menu), item);
+        
+       
 
-       item = gtk_check_menu_item_new_with_label(_("Fast Mode"));
-       if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_FASTMODE)
-               gtk_widget_set_sensitive(GTK_WIDGET(item),FALSE);
-       if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_FASTMODE)
-               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item),TRUE);
-       else
-               gtk_signal_connect(GTK_OBJECT(item), "activate",
-                               GTK_SIGNAL_FUNC(set_sug_mode_cb),
-                               gtkpspell);
-       gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+}
 
-       item = gtk_check_menu_item_new_with_label(_("Normal Mode"));
-       if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_NORMALMODE)
-               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
-       if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_NORMALMODE) 
-               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
-       else
-               gtk_signal_connect(GTK_OBJECT(item), "activate",
-                               GTK_SIGNAL_FUNC(set_sug_mode_cb),
-                               gtkpspell);
-       gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu),item);
+static GtkMenu *make_config_menu(GtkPspell *gtkpspell)
+{
+       if (!gtkpspell->popup_config_menu)
+               gtkpspell->popup_config_menu = gtk_menu_new();
 
-       item = gtk_check_menu_item_new_with_label(_("Bad Spellers Mode"));
-       if (ispell || gtkpspell->gtkpspeller->sug_mode == PSPELL_BADSPELLERMODE)
-               gtk_widget_set_sensitive(GTK_WIDGET(item), FALSE);
-       if (!ispell && gtkpspell->gtkpspeller->sug_mode == PSPELL_BADSPELLERMODE)
-               gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item), TRUE);
-       else
-               gtk_signal_connect(GTK_OBJECT(item), "activate",
-                               GTK_SIGNAL_FUNC(set_sug_mode_cb),
-                               gtkpspell);
-       gtk_widget_show(item);
-       gtk_menu_append(GTK_MENU(menu), item);
+       debug_print("Pspell: creating/using popup_config_menu %0x\n", 
+                       gtkpspell->popup_config_menu);
+       populate_submenu(gtkpspell, gtkpspell->popup_config_menu);
+
+        return GTK_MENU(gtkpspell->popup_config_menu);
+}
 
-        return GTK_MENU(menu);
+void gtkpspell_populate_submenu(GtkPspell *gtkpspell, GtkWidget *menuitem)
+{
+       GtkWidget *menu;
+
+       menu = GTK_WIDGET(GTK_MENU_ITEM(menuitem)->submenu);
+       
+       debug_print("Pspell: using config menu %0x\n", 
+                       gtkpspell->popup_config_menu);
+       populate_submenu(gtkpspell, menu);
+       
+       gtkpspell->config_menu = menu;
+       
 }
 
 static void set_menu_pos(GtkMenu *menu, gint *x, gint *y, gpointer data)
@@ -1688,20 +2011,14 @@ static void set_menu_pos(GtkMenu *menu, gint *x, gint *y, gpointer data)
 /*************************************************************************/
 /* Menu call backs */
 
-static gboolean deactivate_menu_cb(GtkWidget *w, gpointer *data)
+static gboolean cancel_menu_cb(GtkMenuShell *w, gpointer data)
 {
        GtkPspell *gtkpspell = (GtkPspell *) data;
-       GtkSText *gtktext;
-       gtktext = gtkpspell->gtktext;
-
-       gtk_stext_freeze(gtktext);
-       gtk_editable_set_position(GTK_EDITABLE(gtktext),gtkpspell->orig_pos);
-       gtk_stext_set_point(gtktext, gtkpspell->orig_pos);
-       gtk_stext_thaw(gtktext);
-
+       gtkpspell->continue_check = NULL;
+       set_point_continue(gtkpspell);
        return FALSE;
+       
 }
-
 /* change_dict_cb() - Menu callback : change dict */
 static void change_dict_cb(GtkWidget *w, GtkPspell *gtkpspell)
 {
@@ -1725,7 +2042,7 @@ static void change_dict_cb(GtkWidget *w, GtkPspell *gtkpspell)
 
        if (!gtkpspeller) {
                gchar *message;
-               message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s\n"), 
+               message = g_strdup_printf(_("The spell checker could not change dictionary.\n%s"), 
                                          gtkpspellcheckers->error_message);
 
                gtkpspell_alert_dialog(message); 
@@ -1737,23 +2054,41 @@ static void change_dict_cb(GtkWidget *w, GtkPspell *gtkpspell)
        }
        
        dictionary_delete(dict);
+
+       if (gtkpspell->config_menu)
+               populate_submenu(gtkpspell, gtkpspell->config_menu);
+
 }
 
 /******************************************************************************/
 /* Misc. helper functions */
 
-static void allocate_color(GtkPspell *gtkpspell)
+static void set_point_continue(GtkPspell *gtkpspell)
+{
+       GtkSText  *gtktext;
+
+       gtktext = gtkpspell->gtktext;
+
+       gtk_stext_freeze(gtktext);
+       gtk_editable_set_position(GTK_EDITABLE(gtktext),gtkpspell->orig_pos);
+       gtk_stext_set_point(gtktext, gtkpspell->orig_pos);
+       gtk_stext_thaw(gtktext);
+
+       if (gtkpspell->continue_check)
+               gtkpspell->continue_check((gpointer *) gtkpspell);
+}
+
+static void allocate_color(GtkPspell *gtkpspell, gint rgbvalue)
 {
        GdkColormap *gc;
        GdkColor *color = &(gtkpspell->highlight);
-       gint rgbvalue   = prefs_common.misspelled_col;
 
        gc = gtk_widget_get_colormap(GTK_WIDGET(gtkpspell->gtktext));
 
        if (gtkpspell->highlight.pixel)
                gdk_colormap_free_colors(gc, &(gtkpspell->highlight), 1);
 
-       /* Shameless copy from Sylpheed gtkutils.c */
+       /* Shameless copy from Sylpheed's gtkutils.c */
        color->pixel = 0L;
        color->red   = (int) (((gdouble)((rgbvalue & 0xff0000) >> 16) / 255.0) * 65535.0);
        color->green = (int) (((gdouble)((rgbvalue & 0x00ff00) >>  8) / 255.0) * 65535.0);
@@ -1763,10 +2098,10 @@ static void allocate_color(GtkPspell *gtkpspell)
 }
 
 static void change_color(GtkPspell * gtkpspell, 
-                        int start, int end, 
+                        gint start, gint end,
+                        gchar *newtext,
                          GdkColor *color) 
 {
-       char     *newtext;
        GtkSText *gtktext;
 
        g_return_if_fail(start < end);
@@ -1774,13 +2109,10 @@ static void change_color(GtkPspell * gtkpspell,
        gtktext = gtkpspell->gtktext;
     
        gtk_stext_freeze(gtktext);
-       newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
        if (newtext) {
                gtk_stext_set_point(gtktext, start);
                gtk_stext_forward_delete(gtktext, end - start);
-
                gtk_stext_insert(gtktext, NULL, color, NULL, newtext, end - start);
-               g_free(newtext);
        }
        gtk_stext_thaw(gtktext);
 }
@@ -1869,7 +2201,6 @@ static void reset_theword_data(GtkPspell *gtkpspell)
        gtkpspell->end_pos       =  0;
        gtkpspell->theword[0]    =  0;
        gtkpspell->max_sug       = -1;
-       gtkpspell->newword[0]    =  0;
 
        free_suggestions_list(gtkpspell);
 }
@@ -1880,7 +2211,7 @@ static void free_checkers(gpointer elt, gpointer data)
 
        g_return_if_fail(gtkpspeller);
 
-       gtkpspeller_delete(gtkpspeller);
+       gtkpspeller_real_delete(gtkpspeller);
 }
 
 static gint find_gtkpspeller(gconstpointer aa, gconstpointer bb)
@@ -1888,11 +2219,9 @@ static gint find_gtkpspeller(gconstpointer aa, gconstpointer bb)
        Dictionary *a = ((GtkPspeller *) aa)->dictionary;
        Dictionary *b = ((GtkPspeller *) bb)->dictionary;
 
-       if (!a || !b)
-               return 1;
-
-       if (strcmp(a->fullname, b->fullname) == 0
-           && a->encoding && b->encoding)
+       if (a && b && a->fullname && b->fullname  &&
+           strcmp(a->fullname, b->fullname) == 0 &&
+           a->encoding && b->encoding)
                return strcmp(a->encoding, b->encoding);
 
        return 1;
@@ -1901,27 +2230,37 @@ static gint find_gtkpspeller(gconstpointer aa, gconstpointer bb)
 static void gtkpspell_alert_dialog(gchar *message)
 {
        GtkWidget *dialog;
+       GtkWidget *hbox;
        GtkWidget *label;
        GtkWidget *ok_button;
 
        dialog = gtk_dialog_new();
-       label  = gtk_label_new(message);
-       ok_button = gtk_button_new_with_label(_("OK"));
-
        gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
        gtk_window_set_position(GTK_WINDOW(dialog),GTK_WIN_POS_MOUSE);
+       gtk_signal_connect_object(GTK_OBJECT(dialog), "destroy",
+                                  GTK_SIGNAL_FUNC(gtk_widget_destroy), 
+                                  GTK_OBJECT(dialog));
+
+       label  = gtk_label_new(message);
+       gtk_misc_set_padding(GTK_MISC(label), 8, 8);
+
+       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
+       
+       hbox = gtk_hbox_new(FALSE, 0);
+
+       ok_button = gtk_button_new_with_label(_("OK"));
        GTK_WIDGET_SET_FLAGS(ok_button, GTK_CAN_DEFAULT);
+       gtk_box_pack_start(GTK_BOX(hbox), ok_button, TRUE, TRUE, 8);    
+
        gtk_signal_connect_object(GTK_OBJECT(ok_button), "clicked",
                                   GTK_SIGNAL_FUNC(gtk_widget_destroy), 
                                   GTK_OBJECT(dialog));
-
-       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), ok_button);
+       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), hbox);
                        
-       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), label);
-       gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 8);
        gtk_widget_grab_default(ok_button);
        gtk_widget_grab_focus(ok_button);
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
+
        gtk_widget_show_all(dialog);
 }
 #endif