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"
53 static struct MessageSearchWindow {
55 GtkWidget *body_entry;
56 GtkWidget *case_checkbtn;
61 MessageView *messageview;
64 gboolean search_compose;
67 static void message_search_create (void);
68 static void message_search_execute (gboolean backward);
70 static void message_search_prev_clicked (GtkButton *button,
72 static void message_search_next_clicked (GtkButton *button,
74 static void body_activated (void);
75 static gboolean key_pressed (GtkWidget *widget,
79 void message_search(MessageView *messageview)
81 if (!search_window.window)
82 message_search_create();
84 gtk_widget_hide(search_window.window);
86 search_window.messageview = messageview;
87 search_window.search_compose = FALSE;
89 gtk_widget_grab_focus(search_window.next_btn);
90 gtk_widget_grab_focus(search_window.body_entry);
91 gtk_widget_show(search_window.window);
94 void message_search_compose(Compose *compose)
96 if (!search_window.window)
97 message_search_create();
99 gtk_widget_hide(search_window.window);
101 search_window.compose = compose;
102 search_window.search_compose = TRUE;
104 gtk_widget_grab_focus(search_window.next_btn);
105 gtk_widget_grab_focus(search_window.body_entry);
106 gtk_widget_show(search_window.window);
109 static void message_search_create(void)
115 GtkWidget *body_label;
116 GtkWidget *body_entry;
118 GtkWidget *checkbtn_hbox;
119 GtkWidget *case_checkbtn;
121 GtkWidget *confirm_area;
124 GtkWidget *close_btn;
126 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
127 gtk_window_set_title (GTK_WINDOW (window),
128 _("Find in current message"));
129 gtk_widget_set_size_request (window, 450, -1);
130 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
131 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
132 g_signal_connect(G_OBJECT(window), "delete_event",
133 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
134 g_signal_connect(G_OBJECT(window), "key_press_event",
135 G_CALLBACK(key_pressed), NULL);
136 MANAGE_WINDOW_SIGNALS_CONNECT(window);
138 vbox1 = gtk_vbox_new (FALSE, 0);
139 gtk_widget_show (vbox1);
140 gtk_container_add (GTK_CONTAINER (window), vbox1);
142 hbox1 = gtk_hbox_new (FALSE, 8);
143 gtk_widget_show (hbox1);
144 gtk_box_pack_start (GTK_BOX (vbox1), hbox1, TRUE, TRUE, 0);
146 body_label = gtk_label_new (_("Find text:"));
147 gtk_widget_show (body_label);
148 gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
150 body_entry = gtk_entry_new ();
151 gtk_widget_show (body_entry);
152 gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
153 g_signal_connect(G_OBJECT(body_entry), "activate",
154 G_CALLBACK(body_activated), NULL);
156 checkbtn_hbox = gtk_hbox_new (FALSE, 8);
157 gtk_widget_show (checkbtn_hbox);
158 gtk_box_pack_start (GTK_BOX (vbox1), checkbtn_hbox, TRUE, TRUE, 0);
159 gtk_container_set_border_width (GTK_CONTAINER (checkbtn_hbox), 8);
161 case_checkbtn = gtk_check_button_new_with_label (_("Case sensitive"));
162 gtk_widget_show (case_checkbtn);
163 gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
166 gtkut_stock_button_set_create(&confirm_area,
167 &prev_btn, GTK_STOCK_GO_BACK,
168 &next_btn, GTK_STOCK_GO_FORWARD,
169 &close_btn, GTK_STOCK_CLOSE);
170 gtk_widget_show (confirm_area);
171 gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
172 gtk_widget_grab_default(next_btn);
174 g_signal_connect(G_OBJECT(prev_btn), "clicked",
175 G_CALLBACK(message_search_prev_clicked), NULL);
176 g_signal_connect(G_OBJECT(next_btn), "clicked",
177 G_CALLBACK(message_search_next_clicked), NULL);
178 g_signal_connect_closure
179 (G_OBJECT(close_btn), "clicked",
180 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
184 search_window.window = window;
185 search_window.body_entry = body_entry;
186 search_window.case_checkbtn = case_checkbtn;
187 search_window.prev_btn = prev_btn;
188 search_window.next_btn = next_btn;
189 search_window.close_btn = close_btn;
192 static void message_search_execute(gboolean backward)
194 MessageView *messageview = search_window.messageview;
195 Compose *compose = search_window.compose;
197 gboolean all_searched = FALSE;
198 const gchar *body_str;
200 body_str = gtk_entry_get_text(GTK_ENTRY(search_window.body_entry));
201 if (*body_str == '\0') return;
203 case_sens = gtk_toggle_button_get_active
204 (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
211 if (search_window.search_compose) {
212 if (compose_search_string_backward
213 (compose, body_str, case_sens) == TRUE)
216 if (messageview_search_string_backward
217 (messageview, body_str, case_sens) == TRUE)
221 if (search_window.search_compose) {
222 if (compose_search_string
223 (compose, body_str, case_sens) == TRUE)
226 if (messageview_search_string
227 (messageview, body_str, case_sens) == TRUE)
233 alertpanel_full(_("Search failed"),
234 _("Search string not found."),
235 GTK_STOCK_CLOSE, NULL, NULL, FALSE,
236 NULL, ALERT_WARNING, G_ALERTDEFAULT);
243 str = _("Beginning of message reached; "
244 "continue from end?");
246 str = _("End of message reached; "
247 "continue from beginning?");
249 val = alertpanel(_("Search finished"), str,
250 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
251 if (G_ALERTALTERNATE == val) {
252 manage_window_focus_in(search_window.window,
254 if (search_window.search_compose) {
255 compose_set_position(compose,
258 messageview_set_position(messageview,
266 static void message_search_prev_clicked(GtkButton *button, gpointer data)
268 message_search_execute(TRUE);
271 static void message_search_next_clicked(GtkButton *button, gpointer data)
273 message_search_execute(FALSE);
276 static void body_activated(void)
278 gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
281 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
284 if (event && event->keyval == GDK_Escape)
285 gtk_widget_hide(search_window.window);