]> saetta.ns0.it Git - libgtkform/commitdiff
Started autocompletion on GtkFormDecoder.
authorAndrea Zagli <azagli@libero.it>
Thu, 1 Sep 2011 16:47:34 +0000 (18:47 +0200)
committerAndrea Zagli <azagli@libero.it>
Thu, 1 Sep 2011 16:47:34 +0000 (18:47 +0200)
refs #41

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

index b9fbcedfb688cc2f5ad0ecf0b77c09a73158d484..4baf4b9c3341b1ec17a40b0edb8b6dc7dfcf4476 100644 (file)
@@ -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;
+}
index 07a02af8f384270ff19bf64d6543e8f74755760d..95a6aff74e70e45759aca5e804e13fe13921f16b 100644 (file)
@@ -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
 
index 3d40b4b0aaea2aa88f8fccd6b8bb565e6820c2bc..a0e0566c490f014fdd27e6e462d3926d268ece9a 100644 (file)
@@ -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",