2007-07-11 [colin] 2.10.0cvs16
[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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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         gchar *body_str;
279
280         body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
281         if (!body_str)
282                 body_str = gtk_editable_get_chars(
283                                 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
284         if (!body_str || *body_str == '\0') return;
285
286         /* add to history */
287         combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
288         prefs_common.message_search_history = add_history(
289                         prefs_common.message_search_history, body_str);
290         combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
291                         prefs_common.message_search_history);
292
293         case_sens = gtk_toggle_button_get_active
294                 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
295
296         search_window.is_searching = TRUE;
297         message_show_stop_button();
298
299         for (; search_window.is_searching;) {
300                 gchar *str;
301                 AlertValue val;
302
303                 if (backward) {
304                         if (search_window.search_compose) {
305                                 if (compose_search_string_backward
306                                         (compose, body_str, case_sens) == TRUE)
307                                         break;
308                         } else {
309                                 if (messageview_search_string_backward
310                                         (messageview, body_str, case_sens) == TRUE)
311                                         break;
312                         }
313                 } else {
314                         if (search_window.search_compose) {
315                                 if (compose_search_string
316                                         (compose, body_str, case_sens) == TRUE)
317                                         break;
318                         } else {
319                                 if (messageview_search_string
320                                         (messageview, body_str, case_sens) == TRUE)
321                                         break;
322                         }
323                 }
324
325                 if (all_searched) {
326                         alertpanel_full(_("Search failed"),
327                                         _("Search string not found."),
328                                          GTK_STOCK_CLOSE, NULL, NULL, FALSE,
329                                          NULL, ALERT_WARNING, G_ALERTDEFAULT);
330                         break;
331                 }
332
333                 all_searched = TRUE;
334
335                 if (backward)
336                         str = _("Beginning of message reached; "
337                                 "continue from end?");
338                 else
339                         str = _("End of message reached; "
340                                 "continue from beginning?");
341
342                 val = alertpanel(_("Search finished"), str,
343                                  GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
344                 if (G_ALERTALTERNATE == val) {
345                         manage_window_focus_in(search_window.window,
346                                                NULL, NULL);
347                         if (search_window.search_compose) {
348                                 compose_set_position(compose,
349                                                          backward ? -1 : 0);
350                         } else {
351                                 messageview_set_position(messageview,
352                                                          backward ? -1 : 0);
353                         }
354                 } else
355                         break;
356         }
357
358         search_window.is_searching = FALSE;
359         message_hide_stop_button();
360         g_free(body_str);
361 }
362
363 static void body_changed(void)
364 {
365         if (!search_window.body_entry_has_focus)
366                 gtk_widget_grab_focus(search_window.body_entry);
367 }
368
369 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
370                                   gpointer data)
371 {
372         search_window.body_entry_has_focus = TRUE;
373         return FALSE;
374 }
375
376 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
377                                   gpointer data)
378 {
379         search_window.body_entry_has_focus = FALSE;
380         return FALSE;
381 }
382
383 static void message_search_prev_clicked(GtkButton *button, gpointer data)
384 {
385         message_search_execute(TRUE);
386 }
387
388 static void message_search_next_clicked(GtkButton *button, gpointer data)
389 {
390         message_search_execute(FALSE);
391 }
392
393 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
394                             gpointer data)
395 {
396         if (event && (event->keyval == GDK_Escape)) {
397                 gtk_widget_hide(search_window.window);
398         }
399
400         if (event && (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)) {
401                 message_search_execute(FALSE);
402         }
403
404         if (event && (event->keyval == GDK_Down || event->keyval == GDK_Up)) {
405                 if (search_window.body_entry_has_focus) {
406                         combobox_set_value_from_arrow_key(
407                                         GTK_COMBO_BOX(search_window.body_entry),
408                                         event->keyval);
409                         return TRUE;
410                 }
411         }
412
413         return FALSE;
414 }
415
416 static void message_search_stop_clicked(GtkButton *button, gpointer data)
417 {
418         search_window.is_searching = FALSE;
419 }