/* * 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 * obj) { return g_object_new (VN_TYPE_ENTRY, NULL); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Private static void vn_entry_cb_editing_done (GtkEntry * entry, VnField * obj) { GValue value = {0}; const gchar * text = gtk_entry_get_text (entry); if (g_strcmp0 (text, "") || !vn_field_get_null (obj)) { 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 (obj)->value_changed (obj, &value); g_value_unset (&value); } static gboolean vn_entry_cb_focus_out (GtkEntry * entry, GdkEvent * event, VnField * obj) { vn_entry_cb_editing_done (entry, obj); return FALSE; } static void vn_entry_set_value (VnEntry * obj, const GValue * value) { GValue new_value = {0}; gvn_value_to_format_string (value, obj->digits, &new_value); g_signal_handlers_block_by_func (obj->entry, vn_entry_cb_editing_done, obj); g_signal_handlers_block_by_func (obj->entry, vn_entry_cb_focus_out, obj); gtk_entry_set_text (obj->entry, g_value_get_string (&new_value)); g_signal_handlers_unblock_by_func (obj->entry, vn_entry_cb_editing_done, obj); g_signal_handlers_unblock_by_func (obj->entry, vn_entry_cb_focus_out, obj); g_value_unset (&new_value); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties enum { PROP_DIGITS = 1 }; static void vn_entry_set_property (VnEntry * obj, guint id, const GValue * value, GParamSpec * pspec) { switch (id) { case PROP_DIGITS: obj->digits = g_value_get_uint (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec); } } static void vn_entry_get_property (VnEntry * obj, guint id, GValue * value, GParamSpec * pspec) { switch (id) { case PROP_DIGITS: g_value_set_uint (value, obj->digits); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec); } } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Class static void vn_entry_init (VnEntry * obj) { obj->entry = GTK_ENTRY (gtk_entry_new ()); g_object_connect (obj->entry ,"signal::activate", vn_entry_cb_editing_done, obj ,"signal::focus-out-event", vn_entry_cb_focus_out, obj ,NULL ); gtk_container_add (GTK_CONTAINER (obj), GTK_WIDGET (obj->entry)); VN_FIELD (obj)->field = GTK_WIDGET (obj->entry); } static void vn_entry_finalize (VnEntry * obj) { G_OBJECT_CLASS (vn_entry_parent_class)->finalize (G_OBJECT (obj)); } 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_DIGITS, g_param_spec_uint ("digits" ,_("Digits") ,_("The number of decimal places to display.") ,0 ,20 ,0 ,G_PARAM_CONSTRUCT | G_PARAM_READWRITE )); }