X-Git-Url: http://git.maemo.org/git/?p=modest;a=blobdiff_plain;f=src%2Fwidgets%2Fmodest-recpt-editor.c;h=4ef9fc9a077cd0177d0fa59c44c7094a0f304ceb;hp=e77a981eb7db4dd527f9fea9ad409317c020420a;hb=ecd20f1229f24b99f53f395dc7341984c128a340;hpb=c82fe05ac5c60ec3b67d0dc59de20ffb40219695 diff --git a/src/widgets/modest-recpt-editor.c b/src/widgets/modest-recpt-editor.c index e77a981..4ef9fc9 100644 --- a/src/widgets/modest-recpt-editor.c +++ b/src/widgets/modest-recpt-editor.c @@ -44,6 +44,12 @@ #include #include +/* FIXNE: we should have no maemo-deps in widgets/ */ +#ifndef MODEST_TOOLKIT_GTK +#include "maemo/modest-hildon-includes.h" +#endif /*!MODEST_TOOLKIT_GTK*/ + + static GObjectClass *parent_class = NULL; #define RECIPIENT_TAG_ID "recpt-id" @@ -100,6 +106,8 @@ static GtkTextTag *prev_iter_has_recipient (GtkTextIter *iter); /* static GtkTextTag *next_iter_has_recipient (GtkTextIter *iter); */ static void select_tag_of_iter (GtkTextIter *iter, GtkTextTag *tag); static gboolean quote_opened (GtkTextIter *iter); +static gboolean is_valid_insert (const gchar *text, gint len); +static gchar *create_valid_text (const gchar *text, gint len); /** * modest_recpt_editor_new: @@ -122,14 +130,17 @@ modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar { ModestRecptEditorPrivate *priv; GtkTextBuffer *buffer = NULL; + gchar *valid_recipients = NULL; g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor)); priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view)); + valid_recipients = create_valid_text (recipients, -1); g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor); - gtk_text_buffer_set_text (buffer, recipients, -1); + gtk_text_buffer_set_text (buffer, valid_recipients, -1); + g_free (valid_recipients); if (GTK_WIDGET_REALIZED (recpt_editor)) gtk_widget_queue_resize (GTK_WIDGET (recpt_editor)); g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor); @@ -142,7 +153,7 @@ modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar ModestRecptEditorPrivate *priv; GtkTextBuffer *buffer = NULL; GtkTextIter iter; - gchar * string_to_add; + gchar *string_to_add; g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor)); priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor); @@ -162,13 +173,14 @@ modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor); - gtk_text_buffer_insert (buffer, &iter, recipients, -1); + gtk_text_buffer_insert (buffer, &iter, string_to_add, -1); g_signal_handlers_unblock_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor); if (GTK_WIDGET_REALIZED (recpt_editor)) gtk_widget_queue_resize (GTK_WIDGET (recpt_editor)); + g_free (string_to_add); } void @@ -328,8 +340,15 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class) gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon); priv->text_view = gtk_text_view_new (); + /* Auto-capitalization is the default, so let's turn it off: */ +#ifdef MAEMO_CHANGES + hildon_gtk_text_view_set_input_mode (GTK_TEXT_VIEW (priv->text_view), + HILDON_GTK_INPUT_MODE_FULL); +#endif - priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 5); + priv->recipients = NULL; + + priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 1024); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (priv->scrolled_window), GTK_SHADOW_IN); @@ -425,7 +444,11 @@ modest_recpt_editor_on_button_release_event (GtkWidget *widget, selection_changed = TRUE; } - gtk_text_buffer_select_range (buffer, &start, &end); + if (selection_changed) { + gtk_text_buffer_select_range (buffer, &start, &end); + } else { + GTK_TEXT_VIEW (priv->text_view)->pending_place_cursor_button = 0; + } return FALSE; } @@ -439,6 +462,84 @@ modest_recpt_editor_on_focus_in (GtkTextView *text_view, gtk_text_view_place_cursor_onscreen (GTK_TEXT_VIEW (priv->text_view)); } +static gboolean +is_valid_insert (const gchar *text, gint len) +{ + gunichar c; + gunichar next_c; + gint i= 0; + gboolean quoted = FALSE; + const gchar *current, *next_current; + if (text == NULL) + return TRUE; + current = text; + + while (((len == -1)||(i < len)) && (*current != '\0')) { + c = g_utf8_get_char (current); + next_current = g_utf8_next_char (current); + if (next_current && *next_current != '\0') + next_c = g_utf8_get_char (g_utf8_next_char (current)); + else + next_c = 0; + if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) { + if ((next_c != 0) && (next_c != g_utf8_get_char ("\n"))) + return FALSE; + else { + current = g_utf8_next_char (next_current); + continue; + } + } + if (c == 0x2022 || c == 0xfffc || + c == g_utf8_get_char ("\n") || + c == g_utf8_get_char ("\t")) + return FALSE; + if (c == g_utf8_get_char ("\"")) + quoted = !quoted; + current = g_utf8_next_char (current); + i = current - text; + } + return TRUE; +} + +static gchar * +create_valid_text (const gchar *text, gint len) +{ + gunichar c; + gunichar next_c; + gint i= 0; + GString *str; + gboolean quoted = FALSE; + const gchar *current, *next_current; + + if (text == NULL) + return NULL; + + str = g_string_new (""); + current = text; + + while (((len == -1)||(i < len)) && (*current != '\0')) { + c = g_utf8_get_char (current); + next_current = g_utf8_next_char (current); + if (next_current && *next_current != '\0') + next_c = g_utf8_get_char (g_utf8_next_char (current)); + else + next_c = 0; + if (c != 0x2022 && c != 0xfffc && + c != g_utf8_get_char ("\n") && + c != g_utf8_get_char ("\t")) + str = g_string_append_unichar (str, c); + if (!quoted && ((c == g_utf8_get_char(",") || c == g_utf8_get_char (";")))) { + if ((next_c != 0) && (next_c != g_utf8_get_char ("\n"))) + str = g_string_append_c (str, '\n'); + } + if (c == g_utf8_get_char ("\"")) + quoted = !quoted; + current = g_utf8_next_char (current); + i = current - text; + } + return g_string_free (str, FALSE); +} + static void modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer, GtkTextIter *location, @@ -449,6 +550,22 @@ modest_recpt_editor_on_insert_text (GtkTextBuffer *buffer, GtkTextIter prev; gunichar prev_char; ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor); + + if (len > 1024) + len = 1024; + + if (!is_valid_insert (text, len)) { + gchar *new_text = create_valid_text (text, len); + g_signal_stop_emission_by_name (G_OBJECT (buffer), "insert-text"); + g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, + editor); + gtk_text_buffer_insert (buffer, location, new_text, -1); + g_signal_handlers_unblock_by_func (buffer, + modest_recpt_editor_on_insert_text, + editor); + g_free (new_text); + return; + } if (iter_has_recipient (location)) { gtk_text_buffer_get_end_iter (buffer, location); @@ -580,12 +697,14 @@ modest_recpt_editor_on_key_press_event (GtkTextView *text_view, ModestRecptEditor *editor) { GtkTextMark *insert; + GtkTextMark *selection; GtkTextBuffer * buffer; - GtkTextIter location; + GtkTextIter location, selection_loc; GtkTextTag *tag; buffer = gtk_text_view_get_buffer (text_view); insert = gtk_text_buffer_get_insert (buffer); + selection = gtk_text_buffer_get_selection_bound (buffer); /* cases to cover: * * cursor is on resolved recipient: @@ -607,6 +726,7 @@ modest_recpt_editor_on_key_press_event (GtkTextView *text_view, */ gtk_text_buffer_get_iter_at_mark (buffer, &location, insert); + gtk_text_buffer_get_iter_at_mark (buffer, &selection_loc, selection); switch (key->keyval) { case GDK_Left: @@ -676,7 +796,12 @@ modest_recpt_editor_on_key_press_event (GtkTextView *text_view, case GDK_Return: case GDK_KP_Enter: { + gint insert_offset, selection_offset; + insert_offset = gtk_text_iter_get_offset (&location); + selection_offset = gtk_text_iter_get_offset (&selection_loc); g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, editor); + if (selection_offset > insert_offset) + location = selection_loc; tag = iter_has_recipient (&location); if (tag != NULL) { gtk_text_buffer_get_end_iter (buffer, &location); @@ -755,9 +880,28 @@ modest_recpt_editor_grab_focus (ModestRecptEditor *recpt_editor) gtk_widget_grab_focus (priv->text_view); } +gboolean +modest_recpt_editor_has_focus (ModestRecptEditor *recpt_editor) +{ + ModestRecptEditorPrivate *priv; + + g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), FALSE); + priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor); + + return gtk_widget_is_focus (priv->text_view); +} + static void modest_recpt_editor_finalize (GObject *object) { + ModestRecptEditorPrivate *priv; + priv = MODEST_RECPT_EDITOR_GET_PRIVATE (object); + + if (priv->recipients) { + g_free (priv->recipients); + priv->recipients = NULL; + } + (*parent_class->finalize) (object); return;