This is a manual merge of branch drop split view intro trunk.
[modest] / src / widgets / modest-recpt-editor.c
index 87aaf6d..97e0ddd 100644 (file)
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtk.h>
 
+/* FIXNE: we should have no maemo-deps in widgets/ */
+#ifndef MODEST_TOOLKIT_GTK
+#include "modest-hildon-includes.h"
+#endif /*!MODEST_TOOLKIT_GTK*/
+
+
 static GObjectClass *parent_class = NULL;
 
 #define RECIPIENT_TAG_ID "recpt-id"
@@ -129,7 +135,11 @@ modest_recpt_editor_set_recipients (ModestRecptEditor *recpt_editor, const gchar
        g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
 
        valid_recipients = create_valid_text (recipients, -1);
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
@@ -147,7 +157,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);
@@ -155,7 +165,11 @@ modest_recpt_editor_add_recipients (ModestRecptEditor *recpt_editor, const gchar
        if (recipients == NULL)
                return;
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
 
        if (gtk_text_buffer_get_char_count (buffer) > 0) {
                string_to_add = g_strconcat (";\n", recipients, NULL);
@@ -167,13 +181,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 
@@ -189,7 +204,11 @@ modest_recpt_editor_add_resolved_recipient (ModestRecptEditor *recpt_editor, GSL
        g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
 
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
        gtk_text_buffer_get_bounds (buffer, &start, &end);
@@ -250,7 +269,11 @@ modest_recpt_editor_replace_with_resolved_recipient (ModestRecptEditor *recpt_ed
        g_return_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor));
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
        g_signal_handlers_block_by_func (buffer, modest_recpt_editor_on_insert_text, recpt_editor);
 
        gtk_text_buffer_delete (buffer, start, end);
@@ -299,7 +322,11 @@ modest_recpt_editor_get_recipients (ModestRecptEditor *recpt_editor)
                priv->recipients = NULL;
        }
 
+#ifdef MODEST_TOOKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
 
        gtk_text_buffer_get_start_iter (buffer, &start);
        gtk_text_buffer_get_end_iter (buffer, &end);
@@ -329,13 +356,31 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
        gtk_button_set_focus_on_click (GTK_BUTTON (priv->abook_button), FALSE);
        GTK_WIDGET_UNSET_FLAGS (priv->abook_button, GTK_CAN_FOCUS);
        gtk_button_set_alignment (GTK_BUTTON (priv->abook_button), 1.0, 1.0);
+#ifdef MODEST_TOOLKIT_HILDON2
+       abook_icon = gtk_image_new_from_icon_name ("general_contacts", HILDON_ICON_SIZE_FINGER);
+#else
        abook_icon = gtk_image_new_from_icon_name ("qgn_list_addressbook", GTK_ICON_SIZE_BUTTON);
+#endif
        gtk_container_add (GTK_CONTAINER (priv->abook_button), abook_icon);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       priv->text_view = hildon_text_view_new ();
+#else
        priv->text_view = gtk_text_view_new ();
+#endif
+       /* 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->recipients = NULL;
 
-       priv->scrolled_window = modest_scroll_text_new (GTK_TEXT_VIEW (priv->text_view), 5);
+#ifdef MODEST_TOOLKIT_HILDON2
+       priv->scrolled_window = NULL;
+       gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0);
+#else
+       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);
@@ -343,6 +388,7 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
 
        gtk_box_pack_start (GTK_BOX (instance), priv->scrolled_window, TRUE, TRUE, 0);
 /*     gtk_box_pack_start (GTK_BOX (instance), priv->text_view, TRUE, TRUE, 0); */
+#endif
        gtk_box_pack_end (GTK_BOX (instance), priv->abook_button, FALSE, FALSE, 0);
 
        gtk_text_view_set_accepts_tab (GTK_TEXT_VIEW (priv->text_view), FALSE);
@@ -354,7 +400,11 @@ modest_recpt_editor_instance_init (GTypeInstance *instance, gpointer g_class)
 
        gtk_widget_set_size_request (priv->text_view, 75, -1);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
        g_signal_connect (G_OBJECT (priv->abook_button), "clicked", G_CALLBACK (modest_recpt_editor_on_abook_clicked), instance);
        g_signal_connect (G_OBJECT (priv->text_view), "button-release-event", G_CALLBACK (modest_recpt_editor_on_button_release_event), instance);
        g_signal_connect (G_OBJECT (priv->text_view), "key-press-event", G_CALLBACK (modest_recpt_editor_on_key_press_event), instance);
@@ -375,7 +425,11 @@ modest_recpt_editor_set_field_size_group (ModestRecptEditor *recpt_editor, GtkSi
        g_return_if_fail (GTK_IS_SIZE_GROUP (size_group));
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       gtk_size_group_add_widget (size_group, priv->text_view);
+#else
        gtk_size_group_add_widget (size_group, priv->scrolled_window);
+#endif
 }
 
 GtkTextBuffer *
@@ -386,7 +440,11 @@ modest_recpt_editor_get_buffer (ModestRecptEditor *recpt_editor)
        g_return_val_if_fail (MODEST_IS_RECPT_EDITOR (recpt_editor), NULL);
        priv = MODEST_RECPT_EDITOR_GET_PRIVATE (recpt_editor);
 
+#ifdef MODEST_TOOLKIT_HILDON2
+       return hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        return gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
 }
 
 static void
@@ -431,7 +489,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;
 }
@@ -533,11 +595,19 @@ 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;
        }
@@ -672,12 +742,18 @@ 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;
      
+#ifdef MODEST_TOOLKIT_HILDON2
+       buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (text_view));
+#else
        buffer = gtk_text_view_get_buffer (text_view);
+#endif
        insert = gtk_text_buffer_get_insert (buffer);
+       selection = gtk_text_buffer_get_selection_bound (buffer);
 
        /* cases to cover:
         *    * cursor is on resolved recipient:
@@ -699,6 +775,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:
@@ -768,7 +845,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);
@@ -826,7 +908,11 @@ static void
 modest_recpt_editor_move_cursor_to_end (ModestRecptEditor *editor)
 {
        ModestRecptEditorPrivate *priv = MODEST_RECPT_EDITOR_GET_PRIVATE (editor);
+#ifdef MODEST_TOOLKIT_HILDON2
+       GtkTextBuffer *buffer = hildon_text_view_get_buffer (HILDON_TEXT_VIEW (priv->text_view));
+#else
        GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->text_view));
+#endif
        GtkTextIter start, end;
 
        gtk_text_buffer_get_end_iter (buffer, &start);
@@ -847,6 +933,17 @@ 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)
 {