Fixed bugs:
[claws.git] / src / gtkspell.c
index 6f88e60b322cb7c8a8f3850eb7ae77a309e18d88..d4b1838b7dbe10981ace1492f8d7ba78c1a79911 100644 (file)
@@ -77,16 +77,16 @@ static void menu_change_dict                (GtkWidget *w, GtkPspell *gtkpspell);
 static void entry_insert_cb            (GtkXText *gtktext, gchar *newtext, 
                                         guint len, guint *ppos, 
                                         GtkPspell *gtkpspell);
+static void entry_delete_cb            (GtkXText *gtktext, gint start, gint end, 
+                                        GtkPspell *gtkpspell);
 static gint compare_dict               (Dictionary *a, Dictionary *b);
 guchar *convert_to_pspell_encoding     (const guchar *encoding);
 
+static void allocate_color(GtkPspell *gtkpspell);
 
 /* gtkspellconfig - only one config per session */
 GtkPspellConfig * gtkpspellconfig;
 
-/* TODO: configurable */
-static GdkColor highlight = { 0, 255 * 256, 0, 0 };
-
 /******************************************************************************/
 
 /* gtkspell_init() - run the first pspell_config from which every
@@ -151,6 +151,7 @@ GtkPspell * gtkpspell_new(GtkPspellConfig *gtkpspellconfig)
        gtkpspell->mode            = PSPELL_FASTMODE;
        gtkpspell->learn           = TRUE;
        gtkpspell->gtktext         = NULL;
+
        return gtkpspell;
 }
 
@@ -178,7 +179,7 @@ GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig,
        gtkpspell->gtktext         = NULL;
   
        gtkpspell->config          = pspell_config_clone(gtkpspellconfig);
-       gtkpspell->mode            = PSPELL_FASTMODE;
+       gtkpspell->mode            = mode;
        gtkpspell->learn           = TRUE;
        
        if (!set_path_and_dict(gtkpspell, gtkpspell->config, path, dict)) {
@@ -211,6 +212,7 @@ GtkPspell *gtkpspell_new_with_config(GtkPspellConfig *gtkpspellconfig,
        else {
                gtkpspell->checker = to_pspell_manager( gtkpspell->possible_err );
        }
+
        return gtkpspell;
 }
 
@@ -698,16 +700,10 @@ static void change_color(GtkPspell * gtkpspell,
        gtk_xtext_freeze(gtktext);
        newtext = gtk_editable_get_chars(GTK_EDITABLE(gtktext), start, end);
        if (newtext) {
-               gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
-                                                GTK_SIGNAL_FUNC(entry_insert_cb), 
-                                                gtkpspell);
                gtk_xtext_set_point(gtktext, start);
                gtk_xtext_forward_delete(gtktext, end - start);
 
                gtk_xtext_insert(gtktext, NULL, color, NULL, newtext, end - start);
-               gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
-                                                  GTK_SIGNAL_FUNC(entry_insert_cb), 
-                                                  gtkpspell);
                g_free(newtext);
        }
        gtk_xtext_thaw(gtktext);
@@ -730,16 +726,10 @@ static gboolean check_at(GtkPspell *gtkpspell, int from_pos)
        gtkpspell->theword[BUFSIZE - 1] = 0;
 
        if (misspelled_test(gtkpspell, buf)) {
-               if (highlight.pixel == 0) {
-                       /* add an entry for the highlight in the color map. */
-                       GdkColormap *gc = gtk_widget_get_colormap(GTK_WIDGET(gtktext));
-                       gdk_colormap_alloc_color(gc, &highlight, FALSE, TRUE);
-               }
-               change_color(gtkpspell, start, end, &highlight);
+                       change_color(gtkpspell, start, end, &(gtkpspell->highlight));
                return TRUE;
        } else {
-               change_color(gtkpspell, start, end,
-                            &(GTK_WIDGET(gtktext)->style->fg[0]));
+               change_color(gtkpspell, start, end, NULL);
                return FALSE;
        }
 }
@@ -795,8 +785,7 @@ static void entry_insert_cb(GtkXText *gtktext, gchar *newtext,
        /* Never mess with set_insertion when frozen */
        gtk_xtext_freeze(gtktext);
        gtk_xtext_backward_delete(GTK_XTEXT(gtktext), len);
-       gtk_xtext_insert(GTK_XTEXT(gtktext), NULL,
-                        &(GTK_WIDGET(gtktext)->style->fg[0]), NULL, newtext, len);
+       gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newtext, len);
        *ppos = gtk_xtext_get_point(GTK_XTEXT(gtktext));
               
        if (iswordsep(newtext[0])) {
@@ -862,9 +851,22 @@ static void replace_word(GtkWidget *w, GtkPspell *gtkpspell)
        oldlen = end - start;
 
        gtk_xtext_set_point(GTK_XTEXT(gtktext), end);
-       gtk_xtext_backward_delete(GTK_XTEXT(gtktext), end - start);
-       gtk_xtext_insert(GTK_XTEXT(gtktext), NULL, NULL, NULL, newword, strlen(newword));
+       gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
+                       GTK_SIGNAL_FUNC(entry_insert_cb), 
+                       gtkpspell);
+       gtk_signal_handler_block_by_func(GTK_OBJECT(gtktext),
+                       GTK_SIGNAL_FUNC(entry_delete_cb), 
+                       gtkpspell);
+       gtk_xtext_set_point(gtktext, start);
+       gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "delete-text", start, end);
+       gtk_signal_emit_by_name(GTK_OBJECT(gtktext), "insert-text", newword, strlen(newword), &start);
     
