2005-09-26 [colin] 1.9.14cvs55
[claws.git] / src / gtk / gtkaspell.c
index 16dc8b907dd80f9b99585cc6b405383e3ba95478..98e1ceccfc4fd42d63b5dc8a9c61e2292d379eac 100644 (file)
 
 #include <aspell.h>
 
-#include "gtk/gtktext.h"
 #include "utils.h"
 #include "codeconv.h"
 #include "alertpanel.h"
 #include "gtkaspell.h"
+#include "gtk/gtkutils.h"
+
 #define ASPELL_FASTMODE       1
 #define ASPELL_NORMALMODE     2
 #define ASPELL_BADSPELLERMODE 3
@@ -257,7 +258,6 @@ static void destroy_menu(GtkWidget *widget, gpointer user_data);
 
 /******************************************************************************/
 static gint get_textview_buffer_charcount(GtkTextView *view);
-static gint get_textview_buffer_position(GtkTextView *view);
 
 static gint get_textview_buffer_charcount(GtkTextView *view)
 {
@@ -325,6 +325,7 @@ void gtkaspell_checkers_quit(void)
 
                g_slist_foreach(checkers, free_checkers, NULL);
                g_slist_free(checkers);
+               gtkaspellcheckers->checkers = NULL;
        }
 
        if ((dict_list = gtkaspellcheckers->dictionary_list)) {
@@ -336,7 +337,7 @@ void gtkaspell_checkers_quit(void)
        }
 
        g_free(gtkaspellcheckers->error_message);
-
+       gtkaspellcheckers->error_message = NULL;
        return;
 }
 
@@ -436,19 +437,19 @@ GtkAspell *gtkaspell_new(const gchar *dictionary_path,
        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);
        
@@ -470,6 +471,7 @@ void gtkaspell_delete(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);
 
@@ -484,7 +486,6 @@ static void entry_insert_cb(GtkTextBuffer *textbuf,
                            gint len,
                            GtkAspell *gtkaspell)
 {
-       size_t wlen;
        guint pos;
 
        g_return_if_fail(gtkaspell->gtkaspeller->checker);
@@ -809,9 +810,11 @@ static void set_sug_mode_cb(GtkMenuItem *w, GtkAspell *gtkaspell)
 {
        char *themode;
        
-       gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar **) &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);
@@ -895,7 +898,7 @@ static GList *misspelled_suggest(GtkAspell *gtkaspell, guchar *word)
                elements    = aspell_word_list_elements(suggestions);
                list        = g_list_append(list, g_strdup(word)); 
                
-               while (newword = aspell_string_enumeration_next(elements))
+               while ((newword = aspell_string_enumeration_next(elements)) != NULL)
                        list = g_list_append(list, g_strdup(newword));
 
                gtkaspell->max_sug          = g_list_length(list) - 1;
@@ -919,7 +922,7 @@ static int misspelled_test(GtkAspell *gtkaspell, unsigned char *word)
 
 static gboolean iswordsep(unsigned char c) 
 {
-       return !isalpha(c) && c != '\'';
+       return (isspace(c) || ispunct(c)) && c != '\'';
 }
 
 static guchar get_text_index_whar(GtkAspell *gtkaspell, int pos) 
