From: Andrea Zagli Date: Thu, 1 Sep 2011 16:47:34 +0000 (+0200) Subject: Started autocompletion on GtkFormDecoder. X-Git-Tag: 0.4.0~2^2~3 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=faca3054b139cf4fa053b529407573c113002e15;p=libgtkform Started autocompletion on GtkFormDecoder. refs #41 --- diff --git a/libgtkformui/gtkformdecoder.c b/libgtkformui/gtkformdecoder.c index b9fbced..4baf4b9 100644 --- a/libgtkformui/gtkformdecoder.c +++ b/libgtkformui/gtkformdecoder.c @@ -71,6 +71,11 @@ static void gtk_form_decoder_on_btn_open_clicked (GtkButton *button, static void gtk_form_decoder_on_btn_browse_clicked (GtkButton *button, gpointer user_data); +static gboolean gtk_form_decoder_on_completion_match_selected (GtkEntryCompletion *widget, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer user_data); + static GtkWidgetClass *parent_class = NULL; @@ -87,6 +92,10 @@ struct _GtkFormDecoderPrivate GdaEx *gdaex; gchar *sql; + + GdaDataModel *completion_dm; + guint completion_text_column; + guint completion_key_column; }; G_DEFINE_TYPE (GtkFormDecoder, gtk_form_decoder, GTK_TYPE_BIN) @@ -459,6 +468,82 @@ gtk_form_decoder_btn_browse_is_visible (GtkFormDecoder *decoder) return gtk_widget_get_visible (priv->btn_browse); } +void +gtk_form_decoder_set_completion (GtkFormDecoder *decoder, + const gchar *sql, + GdaDataModel *data_model, + guint key_column, + guint text_column) +{ + gchar *_sql; + + GtkTreeModel *model; + GtkEntryCompletion *completion; + + g_return_if_fail (GTK_IS_FORM_DECODER (decoder)); + + GtkFormDecoderPrivate *priv = GTK_FORM_DECODER_GET_PRIVATE (decoder); + + if (decoder == NULL) + { + priv->completion_dm = NULL; + priv->completion_text_column = 0; + priv->completion_key_column = 0; + + gtk_entry_set_completion (GTK_ENTRY (priv->txt_decoded), NULL); + gtk_editable_set_editable (GTK_EDITABLE (priv->txt_decoded), FALSE); + return; + } + + g_return_if_fail (sql != NULL || GDA_IS_DATA_MODEL (data_model)); + + if (!GDA_IS_DATA_MODEL (data_model)) + { + _sql = g_strstrip (g_strdup (sql)); + g_return_if_fail (g_strcmp0 (_sql, "") != 0); + + if (!IS_GDAEX (priv->gdaex)) + { + g_warning (_("No GdaEx object inside GtkFormDecoder.")); + return; + } + + priv->completion_dm = gdaex_query (priv->gdaex, sql); + if (priv->completion_dm == NULL) + { + g_warning (_("Unable to create the GdaDataModel for completion.")); + return; + } + } + else + { + priv->completion_dm = data_model; + } + + priv->completion_text_column = text_column; + priv->completion_key_column = key_column; + + model = GTK_TREE_MODEL (gdaex_data_model_to_gtkliststore (priv->completion_dm, FALSE)); + if (model == NULL) + { + return; + } + + completion = gtk_entry_completion_new (); + gtk_entry_set_completion (GTK_ENTRY (priv->txt_decoded), completion); + + g_signal_connect (G_OBJECT (completion), "match-selected", + G_CALLBACK (gtk_form_decoder_on_completion_match_selected), (gpointer)decoder); + + g_object_unref (completion); + + gtk_entry_completion_set_model (completion, model); + gtk_entry_completion_set_minimum_key_length (completion, 3); + gtk_entry_completion_set_text_column (completion, text_column); + + gtk_editable_set_editable (GTK_EDITABLE (priv->txt_decoded), TRUE); +} + /* CALLBACKS */ static void gtk_form_decoder_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) @@ -644,3 +729,21 @@ gtk_form_decoder_on_btn_browse_clicked (GtkButton *button, g_signal_emit (G_OBJECT (user_data), klass->btn_browse_clicked_signal_id, 0); } + +static gboolean +gtk_form_decoder_on_completion_match_selected (GtkEntryCompletion *widget, + GtkTreeModel *model, + GtkTreeIter *iter, + gpointer user_data) +{ + GValue gval = {0}; + + GtkFormDecoder *decoder = (GtkFormDecoder *)user_data; + GtkFormDecoderPrivate *priv = GTK_FORM_DECODER_GET_PRIVATE (decoder); + + gtk_tree_model_get_value (model, iter, priv->completion_key_column, &gval); + + gtk_form_decoder_set_key (decoder, gda_value_stringify (&gval)); + + return TRUE; +} diff --git a/libgtkformui/gtkformdecoder.h b/libgtkformui/gtkformdecoder.h index 07a02af..95a6aff 100644 --- a/libgtkformui/gtkformdecoder.h +++ b/libgtkformui/gtkformdecoder.h @@ -71,6 +71,12 @@ gboolean gtk_form_decoder_btn_clean_is_visible (GtkFormDecoder *decoder); gboolean gtk_form_decoder_btn_open_is_visible (GtkFormDecoder *decoder); gboolean gtk_form_decoder_btn_browse_is_visible (GtkFormDecoder *decoder); +void gtk_form_decoder_set_completion (GtkFormDecoder *decoder, + const gchar *sql, + GdaDataModel *data_model, + guint key_column, + guint text_column); + G_END_DECLS diff --git a/libgtkformui/test/decoder.c b/libgtkformui/test/decoder.c index 3d40b4b..a0e0566 100644 --- a/libgtkformui/test/decoder.c +++ b/libgtkformui/test/decoder.c @@ -80,6 +80,10 @@ main (int argc, char **argv) "show-btn-open", TRUE, NULL); + gtk_form_decoder_set_completion (GTK_FORM_DECODER (decoder), + "SELECT id, name FROM customers", + NULL, 0, 1); + g_signal_connect (G_OBJECT (decoder), "btn_clean_clicked", G_CALLBACK (on_btn_clean_clicked), NULL); g_signal_connect (G_OBJECT (decoder), "btn_open_clicked",