+       gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
+                       GTK_SIGNAL_FUNC(entry_insert_cb), 
+                       gtkpspell);
+       gtk_signal_handler_unblock_by_func(GTK_OBJECT(gtktext),
+                       GTK_SIGNAL_FUNC(entry_delete_cb), 
+                       gtkpspell);
        if (end-start > 0 && gtkpspell->learn) { 
                /* Just be sure the buffer ends somewhere... */
                buf[end-start] = 0; 
@@ -1236,7 +1238,7 @@ void gtkpspell_uncheck_all(GtkPspell * gtkpspell)
 
 void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext) 
 {
-       gtkpspell->gtktext=gtktext;
+       gtkpspell->gtktext = gtktext;
        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",
@@ -1244,6 +1246,8 @@ void gtkpspell_attach(GtkPspell *gtkpspell, GtkXText *gtktext)
        gtk_signal_connect(GTK_OBJECT(gtktext), "button-press-event",
                           GTK_SIGNAL_FUNC(button_press_intercept_cb), gtkpspell);
 
+       allocate_color(gtkpspell);
+       
 }
 
 void gtkpspell_detach(GtkPspell * gtkpspell) 
@@ -1417,6 +1421,59 @@ gchar *gtkpspell_get_dictionary_menu_active_item(GtkWidget *menu)
   
 }
 
+GtkWidget *gtkpspell_sugmode_option_menu_new(gint sugmode)
+{
+       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);
+       gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_FASTMODE));
+
+       item = gtk_menu_item_new_with_label(_("Normal Mode"));
+        gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+       gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_NORMALMODE));
+       
+       item = gtk_menu_item_new_with_label(_("Bad Spellers Mode"));
+        gtk_widget_show(item);
+       gtk_menu_append(GTK_MENU(menu), item);
+       gtk_object_set_data(GTK_OBJECT(item), "sugmode", GINT_TO_POINTER(PSPELL_BADSPELLERMODE));
+
+       return menu;
+}
+       
+void gtkpspell_sugmode_option_menu_set(GtkOptionMenu *optmenu, gint sugmode)
+{
+       g_return_if_fail(GTK_IS_OPTION_MENU(optmenu));
+
+       g_return_if_fail(sugmode == PSPELL_FASTMODE ||
+                        sugmode == PSPELL_NORMALMODE ||
+                        sugmode == PSPELL_BADSPELLERMODE);
+
+       gtk_option_menu_set_history(GTK_OPTION_MENU(optmenu), sugmode - 1);
+}
+
+gint gtkpspell_get_sugmode_from_option_menu(GtkOptionMenu *optmenu)
+{
+       gint sugmode;
+       GtkWidget *item;
+       
+       g_return_val_if_fail(GTK_IS_OPTION_MENU(optmenu), -1);
+
+       item = gtk_menu_get_active(GTK_MENU(gtk_option_menu_get_menu(optmenu)));
+       
+       sugmode = GPOINTER_TO_INT(gtk_object_get_data(GTK_OBJECT(item), "sugmode"));
+
+       return sugmode;
+       
+}
+               
 /* convert_to_pspell_encoding () - converts ISO-8859-* strings to iso8859-* 
  * as needed by pspell. Returns an allocated string.
  */
@@ -1437,6 +1494,21 @@ guchar *convert_to_pspell_encoding (const guchar *encoding)
        
 }
 
-               
-               
+static void allocate_color(GtkPspell *gtkpspell)
+{
+
+       GdkColormap *gc;
+       /* Color allocation */
+       gc = gtk_widget_get_colormap(GTK_WIDGET(gtkpspell->gtktext));
+
+       if (gtkpspell->highlight.pixel)
+               gdk_colormap_free_colors(gc, &(gtkpspell->highlight), 1);
+
+       gtkut_convert_int_to_gdk_color(prefs_common.misspelled_col,
+                       &(gtkpspell->highlight));
+
+       /* add an entry for the highlight in the color map. */
+       gdk_colormap_alloc_color(gc, &(gtkpspell->highlight), FALSE, TRUE);
+}
+
 #endif