/* * 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-check.h" #include /** * SECTION:vn-check * @Short_description: a checkbox widget * @Title: VnCheck * @See_also: #VnField * @Image: check.png * * A checkbox widget to use in boolean fields. */ G_DEFINE_TYPE (VnCheck, vn_check, VN_TYPE_FIELD); /** * vn_check_new: * * Creates a new #VnCheck * * Return value: a #VnCheck **/ VnField * vn_check_new () { return VN_FIELD (g_object_new (VN_TYPE_CHECK, NULL)); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Private static void vn_check_changed (VnCheck * obj) { GValue value = {0}; if (!gtk_toggle_button_get_inconsistent (obj->button)) { g_value_init (&value, G_TYPE_BOOLEAN); g_value_set_boolean (&value, gtk_toggle_button_get_active (obj->button)); } else g_value_init (&value, GVN_TYPE_NULL); VN_FIELD_GET_CLASS (obj)->value_changed (VN_FIELD (obj), &value); g_value_unset (&value); } static void vn_check_on_toggled (GtkToggleButton * button, VnCheck * obj) { gtk_toggle_button_set_inconsistent (button, FALSE); vn_check_changed (obj); } static gboolean vn_check_on_secondary_button (GtkToggleButton * button, GdkEventButton * event, VnField * obj) { if (event->type != GDK_BUTTON_RELEASE || event->button != 3 || !vn_field_get_null (obj)) return FALSE; if (gtk_toggle_button_get_inconsistent (button)) gtk_toggle_button_set_inconsistent (button, FALSE); else gtk_toggle_button_set_inconsistent (button, TRUE); g_signal_handlers_block_by_func (button, vn_check_on_toggled, obj); gtk_toggle_button_set_active (button, FALSE); g_signal_handlers_unblock_by_func (button, vn_check_on_toggled, obj); vn_check_changed (VN_CHECK (obj)); return FALSE; } static void vn_check_set_value (VnCheck * obj, const GValue * value) { GValue new_value = {0}; if (!gvn_value_is_null (value)) { g_value_init (&new_value, G_TYPE_BOOLEAN); g_value_transform (value, &new_value); } else g_value_init (&new_value, GVN_TYPE_NULL); g_signal_handlers_block_by_func (obj->button, vn_check_on_toggled, obj); if (!gvn_value_is_null (&new_value)) { gtk_toggle_button_set_inconsistent (obj->button, FALSE); gtk_toggle_button_set_active (obj->button, g_value_get_boolean (&new_value)); } else { gtk_toggle_button_set_inconsistent (obj->button, TRUE); gtk_toggle_button_set_active (obj->button, FALSE); } g_signal_handlers_unblock_by_func (obj->button, vn_check_on_toggled, obj); g_value_unset (&new_value); } //+++++++++++++++++++++++++++++++++++++++++++++++++++ Class static void vn_check_init (VnCheck * obj) { obj->button = GTK_TOGGLE_BUTTON (gtk_check_button_new ()); gtk_button_set_use_underline (GTK_BUTTON (obj->button), TRUE); g_object_connect (obj->button ,"signal::toggled", vn_check_on_toggled, obj ,"signal::button-release-event", vn_check_on_secondary_button, obj ,NULL ); gtk_container_add (GTK_CONTAINER (obj), GTK_WIDGET (obj->button)); VN_FIELD (obj)->field = GTK_WIDGET (obj->button); } static void vn_check_finalize (VnCheck * obj) { G_OBJECT_CLASS (vn_check_parent_class)->finalize (G_OBJECT (obj)); } static void vn_check_class_init (VnCheckClass * klass) { G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) vn_check_finalize; VN_FIELD_CLASS (klass)->set_value = (VnFieldSetValueFunc) vn_check_set_value; }