2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / gtk / spell_entry.c
index ba6a6d84f0cadb09cb1976dad83ea3947a05c1a3..1d6dbb57f788a9cfeca28e2cf1e69942d974bfbc 100644 (file)
@@ -3,7 +3,7 @@
  *
  * @Copyright (C) 2004-2006 Christian Hammond.
  * Some of this code is from gtkspell, Copyright (C) 2002 Evan Martin.
- * Adapted for Claws Mail (c) 2009 Pawel Pekala and the Claws Mail team
+ * Adapted for Claws Mail (c) 2009-2011 Pawel Pekala and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
 #include "prefs_common.h"
 #include "codeconv.h"
 #include "defs.h"
+#include "gtkutils.h"
 
 static void claws_spell_entry_init             (ClawsSpellEntry *entry);
 static void claws_spell_entry_editable_init    (GtkEditableClass *iface);
 static void claws_spell_entry_finalize         (GObject *object);
+#if !GTK_CHECK_VERSION(3, 0, 0)
 static void claws_spell_entry_destroy          (GtkObject *object);
 static gint claws_spell_entry_expose           (GtkWidget *widget,
                                                 GdkEventExpose *event);
+#else
+static void claws_spell_entry_destroy          (GtkWidget *object);
+static gint claws_spell_entry_expose           (GtkWidget *widget,
+                                                cairo_t *cr);
+#endif
 static gint claws_spell_entry_button_press     (GtkWidget *widget,
                                                 GdkEventButton *event);
 static gboolean claws_spell_entry_popup_menu   (GtkWidget *widget,
@@ -75,7 +82,9 @@ G_DEFINE_TYPE_EXTENDED(ClawsSpellEntry, claws_spell_entry, GTK_TYPE_ENTRY, 0, G_
 static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass)
 {
        GObjectClass    *g_object_class;
+#if !GTK_CHECK_VERSION(3, 0, 0)
        GtkObjectClass  *gtk_object_class;
+#endif
        GtkWidgetClass  *widget_class;
        
        parent_class = g_type_class_peek_parent(klass);
@@ -83,12 +92,19 @@ static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass)
        g_object_class = G_OBJECT_CLASS(klass);
        g_object_class->finalize = claws_spell_entry_finalize;
        
+#if !GTK_CHECK_VERSION(3, 0, 0)
        gtk_object_class = GTK_OBJECT_CLASS(klass);
        gtk_object_class->destroy = claws_spell_entry_destroy;
+#endif
        
        widget_class = GTK_WIDGET_CLASS(klass);
-       widget_class->expose_event = claws_spell_entry_expose;
        widget_class->button_press_event = claws_spell_entry_button_press;
+#if !GTK_CHECK_VERSION(3, 0, 0)
+       widget_class->expose_event = claws_spell_entry_expose;
+#else
+       widget_class->draw = claws_spell_entry_expose;
+       widget_class->destroy = claws_spell_entry_destroy;
+#endif
        
        g_type_class_add_private(g_object_class,
                        sizeof(struct _ClawsSpellEntryPriv));
@@ -128,10 +144,17 @@ static void claws_spell_entry_finalize(GObject *object)
        G_OBJECT_CLASS(parent_class)->finalize(object);
 }
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
 static void claws_spell_entry_destroy(GtkObject *object)
 {
        GTK_OBJECT_CLASS(parent_class)->destroy(object);
 }
+#else
+static void claws_spell_entry_destroy(GtkWidget *object)
+{
+       GTK_WIDGET_CLASS(parent_class)->destroy(object);
+}
+#endif
 
 GtkWidget *claws_spell_entry_new(void)
 {
@@ -141,7 +164,6 @@ GtkWidget *claws_spell_entry_new(void)
 void claws_spell_entry_set_gtkaspell(ClawsSpellEntry *entry, GtkAspell *gtkaspell)
 {
        cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry));
-       cm_return_if_fail(gtkaspell != NULL);
 
        entry->gtkaspell = gtkaspell;
 }
@@ -249,7 +271,7 @@ static gboolean word_misspelled(ClawsSpellEntry *entry, int start, int end)
        gboolean ret;
 
        word = get_word(entry, start, end);
-       if (word == NULL)
+       if (word == NULL || g_unichar_isdigit(word[0]))
                return FALSE;
                
        ret = gtkaspell_misspelled_test(entry->gtkaspell, word);
@@ -258,6 +280,29 @@ static gboolean word_misspelled(ClawsSpellEntry *entry, int start, int end)
        return ret;
 }
 
+static gboolean is_word_end (GtkEntry *entry, const int offset)
+{
+       gchar *p = gtk_editable_get_chars(GTK_EDITABLE(entry), offset, offset+1);
+       gunichar ch;
+       
+       ch = g_utf8_get_char(p);
+       g_free(p);
+       
+       if (ch == '\0')
+               return TRUE;
+
+       if (ch == '\'') {
+               p = gtk_editable_get_chars(GTK_EDITABLE(entry), offset+1, offset+2);
+               ch = g_utf8_get_char(p);
+               g_free(p);
+               
+               return (g_unichar_isspace(ch) || g_unichar_ispunct(ch)
+                       || g_unichar_isdigit(ch));
+       }
+       
+       return (g_unichar_isspace(ch) || g_unichar_ispunct(ch));
+}
+
 static void entry_strsplit_utf8(GtkEntry *entry, gchar ***set, gint **starts, gint **ends)
 {
        PangoLayout   *layout;
@@ -287,7 +332,7 @@ static void entry_strsplit_utf8(GtkEntry *entry, gchar ***set, gint **starts, gi
 
                        /* Find the end of this string */
                        cend = i;
-                       while (!(log_attrs[cend].is_word_end))
+                       while (!is_word_end(entry, cend))
                                cend++;
 
                        /* Copy sub-string */
@@ -405,7 +450,7 @@ void claws_spell_entry_recheck_all(ClawsSpellEntry *entry)
        layout = gtk_entry_get_layout(GTK_ENTRY(entry));
        pango_layout_set_attributes(layout, entry->priv->attr_list);
 
-       if (GTK_WIDGET_REALIZED(GTK_WIDGET(entry))) {
+       if (gtkut_widget_get_realized(GTK_WIDGET(entry))) {
                rect.x = 0; rect.y = 0;
                rect.width  = GTK_WIDGET(entry)->allocation.width;
                rect.height = GTK_WIDGET(entry)->allocation.height;
@@ -413,7 +458,11 @@ void claws_spell_entry_recheck_all(ClawsSpellEntry *entry)
        }
 }
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
 static gint claws_spell_entry_expose(GtkWidget *widget, GdkEventExpose *event)
+#else
+static gint claws_spell_entry_expose(GtkWidget *widget, cairo_t *cr)
+#endif
 {
        ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget);
        GtkEntry *gtk_entry = GTK_ENTRY(widget);
@@ -424,7 +473,11 @@ static gint claws_spell_entry_expose(GtkWidget *widget, GdkEventExpose *event)
                pango_layout_set_attributes(layout, entry->priv->attr_list);
        }
 
+#if !GTK_CHECK_VERSION(3, 0, 0)
        return GTK_WIDGET_CLASS(parent_class)->expose_event (widget, event);
+#else
+       return GTK_WIDGET_CLASS(parent_class)->draw (widget, cr);
+#endif
 }
 
 static gint claws_spell_entry_button_press(GtkWidget *widget, GdkEventButton *event)