2004-11-19 [colin] 0.9.12cvs158.3
[claws.git] / src / textview.c
index bfccff6627b3a1766def8661bb000987547d2055..1476bab0a7b9b075b014e9aecc75b810ccfbcab1 100644 (file)
@@ -51,6 +51,7 @@
 #include "account.h"
 #include "mimeview.h"
 #include "alertpanel.h"
+#include "menu.h"
 
 struct _RemoteURI
 {
@@ -98,6 +99,7 @@ static GdkColor error_color = {
 
 
 static GdkCursor *hand_cursor = NULL;
+static GdkCursor *text_cursor = NULL;
 
 #define TEXTVIEW_STATUSBAR_PUSH(textview, str)                                     \
 {                                                                          \
@@ -184,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)
 {
@@ -199,6 +208,9 @@ TextView *textview_create(void)
        GtkWidget *text;
        GtkTextBuffer *buffer;
        GtkClipboard *clipboard;
+       GtkItemFactory *popupfactory;
+       GtkWidget *popupmenu;
+       gint n_entries;
        PangoFontDescription *font_desc = NULL;
 
        debug_print("Creating text view...\n");
@@ -218,9 +230,6 @@ 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);
-
 
        buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text));
        clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
@@ -242,6 +251,8 @@ TextView *textview_create(void)
 
        if (!hand_cursor)
                hand_cursor = gdk_cursor_new(GDK_HAND2);
+       if (!text_cursor)
+               text_cursor = gdk_cursor_new(GDK_XTERM);
 
        g_signal_connect(G_OBJECT(text), "key_press_event",
                         G_CALLBACK(textview_key_pressed),
@@ -263,6 +274,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;
@@ -270,6 +287,8 @@ 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;
 }
@@ -477,8 +496,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;
                         
@@ -503,8 +522,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);
                 }
         }
@@ -802,7 +821,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        const gchar *last_dot = NULL;
        const gchar *prelast_dot = NULL;
        const gchar *last_tld_char = NULL;
-
+       
        /* the informative part of the email address (describing the name
         * of the email address owner) may contain quoted parts. the
         * closure stack stores the last encountered quotes. */
@@ -870,8 +889,30 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        if (!result) return FALSE;
 
+       if (*(bp_ - 1) == '"' && *(ep_) == '"' 
+       && *(ep_ + 1) == ' ' && *(ep_ + 2) == '<'
+       && IS_RFC822_CHAR(*(ep_ + 3))) {
+               /* this informative part with an @ in it is 
+                * followed by the email address */
+               ep_ += 3;
+               
+               /* go to matching '>' (or next non-rfc822 char, like \n) */
+               for (; *ep_ != '>' && *ep != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
+                       ;
+                       
+               /* include the bracket */
+               if (*ep_ == '>') ep_++;
+               
+               /* include the leading quote */         
+               bp_--;
+
+               *ep = ep_;
+               *bp = bp_;
+               return TRUE;
+       }
+
        /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
-       if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_)) 
+       if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
                return FALSE;
 
        /* see if this is <bracketed>; in this case we also scan for the informative part. */
@@ -934,7 +975,7 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
 
        *ep = ep_;
        *bp = bp_;
-
+       
        return result;
 }
 
