2005-02-17 [paul] 1.0.1cvs11.4
[claws.git] / src / sourcewindow.c
index 80594ab09fdba473aa28b8db4dfd1f85a3bc7dd3..d1328c8830faa1208699f064c1b3bd23d1f998e5 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-2005 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
@@ -20,6 +20,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 #include <gtk/gtkwidget.h>
 #include <gtk/gtkwindow.h>
 #include <stdio.h>
 #include <stdlib.h>
 
-#include "intl.h"
 #include "sourcewindow.h"
-#include "procmsg.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "prefs_common.h"
 
+static void source_window_size_alloc_cb        (GtkWidget      *widget,
+                                        GtkAllocation  *allocation);
 static void source_window_destroy_cb   (GtkWidget      *widget,
                                         SourceWindow   *sourcewin);
-static void key_pressed                        (GtkWidget      *widget,
+static gboolean key_pressed            (GtkWidget      *widget,
                                         GdkEventKey    *event,
                                         SourceWindow   *sourcewin);
 
-static GdkFont *msgfont = NULL;
-
-#define FONT_LOAD(font, s) \
-{ \
-       gchar *fontstr, *p; \
- \
-       Xstrdup_a(fontstr, s, ); \
-       if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
-       font = gdk_font_load(fontstr); \
-}
-
 static void source_window_init()
 {
-       if (msgfont != NULL || prefs_common.textfont == NULL)
-               return;
-
-       if (MB_CUR_MAX == 1) {
-               FONT_LOAD(msgfont, prefs_common.textfont);
-       } else {
-               msgfont = gdk_fontset_load(prefs_common.textfont);
-       }
 }
 
 SourceWindow *source_window_create(void)
@@ -72,32 +54,53 @@ SourceWindow *source_window_create(void)
        GtkWidget *window;
        GtkWidget *scrolledwin;
        GtkWidget *text;
+       static PangoFontDescription *font_desc = NULL;
 
-       debug_print(_("Creating source window...\n"));
+       static GdkGeometry geometry;
+       
+       debug_print("Creating source window...\n");
        sourcewin = g_new0(SourceWindow, 1);
 
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
-       gtk_window_set_wmclass(GTK_WINDOW(window), "source_window", "Sylpheed");
-       gtk_window_set_policy(GTK_WINDOW(window), TRUE, TRUE, FALSE);
-       gtk_widget_set_usize(window, 600, 500);
-       gtk_signal_connect(GTK_OBJECT(window), "destroy",
-                          GTK_SIGNAL_FUNC(source_window_destroy_cb),
-                          sourcewin);
-       gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
-                          GTK_SIGNAL_FUNC(key_pressed), sourcewin);
+       gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
+       gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
+                                   prefs_common.sourcewin_height);
+       
+       if (!geometry.min_height) {
+               geometry.min_width = 400;
+               geometry.min_height = 320;
+       }
+       gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
+                                     GDK_HINT_MIN_SIZE);
+
+       g_signal_connect(G_OBJECT(window), "size_allocate",
+                        G_CALLBACK(source_window_size_alloc_cb),
+                        sourcewin);
+       g_signal_connect(G_OBJECT(window), "destroy",
+                        G_CALLBACK(source_window_destroy_cb),
+                        sourcewin);
+       g_signal_connect(G_OBJECT(window), "key_press_event",
+                        G_CALLBACK(key_pressed), sourcewin);
        gtk_widget_realize(window);
 
        scrolledwin = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
-                                      GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
+                                      GTK_POLICY_AUTOMATIC,
+                                      GTK_POLICY_AUTOMATIC);
+       gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
+                                           GTK_SHADOW_IN);
        gtk_container_add(GTK_CONTAINER(window), scrolledwin);
        gtk_widget_show(scrolledwin);
 
-       text = gtk_text_new(gtk_scrolled_window_get_hadjustment
-                           (GTK_SCROLLED_WINDOW(scrolledwin)),
-                           gtk_scrolled_window_get_vadjustment
-                           (GTK_SCROLLED_WINDOW(scrolledwin)));
+       text = gtk_text_view_new();
+       gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
+       gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
+       if (!font_desc && prefs_common.textfont)
+               font_desc = pango_font_description_from_string
+                                       (prefs_common.textfont);
+       if (font_desc)
+               gtk_widget_modify_font(text, font_desc);
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
        gtk_widget_show(text);
 
@@ -138,27 +141,42 @@ void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
                return;
        }
 
-       debug_print(_("Displaying the source of %s ...\n"), file);
+       debug_print("Displaying the source of %s ...\n", file);
 
        title = g_strdup_printf(_("%s - Source"), file);
        gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
        g_free(title);
        g_free(file);
 
-       gtk_text_freeze(GTK_TEXT(sourcewin->text));
-
        while (fgets(buf, sizeof(buf), fp) != NULL)
                source_window_append(sourcewin, buf);
 
-       gtk_text_thaw(GTK_TEXT(sourcewin->text));
-
        fclose(fp);
 }
 
 void source_window_append(SourceWindow *sourcewin, const gchar *str)
 {
-       gtk_text_insert(GTK_TEXT(sourcewin->text), msgfont, NULL, NULL,
-                       str, -1);
+       GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+       GtkTextIter iter;
+       gchar *out;
+       gint len;
+
+       len = strlen(str) + 1;
+       Xalloca(out, len, return);
+       conv_utf8todisp(out, len, str);
+
+       gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
+       gtk_text_buffer_insert(buffer, &iter, out, -1);
+}
+
+static void source_window_size_alloc_cb(GtkWidget *widget,
+                                       GtkAllocation *allocation)
+{
+       g_return_if_fail(allocation != NULL);
+
+       prefs_common.sourcewin_width  = allocation->width;
+       prefs_common.sourcewin_height = allocation->height;
 }
 
 static void source_window_destroy_cb(GtkWidget *widget,
@@ -167,9 +185,27 @@ static void source_window_destroy_cb(GtkWidget *widget,
        source_window_destroy(sourcewin);
 }
 
-static void key_pressed(GtkWidget *widget, GdkEventKey *event,
-                       SourceWindow *sourcewin)
+static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
+                           SourceWindow *sourcewin)
 {
-       if (event && event->keyval == GDK_Escape)
+
+       if (!event || !sourcewin) return FALSE;
+       
+       switch (event->keyval) {
+       case GDK_A:
+       case GDK_a:
+               if ((event->state & GDK_CONTROL_MASK) != 0)
+                       gtk_editable_select_region(GTK_EDITABLE(sourcewin->text), 0, -1);
+               break;
+       case GDK_W:
+       case GDK_w:
+               if ((event->state & GDK_CONTROL_MASK) != 0)
+                       gtk_widget_destroy(sourcewin->window);
+               break;
+       case GDK_Escape:
                gtk_widget_destroy(sourcewin->window);
+               break;
+       }
+
+       return FALSE;
 }