2007-08-04 [colin] 2.10.0cvs89
[claws.git] / src / summary_search.c
index b861db7261f3dd2c60cd2c058b220bc3ad96927f..48f927b2922fde12f21b2a7cb4b80ef0e8d69262 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2007 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
@@ -114,6 +114,12 @@ static void summary_search_stop_clicked    (GtkButton      *button,
 static void adv_condition_btn_clicked  (GtkButton      *button,
                                         gpointer        data);
 
+static void from_changed                       (void);
+static void to_changed                         (void);
+static void subject_changed                    (void);
+static void body_changed                       (void);
+static void adv_condition_changed      (void);
+
 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
                                  gpointer data);
 static gboolean from_entry_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
@@ -188,8 +194,8 @@ static void summary_search_create(void)
        GtkWidget *vbox1;
        GtkWidget *bool_hbox;
        GtkWidget *bool_optmenu;
-       GtkWidget *bool_menu;
-       GtkWidget *menuitem;
+       GtkListStore *menu;
+       GtkTreeIter iter;
        GtkWidget *clear_btn;
 
        GtkWidget *table1;
@@ -220,14 +226,18 @@ static void summary_search_create(void)
 
        gboolean is_searching = FALSE;
 
-       window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+       window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "summary_search");
        gtk_window_set_title(GTK_WINDOW (window), _("Search messages"));
        gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
        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);
+#ifdef MAEMO
+       maemo_connect_key_press_to_mainwindow(GTK_WINDOW(window));
+#else
        g_signal_connect(G_OBJECT(window), "key_press_event",
                         G_CALLBACK(key_pressed), NULL);
+#endif
        MANAGE_WINDOW_SIGNALS_CONNECT(window);
 
        vbox1 = gtk_vbox_new (FALSE, 0);
@@ -238,14 +248,14 @@ static void summary_search_create(void)
        gtk_widget_show(bool_hbox);
        gtk_box_pack_start(GTK_BOX(vbox1), bool_hbox, FALSE, FALSE, 0);
 
-       bool_optmenu = gtk_option_menu_new();
+       bool_optmenu = gtkut_sc_combobox_create(NULL, FALSE);
+       menu = GTK_LIST_STORE(gtk_combo_box_get_model(GTK_COMBO_BOX(bool_optmenu)));
        gtk_widget_show(bool_optmenu);
        gtk_box_pack_start(GTK_BOX(bool_hbox), bool_optmenu, FALSE, FALSE, 0);
 
-       bool_menu = gtk_menu_new();
-       MENUITEM_ADD(bool_menu, menuitem, _("Match any of the following"), 0);
-       MENUITEM_ADD(bool_menu, menuitem, _("Match all of the following"), 1);
-       gtk_option_menu_set_menu(GTK_OPTION_MENU(bool_optmenu), bool_menu);
+       COMBOBOX_ADD(menu, _("Match any of the following"), 0);
+       gtk_combo_box_set_active_iter(GTK_COMBO_BOX(bool_optmenu), &iter);
+       COMBOBOX_ADD(menu, _("Match all of the following"), 1);
 
        clear_btn = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
        gtk_widget_show(clear_btn);
@@ -281,8 +291,8 @@ static void summary_search_create(void)
        gtk_widget_show (to_entry);
        gtk_table_attach (GTK_TABLE (table1), to_entry, 1, 3, 1, 2,
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
-       g_signal_connect(G_OBJECT(from_entry), "changed",
-                        G_CALLBACK(from_changed), NULL);
+       g_signal_connect(G_OBJECT(to_entry), "changed",
+                        G_CALLBACK(to_changed), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(to_entry)->child),
                         "focus_in_event", G_CALLBACK(to_entry_focus_evt_in), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(to_entry)->child),
@@ -296,8 +306,8 @@ static void summary_search_create(void)
        gtk_widget_show (subject_entry);
        gtk_table_attach (GTK_TABLE (table1), subject_entry, 1, 3, 2, 3,
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
-       g_signal_connect(G_OBJECT(from_entry), "changed",
-                        G_CALLBACK(from_changed), NULL);
+       g_signal_connect(G_OBJECT(subject_entry), "changed",
+                        G_CALLBACK(subject_changed), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(subject_entry)->child),
                         "focus_in_event", G_CALLBACK(subject_entry_focus_evt_in), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(subject_entry)->child),
