src/account.[ch]
[claws.git] / src / gtkutils.c
index 238eef404b7476d8fcce2c7b957500ce19c82d76..2bcc4b1c4d7405a44535f7dd23f461cdafc6c06f 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,9 @@
 #include "utils.h"
 #include "gtksctree.h"
 #include "codeconv.h"
+#include "stock_pixmap.h"
+#include "menu.h"
+#include "prefs_account.h"
 
 gint gtkut_get_font_width(GdkFont *font)
 {
@@ -248,6 +252,28 @@ void gtkut_combo_set_items(GtkCombo *combo, const gchar *str1, ...)
        g_list_free(combo_items);
 }
 
+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.
@@ -288,8 +314,8 @@ void gtkut_container_remove(GtkContainer *container, GtkWidget *widget)
        gtk_container_remove(container, widget);
 }
 
-gboolean gtkut_text_match_string(GtkSText *text, gint pos, wchar_t *wcs,
-                                gint len, gboolean case_sens)
+gboolean gtkut_stext_match_string(GtkSText *text, gint pos, wchar_t *wcs,
+                                 gint len, gboolean case_sens)
 {
        gint match_count = 0;
 
@@ -310,8 +336,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;
@@ -326,8 +352,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;
@@ -342,25 +368,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;
@@ -389,7 +424,7 @@ void gtkut_widget_wait_for_draw(GtkWidget *widget)
 {
        gboolean flag = FALSE;
 
-       if (!GTK_WIDGET_VISIBLE(widget)) return;
+       if (!GTK_WIDGET_VISIBLE(widget) || !GTK_WIDGET_MAPPED(widget)) return;
 
        gtk_signal_connect(GTK_OBJECT(widget), "draw",
                           GTK_SIGNAL_FUNC(gtkut_widget_draw_cb), &flag);
@@ -466,16 +501,49 @@ 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);    
 }
 
-
+GtkWidget *gtkut_account_menu_new(GList                        *ac_list,
+                                 GtkSignalFunc          callback,
+                                 gpointer               data)
+{
+       GList *cur_ac;
+       GtkWidget *menu;
+       
+       g_return_val_if_fail(ac_list != NULL, NULL);
+
+       menu = gtk_menu_new();
+
+       for (cur_ac = ac_list; cur_ac != NULL; cur_ac = cur_ac->next) {
+               gchar *name;
+               GtkWidget *menuitem;
+               PrefsAccount *account;
+               
+               account = (PrefsAccount *) cur_ac->data;
+               if (account->name)
+                       name = g_strdup_printf("%s: %s <%s>",
+                                              account->account_name,
+                                              account->name,
+                                              account->address);
+               else
+                       name = g_strdup_printf("%s: %s",
+                                              account->account_name,
+                                              account->address);
+               MENUITEM_ADD(menu, menuitem, name, account->account_id);
+               g_free(name);
+               if (callback != NULL)
+                       gtk_signal_connect(GTK_OBJECT(menuitem), "activate",
+                                          callback,
+                                          data);
+       }
+       return menu;
+}