* src/inc.c
[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 void 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_DIALOG);
87         gtk_window_set_title (GTK_WINDOW (window),
88                               _("Find in current message"));
89         gtk_widget_set_usize (window, 450, -1);
90         gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, TRUE);
91         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
92         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
93                            GTK_SIGNAL_FUNC(gtk_widget_hide_on_delete), NULL);
94         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
95                            GTK_SIGNAL_FUNC(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         gtk_signal_connect(GTK_OBJECT(body_entry), "activate",
114                            GTK_SIGNAL_FUNC(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
126         backward_checkbtn =
127                 gtk_check_button_new_with_label (_("Backward search"));
128         gtk_widget_show (backward_checkbtn);
129         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
130                             FALSE, FALSE, 0);
131
132         gtkut_button_set_create(&confirm_area,
133                                 &search_btn, _("Search"),
134                                 &clear_btn,  _("Clear"),
135                                 &close_btn,  _("Close"));
136         gtk_widget_show (confirm_area);
137         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
138         gtk_widget_grab_default(search_btn);
139
140         gtk_signal_connect(GTK_OBJECT(search_btn), "clicked",
141                            GTK_SIGNAL_FUNC(message_search_execute),
142                            messageview);
143         gtk_signal_connect(GTK_OBJECT(clear_btn), "clicked",
144                            GTK_SIGNAL_FUNC(message_search_clear),
145                            messageview);
146         gtk_signal_connect_object(GTK_OBJECT(close_btn), "clicked",
147                                   GTK_SIGNAL_FUNC(gtk_widget_hide),
148                                   GTK_OBJECT(window));
149 }
150
151 static void message_search_execute(GtkButton *button, gpointer data)
152 {
153         MessageView *messageview = data;
154         gboolean case_sens;
155         gboolean backward;
156         gboolean all_searched = FALSE;
157         gchar *body_str;
158
159         body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
160         if (*body_str == '\0') return;
161
162         case_sens = gtk_toggle_button_get_active
163                 (GTK_TOGGLE_BUTTON(case_checkbtn));
164         backward = gtk_toggle_button_get_active
165                 (GTK_TOGGLE_BUTTON(backward_checkbtn));
166
167         for (;;) {
168                 gchar *str;
169                 AlertValue val;
170
171                 if (backward) {
172                         if (messageview_search_string_backward
173                                 (messageview, body_str, case_sens) == TRUE)
174                                 break;
175                 } else {
176                         if (messageview_search_string
177                                 (messageview, body_str, case_sens) == TRUE)
178                                 break;
179                 }
180
181                 if (all_searched) {
182                         alertpanel_message
183                                 (_("Search failed"),
184                                  _("Search string not found."));
185                         break;
186                 }
187
188                 all_searched = TRUE;
189
190                 if (backward)
191                         str = _("Beginning of message reached; "
192                                 "continue from end?");
193                 else
194                         str = _("End of message reached; "
195                                 "continue from beginning?");
196
197                 val = alertpanel(_("Search finished"), str,
198                                  _("Yes"), _("No"), NULL);
199                 if (G_ALERTDEFAULT == val) {
200                         manage_window_focus_in(window, NULL, NULL);
201                         messageview_set_position(messageview,
202                                                  backward ? -1 : 0);
203                 } else
204                         break;
205         }
206 }
207
208 static void message_search_clear(GtkButton *button, gpointer data)
209 {
210         gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
211 }
212
213 static void body_activated(void)
214 {
215         gtk_button_clicked(GTK_BUTTON(search_btn));
216 }
217
218 static void key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
219 {
220         if (event && event->keyval == GDK_Escape)
221                 gtk_widget_hide(window);
222 }