sync with 0.7.8cvs7
[claws.git] / src / compose.c
index 99938cde1ddd910fc2283637f5617689b56f5869..bc3602370ee429c7688ba5accd78ed075bb939ca 100644 (file)
@@ -365,6 +365,7 @@ static void compose_redo_cb         (Compose        *compose);
 static void compose_cut_cb             (Compose        *compose);
 static void compose_copy_cb            (Compose        *compose);
 static void compose_paste_cb           (Compose        *compose);
+static void compose_paste_as_quote_cb  (Compose        *compose);
 static void compose_allsel_cb          (Compose        *compose);
 
 static void compose_gtk_stext_action_cb        (Compose                   *compose,
@@ -443,8 +444,6 @@ static void to_activated            (GtkWidget      *widget,
                                         Compose        *compose);
 static void newsgroups_activated       (GtkWidget      *widget,
                                         Compose        *compose);
-static void subject_activated          (GtkWidget      *widget,
-                                        Compose        *compose);
 static void cc_activated               (GtkWidget      *widget,
                                         Compose        *compose);
 static void bcc_activated              (GtkWidget      *widget,
@@ -453,8 +452,17 @@ static void replyto_activated              (GtkWidget      *widget,
                                         Compose        *compose);
 static void followupto_activated       (GtkWidget      *widget,
                                         Compose        *compose);
+static void subject_activated          (GtkWidget      *widget,
+                                        Compose        *compose);
 #endif
 
+static void text_activated             (GtkWidget      *widget,
+                                        Compose        *compose);
+static void text_inserted              (GtkWidget      *widget,
+                                        const gchar    *text,
+                                        gint            length,
+                                        gint           *position,
+                                        Compose        *compose);
 static void compose_generic_reply(MsgInfo *msginfo, gboolean quote,
                                  gboolean to_all,
                                  gboolean ignore_replyto,
@@ -476,9 +484,7 @@ static void compose_check_backwards    (Compose *compose);
 static void compose_check_forwards_go     (Compose *compose);
 #endif
 
-static gboolean compose_send_control_enter (Compose *compose);
-static void text_activated                (GtkWidget   *widget,
-                                           Compose     *compose);
+static gboolean compose_send_control_enter     (Compose        *compose);
 
 static GtkItemFactoryEntry compose_popup_entries[] =
 {
@@ -504,6 +510,8 @@ static GtkItemFactoryEntry compose_entries[] =
        {N_("/_Edit/Cu_t"),             "<control>X", compose_cut_cb,    0, NULL},
        {N_("/_Edit/_Copy"),            "<control>C", compose_copy_cb,   0, NULL},
        {N_("/_Edit/_Paste"),           "<control>V", compose_paste_cb,  0, NULL},
+       {N_("/_Edit/Paste as _quotation"),
+                                       NULL, compose_paste_as_quote_cb, 0, NULL},
        {N_("/_Edit/Select _all"),      "<control>A", compose_allsel_cb, 0, NULL},
        {N_("/_Edit/A_dvanced"),        NULL, NULL, 0, "<Branch>"},
        {N_("/_Edit/A_dvanced/Move a character backward"),
@@ -734,6 +742,9 @@ Compose *compose_new_with_folderitem(PrefsAccount *account, FolderItem *item)
 Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderItem *item)
 {
        Compose *compose;
+       GtkSText *text;
+       GtkItemFactory *ifactory;
+       gboolean grab_focus_on_last = TRUE;
 
        if (item && item->prefs && item->prefs->enable_default_account)
                account = account_find_from_id(item->prefs->default_account);
@@ -742,12 +753,24 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
        g_return_val_if_fail(account != NULL, NULL);
 
        compose = compose_create(account, COMPOSE_NEW);
+       ifactory = gtk_item_factory_from_widget(compose->menubar);
+
        compose->replyinfo = NULL;
 
+       text = GTK_STEXT(compose->text);
+       gtk_stext_freeze(text);
+
        if (prefs_common.auto_sig)
                compose_insert_sig(compose);
-       gtk_editable_set_position(GTK_EDITABLE(compose->text), 0);
-       gtk_stext_set_point(GTK_STEXT(compose->text), 0);
+       gtk_editable_set_position(GTK_EDITABLE(text), 0);
+       gtk_stext_set_point(text, 0);
+
+       gtk_stext_thaw(text);
+
+       /* workaround for initial XIM problem */
+       gtk_widget_grab_focus(compose->text);
+       gtkut_widget_wait_for_draw(compose->text);
+
 
        if (account->protocol != A_NNTP) {
                if (mailto) {
@@ -755,17 +778,21 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
 
                } else if(item && item->prefs->enable_default_to) {
                        compose_entry_append(compose, item->prefs->default_to, COMPOSE_TO);
+                       compose_entry_select(compose, item->prefs->default_to);
+                       grab_focus_on_last = FALSE;
                }
                if (item && item->ret_rcpt) {
-                       GtkItemFactory *ifactory;
-               
-                       ifactory = gtk_item_factory_from_widget(compose->menubar);
                        menu_set_toggle(ifactory, "/Message/Request Return Receipt", TRUE);
                }
        } else {
                if (mailto) {
                        compose_entry_append(compose, mailto, COMPOSE_NEWSGROUPS);
                }
+               /*
+                * CLAWS: just don't allow return receipt request, even if the user
+                * may want to send an email. simple but foolproof.
+                */
+               menu_set_sensitive(ifactory, "/Message/Request Return Receipt", FALSE); 
        }
        compose_show_first_last_header(compose, TRUE);
 
@@ -778,8 +805,10 @@ Compose *compose_generic_new(PrefsAccount *account, const gchar *mailto, FolderI
                gtk_entry_set_text(GTK_ENTRY(compose->savemsg_entry), folderidentifier);
                g_free(folderidentifier);
        }
-
-       gtk_widget_grab_focus(compose->header_last->entry);
+       
+       /* Grab focus on last header only if no default_to was set */
+       if(grab_focus_on_last)
+               gtk_widget_grab_focus(compose->header_last->entry);
 
        if (prefs_common.auto_exteditor)
                compose_exec_ext_editor(compose);
@@ -1341,6 +1370,20 @@ void compose_entry_append(Compose *compose, const gchar *address,
        compose_add_header_entry(compose, header, (gchar *)address);
 }
 
+void compose_entry_select (Compose *compose, const gchar *mailto)
+{
+       GSList *header_list;
+               
+       for (header_list = compose->header_list; header_list != NULL; header_list = header_list->next) {
+               GtkEntry * entry = GTK_ENTRY(((ComposeHeaderEntry *)header_list->data)->entry);
+
+               if (gtk_entry_get_text(entry) && !g_strcasecmp(gtk_entry_get_text(entry), mailto)) {
+                       gtk_entry_select_region(entry, 0, -1);
+                       gtk_widget_grab_focus(GTK_WIDGET(entry));
+               }
+       }
+}
+
 static void compose_entries_set(Compose *compose, const gchar *mailto)
 {
        gchar *subject = NULL;
@@ -1595,11 +1638,15 @@ static gchar *compose_quote_fmt(Compose *compose, MsgInfo *msginfo,
                                const gchar *body)
 {
        GtkSText *text = GTK_STEXT(compose->text);
+       static MsgInfo dummyinfo;
        gchar *quote_str = NULL;
        gchar *buf;
        gchar *p, *lastp;
        gint len;
 
+       if (!msginfo)
+               msginfo = &dummyinfo;
+
        if (qmark != NULL) {
                quote_fmt_init(msginfo, NULL, NULL);
                quote_fmt_scan_string(qmark);
@@ -1956,16 +2003,24 @@ static void compose_attach_append(Compose *compose, const gchar *file,
                                (_("Message: %s"),
                                 g_basename(filename ? filename : file));
                } else {
-                       ainfo->encoding = ENC_BASE64;
+                       if (!g_strncasecmp(content_type, "text", 4))
+                               ainfo->encoding =
+                                       procmime_get_encoding_for_file(file);
+                       else
+                               ainfo->encoding = ENC_BASE64;
                        ainfo->name = g_strdup
                                (g_basename(filename ? filename : file));
                }
        } else {
                ainfo->content_type = procmime_get_mime_type(file);
-               if (!ainfo->content_type)
+               if (!ainfo->content_type) {
                        ainfo->content_type =
                                g_strdup("application/octet-stream");
-               ainfo->encoding = ENC_BASE64;
+                       ainfo->encoding = ENC_BASE64;
+               } else if (!g_strncasecmp(ainfo->content_type, "text", 4))
+                       ainfo->encoding = procmime_get_encoding_for_file(file);
+               else
+                       ainfo->encoding = ENC_BASE64;
                ainfo->name = g_strdup(g_basename(filename ? filename : file)); 
        }
        ainfo->size = size;
@@ -4504,6 +4559,8 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
                           GTK_SIGNAL_FUNC(compose_grab_focus_cb), compose);
        gtk_signal_connect(GTK_OBJECT(text), "activate",
                           GTK_SIGNAL_FUNC(text_activated), compose);
+       gtk_signal_connect(GTK_OBJECT(text), "insert_text",
+                          GTK_SIGNAL_FUNC(text_inserted), compose);
        gtk_signal_connect_after(GTK_OBJECT(text), "button_press_event",
                                 GTK_SIGNAL_FUNC(compose_button_press_cb),
                                 edit_vbox);
@@ -4547,9 +4604,6 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
                new_style = gtk_style_copy(style);
 
        if (prefs_common.textfont) {
-               CharSet charset;
-
-               charset = conv_get_current_charset();
                if (MB_CUR_MAX == 1) {
                        gchar *fontstr, *p;
 
@@ -4670,6 +4724,7 @@ static Compose *compose_create(PrefsAccount *account, ComposeMode mode)
        compose->modified = FALSE;
 
        compose->return_receipt = FALSE;
+       compose->paste_as_quotation = FALSE;
 
        compose->to_list        = NULL;
        compose->newsgroup_list = NULL;
@@ -5101,11 +5156,8 @@ static void compose_template_apply(Compose *compose, Template *tmpl,
                gtk_stext_clear(GTK_STEXT(compose->text));
 
        if (compose->replyinfo == NULL) {
-               MsgInfo dummyinfo;
-
-               memset(&dummyinfo, 0, sizeof(MsgInfo));
-               parsed_str = compose_quote_fmt(compose, &dummyinfo,
-                                              tmpl->value, NULL, NULL);
+               parsed_str = compose_quote_fmt(compose, NULL, tmpl->value,
+                                              NULL, NULL);
        } else {
                if (prefs_common.quotemark && *prefs_common.quotemark)
                        qmark = prefs_common.quotemark;
@@ -6316,6 +6368,17 @@ static void compose_paste_cb(Compose *compose)
                        (GTK_EDITABLE(compose->focused_editable));
 }
 
+static void compose_paste_as_quote_cb(Compose *compose)
+{
+       if (compose->focused_editable &&
+           GTK_WIDGET_HAS_FOCUS(compose->focused_editable)) {
+               compose->paste_as_quotation = TRUE;
+               gtk_editable_paste_clipboard
+                       (GTK_EDITABLE(compose->focused_editable));
+               compose->paste_as_quotation = FALSE;
+       }
+}
+
 static void compose_allsel_cb(Compose *compose)
 {
        if (compose->focused_editable &&
@@ -6780,6 +6843,42 @@ static void text_activated(GtkWidget *widget, Compose *compose)
        compose_send_control_enter(compose);
 }
 
+static void text_inserted(GtkWidget *widget, const gchar *text,
+                         gint length, gint *position, Compose *compose)
+{
+       GtkEditable *editable = GTK_EDITABLE(widget);
+
+       gtk_signal_handler_block_by_func(GTK_OBJECT(widget),
+                                        GTK_SIGNAL_FUNC(text_inserted),
+                                        compose);
+       if (compose->paste_as_quotation) {
+               gchar *new_text;
+               gchar *qmark;
+               gint pos;
+
+               new_text = g_strndup(text, length);
+               if (prefs_common.quotemark && *prefs_common.quotemark)
+                       qmark = prefs_common.quotemark;
+               else
+                       qmark = "> ";
+               gtk_stext_set_point(GTK_STEXT(widget), *position);
+               compose_quote_fmt(compose, NULL, "%Q", qmark, new_text);
+               pos = gtk_stext_get_point(GTK_STEXT(widget));
+               gtk_editable_set_position(editable, pos);
+               *position = pos;
+               g_free(new_text);
+       } else
+               gtk_editable_insert_text(editable, text, length, position);
+
+       if (prefs_common.autowrap)
+               compose_wrap_line_all(compose);
+
+       gtk_signal_handler_unblock_by_func(GTK_OBJECT(widget),
+                                          GTK_SIGNAL_FUNC(text_inserted),
+                                          compose);
+       gtk_signal_emit_stop_by_name(GTK_OBJECT(editable), "insert_text");
+}
+
 static gboolean compose_send_control_enter(Compose *compose)
 {
        GdkEvent *ev;