339 lines
8.8 KiB
C
339 lines
8.8 KiB
C
/*
|
|
* 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>
|
|
#include <vn/vn-grid-model.h>
|
|
#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.
|
|
*/
|
|
|
|
static void vn_completion_model_holder_init (DbModelHolderInterface * iface);
|
|
|
|
G_DEFINE_TYPE_WITH_CODE (VnCompletion, vn_completion, VN_TYPE_FIELD,
|
|
G_IMPLEMENT_INTERFACE (DB_TYPE_MODEL_HOLDER,
|
|
vn_completion_model_holder_init)
|
|
);
|
|
|
|
/**
|
|
* vn_completion_new:
|
|
* @model: a #DbModel with the contents for the new completion.
|
|
* @field: the name of the field to search the values in.
|
|
*
|
|
* Creates a new #VnCompletion that will complete text with the data contained
|
|
* in the field named @field of @model.
|
|
*
|
|
* Return value: a #VnCompletion.
|
|
**/
|
|
VnField * vn_completion_new (DbModel * model, const gchar * field)
|
|
{
|
|
return g_object_new (VN_TYPE_COMPLETION, "model", model, "field", field, NULL);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
static void vn_completion_cb_status_changed (DbModel * model,
|
|
DbModelStatus status, VnCompletion * obj)
|
|
{
|
|
const gchar * icon_name;
|
|
|
|
if (status == DB_MODEL_STATUS_READY)
|
|
{
|
|
GtkTreeModel * tree = GTK_TREE_MODEL (vn_grid_model_new (model));
|
|
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:
|
|
icon_name = "edit-find-symbolic";
|
|
break;
|
|
case DB_MODEL_STATUS_ERROR:
|
|
icon_name = "edit-delete-symbolic";
|
|
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);
|
|
}
|
|
|
|
if (obj->model && strlen (text) == 1
|
|
&& (!obj->last_match || g_strcmp0 (text, obj->last_match)))
|
|
{
|
|
gchar * pattern;
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
set_icon (obj, "edit-find-symbolic");
|
|
|
|
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)
|
|
{
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
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;
|
|
}
|
|
|
|
static void vn_completion_substitute (VnCompletion * obj)
|
|
{
|
|
SqlExpr * field;
|
|
SqlString * stmt;
|
|
SqlOperation * op;
|
|
|
|
if (!(obj->model && obj->field))
|
|
return;
|
|
|
|
g_object_get (obj->model, "stmt", &stmt, NULL);
|
|
|
|
op = sql_operation_new (SQL_OPERATION_TYPE_LIKE);
|
|
|
|
field = sql_field_new (obj->field, NULL, NULL);
|
|
sql_operation_add_expr (op, field);
|
|
|
|
obj->value = sql_value_new ();
|
|
sql_operation_add_expr (op, obj->value);
|
|
|
|
sql_string_add_expr (stmt, field);
|
|
sql_string_add_expr (stmt, SQL_EXPR (op));
|
|
}
|
|
|
|
static void vn_completion_set_field (VnCompletion * obj, gchar * field)
|
|
{
|
|
obj->field = field;
|
|
vn_completion_substitute (obj);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++ DbModelHolder implementation
|
|
|
|
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);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
|
|
|
|
enum
|
|
{
|
|
PROP_MODEL = 1
|
|
,PROP_FIELD
|
|
};
|
|
|
|
static void vn_completion_set_property (VnCompletion * obj, guint property_id,
|
|
const GValue * value, GParamSpec * pspec)
|
|
{
|
|
switch (property_id)
|
|
{
|
|
case PROP_MODEL:
|
|
vn_completion_set_model (obj, g_value_dup_object (value));
|
|
break;
|
|
case PROP_FIELD:
|
|
vn_completion_set_field (obj, g_value_dup_string (value));
|
|
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)
|
|
{
|
|
case PROP_MODEL:
|
|
g_value_set_object (value, obj->model);
|
|
break;
|
|
default:
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, property_id, pspec);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_completion_init (VnCompletion * obj)
|
|
{
|
|
GtkEntryCompletion * completion;
|
|
|
|
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;
|
|
|
|
g_object_class_override_property (k, PROP_MODEL, "model");
|
|
|
|
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
|
|
));
|
|
}
|
|
|
|
static void vn_completion_model_holder_init (DbModelHolderInterface * iface)
|
|
{
|
|
iface->get_model = (DbModelHolderGetModelFunc) vn_completion_get_model;
|
|
iface->set_model = (DbModelHolderSetModelFunc) vn_completion_set_model;
|
|
} |