Sync with hiro's cvs 10 to 17.
[claws.git] / src / gtkutils.c
index 9c0d01a1e06c5e275390a737d7627ead4dbf54cc..be927178fc0be98ed72642dfec7511835b7d6f00 100644 (file)
@@ -248,17 +248,57 @@ void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
        g_list_free(combo_items);
 }
 
-gboolean gtkut_text_match_string(GtkText *text, gint pos, wchar_t *wcs,
-                                gint len, gboolean case_sens)
+/*
+ * 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;
 
        for (; match_count < len; pos++, match_count++) {
                if (case_sens) {
-                       if (GTK_TEXT_INDEX(text, pos) != wcs[match_count])
+                       if (GTK_STEXT_INDEX(text, pos) != wcs[match_count])
                                break;
                } else {
-                       if (towlower(GTK_TEXT_INDEX(text, pos)) !=
+                       if (towlower(GTK_STEXT_INDEX(text, pos)) !=
                            towlower(wcs[match_count]))
                                break;
                }
@@ -270,15 +310,15 @@ gboolean gtkut_text_match_string(GtkText *text, gint pos, wchar_t *wcs,
                return FALSE;
 }
 
-guint gtkut_text_str_compare_n(GtkText *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;
 
        for (i = 0; i < len && pos1 + i < text_len && pos2 + i < text_len; i++) {
-               ch1 = GTK_TEXT_INDEX(text, pos1 + i);
-               ch2 = GTK_TEXT_INDEX(text, pos2 + i);
+               ch1 = GTK_STEXT_INDEX(text, pos1 + i);
+               ch2 = GTK_STEXT_INDEX(text, pos2 + i);
                if (ch1 != ch2)
                        break;
        }
@@ -286,8 +326,8 @@ guint gtkut_text_str_compare_n(GtkText *text, guint pos1, guint pos2,
        return i;
 }
 
-guint gtkut_text_str_compare(GtkText *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,20 +342,20 @@ guint gtkut_text_str_compare(GtkText *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(GtkText *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://"))
                return TRUE;
 
        return FALSE;