2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail 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"
51 #include "manage_window.h"
52 #include "alertpanel.h"
54 #include "prefs_common.h"
56 static struct MessageSearchWindow {
58 GtkWidget *body_entry;
59 GtkWidget *case_checkbtn;
66 MessageView *messageview;
69 gboolean search_compose;
70 gboolean is_searching;
73 static void message_search_create (void);
74 static void message_search_execute (gboolean backward);
76 static void message_search_prev_clicked (GtkButton *button,
78 static void message_search_next_clicked (GtkButton *button,
80 static void message_search_stop_clicked (GtkButton *button,
82 static void body_changed (void);
83 static gboolean key_pressed (GtkWidget *widget,
88 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
89 gboolean in_btn = FALSE; \
90 if (GTK_IS_BUTTON(widget)) \
91 in_btn = GTK_BUTTON(widget)->in_button; \
92 gtk_widget_set_sensitive(widget, sensitive); \
93 if (GTK_IS_BUTTON(widget)) \
94 GTK_BUTTON(widget)->in_button = in_btn; \
97 static void message_show_stop_button(void)
99 gtk_widget_hide(search_window.close_btn);
100 gtk_widget_show(search_window.stop_btn);
101 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
102 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
105 static void message_hide_stop_button(void)
107 gtk_widget_hide(search_window.stop_btn);
108 gtk_widget_show(search_window.close_btn);
109 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
110 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
113 void message_search(MessageView *messageview)
115 if (!search_window.window)
116 message_search_create();
118 gtk_widget_hide(search_window.window);
120 search_window.messageview = messageview;
121 search_window.search_compose = FALSE;
123 gtk_widget_grab_focus(search_window.next_btn);
124 gtk_widget_grab_focus(search_window.body_entry);
125 gtk_widget_show(search_window.window);
128 void message_search_compose(Compose *compose)
130 if (!search_window.window)
131 message_search_create();
133 gtk_widget_hide(search_window.window);
135 search_window.compose = compose;
136 search_window.search_compose = TRUE;
138 gtk_widget_grab_focus(search_window.next_btn);
139 gtk_widget_grab_focus(search_window.body_entry);
140 gtk_widget_show(search_window.window);
143 static void message_search_create(void)
149 GtkWidget *body_label;
150 GtkWidget *body_entry;
152 GtkWidget *checkbtn_hbox;
153 GtkWidget *case_checkbtn;
155 GtkWidget *confirm_area;
159 GtkWidget *close_btn;
162 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
163 gtk_window_set_title (GTK_WINDOW (window),
164 _("Find in current message"));
165 gtk_widget_set_size_request (window, 450, -1);
166 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
167 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
168 g_signal_connect(G_OBJECT(window), "delete_event",
169 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
170 g_signal_connect(G_OBJECT(window), "key_press_event",
171 G_CALLBACK(key_pressed), NULL);
172 MANAGE_WINDOW_SIGNALS_CONNECT(window);
174 vbox1 = gtk_vbox_new (FALSE, 0);
175 gtk_widget_show (vbox1);
176 gtk_container_add (GTK_CONTAINER (window), vbox1);
178 hbox1 = gtk_hbox_new (FALSE, 8);
179 gtk_widget_show (hbox1);
180 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
182 body_label = gtk_label_new (_("Find text:"));
183 gtk_widget_show (body_label);
184 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
186 body_entry = gtk_combo_box_entry_new_text ();
187 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
188 if (prefs_common.message_search_history)
189 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
190 prefs_common.message_search_history);
191 gtk_widget_show (body_entry);
192 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
193 g_signal_connect(G_OBJECT(body_entry), "changed",
194 G_CALLBACK(body_changed), NULL);
196 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
197 gtk_widget_show (checkbtn_hbox);
198 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
199 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
201 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
202 gtk_widget_show (case_checkbtn);
203 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
206 confirm_area = gtk_hbutton_box_new();
207 gtk_widget_show (confirm_area);
208 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
210 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
212 gtkut_stock_button_add_help(confirm_area, &help_btn);
214 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
215 GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
216 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
217 gtk_widget_show(prev_btn);
219 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
220 GTK_WIDGET_SET_FLAGS(next_btn, GTK_CAN_DEFAULT);
221 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
222 gtk_widget_show(next_btn);
224 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
225 GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
226 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
227 gtk_widget_show(close_btn);
229 /* stop button hidden */
230 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
231 GTK_WIDGET_SET_FLAGS(stop_btn, GTK_CAN_DEFAULT);
232 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
234 gtk_widget_show (confirm_area);
235 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
236 gtk_widget_grab_default(next_btn);
238 g_signal_connect(G_OBJECT(help_btn), "clicked",
239 G_CALLBACK(manual_open_with_anchor_cb),
240 MANUAL_ANCHOR_SEARCHING);
241 g_signal_connect(G_OBJECT(prev_btn), "clicked",
242 G_CALLBACK(message_search_prev_clicked), NULL);
243 g_signal_connect(G_OBJECT(next_btn), "clicked",
244 G_CALLBACK(message_search_next_clicked), NULL);
245 g_signal_connect_closure
246 (G_OBJECT(close_btn), "clicked",
247 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
250 g_signal_connect(G_OBJECT(stop_btn), "clicked",
251 G_CALLBACK(message_search_stop_clicked), NULL);
253 search_window.window = window;
254 search_window.body_entry = body_entry;
255 search_window.case_checkbtn = case_checkbtn;
256 search_window.help_btn = help_btn;
257 search_window.prev_btn = prev_btn;
258 search_window.next_btn = next_btn;
259 search_window.close_btn = close_btn;
260 search_window.stop_btn = stop_btn;
263 static void message_search_execute(gboolean backward)
265 MessageView *messageview = search_window.messageview;
266 Compose *compose = search_window.compose;
268 gboolean all_searched = FALSE;
269 const gchar *body_str;
271 body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
272 if (*body_str == '\0') return;
275 combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
276 prefs_common.message_search_history = add_history(
277 prefs_common.message_search_history, body_str);
278 combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
279 prefs_common.message_search_history);
281 case_sens = gtk_toggle_button_get_active
282 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
284 search_window.is_searching = TRUE;
285 message_show_stop_button();
287 for (; search_window.is_searching;) {
292 if (search_window.search_compose) {
293 if (compose_search_string_backward
294 (compose, body_str, case_sens) == TRUE)
297 if (messageview_search_string_backward
298 (messageview, body_str, case_sens) == TRUE)
302 if (search_window.search_compose) {
303 if (compose_search_string
304 (compose, body_str, case_sens) == TRUE)
307 if (messageview_search_string
308 (messageview, body_str, case_sens) == TRUE)
314 alertpanel_full(_("Search failed"),
315 _("Search string not found."),
316 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
317 NULL, ALERT_WARNING, G_ALERTDEFAULT);
324 str = _("Beginning of message reached; "
325 "continue from end?");
327 str = _("End of message reached; "
328 "continue from beginning?");
330 val = alertpanel(_("Search finished"), str,
331 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
332 if (G_ALERTALTERNATE == val) {
333 manage_window_focus_in(search_window.window,
335 if (search_window.search_compose) {
336 compose_set_position(compose,
339 messageview_set_position(messageview,
346 search_window.is_searching = FALSE;
347 message_hide_stop_button();
350 static void body_changed(void)
352 gtk_widget_grab_focus(search_window.body_entry);
355 static void message_search_prev_clicked(GtkButton *button, gpointer data)
357 message_search_execute(TRUE);
360 static void message_search_next_clicked(GtkButton *button, gpointer data)
362 message_search_execute(FALSE);
365 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
368 if (event && event->keyval == GDK_Escape)
369 gtk_widget_hide(search_window.window);
373 static void message_search_stop_clicked(GtkButton *button, gpointer data)
375 search_window.is_searching = FALSE;