2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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/>.
22 #include "claws-features.h"
28 #include <glib/gi18n.h>
29 #include <gdk/gdkkeysyms.h>
36 #include "message_search.h"
37 #include "messageview.h"
42 #include "manage_window.h"
43 #include "alertpanel.h"
45 #include "prefs_common.h"
47 static struct MessageSearchWindow {
49 GtkWidget *body_entry;
50 GtkWidget *case_checkbtn;
57 MessageView *messageview;
60 gboolean search_compose;
61 gboolean is_searching;
62 gboolean body_entry_has_focus;
65 static void message_search_create (void);
66 static void message_search_execute (gboolean backward);
68 static void message_search_prev_clicked (GtkButton *button,
70 static void message_search_next_clicked (GtkButton *button,
72 static void message_search_stop_clicked (GtkButton *button,
74 static void body_changed (void);
75 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
77 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
79 static gboolean key_pressed (GtkWidget *widget,
84 #if !GTK_CHECK_VERSION(2,14,0)
85 /* Work around http://bugzilla.gnome.org/show_bug.cgi?id=56070 */
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 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
96 gtk_widget_set_sensitive(widget, sensitive); \
100 static void message_show_stop_button(void)
102 gtk_widget_hide(search_window.close_btn);
103 gtk_widget_show(search_window.stop_btn);
104 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
105 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
108 static void message_hide_stop_button(void)
110 gtk_widget_hide(search_window.stop_btn);
111 gtk_widget_show(search_window.close_btn);
112 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
113 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
116 void message_search(MessageView *messageview)
118 if (!search_window.window)
119 message_search_create();
121 gtk_widget_hide(search_window.window);
123 search_window.messageview = messageview;
124 search_window.search_compose = FALSE;
126 gtk_widget_grab_focus(search_window.next_btn);
127 gtk_widget_grab_focus(search_window.body_entry);
128 gtk_widget_show(search_window.window);
131 void message_search_compose(Compose *compose)
133 if (!search_window.window)
134 message_search_create();
136 gtk_widget_hide(search_window.window);
138 search_window.compose = compose;
139 search_window.search_compose = TRUE;
141 gtk_widget_grab_focus(search_window.next_btn);
142 gtk_widget_grab_focus(search_window.body_entry);
143 gtk_widget_show(search_window.window);
146 static void message_search_create(void)
152 GtkWidget *body_label;
153 GtkWidget *body_entry;
155 GtkWidget *checkbtn_hbox;
156 GtkWidget *case_checkbtn;
158 GtkWidget *confirm_area;
162 GtkWidget *close_btn;
165 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
166 gtk_window_set_title (GTK_WINDOW (window),
167 _("Find in current message"));
168 gtk_widget_set_size_request (window, 450, -1);
169 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
170 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
171 g_signal_connect(G_OBJECT(window), "delete_event",
172 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
173 g_signal_connect(G_OBJECT(window), "key_press_event",
174 G_CALLBACK(key_pressed), NULL);
175 MANAGE_WINDOW_SIGNALS_CONNECT(window);
177 vbox1 = gtk_vbox_new (FALSE, 0);
178 gtk_widget_show (vbox1);
179 gtk_container_add (GTK_CONTAINER (window), vbox1);
181 hbox1 = gtk_hbox_new (FALSE, 8);
182 gtk_widget_show (hbox1);
183 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
185 body_label = gtk_label_new (_("Find text:"));
186 gtk_widget_show (body_label);
187 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
189 #if !GTK_CHECK_VERSION(2, 24, 0)
190 body_entry = gtk_combo_box_entry_new_text ();
192 body_entry = gtk_combo_box_text_new_with_entry ();
194 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
195 if (prefs_common.message_search_history)
196 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
197 prefs_common.message_search_history);
198 gtk_widget_show (body_entry);
199 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
200 g_signal_connect(G_OBJECT(body_entry), "changed",
201 G_CALLBACK(body_changed), NULL);
202 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
203 "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
204 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
205 "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
207 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
208 gtk_widget_show (checkbtn_hbox);
209 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
210 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
212 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
213 gtk_widget_show (case_checkbtn);
214 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
217 confirm_area = gtk_hbutton_box_new();
218 gtk_widget_show (confirm_area);
219 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
221 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
223 gtkut_stock_button_add_help(confirm_area, &help_btn);
225 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
226 gtkut_widget_set_can_default(prev_btn, TRUE);
227 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
228 gtk_widget_show(prev_btn);
230 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
231 gtkut_widget_set_can_default(next_btn, TRUE);
232 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
233 gtk_widget_show(next_btn);
235 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
236 gtkut_widget_set_can_default(close_btn, TRUE);
237 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
238 gtk_widget_show(close_btn);
240 /* stop button hidden */
241 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
242 gtkut_widget_set_can_default(stop_btn, TRUE);
243 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
245 gtk_widget_show (confirm_area);
246 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
247 gtk_widget_grab_default(next_btn);
249 g_signal_connect(G_OBJECT(help_btn), "clicked",
250 G_CALLBACK(manual_open_with_anchor_cb),
251 MANUAL_ANCHOR_SEARCHING);
252 g_signal_connect(G_OBJECT(prev_btn), "clicked",
253 G_CALLBACK(message_search_prev_clicked), NULL);
254 g_signal_connect(G_OBJECT(next_btn), "clicked",
255 G_CALLBACK(message_search_next_clicked), NULL);
256 g_signal_connect_closure
257 (G_OBJECT(close_btn), "clicked",
258 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
261 g_signal_connect(G_OBJECT(stop_btn), "clicked",
262 G_CALLBACK(message_search_stop_clicked), NULL);
264 search_window.window = window;
265 search_window.body_entry = body_entry;
266 search_window.case_checkbtn = case_checkbtn;
267 search_window.help_btn = help_btn;
268 search_window.prev_btn = prev_btn;
269 search_window.next_btn = next_btn;
270 search_window.close_btn = close_btn;
271 search_window.stop_btn = stop_btn;
274 static void message_search_execute(gboolean backward)
276 MessageView *messageview = search_window.messageview;
277 Compose *compose = search_window.compose;
279 gboolean all_searched = FALSE;
282 #if !GTK_CHECK_VERSION(2, 24, 0)
283 body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
285 body_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.body_entry));
288 body_str = gtk_editable_get_chars(
289 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
290 if (!body_str || *body_str == '\0') return;
293 combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
294 prefs_common.message_search_history = add_history(
295 prefs_common.message_search_history, body_str);
296 combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
297 prefs_common.message_search_history);
299 case_sens = gtk_toggle_button_get_active
300 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
302 search_window.is_searching = TRUE;
303 message_show_stop_button();
305 for (; search_window.is_searching;) {
310 if (search_window.search_compose) {
311 if (compose_search_string_backward
312 (compose, body_str, case_sens) == TRUE)
315 if (messageview_search_string_backward
316 (messageview, body_str, case_sens) == TRUE)
320 if (search_window.search_compose) {
321 if (compose_search_string
322 (compose, body_str, case_sens) == TRUE)
325 if (messageview_search_string
326 (messageview, body_str, case_sens) == TRUE)
332 alertpanel_full(_("Search failed"),
333 _("Search string not found."),
334 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
335 NULL, ALERT_WARNING, G_ALERTDEFAULT);
342 str = _("Beginning of message reached; "
343 "continue from end?");
345 str = _("End of message reached; "
346 "continue from beginning?");
348 val = alertpanel(_("Search finished"), str,
349 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
350 if (G_ALERTALTERNATE == val) {
351 manage_window_focus_in(search_window.window,
353 if (search_window.search_compose) {
354 compose_set_position(compose,
357 messageview_set_position(messageview,
364 search_window.is_searching = FALSE;
365 message_hide_stop_button();
369 static void body_changed(void)
371 if (!search_window.body_entry_has_focus)
372 gtk_widget_grab_focus(search_window.body_entry);
375 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
378 search_window.body_entry_has_focus = TRUE;
382 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
385 search_window.body_entry_has_focus = FALSE;
389 static void message_search_prev_clicked(GtkButton *button, gpointer data)
391 message_search_execute(TRUE);
394 static void message_search_next_clicked(GtkButton *button, gpointer data)
396 message_search_execute(FALSE);
399 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
402 if (event && (event->keyval == GDK_KEY_Escape)) {
403 gtk_widget_hide(search_window.window);
406 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
407 message_search_execute(FALSE);
410 if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
411 if (search_window.body_entry_has_focus) {
412 combobox_set_value_from_arrow_key(
413 GTK_COMBO_BOX(search_window.body_entry),
422 static void message_search_stop_clicked(GtkButton *button, gpointer data)
424 search_window.is_searching = FALSE;