2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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;
71 gboolean body_entry_has_focus;
74 static void message_search_create (void);
75 static void message_search_execute (gboolean backward);
77 static void message_search_prev_clicked (GtkButton *button,
79 static void message_search_next_clicked (GtkButton *button,
81 static void message_search_stop_clicked (GtkButton *button,
83 static void body_changed (void);
84 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
86 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
88 static gboolean key_pressed (GtkWidget *widget,
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; \
102 static void message_show_stop_button(void)
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)
110 static void message_hide_stop_button(void)
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);
118 void message_search(MessageView *messageview)
120 if (!search_window.window)
121 message_search_create();
123 gtk_widget_hide(search_window.window);
125 search_window.messageview = messageview;
126 search_window.search_compose = FALSE;
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);
133 void message_search_compose(Compose *compose)
135 if (!search_window.window)
136 message_search_create();
138 gtk_widget_hide(search_window.window);
140 search_window.compose = compose;
141 search_window.search_compose = TRUE;
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);
148 static void message_search_create(void)
154 GtkWidget *body_label;
155 GtkWidget *body_entry;
157 GtkWidget *checkbtn_hbox;
158 GtkWidget *case_checkbtn;
160 GtkWidget *confirm_area;
164 GtkWidget *close_btn;
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);
179 vbox1 = gtk_vbox_new (FALSE, 0);
180 gtk_widget_show (vbox1);
181 gtk_container_add (GTK_CONTAINER (window), vbox1);
183 hbox1 = gtk_hbox_new (FALSE, 8);
184 gtk_widget_show (hbox1);
185 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
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);
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);
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);
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,
215 confirm_area = gtk_hbutton_box_new();
216 gtk_widget_show (confirm_area);
217 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
219 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
221 gtkut_stock_button_add_help(confirm_area, &help_btn);
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);
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);
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);
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);
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);
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),
259 g_signal_connect(G_OBJECT(stop_btn), "clicked",
260 G_CALLBACK(message_search_stop_clicked), NULL);
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;
272 static void message_search_execute(gboolean backward)
274 MessageView *messageview = search_window.messageview;
275 Compose *compose = search_window.compose;
277 gboolean all_searched = FALSE;
278 const gchar *body_str;
280 body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
281 if (!body_str || *body_str == '\0') return;
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);
290 case_sens = gtk_toggle_button_get_active
291 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
293 search_window.is_searching = TRUE;
294 message_show_stop_button();
296 for (; search_window.is_searching;) {
301 if (search_window.search_compose) {
302 if (compose_search_string_backward
303 (compose, body_str, case_sens) == TRUE)
306 if (messageview_search_string_backward
307 (messageview, body_str, case_sens) == TRUE)
311 if (search_window.search_compose) {
312 if (compose_search_string
313 (compose, body_str, case_sens) == TRUE)
316 if (messageview_search_string
317 (messageview, body_str, case_sens) == TRUE)
323 alertpanel_full(_("Search failed"),
324 _("Search string not found."),
325 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
326 NULL, ALERT_WARNING, G_ALERTDEFAULT);
333 str = _("Beginning of message reached; "
334 "continue from end?");
336 str = _("End of message reached; "
337 "continue from beginning?");
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,
344 if (search_window.search_compose) {
345 compose_set_position(compose,
348 messageview_set_position(messageview,
355 search_window.is_searching = FALSE;
356 message_hide_stop_button();
359 static void body_changed(void)
361 if (!search_window.body_entry_has_focus)
362 gtk_widget_grab_focus(search_window.body_entry);
365 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
368 search_window.body_entry_has_focus = TRUE;
372 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
375 search_window.body_entry_has_focus = FALSE;
379 static void message_search_prev_clicked(GtkButton *button, gpointer data)
381 message_search_execute(TRUE);
384 static void message_search_next_clicked(GtkButton *button, gpointer data)
386 message_search_execute(FALSE);
389 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
392 if (event && (event->keyval == GDK_Escape)) {
393 gtk_widget_hide(search_window.window);
396 if (event && (event->keyval == GDK_Return)) {
397 message_search_execute(FALSE);
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),
412 static void message_search_stop_clicked(GtkButton *button, gpointer data)
414 search_window.is_searching = FALSE;