2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
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.
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.
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.
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>
45 #include "message_search.h"
46 #include "messageview.h"
50 #include "manage_window.h"
51 #include "alertpanel.h"
54 static struct MessageSearchWindow {
56 GtkWidget *body_entry;
57 GtkWidget *case_checkbtn;
64 MessageView *messageview;
67 gboolean search_compose;
68 gboolean is_searching;
71 static void message_search_create (void);
72 static void message_search_execute (gboolean backward);
74 static void message_search_prev_clicked (GtkButton *button,
76 static void message_search_next_clicked (GtkButton *button,
78 static void message_search_stop_clicked (GtkButton *button,
80 static void body_activated (void);
81 static gboolean key_pressed (GtkWidget *widget,
86 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
87 gboolean in_btn = FALSE; \
88 if (GTK_IS_BUTTON(widget)) \
89 in_btn = GTK_BUTTON(widget)->in_button; \
90 gtk_widget_set_sensitive(widget, sensitive); \
91 if (GTK_IS_BUTTON(widget)) \
92 GTK_BUTTON(widget)->in_button = in_btn; \
95 static void message_show_stop_button(void)
97 gtk_widget_hide(search_window.close_btn);
98 gtk_widget_show(search_window.stop_btn);
99 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
100 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
103 static void message_hide_stop_button(void)
105 gtk_widget_hide(search_window.stop_btn);
106 gtk_widget_show(search_window.close_btn);
107 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
108 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
111 void message_search(MessageView *messageview)
113 if (!search_window.window)
114 message_search_create();
116 gtk_widget_hide(search_window.window);
118 search_window.messageview = messageview;
119 search_window.search_compose = FALSE;
121 gtk_widget_grab_focus(search_window.next_btn);
122 gtk_widget_grab_focus(search_window.body_entry);
123 gtk_widget_show(search_window.window);
126 void message_search_compose(Compose *compose)
128 if (!search_window.window)
129 message_search_create();
131 gtk_widget_hide(search_window.window);
133 search_window.compose = compose;
134 search_window.search_compose = TRUE;
136 gtk_widget_grab_focus(search_window.next_btn);
137 gtk_widget_grab_focus(search_window.body_entry);
138 gtk_widget_show(search_window.window);
141 static void message_search_create(void)
147 GtkWidget *body_label;
148 GtkWidget *body_entry;
150 GtkWidget *checkbtn_hbox;
151 GtkWidget *case_checkbtn;
153 GtkWidget *confirm_area;
157 GtkWidget *close_btn;
160 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
161 gtk_window_set_title (GTK_WINDOW (window),
162 _("Find in current message"));
163 gtk_widget_set_size_request (window, 450, -1);
164 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
165 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
166 g_signal_connect(G_OBJECT(window), "delete_event",
167 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
168 g_signal_connect(G_OBJECT(window), "key_press_event",
169 G_CALLBACK(key_pressed), NULL);
170 MANAGE_WINDOW_SIGNALS_CONNECT(window);
172 vbox1 = gtk_vbox_new (FALSE, 0);
173 gtk_widget_show (vbox1);
174 gtk_container_add (GTK_CONTAINER (window), vbox1);
176 hbox1 = gtk_hbox_new (FALSE, 8);
177 gtk_widget_show (hbox1);
178 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
180 body_label = gtk_label_new (_("Find text:"));
181 gtk_widget_show (body_label);
182 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
184 body_entry = gtk_entry_new ();
185 gtk_widget_show (body_entry);
186 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
187 g_signal_connect(G_OBJECT(body_entry), "activate",
188 G_CALLBACK(body_activated), NULL);
190 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
191 gtk_widget_show (checkbtn_hbox);
192 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
193 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
195 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
196 gtk_widget_show (case_checkbtn);
197 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
200 confirm_area = gtk_hbutton_box_new();
201 gtk_widget_show (confirm_area);
202 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
204 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
206 gtkut_stock_button_add_help(confirm_area, &help_btn);
208 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
209 GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
210 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
211 gtk_widget_show(prev_btn);
213 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
214 GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
215 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
216 gtk_widget_show(next_btn);
218 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
219 GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
220 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
221 gtk_widget_show(close_btn);
223 /* stop button hidden */
224 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
225 GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
226 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
228 gtk_widget_show (confirm_area);
229 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
230 gtk_widget_grab_default(next_btn);
232 g_signal_connect(G_OBJECT(help_btn), "clicked",
233 G_CALLBACK(manual_open_with_anchor_cb),
234 MANUAL_ANCHOR_SEARCHING);
235 g_signal_connect(G_OBJECT(prev_btn), "clicked",
236 G_CALLBACK(message_search_prev_clicked), NULL);
237 g_signal_connect(G_OBJECT(next_btn), "clicked",
238 G_CALLBACK(message_search_next_clicked), NULL);
239 g_signal_connect_closure
240 (G_OBJECT(close_btn), "clicked",
241 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
244 g_signal_connect(G_OBJECT(stop_btn), "clicked",
245 G_CALLBACK(message_search_stop_clicked), NULL);
247 search_window.window = window;
248 search_window.body_entry = body_entry;
249 search_window.case_checkbtn = case_checkbtn;
250 search_window.help_btn = help_btn;
251 search_window.prev_btn = prev_btn;
252 search_window.next_btn = next_btn;
253 search_window.close_btn = close_btn;
254 search_window.stop_btn = stop_btn;
257 static void message_search_execute(gboolean backward)
259 MessageView *messageview = search_window.messageview;
260 Compose *compose = search_window.compose;
262 gboolean all_searched = FALSE;
263 const gchar *body_str;
265 body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
266 if (*body_str == '\0') return;
268 case_sens = gtk_toggle_button_get_active
269 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
271 search_window.is_searching = TRUE;
272 message_show_stop_button();
274 for (; search_window.is_searching;) {
279 if (search_window.search_compose) {
280 if (compose_search_string_backward
281 (compose, body_str, case_sens) == TRUE)
284 if (messageview_search_string_backward
285 (messageview, body_str, case_sens) == TRUE)
289 if (search_window.search_compose) {
290 if (compose_search_string
291 (compose, body_str, case_sens) == TRUE)
294 if (messageview_search_string
295 (messageview, body_str, case_sens) == TRUE)
301 alertpanel_full(_("Search failed"),
302 _("Search string not found."),
303 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
304 NULL, ALERT_WARNING, G_ALERTDEFAULT);
311 str = _("Beginning of message reached; "
312 "continue from end?");
314 str = _("End of message reached; "
315 "continue from beginning?");
317 val = alertpanel(_("Search finished"), str,
318 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
319 if (G_ALERTALTERNATE == val) {
320 manage_window_focus_in(search_window.window,
322 if (search_window.search_compose) {
323 compose_set_position(compose,
326 messageview_set_position(messageview,
333 search_window.is_searching = FALSE;
334 message_hide_stop_button();
337 static void message_search_prev_clicked(GtkButton *button, gpointer data)
339 message_search_execute(TRUE);
342 static void message_search_next_clicked(GtkButton *button, gpointer data)
344 message_search_execute(FALSE);
347 static void body_activated(void)
349 gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
352 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
355 if (event && event->keyval == GDK_Escape)
356 gtk_widget_hide(search_window.window);
360 static void message_search_stop_clicked(GtkButton *button, gpointer data)
362 search_window.is_searching = FALSE;