2005-09-28 [paul] 1.9.14cvs59
[claws.git] / src / gtk / gtkaspell.c
index d160dbfd65a7c2dc82dd85c12fdd30e04df2046c..18de6b3f83705bda478f738a992b07691bce4bcf 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-1]='\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;
@@ -1259,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);
 
@@ -1276,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)
@@ -1283,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;
@@ -1378,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,
@@ -1562,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;
@@ -1926,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);
@@ -2018,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",
@@ -2301,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;
        }
        
@@ -2381,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, 
@@ -2394,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;
     
@@ -2404,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-* 
@@ -2482,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;