inital gtk2 patch
[claws.git] / src / alertpanel.c
index 9fa164771c30a0136c74b1f404e17a68bb0da95e..1387c5f9c9660e63f6f2e5d9c1616ab18715c5e7 100644 (file)
 #include <gdk/gdkkeysyms.h>
 
 #include "intl.h"
+#include "mainwindow.h"
 #include "alertpanel.h"
 #include "manage_window.h"
 #include "utils.h"
 #include "gtkutils.h"
+#include "inc.h"
+#include "log.h"
+#include "logwindow.h"
+#include "prefs_common.h"
 
-#define TITLE_FONT     "-*-helvetica-medium-r-normal--17-*-*-*-*-*-*-*," \
-                       "-*-*-medium-r-normal--16-*-*-*-*-*-*-*,*"
-#define MESSAGE_FONT   "-*-helvetica-medium-r-normal--14-*-*-*-*-*-*-*," \
-                       "-*-*-medium-r-normal--14-*-*-*-*-*-*-*,*"
 #define ALERT_PANEL_WIDTH      380
 #define TITLE_HEIGHT           72
 #define MESSAGE_HEIGHT         62
 
+#define DEFAULT_TITLE_FONT     "Helvetica 16"
+
 static gboolean alertpanel_is_open = FALSE;
 static AlertValue value;
 
@@ -49,7 +52,8 @@ static void alertpanel_create         (const gchar    *title,
                                         const gchar    *button1_label,
                                         const gchar    *button2_label,
                                         const gchar    *button3_label,
-                                        gboolean        can_disable);
+                                        gboolean        can_disable,
+                                        GtkWidget      *custom_widget);
 
 static void alertpanel_button_toggled  (GtkToggleButton        *button,
                                         gpointer                data);