@@ -311,8 +321,8 @@ static void summary_search_create(void)
        gtk_widget_show (body_entry);
        gtk_table_attach (GTK_TABLE (table1), body_entry, 1, 3, 3, 4,
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
-       g_signal_connect(G_OBJECT(from_entry), "changed",
-                        G_CALLBACK(from_changed), NULL);
+       g_signal_connect(G_OBJECT(body_entry), "changed",
+                        G_CALLBACK(body_changed), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(body_entry)->child),
                         "focus_in_event", G_CALLBACK(body_entry_focus_evt_in), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(body_entry)->child),
@@ -326,8 +336,8 @@ static void summary_search_create(void)
        gtk_widget_show (adv_condition_entry);
        gtk_table_attach (GTK_TABLE (table1), adv_condition_entry, 1, 2, 4, 5,
                          GTK_EXPAND|GTK_FILL, 0, 0, 0);
-       g_signal_connect(G_OBJECT(from_entry), "changed",
-                        G_CALLBACK(from_changed), NULL);
+       g_signal_connect(G_OBJECT(adv_condition_entry), "changed",
+                        G_CALLBACK(adv_condition_changed), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(adv_condition_entry)->child),
                         "focus_in_event", G_CALLBACK(adv_condition_entry_focus_evt_in), NULL);
        g_signal_connect(G_OBJECT(GTK_BIN(adv_condition_entry)->child),
@@ -429,7 +439,7 @@ static void summary_search_create(void)
        gtk_box_pack_start (GTK_BOX (vbox1), confirm_area, FALSE, FALSE, 0);
        gtk_widget_grab_default(next_btn);
 
-       SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_menu)
+       SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, bool_optmenu)
        SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, from_entry)
        SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, to_entry)
        SET_TOGGLE_SENSITIVITY_REVERSE(adv_search_checkbtn, subject_entry)
@@ -476,6 +486,9 @@ static void summary_search_create(void)
        search_window.stop_btn = stop_btn;
        search_window.matcher_list = NULL;
        search_window.is_searching = is_searching;
+#ifdef MAEMO
+       maemo_window_full_screen_if_needed(GTK_WINDOW(search_window.window));
+#endif
 }
 
 static void summary_search_execute(gboolean backward, gboolean search_all)
@@ -490,8 +503,9 @@ static void summary_search_execute(gboolean backward, gboolean search_all)
        gboolean all_searched = FALSE;
        gboolean matched = FALSE;
        gboolean body_matched = FALSE;
-       const gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
-       const gchar *body_str = NULL, *adv_condition = NULL;
+       gchar *from_str = NULL, *to_str = NULL, *subject_str = NULL;
+       gchar *body_str = NULL;
+       gchar *adv_condition = NULL;
        StrFindFunc str_find_func = NULL;
        gboolean is_fast = TRUE;
        gint interval = 1000;
@@ -511,7 +525,10 @@ static void summary_search_execute(gboolean backward, gboolean search_all)
                        search_window.matcher_list = NULL;
                }
                adv_condition = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.adv_condition_entry));
