fix bug 4239, 'Preferences: Text Options Header Display modal is not modal' (sic)
[claws.git] / src / message_search.c
index d5f0808c1e12166a9700a6e69bf362043b79c7e0..fab403bea0bf0a658715a1b6ca22b754eaa32f16 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkwindow.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtktable.h>
-#include <gtk/gtklabel.h>
-#include <gtk/gtkentry.h>
-#include <gtk/gtkhbox.h>
-#include <gtk/gtkcheckbutton.h>
-#include <gtk/gtkhbbox.h>
-#include <gtk/gtkbutton.h>
-#include <gtk/gtkctree.h>
+#include <gtk/gtk.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include "intl.h"
 #include "main.h"
 #include "message_search.h"
 #include "messageview.h"
+#include "compose.h"
 #include "utils.h"
 #include "gtkutils.h"
+#include "combobox.h"
 #include "manage_window.h"
 #include "alertpanel.h"
+#include "manual.h"
+#include "prefs_common.h"
 
-static GtkWidget *window;
-static GtkWidget *body_entry;
-static GtkWidget *case_checkbtn;
-static GtkWidget *backward_checkbtn;
-static GtkWidget *search_btn;
-static GtkWidget *clear_btn;
-static GtkWidget *close_btn;
+static struct MessageSearchWindow {
+       GtkWidget *window;
+       GtkWidget *body_entry;
+       GtkWidget *case_checkbtn;
+       GtkWidget *help_btn;
+       GtkWidget *prev_btn;
+       GtkWidget *next_btn;
+       GtkWidget *close_btn;
+       GtkWidget *stop_btn;
 
-static void message_search_create(MessageView *summaryview);
-static void message_search_execute(GtkButton *button, gpointer data);
-static void message_search_clear(GtkButton *button, gpointer data);
-static void body_activated(void);
-static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data);
+       SearchInterface *interface;
+       void *interface_obj;
+
+       gboolean is_searching;
+       gboolean body_entry_has_focus;
+} search_window;
+
+static SearchInterface compose_interface = {
+       .search_string_backward = (SearchStringFunc) compose_search_string_backward,
+       .set_position = (SetPositionFunc) compose_set_position,
+       .search_string = (SearchStringFunc) compose_search_string,
+};
+
+static SearchInterface messageview_interface = {
+       .set_position = (SetPositionFunc) messageview_set_position,
+       .search_string = (SearchStringFunc) messageview_search_string,
+       .search_string_backward = (SearchStringFunc) messageview_search_string_backward,
+};
+
+static void message_search_create      (void);
+static void message_search_execute     (gboolean        backward);
+
+static void message_search_prev_clicked        (GtkButton      *button,
+                                        gpointer        data);
+static void message_search_next_clicked        (GtkButton      *button,
+                                        gpointer        data);
+static void message_search_stop_clicked        (GtkButton      *button,
+                                        gpointer        data);
+static void body_changed               (void);
+static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
+                                 gpointer data);
+static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
+                                 gpointer data);
+static gboolean key_pressed            (GtkWidget      *widget,
+                                        GdkEventKey    *event,
+                                        gpointer        data);
+
+
+#define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                   \
+       gtk_widget_set_sensitive(widget, sensitive);                                    \
+}
+
+static void message_show_stop_button(void)
+{
+       gtk_widget_hide(search_window.close_btn);
+       gtk_widget_show(search_window.stop_btn);
+       GTK_BUTTON_SET_SENSITIVE(search_window.prev_btn, FALSE)
+       GTK_BUTTON_SET_SENSITIVE(search_window.next_btn, FALSE)
+}
+
+static void message_hide_stop_button(void)
+{
+       gtk_widget_hide(search_window.stop_btn);
+       gtk_widget_show(search_window.close_btn);
+       gtk_widget_set_sensitive(search_window.prev_btn, TRUE);
+       gtk_widget_set_sensitive(search_window.next_btn, TRUE);
+}
 
 void message_search(MessageView *messageview)
 {
-       if (!window)
-               message_search_create(messageview);
+       message_search_other(&messageview_interface, (void *)messageview);
+}
+
+void message_search_compose(Compose *compose)
+{
+       message_search_other(&compose_interface, (void *)compose);
+}
+
+void message_search_close (void *obj)
+{
+       if(!search_window.window) {
+               return;
+       }
+       if (search_window.interface_obj == obj) {
+               gtk_widget_hide(search_window.window);
+               search_window.interface_obj = NULL;
+       }
+}
+
+void message_search_other(SearchInterface *interface, void *obj)
+{
+       if (!search_window.window)
+               message_search_create();
        else
-               gtk_widget_hide(window);
+               gtk_widget_hide(search_window.window);
+
+       search_window.interface_obj = obj;
+       search_window.interface = interface;
 
-       gtk_widget_grab_focus(search_btn);
-       gtk_widget_grab_focus(body_entry);
-       gtk_widget_show(window);
+       gtk_widget_grab_focus(search_window.next_btn);
+       gtk_widget_grab_focus(search_window.body_entry);
+       gtk_widget_show(search_window.window);
 }
 
