]> saetta.ns0.it Git - libgtkform/commitdiff
In GtkFormDecoder's autocompletion added ability to search inside values. autocompletion
authorAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 20 Oct 2011 14:15:10 +0000 (16:15 +0200)
committerAndrea Zagli <a.zagli@comune.scandicci.fi.it>
Thu, 20 Oct 2011 14:15:10 +0000 (16:15 +0200)
closes #41

libgtkformui/gtkformdecoder.c
libgtkformui/gtkformdecoder.h
libgtkformui/test/decoder.c

index 0c3ffefa3df8643ad57129f83c017bdbc97bd14a..fd6256afc328e20969d0c11c2eb5f2795f335c98 100644 (file)
@@ -78,6 +78,10 @@ static gboolean gtk_form_decoder_on_completion_match_selected (GtkEntryCompletio
 static gboolean gtk_form_decoder_on_decoded_focus_out_event (GtkWidget *widget,
                                                              GdkEvent *event,
                                                              gpointer user_data);
+static gboolean gtk_form_decoder_match_func (GtkEntryCompletion *completion,
+                                             const gchar *key,
+                                             GtkTreeIter *iter,
+                                             gpointer user_data);
 
 static GtkWidgetClass *parent_class = NULL;
 
@@ -100,6 +104,7 @@ struct _GtkFormDecoderPrivate
                guint completion_text_column;
                guint completion_key_column;
                gboolean completion_only_valid_values;
+               gboolean completion_search_inside;
                gulong completion_focus_out_handler_id;
        };
 
@@ -251,6 +256,7 @@ gtk_form_decoder_init (GtkFormDecoder *decoder)
        priv->completion_key_column = 0;
        priv->completion_text_column = 0;
        priv->completion_only_valid_values = FALSE;
+       priv->completion_search_inside = FALSE;
        priv->completion_focus_out_handler_id = 0;
 }
 