-               if (adv_condition[0] != '\0') {
+               if (!adv_condition)
+                       adv_condition = gtk_editable_get_chars(
+                                       GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.adv_condition_entry))),0,-1);
+               if (adv_condition && adv_condition[0] != '\0') {
 
                        /* add to history */
                        combobox_unset_popdown_strings(GTK_COMBO_BOX(search_window.adv_condition_entry));
@@ -524,6 +541,7 @@ static void summary_search_execute(gboolean backward, gboolean search_all)
                        if (!is_fast)
                                interval = 100;
                        /* TODO: check for condition parsing error and show an error dialog */
+                       g_free(adv_condition);
                } else {
                        /* TODO: warn if no search condition? (or make buttons enabled only when
                                at least one search condition has been set */
@@ -531,9 +549,8 @@ static void summary_search_execute(gboolean backward, gboolean search_all)
                        return;
                }
        } else {
-               bool_and = GPOINTER_TO_INT
-                       (menu_get_option_menu_active_user_data
-                               (GTK_OPTION_MENU(search_window.bool_optmenu)));
+               bool_and = combobox_get_active_data(
+                               GTK_COMBO_BOX(search_window.bool_optmenu));
                case_sens = gtk_toggle_button_get_active
                        (GTK_TOGGLE_BUTTON(search_window.case_checkbtn));
 
@@ -548,6 +565,25 @@ static void summary_search_execute(gboolean backward, gboolean search_all)
                subject_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.subject_entry));
                body_str    = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.body_entry));
 
+               if (!from_str)
+                       from_str = gtk_editable_get_chars(
+                                       GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.from_entry))),0,-1);
+               if (!to_str)
+                       to_str = gtk_editable_get_chars(
+                                       GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.to_entry))),0,-1);
+               if (!subject_str)
+                       subject_str = gtk_editable_get_chars(
+                                       GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.subject_entry))),0,-1);
+               if (!body_str)
+                       body_str = gtk_editable_get_chars(
+                                       GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(search_window.body_entry))),0,-1);
+
+               if (!from_str || !to_str || !subject_str || !body_str) {
+                       /* TODO: warn if no search criteria? (or make buttons enabled only when
+                        * at least one search criteria has been set */
+                       summary_unlock(summaryview);
+                       return;
+               }
                if (    (from_str[0] == '\0') &&
                                (to_str[0] == '\0') &&
                                (subject_str[0] == '\0') &&
@@ -753,6 +789,11 @@ static void summary_search_execute(gboolean backward, gboolean search_all)
                        GTK_EVENTS_FLUSH();
        }
 
+       g_free(from_str);
+       g_free(to_str);
+       g_free(subject_str);
+       g_free(body_str);
+
        search_window.is_searching = FALSE;
        summary_hide_stop_button();
        main_window_cursor_normal(summaryview->mainwin);
@@ -828,7 +869,7 @@ static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
        /* re-use the current search value if it's a condition expression,
           otherwise ignore it silently */
        cond_str = gtk_combo_box_get_active_text(GTK_COMBO_BOX(search_window.adv_condition_entry));
-       if (*cond_str != '\0') {
+       if (cond_str && *cond_str != '\0') {
                matchers = matcher_parser_get_cond((gchar*)cond_str, NULL);
        }
 
@@ -839,6 +880,36 @@ static void adv_condition_btn_clicked(GtkButton *button, gpointer data)
        }
 };
 
+static void from_changed(void)
+{
+       if (!search_window.from_entry_has_focus)
+               gtk_widget_grab_focus(search_window.from_entry);
+}
+
+static void to_changed(void)
+{
+       if (!search_window.to_entry_has_focus)
+               gtk_widget_grab_focus(search_window.to_entry);
+}
+
+static void subject_changed(void)
+{
+       if (!search_window.subject_entry_has_focus)
+               gtk_widget_grab_focus(search_window.subject_entry);
+}
+
+static void body_changed(void)
+{
+       if (!search_window.body_entry_has_focus)
+               gtk_widget_grab_focus(search_window.body_entry);
+}
+
+static void adv_condition_changed(void)
+{
+       if (!search_window.adv_condition_entry_has_focus)
+               gtk_widget_grab_focus(search_window.adv_condition_entry);
+}
+
 static gboolean from_entry_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
                                  gpointer data)
 {
@@ -924,6 +995,12 @@ static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event,
                }
        }
 
+       if (event && (event->keyval == GDK_Return || event->keyval == GDK_KP_Enter)) {
+               if (!search_window.is_searching) {
+                       summary_search_execute(FALSE, FALSE);
+               }
+       }
+
        if (event && (event->keyval == GDK_Down || event->keyval == GDK_Up)) {
                if (search_window.from_entry_has_focus) {
                        combobox_set_value_from_arrow_key(