@@ -1069,7 +1110,6 @@ static void textview_make_clickable_parts(TextView *textview,
                for (last = head.next; last != NULL;
                     normal_text = last->ep, last = last->next) {
                        RemoteURI *uri;
-
                        uri = g_new(RemoteURI, 1);
                        if (last->bp - normal_text > 0)
                                gtk_text_buffer_insert_with_tags_by_name
@@ -1459,8 +1499,8 @@ gboolean textview_search_string(TextView *textview, const gchar *str,
                gchar *text = NULL;
                int i = 0;
                gtk_text_buffer_get_end_iter(buffer, &real_end);
-               text = strdup(gtk_text_buffer_get_text(buffer, &iter, 
-                                                      &real_end, FALSE));
+               text = gtk_text_buffer_get_text(buffer, &iter, 
+                                               &real_end, FALSE);
                
                while (!found && i++ < strlen(text) - 1) {
                        found = (strncasecmp(text+i, str, strlen(str)) == 0);
@@ -1525,8 +1565,8 @@ gboolean textview_search_string_backward(TextView *textview, const gchar *str,
                i = gtk_text_iter_get_offset(&iter) - strlen(str) - 1;
                gtk_text_buffer_get_start_iter(buffer, &real_start);
                
-               text = strdup(gtk_text_buffer_get_text(buffer, &real_start, 
-                                                      &iter, FALSE));
+               text = gtk_text_buffer_get_text(buffer, &real_start, 
+                                               &iter, FALSE);
 
                while (!found && i-- > 0) {
                        found = (strncasecmp(text+i, str, strlen(str)) == 0);
@@ -1757,14 +1797,18 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                if (summaryview)
                        summary_pass_key_press_event(summaryview, event);
                else
-                       textview_scroll_page(textview, FALSE);
+                       textview_scroll_page
+                               (textview,
+                                (event->state &
+                                 (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
                break;
        case GDK_BackSpace:
                textview_scroll_page(textview, TRUE);
                break;
        case GDK_Return:
-               textview_scroll_one_line(textview,
-                                        (event->state & GDK_MOD1_MASK) != 0);
+               textview_scroll_one_line
+                       (textview, (event->state &
+                                   (GDK_SHIFT_MASK|GDK_MOD1_MASK)) != 0);
                break;
        case GDK_Delete:
                if (summaryview)
@@ -1796,70 +1840,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)
@@ -1884,7 +1864,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);
 
@@ -1949,7 +1937,7 @@ static void textview_uri_update(TextView *textview, gint x, gint y)
                
                window = gtk_text_view_get_window(GTK_TEXT_VIEW(textview->text),
                                                  GTK_TEXT_WINDOW_TEXT);
-               gdk_window_set_cursor(window, uri ? hand_cursor : NULL);
+               gdk_window_set_cursor(window, uri ? hand_cursor : text_cursor);
 
                TEXTVIEW_STATUSBAR_POP(textview);
 
@@ -1976,15 +1964,15 @@ static gboolean textview_get_uri_range(TextView *textview,
 {
        GtkTextIter _start_iter, _end_iter;
 
-       _start_iter = *iter;
-       if(!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
-               debug_print("Can't find start.");
+       _end_iter = *iter;
+       if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
+               debug_print("Can't find end");
                return FALSE;
        }
 
-       _end_iter = *iter;
-       if(!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
-               debug_print("Can't find end");
+       _start_iter = _end_iter;
+       if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
+               debug_print("Can't find start.");
                return FALSE;
        }
 
@@ -2000,21 +1988,33 @@ static RemoteURI *textview_get_uri_from_range(TextView *textview,
                                              GtkTextIter *start_iter,
                                              GtkTextIter *end_iter)
 {
-       gint start_pos, end_pos;
+       gint start_pos, end_pos, cur_pos;
        RemoteURI *uri = NULL;
        GSList *cur;
 
        start_pos = gtk_text_iter_get_offset(start_iter);
        end_pos = gtk_text_iter_get_offset(end_iter);
+       cur_pos = gtk_text_iter_get_offset(iter);
 
        for (cur = textview->uri_list; cur != NULL; cur = cur->next) {
                RemoteURI *uri_ = (RemoteURI *)cur->data;
-
                if (start_pos == uri_->start &&
                    end_pos ==  uri_->end) {
                        uri = uri_;
                        break;
-               }
+               } else if (start_pos == uri_->start ||
+                          end_pos == uri_->end) {
+                       /* in case of contiguous links, textview_get_uri_range
+                        * returns a broader range (start of 1st link to end
+                        * of last link).
+                        * In that case, correct link is the one covering
+                        * current iter.
+                        */
+                       if (uri_->start <= cur_pos && cur_pos <= uri_->end) {
+                               uri = uri_;
+                               break;
+                       }
+               } 
        }
 
        return uri;
@@ -2090,9 +2090,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;
                }
        }
@@ -2115,12 +2124,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;
 
@@ -2167,3 +2183,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);
+}