-static void message_search_create(MessageView *messageview)
+
+static void message_search_create(void)
 {
+       GtkWidget *window;
+
        GtkWidget *vbox1;
        GtkWidget *hbox1;
        GtkWidget *body_label;
+       GtkWidget *body_entry;
+
        GtkWidget *checkbtn_hbox;
+       GtkWidget *case_checkbtn;
+
        GtkWidget *confirm_area;
+       GtkWidget *help_btn;
+       GtkWidget *prev_btn;
+       GtkWidget *next_btn;
+       GtkWidget *close_btn;
+       GtkWidget *stop_btn;
 
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "message_search");
        gtk_window_set_title (GTK_WINDOW (window),
                              _("Find in current message"));
        gtk_widget_set_size_request (window, 450, -1);
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
+       gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
        gtk_container_set_border_width (GTK_CONTAINER (window), 8);
        g_signal_connect(G_OBJECT(window), "delete_event",
                         G_CALLBACK(gtk_widget_hide_on_delete), NULL);
@@ -107,11 +193,19 @@ static void message_search_create(MessageView *messageview)
        gtk_widget_show (body_label);
        gtk_box_pack_start (GTK_BOX (hbox1), body_label, FALSE, FALSE, 0);
 
-       body_entry = gtk_entry_new ();
+       body_entry = gtk_combo_box_text_new_with_entry ();
+       gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
+       if (prefs_common.message_search_history)
+               combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(body_entry),
+                               prefs_common.message_search_history);
        gtk_widget_show (body_entry);
        gtk_box_pack_start (GTK_BOX (hbox1), body_entry, TRUE, TRUE, 0);
-       g_signal_connect(G_OBJECT(body_entry), "activate",
-                        G_CALLBACK(body_activated), messageview);
+       g_signal_connect(G_OBJECT(body_entry), "changed",
+                        G_CALLBACK(body_changed), NULL);
+       g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
+                        "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
+       g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN((body_entry)))),
+                        "focus_out_event", G_CALLBACK(body_entry_focus_evt_out), NULL);
 
        checkbtn_hbox = gtk_hbox_new (FALSE, 8);
        gtk_widget_show (checkbtn_hbox);
@@ -122,68 +216,109 @@ static void message_search_create(MessageView *messageview)
        gtk_widget_show (case_checkbtn);
        gtk_box_pack_start (GTK_BOX (checkbtn_hbox), case_checkbtn,
                            FALSE, FALSE, 0);
-       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(case_checkbtn), FALSE);
 