@@ -485,7 +491,8 @@ gtk_form_decoder_set_completion (GtkFormDecoder *decoder,
                                  GdaDataModel *data_model,
                                  guint key_column,
                                  guint text_column,
-                                 gboolean only_valid_values)
+                                 gboolean only_valid_values,
+                                 gboolean search_inside)
 {
        gchar *_sql;
 
@@ -502,6 +509,7 @@ gtk_form_decoder_set_completion (GtkFormDecoder *decoder,
                        priv->completion_text_column = 0;
                        priv->completion_key_column = 0;
                        priv->completion_only_valid_values = FALSE;
+                       priv->completion_search_inside = FALSE;
 
                        gtk_entry_set_completion (GTK_ENTRY (priv->txt_decoded), NULL);
                        gtk_editable_set_editable (GTK_EDITABLE (priv->txt_decoded), FALSE);
@@ -563,6 +571,15 @@ gtk_form_decoder_set_completion (GtkFormDecoder *decoder,
        gtk_entry_completion_set_minimum_key_length (completion, 3);
        gtk_entry_completion_set_text_column (completion, priv->completion_text_column);
 
+       priv->completion_search_inside = search_inside;
+       if (priv->completion_search_inside)
+               {
+                       gtk_entry_completion_set_match_func (completion,
+                                                            gtk_form_decoder_match_func,
+                                                            (gpointer)decoder,
+                                                            NULL);
+               }
+
        gtk_editable_set_editable (GTK_EDITABLE (priv->txt_decoded), TRUE);
 
        priv->completion_only_valid_values = only_valid_values;
@@ -788,6 +805,7 @@ gtk_form_decoder_on_decoded_focus_out_event (GtkWidget *widget,
 
        gchar *decoded;
        gchar *decoded_casefold;
+       gchar *value_casefold;
 
        gboolean found;
 
@@ -829,16 +847,19 @@ gtk_form_decoder_on_decoded_focus_out_event (GtkWidget *widget,
                                                                                  priv->completion_key_column, key);
                                                        gtk_tree_model_get_value (model, &iter,
                                                                                  priv->completion_text_column, value);
-                                                       if (g_strcmp0 (decoded_casefold, g_utf8_casefold (gda_value_stringify (value), -1)) == 0)
+                                                       value_casefold = g_utf8_casefold (gda_value_stringify (value), -1);
+                                                       if (g_strcmp0 (decoded_casefold, value_casefold) == 0)
                                                                {
                                                                        gtk_form_decoder_set_key (decoder, gda_value_stringify (key));
                                                                        found = TRUE;
                                                                        g_value_unset (key);
                                                                        g_value_unset (value);
+                                                                       g_free (value_casefold);
                                                                        break;
                                                                }
                                                        g_value_unset (key);
                                                        g_value_unset (value);
+                                                       g_free (value_casefold);
                                                } while (gtk_tree_model_iter_next (model, &iter));
 
                                        if (!found)
@@ -865,3 +886,70 @@ gtk_form_decoder_on_decoded_focus_out_event (GtkWidget *widget,
 
        return FALSE;
 }
+
+static gboolean
+gtk_form_decoder_match_func (GtkEntryCompletion *completion,
+                             const gchar *key,
+                             GtkTreeIter *iter,
+                             gpointer user_data)
+{
+       gchar *item;
+       gchar *item_down;
+       gchar *decoded;
+       gchar *decoded_down;
+
+       gboolean ret;
+
+       GtkTreeModel *model;
+
+       GtkFormDecoder *decoder = (GtkFormDecoder *)user_data;
+       GtkFormDecoderPrivate *priv = GTK_FORM_DECODER_GET_PRIVATE (decoder);
+
+       model = gtk_entry_completion_get_model (GTK_ENTRY_COMPLETION (completion));
+       if (model == NULL)
+               {
+                       return FALSE;
+               }
+
+       ret = FALSE;
+
+       gtk_tree_model_get (model, iter,
+                           priv->completion_text_column, &item,
+                           -1);
+
+       if (item != NULL)
+               {
+                       item_down = g_utf8_strdown (item, -1);
+
+                       decoded = (gchar *)gtk_entry_get_text (GTK_ENTRY (priv->txt_decoded));
+                       if (decoded == NULL)
+                               {
+                                       gtk_form_decoder_set_key (decoder, "0");
+                                       g_free (item_down);
+                                       return FALSE;
+                               }
+                       decoded = g_strdup (decoded);
+                       g_strstrip (decoded);
+                       if (g_strcmp0 (decoded, "") == 0)
+                               {
+                                       gtk_form_decoder_set_key (decoder, "0");
+                                       g_free (decoded);
+                                       g_free (item_down);
+                                       return FALSE;
+                               }
+
+                       decoded_down = g_utf8_strdown (decoded, -1);
+
+                       if (g_strstr_len (item_down, -1, decoded_down) != NULL)
+                               {
+                                       ret = TRUE;
+                               }
+
+                       g_free (decoded);
+                       g_free (decoded_down);
+                       g_free (item_down);
+                       g_free (item);
+               }
+
+       return ret;
+}
index 7e5eb52bc4e31d6e0c70e096656161c166a18cd9..c3fc9046e90edbc76ea3e092cc052efb927abf2c 100644 (file)
@@ -76,7 +76,8 @@ void gtk_form_decoder_set_completion (GtkFormDecoder *decoder,
                                       GdaDataModel *data_model,
                                       guint key_column,
                                       guint text_column,
-                                      gboolean only_valid_values);
+                                      gboolean only_valid_values,
+                                      gboolean search_inside);
 
 
 G_END_DECLS
index 189d0b02746ae8a6c7ecc54592d0f7424adef1a1..626f16e21ca84ec3ad8a0043c063ed363472fb39 100644 (file)
@@ -83,7 +83,7 @@ main (int argc, char **argv)
        gtk_form_decoder_set_completion (GTK_FORM_DECODER (decoder),
                                         "SELECT id, name FROM customers",
                                         NULL, 0, 1,
-                                        TRUE);
+                                        TRUE, TRUE);
 
        g_signal_connect (G_OBJECT (decoder), "btn_clean_clicked",
                          G_CALLBACK (on_btn_clean_clicked), NULL);