sync with sylpheed 0.6.5cvs14
[claws.git] / src / gtkutils.c
index bf5df7aef5d8f27b3dfb942bbeea15697d5878ce..9c0d01a1e06c5e275390a737d7627ead4dbf54cc 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2001 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
 #include <gtk/gtkbindings.h>
 #include <stdarg.h>
 
+#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
+#  include <wchar.h>
+#  include <wctype.h>
+#endif
+
 #include "intl.h"
 #include "gtkutils.h"
 #include "utils.h"
@@ -243,6 +248,79 @@ 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)
+{
+       gint match_count = 0;
+
+       for (; match_count < len; pos++, match_count++) {
+               if (case_sens) {
+                       if (GTK_TEXT_INDEX(text, pos) != wcs[match_count])
+                               break;
+               } else {
+                       if (towlower(GTK_TEXT_INDEX(text, pos)) !=
+                           towlower(wcs[match_count]))
+                               break;
+               }
+       }
+
+       if (match_count == len)
+               return TRUE;
+       else
+               return FALSE;
+}
+
+guint gtkut_text_str_compare_n(GtkText *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);
+               if (ch1 != ch2)
+                       break;
+       }
+
+       return i;
+}
+
+guint gtkut_text_str_compare(GtkText *text, guint start_pos, guint text_len,
+                            const gchar *str)
+{
+       wchar_t *wcs;
+       guint len;
+       gboolean result;
+
+       if (!str) return 0;
+
+       wcs = strdup_mbstowcs(str);
+       if (!wcs) return 0;
+       len = wcslen(wcs);
+
+       if (len > text_len - start_pos)
+               result = FALSE;
+       else
+               result = gtkut_text_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)
+{
+       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://"))
+               return TRUE;
+
+       return FALSE;
+}
+
 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
 {
        GtkStyle *style, *new_style;
@@ -292,8 +370,8 @@ void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
 
        sx = gdk_screen_width();
        sy = gdk_screen_height();
-       x %= sx; if (x < 0) x += sx;
-       y %= sy; if (y < 0) y += sy;
+       x %= sx; if (x < 0) x = 0;
+       y %= sy; if (y < 0) y = 0;
        *px = x;
        *py = y;
 }