recognise urls that just begin with 'www'
[claws.git] / src / gtkutils.c
index a9689078802e679599f06429bf65b14cfbec7a2a..f2125788c150d734457c3aba5aab1fd213611cc2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2001 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2002 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
@@ -32,6 +32,7 @@
 #include <gtk/gtkthemes.h>
 #include <gtk/gtkbindings.h>
 #include <stdarg.h>
+#include <sys/stat.h>
 
 #if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
 #  include <wchar.h>
@@ -43,6 +44,7 @@
 #include "utils.h"
 #include "gtksctree.h"
 #include "codeconv.h"
+#include "stock_pixmap.h"
 
 gint gtkut_get_font_width(GdkFont *font)
 {
@@ -248,8 +250,70 @@ void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
        g_list_free(combo_items);
 }
 
-gboolean gtkut_text_match_string(GtkSText *text, gint pos, wchar_t *wcs,
-                                gint len, gboolean case_sens)
+gchar *gtkut_editable_get_selection(GtkEditable *editable)
+{
+       guint start_pos, end_pos;
+
+       g_return_val_if_fail(editable != NULL, NULL);
+
+       if (!editable->has_selection) return NULL;
+
+       if (editable->selection_start_pos == editable->selection_end_pos)
+               return NULL;
+
+       if (editable->selection_start_pos < editable->selection_end_pos) {
+               start_pos = editable->selection_start_pos;
+               end_pos = editable->selection_end_pos;
+       } else {
+               start_pos = editable->selection_end_pos;
+               end_pos = editable->selection_start_pos;
+       }
+
+       return gtk_editable_get_chars(editable, start_pos, end_pos);
+}
+
+/*
+ * Walk through the widget tree and disclaim the selection from all currently
+ * realized GtkEditable widgets.
+ */
+static void gtkut_check_before_remove(GtkWidget *widget, gpointer unused)
+{
+       g_return_if_fail(widget != NULL);
+
+       if (!GTK_WIDGET_REALIZED(widget))
+               return; /* all nested widgets must be unrealized too */
+       if (GTK_IS_CONTAINER(widget))
+               gtk_container_forall(GTK_CONTAINER(widget),
+                                    gtkut_check_before_remove, NULL);
+       if (GTK_IS_EDITABLE(widget))
+               gtk_editable_claim_selection(GTK_EDITABLE(widget),
+                                            FALSE, GDK_CURRENT_TIME);
+}
+
+/*
+ * Wrapper around gtk_container_remove to work around a bug in GtkText and
+ * GtkEntry (in all GTK+ versions up to and including at least 1.2.10).
+ *
+ * The problem is that unrealizing a GtkText or GtkEntry widget which has the
+ * active selection completely messes up selection handling, leading to
+ * non-working selections and crashes.  Removing a widget from its container
+ * implies unrealizing it and all its child widgets; this triggers the bug if
+ * the removed widget or any of its children is GtkText or GtkEntry.  As a
+ * workaround, this function walks through the widget subtree before removing
+ * and disclaims the selection from all GtkEditable widgets found.
+ *
+ * A similar workaround may be needed for gtk_widget_reparent(); currently it
+ * is not necessary because Sylpheed does not use gtk_widget_reparent() for
+ * GtkEditable widgets or containers holding such widgets.
+ */
+void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
+{
+       gtkut_check_before_remove(widget, NULL);
+       gtk_container_remove(container, widget);
+}
+
+gboolean gtkut_stext_match_string(GtkSText *text, gint pos, wchar_t *wcs,
+                                 gint len, gboolean case_sens)
 {
        gint match_count = 0;
 
@@ -270,8 +334,8 @@ gboolean gtkut_text_match_string(GtkSText *text, gint pos, wchar_t *wcs,
                return FALSE;
 }
 
-guint gtkut_text_str_compare_n(GtkSText *text, guint pos1, guint pos2,
-                              guint len, guint text_len)
+guint gtkut_stext_str_compare_n(GtkSText *text, guint pos1, guint pos2,
+                               guint len, guint text_len)
 {
        guint i;
        GdkWChar ch1, ch2;
@@ -286,8 +350,8 @@ guint gtkut_text_str_compare_n(GtkSText *text, guint pos1, guint pos2,
        return i;
 }
 
-guint gtkut_text_str_compare(GtkSText *text, guint start_pos, guint text_len,
-                            const gchar *str)
+guint gtkut_stext_str_compare(GtkSText *text, guint start_pos, guint text_len,
+                             const gchar *str)
 {
        wchar_t *wcs;
        guint len;
@@ -302,25 +366,34 @@ guint gtkut_text_str_compare(GtkSText *text, guint start_pos, guint text_len,
        if (len > text_len - start_pos)
                result = FALSE;
        else
-               result = gtkut_text_match_string(text, start_pos, wcs, len,
-                                                TRUE);
+               result = gtkut_stext_match_string(text, start_pos, wcs, len,
+                                                 TRUE);
 
        g_free(wcs);
 
        return result ? len : 0;
 }
 
-gboolean gtkut_text_is_uri_string(GtkSText *text,
-                                 guint start_pos, guint text_len)
+gboolean gtkut_stext_is_uri_string(GtkSText *text,
+                                  guint start_pos, guint text_len)
 {
-       if (gtkut_text_str_compare(text, start_pos, text_len, "http://") ||
-           gtkut_text_str_compare(text, start_pos, text_len, "ftp://")  ||
-           gtkut_text_str_compare(text, start_pos, text_len, "https://"))
+       if (gtkut_stext_str_compare(text, start_pos, text_len, "http://") ||
+           gtkut_stext_str_compare(text, start_pos, text_len, "ftp://")  ||
+           gtkut_stext_str_compare(text, start_pos, text_len, "https://")||
+           gtkut_stext_str_compare(text, start_pos, text_len, "www."))
                return TRUE;
 
        return FALSE;
 }
 
+void gtk_stext_clear(GtkSText *text)
+{
+       gtk_stext_freeze(text);
+       gtk_stext_set_point(text, 0);
+       gtk_stext_forward_delete(text, gtk_stext_get_length(text));
+       gtk_stext_thaw(text);
+}
+
 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
 {
        GtkStyle *style, *new_style;
@@ -426,16 +499,13 @@ void gtkut_widget_set_app_icon(GtkWidget *widget)
 
 void gtkut_widget_set_composer_icon(GtkWidget *widget)
 {
-#include "pixmaps/stock_mail_compose.xpm"
        static GdkPixmap *xpm;
        static GdkBitmap *bmp;
 
        g_return_if_fail(widget != NULL);
        g_return_if_fail(widget->window != NULL);
        if (!xpm) {
-               PIXMAP_CREATE(widget, xpm, bmp, stock_mail_compose_xpm);
+               stock_pixmap_gdk(widget, STOCK_PIXMAP_MAIL_COMPOSE, &xpm, &bmp);
        }
        gdk_window_set_icon(widget->window, NULL, xpm, bmp);    
 }
-
-