2007-02-04 [colin] 2.7.2cvs20
[claws.git] / src / message_search.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 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 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 "compose.h"
48 #include "utils.h"
49 #include "gtkutils.h"
50 #include "combobox.h"
51 #include "manage_window.h"
52 #include "alertpanel.h"
53 #include "manual.h"
54 #include "prefs_common.h"
55
56 static struct MessageSearchWindow {
57         GtkWidget *window;
58         GtkWidget *body_entry;
59         GtkWidget *case_checkbtn;
60         GtkWidget *help_btn;
61         GtkWidget *prev_btn;
62         GtkWidget *next_btn;
63         GtkWidget *close_btn;
64         GtkWidget *stop_btn;
65
66         MessageView *messageview;
67
68         Compose *compose;
69         gboolean search_compose;
70         gboolean is_searching;
71         gboolean body_entry_has_focus;
72 } search_window;
73
74 static void message_search_create       (void);
75 static void message_search_execute      (gboolean        backward);
76
77 static void message_search_prev_clicked (GtkButton      *button,
78                                          gpointer        data);
79 static void message_search_next_clicked (GtkButton      *button,
80                                          gpointer        data);
81 static void message_search_stop_clicked (GtkButton      *button,
82                                          gpointer        data);
83 static void body_changed                (void);
84 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
85                                   gpointer data);
86 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
87                                   gpointer data);
88 static gboolean key_pressed             (GtkWidget      *widget,
89                                          GdkEventKey    *event,
90                                          gpointer        data);
91
92
93 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {    \
94         gboolean in_btn = FALSE;                                                        \
95         if (GTK_IS_BUTTON(widget))                                                      \
96                 in_btn = GTK_BUTTON(widget)->in_button;                 \
97         gtk_widget_set_sensitive(widget, sensitive);            \
98         if (GTK_IS_BUTTON(widget))                                                      \
99                 GTK_BUTTON(widget)->in_button = in_btn;                 \
100 }
101
102 static void message_show_stop_button(void)
103 {
104         gtk_widget_hide(search_window.close_btn);
105         gtk_widget_show(search_window.stop_btn);
106         GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
107         GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
108 }
109
110 static void message_hide_stop_button(void)
111 {
112         gtk_widget_hide(search_window.stop_btn);
113         gtk_widget_show(search_window.close_btn);
114         gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
115         gtk_widget_set_sensitive(search_window.next_btn, TRUE);
116 }
117
118 void message_search(MessageView *messageview)
119 {
120         if (!search_window.window)
121                 message_search_create();
122         else
123                 gtk_widget_hide(search_window.window);
124
125         search_window.messageview = messageview;
126         search_window.search_compose = FALSE;
127
128         gtk_widget_grab_focus(search_window.next_btn);
129         gtk_widget_grab_focus(search_window.body_entry);
130         gtk_widget_show(search_window.window);
131 }
132
133 void message_search_compose(Compose *compose)
134 {
135         if (!search_window.window)
136                 message_search_create();
137         else
138                 gtk_widget_hide(search_window.window);
139
140         search_window.compose = compose;
141         search_window.search_compose = TRUE;
142
143         gtk_widget_grab_focus(search_window.next_btn);
144         gtk_widget_grab_focus(search_window.body_entry);
145         gtk_widget_show(search_window.window);
146 }
147
148 static void message_search_create(void)
149 {
150         GtkWidget *window;
151
152         GtkWidget *vbox1;
153         GtkWidget *hbox1;
154         GtkWidget *body_label;
155         GtkWidget *body_entry;
156
157         GtkWidget *checkbtn_hbox;
158         GtkWidget *case_checkbtn;
159
160         GtkWidget *confirm_area;
161         GtkWidget *help_btn;
162         GtkWidget *prev_btn;
163         GtkWidget *next_btn;
164         GtkWidget *close_btn;
165         GtkWidget *stop_btn;
166
167         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
168         gtk_window_set_title (GTK_WINDOW (window),
169                               _("Find in current message"));
170         gtk_widget_set_size_request (window, 450, -1);
171         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
172         gtk_container_set_border_width (GTK_CONTAINER (window), 8);
173         g_signal_connect(G_OBJECT(window), "delete_event",
174                          G_CALLBACK(gtk_widget_hide_on_delete), NULL);
175         g_signal_connect(G_OBJECT(window), "key_press_event",
176                          G_CALLBACK(key_pressed), NULL);
177         MANAGE_WINDOW_SIGNALS_CONNECT(window);
178
179         vbox1 = gtk_vbox_new (FALSE, 0);
180         gtk_widget_show (vbox1);
181         gtk_container_add (GTK_CONTAINER (window), vbox1);
182
183         hbox1 = gtk_hbox_new (FALSE, 8);
184         gtk_widget_show (hbox1);
185         gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
186
187         body_label = gtk_label_new (_("Find text:"));
188         gtk_widget_show (body_label);
189         gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
190
191         body_entry = gtk_combo_box_entry_new_text ();
192         gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
193         if (prefs_common.message_search_history)
194                 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
195                                 prefs_common.message_search_history);
196         gtk_widget_show (body_entry);
197         gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
198         g_signal_connect(G_OBJECT(body_entry), "changed",
199                          G_CALLBACK(body_changed), NULL);
200         g_signal_connect(G_OBJECT(GTK_BIN(body_entry)->child),
201                          "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
202         g_signal_connect(G_OBJECT(GTK_BIN(body_entry)->child),
203                          "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
204
205         checkbtn_hbox = gtk_hbox_new (FALSE, 8);
206         gtk_widget_show (checkbtn_hbox);
207         gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
208         gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
209
210         case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
211         gtk_widget_show (case_checkbtn);
212         gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
213                             FALSE, FALSE, 0);
214
215         confirm_area = gtk_hbutton_box_new();
216         gtk_widget_show (confirm_area);
217         gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
218                                   GTK_BUTTONBOX_END);
219         gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
220
221         gtkut_stock_button_add_help(confirm_area, &help_btn);
222
223         prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
224         GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
225         gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
226         gtk_widget_show(prev_btn);
227
228         next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
229         GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
230         gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
231         gtk_widget_show(next_btn);
232
233         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
234         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
235         gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
236         gtk_widget_show(close_btn);
237
238         /* stop button hidden */
239         stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
240         GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
241         gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
242
243         gtk_widget_show (confirm_area);
244         gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
245         gtk_widget_grab_default(next_btn);
246
247         g_signal_connect(G_OBJECT(help_btn), "clicked",
248                          G_CALLBACK(manual_open_with_anchor_cb),
249                          MANUAL_ANCHOR_SEARCHING);
250         g_signal_connect(G_OBJECT(prev_btn), "clicked",
251                          G_CALLBACK(message_search_prev_clicked), NULL);
252         g_signal_connect(G_OBJECT(next_btn), "clicked",
253                          G_CALLBACK(message_search_next_clicked), NULL);
254         g_signal_connect_closure
255                 (G_OBJECT(close_btn), "clicked",
256                  g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
257                                      window, NULL),
258                  FALSE);
259         g_signal_connect(G_OBJECT(stop_btn), "clicked",
260                          G_CALLBACK(message_search_stop_clicked), NULL);
261
262         search_window.window = window;
263         search_window.body_entry = body_entry;
264         search_window.case_checkbtn = case_checkbtn;
265         search_window.help_btn = help_btn;
266         search_window.prev_btn = prev_btn;
267         search_window.next_btn = next_btn;
268         search_window.close_btn = close_btn;
269         search_window.stop_btn = stop_btn;
270 }
271
272 static void message_search_execute(gboolean backward)
273 {
274         MessageView *messageview = search_window.messageview;
275         Compose *compose = search_window.compose;
276         gboolean case_sens;
277         gboolean all_searched = FALSE;
278         const gchar *body_str;
279
280         body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
281         if (*body_str == '\0') return;
282
283         /* add to history */
284         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
285         prefs_common.message_search_history = add_history(
286                         prefs_common.message_search_history, body_str);
287         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
288                         prefs_common.message_search_history);
289
290         case_sens = gtk_toggle_button_get_active
291                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
292
293         search_window.is_searching = TRUE;
294         message_show_stop_button();
295
296         for (; search_window.is_searching;) {
297                 gchar *str;
298                 AlertValue val;
299
300                 if (backward) {
301                         if (search_window.search_compose) {
302                                 if (compose_search_string_backward
303                                         (compose, body_str, case_sens) == TRUE)
304                                         break;
305                         } else {
306                                 if (messageview_search_string_backward
307                                         (messageview, body_str, case_sens) == TRUE)
308                                         break;
309                         }
310                 } else {
311                         if (search_window.search_compose) {
312                                 if (compose_search_string
313                                         (compose, body_str, case_sens) == TRUE)
314                                         break;
315                         } else {
316                                 if (messageview_search_string
317                                         (messageview, body_str, case_sens) == TRUE)
318                                         break;
319                         }
320                 }
321
322                 if (all_searched) {
323                         alertpanel_full(_("Search failed"),
324                                         _("Search string not found."),
325                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
326                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
327                         break;
328                 }
329
330                 all_searched = TRUE;
331
332                 if (backward)
333                         str = _("Beginning of message reached; "
334                                 "continue from end?");
335                 else
336                         str = _("End of message reached; "
337                                 "continue from beginning?");
338
339                 val = alertpanel(_("Search finished"), str,
340                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
341                 if (G_ALERTALTERNATE == val) {
342                         manage_window_focus_in(search_window.window,
343                                                NULL, NULL);
344                         if (search_window.search_compose) {
345                                 compose_set_position(compose,
346                                                          backward ? -1 : 0);
347                         } else {
348                                 messageview_set_position(messageview,
349                                                          backward ? -1 : 0);
350                         }
351                 } else
352                         break;
353         }
354
355         search_window.is_searching = FALSE;
356         message_hide_stop_button();
357 }
358
359 static void body_changed(void)
360 {
361         if (!search_window.body_entry_has_focus)
362                 gtk_widget_grab_focus(search_window.body_entry);
363 }
364
365 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
366                                   gpointer data)
367 {
368         search_window.body_entry_has_focus = TRUE;
369         return FALSE;
370 }
371
372 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
373                                   gpointer data)
374 {
375         search_window.body_entry_has_focus = FALSE;
376         return FALSE;
377 }
378
379 static void message_search_prev_clicked(GtkButton *button, gpointer data)
380 {
381         message_search_execute(TRUE);
382 }
383
384 static void message_search_next_clicked(GtkButton *button, gpointer data)
385 {
386         message_search_execute(FALSE);
387 }
388
389 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
390                             gpointer data)
391 {
392         if (event && (event->keyval == GDK_Escape)) {
393                 gtk_widget_hide(search_window.window);
394         }
395
396         if (event && (event->keyval == GDK_Return)) {
397                 message_search_execute(FALSE);
398         }
399
400         if (event && (event->keyval == GDK_Down || event->keyval == GDK_Up)) {
401                 if (search_window.body_entry_has_focus) {
402                         combobox_set_value_from_arrow_key(
403                                         GTK_COMBO_BOX(search_window.body_entry),
404                                         event->keyval);
405                         return TRUE;
406                 }
407         }
408
409         return FALSE;
410 }
411
412 static void message_search_stop_clicked(GtkButton *button, gpointer data)
413 {
414         search_window.is_searching = FALSE;
415 }