sync with 0.8.6cvs22
[claws.git] / src / gtkutils.c
index e65adbabdb05ab13c9d80f2c03c1ff434758232d..630e9d42a5ac0c79c841d75b55be99d2b737067a 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-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
 #include <gtk/gtkcombo.h>
 #include <gtk/gtkthemes.h>
 #include <gtk/gtkbindings.h>
+#include <stdlib.h>
 #include <stdarg.h>
+#include <sys/stat.h>
+
+#if (HAVE_WCTYPE_H && HAVE_WCHAR_H)
+#  include <wchar.h>
+#  include <wctype.h>
+#endif
 
 #include "intl.h"
 #include "gtkutils.h"
 #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)
 {
@@ -70,6 +80,42 @@ gint gtkut_get_font_height(GdkFont *font)
        return height;
 }
 
+GdkFont *gtkut_font_load(const gchar *fontset_name)
+{
+       GdkFont *font;
+
+       g_return_val_if_fail(fontset_name != NULL, NULL);
+
+       if (conv_get_current_charset() == C_US_ASCII)
+               font = gtkut_font_load_from_fontset(fontset_name);
+       else
+               font = gdk_fontset_load(fontset_name);
+
+       return font;
+}
+
+GdkFont *gtkut_font_load_from_fontset(const gchar *fontset_name)
+{
+       GdkFont *font = NULL;
+
+       if (strchr(fontset_name, ',') == NULL) {
+               font = gdk_font_load(fontset_name);
+       } else {
+               gchar **fonts;
+               gint i;
+
+               fonts = g_strsplit(fontset_name, ",", -1);
+               for (i = 0; fonts[i] != NULL && fonts[i][0] != '\0';
+                    i++) {
+                       font = gdk_font_load(fonts[i]);
+                       if (font) break;
+               }
+               g_strfreev(fonts);
+       }
+
+       return font;
+}
+
 void gtkut_convert_int_to_gdk_color(gint rgbvalue, GdkColor *color)
 {
        g_return_if_fail(color != NULL);
@@ -209,6 +255,22 @@ void gtkut_ctree_expand_parent_all(GtkCTree *ctree, GtkCTreeNode *node)
                gtk_ctree_expand(ctree, node);
 }
 
+gboolean gtkut_ctree_node_is_parent(GtkCTreeNode *parent, GtkCTreeNode *node)
+{
+       GtkCTreeNode *tmp;
+       g_return_val_if_fail(node != NULL, FALSE);
+       g_return_val_if_fail(parent != NULL, FALSE);
+       tmp = node;
+       
+       while (tmp) {
+               if(GTK_CTREE_ROW(tmp)->parent && GTK_CTREE_ROW(tmp)->parent == parent)
+                       return TRUE;
+               tmp = GTK_CTREE_ROW(tmp)->parent;
+       }
+       
+       return FALSE;
+}
+
 void gtkut_ctree_set_focus_row(GtkCTree *ctree, GtkCTreeNode *node)
 {
        gtkut_clist_set_focus_row(GTK_CLIST(ctree),
@@ -243,6 +305,108 @@ 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.
+ */
+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);
+}
+
+void gtkut_window_popup(GtkWidget *window)
+{
+       gint x, y, sx, sy, new_x, new_y;
+
+       g_return_if_fail(window != NULL);
+       g_return_if_fail(window->window != NULL);
+
+       sx = gdk_screen_width();
+       sy = gdk_screen_height();
+
+       gdk_window_get_origin(window->window, &x, &y);
+       new_x = x % sx; if (new_x < 0) new_x = 0;
+       new_y = y % sy; if (new_y < 0) new_y = 0;
+       if (new_x != x || new_y != y)
+               gdk_window_move(window->window, new_x, new_y);
+
+       gdk_window_raise(window->window);
+       gdk_window_show(window->window);
+}
+
+void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
+{
+       gint x, y;
+       gint sx, sy;
+
+       g_return_if_fail(widget != NULL);
+       g_return_if_fail(widget->window != NULL);
+
+       sx = gdk_screen_width();
+       sy = gdk_screen_height();
+
+       /* gdk_window_get_root_origin ever return *rootwindow*'s position */
+       gdk_window_get_root_origin(widget->window, &x, &y);
+
+       x %= sx; if (x < 0) x = 0;
+       y %= sy; if (y < 0) y = 0;
+       *px = x;
+       *py = y;
+}
+
 void gtkut_widget_disable_theme_engine(GtkWidget *widget)
 {
        GtkStyle *style, *new_style;
@@ -271,7 +435,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);
@@ -279,25 +443,6 @@ void gtkut_widget_wait_for_draw(GtkWidget *widget)
                gtk_main_iteration();
 }
 
-void gtkut_widget_get_uposition(GtkWidget *widget, gint *px, gint *py)
-{
-       gint x, y;
-       gint sx, sy;
-
-       g_return_if_fail(widget != NULL);
-       g_return_if_fail(widget->window != NULL);
-
-       /* gdk_window_get_root_origin ever return *rootwindow*'s position*/
-       gdk_window_get_root_origin(widget->window, &x, &y);
-
-       sx = gdk_screen_width();
-       sy = gdk_screen_height();
-       x %= sx; if (x < 0) x += sx;
-       y %= sy; if (y < 0) y += sy;
-       *px = x;
-       *py = y;
-}
-
 static void gtkut_clist_bindings_add(GtkWidget *clist)
 {
        GtkBindingSet *binding_set;
@@ -331,3 +476,66 @@ void gtkut_widget_init(void)
        gtkut_clist_bindings_add(clist);
        gtk_widget_unref(clist);
 }
+
+void gtkut_widget_set_app_icon(GtkWidget *widget)
+{
+#include "pixmaps/sylpheed.xpm"        
+       static GdkPixmap *sylpheedxpm;
+       static GdkBitmap *sylpheedxpmmask;
+       
+       g_return_if_fail(widget != NULL);
+       g_return_if_fail(widget->window != NULL);
+       if (!sylpheedxpm) {
+               PIXMAP_CREATE(widget, sylpheedxpm, sylpheedxpmmask, sylpheed_xpm);
+       }               
+       gdk_window_set_icon(widget->window, NULL, sylpheedxpm, sylpheedxpmmask);
+}
+
+void gtkut_widget_set_composer_icon(GtkWidget *widget)
+{
+       static GdkPixmap *xpm;
+       static GdkBitmap *bmp;
+
+       g_return_if_fail(widget != NULL);
+       g_return_if_fail(widget->window != NULL);
+       if (!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;
+}