GtkTreeModel *model,
GtkTreeIter *iter,
gpointer user_data);
+static gboolean gtk_form_decoder_on_decoded_focus_out_event (GtkWidget *widget,
+ GdkEvent *event,
+ gpointer user_data);
static GtkWidgetClass *parent_class = NULL;
GdaDataModel *completion_dm;
guint completion_text_column;
guint completion_key_column;
+ gboolean completion_only_valid_values;
+ gulong completion_focus_out_handler_id;
};
G_DEFINE_TYPE (GtkFormDecoder, gtk_form_decoder, GTK_TYPE_BIN)
priv->lbl_key = gtk_label_new ("");
gtk_box_pack_start (GTK_BOX (priv->hbox), priv->lbl_key, FALSE, FALSE, 0);
- //gtk_widget_set_no_show_all (priv->lbl_key, TRUE);
- gtk_widget_show (priv->lbl_key);
+ gtk_widget_set_no_show_all (priv->lbl_key, TRUE);
+
+ priv->completion_dm = NULL;
+ priv->completion_key_column = 0;
+ priv->completion_text_column = 0;
+ priv->completion_only_valid_values = FALSE;
+ priv->completion_focus_out_handler_id = 0;
}
/**
const gchar *sql,
GdaDataModel *data_model,
guint key_column,
- guint text_column)
+ guint text_column,
+ gboolean only_valid_values)
{
gchar *_sql;
priv->completion_dm = NULL;
priv->completion_text_column = 0;
priv->completion_key_column = 0;
+ priv->completion_only_valid_values = FALSE;
gtk_entry_set_completion (GTK_ENTRY (priv->txt_decoded), NULL);
gtk_editable_set_editable (GTK_EDITABLE (priv->txt_decoded), FALSE);
+
+ g_signal_handler_disconnect (G_OBJECT (priv->txt_decoded), priv->completion_focus_out_handler_id);
return;
}
gtk_entry_completion_set_text_column (completion, priv->completion_text_column);
gtk_editable_set_editable (GTK_EDITABLE (priv->txt_decoded), TRUE);
+
+ priv->completion_only_valid_values = only_valid_values;
+
+ priv->completion_focus_out_handler_id = g_signal_connect (G_OBJECT (priv->txt_decoded), "focus-out-event",
+ G_CALLBACK (gtk_form_decoder_on_decoded_focus_out_event), (gpointer)decoder);
}
/* CALLBACKS */
return TRUE;
}
+
+static gboolean
+gtk_form_decoder_on_decoded_focus_out_event (GtkWidget *widget,
+ GdkEvent *event,
+ gpointer user_data)
+{
+ GtkEntryCompletion *completion;
+ GtkTreeModel *model;
+ GtkTreeIter iter;
+ GValue *key;
+ GValue *value;
+
+ gchar *decoded;
+ gchar *decoded_casefold;
+
+ gboolean found;
+
+ GtkWidget *dialog;
+
+ GtkFormDecoder *decoder = (GtkFormDecoder *)user_data;
+ GtkFormDecoderPrivate *priv = GTK_FORM_DECODER_GET_PRIVATE (decoder);
+
+ found = FALSE;
+
+ /* check if inserted value exists in GtkTreeModel */
+ completion = gtk_entry_get_completion (GTK_ENTRY (priv->txt_decoded));
+ if (completion != NULL)
+ {
+ model = gtk_entry_completion_get_model (GTK_ENTRY_COMPLETION (completion));
+ if (model != NULL && gtk_tree_model_get_iter_first (model, &iter))
+ {
+ decoded = (gchar *)gtk_entry_get_text (GTK_ENTRY (priv->txt_decoded));
+ if (decoded == NULL)
+ {
+ gtk_form_decoder_set_key (decoder, "0");
+ return FALSE;
+ }
+ decoded = g_strdup (decoded);
+ g_strstrip (decoded);
+ if (g_strcmp0 (decoded, "") == 0)
+ {
+ gtk_form_decoder_set_key (decoder, "0");
+ return FALSE;
+ }
+
+ decoded_casefold = g_utf8_casefold (decoded, -1);
+ g_free (decoded);
+ do
+ {
+ key = g_new0 (GValue, 1);
+ value = g_new0 (GValue, 1);
+ gtk_tree_model_get_value (model, &iter,
+ 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)
+ {
+ gtk_form_decoder_set_key (decoder, gda_value_stringify (key));
+ found = TRUE;
+ g_value_unset (key);
+ g_value_unset (value);
+ break;
+ }
+ g_value_unset (key);
+ g_value_unset (value);
+ } while (gtk_tree_model_iter_next (model, &iter));
+
+ if (!found)
+ {
+ if (priv->completion_only_valid_values)
+ {
+ gtk_form_decoder_set_key (decoder, "0");
+ /* TODO message dialog */
+ dialog = gtk_message_dialog_new (GTK_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (decoder))),
+ GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
+ GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ _("Only valid values are allowed."));
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
+ }
+ else
+ {
+ gtk_label_set_text (GTK_LABEL (priv->lbl_key), "0");
+ }
+ }
+ }
+ }
+
+ return FALSE;
+}