* src/maemo/modest-msg-edit-window.c:
[modest] / src / maemo / modest-msg-edit-window.c
index fc777c2..28aaef3 100644 (file)
@@ -82,7 +82,7 @@
 #define DEFAULT_SIZE_COMBOBOX_WIDTH 80
 #define DEFAULT_MAIN_VBOX_SPACING 6
 #define SUBJECT_MAX_LENGTH 1000
-#define IMAGE_MAX_WIDTH 608
+#define IMAGE_MAX_WIDTH 560
 #define DEFAULT_FONT_SCALE 1.5
 
 static void  modest_msg_edit_window_class_init   (ModestMsgEditWindowClass *klass);
@@ -120,7 +120,8 @@ static void modest_msg_edit_window_add_attachment_clicked (GtkButton *button,
                                                           ModestMsgEditWindow *window);
 
 /* ModestWindow methods implementation */
-static void  modest_msg_edit_window_set_zoom (ModestWindow *window, gdouble zoom);
+static void modest_msg_edit_window_disconnect_signals (ModestWindow *window);
+static void modest_msg_edit_window_set_zoom (ModestWindow *window, gdouble zoom);
 static gdouble modest_msg_edit_window_get_zoom (ModestWindow *window);
 static gboolean modest_msg_edit_window_zoom_minus (ModestWindow *window);
 static gboolean modest_msg_edit_window_zoom_plus (ModestWindow *window);
@@ -238,6 +239,7 @@ struct _ModestMsgEditWindowPrivate {
 
        GtkWidget   *scroll;
        GtkWidget   *scroll_area;
+       gint        last_vadj_upper;
 
        gint last_cid;
        GList *attachments;
@@ -321,10 +323,9 @@ modest_msg_edit_window_class_init (ModestMsgEditWindowClass *klass)
        modest_window_class->zoom_minus_func = modest_msg_edit_window_zoom_minus;
        modest_window_class->show_toolbar_func = modest_msg_edit_window_show_toolbar;
        modest_window_class->save_state_func = save_state;
+       modest_window_class->disconnect_signals_func = modest_msg_edit_window_disconnect_signals;
 
        g_type_class_add_private (gobject_class, sizeof(ModestMsgEditWindowPrivate));
-
-
 }
 
 static void
@@ -356,6 +357,8 @@ modest_msg_edit_window_init (ModestMsgEditWindow *obj)
        priv->outbox_msg = NULL;
        priv->clipboard_change_handler_id = 0;
        priv->sent = FALSE;
+
+       priv->last_vadj_upper = 0;
 }
 
 
@@ -396,6 +399,40 @@ get_transports (void)
        return transports;
 }
 
+void vadj_changed (GtkAdjustment *adj,
+                  ModestMsgEditWindow *window)
+{
+       ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
+
+       GdkRectangle rectangle, cursor_rectangle;
+       GtkTextIter position;
+       gboolean visible;
+       gint cursor_bottom;
+
+       /* We detect if cursor is visible using the full height, not only the center. This
+          seems to work */
+       gtk_text_view_get_visible_rect (GTK_TEXT_VIEW (priv->msg_body), &rectangle);
+       gtk_text_buffer_get_iter_at_mark (GTK_TEXT_BUFFER (priv->text_buffer),
+                                         &position,
+                                         gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer)));
+       gtk_text_view_get_iter_location (GTK_TEXT_VIEW (priv->msg_body), &position, &cursor_rectangle);
+
+       cursor_bottom = (cursor_rectangle.y + cursor_rectangle.height);
+       visible = (cursor_rectangle.y >= rectangle.y) && (cursor_bottom < (rectangle.y + rectangle.height));
+
+       if (gtk_widget_is_focus (priv->msg_body) && 
+           !visible) {
+               if (priv->last_vadj_upper != adj->upper) {
+                       GtkTextMark *insert;
+                       
+                       insert = gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (priv->text_buffer));
+                       gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (priv->msg_body), 
+                                                     insert, 0.1, FALSE, 0.0, 0.0);
+               }
+       }
+       priv->last_vadj_upper = adj->upper;
+}
+
 
 static void
 init_window (ModestMsgEditWindow *obj)
@@ -540,6 +577,10 @@ init_window (ModestMsgEditWindow *obj)
 
        gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (priv->scroll), main_vbox);
        gtk_container_set_focus_vadjustment (GTK_CONTAINER (main_vbox), gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scroll)));
+       g_signal_connect (G_OBJECT (gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scroll))),
+                         "changed",
+                         G_CALLBACK (vadj_changed),
+                         obj);
        gtk_widget_show_all (GTK_WIDGET(priv->scroll));
        
        window_box = gtk_vbox_new (FALSE, 0);
@@ -548,25 +589,30 @@ init_window (ModestMsgEditWindow *obj)
        priv->scroll_area = modest_scroll_area_new (priv->scroll, priv->msg_body);
        gtk_container_add (GTK_CONTAINER (frame), priv->scroll_area);
        
-       gtk_container_set_focus_vadjustment (GTK_CONTAINER (priv->scroll_area), 
-                                            gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (priv->scroll)));
-
        priv->clipboard_change_handler_id = g_signal_connect (G_OBJECT (gtk_clipboard_get (GDK_SELECTION_PRIMARY)), "owner-change",
                                                              G_CALLBACK (modest_msg_edit_window_clipboard_owner_change), obj);
 
 }
        