-       backward_checkbtn =
-               gtk_check_button_new_with_label (_("Backward search"));
-       gtk_widget_show (backward_checkbtn);
-       gtk_box_pack_start (GTK_BOX (checkbtn_hbox), backward_checkbtn,
-                           FALSE, FALSE, 0);
+       confirm_area = gtk_hbutton_box_new();
+       gtk_widget_show (confirm_area);
+       gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area),
+                                 GTK_BUTTONBOX_END);
+       gtk_box_set_spacing(GTK_BOX(confirm_area), 5);
+
+       gtkut_stock_button_add_help(confirm_area, &help_btn);
+
+       prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
+       gtk_widget_set_can_default(prev_btn, TRUE);
+       gtk_box_pack_start(GTK_BOX(confirm_area), prev_btn, TRUE, TRUE, 0);
+       gtk_widget_show(prev_btn);
+
+       next_btn = gtk_button_new_from_stock(GTK_STOCK_GO_FORWARD);
+       gtk_widget_set_can_default(next_btn, TRUE);
+       gtk_box_pack_start(GTK_BOX(confirm_area), next_btn, TRUE, TRUE, 0);
+       gtk_widget_show(next_btn);
+
+       close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
+       gtk_widget_set_can_default(close_btn, TRUE);
+       gtk_box_pack_start(GTK_BOX(confirm_area), close_btn, TRUE, TRUE, 0);
+       gtk_widget_show(close_btn);
+
+       /* stop button hidden */
+       stop_btn = gtk_button_new_from_stock(GTK_STOCK_STOP);
+       gtk_widget_set_can_default(stop_btn, TRUE);
+       gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
 
-       gtkut_button_set_create(&confirm_area,
-                               &search_btn, _("Search"),
-                               &clear_btn,  _("Clear"),
-                               &close_btn,  _("Close"));
        gtk_widget_show (confirm_area);
        gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
-       gtk_widget_grab_default(search_btn);
-
-       g_signal_connect(G_OBJECT(search_btn), "clicked",
-                        G_CALLBACK(message_search_execute),
-                        messageview);
-       g_signal_connect(G_OBJECT(clear_btn), "clicked",
-                        G_CALLBACK(message_search_clear),
-                        messageview);
+       gtk_widget_grab_default(next_btn);
+
+       g_signal_connect(G_OBJECT(help_btn), "clicked",
+                        G_CALLBACK(manual_open_with_anchor_cb),
+                        MANUAL_ANCHOR_SEARCHING);
+       g_signal_connect(G_OBJECT(prev_btn), "clicked",
+                        G_CALLBACK(message_search_prev_clicked), NULL);
+       g_signal_connect(G_OBJECT(next_btn), "clicked",
+                        G_CALLBACK(message_search_next_clicked), NULL);
        g_signal_connect_closure
                (G_OBJECT(close_btn), "clicked",
                 g_cclosure_new_swap(G_CALLBACK(gtk_widget_hide),
                                     window, NULL),
                 FALSE);
+       g_signal_connect(G_OBJECT(stop_btn), "clicked",
+                        G_CALLBACK(message_search_stop_clicked), NULL);
+
+       search_window.window = window;
+       search_window.body_entry = body_entry;
+       search_window.case_checkbtn = case_checkbtn;
+       search_window.help_btn = help_btn;
+       search_window.prev_btn = prev_btn;
+       search_window.next_btn = next_btn;
+       search_window.close_btn = close_btn;
+       search_window.stop_btn = stop_btn;
 }
 
-static void message_search_execute(GtkButton *button, gpointer data)
+static void message_search_execute(gboolean backward)
 {
-       MessageView *messageview = data;
+       void *interface_obj = search_window.interface_obj;
        gboolean case_sens;
-       gboolean backward;
        gboolean all_searched = FALSE;
-       const gchar *body_str;
+       gchar *body_str;
+
+       body_str = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(search_window.body_entry));
+       if (!body_str)
+               body_str = gtk_editable_get_chars(
+                               GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
+       if (!body_str || *body_str == '\0') return;
 
-       body_str = gtk_entry_get_text(GTK_ENTRY(body_entry));
-       if (*body_str == '\0') return;
+       /* add to history */
+       combobox_unset_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry));
+       prefs_common.message_search_history = add_history(
+                       prefs_common.message_search_history, body_str);
+       combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(search_window.body_entry),
+                       prefs_common.message_search_history);
 
        case_sens = gtk_toggle_button_get_active
