144 lines
4.0 KiB
C
144 lines
4.0 KiB
C
/*
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "vn-check.h"
|
|
#include <db/db.h>
|
|
|
|
/**
|
|
* 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 * self)
|
|
{
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
if (!gtk_toggle_button_get_inconsistent (self->button))
|
|
{
|
|
g_value_init (&value, G_TYPE_BOOLEAN);
|
|
g_value_set_boolean (&value, gtk_toggle_button_get_active (self->button));
|
|
}
|
|
else
|
|
g_value_init (&value, GVN_TYPE_NULL);
|
|
|
|
VN_FIELD_GET_CLASS (self)->value_changed (VN_FIELD (self), &value);
|
|
g_value_unset (&value);
|
|
}
|
|
|
|
static void vn_check_on_toggled (GtkToggleButton * button, VnCheck * self)
|
|
{
|
|
gtk_toggle_button_set_inconsistent (button, FALSE);
|
|
vn_check_changed (self);
|
|
}
|
|
|
|
static gboolean vn_check_on_secondary_button (GtkToggleButton * button, GdkEventButton * event, VnField * self)
|
|
{
|
|
if (event->type != GDK_BUTTON_RELEASE || event->button != 3 || !vn_field_get_null (self))
|
|
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, self);
|
|
gtk_toggle_button_set_active (button, FALSE);
|
|
g_signal_handlers_unblock_by_func (button, vn_check_on_toggled, self);
|
|
|
|
vn_check_changed (VN_CHECK (self));
|
|
return FALSE;
|
|
}
|
|
|
|
static void vn_check_set_value (VnCheck * self, const GValue * value)
|
|
{
|
|
GValue new_value = G_VALUE_INIT;
|
|
|
|
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 (self->button, vn_check_on_toggled, self);
|
|
|
|
if (!gvn_value_is_null (&new_value))
|
|
{
|
|
gtk_toggle_button_set_inconsistent (self->button, FALSE);
|
|
gtk_toggle_button_set_active (self->button,
|
|
g_value_get_boolean (&new_value));
|
|
}
|
|
else
|
|
{
|
|
gtk_toggle_button_set_inconsistent (self->button, TRUE);
|
|
gtk_toggle_button_set_active (self->button, FALSE);
|
|
}
|
|
|
|
g_signal_handlers_unblock_by_func (self->button, vn_check_on_toggled, self);
|
|
|
|
g_value_unset (&new_value);
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_check_init (VnCheck * self)
|
|
{
|
|
self->button = GTK_TOGGLE_BUTTON (gtk_check_button_new ());
|
|
gtk_button_set_use_underline (GTK_BUTTON (self->button), TRUE);
|
|
g_object_connect (self->button
|
|
,"signal::toggled", vn_check_on_toggled, self
|
|
,"signal::button-release-event", vn_check_on_secondary_button, self
|
|
,NULL
|
|
);
|
|
gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->button));
|
|
|
|
VN_FIELD_GET_CLASS (self)->set_widget (VN_FIELD (self),
|
|
GTK_WIDGET (self->button));
|
|
}
|
|
|
|
static void vn_check_finalize (VnCheck * self)
|
|
{
|
|
G_OBJECT_CLASS (vn_check_parent_class)->finalize (G_OBJECT (self));
|
|
}
|
|
|
|
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;
|
|
}
|