@@ -58,7 +62,7 @@ static void alertpanel_button_clicked (GtkWidget              *widget,
 static gint alertpanel_deleted         (GtkWidget              *widget,
                                         GdkEventAny            *event,
                                         gpointer                data);
-static void alertpanel_close           (GtkWidget              *widget,
+static gboolean alertpanel_close       (GtkWidget              *widget,
                                         GdkEventAny            *event,
                                         gpointer                data);
 
@@ -74,13 +78,31 @@ AlertValue alertpanel(const gchar *title,
                alertpanel_is_open = TRUE;
 
        alertpanel_create(title, message, button1_label, button2_label,
-                         button3_label, FALSE);
+                         button3_label, FALSE, NULL);
        alertpanel_show();
 
        debug_print("return value = %d\n", value);
        return value;
 }
 
+AlertValue alertpanel_with_widget(const gchar *title,
+                                 const gchar *message,
+                                 const gchar *button1_label,
+                                 const gchar *button2_label,
+                                 const gchar *button3_label,
+                                 GtkWidget *widget)
+{
+       if (alertpanel_is_open)
+               return -1;
+       else
+               alertpanel_is_open = TRUE;
+       alertpanel_create(title, message, button1_label, button2_label, 
+                         button3_label, FALSE, widget);
+       alertpanel_show();
+
+       debug_print("return value = %d\n", value);
+       return value;
+}
 void alertpanel_message(const gchar *title, const gchar *message)
 {
        if (alertpanel_is_open)
@@ -88,7 +110,7 @@ void alertpanel_message(const gchar *title, const gchar *message)
        else
                alertpanel_is_open = TRUE;
 
-       alertpanel_create(title, message, NULL, NULL, NULL, FALSE);
+       alertpanel_create(title, message, NULL, NULL, NULL, FALSE, NULL);
        alertpanel_show();
 }
 
@@ -100,7 +122,7 @@ AlertValue alertpanel_message_with_disable(const gchar *title,
        else
                alertpanel_is_open = TRUE;
 
-       alertpanel_create(title, message, NULL, NULL, NULL, TRUE);
+       alertpanel_create(title, message, NULL, NULL, NULL, TRUE, NULL);
        alertpanel_show();
 
        return value;
@@ -145,16 +167,41 @@ void alertpanel_error(const gchar *format, ...)
        alertpanel_message(_("Error"), buf);
 }
 
+/*!
+ *\brief       display an error with a View Log button
+ *
+ */
+void alertpanel_error_log(const gchar *format, ...)
+{
+       va_list args;
+       int val;
+       MainWindow *mainwin;
+       gchar buf[256];
+
+       va_start(args, format);
+       g_vsnprintf(buf, sizeof(buf), format, args);
+       va_end(args);
+       strretchomp(buf);
+
+       mainwin = mainwindow_get_mainwindow();
+       
+       if (mainwin && mainwin->logwin) {
+               val = alertpanel(_("Error"), buf, _("OK"), _("View log"), NULL);
+               if (val == G_ALERTALTERNATE)
+                       log_window_show(mainwin->logwin);
+       } else
+               alertpanel_error(buf);
+}
+
 static void alertpanel_show(void)
 {
        gtk_window_set_modal(GTK_WINDOW(dialog), TRUE);
        manage_window_set_transient(GTK_WINDOW(dialog));
        value = G_ALERTWAIT;
 
-       /* ungrab the mouse events ? */
        if (gdk_pointer_is_grabbed())
                gdk_pointer_ungrab(GDK_CURRENT_TIME);
-
+       inc_lock();
        while ((value & G_ALERT_VALUE_MASK) == G_ALERTWAIT)
                gtk_main_iteration();
 
@@ -162,6 +209,7 @@ static void alertpanel_show(void)
        GTK_EVENTS_FLUSH();
 
        alertpanel_is_open = FALSE;
+       inc_unlock();
 }
 
 static void alertpanel_create(const gchar *title,
@@ -169,13 +217,15 @@ static void alertpanel_create(const gchar *title,
                              const gchar *button1_label,
                              const gchar *button2_label,
                              const gchar *button3_label,
-                             gboolean     can_disable)
+                             gboolean     can_disable,
+                             GtkWidget *custom_widget)
 {
-       static GdkFont *titlefont;
-       GtkStyle *style;
+       static PangoFontDescription *font_desc;
        GtkWidget *label;
        GtkWidget *hbox;
        GtkWidget *vbox;
+       GtkWidget *spc_vbox;
+       GtkWidget *msg_vbox;
        GtkWidget *disable_chkbtn;
        GtkWidget *confirm_area;
        GtkWidget *button1;
@@ -184,20 +234,20 @@ static void alertpanel_create(const gchar *title,
        const gchar *label2;
        const gchar *label3;
 
-       debug_print(_("Creating alert panel dialog...\n"));
+       debug_print("Creating alert panel dialog...\n");
 
        dialog = gtk_dialog_new();
        gtk_window_set_title(GTK_WINDOW(dialog), title);
        gtk_window_set_policy(GTK_WINDOW(dialog), FALSE, FALSE, FALSE);
        gtk_container_set_border_width
                (GTK_CONTAINER(GTK_DIALOG(dialog)->action_area), 5);
-       gtk_window_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
-       gtk_signal_connect(GTK_OBJECT(dialog), "delete_event",
-                          GTK_SIGNAL_FUNC(alertpanel_deleted),
-                          (gpointer)G_ALERTOTHER);
-       gtk_signal_connect(GTK_OBJECT(dialog), "key_press_event",
-                          GTK_SIGNAL_FUNC(alertpanel_close),
-                          (gpointer)G_ALERTOTHER);
+       gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER);
+       g_signal_connect(G_OBJECT(dialog), "delete_event",
+                        G_CALLBACK(alertpanel_deleted),
+                        (gpointer)G_ALERTOTHER);
+       g_signal_connect(G_OBJECT(dialog), "key_press_event",
+                        G_CALLBACK(alertpanel_close),
+                        (gpointer)G_ALERTOTHER);
        gtk_widget_realize(dialog);
 
        /* for title label */
@@ -209,28 +259,67 @@ static void alertpanel_create(const gchar *title,
        /* pixmapwid = create_pixmapwid(dialog, GNUstep_xpm); */
        /* gtk_box_pack_start(GTK_BOX(hbox), pixmapwid, FALSE, FALSE, 16); */
        label = gtk_label_new(title);
-       gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 16);
-       style = gtk_style_copy(gtk_widget_get_style(label));
-       if (!titlefont)
-               titlefont = gdk_fontset_load(TITLE_FONT);
-       if (titlefont)
-               style->font = titlefont;
-       gtk_widget_set_style(label, style);
+       gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 16);
+       gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
+       if (!font_desc) {
+               gchar *fontstr = prefs_common.titlefont
+                                       ? prefs_common.titlefont
+                                       : DEFAULT_TITLE_FONT;
+               font_desc = pango_font_description_from_string (fontstr);
+       }
+       if (font_desc) {
+               gtk_widget_modify_font (label, font_desc);
+       }
 
        /* for message and button(s) */
        vbox = gtk_vbox_new(FALSE, 0);
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->action_area),
                          vbox);
 
+       spc_vbox = gtk_vbox_new(FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
+       gtk_widget_set_size_request(spc_vbox, -1, 16);
+
+       msg_vbox = gtk_vbox_new(FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), msg_vbox, FALSE, FALSE, 0);
+
        /* for message label */
        hbox = gtk_hbox_new(FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 20);
