2005-02-15 [colin] 1.0.1cvs7.1
[claws.git] / src / compose.c
index d6ed44c29b11e2a8b26281a7847ad5a1cd2dd92c..07a9a15a87352f1feac663e08c80a873fcd24aa9 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2004 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2005 Hiroyuki Yamamoto
  *
  * 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
@@ -24,6 +24,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtkmain.h>
 #include <gtk/gtkmenu.h>
@@ -51,6 +52,7 @@
 #include <gtk/gtktreemodel.h>
 
 #include <gtk/gtkdnd.h>
+#include <gtk/gtkclipboard.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -71,7 +73,6 @@
 #  include <wctype.h>
 #endif
 
-#include "intl.h"
 #include "main.h"
 #include "mainwindow.h"
 #include "compose.h"
@@ -299,10 +300,6 @@ static void compose_undo_state_changed             (UndoMain       *undostruct,
                                                 gint            redo_state,
                                                 gpointer        data);
 
-static gint calc_cursor_xpos   (GtkTextView    *text,
-                                gint            extra,
-                                gint            char_width);
-
 static void compose_create_header_entry        (Compose *compose);
 static void compose_add_header_entry   (Compose *compose, gchar *header, gchar *text);
 static void compose_update_priority_menu_item(Compose * compose);
@@ -1332,26 +1329,18 @@ void compose_reedit(MsgInfo *msginfo)
                                        G_CALLBACK(compose_changed_cb),
                                        compose);
                                        
-       g_signal_handlers_block_by_func(G_OBJECT(textbuf),
-                                       G_CALLBACK(text_inserted),
-                                       compose);
-
        if ((fp = procmime_get_first_text_content(msginfo)) == NULL)
                g_warning("Can't get text part\n");
        else {
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
                        gtk_text_buffer_insert(textbuf, &iter, buf, -1);
-                       gtk_text_buffer_get_iter_at_mark(textbuf, &iter, mark);
                }
                fclose(fp);
        }
        
        compose_attach_parts(compose, msginfo);
 
-       g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
-                                       G_CALLBACK(text_inserted),
-                                       compose);
        g_signal_handlers_unblock_by_func(G_OBJECT(textbuf),
                                        G_CALLBACK(compose_changed_cb),
                                        compose);
@@ -1386,7 +1375,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
 
        gtk_widget_grab_focus(compose->header_last->entry);
 
-       filename = procmsg_get_message_file(msginfo);
+       filename = procmsg_get_message_file_path(msginfo);
        if (filename == NULL)
                return NULL;
 
@@ -1411,7 +1400,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
        gtk_editable_set_editable(GTK_EDITABLE(compose->subject_entry), FALSE);
 
        compose_quote_fmt(compose, msginfo, "%M", NULL, NULL);
-       gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), TRUE);
+       gtk_text_view_set_editable(GTK_TEXT_VIEW(compose->text), FALSE);
 
        ifactory = gtk_item_factory_from_widget(compose->popupmenu);
        menu_set_sensitive(ifactory, "/Add...", FALSE);
@@ -1424,10 +1413,7 @@ Compose *compose_redirect(PrefsAccount *account, MsgInfo *msginfo)
        menu_set_sensitive(ifactory, "/Message/Attach file", FALSE);
        menu_set_sensitive(ifactory, "/Message/Insert signature", FALSE);
        menu_set_sensitive(ifactory, "/Edit", FALSE);
-       menu_set_sensitive(ifactory, "/Options/Sign", FALSE);
-       menu_set_sensitive(ifactory, "/Options/Encrypt", FALSE);
-       menu_set_sensitive(ifactory, "/Options/Priority", FALSE);
-       menu_set_sensitive(ifactory, "/Options/Request Return Receipt", FALSE);
+       menu_set_sensitive(ifactory, "/Options", FALSE);
        menu_set_sensitive(ifactory, "/Tools/Show ruler", FALSE);
        menu_set_sensitive(ifactory, "/Tools/Actions", FALSE);
        
@@ -2449,37 +2435,23 @@ static void compose_attach_parts(Compose *compose, MsgInfo *msginfo)
 #define CHAR_BUF_SIZE 8
 #undef NEXT_PART_NOT_CHILD
 
