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 SearchInterface *interface;
60 gboolean is_searching;
61 gboolean body_entry_has_focus;
64 static SearchInterface compose_interface = {
65 .search_string_backward = (SearchStringFunc) compose_search_string_backward,
66 .set_position = (SetPositionFunc) compose_set_position,
67 .search_string = (SearchStringFunc) compose_search_string,
70 static SearchInterface messageview_interface = {
71 .set_position = (SetPositionFunc) messageview_set_position,
72 .search_string = (SearchStringFunc) messageview_search_string,
73 .search_string_backward = (SearchStringFunc) messageview_search_string_backward,
76 static void message_search_create (void);
77 static void message_search_execute (gboolean backward);
79 static void message_search_prev_clicked (GtkButton *button,
81 static void message_search_next_clicked (GtkButton *button,
83 static void message_search_stop_clicked (GtkButton *button,
85 static void body_changed (void);
86 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
88 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
90 static gboolean key_pressed (GtkWidget *widget,
95 #define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) { \
96 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 message_search_other(&messageview_interface, (void *)messageview);
120 void message_search_compose(Compose *compose)
122 message_search_other(&compose_interface, (void *)compose);
125 void message_search_close (void *obj)
127 if(!search_window.window) {
130 if (search_window.interface_obj == obj) {
131 gtk_widget_hide(search_window.window);
132 search_window.interface_obj = NULL;
136 void message_search_other(SearchInterface *interface, void *obj)
138 if (!search_window.window)
139 message_search_create();
141 gtk_widget_hide(search_window.window);
143 search_window.interface_obj = obj;
144 search_window.interface = interface;
146 gtk_widget_grab_focus(search_window.next_btn);
147 gtk_widget_grab_focus(search_window.body_entry);
148 gtk_widget_show(search_window.window);
152 static void message_search_create(void)
158 GtkWidget *body_label;
159 GtkWidget *body_entry;
161 GtkWidget *checkbtn_hbox;
162 GtkWidget *case_checkbtn;
164 GtkWidget *confirm_area;
168 GtkWidget *close_btn;
171 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
172 gtk_window_set_title (GTK_WINDOW (window),
173 _("Find in current message"));
174 gtk_widget_set_size_request (window, 450, -1);
175 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
176 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
177 g_signal_connect(G_OBJECT(window), "delete_event",
178 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
179 g_signal_connect(G_OBJECT(window), "key_press_event",
180 G_CALLBACK(key_pressed), NULL);
181 MANAGE_WINDOW_SIGNALS_CONNECT(window);
183 vbox1 = gtk_vbox_new (FALSE, 0);
184 gtk_widget_show (vbox1);
185 gtk_container_add (GTK_CONTAINER (window), vbox1);
187 hbox1 = gtk_hbox_new (FALSE, 8);
188 gtk_widget_show (hbox1);
189 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
191 body_label = gtk_label_new (_("Find text:"));
192 gtk_widget_show (body_label);
193 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
195 body_entry = gtk_combo_box_text_new_with_entry ();
196 gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
197 if (prefs_common.message_search_history)
198 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
199 prefs_common.message_search_history);
200 gtk_widget_show (body_entry);
201 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
202 g_signal_connect(G_OBJECT(body_entry), "changed",
203 G_CALLBACK(body_changed), NULL);
204 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
205 "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
206 g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
207 "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
209 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
210 gtk_widget_show (checkbtn_hbox);
211 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
212 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
214 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
215 gtk_widget_show (case_checkbtn);
216 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
219 confirm_area = gtk_hbutton_box_new();
220 gtk_widget_show (confirm_area);
221 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
223 gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
225 gtkut_stock_button_add_help(confirm_area, &help_btn);
227 prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
228 gtk_widget_set_can_default(prev_btn, TRUE);
229 gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
230 gtk_widget_show(prev_btn);
232 next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
233 gtk_widget_set_can_default(next_btn, TRUE);
234 gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
235 gtk_widget_show(next_btn);
237 close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
238 gtk_widget_set_can_default(close_btn, TRUE);
239 gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
240 gtk_widget_show(close_btn);
242 /* stop button hidden */
243 stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
244 gtk_widget_set_can_default(stop_btn, TRUE);
245 gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
247 gtk_widget_show (confirm_area);
248 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
249 gtk_widget_grab_default(next_btn);
251 g_signal_connect(G_OBJECT(help_btn), "clicked",
252 G_CALLBACK(manual_open_with_anchor_cb),
253 MANUAL_ANCHOR_SEARCHING);
254 g_signal_connect(G_OBJECT(prev_btn), "clicked",
255 G_CALLBACK(message_search_prev_clicked), NULL);
256 g_signal_connect(G_OBJECT(next_btn), "clicked",
257 G_CALLBACK(message_search_next_clicked), NULL);
258 g_signal_connect_closure
259 (G_OBJECT(close_btn), "clicked",
260 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
263 g_signal_connect(G_OBJECT(stop_btn), "clicked",
264 G_CALLBACK(message_search_stop_clicked), NULL);
266 search_window.window = window;
267 search_window.body_entry = body_entry;
268 search_window.case_checkbtn = case_checkbtn;
269 search_window.help_btn = help_btn;
270 search_window.prev_btn = prev_btn;
271 search_window.next_btn = next_btn;
272 search_window.close_btn = close_btn;
273 search_window.stop_btn = stop_btn;
276 static void message_search_execute(gboolean backward)
278 void *interface_obj = search_window.interface_obj;
280 gboolean all_searched = FALSE;
283 body_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.body_entry));
285 body_str = gtk_editable_get_chars(
286 GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
287 if (!body_str || *body_str == '\0') return;
290 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 combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry),
294 prefs_common.message_search_history);
296 case_sens = gtk_toggle_button_get_active
297 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
299 search_window.is_searching = TRUE;
300 message_show_stop_button();
302 for (; search_window.is_searching;) {
307 if (search_window.interface->search_string_backward
308 (interface_obj, body_str, case_sens) == TRUE)
311 if (search_window.interface->search_string
312 (interface_obj, body_str, case_sens) == TRUE)
317 alertpanel_full(_("Search failed"),
318 _("Search string not found."),
319 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
320 ALERTFOCUS_FIRST, NULL, ALERT_WARNING);
327 str = _("Beginning of message reached; "
328 "continue from end?");
330 str = _("End of message reached; "
331 "continue from beginning?");
333 val = alertpanel(_("Search finished"), str,
334 GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
335 if (G_ALERTALTERNATE == val) {
336 manage_window_focus_in(search_window.window,
338 search_window.interface->set_position(interface_obj,
344 search_window.is_searching = FALSE;
345 message_hide_stop_button();
349 static void body_changed(void)
351 if (!search_window.body_entry_has_focus)
352 gtk_widget_grab_focus(search_window.body_entry);
355 static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
358 search_window.body_entry_has_focus = TRUE;
362 static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
365 search_window.body_entry_has_focus = FALSE;
369 static void message_search_prev_clicked(GtkButton *button, gpointer data)
371 message_search_execute(TRUE);
374 static void message_search_next_clicked(GtkButton *button, gpointer data)
376 message_search_execute(FALSE);
379 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
382 if (event && (event->keyval == GDK_KEY_Escape)) {
383 gtk_widget_hide(search_window.window);
386 if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
387 message_search_execute(FALSE);
390 if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
391 if (search_window.body_entry_has_focus) {
392 combobox_set_value_from_arrow_key(
393 GTK_COMBO_BOX(search_window.body_entry),
402 static void message_search_stop_clicked(GtkButton *button, gpointer data)
404 search_window.is_searching = FALSE;