2009-02-27 Alberto Garcia <agarcia@igalia.com>
[hildon] / src / hildon-find-toolbar.c
index 8a869e0..f0bbd60 100644 (file)
@@ -3,12 +3,12 @@
  *
  * Copyright (C) 2005, 2006 Nokia Corporation, all rights reserved.
  *
- * Contact: Michael Dominic Kostrzewa <michael.kostrzewa@nokia.com>
+ * Contact: Rodrigo Novo <rodrigo.novo@nokia.com>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public License
  * as published by the Free Software Foundation; version 2.1 of
- * the License.
+ * the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * #GtkListStore and can be accesed using a property 'list'. Entries are added
  * automatically to the list when the search button is pressed.
  *
- */    
+ */
 
 #ifdef                                          HAVE_CONFIG_H
 #include                                        <config.h>
 #endif
 
-#include                                        "hildon-find-toolbar.h"
-#include                                        "hildon-defines.h"
-#include                                        <gdk/gdkkeysyms.h>
-#include                                        <gtk/gtklabel.h>
-#include                                        <gtk/gtkentry.h>
-#include                                        <gtk/gtkbutton.h>
-#include                                        <gtk/gtktoolbutton.h>
-#include                                        <gtk/gtktoolitem.h>
-#include                                        <gtk/gtkcomboboxentry.h>
-#include                                        <gtk/gtkseparatortoolitem.h>
 #include                                        <string.h>
 #include                                        <libintl.h>
+#include                                        <gdk/gdkkeysyms.h>
+
+#include                                        "hildon-find-toolbar.h"
+#include                                        "hildon-defines.h"
 #include                                        "hildon-find-toolbar-private.h"
+#include                                        "hildon-marshalers.h"
 
 #define                                         _(String) \
                                                 dgettext("hildon-libs", String)
@@ -108,10 +103,12 @@ static void
 hildon_find_toolbar_emit_close                  (GtkButton *button, 
                                                  gpointer self);
 
+#ifdef MAEMO_GTK 
 static void
 hildon_find_toolbar_emit_invalid_input          (GtkEntry *entry, 
                                                  GtkInvalidInputType type, 
                                                  gpointer self);
