2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2013 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 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
85 gtk_widget_set_sensitive(widget, sensitive); \
88 static void message_show_stop_button(void)
90 gtk_widget_hide(search_window.close_btn);
91 gtk_widget_show(search_window.stop_btn);
92 GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
93 GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
96 static void message_hide_stop_button(void)
98 gtk_widget_hide(search_window.stop_btn);
99 gtk_widget_show(search_window.close_btn);
100 gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
101 gtk_widget_set_sensitive(search_window.next_btn, TRUE);
104 void message_search(MessageView *messageview)
106 if (!search_window.window)
107 message_search_create();
109 gtk_widget_hide(search_window.window);
111 search_window.messageview = messageview;
112 search_window.search_compose = FALSE;
114 gtk_widget_grab_focus(search_window.next_btn);
115 gtk_widget_grab_focus(search_window.body_entry);
116 gtk_widget_show(search_window.window);
119 void message_search_compose(Compose *compose)
121 if (!search_window.window)
122 message_search_create();
124 gtk_widget_hide(search_window.window);
126 search_window.compose = compose;
127 search_window.search_compose = TRUE;
129 gtk_widget_grab_focus(search_window.next_btn);
130 gtk_widget_grab_focus(search_window.body_entry);
131 gtk_widget_show(search_window.window);
134 static void message_search_create(void)
140 GtkWidget *body_label;
141 GtkWidget *body_entry;
143 GtkWidget *checkbtn_hbox;
144 GtkWidget *case_checkbtn;
146 GtkWidget *confirm_area;
150 GtkWidget *close_btn;
153 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
154 gtk_window_set_title (GTK_WINDOW (window),
155 _("Find in current message"));
156 gtk_widget_set_size_request (window, 450, -1);
157 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
158 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
159 g_signal_connect(G_OBJECT(window), "delete_event",
160 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
161 g_signal_connect(G_OBJECT(window), "key_press_event",
162 G_CALLBACK(key_pressed), NULL);
163 MANAGE_WINDOW_SIGNALS_CONNECT(window);
165 vbox1 = gtk_vbox_new (FALSE, 0);
166 gtk_widget_show (vbox1);
167 gtk_container_add (GTK_CONTAINER (window), vbox1);
169 hbox1 = gtk_hbox_new (FALSE, 8);
170 gtk_widget_show (hbox1);
171 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
173 body_label = gtk_label_new (_("Find text:"));
174 gtk_widget_show (body_label);
175 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
177 #if !GTK_CHECK_VERSION(2, 24, 0)
178 body_entry = gtk_combo_box_entry_new_text ();
180 body_entry = gtk_combo_box_text_new_with_entry ();
182 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
183 if (prefs_common.message_search_history)
184 #if !GTK_CHECK_VERSION(2, 24, 0)
185 combobox_set_popdown_strings(GTK_COMBO_BOX(body_entry),
186 prefs_common.message_search_history);
188 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
189 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);
195 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
196 "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
197 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
198 "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
200 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
201 gtk_widget_show (checkbtn_hbox);
202 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
203 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
205 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
206 gtk_widget_show (case_checkbtn);
207 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
210 confirm_area = gtk_hbutton_box_new();
211 gtk_widget_show (confirm_area);
212 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
214 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
216 gtkut_stock_button_add_help(confirm_area, &help_btn);
218 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
219 gtkut_widget_set_can_default(prev_btn, TRUE);
220 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
221 gtk_widget_show(prev_btn);
223 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
224 gtkut_widget_set_can_default(next_btn, TRUE);
225 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
226 gtk_widget_show(next_btn);
228 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
229 gtkut_widget_set_can_default(close_btn, TRUE);
230 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
231 gtk_widget_show(close_btn);
233 /* stop button hidden */
234 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
235 gtkut_widget_set_can_default(stop_btn, TRUE);
236 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
238 gtk_widget_show (confirm_area);
239 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
240 gtk_widget_grab_default(next_btn);
242 g_signal_connect(G_OBJECT(help_btn), "clicked",
243 G_CALLBACK(manual_open_with_anchor_cb),
244 MANUAL_ANCHOR_SEARCHING);
245 g_signal_connect(G_OBJECT(prev_btn), "clicked",
246 G_CALLBACK(message_search_prev_clicked), NULL);
247 g_signal_connect(G_OBJECT(next_btn), "clicked",
248 G_CALLBACK(message_search_next_clicked), NULL);
249 g_signal_connect_closure
250 (G_OBJECT(close_btn), "clicked",
251 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
254 g_signal_connect(G_OBJECT(stop_btn), "clicked",
255 G_CALLBACK(message_search_stop_clicked), NULL);
257 search_window.window = window;
258 search_window.body_entry = body_entry;
259 search_window.case_checkbtn = case_checkbtn;
260 search_window.help_btn = help_btn;
261 search_window.prev_btn = prev_btn;
262 search_window.next_btn = next_btn;
263 search_window.close_btn = close_btn;
264 search_window.stop_btn = stop_btn;
267 static void message_search_execute(gboolean backward)
269 MessageView *messageview = search_window.messageview;
270 Compose *compose = search_window.compose;
272 gboolean all_searched = FALSE;
275 #if !GTK_CHECK_VERSION(2, 24, 0)
276 body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
278 body_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.body_entry));
281 body_str = gtk_editable_get_chars(
282 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
283 if (!body_str || *body_str == '\0') return;
286 #if !GTK_CHECK_VERSION(2, 24, 0)
287 combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.body_entry));
289 combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry));
291 prefs_common.message_search_history = add_history(
292 prefs_common.message_search_history, body_str);
293 #if !GTK_CHECK_VERSION(2, 24, 0)
294 combobox_set_popdown_strings(GTK_COMBO_BOX(search_window.body_entry),
295 prefs_common.message_search_history);
297 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry),
298 prefs_common.message_search_history);
301 case_sens = gtk_toggle_button_get_active
302 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
304 search_window.is_searching = TRUE;
305 message_show_stop_button();
307 for (; search_window.is_searching;) {
312 if (search_window.search_compose) {
313 if (compose_search_string_backward
314 (compose, body_str, case_sens) == TRUE)
317 if (messageview_search_string_backward
318 (messageview, body_str, case_sens) == TRUE)
322 if (search_window.search_compose) {
323 if (compose_search_string
324 (compose, body_str, case_sens) == TRUE)
327 if (messageview_search_string
328 (messageview, body_str, case_sens) == TRUE)
334 alertpanel_full(_("Search failed"),
335 _("Search string not found."),
336 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
337 NULL, ALERT_WARNING, G_ALERTDEFAULT);
344 str = _("Beginning of message reached; "
345 "continue from end?");
347 str = _("End of message reached; "
348 "continue from beginning?");
350 val = alertpanel(_("Search finished"), str,
351 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
352 if (G_ALERTALTERNATE == val) {
353 manage_window_focus_in(search_window.window,
355 if (search_window.search_compose) {
356 compose_set_position(compose,
359 messageview_set_position(messageview,
366 search_window.is_searching = FALSE;
367 message_hide_stop_button();
371 static void body_changed(void)
373 if (!search_window.body_entry_has_focus)
374 gtk_widget_grab_focus(search_window.body_entry);
377 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
380 search_window.body_entry_has_focus = TRUE;
384 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
387 search_window.body_entry_has_focus = FALSE;
391 static void message_search_prev_clicked(GtkButton *button, gpointer data)
393 message_search_execute(TRUE);
396 static void message_search_next_clicked(GtkButton *button, gpointer data)
398 message_search_execute(FALSE);
401 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
404 if (event && (event->keyval == GDK_KEY_Escape)) {
405 gtk_widget_hide(search_window.window);
408 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
409 message_search_execute(FALSE);
412 if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
413 if (search_window.body_entry_has_focus) {
414 combobox_set_value_from_arrow_key(
415 GTK_COMBO_BOX(search_window.body_entry),
424 static void message_search_stop_clicked(GtkButton *button, gpointer data)
426 search_window.is_searching = FALSE;