+static void
+modest_msg_edit_window_disconnect_signals (ModestWindow *window)
+{
+       ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (window);
 
+       if (g_signal_handler_is_connected (gtk_clipboard_get (GDK_SELECTION_PRIMARY), 
+                                          priv->clipboard_change_handler_id))
+               g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_PRIMARY), 
+                                            priv->clipboard_change_handler_id);
+}
 
 static void
 modest_msg_edit_window_finalize (GObject *obj)
 {
        ModestMsgEditWindowPrivate *priv = MODEST_MSG_EDIT_WINDOW_GET_PRIVATE (obj);
 
-       if (priv->clipboard_change_handler_id > 0) {
-               g_signal_handler_disconnect (gtk_clipboard_get (GDK_SELECTION_PRIMARY), priv->clipboard_change_handler_id);
-               priv->clipboard_change_handler_id = 0;
-       }
+       /* Sanity check: shouldn't be needed, the window mgr should
+          call this function before */
+       modest_msg_edit_window_disconnect_signals (MODEST_WINDOW (obj));
        
        if (priv->draft_msg != NULL) {
                TnyHeader *header = tny_msg_get_header (priv->draft_msg);
@@ -674,7 +720,7 @@ replace_with_attachments (ModestMsgEditWindow *self, GList *attachments)
                        g_object_unref (stream);
 
                        if (pixbuf != NULL) {
-                               wp_text_buffer_replace_image (WP_TEXT_BUFFER (priv->text_buffer), cid, pixbuf);
+/*                             wp_text_buffer_replace_image (WP_TEXT_BUFFER (priv->text_buffer), cid, pixbuf); */
                                g_object_unref (pixbuf);
                        }
                }
@@ -702,7 +748,7 @@ update_last_cid (ModestMsgEditWindow *self, GList *attachments)
 }
 
 static void
-set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
+set_msg (ModestMsgEditWindow *self, TnyMsg *msg, gboolean preserve_is_rich)
 {
        TnyHeader *header;
        const gchar *to, *cc, *bcc, *subject;
@@ -711,6 +757,7 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
        GtkTextIter iter;
        TnyHeaderFlags priority_flags;
        TnyFolder *msg_folder;
+       gboolean is_html = FALSE;
        
        g_return_if_fail (MODEST_IS_MSG_EDIT_WINDOW (self));
        g_return_if_fail (TNY_IS_MSG (msg));
@@ -750,11 +797,12 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
        update_window_title (self);
 
        wp_text_buffer_reset_buffer (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
-       body = modest_tny_msg_get_body (msg, TRUE);
+       body = modest_tny_msg_get_body (msg, TRUE, &is_html);
 
        if ((body == NULL)||(body[0] == '\0')) {
                g_free (body);
                body = modest_text_utils_convert_to_html ("");
+               is_html = FALSE;
        }
        wp_text_buffer_load_document_begin (WP_TEXT_BUFFER (priv->text_buffer), TRUE);
        wp_text_buffer_load_document_write (WP_TEXT_BUFFER (priv->text_buffer),
@@ -763,8 +811,10 @@ set_msg (ModestMsgEditWindow *self, TnyMsg *msg)
        wp_text_buffer_load_document_end (WP_TEXT_BUFFER (priv->text_buffer));
        g_free (body);
 
+       if (preserve_is_rich && !is_html) {
+               wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
        /* Get the default format required from configuration */
-       if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL)) {
+       } else if (!modest_conf_get_bool (modest_runtime_get_conf (), MODEST_CONF_PREFER_FORMATTED_TEXT, NULL)) {
                wp_text_buffer_enable_rich_text (WP_TEXT_BUFFER (priv->text_buffer), FALSE);
        }
 
@@ -1000,7 +1050,7 @@ modest_msg_edit_window_setup_toolbar (ModestMsgEditWindow *window)
 
 
 ModestWindow*
-modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name)
+modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name, gboolean preserve_is_rich)
 {
        GObject *obj;
        ModestWindowPrivate *parent_priv;
@@ -1094,7 +1144,7 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name)
        if (account_pair != NULL)
                modest_combo_box_set_active_id (MODEST_COMBO_BOX (priv->from_field), account_pair->first);
 
-       set_msg (MODEST_MSG_EDIT_WINDOW (obj), msg);
+       set_msg (MODEST_MSG_EDIT_WINDOW (obj), msg, preserve_is_rich);
 
        text_buffer_refresh_attributes (WP_TEXT_BUFFER (priv->text_buffer), MODEST_MSG_EDIT_WINDOW (obj));
 
@@ -1142,6 +1192,8 @@ modest_msg_edit_window_new (TnyMsg *msg, const gchar *account_name)
 
        update_paste_dimming (MODEST_MSG_EDIT_WINDOW (obj));
        priv->update_caption_visibility = TRUE;
+
+       reset_modified (MODEST_MSG_EDIT_WINDOW (obj));
        
        return (ModestWindow*) obj;
 }
@@ -1209,7 +1261,7 @@ modest_msg_edit_window_get_msg_data (ModestMsgEditWindow *edit_window)
        GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->msg_body));
        GtkTextIter b, e;
        gtk_text_buffer_get_bounds (buf, &b, &e);
-       data->plain_body = g_strdup (gtk_text_buffer_get_text (priv->text_buffer, &b, &e, FALSE)); /* returns a copy */
+       data->plain_body = modest_text_utils_text_buffer_get_text (priv->text_buffer); /* returns a copy */
 
        if (wp_text_buffer_is_rich_text (WP_TEXT_BUFFER (priv->text_buffer)))
                data->html_body = get_formatted_data (edit_window); /* returns a copy. */