2005-12-06 [colin] 1.9.100cvs70
[claws.git] / src / message_search.c
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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/gtkctree.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 struct MessageSearchWindow {
53         GtkWidget *window;
54         GtkWidget *body_entry;
55         GtkWidget *case_checkbtn;
56         GtkWidget *prev_btn;
57         GtkWidget *next_btn;
58         GtkWidget *close_btn;
59
60         MessageView *messageview;
61 } search_window;
62
63 static void message_search_create       (void);
64 static void message_search_execute      (gboolean        backward);
65
66 static void message_search_prev_clicked (GtkButton      *button,
67                                          gpointer        data);
68 static void message_search_next_clicked (GtkButton      *button,
69                                          gpointer        data);
70 static void body_activated              (void);
71 static gboolean key_pressed             (GtkWidget      *widget,
72                                          GdkEventKey    *event,
73                                          gpointer        data);
74
75 void message_search(MessageView *messageview)
76 {
77         if (!search_window.window)
78                 message_search_create();
79         else
80                 gtk_widget_hide(search_window.window);
81
82         search_window.messageview = messageview;
83
84         gtk_widget_grab_focus(search_window.next_btn);
85         gtk_widget_grab_focus(search_window.body_entry);
86         gtk_widget_show(search_window.window);
87 }
88
89 static void message_search_create(void)
90 {
91         GtkWidget *window;
92
93         GtkWidget *vbox1;
94         GtkWidget *hbox1;
95         GtkWidget *body_label;
96         GtkWidget *body_entry;
97
98         GtkWidget *checkbtn_hbox;
99         GtkWidget *case_checkbtn;
100
101         GtkWidget *confirm_area;
102         GtkWidget *prev_btn;
103         GtkWidget *next_btn;
104         GtkWidget *close_btn;
105
106         window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
107         gtk_window_set_title (GTK_WINDOW (window),
108                               _("Find in current message"));
109         gtk_widget_set_size_request (window, 450, -1);
110         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
111         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
112         g_signal_connect(G_OBJECT(window), "delete_event",
113                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
114         g_signal_connect(G_OBJECT(window), "key_press_event",
115                          G_CALLBACK(key_pressed), NULL);
116         MANAGE_WINDOW_SIGNALS_CONNECT(window);
117
118         vbox1 = gtk_vbox_new (FALSE, 0);
119         gtk_widget_show (vbox1);
120         gtk_container_add (GTK_CONTAINER (window), vbox1);
121
122         hbox1 = gtk_hbox_new (FALSE, 8);
123         gtk_widget_show (hbox1);
124         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
125
126         body_label = gtk_label_new (_("Find text:"));
127         gtk_widget_show (body_label);
128         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
129
130         body_entry = gtk_entry_new ();
131         gtk_widget_show (body_entry);
132         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
133         g_signal_connect(G_OBJECT(body_entry), "activate",
134                          G_CALLBACK(body_activated), NULL);
135
136         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
137         gtk_widget_show (checkbtn_hbox);
138         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
139         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
140
141         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
142         gtk_widget_show (case_checkbtn);
143         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
144                             FALSE, FALSE, 0);
145
146         gtkut_stock_button_set_create(&confirm_area,
147                                       &prev_btn, GTK_STOCK_GO_BACK,
148                                       &next_btn, GTK_STOCK_GO_FORWARD,
149                                       &close_btn, GTK_STOCK_CLOSE);
150         gtk_widget_show (confirm_area);
151         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
152         gtk_widget_grab_default(next_btn);
153
154         g_signal_connect(G_OBJECT(prev_btn), "clicked",
155                          G_CALLBACK(message_search_prev_clicked), NULL);
156         g_signal_connect(G_OBJECT(next_btn), "clicked",
157                          G_CALLBACK(message_search_next_clicked), NULL);
158         g_signal_connect_closure
159                 (G_OBJECT(close_btn), "clicked",
160                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
161                                      window, NULL),
162                  FALSE);
163
164         search_window.window = window;
165         search_window.body_entry = body_entry;
166         search_window.case_checkbtn = case_checkbtn;
167         search_window.prev_btn = prev_btn;
168         search_window.next_btn = next_btn;
169         search_window.close_btn = close_btn;
170 }
171
172 static void message_search_execute(gboolean backward)
173 {
174         MessageView *messageview = search_window.messageview;
175         gboolean case_sens;
176         gboolean all_searched = FALSE;
177         const gchar *body_str;
178
179         body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
180         if (*body_str == '\0') return;
181
182         case_sens = gtk_toggle_button_get_active
183                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
184
185         for (;;) {
186                 gchar *str;
187                 AlertValue val;
188
189                 if (backward) {
190                         if (messageview_search_string_backward
191                                 (messageview, body_str, case_sens) == TRUE)
192                                 break;
193                 } else {
194                         if (messageview_search_string
195                                 (messageview, body_str, case_sens) == TRUE)
196                                 break;
197                 }
198
199                 if (all_searched) {
200                         alertpanel_full(_("Search failed"),
201                                         _("Search string not found."),
202                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
203                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
204                         break;
205                 }
206
207                 all_searched = TRUE;
208
209                 if (backward)
210                         str = _("Beginning of message reached; "
211                                 "continue from end?");
212                 else
213                         str = _("End of message reached; "
214                                 "continue from beginning?");
215
216                 val = alertpanel(_("Search finished"), str,
217                                  GTK_STOCK_YES, GTK_STOCK_NO, NULL);
218                 if (G_ALERTDEFAULT == val) {
219                         manage_window_focus_in(search_window.window,
220                                                NULL, NULL);
221                         messageview_set_position(messageview,
222                                                  backward ? -1 : 0);
223                 } else
224                         break;
225         }
226 }
227
228 static void message_search_prev_clicked(GtkButton *button, gpointer data)
229 {
230         message_search_execute(TRUE);
231 }
232
233 static void message_search_next_clicked(GtkButton *button, gpointer data)
234 {
235         message_search_execute(FALSE);
236 }
237
238 static void body_activated(void)
239 {
240         gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
241 }
242
243 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
244                             gpointer data)
245 {
246         if (event && event->keyval == GDK_Escape)
247                 gtk_widget_hide(search_window.window);
248         return FALSE;
249 }