-               (GTK_TOGGLE_BUTTON(case_checkbtn));
-       backward = gtk_toggle_button_get_active
-               (GTK_TOGGLE_BUTTON(backward_checkbtn));
+               (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
 
-       for (;;) {
+       search_window.is_searching = TRUE;
+       message_show_stop_button();
+
+       for (; search_window.is_searching;) {
                gchar *str;
                AlertValue val;
 
                if (backward) {
-                       if (messageview_search_string_backward
-                               (messageview, body_str, case_sens) == TRUE)
+                       if (search_window.interface->search_string_backward
+                               (interface_obj, body_str, case_sens) == TRUE)
                                break;
                } else {
-                       if (messageview_search_string
-                               (messageview, body_str, case_sens) == TRUE)
+                       if (search_window.interface->search_string
+                               (interface_obj, body_str, case_sens) == TRUE)
                                break;
                }
 
                if (all_searched) {
-                       alertpanel_notice
-                               (_("Search string not found."));
+                       alertpanel_full(_("Search failed"),
+                                       _("Search string not found."),
+                                        GTK_STOCK_CLOSE, NULL, NULL, FALSE,
+                                        ALERTFOCUS_FIRST, NULL, ALERT_WARNING);
                        break;
                }
 
@@ -197,29 +332,75 @@ static void message_search_execute(GtkButton *button, gpointer data)
                                "continue from beginning?");
 
                val = alertpanel(_("Search finished"), str,
-                                _("Yes"), _("No"), NULL);
-               if (G_ALERTDEFAULT == val) {
-                       manage_window_focus_in(window, NULL, NULL);
-                       messageview_set_position(messageview,
-                                                backward ? -1 : 0);
+                                GTK_STOCK_NO, GTK_STOCK_YES, NULL, ALERTFOCUS_SECOND);
+               if (G_ALERTALTERNATE == val) {
+                       manage_window_focus_in(search_window.window,
+                                              NULL, NULL);
+                       search_window.interface->set_position(interface_obj,
+                                                       backward ? -1 : 0);
                } else
                        break;
        }
+
+       search_window.is_searching = FALSE;
+       message_hide_stop_button();
+       g_free(body_str);
 }
 
-static void message_search_clear(GtkButton *button, gpointer data)
+static void body_changed(void)
 {
-       gtk_editable_delete_text(GTK_EDITABLE(body_entry),    0, -1);
+       if (!search_window.body_entry_has_focus)
+               gtk_widget_grab_focus(search_window.body_entry);
 }
 
-static void body_activated(void)
+static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
+                                 gpointer data)
 {
-       gtk_button_clicked(GTK_BUTTON(search_btn));
+       search_window.body_entry_has_focus = TRUE;
+       return FALSE;
 }
 
-static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
+static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
+                                 gpointer data)
 {
-       if (event && event->keyval == GDK_Escape)
-               gtk_widget_hide(window);
+       search_window.body_entry_has_focus = FALSE;
        return FALSE;
 }
+
+static void message_search_prev_clicked(GtkButton *button, gpointer data)
+{
+       message_search_execute(TRUE);
+}
+
+static void message_search_next_clicked(GtkButton *button, gpointer data)
+{
+       message_search_execute(FALSE);
+}
+
+static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
+                           gpointer data)
+{
+       if (event && (event->keyval == GDK_KEY_Escape)) {
+               gtk_widget_hide(search_window.window);
+       }
+
+       if (event && (event->keyval == GDK_KEY_Return || event->keyval == GDK_KEY_KP_Enter)) {
+               message_search_execute(FALSE);
+       }
+
+       if (event && (event->keyval == GDK_KEY_Down || event->keyval == GDK_KEY_Up)) {
+               if (search_window.body_entry_has_focus) {
+                       combobox_set_value_from_arrow_key(
+                                       GTK_COMBO_BOX(search_window.body_entry),
+                                       event->keyval);
+                       return TRUE;
+               }
+       }
+
+       return FALSE;
+}
+
+static void message_search_stop_clicked(GtkButton *button, gpointer data)
+{
+       search_window.is_searching = FALSE;
+}