/*
* 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-entry.h"
#include
/**
* SECTION:vn-entry
* @Short_description: a text box widget
* @Title: VnEntry
* @See_also: #VnField
* @Image: entry.png
*
* A text box representing a generic value field.
*/
G_DEFINE_TYPE (VnEntry, vn_entry, VN_TYPE_FIELD);
/**
* vn_entry_new:
*
* Creates a new #VnEntry
*
* Return value: a #VnEntry
**/
VnField * vn_entry_new (VnEntry * self)
{
return g_object_new (VN_TYPE_ENTRY, NULL);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
static void vn_entry_on_editing_done (GtkEntry * entry, VnField * self)
{
GValue value = G_VALUE_INIT;
const gchar * text = gtk_entry_get_text (entry);
if (g_strcmp0 (text, "") || !vn_field_get_null (self))
{
g_value_init (&value, G_TYPE_STRING);
g_value_set_string (&value, text);
}
else
g_value_init (&value, GVN_TYPE_NULL);
VN_FIELD_GET_CLASS (self)->value_changed (self, &value);
g_value_unset (&value);
}
static gboolean vn_entry_on_focus_out (GtkEntry * entry, GdkEvent * event, VnField * self)
{
vn_entry_on_editing_done (entry, self);
return FALSE;
}
static void vn_entry_set_value (VnEntry * self, const GValue * value)
{
GValue new_value = G_VALUE_INIT;
gvn_value_to_format_string (value, self->format, &new_value);
g_signal_handlers_block_by_func (self->entry,
vn_entry_on_editing_done, self);
g_signal_handlers_block_by_func (self->entry,
vn_entry_on_focus_out, self);
gtk_entry_set_text (self->entry,
g_value_get_string (&new_value));
g_signal_handlers_unblock_by_func (self->entry,
vn_entry_on_editing_done, self);
g_signal_handlers_unblock_by_func (self->entry,
vn_entry_on_focus_out, self);
g_value_unset (&new_value);
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties
enum
{
PROP_FORMAT = 1
};
static void vn_entry_set_property (VnEntry * self, guint id,
const GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_FORMAT:
g_free (self->format);
self->format = g_value_dup_string (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
static void vn_entry_get_property (VnEntry * self, guint id,
GValue * value, GParamSpec * pspec)
{
switch (id)
{
case PROP_FORMAT:
g_value_set_string (value, self->format);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (self, id, pspec);
}
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
static void vn_entry_init (VnEntry * self)
{
self->format = NULL;
self->entry = GTK_ENTRY (gtk_entry_new ());
g_object_connect (self->entry
,"signal::activate", vn_entry_on_editing_done, self
,"signal::focus-out-event", vn_entry_on_focus_out, self
,NULL
);
gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->entry));
VN_FIELD_GET_CLASS (self)->set_widget (VN_FIELD (self),
GTK_WIDGET (self->entry));
}
static void vn_entry_finalize (VnEntry * self)
{
g_free (self->format);
G_OBJECT_CLASS (vn_entry_parent_class)->finalize (G_OBJECT (self));
}
static void vn_entry_class_init (VnEntryClass * klass)
{
GObjectClass * k = G_OBJECT_CLASS (klass);
k->set_property = (GObjectSetPropertyFunc) vn_entry_set_property;
k->get_property = (GObjectGetPropertyFunc) vn_entry_get_property;
k->finalize = (GObjectFinalizeFunc) vn_entry_finalize;
VN_FIELD_CLASS (klass)->set_value = (VnFieldSetValueFunc) vn_entry_set_value;
g_object_class_install_property (k, PROP_FORMAT,
g_param_spec_string ("format"
,_("Format")
,_("The format string describing the output of the entry.")
,NULL
,G_PARAM_CONSTRUCT | G_PARAM_READWRITE
));
}