2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vn-completion.h"
|
|
|
|
#include <db/db.h>
|
2014-02-11 12:16:39 +00:00
|
|
|
#include <vn/vn-grid-model.h>
|
2013-10-11 23:07:35 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define set_icon(obj,icon_name) (gtk_entry_set_icon_from_icon_name (obj->entry, GTK_ENTRY_ICON_SECONDARY, icon_name))
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:vn-completion
|
|
|
|
* @Short_description: an auto-completable text box
|
|
|
|
* @Title: VnCompletion
|
|
|
|
* @See_also: #VnField
|
|
|
|
* @Image: completion.png
|
|
|
|
*
|
|
|
|
* A text box widget that auto-completes as the user writes using the data
|
|
|
|
* retrieved from a database.
|
2013-10-14 12:08:06 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-02 11:29:46 +00:00
|
|
|
static void vn_completion_model_holder_init (DbModelHolderInterface * iface);
|
2013-10-14 12:08:06 +00:00
|
|
|
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (VnCompletion, vn_completion, VN_TYPE_FIELD,
|
|
|
|
G_IMPLEMENT_INTERFACE (DB_TYPE_MODEL_HOLDER,
|
2014-01-02 11:29:46 +00:00
|
|
|
vn_completion_model_holder_init)
|
2013-10-14 12:08:06 +00:00
|
|
|
);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* vn_completion_new:
|
2013-10-14 12:08:06 +00:00
|
|
|
* @model: a #DbModel with the contents for the new completion.
|
|
|
|
* @field: the name of the field to search the values in.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
2013-10-14 12:08:06 +00:00
|
|
|
* Creates a new #VnCompletion that will complete text with the data contained
|
|
|
|
* in the field named @field of @model.
|
2013-10-11 23:07:35 +00:00
|
|
|
*
|
|
|
|
* Return value: a #VnCompletion.
|
|
|
|
**/
|
2013-10-14 12:08:06 +00:00
|
|
|
VnField * vn_completion_new (DbModel * model, const gchar * field)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
return g_object_new (VN_TYPE_COMPLETION, "model", model, "field", field, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
2014-01-02 11:29:46 +00:00
|
|
|
static void vn_completion_cb_status_changed (DbModel * model,
|
|
|
|
DbModelStatus status, VnCompletion * obj)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
const gchar * icon_name;
|
|
|
|
|
|
|
|
if (status == DB_MODEL_STATUS_READY)
|
|
|
|
{
|
2014-02-11 12:16:39 +00:00
|
|
|
GtkTreeModel * tree = GTK_TREE_MODEL (vn_grid_model_new (model));
|
2013-10-11 23:07:35 +00:00
|
|
|
gtk_entry_completion_set_model (obj->completion, tree);
|
|
|
|
g_signal_emit_by_name (obj->entry, "changed");
|
|
|
|
g_object_unref (tree);
|
|
|
|
icon_name = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (status)
|
|
|
|
{
|
|
|
|
case DB_MODEL_STATUS_LOADING:
|
2014-01-02 11:29:46 +00:00
|
|
|
icon_name = "edit-find-symbolic";
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case DB_MODEL_STATUS_ERROR:
|
2014-01-02 11:29:46 +00:00
|
|
|
icon_name = "edit-delete-symbolic";
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
icon_name = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_entry_completion_set_model (obj->completion, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_icon (obj, icon_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_cb_changed (GtkEditable * entry, VnCompletion * obj)
|
|
|
|
{
|
|
|
|
const gchar * text = gtk_entry_get_text (GTK_ENTRY (entry));
|
|
|
|
|
|
|
|
if (obj->invalid)
|
|
|
|
{
|
|
|
|
obj->invalid = FALSE;
|
|
|
|
set_icon (obj, NULL);
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
if (obj->model && strlen (text) == 1
|
2013-10-11 23:07:35 +00:00
|
|
|
&& (!obj->last_match || g_strcmp0 (text, obj->last_match)))
|
|
|
|
{
|
|
|
|
gchar * pattern;
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue value = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-01-02 11:29:46 +00:00
|
|
|
set_icon (obj, "edit-find-symbolic");
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_free (obj->last_match);
|
|
|
|
obj->last_match = g_strdup (text);
|
|
|
|
pattern = g_strconcat (text, "%", NULL);
|
|
|
|
|
|
|
|
g_value_init (&value, G_TYPE_STRING);
|
|
|
|
g_value_set_string (&value, pattern);
|
|
|
|
sql_value_set_value (SQL_VALUE (obj->value), &value);
|
|
|
|
g_value_unset (&value);
|
|
|
|
|
|
|
|
g_free (pattern);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_iter_changed (VnCompletion * obj, DbIter * iter)
|
|
|
|
{
|
|
|
|
const GValue * value = db_model_get_value (obj->model, iter, 0, NULL);
|
|
|
|
|
|
|
|
if (value)
|
|
|
|
VN_FIELD_GET_CLASS (obj)->value_changed (VN_FIELD (obj), value);
|
|
|
|
|
|
|
|
set_icon (obj, "gtk-apply");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_cb_activate (GtkEntry * entry, VnCompletion * obj)
|
|
|
|
{
|
|
|
|
gboolean ok = FALSE;
|
|
|
|
const gchar * text = gtk_entry_get_text (entry);
|
|
|
|
|
|
|
|
if (text && g_strcmp0 (text, ""))
|
|
|
|
{
|
|
|
|
DbModel * model = obj->model;
|
|
|
|
|
|
|
|
if (db_model_get_column_type (model, obj->column) == G_TYPE_STRING)
|
|
|
|
{
|
|
|
|
DbIter iter;
|
|
|
|
const GValue * v;
|
|
|
|
|
|
|
|
if (db_model_get_iter_first (model, &iter))
|
|
|
|
{
|
|
|
|
do {
|
|
|
|
v = db_model_get_value (model, &iter, obj->column, NULL);
|
|
|
|
|
|
|
|
if (!gvn_value_is_null (v)
|
|
|
|
&& !g_ascii_strcasecmp (g_value_get_string (v), text))
|
|
|
|
{
|
|
|
|
vn_completion_iter_changed (obj, &iter);
|
|
|
|
ok = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (!ok && db_model_iter_next (model, &iter));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
{
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue value = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
g_value_init (&value, GVN_TYPE_NULL);
|
|
|
|
VN_FIELD_GET_CLASS (obj)->value_changed (VN_FIELD (obj), &value);
|
|
|
|
g_value_unset (&value);
|
|
|
|
|
|
|
|
if (g_strcmp0 (text, ""))
|
|
|
|
{
|
|
|
|
obj->invalid = TRUE;
|
|
|
|
set_icon (obj, "edit-delete");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
set_icon (obj, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean vn_completion_match_selected (GtkEntryCompletion * completion,
|
|
|
|
GtkTreeModel * model, GtkTreeIter * tree_iter, VnCompletion * obj)
|
|
|
|
{
|
|
|
|
DbIter iter;
|
|
|
|
|
|
|
|
vn_gtk_tree_iter_to_db_iter (tree_iter, &iter);
|
|
|
|
vn_completion_iter_changed (obj, &iter);
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
static void vn_completion_substitute (VnCompletion * obj)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-05-09 11:40:32 +00:00
|
|
|
SqlObject * field;
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlObject * like;
|
2014-05-09 11:40:32 +00:00
|
|
|
SqlBatch * batch;
|
2014-05-27 07:55:01 +00:00
|
|
|
SqlList * operands;
|
2013-10-14 12:08:06 +00:00
|
|
|
|
|
|
|
if (!(obj->model && obj->field))
|
2013-10-11 23:07:35 +00:00
|
|
|
return;
|
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
like = sql_operation_new (SQL_OPERATION_TYPE_LIKE);
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
operands = sql_list_new (SQL_TYPE_EXPR);
|
|
|
|
g_object_set (like, "operands", operands, NULL);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-05-27 07:55:01 +00:00
|
|
|
field = sql_field_new (obj->field);
|
|
|
|
sql_list_add (operands, field);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
obj->value = sql_value_new ();
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_list_add (operands, obj->value);
|
2014-05-09 11:40:32 +00:00
|
|
|
|
|
|
|
batch = sql_batch_new ();
|
|
|
|
sql_batch_add (batch, "field", field);
|
2014-05-27 07:55:01 +00:00
|
|
|
sql_batch_add (batch, "filter", like);
|
2014-05-09 11:40:32 +00:00
|
|
|
db_model_set_batch (obj->model, batch);
|
2013-10-14 12:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_set_field (VnCompletion * obj, gchar * field)
|
|
|
|
{
|
|
|
|
obj->field = field;
|
|
|
|
vn_completion_substitute (obj);
|
|
|
|
}
|
|
|
|
|
2014-01-02 11:29:46 +00:00
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++ DbModelHolder implementation
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
static DbModel * vn_completion_get_model (VnCompletion * obj)
|
|
|
|
{
|
|
|
|
return obj->model;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_set_model (VnCompletion * obj, DbModel * model)
|
|
|
|
{
|
|
|
|
obj->model = model;
|
|
|
|
g_signal_connect (obj->model, "status-changed",
|
|
|
|
G_CALLBACK (vn_completion_cb_status_changed), obj);
|
|
|
|
|
|
|
|
vn_completion_substitute (obj);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
PROP_MODEL = 1
|
2013-10-11 23:07:35 +00:00
|
|
|
,PROP_FIELD
|
|
|
|
};
|
|
|
|
|
|
|
|
static void vn_completion_set_property (VnCompletion * obj, guint property_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
case PROP_MODEL:
|
|
|
|
vn_completion_set_model (obj, g_value_dup_object (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
case PROP_FIELD:
|
2013-10-14 12:08:06 +00:00
|
|
|
vn_completion_set_field (obj, g_value_dup_string (value));
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_get_property (VnCompletion * obj, guint property_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
switch (property_id)
|
|
|
|
{
|
2013-10-14 12:08:06 +00:00
|
|
|
case PROP_MODEL:
|
|
|
|
g_value_set_object (value, obj->model);
|
2013-10-11 23:07:35 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
|
|
|
static void vn_completion_init (VnCompletion * obj)
|
|
|
|
{
|
|
|
|
GtkEntryCompletion * completion;
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
obj->field = NULL;
|
|
|
|
obj->last_match = NULL;
|
|
|
|
obj->invalid = FALSE;
|
|
|
|
obj->column = 1;
|
|
|
|
|
|
|
|
obj->entry = GTK_ENTRY (gtk_entry_new ());
|
|
|
|
gtk_entry_set_icon_sensitive (obj->entry, GTK_ENTRY_ICON_SECONDARY, FALSE);
|
|
|
|
g_object_connect (obj->entry,
|
|
|
|
"signal::changed", vn_completion_cb_changed, obj
|
|
|
|
,"signal::activate", vn_completion_cb_activate, obj
|
|
|
|
,NULL
|
|
|
|
);
|
|
|
|
gtk_container_add (GTK_CONTAINER (obj), GTK_WIDGET (obj->entry));
|
|
|
|
VN_FIELD (obj)->field = GTK_WIDGET (obj->entry);
|
|
|
|
|
|
|
|
completion = gtk_entry_completion_new ();
|
|
|
|
gtk_entry_completion_set_minimum_key_length (completion, 1);
|
|
|
|
gtk_entry_completion_set_text_column (completion, obj->column);
|
|
|
|
gtk_entry_completion_set_inline_selection (completion, FALSE);
|
|
|
|
// gtk_entry_completion_set_inline_completion (completion, TRUE);
|
|
|
|
g_signal_connect (completion, "match-selected",
|
|
|
|
G_CALLBACK (vn_completion_match_selected), obj);
|
|
|
|
gtk_entry_set_completion (obj->entry, completion);
|
|
|
|
obj->completion = completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_finalize (VnCompletion * obj)
|
|
|
|
{
|
|
|
|
g_free (obj->field);
|
|
|
|
g_free (obj->last_match);
|
|
|
|
g_object_unref (obj->completion);
|
|
|
|
g_object_unref (obj->model);
|
|
|
|
G_OBJECT_CLASS (vn_completion_parent_class)->finalize (G_OBJECT (obj));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_completion_class_init (VnCompletionClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass * k = G_OBJECT_CLASS (klass);
|
|
|
|
k->finalize = (GObjectFinalizeFunc) vn_completion_finalize;
|
|
|
|
k->set_property = (GObjectSetPropertyFunc) vn_completion_set_property;
|
|
|
|
k->get_property = (GObjectGetPropertyFunc) vn_completion_get_property;
|
|
|
|
|
2013-10-14 12:08:06 +00:00
|
|
|
g_object_class_override_property (k, PROP_MODEL, "model");
|
|
|
|
|
2013-10-11 23:07:35 +00:00
|
|
|
g_object_class_install_property (k, PROP_FIELD,
|
|
|
|
g_param_spec_string ("field"
|
|
|
|
,_("Field")
|
|
|
|
,_("The name of the field used for the search")
|
|
|
|
,NULL
|
|
|
|
,G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY
|
|
|
|
));
|
|
|
|
}
|
2013-10-14 12:08:06 +00:00
|
|
|
|
2014-01-02 11:29:46 +00:00
|
|
|
static void vn_completion_model_holder_init (DbModelHolderInterface * iface)
|
2013-10-14 12:08:06 +00:00
|
|
|
{
|
|
|
|
iface->get_model = (DbModelHolderGetModelFunc) vn_completion_get_model;
|
|
|
|
iface->set_model = (DbModelHolderSetModelFunc) vn_completion_set_model;
|
2014-06-20 13:46:39 +00:00
|
|
|
}
|