2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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"
50 #include "manage_window.h"
51 #include "alertpanel.h"
54 static struct MessageSearchWindow {
56 GtkWidget *body_entry;
57 GtkWidget *case_checkbtn;
63 MessageView *messageview;
66 gboolean search_compose;
69 static void message_search_create (void);
70 static void message_search_execute (gboolean backward);
72 static void message_search_prev_clicked (GtkButton *button,
74 static void message_search_next_clicked (GtkButton *button,
76 static void body_activated (void);
77 static gboolean key_pressed (GtkWidget *widget,
81 void message_search(MessageView *messageview)
83 if (!search_window.window)
84 message_search_create();
86 gtk_widget_hide(search_window.window);
88 search_window.messageview = messageview;
89 search_window.search_compose = FALSE;
91 gtk_widget_grab_focus(search_window.next_btn);
92 gtk_widget_grab_focus(search_window.body_entry);
93 gtk_widget_show(search_window.window);
96 void message_search_compose(Compose *compose)
98 if (!search_window.window)
99 message_search_create();
101 gtk_widget_hide(search_window.window);
103 search_window.compose = compose;
104 search_window.search_compose = TRUE;
106 gtk_widget_grab_focus(search_window.next_btn);
107 gtk_widget_grab_focus(search_window.body_entry);
108 gtk_widget_show(search_window.window);
111 static void message_search_create(void)
117 GtkWidget *body_label;
118 GtkWidget *body_entry;
120 GtkWidget *checkbtn_hbox;
121 GtkWidget *case_checkbtn;
123 GtkWidget *confirm_area;
127 GtkWidget *close_btn;
129 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
130 gtk_window_set_title (GTK_WINDOW (window),
131 _("Find in current message"));
132 gtk_widget_set_size_request (window, 450, -1);
133 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
134 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
135 g_signal_connect(G_OBJECT(window), "delete_event",
136 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
137 g_signal_connect(G_OBJECT(window), "key_press_event",
138 G_CALLBACK(key_pressed), NULL);
139 MANAGE_WINDOW_SIGNALS_CONNECT(window);
141 vbox1 = gtk_vbox_new (FALSE, 0);
142 gtk_widget_show (vbox1);
143 gtk_container_add (GTK_CONTAINER (window), vbox1);
145 hbox1 = gtk_hbox_new (FALSE, 8);
146 gtk_widget_show (hbox1);
147 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
149 body_label = gtk_label_new (_("Find text:"));
150 gtk_widget_show (body_label);
151 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
153 body_entry = gtk_entry_new ();
154 gtk_widget_show (body_entry);
155 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
156 g_signal_connect(G_OBJECT(body_entry), "activate",
157 G_CALLBACK(body_activated), NULL);
159 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
160 gtk_widget_show (checkbtn_hbox);
161 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
162 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
164 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
165 gtk_widget_show (case_checkbtn);
166 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
169 gtkut_stock_button_set_create_with_help(&confirm_area, &help_btn,
170 &prev_btn, GTK_STOCK_GO_BACK,
171 &next_btn, GTK_STOCK_GO_FORWARD,
172 &close_btn, GTK_STOCK_CLOSE);
173 gtk_widget_show (confirm_area);
174 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
175 gtk_widget_grab_default(next_btn);
177 g_signal_connect(G_OBJECT(help_btn), "clicked",
178 G_CALLBACK(manual_open_with_anchor_cb),
179 MANUAL_ANCHOR_SEARCHING);
180 g_signal_connect(G_OBJECT(prev_btn), "clicked",
181 G_CALLBACK(message_search_prev_clicked), NULL);
182 g_signal_connect(G_OBJECT(next_btn), "clicked",
183 G_CALLBACK(message_search_next_clicked), NULL);
184 g_signal_connect_closure
185 (G_OBJECT(close_btn), "clicked",
186 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
190 search_window.window = window;
191 search_window.body_entry = body_entry;
192 search_window.case_checkbtn = case_checkbtn;
193 search_window.help_btn = help_btn;
194 search_window.prev_btn = prev_btn;
195 search_window.next_btn = next_btn;
196 search_window.close_btn = close_btn;
199 static void message_search_execute(gboolean backward)
201 MessageView *messageview = search_window.messageview;
202 Compose *compose = search_window.compose;
204 gboolean all_searched = FALSE;
205 const gchar *body_str;
207 body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
208 if (*body_str == '\0') return;
210 case_sens = gtk_toggle_button_get_active
211 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
218 if (search_window.search_compose) {
219 if (compose_search_string_backward
220 (compose, body_str, case_sens) == TRUE)
223 if (messageview_search_string_backward
224 (messageview, body_str, case_sens) == TRUE)
228 if (search_window.search_compose) {
229 if (compose_search_string
230 (compose, body_str, case_sens) == TRUE)
233 if (messageview_search_string
234 (messageview, body_str, case_sens) == TRUE)
240 alertpanel_full(_("Search failed"),
241 _("Search string not found."),
242 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
243 NULL, ALERT_WARNING, G_ALERTDEFAULT);
250 str = _("Beginning of message reached; "
251 "continue from end?");
253 str = _("End of message reached; "
254 "continue from beginning?");
256 val = alertpanel(_("Search finished"), str,
257 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
258 if (G_ALERTALTERNATE == val) {
259 manage_window_focus_in(search_window.window,
261 if (search_window.search_compose) {
262 compose_set_position(compose,
265 messageview_set_position(messageview,
273 static void message_search_prev_clicked(GtkButton *button, gpointer data)
275 message_search_execute(TRUE);
278 static void message_search_next_clicked(GtkButton *button, gpointer data)
280 message_search_execute(FALSE);
283 static void body_activated(void)
285 gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
288 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
291 if (event && event->keyval == GDK_Escape)
292 gtk_widget_hide(search_window.window);