2005-02-03 [paul] 1.0.0cvs25.3
[claws.git] / src / textview.c
index e2c60b01126c7c1281be5873e81f3118beec42f2..a50dd104f7a633ec1f6687ef6d5a8ca01b63907b 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
@@ -51,6 +51,7 @@
 #include "account.h"
 #include "mimeview.h"
 #include "alertpanel.h"
+#include "menu.h"
 
 struct _RemoteURI
 {
@@ -185,12 +186,19 @@ static gboolean textview_uri_security_check       (TextView       *textview,
                                                 RemoteURI      *uri);
 static void textview_uri_list_remove_all       (GSList         *uri_list);
 
+static void open_uri_cb                                (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
+static void copy_uri_cb                                (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
 
-static void populate_popup(GtkTextView *textview, GtkMenu *menu,
-                          gpointer *dummy)
+static GtkItemFactoryEntry textview_popup_entries[] = 
 {
-       gtk_menu_detach(menu);
-}
+       {N_("/_Open link"),             NULL, open_uri_cb, 0, NULL},
+       {N_("/_Copy link location"),    NULL, copy_uri_cb, 0, NULL},
+};
+
 
 TextView *textview_create(void)
 {
@@ -200,7 +208,9 @@ TextView *textview_create(void)
        GtkWidget *text;
        GtkTextBuffer *buffer;
        GtkClipboard *clipboard;
-       PangoFontDescription *font_desc = NULL;
+       GtkItemFactory *popupfactory;
+       GtkWidget *popupmenu;
+       gint n_entries;
 
        debug_print("Creating text view...\n");
        textview = g_new0(TextView, 1);
@@ -208,9 +218,10 @@ TextView *textview_create(void)
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
                                       GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
-       gtk_widget_set_size_request(scrolledwin, prefs_common.mainview_width, -1);
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
                                            GTK_SHADOW_IN);
+       gtk_widget_set_size_request
+               (scrolledwin, prefs_common.mainview_width, -1);
 
        /* create GtkSText widgets for single-byte and multi-byte character */
        text = gtk_text_view_new();
@@ -219,9 +230,8 @@ TextView *textview_create(void)
        gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
        gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
        gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(text), FALSE);
-       g_signal_connect(G_OBJECT(text), "populate-popup",
-                G_CALLBACK(populate_popup), NULL);
-
+       gtk_text_view_set_left_margin(GTK_TEXT_VIEW(text), 6);
+       gtk_text_view_set_right_margin(GTK_TEXT_VIEW(text), 6);
 
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
@@ -229,14 +239,6 @@ TextView *textview_create(void)
 
        gtk_widget_ensure_style(text);
 
-       if (prefs_common.normalfont)
-               font_desc = pango_font_description_from_string
-                                       (prefs_common.normalfont);
-       if (font_desc) {
-               gtk_widget_modify_font(text, font_desc);
-       }
-       pango_font_description_free(font_desc);
-
        gtk_widget_ref(scrolledwin);
 
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
@@ -266,6 +268,12 @@ TextView *textview_create(void)
 
        gtk_widget_show(vbox);
 
+       n_entries = sizeof(textview_popup_entries) /
+               sizeof(textview_popup_entries[0]);
+       popupmenu = menu_create_items(textview_popup_entries, n_entries,
+                                     "<UriPopupMenu>", &popupfactory,
+                                     textview);
+
        textview->vbox             = vbox;
        textview->scrolledwin      = scrolledwin;
        textview->text             = text;
@@ -273,25 +281,42 @@ TextView *textview_create(void)
        textview->body_pos         = 0;
        textview->show_all_headers = FALSE;
        textview->last_buttonpress = GDK_NOTHING;
+       textview->popup_menu       = popupmenu;
+       textview->popup_factory    = popupfactory;
 
        return textview;
 }
 
 static void textview_create_tags(GtkTextView *text, TextView *textview)
 {
-       GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+       GtkTextBuffer *buffer;
        GtkTextTag *tag;
+       static PangoFontDescription *font_desc, *bold_font_desc;
+
+       if (!font_desc)
+               font_desc = pango_font_description_from_string
+                       (NORMAL_FONT);
+
+       if (!bold_font_desc) {
+               bold_font_desc = pango_font_description_from_string
+                       (BOLD_FONT);
+               pango_font_description_set_weight
+                       (bold_font_desc, PANGO_WEIGHT_BOLD);
+       }
+
+       buffer = gtk_text_view_get_buffer(text);
 
        gtk_text_buffer_create_tag(buffer, "header",
                                   "pixels-above-lines", 0,
                                   "pixels-above-lines-set", TRUE,
                                   "pixels-below-lines", 0,
                                   "pixels-below-lines-set", TRUE,
+                                  "font-desc", font_desc,
                                   "left-margin", 0,
                                   "left-margin-set", TRUE,
                                   NULL);
        gtk_text_buffer_create_tag(buffer, "header_title",
-                                  "font", prefs_common.boldfont,
+                                  "font-desc", bold_font_desc,
                                   NULL);
        gtk_text_buffer_create_tag(buffer, "quote0",
                                   "foreground-gdk", &quote_colors[0],
@@ -321,11 +346,9 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
 
 void textview_init(TextView *textview)
 {
-       gtkut_widget_disable_theme_engine(textview->text);
        textview_update_message_colors();
        textview_set_all_headers(textview, FALSE);
        textview_set_font(textview, NULL);
-
        textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
 }
 
@@ -480,8 +503,8 @@ static void recursive_add_parts(TextView *textview, GNode *node)
                 prefered_body = NULL;
                 prefered_score = 0;
                 
-                for(iter = g_node_first_child(node) ; iter != NULL ;
-                    iter = g_node_next_sibling(iter)) {
+                for (iter = g_node_first_child(node) ; iter != NULL ;
+                     iter = g_node_next_sibling(iter)) {
                         int score;
                         MimeInfo * submime;
                         
@@ -506,8 +529,8 @@ static void recursive_add_parts(TextView *textview, GNode *node)
                 }
         }
         else {
-                for(iter = g_node_first_child(node) ; iter != NULL ;
-                    iter = g_node_next_sibling(iter)) {
+                for (iter = g_node_first_child(node) ; iter != NULL ;
+                     iter = g_node_next_sibling(iter)) {
                         recursive_add_parts(textview, iter);
                 }
         }
@@ -873,7 +896,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        if (!result) return FALSE;
 
-       if (*(bp_ - 1) == '"' && *(ep_) == '"' 
+       if (*ep_ && *(bp_ - 1) == '"' && *(ep_) == '"' 
        && *(ep_ + 1) == ' ' && *(ep_ + 2) == '<'
        && IS_RFC822_CHAR(*(ep_ + 3))) {
                /* this informative part with an @ in it is 
@@ -1105,22 +1128,19 @@ static void textview_make_clickable_parts(TextView *textview,
                                                               last->ep);
                        uri->start = gtk_text_iter_get_offset(&iter);
                        gtk_text_buffer_insert_with_tags_by_name
-                               (buffer, &iter,
-                                last->bp, last->ep - last->bp,
-                                uri_tag, NULL);
+                               (buffer, &iter, last->bp, last->ep - last->bp,
+                                uri_tag, fg_tag, NULL);
                        uri->end = gtk_text_iter_get_offset(&iter);
                        textview->uri_list =
                                g_slist_append(textview->uri_list, uri);
                }
 
                if (*normal_text)
-                       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
-                                                                normal_text, -1,
-                                                                fg_tag, NULL);
+                       gtk_text_buffer_insert_with_tags_by_name
+                               (buffer, &iter, normal_text, -1, fg_tag, NULL);
        } else {
-               gtk_text_buffer_insert_with_tags_by_name(buffer, &iter,
-                                                        linebuf, -1,
-                                                        fg_tag, NULL);
+               gtk_text_buffer_insert_with_tags_by_name
+                       (buffer, &iter, linebuf, -1, fg_tag, NULL);
        }
 }
 
@@ -1141,14 +1161,13 @@ static void textview_write_line(TextView *textview, const gchar *str,
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
-       if (!conv) {
+       if (!conv)
                strncpy2(buf, str, sizeof(buf));
-       } else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
-               conv_localetodisp(buf, sizeof(buf), str);
-       }
+       else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
+               conv_utf8todisp(buf, sizeof(buf), str);
 
        strcrchomp(buf);
-       if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
+       //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
        fg_color = NULL;
 
        /* change color of quotation
@@ -1206,23 +1225,10 @@ void textview_write_link(TextView *textview, const gchar *str,
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
-#warning FIXME_GTK2
-#if 0
-       if (!conv) {
-               if (textview->text_is_mb)
-                       conv_localetodisp(buf, sizeof(buf), str);
-               else
-                       strncpy2(buf, str, sizeof(buf));
-       } else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
-               conv_localetodisp(buf, sizeof(buf), str);
-       else if (textview->text_is_mb)
-               conv_unreadable_locale(buf);
-#else
        if (!conv)
                strncpy2(buf, str, sizeof(buf));
        else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
-               conv_localetodisp(buf, sizeof(buf), str);
-#endif
+               conv_utf8todisp(buf, sizeof(buf), str);
 
        strcrchomp(buf);
 
@@ -1237,8 +1243,8 @@ void textview_write_link(TextView *textview, const gchar *str,
        r_uri = g_new(RemoteURI, 1);
        r_uri->uri = g_strdup(uri);
        r_uri->start = gtk_text_iter_get_offset(&iter);
-       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, bufp, -1,
-                                                "link", NULL);
+       gtk_text_buffer_insert_with_tags_by_name
+               (buffer, &iter, bufp, -1, "link", NULL);
        r_uri->end = gtk_text_iter_get_offset(&iter);
        textview->uri_list = g_slist_append(textview->uri_list, r_uri);
 }
@@ -1249,7 +1255,7 @@ void textview_clear(TextView *textview)
        GtkTextBuffer *buffer;
 
        buffer = gtk_text_view_get_buffer(text);
-       gtk_text_buffer_set_text(buffer, "\0", -1);
+       gtk_text_buffer_set_text(buffer, "", -1);
 
        TEXTVIEW_STATUSBAR_POP(textview);
        textview_uri_list_remove_all(textview->uri_list);
@@ -1276,8 +1282,7 @@ void textview_set_font(TextView *textview, const gchar *codeset)
        if (prefs_common.textfont) {
                PangoFontDescription *font_desc = NULL;
 
-               if (prefs_common.textfont)
-                       font_desc = pango_font_description_from_string
+               font_desc = pango_font_description_from_string
                                                (prefs_common.textfont);
                if (font_desc) {
                        gtk_widget_modify_font(textview->text, font_desc);
@@ -1288,11 +1293,6 @@ void textview_set_font(TextView *textview, const gchar *codeset)
                                             prefs_common.line_space / 2);
        gtk_text_view_set_pixels_below_lines(GTK_TEXT_VIEW(textview->text),
                                             prefs_common.line_space / 2);
-       if (prefs_common.head_space) {
-               gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview->text), 6);
-       } else {
-               gtk_text_view_set_left_margin(GTK_TEXT_VIEW(textview->text), 0);
-       }
 }
 
 void textview_set_text(TextView *textview, const gchar *text)
@@ -1590,19 +1590,15 @@ void textview_scroll_one_line(TextView *textview, gboolean up)
        if (!up) {
                upper = vadj->upper - vadj->page_size;
                if (vadj->value < upper) {
-                       vadj->value +=
-                               vadj->step_increment * 4;
-                       vadj->value =
-                               MIN(vadj->value, upper);
+                       vadj->value += vadj->step_increment;
+                       vadj->value = MIN(vadj->value, upper);
                        g_signal_emit_by_name(G_OBJECT(vadj),
                                              "value_changed", 0);
                }
        } else {
                if (vadj->value > 0.0) {
-                       vadj->value -=
-                               vadj->step_increment * 4;
-                       vadj->value =
-                               MAX(vadj->value, 0.0);
+                       vadj->value -= vadj->step_increment;
+                       vadj->value = MAX(vadj->value, 0.0);
                        g_signal_emit_by_name(G_OBJECT(vadj),
                                              "value_changed", 0);
                }
@@ -1686,8 +1682,7 @@ static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
                upper = vadj->upper - vadj->page_size;
                if (vadj->value < upper) {
                        old_value = vadj->value;
-                       last_value = vadj->value +
-                               vadj->step_increment * 4;
+                       last_value = vadj->value + vadj->step_increment;
                        last_value = MIN(last_value, upper);
 
                        textview_smooth_scroll_do(textview, old_value,
@@ -1697,8 +1692,7 @@ static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
        } else {
                if (vadj->value > 0.0) {
                        old_value = vadj->value;
-                       last_value = vadj->value -
-                               vadj->step_increment * 4;
+                       last_value = vadj->value - vadj->step_increment;
                        last_value = MAX(last_value, 0.0);
 
                        textview_smooth_scroll_do(textview, old_value,
@@ -1776,7 +1770,7 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_End:
        case GDK_Control_L:
        case GDK_Control_R:
-               break;
+               return FALSE;
        case GDK_space:
                if (summaryview)
                        summary_pass_key_press_event(summaryview, event);
@@ -1824,70 +1818,6 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        return TRUE;
 }
 
-/*!
- *\brief    Check to see if a web URL has been disguised as a different
- *          URL (possible with HTML email).
- *
- *\param    uri The uri to check
- *
- *\param    textview The TextView the URL is contained in
- *
- *\return   gboolean TRUE if the URL is ok, or if the user chose to open
- *          it anyway, otherwise FALSE          
- */
-static gboolean uri_security_check(RemoteURI *uri, TextView *textview) 
-{
-       gchar *clicked_str;
-       gboolean retval = TRUE;
-
-       if (g_ascii_strncasecmp(uri->uri, "http:", 5) &&
-           g_ascii_strncasecmp(uri->uri, "https:", 6) &&
-           g_ascii_strncasecmp(uri->uri, "www.", 4)) 
-               return retval;
-
-       clicked_str = gtk_editable_get_chars(GTK_EDITABLE(textview->text),
-                                            uri->start,
-                                            uri->end);
-       if (clicked_str == NULL)
-               return TRUE;
-
-       if (strcmp(clicked_str, uri->uri) &&
-           (!g_ascii_strncasecmp(clicked_str, "http:",  5) ||
-            !g_ascii_strncasecmp(clicked_str, "https:", 6) ||
-            !g_ascii_strncasecmp(clicked_str, "www.",   4))) {
-               gchar *str;
-               retval = FALSE;
-
-               /* allow uri->uri    == http://somewhere.com
-                  and   clicked_str ==        somewhere.com */
-               str = g_strconcat("http://", clicked_str, NULL);
-
-               if (!g_ascii_strcasecmp(str, uri->uri))
-                       retval = TRUE;
-               g_free(str);
-       }
-
-       if (retval == FALSE) {
-               gchar *msg = NULL;
-               AlertValue resp;
-
-               msg = g_strdup_printf(_("The real URL (%s) is different from\n"
-                                       "the apparent URL (%s).  \n"
-                                       "Open it anyway?"),
-                                       uri->uri, clicked_str);
-               resp = alertpanel_with_type(_("Warning"), 
-                                 msg,
-                                 _("Yes"), 
-                                 _("No"),
-                                 NULL, NULL, ALERT_WARNING);
-               g_free(msg);
-               if (resp == G_ALERTDEFAULT)
-                       retval = TRUE;
-       } 
-       g_free(clicked_str);
-       return retval;
-}
-
 static gboolean textview_motion_notify(GtkWidget *widget,
                                       GdkEventMotion *event,
                                       TextView *textview)
@@ -1912,7 +1842,15 @@ static gboolean textview_visibility_notify(GtkWidget *widget,
                                           TextView *textview)
 {
        gint wx, wy;
-  
+       GdkWindow *window;
+
+       window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget),
+                                         GTK_TEXT_WINDOW_TEXT);
+
+       /* check if occurred for the text window part */
+       if (window != event->window)
+               return FALSE;
+       
        gdk_window_get_pointer(widget->window, &wx, &wy, NULL);
        textview_uri_update(textview, wx, wy);
 
@@ -2005,13 +1943,13 @@ static gboolean textview_get_uri_range(TextView *textview,
        GtkTextIter _start_iter, _end_iter;
 
        _end_iter = *iter;
-       if(!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
+       if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
                debug_print("Can't find end");
                return FALSE;
        }
 
        _start_iter = _end_iter;
-       if(!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
+       if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
                debug_print("Can't find start.");
                return FALSE;
        }
@@ -2130,9 +2068,18 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                        }
                        return TRUE;
                } else {
-                       if (textview_uri_security_check(textview, uri) == TRUE) 
-                               open_uri(uri->uri,
-                                        prefs_common.uri_cmd);
+                       if (bevent->button == 1 &&
+                           textview_uri_security_check(textview, uri) == TRUE) 
+                                       open_uri(uri->uri,
+                                                prefs_common.uri_cmd);
+                       else if (bevent->button == 3) {
+                               g_object_set_data(
+                                       G_OBJECT(textview->popup_menu),
+                                       "menu_button", uri);
+                               gtk_menu_popup(GTK_MENU(textview->popup_menu), 
+                                              NULL, NULL, NULL, NULL, 
+                                              bevent->button, bevent->time);
+                       }
                        return TRUE;
                }
        }
@@ -2155,12 +2102,19 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
 {
        gchar *visible_str;
        gboolean retval = TRUE;
+       GtkTextBuffer *buffer;
+       GtkTextIter start, end;
 
        if (is_uri_string(uri->uri) == FALSE)
                return TRUE;
 
-       visible_str = gtk_editable_get_chars(GTK_EDITABLE(textview->text),
-                                            uri->start, uri->end);
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+
+       gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
+       gtk_text_buffer_get_iter_at_offset(buffer, &end,   uri->end);
+
+       visible_str = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+
        if (visible_str == NULL)
                return TRUE;
 
@@ -2182,8 +2136,9 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
                                        "the apparent URL (%s).\n"
                                        "Open it anyway?"),
                                      uri->uri, visible_str);
-               aval = alertpanel_with_type(_("Warning"), msg, _("Yes"), _("No"), NULL,
-                                           NULL, ALERT_WARNING);
+               aval = alertpanel_with_type(_("Warning"), msg,
+                                           GTK_STOCK_YES, GTK_STOCK_NO,
+                                           NULL, NULL, ALERT_WARNING);
                g_free(msg);
                if (aval == G_ALERTDEFAULT)
                        retval = TRUE;
@@ -2207,3 +2162,29 @@ static void textview_uri_list_remove_all(GSList *uri_list)
 
        g_slist_free(uri_list);
 }
+
+static void open_uri_cb (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->popup_menu),
+                                          "menu_button");
+       if (uri == NULL)
+               return;
+
+       if (textview_uri_security_check(textview, uri) == TRUE) 
+               open_uri(uri->uri,
+                        prefs_common.uri_cmd);
+       g_object_set_data(G_OBJECT(textview->popup_menu), "menu_button",
+                         NULL);
+}
+
+static void copy_uri_cb        (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->popup_menu),
+                                          "menu_button");
+       if (uri == NULL)
+               return;
+
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE), uri->uri, -1);
+       g_object_set_data(G_OBJECT(textview->popup_menu), "menu_button",
+                         NULL);
+}