-#define GET_CHAR(iter_p, buf, len)                                          \
-{                                                                           \
-       GtkTextIter end_iter;                                                \
-       gchar *tmp;                                                          \
-       end_iter = *iter_p;                                                  \
-       gtk_text_iter_forward_char(&end_iter);                               \
-       tmp = gtk_text_buffer_get_text(textbuf, iter_p, &end_iter, FALSE);   \
-       if (tmp) {                                                           \
-               glong items_read, items_witten;                              \
-               GError *error = NULL;                                        \
-               gunichar *wide_char;                                         \
-               strncpy2(buf, tmp, CHAR_BUF_SIZE);                           \
-               wide_char = g_utf8_to_ucs4(tmp, -1,                          \
-                                          &items_read, &items_witten,       \
-                                          &error);                          \
-               if (error != NULL) {                                         \
-                       g_warning("%s\n", error->message);                   \
-                       g_error_free(error);                                 \
-               }                                                            \
-               len = wide_char && g_unichar_iswide(*wide_char) ? 2 : 1;     \
-               g_free(wide_char);                                           \
-       } else {                                                             \
-               buf[0] = '\0';                                               \
-               len = 1;                                                     \
-       }                                                                    \
-       g_free(tmp);                                                         \
-}
 
+#define GET_CHAR(iter_p, buf, len)                             \
+{                                                              \
+       gunichar uc;                                            \
+                                                               \
+       uc = gtk_text_iter_get_char(iter_p);                    \
+       if (uc != 0) {                                          \
+               len = g_unichar_to_utf8(uc, buf) > 1 ? 2 : 1;   \
+               buf[len]='\0';                                  \
+       } else {                                                \
+               buf[0] = '\0';                                  \
+               len = 1;                                        \
+       }                                                       \
+}
 #define DISP_WIDTH(len) \
-       ((len > 2 && conv_get_locale_charset() == C_UTF_8) ? 2 : \
-        (len == 2 && conv_get_locale_charset() == C_UTF_8) ? 1 : len)
+       ((len > 2) ? 2 : \
+        (len == 2) ? 1 : len)
 
 #define SPACE_CHARS    " \t"
 
@@ -2616,7 +2588,7 @@ static void compose_wrap_line(Compose *compose)
 
                if (space) {
                        line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
+                       line_len = cur_len + DISP_WIDTH(ch_len);
                }
 
                gtk_text_buffer_get_iter_at_offset(textbuf, &iter, line_pos);
@@ -2644,16 +2616,12 @@ static void compose_wrap_line(Compose *compose)
                        p_end++;
                        cur_pos++;
                        line_pos++;
-                       cur_len = cur_len - line_len + ch_len;
+                       cur_len = cur_len - line_len + DISP_WIDTH(ch_len);
                        line_len = 0;
                        continue;
                }
 
-               if (ch_len > 1) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
-               cur_len += ch_len;
+               cur_len += DISP_WIDTH(ch_len);
        }
 }
 