+       gtk_box_pack_start(GTK_BOX(msg_vbox), hbox, FALSE, FALSE, 0);
 
        /* message label */
        label = gtk_label_new(message);
-       gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 32);
+       gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 24);
        gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_LEFT);
 
+       /* Claws: custom widget */
+       if (custom_widget) {
+               GtkWidget *custom_hbox = gtk_hbox_new(FALSE, 0);
+               gtk_box_pack_start(GTK_BOX(msg_vbox), custom_hbox, FALSE,
+                                  FALSE, 0);
+               gtk_box_pack_start(GTK_BOX(custom_hbox), custom_widget, FALSE,
+                                  FALSE, 24);
+       }
+       if (can_disable) {
+               hbox = gtk_hbox_new(FALSE, 0);
+               gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
+               gtk_container_set_border_width(GTK_CONTAINER(hbox), 8);
+
+               disable_chkbtn = gtk_check_button_new_with_label
+                       (_("Show this message next time"));
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
+                                            TRUE);
+               gtk_box_pack_start(GTK_BOX(hbox), disable_chkbtn,
+                                  FALSE, FALSE, 0);
+               g_signal_connect(G_OBJECT(disable_chkbtn), "toggled",
+                                G_CALLBACK(alertpanel_button_toggled),
+                                GUINT_TO_POINTER(G_ALERTDISABLE));
+       } else {
+               spc_vbox = gtk_vbox_new(FALSE, 0);
+               gtk_box_pack_start(GTK_BOX(vbox), spc_vbox, FALSE, FALSE, 0);
+               gtk_widget_set_size_request(spc_vbox, -1, 20);
+       }
+
        /* for button(s) */
        if (!button1_label)
                button1_label = _("OK");
@@ -256,29 +345,17 @@ static void alertpanel_create(const gchar *title,
                gtk_widget_grab_focus(button3);
        }
 
-       gtk_signal_connect(GTK_OBJECT(button1), "clicked",
-                          GTK_SIGNAL_FUNC(alertpanel_button_clicked),
-                          GUINT_TO_POINTER(G_ALERTDEFAULT));
+       g_signal_connect(G_OBJECT(button1), "clicked",
+                        G_CALLBACK(alertpanel_button_clicked),
+                        GUINT_TO_POINTER(G_ALERTDEFAULT));
        if (button2_label)
-               gtk_signal_connect(GTK_OBJECT(button2), "clicked",
-                                  GTK_SIGNAL_FUNC(alertpanel_button_clicked),
-                                  GUINT_TO_POINTER(G_ALERTALTERNATE));
+               g_signal_connect(G_OBJECT(button2), "clicked",
+                                G_CALLBACK(alertpanel_button_clicked),
+                                GUINT_TO_POINTER(G_ALERTALTERNATE));
        if (button3_label)
-               gtk_signal_connect(GTK_OBJECT(button3), "clicked",
-                                  GTK_SIGNAL_FUNC(alertpanel_button_clicked),
-                                  GUINT_TO_POINTER(G_ALERTOTHER));
-
-       if (can_disable) {
-               disable_chkbtn = gtk_check_button_new_with_label
-                       (_("Show this message next time"));
-               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(disable_chkbtn),
-                                            TRUE);
-               gtk_box_pack_end(GTK_BOX(vbox), disable_chkbtn,
-                                FALSE, FALSE, 0);
-               gtk_signal_connect(GTK_OBJECT(disable_chkbtn), "toggled",
-                                  GTK_SIGNAL_FUNC(alertpanel_button_toggled),
-                                  GUINT_TO_POINTER(G_ALERTDISABLE));
-       }
+               g_signal_connect(G_OBJECT(button3), "clicked",
+                                G_CALLBACK(alertpanel_button_clicked),
+                                GUINT_TO_POINTER(G_ALERTOTHER));
 
        gtk_widget_show_all(dialog);
 }
@@ -304,12 +381,13 @@ static gint alertpanel_deleted(GtkWidget *widget, GdkEventAny *event,
        return TRUE;
 }
 
-static void alertpanel_close(GtkWidget *widget, GdkEventAny *event,
+static gboolean alertpanel_close(GtkWidget *widget, GdkEventAny *event,
                             gpointer data)
 {
        if (event->type == GDK_KEY_PRESS)
                if (((GdkEventKey *)event)->keyval != GDK_Escape)
-                       return;
+                       return FALSE;
 
        value = (value & ~G_ALERT_VALUE_MASK) | (AlertValue)data;
+       return FALSE;
 }