2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / message_search.c
index e4d31f13f67132e50e312fffa390fc680de94ae3..0fc06cca22d8fdbcd0dff20f53c71c712d93df70 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2011 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,
@@ -13,8 +13,8 @@
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_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 "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 struct MessageSearchWindow {
        GtkWidget *window;
@@ -66,6 +58,7 @@ static struct MessageSearchWindow {
        Compose *compose;
        gboolean search_compose;
        gboolean is_searching;
+       gboolean body_entry_has_focus;
 } search_window;
 
 static void message_search_create      (void);
@@ -77,20 +70,31 @@ static void message_search_next_clicked     (GtkButton      *button,
                                         gpointer        data);
 static void message_search_stop_clicked        (GtkButton      *button,
                                         gpointer        data);
-static void body_activated             (void);
+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) {   \
+#if !GTK_CHECK_VERSION(2,14,0)
+/* Work around http://bugzilla.gnome.org/show_bug.cgi?id=56070 */
+#define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                   \
        gboolean in_btn = FALSE;                                                        \
        if (GTK_IS_BUTTON(widget))                                                      \
-               in_btn = GTK_BUTTON(widget)->in_button;                 \
-       gtk_widget_set_sensitive(widget, sensitive);            \
+               in_btn = GTK_BUTTON(widget)->in_button;                                 \
+       gtk_widget_set_sensitive(widget, sensitive);                                    \
        if (GTK_IS_BUTTON(widget))                                                      \
-               GTK_BUTTON(widget)->in_button = in_btn;                 \
+               GTK_BUTTON(widget)->in_button = in_btn;                                 \
 }
+#else
+#define GTK_BUTTON_SET_SENSITIVE(widget,sensitive) {                                   \
+       gtk_widget_set_sensitive(widget, sensitive);                                    \
+}
+#endif
 
 static void message_show_stop_button(void)
 {
@@ -157,7 +161,7 @@ static void message_search_create(void)
        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);
@@ -181,11 +185,19 @@ static void message_search_create(void)
        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_entry_new_text ();
+       gtk_combo_box_set_active(GTK_COMBO_BOX(body_entry), -1);
+       if (prefs_common.message_search_history)
+               combobox_set_popdown_strings(GTK_COMBO_BOX(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), NULL);
+       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);
@@ -206,23 +218,23 @@ static void message_search_create(void)
        gtkut_stock_button_add_help(confirm_area, &help_btn);
 
        prev_btn = gtk_button_new_from_stock(GTK_STOCK_GO_BACK);
-       GTK_WIDGET_SET_FLAGS(prev_btn, GTK_CAN_DEFAULT);
+       gtkut_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_FLAGS(next_btn, GTK_CAN_DEFAULT);
+       gtkut_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_FLAGS(close_btn, GTK_CAN_DEFAULT);
+       gtkut_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_FLAGS(stop_btn, GTK_CAN_DEFAULT);
+       gtkut_widget_set_can_default(stop_btn, TRUE);
        gtk_box_pack_start(GTK_BOX(confirm_area), stop_btn, TRUE, TRUE, 0);
 
        gtk_widget_show (confirm_area);
@@ -260,10 +272,20 @@ static void message_search_execute(gboolean backward)
        Compose *compose = search_window.compose;
        gboolean case_sens;
        gboolean all_searched = FALSE;
-       const gchar *body_str;
+       gchar *body_str;
+
+       body_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(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(search_window.body_entry));
-       if (*body_str == '\0') return;
+       /* add to history */
+       combobox_unset_popdown_strings(GTK_COMBO_BOX(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(search_window.body_entry),
+                       prefs_common.message_search_history);
 
        case_sens = gtk_toggle_button_get_active
                (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
@@ -332,6 +354,27 @@ static void message_search_execute(gboolean backward)
 
        search_window.is_searching = FALSE;
        message_hide_stop_button();
+       g_free(body_str);
+}
+
+static void body_changed(void)
+{
+       if (!search_window.body_entry_has_focus)
+               gtk_widget_grab_focus(search_window.body_entry);
+}
+
+static gboolean body_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
+                                 gpointer data)
+{
+       search_window.body_entry_has_focus = TRUE;
+       return FALSE;
+}
+
+static gboolean body_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
+                                 gpointer data)
+{
+       search_window.body_entry_has_focus = FALSE;
+       return FALSE;
 }
 
 static void message_search_prev_clicked(GtkButton *button, gpointer data)
@@ -344,16 +387,26 @@ static void message_search_next_clicked(GtkButton *button, gpointer data)
        message_search_execute(FALSE);
 }
 
-static void body_activated(void)
-{
-       gtk_button_clicked(GTK_BUTTON(search_window.next_btn));
-}
-
 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
                            gpointer data)
 {
-       if (event && event->keyval == GDK_Escape)
+       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;
 }