@@ -979,7 +982,11 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
                c = get_text_index_whar(gtkaspell, start);
                if (c == '\'') {
                        if (start > 0) {
-                               if (!isalpha(get_text_index_whar(gtkaspell,
+                               if (isspace(get_text_index_whar(gtkaspell,
+                                                                start - 1))
+                               ||  ispunct(get_text_index_whar(gtkaspell,
+                                                                start - 1))
+                               ||  isdigit(get_text_index_whar(gtkaspell,
                                                                 start - 1))) {
                                        /* start_quote = TRUE; */
                                        break;
@@ -990,7 +997,7 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
                                break;
                        }
                }
-               else if (!isalpha(c))
+               else if (isspace(c) || ispunct(c) || isdigit(c))
                                break;
        }
 
@@ -1000,7 +1007,11 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
                c = get_text_index_whar(gtkaspell, end); 
                if (c == '\'') {
                        if (end < get_textview_buffer_charcount(gtktext)) {
-                               if (!isalpha(get_text_index_whar(gtkaspell,
+                               if (isspace(get_text_index_whar(gtkaspell,
+                                                                end + 1))
+                               ||  ispunct(get_text_index_whar(gtkaspell,
+                                                                end + 1))
+                               ||  isdigit(get_text_index_whar(gtkaspell,
                                                                 end + 1))) {
                                        /* end_quote = TRUE; */
                                        break;
@@ -1011,7 +1022,7 @@ static gboolean get_word_from_pos(GtkAspell *gtkaspell, gint pos,
                                break;
                        }
                }
-               else if(!isalpha(c))
+               else if (isspace(c) || ispunct(c) || isdigit(c))
                                break;
        }
                                                
@@ -1022,11 +1033,18 @@ 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, *conv;
+                       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);
+                       conv = conv_iconv_strdup(tmp, CS_UTF_8, 
+                               gtkaspell->gtkaspeller->dictionary->encoding);
+                       g_free(tmp);
+                       strncpy(buf, conv, buflen-1);
+                       buf[buflen]='\0';
+                       g_free(conv);
                } else
                        return FALSE;
        }
@@ -1047,7 +1065,6 @@ static gboolean check_at(GtkAspell *gtkaspell, gint from_pos)
        if (!get_word_from_pos(gtkaspell, from_pos, buf, sizeof(buf), 
                               &start, &end))
                return FALSE;
-
        if (misspelled_test(gtkaspell, buf)) {
                strncpy(gtkaspell->theword, buf, GTKASPELLWORDSIZE - 1);
                gtkaspell->theword[GTKASPELLWORDSIZE - 1] = 0;
@@ -1226,7 +1243,7 @@ static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
 {
        unsigned char *newword;
        GdkEvent *e= (GdkEvent *) gtk_get_current_event();
-       
+
        newword = gtk_editable_get_chars(GTK_EDITABLE(gtkaspell->replace_entry),
                                         0, -1);
 
@@ -1245,6 +1262,10 @@ static void replace_with_supplied_word_cb(GtkWidget *w, GtkAspell *gtkaspell)
 
        g_free(newword);
 
+       if (w && GTK_IS_DIALOG(w)) {
+               gtk_widget_destroy(w);
+       }
+
        set_point_continue(gtkaspell);
 }
 
@@ -1255,7 +1276,8 @@ static void replace_word_cb(GtkWidget *w, gpointer data)
        GtkAspell *gtkaspell = (GtkAspell *) data;
        GdkEvent *e= (GdkEvent *) gtk_get_current_event();
 
-       gtk_label_get(GTK_LABEL(GTK_BIN(w)->child), (gchar**) &newword);
+       newword = (unsigned char *) gtk_label_get_text(GTK_LABEL(GTK_BIN(w)->child));
+       newword = g_strdup(newword);
 
        replace_real_word(gtkaspell, newword);
 
@@ -1272,6 +1294,7 @@ static void replace_word_cb(GtkWidget *w, gpointer data)
        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)
@@ -1279,7 +1302,6 @@ static void replace_real_word(GtkAspell *gtkaspell, gchar *newword)
        int             oldlen, newlen, wordlen;
        gint            origpos;
        gint            pos;
-       gint            start = gtkaspell->start_pos;
        GtkTextView     *gtktext;
        GtkTextBuffer   *textbuf;
        GtkTextIter     startiter, enditer;
@@ -1374,7 +1396,6 @@ static void add_word_to_session_cb(GtkWidget *w, gpointer data)
 static void add_word_to_personal_cb(GtkWidget *w, gpointer data)
 {
        GtkAspell *gtkaspell = (GtkAspell *) data; 
-       GtkTextView *gtktext    = gtkaspell->gtktext;
 
        aspell_speller_add_to_personal(gtkaspell->gtkaspeller->checker,
                                       gtkaspell->theword,
@@ -1430,8 +1451,7 @@ static gboolean replace_key_pressed(GtkWidget *widget,
                gtk_widget_destroy(widget);
                return TRUE;
        } else if (event && event->keyval == GDK_Return) {
-               replace_with_supplied_word_cb(NULL, gtkaspell);
-               gtk_widget_destroy(widget);
+               replace_with_supplied_word_cb(widget, gtkaspell);
                return TRUE;
        }
        return FALSE;
@@ -1439,12 +1459,17 @@ static gboolean replace_key_pressed(GtkWidget *widget,
        
 static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
 {
+       static PangoFontDescription *font_desc;
        GtkWidget *dialog;
        GtkWidget *label;
+       GtkWidget *w_hbox;
        GtkWidget *hbox;
+       GtkWidget *vbox;
        GtkWidget *entry;
        GtkWidget *ok_button;
        GtkWidget *cancel_button;
+       GtkWidget *confirm_area;
+       GtkWidget *icon;
        gchar *thelabel;
        gint xx, yy;
        GtkAspell *gtkaspell = (GtkAspell *) data;
@@ -1457,21 +1482,56 @@ 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);
 
        g_signal_connect_swapped(G_OBJECT(dialog), "destroy",
                                 G_CALLBACK(gtk_widget_destroy), 
                                 G_OBJECT(dialog));
 
-       hbox = gtk_hbox_new(FALSE, 0);
-       gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
+       gtk_box_set_spacing (GTK_BOX (GTK_DIALOG (dialog)->vbox), 14);
+       hbox = gtk_hbox_new (FALSE, 12);
+       gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
+       gtk_widget_show (hbox);
+       gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
+                           FALSE, FALSE, 0);
 
-       thelabel = g_strdup_printf(_("Replace \"%s\" with: "), 
+       thelabel = g_strdup_printf(_("<span weight=\"bold\" "
+                                       "size=\"larger\">Replace \"%s\" with: </span>"), 
                                   gtkaspell->theword);
+       /* for title label */
+       w_hbox = gtk_hbox_new(FALSE, 0);
+       
+       icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION,
+                                       GTK_ICON_SIZE_DIALOG); 
+       gtk_misc_set_alignment (GTK_MISC (icon), 0.5, 0.0);
+       gtk_box_pack_start (GTK_BOX (hbox), icon, FALSE, FALSE, 0);
+       
+       vbox = gtk_vbox_new (FALSE, 12);
+       gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
+       gtk_widget_show (vbox);
+       
        label = gtk_label_new(thelabel);
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+       gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
+       gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
+       gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
+       gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
+       if (!font_desc) {
+               gint size;
+
+               size = pango_font_description_get_size
+                       (label->style->font_desc);
+               font_desc = pango_font_description_new();
+               pango_font_description_set_weight
+                       (font_desc, PANGO_WEIGHT_BOLD);
+               pango_font_description_set_size
+                       (font_desc, size * PANGO_SCALE_LARGE);
+       }
+       if (font_desc)
+               gtk_widget_modify_font(label, font_desc);
        g_free(thelabel);
-       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
-
+       
        entry = gtk_entry_new();
        gtkaspell->replace_entry = entry;
        gtk_entry_set_text(GTK_ENTRY(entry), gtkaspell->theword);
@@ -1479,22 +1539,26 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
        g_signal_connect(G_OBJECT(dialog),
                        "key_press_event",
                        G_CALLBACK(replace_key_pressed), gtkaspell);
-       gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
 
-       gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), hbox, TRUE, 
-                          TRUE, 0);
        label = gtk_label_new(_("Holding down Control key while pressing "
                                "Enter\nwill learn from mistake.\n"));
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
        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);
+       gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
+       gtk_widget_show(label);
 
        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);
+       gtkut_stock_button_set_create(&confirm_area,
+                                     &ok_button, GTK_STOCK_OK,
+                                     &cancel_button, GTK_STOCK_CANCEL,
+                                     NULL, NULL);
+
+       gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->action_area),
+                        confirm_area, FALSE, FALSE, 0);
+       gtk_container_set_border_width(GTK_CONTAINER(confirm_area), 5);
+
        g_signal_connect(G_OBJECT(ok_button), "clicked",
                         G_CALLBACK(replace_with_supplied_word_cb), 
                         gtkaspell);
