2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2011 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 3 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, see <http://www.gnu.org/licenses/>.
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
35 #include "message_search.h"
36 #include "messageview.h"
41 #include "manage_window.h"
42 #include "alertpanel.h"
44 #include "prefs_common.h"
46 static struct MessageSearchWindow {
48 GtkWidget *body_entry;
49 GtkWidget *case_checkbtn;
56 MessageView *messageview;
59 gboolean search_compose;
60 gboolean is_searching;
61 gboolean body_entry_has_focus;
64 static void message_search_create (void);
65 static void message_search_execute (gboolean backward);
67 static void message_search_prev_clicked (GtkButton *button,
69 static void message_search_next_clicked (GtkButton *button,
71 static void message_search_stop_clicked (GtkButton *button,
73 static void body_changed (void);
74 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
76 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
78 static gboolean key_pressed (GtkWidget *widget,
83 #if !GTK_CHECK_VERSION(2,14,0)
84 /* Work around http://bugzilla.gnome.org/show_bug.cgi?id=56070 */
85 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
86 gboolean in_btn = FALSE; \
87 if (GTK_IS_BUTTON(widget)) \
88 in_btn = GTK_BUTTON(widget)->in_button; \
89 gtk_widget_set_sensitive(widget, sensitive); \
90 if (GTK_IS_BUTTON(widget)) \
91 GTK_BUTTON(widget)->in_button = in_btn; \
94 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
95 gtk_widget_set_sensitive(widget, sensitive); \
99 static void message_show_stop_button(void)
101 gtk_widget_hide(search_window.close_btn);
102 gtk_widget_show(search_window.stop_btn);
103 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
104 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
107 static void message_hide_stop_button(void)
109 gtk_widget_hide(search_window.stop_btn);
110 gtk_widget_show(search_window.close_btn);
111 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
112 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
115 void message_search(MessageView *messageview)
117 if (!search_window.window)
118 message_search_create();
120 gtk_widget_hide(search_window.window);
122 search_window.messageview = messageview;
123 search_window.search_compose = FALSE;
125 gtk_widget_grab_focus(search_window.next_btn);
126 gtk_widget_grab_focus(search_window.body_entry);
127 gtk_widget_show(search_window.window);
130 void message_search_compose(Compose *compose)
132 if (!search_window.window)
133 message_search_create();
135 gtk_widget_hide(search_window.window);
137 search_window.compose = compose;
138 search_window.search_compose = TRUE;
140 gtk_widget_grab_focus(search_window.next_btn);
141 gtk_widget_grab_focus(search_window.body_entry);
142 gtk_widget_show(search_window.window);
145 static void message_search_create(void)
151 GtkWidget *body_label;
152 GtkWidget *body_entry;
154 GtkWidget *checkbtn_hbox;
155 GtkWidget *case_checkbtn;
157 GtkWidget *confirm_area;
161 GtkWidget *close_btn;
164 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
165 gtk_window_set_title (GTK_WINDOW (window),
166 _("Find in current message"));
167 gtk_widget_set_size_request (window, 450, -1);
168 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
169 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
170 g_signal_connect(G_OBJECT(window), "delete_event",
171 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
172 g_signal_connect(G_OBJECT(window), "key_press_event",
173 G_CALLBACK(key_pressed), NULL);
174 MANAGE_WINDOW_SIGNALS_CONNECT(window);
176 vbox1 = gtk_vbox_new (FALSE, 0);
177 gtk_widget_show (vbox1);
178 gtk_container_add (GTK_CONTAINER (window), vbox1);
180 hbox1 = gtk_hbox_new (FALSE, 8);
181 gtk_widget_show (hbox1);
182 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
184 body_label = gtk_label_new (_("Find text:"));
185 gtk_widget_show (body_label);
186 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
188 body_entry = gtk_combo_box_entry_new_text ();
189 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
190 if (prefs_common.message_search_history)
191 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
192 prefs_common.message_search_history);
193 gtk_widget_show (body_entry);
194 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
195 g_signal_connect(G_OBJECT(body_entry), "changed",
196 G_CALLBACK(body_changed), NULL);
197 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
198 "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
199 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
200 "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
202 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
203 gtk_widget_show (checkbtn_hbox);
204 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
205 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
207 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
208 gtk_widget_show (case_checkbtn);
209 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
212 confirm_area = gtk_hbutton_box_new();
213 gtk_widget_show (confirm_area);
214 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
216 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
218 gtkut_stock_button_add_help(confirm_area, &help_btn);
220 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
221 gtkut_widget_set_can_default(prev_btn, TRUE);
222 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
223 gtk_widget_show(prev_btn);
225 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
226 gtkut_widget_set_can_default(next_btn, TRUE);
227 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
228 gtk_widget_show(next_btn);
230 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
231 gtkut_widget_set_can_default(close_btn, TRUE);
232 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
233 gtk_widget_show(close_btn);
235 /* stop button hidden */
236 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
237 gtkut_widget_set_can_default(stop_btn, TRUE);
238 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
240 gtk_widget_show (confirm_area);
241 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
242 gtk_widget_grab_default(next_btn);
244 g_signal_connect(G_OBJECT(help_btn), "clicked",
245 G_CALLBACK(manual_open_with_anchor_cb),
246 MANUAL_ANCHOR_SEARCHING);
247 g_signal_connect(G_OBJECT(prev_btn), "clicked",
248 G_CALLBACK(message_search_prev_clicked), NULL);
249 g_signal_connect(G_OBJECT(next_btn), "clicked",
250 G_CALLBACK(message_search_next_clicked), NULL);
251 g_signal_connect_closure
252 (G_OBJECT(close_btn), "clicked",
253 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
256 g_signal_connect(G_OBJECT(stop_btn), "clicked",
257 G_CALLBACK(message_search_stop_clicked), NULL);
259 search_window.window = window;
260 search_window.body_entry = body_entry;
261 search_window.case_checkbtn = case_checkbtn;
262 search_window.help_btn = help_btn;
263 search_window.prev_btn = prev_btn;
264 search_window.next_btn = next_btn;
265 search_window.close_btn = close_btn;
266 search_window.stop_btn = stop_btn;
269 static void message_search_execute(gboolean backward)
271 MessageView *messageview = search_window.messageview;
272 Compose *compose = search_window.compose;
274 gboolean all_searched = FALSE;
277 body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
279 body_str = gtk_editable_get_chars(
280 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
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();
360 static void body_changed(void)
362 if (!search_window.body_entry_has_focus)
363 gtk_widget_grab_focus(search_window.body_entry);
366 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
369 search_window.body_entry_has_focus = TRUE;
373 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
376 search_window.body_entry_has_focus = FALSE;
380 static void message_search_prev_clicked(GtkButton *button, gpointer data)
382 message_search_execute(TRUE);
385 static void message_search_next_clicked(GtkButton *button, gpointer data)
387 message_search_execute(FALSE);
390 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
393 if (event && (event->keyval == GDK_KEY_Escape)) {
394 gtk_widget_hide(search_window.window);
397 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
398 message_search_execute(FALSE);
401 if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
402 if (search_window.body_entry_has_focus) {
403 combobox_set_value_from_arrow_key(
404 GTK_COMBO_BOX(search_window.body_entry),
413 static void message_search_stop_clicked(GtkButton *button, gpointer data)
415 search_window.is_searching = FALSE;