+#endif
 
 static void
 hildon_find_toolbar_entry_activate              (GtkWidget *widget,
@@ -151,7 +148,7 @@ static guint                                    HildonFindToolbar_signal [LAST_S
  *
  * Initializes and returns the type of a hildon fond toolbar.
  *
- * @Returns: GType of #HildonFindToolbar
+ * Returns: GType of #HildonFindToolbar
  */
 GType G_GNUC_CONST
 hildon_find_toolbar_get_type                    (void)
@@ -463,7 +460,7 @@ hildon_find_toolbar_history_append              (HildonFindToolbar *self,
 
     g_free (string);
 
-    return TRUE;
+    return FALSE;
 }
 
 static void
@@ -481,6 +478,7 @@ static void
 hildon_find_toolbar_emit_close                  (GtkButton *button, 
                                                  gpointer self)
 {
+#ifdef MAEMO_GTK 
     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
     g_assert (priv);
 
@@ -489,11 +487,13 @@ hildon_find_toolbar_emit_close                  (GtkButton *button,
     {
         hildon_gtk_im_context_hide (GTK_ENTRY (entry)->im_context);
     }
+#endif
 
     /* Clicked close button */
     g_signal_emit_by_name (self, "close", NULL);
 }
 
+#ifdef MAEMO_GTK 
 static void
 hildon_find_toolbar_emit_invalid_input          (GtkEntry *entry, 
                                                  GtkInvalidInputType type, 
@@ -502,6 +502,7 @@ hildon_find_toolbar_emit_invalid_input          (GtkEntry *entry,
     if(type == GTK_INVALID_INPUT_MAX_CHARS_REACHED)
         g_signal_emit_by_name (self, "invalid_input", NULL);
 }
+#endif
 
 static void
 hildon_find_toolbar_entry_activate              (GtkWidget *widget,
@@ -624,7 +625,7 @@ hildon_find_toolbar_class_init                  (HildonFindToolbarClass *klass)
                 "search", HILDON_TYPE_FIND_TOOLBAR,
                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
                 (HildonFindToolbarClass, search),
-                NULL, NULL, gtk_marshal_VOID__VOID,
+                NULL, NULL, g_cclosure_marshal_VOID__VOID,
                 G_TYPE_NONE, 0);
 
     /**
@@ -638,7 +639,7 @@ hildon_find_toolbar_class_init                  (HildonFindToolbarClass *klass)
                 "close", HILDON_TYPE_FIND_TOOLBAR,
                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
                 (HildonFindToolbarClass, close),
-                NULL, NULL, gtk_marshal_VOID__VOID,
+                NULL, NULL, g_cclosure_marshal_VOID__VOID,
                 G_TYPE_NONE, 0);
 
     /**
@@ -647,13 +648,13 @@ hildon_find_toolbar_class_init                  (HildonFindToolbarClass *klass)
      * 
      * Gets emitted when the maximum search prefix length is reached and
      * user tries to type more.
-     */ 
+     */
     HildonFindToolbar_signal[INVALID_INPUT] = 
         g_signal_new(
                 "invalid_input", HILDON_TYPE_FIND_TOOLBAR,
                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
                 (HildonFindToolbarClass, invalid_input),
-                NULL, NULL, gtk_marshal_VOID__VOID,
+                NULL, NULL, g_cclosure_marshal_VOID__VOID,
                 G_TYPE_NONE, 0);
 
     /**
@@ -668,7 +669,7 @@ hildon_find_toolbar_class_init                  (HildonFindToolbarClass *klass)
                 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET 
                 (HildonFindToolbarClass, history_append),
                 g_signal_accumulator_true_handled, NULL, 
-                gtk_marshal_BOOLEAN__VOID,
+                _hildon_marshal_BOOLEAN__VOID,
                 G_TYPE_BOOLEAN, 0);
 }
 
@@ -677,6 +678,7 @@ hildon_find_toolbar_init                        (HildonFindToolbar *self)
 {
     GtkToolItem *label_container;
     GtkToolItem *entry_combo_box_container;
+    GtkAlignment *alignment;
 
     HildonFindToolbarPrivate *priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (self);
     g_assert (priv);
@@ -696,15 +698,21 @@ hildon_find_toolbar_init                        (HildonFindToolbar *self)
 
     /* ComboBoxEntry for search prefix string / history list */
     priv->entry_combo_box = GTK_COMBO_BOX_ENTRY (gtk_combo_box_entry_new ());
+
+#ifdef MAEMO_GTK
     g_signal_connect (hildon_find_toolbar_get_entry(priv),
             "invalid_input", 
             G_CALLBACK(hildon_find_toolbar_emit_invalid_input), self);
+#endif
 
     entry_combo_box_container = gtk_tool_item_new ();
+    alignment = GTK_ALIGNMENT (gtk_alignment_new (0, 0.5, 1, 0));
 
     gtk_tool_item_set_expand (entry_combo_box_container, TRUE);
-    gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
+    gtk_container_add (GTK_CONTAINER (alignment),
             GTK_WIDGET (priv->entry_combo_box));
+    gtk_container_add (GTK_CONTAINER (entry_combo_box_container),
+            GTK_WIDGET (alignment));
     gtk_widget_show_all(GTK_WIDGET (entry_combo_box_container));
     gtk_toolbar_insert (GTK_TOOLBAR (self), entry_combo_box_container, -1);
     g_signal_connect (hildon_find_toolbar_get_entry (priv),
@@ -721,6 +729,7 @@ hildon_find_toolbar_init                        (HildonFindToolbar *self)
             G_CALLBACK(hildon_find_toolbar_emit_search), self);
     gtk_widget_show_all( GTK_WIDGET(priv->find_button));
     gtk_toolbar_insert ( GTK_TOOLBAR(self), priv->find_button, -1);
+    gtk_widget_set_size_request (GTK_WIDGET (priv->find_button), 72, -1);
     if ( GTK_WIDGET_CAN_FOCUS( GTK_BIN(priv->find_button)->child) )
         GTK_WIDGET_UNSET_FLAGS(
                 GTK_BIN(priv->find_button)->child, GTK_CAN_FOCUS);
@@ -735,6 +744,7 @@ hildon_find_toolbar_init                        (HildonFindToolbar *self)
             gtk_image_new_from_icon_name ("qgn_toolb_gene_close",
                 HILDON_ICON_SIZE_TOOLBAR),
             "Close");
+    gtk_widget_set_size_request (GTK_WIDGET (priv->close_button), 72, -1);
     g_signal_connect(priv->close_button, "clicked",
             G_CALLBACK(hildon_find_toolbar_emit_close), self);
     gtk_widget_show_all(GTK_WIDGET(priv->close_button));
@@ -820,3 +830,125 @@ hildon_find_toolbar_highlight_entry             (HildonFindToolbar *self,
         gtk_widget_grab_focus (GTK_WIDGET (entry));
 }
 
+/**
+ * hildon_find_toolbar_set_active:
+ * @toolbar: A find toolbar to operate on
+ * @index: An index in the model passed during construction, or -1 to have no active item
+ *
+ * Sets the active item on the toolbar's combo-box. Simply calls gtk_combo_box_set_active on 
+ * the HildonFindToolbar's combo.
+ * 
+ */
+void
+hildon_find_toolbar_set_active                  (HildonFindToolbar *toolbar,
+                                                 gint index)
+{
+    HildonFindToolbarPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar));
+    priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
+
+    gtk_combo_box_set_active (GTK_COMBO_BOX (priv->entry_combo_box), index);
+}
+
+/**
+ * hildon_find_toolbar_get_active:
+ * @toolbar: A find toolbar to query
+ *
+ * Gets the index of the currently active item, or -1 if there's no active item. Simply 
+ * calls gtk_combo_box_get_active on the HildonFindToolbar's combo.
+ *
+ * Returns: An integer which is the index of the currently active item, or -1 if there's no active item.
+ * 
+ */
+gint
+hildon_find_toolbar_get_active                  (HildonFindToolbar *toolbar)
+{
+    HildonFindToolbarPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar), -1);
+    priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
+
+    return gtk_combo_box_get_active (GTK_COMBO_BOX (priv->entry_combo_box));
+}
+
+/**
+ * hildon_find_toolbar_set_active_iter:
+ * @toolbar: A find toolbar to operate on
+ * @iter: An iter to make active
+ *
+ * Sets the current active item to be the one referenced by iter. Simply calls 
+ * gtk_combo_box_set_active_iter on the HildonFindToolbar's combo.
+ * 
+ */
+void
+hildon_find_toolbar_set_active_iter             (HildonFindToolbar *toolbar, 
+                                                 GtkTreeIter *iter)
+{
+    HildonFindToolbarPrivate *priv;
+
+    g_return_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar));
+    priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
+
+    gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->entry_combo_box), iter);
+}
+
+/**
+ * hildon_find_toolbar_get_active_iter:
+ * @toolbar: A find toolbar to query
+ * @iter: The uninitialized GtkTreeIter
+ *
+ * Sets iter to point to the current active item, if it exists. Simply calls 
+ * gtk_combo_box_get_active_iter on the HildonFindToolbar's combo.
+ * 
+ * Returns: TRUE, if iter was set
+ *
+ */
+gboolean
+hildon_find_toolbar_get_active_iter             (HildonFindToolbar *toolbar, 
+                                                 GtkTreeIter *iter)
+{
+    HildonFindToolbarPrivate *priv;
+
+    g_return_val_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar), FALSE);
+    priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
+
+    return gtk_combo_box_get_active_iter (GTK_COMBO_BOX (priv->entry_combo_box), iter);
+}
+
+/**
+ * hildon_find_toolbar_get_last_index
+ * @toolbar: A find toolbar to query
+ *
+ * Returns the index of the last (most recently added) item in the toolbar.
+ * Can be used to set this item active in the history-append signal.
+ *
+ * 
+ * Returns: Index of the last entry
+ *
+ */
+gint32
+hildon_find_toolbar_get_last_index              (HildonFindToolbar *toolbar)
+{
+    HildonFindToolbarPrivate *priv;
+    GtkTreeModel *filter_model;
+    
+    g_return_val_if_fail (HILDON_IS_FIND_TOOLBAR (toolbar), FALSE);
+    priv = HILDON_FIND_TOOLBAR_GET_PRIVATE (toolbar);
+
+    filter_model = gtk_combo_box_get_model (GTK_COMBO_BOX (priv->entry_combo_box));
+
+    if (filter_model == NULL)
+        return 0;
+
+    gint i = 0;
+    GtkTreeIter iter;
+
+    gtk_tree_model_get_iter_first (filter_model, &iter);
+
+    while (gtk_tree_model_iter_next (filter_model, &iter))
+        i++;
+
+    return i;
+}
+