@@ -1502,14 +1566,10 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
                                   G_CALLBACK(gtk_widget_destroy), 
                                   G_OBJECT(dialog));
 
-       cancel_button = gtk_button_new_with_label(_("Cancel"));
-       gtk_box_pack_start(GTK_BOX(hbox), cancel_button, TRUE, TRUE, 8);
        g_signal_connect_swapped(G_OBJECT(cancel_button), "clicked",
                                 G_CALLBACK(gtk_widget_destroy), 
                                 G_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);
@@ -1519,9 +1579,6 @@ static void replace_with_create_dialog_cb(GtkWidget *w, gpointer data)
 
 void gtkaspell_uncheck_all(GtkAspell * gtkaspell) 
 {
-       gint      origpos;
-       gchar    *text;
-       gfloat    adj_value;
        GtkTextView *gtktext;
        GtkTextBuffer *buffer;
        GtkTextIter startiter, enditer;
@@ -1583,17 +1640,7 @@ GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
        list = NULL;
 
        config = new_aspell_config();
-#if 0 
-       aspell_config_replace(config, "rem-all-word-list-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();
 
-               return gtkaspellcheckers->dictionary_list; 
-       }
-#endif
        aspell_config_replace(config, "dict-dir", aspell_path);
        if (aspell_config_error_number(config) != 0) {
                gtkaspellcheckers->error_message = g_strdup(
@@ -1616,8 +1663,15 @@ GSList *gtkaspell_get_dictionary_list(const gchar *aspell_path, gint refresh)
                                entry->name);
                dict->dictname = dict->fullname + strlen(aspell_path);
                dict->encoding = g_strdup(entry->code);
-               debug_print("Aspell: found dictionary %s %s\n", dict->fullname,
-                               dict->dictname);
+               
+               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);
        }
