6d1040117195a2a20d7210ba7e7e6c73364821f2
[claws.git] / src / message_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2002 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 <gdk/gdkkeysyms.h>
28 #include <gtk/gtkwidget.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtkvbox.h>
31 #include <gtk/gtktable.h>
32 #include <gtk/gtklabel.h>
33 #include <gtk/gtkentry.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtkcheckbutton.h>
36 #include <gtk/gtkhbbox.h>
37 #include <gtk/gtkbutton.h>
38 #include <gtk/gtkctree.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include "intl.h"
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, gpointer data);
65
66 void message_search(MessageView *messageview)
67 {
68         if (!window)
69                 message_search_create(messageview);
70         else
71                 gtk_widget_hide(window);
72
73         gtk_widget_grab_focus(search_btn);
74         gtk_widget_grab_focus(body_entry);
75         gtk_widget_show(window);
76 }
77
78 static void message_search_create(MessageView *messageview)
79 {
80         GtkWidget *vbox1;
81         GtkWidget *hbox1;
82         GtkWidget *body_label;
83         GtkWidget *checkbtn_hbox;
84         GtkWidget *confirm_area;
85
86         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
87         gtk_window_set_title (GTK_WINDOW (window),
88                               _("Find in current message"));
89         gtk_widget_set_size_request (window, 450, -1);
90         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
91         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
92         g_signal_connect(G_OBJECT(window), "delete_event",
93                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
94         g_signal_connect(G_OBJECT(window), "key_press_event",
95                          G_CALLBACK(key_pressed), NULL);
96         MANAGE_WINDOW_SIGNALS_CONNECT(window);
97
98         vbox1 = gtk_vbox_new (FALSE, 0);
99         gtk_widget_show (vbox1);
100         gtk_container_add (GTK_CONTAINER (window), vbox1);
101
102         hbox1 = gtk_hbox_new (FALSE, 8);
103         gtk_widget_show (hbox1);
104         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
105
106         body_label = gtk_label_new (_("Find text:"));
107         gtk_widget_show (body_label);
108         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
109
110         body_entry = gtk_entry_new ();
111         gtk_widget_show (body_entry);
112         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
113         g_signal_connect(G_OBJECT(body_entry), "activate",
114                          G_CALLBACK(body_activated), messageview);
115
116         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
117         gtk_widget_show (checkbtn_hbox);
118         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
119         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
120
121         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
122         gtk_widget_show (case_checkbtn);
123         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
124                             FALSE, FALSE, 0);
125         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(case_checkbtn), FALSE);
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_button_set_create_stock(&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),
143                          messageview);
144         g_signal_connect(G_OBJECT(clear_btn), "clicked",
145                          G_CALLBACK(message_search_clear),
146                          messageview);
147         g_signal_connect_closure
148                 (G_OBJECT(close_btn), "clicked",
149                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
150                                      window, NULL),
151                  FALSE);
152 }
153
154 static void message_search_execute(GtkButton *button, gpointer data)
155 {
156         MessageView *messageview = data;
157         gboolean case_sens;
158         gboolean backward;
159         gboolean all_searched = FALSE;
160         const gchar *body_str;
161
162         body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
163         if (*body_str == '\0') return;
164
165         case_sens = gtk_toggle_button_get_active
166                 (GTK_TOGGLE_BUTTON(case_checkbtn));
167         backward = gtk_toggle_button_get_active
168                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
169
170         for (;;) {
171                 gchar *str;
172                 AlertValue val;
173
174                 if (backward) {
175                         if (messageview_search_string_backward
176                                 (messageview, body_str, case_sens) == TRUE)
177                                 break;
178                 } else {
179                         if (messageview_search_string
180                                 (messageview, body_str, case_sens) == TRUE)
181                                 break;
182                 }
183
184                 if (all_searched) {
185                         alertpanel_notice
186                                 (_("Search string not found."));
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                                  _("Yes"), _("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, gpointer data)
221 {
222         if (event && event->keyval == GDK_Escape)
223                 gtk_widget_hide(window);
224         return FALSE;
225 }