root / src / message_search.c @ 237
History | View | Annotate | Download (6.4 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 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 2 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, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <gdk/gdkkeysyms.h> |
| 29 | #include <gtk/gtkwidget.h> |
| 30 | #include <gtk/gtkwindow.h> |
| 31 | #include <gtk/gtkvbox.h> |
| 32 | #include <gtk/gtktable.h> |
| 33 | #include <gtk/gtklabel.h> |
| 34 | #include <gtk/gtkentry.h> |
| 35 | #include <gtk/gtkhbox.h> |
| 36 | #include <gtk/gtkcheckbutton.h> |
| 37 | #include <gtk/gtkhbbox.h> |
| 38 | #include <gtk/gtkbutton.h> |
| 39 | #include <gtk/gtkstock.h> |
| 40 | #include <stdio.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <string.h> |
| 43 | |
| 44 | #include "main.h" |
| 45 | #include "message_search.h" |
| 46 | #include "messageview.h" |
| 47 | #include "utils.h" |
| 48 | #include "gtkutils.h" |
| 49 | #include "manage_window.h" |
| 50 | #include "alertpanel.h" |
| 51 | |
| 52 | static GtkWidget *window;
|
| 53 | static GtkWidget *body_entry;
|
| 54 | static GtkWidget *case_checkbtn;
|
| 55 | static GtkWidget *backward_checkbtn;
|
| 56 | static GtkWidget *search_btn;
|
| 57 | static GtkWidget *clear_btn;
|
| 58 | static GtkWidget *close_btn;
|
| 59 | |
| 60 | static void message_search_create(MessageView *summaryview); |
| 61 | static void message_search_execute(GtkButton *button, gpointer data); |
| 62 | static void message_search_clear(GtkButton *button, gpointer data); |
| 63 | static void body_activated(void); |
| 64 | static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
|
| 65 | gpointer data); |
| 66 | |
| 67 | void message_search(MessageView *messageview)
|
| 68 | {
|
| 69 | if (!window)
|
| 70 | message_search_create(messageview); |
| 71 | else
|
| 72 | gtk_widget_hide(window); |
| 73 | |
| 74 | gtk_widget_grab_focus(search_btn); |
| 75 | gtk_widget_grab_focus(body_entry); |
| 76 | gtk_widget_show(window); |
| 77 | } |
| 78 | |
| 79 | static void message_search_create(MessageView *messageview) |
| 80 | {
|
| 81 | GtkWidget *vbox1; |
| 82 | GtkWidget *hbox1; |
| 83 | GtkWidget *body_label; |
| 84 | GtkWidget *checkbtn_hbox; |
| 85 | GtkWidget *confirm_area; |
| 86 | |
| 87 | window = gtk_window_new (GTK_WINDOW_TOPLEVEL); |
| 88 | gtk_window_set_title (GTK_WINDOW (window), |
| 89 | _("Find in current message"));
|
| 90 | gtk_widget_set_size_request (window, 450, -1); |
| 91 | gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE); |
| 92 | gtk_container_set_border_width (GTK_CONTAINER (window), 8);
|
| 93 | g_signal_connect(G_OBJECT(window), "delete_event",
|
| 94 | G_CALLBACK(gtk_widget_hide_on_delete), NULL);
|
| 95 | g_signal_connect(G_OBJECT(window), "key_press_event",
|
| 96 | G_CALLBACK(key_pressed), NULL);
|
| 97 | MANAGE_WINDOW_SIGNALS_CONNECT(window); |
| 98 | |
| 99 | vbox1 = gtk_vbox_new (FALSE, 0);
|
| 100 | gtk_widget_show (vbox1); |
| 101 | gtk_container_add (GTK_CONTAINER (window), vbox1); |
| 102 | |
| 103 | hbox1 = gtk_hbox_new (FALSE, 8);
|
| 104 | gtk_widget_show (hbox1); |
| 105 | gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
|
| 106 | |
| 107 | body_label = gtk_label_new (_("Find text:"));
|
| 108 | gtk_widget_show (body_label); |
| 109 | gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
|
| 110 | |
| 111 | body_entry = gtk_entry_new (); |
| 112 | gtk_widget_show (body_entry); |
| 113 | gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
|
| 114 | g_signal_connect(G_OBJECT(body_entry), "activate",
|
| 115 | G_CALLBACK(body_activated), messageview); |
| 116 | |
| 117 | checkbtn_hbox = gtk_hbox_new (FALSE, 8);
|
| 118 | gtk_widget_show (checkbtn_hbox); |
| 119 | gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
|
| 120 | gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
|
| 121 | |
| 122 | case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
|
| 123 | gtk_widget_show (case_checkbtn); |
| 124 | gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn, |
| 125 | FALSE, FALSE, 0);
|
| 126 | |
| 127 | backward_checkbtn = |
| 128 | gtk_check_button_new_with_label (_("Backward search"));
|
| 129 | gtk_widget_show (backward_checkbtn); |
| 130 | gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn, |
| 131 | FALSE, FALSE, 0);
|
| 132 | |
| 133 | gtkut_stock_button_set_create(&confirm_area, |
| 134 | &search_btn, GTK_STOCK_FIND, |
| 135 | &clear_btn, GTK_STOCK_CLEAR, |
| 136 | &close_btn, GTK_STOCK_CLOSE); |
| 137 | gtk_widget_show (confirm_area); |
| 138 | gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
|
| 139 | gtk_widget_grab_default(search_btn); |
| 140 | |
| 141 | g_signal_connect(G_OBJECT(search_btn), "clicked",
|
| 142 | G_CALLBACK(message_search_execute), messageview); |
| 143 | g_signal_connect(G_OBJECT(clear_btn), "clicked",
|
| 144 | G_CALLBACK(message_search_clear), messageview); |
| 145 | g_signal_connect_closure |
| 146 | (G_OBJECT(close_btn), "clicked",
|
| 147 | g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide), |
| 148 | window, NULL),
|
| 149 | FALSE); |
| 150 | } |
| 151 | |
| 152 | static void message_search_execute(GtkButton *button, gpointer data) |
| 153 | {
|
| 154 | MessageView *messageview = data; |
| 155 | gboolean case_sens; |
| 156 | gboolean backward; |
| 157 | gboolean all_searched = FALSE; |
| 158 | const gchar *body_str;
|
| 159 | |
| 160 | body_str = gtk_entry_get_text(GTK_ENTRY(body_entry)); |
| 161 | if (*body_str == '\0') return; |
| 162 | |
| 163 | case_sens = gtk_toggle_button_get_active |
| 164 | (GTK_TOGGLE_BUTTON(case_checkbtn)); |
| 165 | backward = gtk_toggle_button_get_active |
| 166 | (GTK_TOGGLE_BUTTON(backward_checkbtn)); |
| 167 | |
| 168 | for (;;) {
|
| 169 | gchar *str; |
| 170 | AlertValue val; |
| 171 | |
| 172 | if (backward) {
|
| 173 | if (messageview_search_string_backward
|
| 174 | (messageview, body_str, case_sens) == TRUE) |
| 175 | break;
|
| 176 | } else {
|
| 177 | if (messageview_search_string
|
| 178 | (messageview, body_str, case_sens) == TRUE) |
| 179 | break;
|
| 180 | } |
| 181 | |
| 182 | if (all_searched) {
|
| 183 | alertpanel_message |
| 184 | (_("Search failed"),
|
| 185 | _("Search string not found."),
|
| 186 | ALERT_WARNING); |
| 187 | break;
|
| 188 | } |
| 189 | |
| 190 | all_searched = TRUE; |
| 191 | |
| 192 | if (backward)
|
| 193 | str = _("Beginning of message reached; "
|
| 194 | "continue from end?");
|
| 195 | else
|
| 196 | str = _("End of message reached; "
|
| 197 | "continue from beginning?");
|
| 198 | |
| 199 | val = alertpanel(_("Search finished"), str,
|
| 200 | GTK_STOCK_YES, GTK_STOCK_NO, NULL);
|
| 201 | if (G_ALERTDEFAULT == val) {
|
| 202 | manage_window_focus_in(window, NULL, NULL); |
| 203 | messageview_set_position(messageview, |
| 204 | backward ? -1 : 0); |
| 205 | } else
|
| 206 | break;
|
| 207 | } |
| 208 | } |
| 209 | |
| 210 | static void message_search_clear(GtkButton *button, gpointer data) |
| 211 | {
|
| 212 | gtk_editable_delete_text(GTK_EDITABLE(body_entry), 0, -1); |
| 213 | } |
| 214 | |
| 215 | static void body_activated(void) |
| 216 | {
|
| 217 | gtk_button_clicked(GTK_BUTTON(search_btn)); |
| 218 | } |
| 219 | |
| 220 | static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
|
| 221 | gpointer data) |
| 222 | {
|
| 223 | if (event && event->keyval == GDK_Escape)
|
| 224 | gtk_widget_hide(window); |
| 225 | return FALSE;
|
| 226 | } |