@@ -1886,10 +1940,14 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
        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);
        caption = g_strdup_printf(_("\"%s\" unknown in %s"), 
-                                 (unsigned char*) l->data
+                                 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);
@@ -1978,6 +2036,7 @@ static GtkMenu *make_sug_menu(GtkAspell *gtkaspell)
                                                        conv_get_locale_charset_str(),
                                                        CS_UTF_8);
                        item = gtk_menu_item_new_with_label(utf8buf);
+                       g_free(utf8buf);
                        gtk_widget_show(item);
                        gtk_menu_append(GTK_MENU(curmenu), item);
                        g_signal_connect(G_OBJECT(item), "activate",
@@ -2261,6 +2320,7 @@ gboolean gtkaspell_change_dict(GtkAspell *gtkaspell, const gchar *dictionary)
            dict == gtkaspell->alternate_speller->dictionary) {
                use_alternate_dict(gtkaspell);
                dictionary_delete(dict);
+               gtkaspell->alternate_speller->dictionary = NULL;
                return TRUE;
        }
        
@@ -2341,8 +2401,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, 
@@ -2354,7 +2419,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;
     
@@ -2364,9 +2430,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-* 
@@ -2442,7 +2510,7 @@ static void free_suggestions_list(GtkAspell *gtkaspell)
             list = list->next)
                g_free(list->data);
 
-       g_list_free(list);
+       g_list_free(gtkaspell->suggestions_list);
        
        gtkaspell->max_sug          = -1;
        gtkaspell->suggestions_list = NULL;