sourcewindow.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
  3. * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. */
  19. #include "defs.h"
  20. #include <glib.h>
  21. #include <glib/gi18n.h>
  22. #include <gdk/gdkkeysyms.h>
  23. #include <gtk/gtk.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include "sourcewindow.h"
  27. #include "utils.h"
  28. #include "gtkutils.h"
  29. #include "prefs_common.h"
  30. static void source_window_size_alloc_cb (GtkWidget *widget,
  31. GtkAllocation *allocation);
  32. static gint source_window_delete_cb (GtkWidget *widget,
  33. GdkEventAny *event,
  34. SourceWindow *sourcewin);
  35. static gboolean key_pressed (GtkWidget *widget,
  36. GdkEventKey *event,
  37. SourceWindow *sourcewin);
  38. static void source_window_append (SourceWindow *sourcewin,
  39. const gchar *str);
  40. static void source_window_destroy (SourceWindow *sourcewin);
  41. static void source_window_init()
  42. {
  43. }
  44. SourceWindow *source_window_create(void)
  45. {
  46. SourceWindow *sourcewin;
  47. GtkWidget *window;
  48. GtkWidget *scrolledwin;
  49. GtkWidget *text;
  50. static PangoFontDescription *font_desc = NULL;
  51. static GdkGeometry geometry;
  52. debug_print("Creating source window...\n");
  53. sourcewin = g_new0(SourceWindow, 1);
  54. window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "sourcewindow");
  55. gtk_window_set_title(GTK_WINDOW(window), _("Source of the message"));
  56. gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
  57. gtk_widget_set_size_request(window, prefs_common.sourcewin_width,
  58. prefs_common.sourcewin_height);
  59. if (!geometry.min_height) {
  60. geometry.min_width = 400;
  61. geometry.min_height = 320;
  62. }
  63. gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
  64. GDK_HINT_MIN_SIZE);
  65. g_signal_connect(G_OBJECT(window), "size_allocate",
  66. G_CALLBACK(source_window_size_alloc_cb),
  67. sourcewin);
  68. g_signal_connect(G_OBJECT(window), "delete_event",
  69. G_CALLBACK(source_window_delete_cb), sourcewin);
  70. g_signal_connect(G_OBJECT(window), "key_press_event",
  71. G_CALLBACK(key_pressed), sourcewin);
  72. gtk_widget_realize(window);
  73. scrolledwin = gtk_scrolled_window_new(NULL, NULL);
  74. gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
  75. GTK_POLICY_AUTOMATIC,
  76. GTK_POLICY_AUTOMATIC);
  77. gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
  78. GTK_SHADOW_IN);
  79. gtk_container_add(GTK_CONTAINER(window), scrolledwin);
  80. gtk_widget_show(scrolledwin);
  81. text = gtk_text_view_new();
  82. gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(text), GTK_WRAP_WORD_CHAR);
  83. gtk_text_view_set_editable(GTK_TEXT_VIEW(text), FALSE);
  84. if (!font_desc && prefs_common.textfont)
  85. font_desc = pango_font_description_from_string
  86. (prefs_common.textfont);
  87. if (font_desc)
  88. gtk_widget_modify_font(text, font_desc);
  89. gtk_container_add(GTK_CONTAINER(scrolledwin), text);
  90. gtk_widget_show(text);
  91. sourcewin->window = window;
  92. sourcewin->scrolledwin = scrolledwin;
  93. sourcewin->text = text;
  94. source_window_init();
  95. return sourcewin;
  96. }
  97. void source_window_show(SourceWindow *sourcewin)
  98. {
  99. gtk_widget_show_all(sourcewin->window);
  100. }
  101. static void source_window_destroy(SourceWindow *sourcewin)
  102. {
  103. if (sourcewin->updating) {
  104. debug_print("deferring destroy\n");
  105. sourcewin->deferred_destroy = TRUE;
  106. return;
  107. }
  108. gtk_widget_destroy(sourcewin->window);
  109. g_free(sourcewin);
  110. }
  111. void source_window_show_msg(SourceWindow *sourcewin, MsgInfo *msginfo)
  112. {
  113. gchar *file;
  114. gchar *title;
  115. FILE *fp;
  116. gchar buf[BUFFSIZE];
  117. cm_return_if_fail(msginfo != NULL);
  118. sourcewin->updating = TRUE;
  119. file = procmsg_get_message_file(msginfo);
  120. sourcewin->updating = FALSE;
  121. if (sourcewin->deferred_destroy) {
  122. g_free(file);
  123. source_window_destroy(sourcewin);
  124. return;
  125. }
  126. cm_return_if_fail(file != NULL);
  127. if ((fp = g_fopen(file, "rb")) == NULL) {
  128. FILE_OP_ERROR(file, "fopen");
  129. g_free(file);
  130. return;
  131. }
  132. debug_print("Displaying the source of %s ...\n", file);
  133. title = g_strdup_printf(_("%s - Source"), file);
  134. gtk_window_set_title(GTK_WINDOW(sourcewin->window), title);
  135. g_free(title);
  136. g_free(file);
  137. while (fgets(buf, sizeof(buf), fp) != NULL)
  138. source_window_append(sourcewin, buf);
  139. fclose(fp);
  140. }
  141. static void source_window_append(SourceWindow *sourcewin, const gchar *str)
  142. {
  143. GtkTextView *text = GTK_TEXT_VIEW(sourcewin->text);
  144. GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
  145. GtkTextIter iter;
  146. gchar *out;
  147. gint len;
  148. len = strlen(str) + 1;
  149. Xalloca(out, len, return);
  150. conv_utf8todisp(out, len, str);
  151. gtk_text_buffer_get_iter_at_offset(buffer, &iter, -1);
  152. gtk_text_buffer_insert(buffer, &iter, out, -1);
  153. }
  154. static void source_window_size_alloc_cb(GtkWidget *widget,
  155. GtkAllocation *allocation)
  156. {
  157. cm_return_if_fail(allocation != NULL);
  158. prefs_common.sourcewin_width = allocation->width;
  159. prefs_common.sourcewin_height = allocation->height;
  160. }
  161. static gint source_window_delete_cb(GtkWidget *widget, GdkEventAny *event,
  162. SourceWindow *sourcewin)
  163. {
  164. source_window_destroy(sourcewin);
  165. return TRUE;
  166. }
  167. static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
  168. SourceWindow *sourcewin)
  169. {
  170. if (!event || !sourcewin) return FALSE;
  171. switch (event->keyval) {
  172. case GDK_A:
  173. case GDK_a:
  174. if ((event->state & GDK_CONTROL_MASK) != 0)
  175. gtk_editable_select_region(GTK_EDITABLE(sourcewin->text), 0, -1);
  176. break;
  177. case GDK_W:
  178. case GDK_w:
  179. if ((event->state & GDK_CONTROL_MASK) != 0)
  180. gtk_widget_destroy(sourcewin->window);
  181. break;
  182. case GDK_Escape:
  183. source_window_destroy(sourcewin);
  184. return TRUE;
  185. break;
  186. }
  187. return FALSE;
  188. }