talons

Fork of Claws Mail https://www.claws-mail
Log | Files | Refs | README | LICENSE

commit 9166f8a580b95095229975d2d33332d4ea921df8
parent 730f6e21f42b49c6bd6e33bc36787b77d4ba96d3
Author: Andrej Kacian <ticho@claws-mail.org>
Date:   Tue, 23 Oct 2018 20:46:32 +0200

Use GTK private struct feature correctly in ClawsSpellEntry.

g_type_class_add_private() takes care of adding,
initializing and cleaning it up, there is no need
to add another copy of it and manage it manually.

Diffstat:
Msrc/gtk/spell_entry.c | 142++++++++++++++++++++++++++++++++++++++++----------------------------------------
Msrc/gtk/spell_entry.h | 2--
2 files changed, 71 insertions(+), 73 deletions(-)

diff --git a/src/gtk/spell_entry.c b/src/gtk/spell_entry.c @@ -44,7 +44,6 @@ #include "gtkutils.h" static void claws_spell_entry_init (ClawsSpellEntry *entry); -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, @@ -67,7 +66,7 @@ static void claws_spell_entry_preedit_changed (GtkEntry *entry, gchar *preedit, gpointer data); -struct _ClawsSpellEntryPriv +typedef struct { PangoAttrList *attr_list; gint mark_character; @@ -75,7 +74,11 @@ struct _ClawsSpellEntryPriv gint *word_starts; gint *word_ends; gint preedit_length; -}; +} ClawsSpellEntryPrivate; + +#define CLAWS_SPELL_ENTRY_GET_PRIVATE(entry) \ + G_TYPE_INSTANCE_GET_PRIVATE (entry, CLAWS_TYPE_SPELL_ENTRY, \ + ClawsSpellEntryPrivate) static GtkEntryClass *parent_class = NULL; @@ -93,9 +96,6 @@ static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass) parent_class = g_type_class_peek_parent(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; @@ -109,18 +109,20 @@ static void claws_spell_entry_class_init(ClawsSpellEntryClass *klass) widget_class->draw = claws_spell_entry_expose; widget_class->destroy = claws_spell_entry_destroy; #endif - + + g_object_class = G_OBJECT_CLASS(klass); g_type_class_add_private(g_object_class, - sizeof(struct _ClawsSpellEntryPriv)); + sizeof(ClawsSpellEntryPrivate)); } static void claws_spell_entry_init(ClawsSpellEntry *entry) { + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); + entry->gtkaspell = NULL; - entry->priv = g_new0(ClawsSpellEntryPriv, 1); - entry->priv->attr_list = pango_attr_list_new(); - entry->priv->preedit_length = 0; + priv->attr_list = pango_attr_list_new(); + priv->preedit_length = 0; g_signal_connect(G_OBJECT(entry), "popup-menu", G_CALLBACK(claws_spell_entry_popup_menu), entry); @@ -132,23 +134,6 @@ static void claws_spell_entry_init(ClawsSpellEntry *entry) G_CALLBACK(claws_spell_entry_preedit_changed), NULL); } -static void claws_spell_entry_finalize(GObject *object) -{ - ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(object); - - if (entry->priv->attr_list) - pango_attr_list_unref(entry->priv->attr_list); - if (entry->priv->words) - g_strfreev(entry->priv->words); - - g_free(entry->priv->word_starts); - g_free(entry->priv->word_ends); - g_free(entry->priv); - entry->priv = NULL; - - G_OBJECT_CLASS(parent_class)->finalize(object); -} - #if !GTK_CHECK_VERSION(3, 0, 0) static void claws_spell_entry_destroy(GtkObject *object) { @@ -184,6 +169,7 @@ static gint claws_spell_entry_find_position (ClawsSpellEntry *_entry, gint x) gint scroll_offset; gboolean trailing; GtkEntry *entry = GTK_ENTRY(_entry); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); g_object_get(entry, "scroll-offset", &scroll_offset, NULL); x = x + scroll_offset; @@ -196,9 +182,9 @@ static gint claws_spell_entry_find_position (ClawsSpellEntry *_entry, gint x) line = pango_layout_get_lines(layout)->data; pango_layout_line_x_to_index(line, x * PANGO_SCALE, &index, &trailing); - if (index >= cursor_index && _entry->priv->preedit_length) { - if (index >= cursor_index + _entry->priv->preedit_length) { - index -= _entry->priv->preedit_length; + if (index >= cursor_index && priv->preedit_length) { + if (index >= cursor_index + priv->preedit_length) { + index -= priv->preedit_length; } else { index = cursor_index; trailing = FALSE; @@ -216,21 +202,22 @@ static void get_word_extents_from_position(ClawsSpellEntry *entry, gint *start, { const gchar *text; gint i, bytes_pos; + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); *start = -1; *end = -1; - if (entry->priv->words == NULL) + if (priv->words == NULL) return; text = gtk_entry_get_text(GTK_ENTRY(entry)); bytes_pos = (gint) (g_utf8_offset_to_pointer(text, position) - text); - for (i = 0; entry->priv->words[i]; i++) { - if (bytes_pos >= entry->priv->word_starts[i] && - bytes_pos <= entry->priv->word_ends[i]) { - *start = entry->priv->word_starts[i]; - *end = entry->priv->word_ends[i]; + for (i = 0; priv->words[i]; i++) { + if (bytes_pos >= priv->word_starts[i] && + bytes_pos <= priv->word_ends[i]) { + *start = priv->word_starts[i]; + *end = priv->word_ends[i]; return; } } @@ -255,6 +242,7 @@ static void replace_word(ClawsSpellEntry *entry, const gchar *newword) { gint cursor, start_pos, end_pos; const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry)); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); start_pos = entry->gtkaspell->start_pos; end_pos = entry->gtkaspell->end_pos; @@ -263,9 +251,9 @@ static void replace_word(ClawsSpellEntry *entry, const gchar *newword) /* is the cursor at the end? If so, restore it there */ if (g_utf8_strlen(text, -1) == cursor) cursor = -1; - else if(cursor < entry->priv->mark_character || - cursor > entry->priv->mark_character) - cursor = entry->priv->mark_character; + else if(cursor < priv->mark_character || + cursor > priv->mark_character) + cursor = priv->mark_character; gtk_editable_delete_text(GTK_EDITABLE(entry), start_pos, end_pos); gtk_editable_insert_text(GTK_EDITABLE(entry), newword, strlen(newword), @@ -362,6 +350,7 @@ static void entry_strsplit_utf8(GtkEntry *entry, gchar ***set, gint **starts, gi static void insert_misspelled_marker(ClawsSpellEntry *entry, guint start, guint end) { + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); guint16 red = (guint16) (((gdouble)((prefs_common.color[COL_MISSPELLED] & 0xff0000) >> 16) / 255.0) * 65535.0); guint16 green = (guint16) (((gdouble)((prefs_common.color[COL_MISSPELLED] & @@ -375,7 +364,7 @@ static void insert_misspelled_marker(ClawsSpellEntry *entry, guint start, guint fcolor->start_index = start; fcolor->end_index = end; - pango_attr_list_insert(entry->priv->attr_list, fcolor); + pango_attr_list_insert(priv->attr_list, fcolor); } else { ucolor = pango_attr_underline_color_new (65535, 0, 0); unline = pango_attr_underline_new (PANGO_UNDERLINE_ERROR); @@ -386,14 +375,15 @@ static void insert_misspelled_marker(ClawsSpellEntry *entry, guint start, guint ucolor->end_index = end; unline->end_index = end; - pango_attr_list_insert (entry->priv->attr_list, ucolor); - pango_attr_list_insert (entry->priv->attr_list, unline); + pango_attr_list_insert (priv->attr_list, ucolor); + pango_attr_list_insert (priv->attr_list, unline); } } static gboolean check_word(ClawsSpellEntry *entry, int start, int end) { GtkAspell *gtkaspell = entry->gtkaspell; + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); PangoAttrIterator *it; gint s, e; gboolean misspelled; @@ -402,7 +392,7 @@ static gboolean check_word(ClawsSpellEntry *entry, int start, int end) /* Check to see if we've got any attributes at this position. * If so, free them, since we'll readd it if the word is misspelled */ - it = pango_attr_list_get_iterator(entry->priv->attr_list); + it = pango_attr_list_get_iterator(priv->attr_list); if (it == NULL) return FALSE; do { @@ -437,28 +427,29 @@ void claws_spell_entry_recheck_all(ClawsSpellEntry *entry) GtkAllocation allocation; GdkRectangle rect; PangoLayout *layout; + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); int length, i; cm_return_if_fail(CLAWS_IS_SPELL_ENTRY(entry)); cm_return_if_fail(entry->gtkaspell != NULL); - if (entry->priv->words == NULL) + if (priv->words == NULL) return; /* Remove all existing pango attributes. These will get readded as we check */ - pango_attr_list_unref(entry->priv->attr_list); - entry->priv->attr_list = pango_attr_list_new(); + pango_attr_list_unref(priv->attr_list); + priv->attr_list = pango_attr_list_new(); /* Loop through words */ - for (i = 0; entry->priv->words[i]; i++) { - length = strlen(entry->priv->words[i]); + for (i = 0; priv->words[i]; i++) { + length = strlen(priv->words[i]); if (length == 0) continue; - check_word(entry, entry->priv->word_starts[i], entry->priv->word_ends[i]); + check_word(entry, priv->word_starts[i], priv->word_ends[i]); } layout = gtk_entry_get_layout(GTK_ENTRY(entry)); - pango_layout_set_attributes(layout, entry->priv->attr_list); + pango_layout_set_attributes(layout, priv->attr_list); if (gtk_widget_get_realized(GTK_WIDGET(entry))) { rect.x = 0; rect.y = 0; @@ -477,12 +468,13 @@ static gint claws_spell_entry_expose(GtkWidget *widget, cairo_t *cr) #endif { ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); GtkEntry *gtk_entry = GTK_ENTRY(widget); PangoLayout *layout; if (entry->gtkaspell != NULL) { layout = gtk_entry_get_layout(gtk_entry); - pango_layout_set_attributes(layout, entry->priv->attr_list); + pango_layout_set_attributes(layout, priv->attr_list); } #if !GTK_CHECK_VERSION(3, 0, 0) @@ -495,17 +487,20 @@ static gint claws_spell_entry_expose(GtkWidget *widget, cairo_t *cr) static gint claws_spell_entry_button_press(GtkWidget *widget, GdkEventButton *event) { ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(widget); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); gint pos; pos = claws_spell_entry_find_position(entry, event->x); - entry->priv->mark_character = pos; + priv->mark_character = pos; return GTK_WIDGET_CLASS(parent_class)->button_press_event (widget, event); } static gboolean claws_spell_entry_popup_menu(GtkWidget *widget, ClawsSpellEntry *entry) { - entry->priv->mark_character = gtk_editable_get_position (GTK_EDITABLE (entry)); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); + + priv->mark_character = gtk_editable_get_position (GTK_EDITABLE (entry)); return FALSE; } @@ -517,13 +512,14 @@ static void set_position(gpointer data, gint pos) static gboolean find_misspelled_cb(gpointer data, gboolean forward) { ClawsSpellEntry *entry = (ClawsSpellEntry *)data; + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); GtkAspell *gtkaspell = entry->gtkaspell; gboolean misspelled = FALSE; gint cursor, minpos, maxpos, i, words_len = 0; gint start, end; gchar *text; - if (entry->priv->words == NULL) + if (priv->words == NULL) return FALSE; gtkaspell->orig_pos = gtk_editable_get_position(GTK_EDITABLE(entry)); @@ -539,22 +535,22 @@ static gboolean find_misspelled_cb(gpointer data, gboolean forward) } g_free(text); - while(entry->priv->words[words_len]) + while(priv->words[words_len]) words_len++; if (forward) { for(i=0; i < words_len; i++) - if (entry->priv->word_ends[i] > minpos && + if (priv->word_ends[i] > minpos && (misspelled = check_word(entry, - entry->priv->word_starts[i], - entry->priv->word_ends[i]))) + priv->word_starts[i], + priv->word_ends[i]))) break; } else { for(i=words_len-1; i >= 0; i--) - if (entry->priv->word_starts[i] < maxpos && + if (priv->word_starts[i] < maxpos && (misspelled = check_word(entry, - entry->priv->word_starts[i], - entry->priv->word_ends[i]))) + priv->word_starts[i], + priv->word_ends[i]))) break; } @@ -564,9 +560,10 @@ static gboolean find_misspelled_cb(gpointer data, gboolean forward) static gboolean check_word_cb(gpointer data) { ClawsSpellEntry *entry = (ClawsSpellEntry *)data; + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); gint start, end; - get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character); + get_word_extents_from_position(entry, &start, &end, priv->mark_character); return check_word(entry, start, end); } @@ -625,13 +622,14 @@ static void claws_spell_entry_populate_popup(ClawsSpellEntry *entry, GtkMenu *me gpointer data) { GtkAspell *gtkaspell = entry->gtkaspell; + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); gint start, end; gchar *word, *text; if (gtkaspell == NULL) return; - get_word_extents_from_position(entry, &start, &end, entry->priv->mark_character); + get_word_extents_from_position(entry, &start, &end, priv->mark_character); if ((word = get_word(entry, start, end)) != NULL) { strncpy(gtkaspell->theword, word, GTKASPELLWORDSIZE - 1); @@ -652,17 +650,18 @@ static void claws_spell_entry_populate_popup(ClawsSpellEntry *entry, GtkMenu *me static void claws_spell_entry_changed(GtkEditable *editable, gpointer data) { ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(editable); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); if (entry->gtkaspell == NULL) return; - if (entry->priv->words) { - g_strfreev(entry->priv->words); - g_free(entry->priv->word_starts); - g_free(entry->priv->word_ends); + if (priv->words) { + g_strfreev(priv->words); + g_free(priv->word_starts); + g_free(priv->word_ends); } - entry_strsplit_utf8(GTK_ENTRY(entry), &entry->priv->words, - &entry->priv->word_starts, &entry->priv->word_ends); + entry_strsplit_utf8(GTK_ENTRY(entry), &priv->words, + &priv->word_starts, &priv->word_ends); if(entry->gtkaspell->check_while_typing == TRUE) claws_spell_entry_recheck_all(entry); } @@ -672,8 +671,9 @@ static void claws_spell_entry_preedit_changed (GtkEntry *_entry, gpointer data) { ClawsSpellEntry *entry = CLAWS_SPELL_ENTRY(_entry); + ClawsSpellEntryPrivate *priv = CLAWS_SPELL_ENTRY_GET_PRIVATE(entry); - entry->priv->preedit_length = preedit != NULL ? strlen(preedit) : 0; + priv->preedit_length = preedit != NULL ? strlen(preedit) : 0; } static void continue_check(gpointer *data) diff --git a/src/gtk/spell_entry.h b/src/gtk/spell_entry.h @@ -33,7 +33,6 @@ G_BEGIN_DECLS typedef struct _ClawsSpellEntry ClawsSpellEntry; typedef struct _ClawsSpellEntryClass ClawsSpellEntryClass; -typedef struct _ClawsSpellEntryPriv ClawsSpellEntryPriv; #include <gtk/gtk.h> #include "gtkaspell.h" @@ -47,7 +46,6 @@ struct _ClawsSpellEntry { GtkEntry parent_object; - ClawsSpellEntryPriv *priv; GtkAspell *gtkaspell; void (*gtk_reserved1)(void);