/* * 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 . */ #include "vn-completion.h" #include #include #include #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. */ G_DEFINE_TYPE (VnCompletion, vn_completion, VN_TYPE_FIELD); /** * vn_completion_new: * @sql: the SQL string used by completion. * @field: the field where to search the values. * * Creates a new #VnCompletion. * * Return value: a #VnCompletion. **/ VnField * vn_completion_new (const gchar * sql, const gchar * field) { return g_object_new (VN_TYPE_COMPLETION, "sql", sql, "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_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"; break; case DB_MODEL_STATUS_ERROR: icon_name = "edit-delete"; 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->sql && strlen (text) == 1 && (!obj->last_match || g_strcmp0 (text, obj->last_match))) { gchar * pattern; GValue value = {0}; set_icon (obj, "edit-find"); 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 = {0}; 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_create_model (VnCompletion * obj) { SqlExpr * field; SqlString * stmt; SqlOperation * op; if (!(obj->sql && obj->field)) return; 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); stmt = sql_string_new (obj->sql); sql_string_add_expr (stmt, field); sql_string_add_expr (stmt, SQL_EXPR (op)); db_model_set_stmt (obj->model, SQL_STMT (stmt)); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties enum { PROP_CONN = 1 ,PROP_SQL ,PROP_FIELD }; static void vn_completion_set_property (VnCompletion * obj, guint property_id, const GValue * value, GParamSpec * pspec) { switch (property_id) { case PROP_CONN: db_model_set_conn (obj->model, g_value_get_object (value)); break; case PROP_SQL: obj->sql = g_value_dup_string (value); vn_completion_create_model (obj); break; case PROP_FIELD: obj->field = g_value_dup_string (value); vn_completion_create_model (obj); 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_CONN: g_value_set_object (value, db_model_get_conn (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->sql = NULL; 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; obj->model = db_model_new (NULL, NULL); g_signal_connect (obj->model, "status-changed", G_CALLBACK (vn_completion_cb_status_changed), obj); } static void vn_completion_finalize (VnCompletion * obj) { g_free (obj->sql); 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_install_property (k, PROP_CONN, g_param_spec_object ("conn" ,_("Connection") ,_("The connection used by the model") ,DB_TYPE_CONN ,G_PARAM_READWRITE )); g_object_class_install_property (k, PROP_SQL, g_param_spec_string ("sql" ,_("SQL") ,_("The SQL query used to create the model") ,NULL ,G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY )); 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 )); }