From: Andrea Zagli Date: Thu, 20 Oct 2011 14:15:10 +0000 (+0200) Subject: In GtkFormDecoder's autocompletion added ability to search inside values. X-Git-Tag: 0.4.0~2^2 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=c96efdf5c60f662da0b39f61791acc590d8d11f1;p=libgtkform In GtkFormDecoder's autocompletion added ability to search inside values. closes #41 --- diff --git a/libgtkformui/gtkformdecoder.c b/libgtkformui/gtkformdecoder.c index 0c3ffef..fd6256a 100644 --- a/libgtkformui/gtkformdecoder.c +++ b/libgtkformui/gtkformdecoder.c @@ -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; +} diff --git a/libgtkformui/gtkformdecoder.h b/libgtkformui/gtkformdecoder.h index 7e5eb52..c3fc904 100644 --- a/libgtkformui/gtkformdecoder.h +++ b/libgtkformui/gtkformdecoder.h @@ -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 diff --git a/libgtkformui/test/decoder.c b/libgtkformui/test/decoder.c index 189d0b0..626f16e 100644 --- a/libgtkformui/test/decoder.c +++ b/libgtkformui/test/decoder.c @@ -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);