xed/pluma/pluma-history-entry.c

618 lines
14 KiB
C
Raw Normal View History

2011-11-07 13:46:58 -06:00
/*
2011-11-07 16:52:18 -06:00
* pluma-history-entry.c
* This file is part of pluma
2011-11-07 13:46:58 -06:00
*
* Copyright (C) 2006 - Paolo Borelli
*
* 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 2 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, write to the Free Software
2012-11-18 19:54:49 -06:00
* Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
2011-11-07 13:46:58 -06:00
*/
/*
2011-11-07 16:52:18 -06:00
* Modified by the pluma Team, 2006. See the AUTHORS file for a
* list of people on the pluma Team.
2011-11-07 13:46:58 -06:00
* See the ChangeLog files for a list of changes.
*
* $Id$
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
2013-01-24 14:03:53 -06:00
#include <gio/gio.h>
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
#include "pluma-history-entry.h"
2013-01-24 14:03:53 -06:00
#include "pluma-prefs-manager.h"
2011-11-07 13:46:58 -06:00
enum {
PROP_0,
PROP_HISTORY_ID,
PROP_HISTORY_LENGTH
};
#define MIN_ITEM_LEN 3
2011-11-07 16:52:18 -06:00
#define PLUMA_HISTORY_ENTRY_HISTORY_LENGTH_DEFAULT 10
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
#define PLUMA_HISTORY_ENTRY_GET_PRIVATE(object)(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
PLUMA_TYPE_HISTORY_ENTRY, \
PlumaHistoryEntryPrivate))
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
struct _PlumaHistoryEntryPrivate
2011-11-07 13:46:58 -06:00
{
gchar *history_id;
guint history_length;
GtkEntryCompletion *completion;
2013-01-24 14:03:53 -06:00
GSettings *settings;
2011-11-07 13:46:58 -06:00
};
2013-10-29 18:56:56 -05:00
#if GTK_CHECK_VERSION (3, 0, 0)
G_DEFINE_TYPE(PlumaHistoryEntry, pluma_history_entry, GTK_TYPE_COMBO_BOX_TEXT)
#else
2011-11-07 16:52:18 -06:00
G_DEFINE_TYPE(PlumaHistoryEntry, pluma_history_entry, GTK_TYPE_COMBO_BOX_ENTRY)
2013-10-29 18:56:56 -05:00
#endif
2011-11-07 13:46:58 -06:00
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
const GValue *value,
GParamSpec *spec)
{
2011-11-07 16:52:18 -06:00
PlumaHistoryEntry *entry;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (object));
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
entry = PLUMA_HISTORY_ENTRY (object);
2011-11-07 13:46:58 -06:00
switch (prop_id) {
case PROP_HISTORY_ID:
entry->priv->history_id = g_value_dup_string (value);
break;
case PROP_HISTORY_LENGTH:
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_history_length (entry,
2011-11-07 13:46:58 -06:00
g_value_get_uint (value));
break;
default:
break;
}
}
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_get_property (GObject *object,
2011-11-07 13:46:58 -06:00
guint prop_id,
GValue *value,
GParamSpec *spec)
{
2011-11-07 16:52:18 -06:00
PlumaHistoryEntryPrivate *priv;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (object));
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
priv = PLUMA_HISTORY_ENTRY (object)->priv;
2011-11-07 13:46:58 -06:00
switch (prop_id) {
case PROP_HISTORY_ID:
g_value_set_string (value, priv->history_id);
break;
case PROP_HISTORY_LENGTH:
g_value_set_uint (value, priv->history_length);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, spec);
}
}
static void
2013-10-29 18:56:56 -05:00
#if GTK_CHECK_VERSION (3, 0, 0)
pluma_history_entry_dispose (GObject *object)
#else
2011-11-07 16:52:18 -06:00
pluma_history_entry_destroy (GtkObject *object)
2013-10-29 18:56:56 -05:00
#endif
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_enable_completion (PLUMA_HISTORY_ENTRY (object),
2011-11-07 13:46:58 -06:00
FALSE);
2013-10-29 18:56:56 -05:00
#if GTK_CHECK_VERSION (3, 0, 0)
G_OBJECT_CLASS (pluma_history_entry_parent_class)->dispose (object);
#else
2011-11-07 16:52:18 -06:00
GTK_OBJECT_CLASS (pluma_history_entry_parent_class)->destroy (object);
2013-10-29 18:56:56 -05:00
#endif
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_finalize (GObject *object)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaHistoryEntryPrivate *priv;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
priv = PLUMA_HISTORY_ENTRY (object)->priv;
2011-11-07 13:46:58 -06:00
g_free (priv->history_id);
2013-01-24 14:03:53 -06:00
if (priv->settings != NULL)
2011-11-07 13:46:58 -06:00
{
2013-01-24 14:03:53 -06:00
g_object_unref (G_OBJECT (priv->settings));
priv->settings = NULL;
2011-11-07 13:46:58 -06:00
}
2011-11-07 16:52:18 -06:00
G_OBJECT_CLASS (pluma_history_entry_parent_class)->finalize (object);
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_class_init (PlumaHistoryEntryClass *klass)
2011-11-07 13:46:58 -06:00
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
2013-10-29 18:56:56 -05:00
#if !GTK_CHECK_VERSION (3, 0, 0)
2011-11-07 13:46:58 -06:00
GtkObjectClass *gtkobject_class = GTK_OBJECT_CLASS (klass);
2013-10-29 18:56:56 -05:00
#endif
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
object_class->set_property = pluma_history_entry_set_property;
object_class->get_property = pluma_history_entry_get_property;
object_class->finalize = pluma_history_entry_finalize;
2013-10-29 18:56:56 -05:00
#if GTK_CHECK_VERSION (3, 0, 0)
object_class->dispose = pluma_history_entry_dispose;
#else
2011-11-07 16:52:18 -06:00
gtkobject_class->destroy = pluma_history_entry_destroy;
2013-10-29 18:56:56 -05:00
#endif
2011-11-07 13:46:58 -06:00
g_object_class_install_property (object_class,
PROP_HISTORY_ID,
g_param_spec_string ("history-id",
"History ID",
"History ID",
NULL,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
g_object_class_install_property (object_class,
PROP_HISTORY_LENGTH,
g_param_spec_uint ("history-length",
"Max History Length",
"Max History Length",
0,
G_MAXUINT,
2011-11-07 16:52:18 -06:00
PLUMA_HISTORY_ENTRY_HISTORY_LENGTH_DEFAULT,
2011-11-07 13:46:58 -06:00
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
/* TODO: Add enable-completion property */
2011-11-07 16:52:18 -06:00
g_type_class_add_private (object_class, sizeof(PlumaHistoryEntryPrivate));
2011-11-07 13:46:58 -06:00
}
static GtkListStore *
2011-11-07 16:52:18 -06:00
get_history_store (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
GtkTreeModel *store;
store = gtk_combo_box_get_model (GTK_COMBO_BOX (entry));
g_return_val_if_fail (GTK_IS_LIST_STORE (store), NULL);
return (GtkListStore *) store;
}
static GSList *
2011-11-07 16:52:18 -06:00
get_history_list (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
GtkListStore *store;
GtkTreeIter iter;
gboolean valid;
GSList *list = NULL;
store = get_history_store (entry);
valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store),
&iter);
while (valid)
{
gchar *str;
gtk_tree_model_get (GTK_TREE_MODEL (store),
&iter,
0, &str,
-1);
list = g_slist_prepend (list, str);
valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (store),
&iter);
}
return g_slist_reverse (list);
}
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_save_history (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2013-01-24 14:03:53 -06:00
GSList *settings_items;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
2013-01-24 14:03:53 -06:00
settings_items = get_history_list (entry);
2011-11-07 13:46:58 -06:00
2013-01-24 14:03:53 -06:00
pluma_prefs_manager_set_gslist (entry->priv->settings,
entry->priv->history_id,
settings_items);
2011-11-07 13:46:58 -06:00
2013-01-24 14:03:53 -06:00
g_slist_foreach (settings_items, (GFunc) g_free, NULL);
g_slist_free (settings_items);
2011-11-07 13:46:58 -06:00
}
static gboolean
remove_item (GtkListStore *store,
const gchar *text)
{
GtkTreeIter iter;
g_return_val_if_fail (text != NULL, FALSE);
if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter))
return FALSE;
do
{
gchar *item_text;
gtk_tree_model_get (GTK_TREE_MODEL (store),
&iter,
0,
&item_text,
-1);
if (item_text != NULL &&
strcmp (item_text, text) == 0)
{
gtk_list_store_remove (store, &iter);
g_free (item_text);
return TRUE;
}
g_free (item_text);
} while (gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter));
return FALSE;
}
static void
clamp_list_store (GtkListStore *store,
guint max)
{
GtkTreePath *path;
GtkTreeIter iter;
/* -1 because TreePath counts from 0 */
path = gtk_tree_path_new_from_indices (max - 1, -1);
if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path))
{
while (1)
{
if (!gtk_list_store_remove (store, &iter))
break;
}
}
gtk_tree_path_free (path);
}
static void
2011-11-07 16:52:18 -06:00
insert_history_item (PlumaHistoryEntry *entry,
2011-11-07 13:46:58 -06:00
const gchar *text,
gboolean prepend)
{
GtkListStore *store;
GtkTreeIter iter;
if (g_utf8_strlen (text, -1) <= MIN_ITEM_LEN)
return;
store = get_history_store (entry);
/* remove the text from the store if it was already
* present. If it wasn't, clamp to max history - 1
* before inserting the new row, otherwise appending
* would not work */
if (!remove_item (store, text))
clamp_list_store (store,
entry->priv->history_length - 1);
if (prepend)
gtk_list_store_insert (store, &iter, 0);
else
gtk_list_store_append (store, &iter);
gtk_list_store_set (store,
&iter,
0,
text,
-1);
2011-11-07 16:52:18 -06:00
pluma_history_entry_save_history (entry);
2011-11-07 13:46:58 -06:00
}
void
2011-11-07 16:52:18 -06:00
pluma_history_entry_prepend_text (PlumaHistoryEntry *entry,
2011-11-07 13:46:58 -06:00
const gchar *text)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
g_return_if_fail (text != NULL);
insert_history_item (entry, text, TRUE);
}
void
2011-11-07 16:52:18 -06:00
pluma_history_entry_append_text (PlumaHistoryEntry *entry,
2011-11-07 13:46:58 -06:00
const gchar *text)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
g_return_if_fail (text != NULL);
insert_history_item (entry, text, FALSE);
}
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_load_history (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2013-01-24 14:03:53 -06:00
GSList *settings_items, *l;
2011-11-07 13:46:58 -06:00
GtkListStore *store;
GtkTreeIter iter;
guint i;
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
store = get_history_store (entry);
2013-01-24 14:03:53 -06:00
settings_items = pluma_prefs_manager_get_gslist (entry->priv->settings,
entry->priv->history_id);
2011-11-07 13:46:58 -06:00
gtk_list_store_clear (store);
2013-01-24 14:03:53 -06:00
for (l = settings_items, i = 0;
2011-11-07 13:46:58 -06:00
l != NULL && i < entry->priv->history_length;
l = l->next, i++)
{
gtk_list_store_append (store, &iter);
gtk_list_store_set (store,
&iter,
0,
l->data,
-1);
}
2013-01-24 14:03:53 -06:00
g_slist_foreach (settings_items, (GFunc) g_free, NULL);
g_slist_free (settings_items);
2011-11-07 13:46:58 -06:00
}
void
2011-11-07 16:52:18 -06:00
pluma_history_entry_clear (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
GtkListStore *store;
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
store = get_history_store (entry);
gtk_list_store_clear (store);
2011-11-07 16:52:18 -06:00
pluma_history_entry_save_history (entry);
2011-11-07 13:46:58 -06:00
}
static void
2011-11-07 16:52:18 -06:00
pluma_history_entry_init (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
PlumaHistoryEntryPrivate *priv;
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
priv = PLUMA_HISTORY_ENTRY_GET_PRIVATE (entry);
2011-11-07 13:46:58 -06:00
entry->priv = priv;
priv->history_id = NULL;
2011-11-07 16:52:18 -06:00
priv->history_length = PLUMA_HISTORY_ENTRY_HISTORY_LENGTH_DEFAULT;
2011-11-07 13:46:58 -06:00
priv->completion = NULL;
2013-01-24 14:03:53 -06:00
priv->settings = g_settings_new (PLUMA_SCHEMA);
2011-11-07 13:46:58 -06:00
}
void
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_history_length (PlumaHistoryEntry *entry,
2011-11-07 13:46:58 -06:00
guint history_length)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
g_return_if_fail (history_length > 0);
entry->priv->history_length = history_length;
/* TODO: update if we currently have more items than max */
}
guint
2011-11-07 16:52:18 -06:00
pluma_history_entry_get_history_length (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_HISTORY_ENTRY (entry), 0);
2011-11-07 13:46:58 -06:00
return entry->priv->history_length;
}
gchar *
2011-11-07 16:52:18 -06:00
pluma_history_entry_get_history_id (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_HISTORY_ENTRY (entry), NULL);
2011-11-07 13:46:58 -06:00
return g_strdup (entry->priv->history_id);
}
void
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_enable_completion (PlumaHistoryEntry *entry,
2011-11-07 13:46:58 -06:00
gboolean enable)
{
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
if (enable)
{
if (entry->priv->completion != NULL)
return;
entry->priv->completion = gtk_entry_completion_new ();
gtk_entry_completion_set_model (entry->priv->completion,
GTK_TREE_MODEL (get_history_store (entry)));
/* Use model column 0 as the text column */
gtk_entry_completion_set_text_column (entry->priv->completion, 0);
gtk_entry_completion_set_minimum_key_length (entry->priv->completion,
MIN_ITEM_LEN);
gtk_entry_completion_set_popup_completion (entry->priv->completion, FALSE);
gtk_entry_completion_set_inline_completion (entry->priv->completion, TRUE);
/* Assign the completion to the entry */
2011-11-07 16:52:18 -06:00
gtk_entry_set_completion (GTK_ENTRY (pluma_history_entry_get_entry(entry)),
2011-11-07 13:46:58 -06:00
entry->priv->completion);
}
else
{
if (entry->priv->completion == NULL)
return;
2011-11-07 16:52:18 -06:00
gtk_entry_set_completion (GTK_ENTRY (pluma_history_entry_get_entry (entry)),
2011-11-07 13:46:58 -06:00
NULL);
g_object_unref (entry->priv->completion);
entry->priv->completion = NULL;
}
}
gboolean
2011-11-07 16:52:18 -06:00
pluma_history_entry_get_enable_completion (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_HISTORY_ENTRY (entry), FALSE);
2011-11-07 13:46:58 -06:00
return entry->priv->completion != NULL;
}
GtkWidget *
2011-11-07 16:52:18 -06:00
pluma_history_entry_new (const gchar *history_id,
2011-11-07 13:46:58 -06:00
gboolean enable_completion)
{
GtkWidget *ret;
GtkListStore *store;
g_return_val_if_fail (history_id != NULL, NULL);
/* Note that we are setting the model, so
* user must be careful to always manipulate
2011-11-07 16:52:18 -06:00
* data in the history through pluma_history_entry_
2011-11-07 13:46:58 -06:00
* functions.
*/
store = gtk_list_store_new (1, G_TYPE_STRING);
2011-11-07 16:52:18 -06:00
ret = g_object_new (PLUMA_TYPE_HISTORY_ENTRY,
2011-11-07 13:46:58 -06:00
"history-id", history_id,
"model", store,
"text-column", 0,
NULL);
g_object_unref (store);
/* loading has to happen after the model
* has been set. However the model is not a
* G_PARAM_CONSTRUCT property of GtkComboBox
* so we cannot do this in the constructor.
* For now we simply do here since this widget is
* not bound to other programming languages.
* A maybe better alternative is to override the
* model property of combobox and mark CONTRUCT_ONLY.
* This would also ensure that the model cannot be
* set explicitely at a later time.
*/
2011-11-07 16:52:18 -06:00
pluma_history_entry_load_history (PLUMA_HISTORY_ENTRY (ret));
2011-11-07 13:46:58 -06:00
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_enable_completion (PLUMA_HISTORY_ENTRY (ret),
2011-11-07 13:46:58 -06:00
enable_completion);
return ret;
}
/*
* Utility function to get the editable text entry internal widget.
* I would prefer to not expose this implementation detail and
2011-11-07 16:52:18 -06:00
* simply make the PlumaHistoryEntry widget implement the
2011-11-07 13:46:58 -06:00
* GtkEditable interface. Unfortunately both GtkEditable and
* GtkComboBox have a "changed" signal and I am not sure how to
* handle the conflict.
*/
GtkWidget *
2011-11-07 16:52:18 -06:00
pluma_history_entry_get_entry (PlumaHistoryEntry *entry)
2011-11-07 13:46:58 -06:00
{
2011-11-07 16:52:18 -06:00
g_return_val_if_fail (PLUMA_IS_HISTORY_ENTRY (entry), NULL);
2011-11-07 13:46:58 -06:00
return gtk_bin_get_child (GTK_BIN (entry));
}
static void
escape_cell_data_func (GtkTreeViewColumn *col,
GtkCellRenderer *renderer,
GtkTreeModel *model,
GtkTreeIter *iter,
2011-11-07 16:52:18 -06:00
PlumaHistoryEntryEscapeFunc escape_func)
2011-11-07 13:46:58 -06:00
{
gchar *str;
gchar *escaped;
gtk_tree_model_get (model, iter, 0, &str, -1);
escaped = escape_func (str);
g_object_set (renderer, "text", escaped, NULL);
g_free (str);
g_free (escaped);
}
void
2011-11-07 16:52:18 -06:00
pluma_history_entry_set_escape_func (PlumaHistoryEntry *entry,
PlumaHistoryEntryEscapeFunc escape_func)
2011-11-07 13:46:58 -06:00
{
GList *cells;
2011-11-07 16:52:18 -06:00
g_return_if_fail (PLUMA_IS_HISTORY_ENTRY (entry));
2011-11-07 13:46:58 -06:00
cells = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (entry));
/* We only have one cell renderer */
g_return_if_fail (cells->data != NULL && cells->next == NULL);
if (escape_func != NULL)
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (entry),
GTK_CELL_RENDERER (cells->data),
(GtkCellLayoutDataFunc) escape_cell_data_func,
escape_func,
NULL);
else
gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (entry),
GTK_CELL_RENDERER (cells->data),
NULL,
NULL,
NULL);
g_list_free (cells);
}