@@ -2662,15 +2630,15 @@ static void compose_wrap_line(Compose *compose)
 /* Darko: used when I debug wrapping */
 void dump_text(GtkTextBuffer *textbuf, int pos, int tlen, int breakoncr)
 {
-       gint i, clen;
-       gchar cbuf[CHAR_BUF_SIZE];
        GtkTextIter iter, end_iter;
+       gint clen;
+       gchar cbuf[CHAR_BUF_SIZE];
 
        printf("%d [", pos);
        gtk_text_buffer_get_iter_at_offset(textbuf, &iter, pos);
        gtk_text_buffer_get_iter_at_offset(textbuf, &end_iter, pos + tlen);
        for (; gtk_text_iter_forward_char(&iter) &&
-                    gtk_text_iter_compare(&iter, &end_iter) < 0;) {
+            gtk_text_iter_compare(&iter, &end_iter) < 0; ) {
                GET_CHAR(&iter, cbuf, clen);
                if (clen < 0) break;
                if (breakoncr && clen == 1 && cbuf[0] == '\n')
@@ -2693,23 +2661,25 @@ typedef enum {
    uppercase characters immediately followed by >,
    and the repeating sequences of the above */
 /* return indent length */
-static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos, guint text_len)
+static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos,
+                              guint text_len)
 {
+       GtkTextIter iter;
        guint i_len = 0;
-       guint i, ch_len, alnum_cnt = 0;
+       guint ch_len, alnum_cnt = 0;
        IndentState state = WAIT_FOR_INDENT_CHAR;
        gchar cbuf[CHAR_BUF_SIZE];
        gboolean is_space;
        gboolean is_indent;
+       gboolean iter_next = TRUE;
 
        if (prefs_common.quote_chars == NULL) {
                return 0 ;
        }
 
-       for (i = start_pos; i < text_len; i++) {
-               GtkTextIter iter;
+       gtk_text_buffer_get_iter_at_offset(textbuf, &iter, start_pos);
 
-               gtk_text_buffer_get_iter_at_offset(textbuf, &iter, i);
+       while (iter_next == TRUE) {
                GET_CHAR(&iter, cbuf, ch_len);
                if (ch_len > 1)
                        break;
@@ -2757,6 +2727,7 @@ static guint get_indent_length(GtkTextBuffer *textbuf, guint start_pos, guint te
                }
 
                i_len++;
+               iter_next = gtk_text_iter_forward_char(&iter);
        }
 
 out:
@@ -3009,7 +2980,7 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                /* possible line break */
                if (ch_len == 1 && isspace(*(guchar *)cbuf)) {
                        line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
+                       line_len = cur_len + DISP_WIDTH(ch_len);
                }
 
                /* are we over wrapping length set in preferences ? */
@@ -3125,12 +3096,8 @@ static void compose_wrap_line_all_full(Compose *compose, gboolean autowrap)
                        continue;
                }
 
-               if (ch_len > 1) {
-                       line_pos = cur_pos + 1;
-                       line_len = cur_len + ch_len;
-               }
                /* advance to next character in buffer */
-               cur_len += ch_len;
+               cur_len += DISP_WIDTH(ch_len);
        }
 
        if (set_editable_pos && editable_pos <= tlen) {
@@ -3244,11 +3211,11 @@ static void compose_select_account(Compose *compose, PrefsAccount *account,
 
 #endif
 
-       if (account->default_sign)
+       if (account->default_sign && compose->mode != COMPOSE_REDIRECT)
                menu_set_active(ifactory, "/Options/Sign", TRUE);
        else
                menu_set_active(ifactory, "/Options/Sign", FALSE);
-       if (account->default_encrypt)
+       if (account->default_encrypt && compose->mode != COMPOSE_REDIRECT)
                menu_set_active(ifactory, "/Options/Encrypt", TRUE);
        else
                menu_set_active(ifactory, "/Options/Encrypt", FALSE);
@@ -3647,6 +3614,7 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
 
                if (action == COMPOSE_WRITE_FOR_SEND) {
                        buf = conv_codeset_strdup(chars, src_codeset, out_codeset);
+                       
                        if (!buf) {
                                AlertValue aval;
                                gchar *msg;
@@ -3675,6 +3643,13 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
        }
        g_free(chars);
 
+       if (encoding == ENC_8BIT || encoding == ENC_7BIT) {
+               if (!strncmp(buf, "From ", sizeof("From ")-1) ||
+                   strstr(buf, "\nFrom ") != NULL) {
+                       encoding = ENC_QUOTED_PRINTABLE;
+               }
+       }
+
        mimetext = procmime_mimeinfo_new();
        mimetext->content = MIMECONTENT_MEM;
        mimetext->data.mem = buf;
@@ -3685,7 +3660,10 @@ static gint compose_write_to_file(Compose *compose, FILE *fp, gint action)
        /* protect trailing spaces when signing message */
        if (action == COMPOSE_WRITE_FOR_SEND && compose->use_signing && 
            privacy_system_can_sign(compose->privacy_system))
-               encoding = ENC_QUOTED_PRINTABLE;
+               if (encoding == ENC_7BIT)
+                       encoding = ENC_QUOTED_PRINTABLE;
+               else if (encoding == ENC_8BIT)
+                       encoding = ENC_BASE64;
        if (encoding != ENC_UNKNOWN)
                procmime_encode_content(mimetext, encoding);
 
@@ -4822,7 +4800,6 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
 
        gchar *titles[N_ATTACH_COLS];
        guint n_menu_entries;
-       GtkStyle  *style, *new_style;
        GdkColormap *cmap;
        GdkColor color[1];
        gboolean success[1];
@@ -4996,29 +4973,24 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        gtk_paned_add2(GTK_PANED(paned), edit_vbox);
        gtk_widget_show_all(paned);
 
-       style = gtk_widget_get_style(text);
-
-       new_style = gtk_style_copy(style);
 
        if (prefs_common.textfont) {
                PangoFontDescription *font_desc;
 
                font_desc = pango_font_description_from_string
-                                       (prefs_common.textfont);
+                       (prefs_common.textfont);
                if (font_desc) {
-                       if (new_style->font_desc)
-                               pango_font_description_free
-                                       (new_style->font_desc);
-                       new_style->font_desc = font_desc;
+                       gtk_widget_modify_font(text, font_desc);
+                       pango_font_description_free(font_desc);
                }
        }
 
-       gtk_widget_set_style(text, new_style);
-
        color[0] = quote_color;
        cmap = gdk_window_get_colormap(window->window);
        gdk_colormap_alloc_colors(cmap, color, 1, FALSE, TRUE, success);
        if (success[0] == FALSE) {
+               GtkStyle *style;
+
                g_warning("Compose: color allocation failed.\n");
                style = gtk_widget_get_style(text);
                quote_color = style->black;
@@ -6252,12 +6224,6 @@ static void compose_undo_state_changed(UndoMain *undostruct, gint undo_state,
        }
 }
 
-static gint calc_cursor_xpos(GtkTextView *text, gint extra, gint char_width)
-{
-#warning FIXME_GTK2
-       return 0;
-}
-
 /* callback functions */
 
 /* compose_edit_size_alloc() - called when resized. don't know whether Gtk
@@ -6279,10 +6245,7 @@ static gboolean compose_edit_size_alloc(GtkEditable *widget,
 
                /* got the maximum */
                gtk_ruler_set_range(GTK_RULER(shruler),
-                                   0.0, line_width_in_chars,
-                                   calc_cursor_xpos(GTK_TEXT_VIEW(widget),
-                                                    allocation->x,
-                                                    char_width),
+                                   0.0, line_width_in_chars, 0,
                                    /*line_width_in_chars*/ char_width);
        }
 
@@ -6535,9 +6498,11 @@ static void compose_attach_cb(gpointer data, guint action, GtkWidget *widget)
 
                for ( tmp = file_list; tmp; tmp = tmp->next) {
                        gchar *file = (gchar *) tmp->data;
-                       compose_attach_append(compose, file, file, NULL);
+                       gchar *utf8_filename = conv_filename_to_utf8(file);
+                       compose_attach_append(compose, file, utf8_filename, NULL);
                        compose_changed_cb(NULL, compose);
                        g_free(file);
+                       g_free(utf8_filename);
                }
                g_list_free(file_list);
        }               
@@ -6890,7 +6855,6 @@ static void textview_move_next_line (GtkTextView *text)
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        mark = gtk_text_buffer_get_insert(buffer);
        gtk_text_buffer_get_iter_at_mark(buffer, &ins, mark);
-#warning FIXME_GTK2 /* should regist current line offset */
        offset = gtk_text_iter_get_line_offset(&ins);
        if (gtk_text_iter_forward_line(&ins)) {
                gtk_text_iter_set_line_offset(&ins, offset);
@@ -6910,7 +6874,6 @@ static void textview_move_previous_line (GtkTextView *text)
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        mark = gtk_text_buffer_get_insert(buffer);
        gtk_text_buffer_get_iter_at_mark(buffer, &ins, mark);
-#warning FIXME_GTK2 /* should regist current line offset */
        offset = gtk_text_iter_get_line_offset(&ins);
        if (gtk_text_iter_backward_line(&ins)) {
                gtk_text_iter_set_line_offset(&ins, offset);
@@ -7415,8 +7378,14 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
 {
        gint paste_as_quotation = GPOINTER_TO_INT(g_object_get_data
                                (G_OBJECT(compose->text), "paste_as_quotation"));
+       GtkTextMark *mark;
 
-       g_return_if_fail(text);
+       /* CLAWS: pass to default handler only if not pasting as quotation, and 
+        * no autowrap */
+       if (!paste_as_quotation && !compose->autowrap) 
+               return;
+
+       g_return_if_fail(text != NULL);
 
        g_signal_handlers_block_by_func(G_OBJECT(buffer),
                                        G_CALLBACK(text_inserted),
@@ -7441,8 +7410,10 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
        } else
                gtk_text_buffer_insert(buffer, iter, text, len);
 
-       if (compose->autowrap)
-               compose_wrap_line_all_full(compose, TRUE);
+       mark = gtk_text_buffer_create_mark(buffer, NULL, iter, FALSE);
+       compose_wrap_line_all_full(compose, TRUE);
+       gtk_text_buffer_get_iter_at_mark(buffer, iter, mark);
+       gtk_text_buffer_delete_mark(buffer, mark);
 
        g_signal_handlers_unblock_by_func(G_OBJECT(buffer),
                                          G_CALLBACK(text_inserted),
@@ -7454,7 +7425,6 @@ static void text_inserted(GtkTextBuffer *buffer, GtkTextIter *iter,
                compose->draft_timeout_tag = gtk_timeout_add
                        (500, (GtkFunction) compose_defer_auto_save_draft, compose);
 }
-
 static gint compose_defer_auto_save_draft(Compose *compose)
 {
        compose->draft_timeout_tag = -1;