From 37b24f02f1e9cb55d92bd926d4dbe18ada868699 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Wed, 25 Nov 2015 22:21:52 +0100 Subject: [PATCH] Added ZakFormGtkFormElementCheck. --- src/Makefile.am | 6 +- src/formelementcheck.c | 206 +++++++++++++++++++++++++++++++++++++++++ src/formelementcheck.h | 60 ++++++++++++ src/libzakformgtk.h | 1 + 4 files changed, 271 insertions(+), 2 deletions(-) create mode 100644 src/formelementcheck.c create mode 100644 src/formelementcheck.h diff --git a/src/Makefile.am b/src/Makefile.am index 7fd8288..4d8a0db 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,13 +8,15 @@ lib_LTLIBRARIES = libzakformgtk.la libzakformgtk_la_SOURCES = \ form.c \ - formelement.c + formelement.c \ + formelementcheck.c libzakformgtk_la_LDFLAGS = -no-undefined libzakformgtk_include_HEADERS = \ libzakformgtk.h \ form.h \ - formelement.h + formelement.h \ + formelementcheck.h libzakformgtk_includedir = $(includedir)/libzakformgtk diff --git a/src/formelementcheck.c b/src/formelementcheck.c new file mode 100644 index 0000000..91a5a4b --- /dev/null +++ b/src/formelementcheck.c @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifdef HAVE_CONFIG_H + #include +#endif + +#include "formelementcheck.h" + +static void zak_form_gtk_form_element_check_class_init (ZakFormGtkFormElementCheckClass *class); +static void zak_form_gtk_form_element_check_init (ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check); + +static void zak_form_gtk_form_element_check_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_gtk_form_element_check_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_gtk_form_element_check_dispose (GObject *gobject); +static void zak_form_gtk_form_element_check_finalize (GObject *gobject); + +static gchar *zak_form_gtk_form_element_check_get_value (ZakFormGtkFormElementCheck *element); +static gboolean zak_form_gtk_form_element_check_set_value (ZakFormGtkFormElementCheck *element, const gchar *value); + +#define ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK, ZakFormGtkFormElementCheckPrivate)) + +typedef struct _ZakFormGtkFormElementCheckPrivate ZakFormGtkFormElementCheckPrivate; +struct _ZakFormGtkFormElementCheckPrivate + { + gpointer empty; + }; + +G_DEFINE_TYPE (ZakFormGtkFormElementCheck, zak_form_gtk_form_element_check, ZAK_FORM_GTK_TYPE_FORM_ELEMENT) + +static void +zak_form_gtk_form_element_check_class_init (ZakFormGtkFormElementCheckClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + ZakFormElementClass *elem_class = ZAK_FORM_ELEMENT_CLASS (klass); + + object_class->set_property = zak_form_gtk_form_element_check_set_property; + object_class->get_property = zak_form_gtk_form_element_check_get_property; + object_class->dispose = zak_form_gtk_form_element_check_dispose; + object_class->finalize = zak_form_gtk_form_element_check_finalize; + + elem_class->get_value = zak_form_gtk_form_element_check_get_value; + elem_class->set_value = zak_form_gtk_form_element_check_set_value; + + g_type_class_add_private (object_class, sizeof (ZakFormGtkFormElementCheckPrivate)); +} + +static void +zak_form_gtk_form_element_check_init (ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check) +{ + ZakFormGtkFormElementCheckPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_gtk_form_element_check); + +} + +/** + * zak_form_gtk_form_element_check_new: + * + * Returns: the newly created #ZakFormGtkFormElementCheck object. + */ +ZakFormGtkFormElement +*zak_form_gtk_form_element_check_new () +{ + ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check; + + zak_form_gtk_form_element_check = ZAK_FORM_GTK_FORM_ELEMENT_CHECK (g_object_new (zak_form_gtk_form_element_check_get_type (), NULL)); + + return ZAK_FORM_GTK_FORM_ELEMENT (zak_form_gtk_form_element_check); +} + +/* PRIVATE */ +static void +zak_form_gtk_form_element_check_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check = (ZakFormGtkFormElementCheck *)object; + ZakFormGtkFormElementCheckPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_gtk_form_element_check); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_gtk_form_element_check_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check = (ZakFormGtkFormElementCheck *)object; + ZakFormGtkFormElementCheckPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_gtk_form_element_check); + + switch (property_id) + { + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_gtk_form_element_check_dispose (GObject *gobject) +{ + ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check = (ZakFormGtkFormElementCheck *)gobject; + ZakFormGtkFormElementCheckPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_gtk_form_element_check); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_gtk_form_element_check_finalize (GObject *gobject) +{ + ZakFormGtkFormElementCheck *zak_form_gtk_form_element_check = (ZakFormGtkFormElementCheck *)gobject; + ZakFormGtkFormElementCheckPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_gtk_form_element_check); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} + +static gchar +*zak_form_gtk_form_element_check_get_value (ZakFormGtkFormElementCheck *element) +{ + gchar *ret; + + GtkWidget *w; + + ret = g_strdup ("FALSE"); + + w = zak_form_gtk_form_element_get_widget (ZAK_FORM_GTK_FORM_ELEMENT (element)); + + if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (w))) + { + ret = g_strdup ("TRUE"); + } + else if (gtk_toggle_button_get_inconsistent (GTK_TOGGLE_BUTTON (w))) + { + ret = g_strdup (""); + } + + return ret; +} + +static gboolean +zak_form_gtk_form_element_check_set_value (ZakFormGtkFormElementCheck *element, const gchar *value) +{ + GtkWidget *w; + + gboolean active; + gchar *str_value; + + w = zak_form_gtk_form_element_get_widget (ZAK_FORM_GTK_FORM_ELEMENT (element)); + + str_value = g_strstrip (g_strdup (zak_form_gtk_form_element_check_get_value (element))); + + if (strcmp (str_value, "0") == 0 + || strcasecmp (str_value, "f") == 0 + || strcasecmp (str_value, "false") == 0 + || strcasecmp (str_value, "n") == 0 + || strcasecmp (str_value, "no") == 0) + { + active = FALSE; + } + else if (strcmp (str_value, "1") == 0 + || strcasecmp (str_value, "t") == 0 + || strcasecmp (str_value, "true") == 0 + || strcasecmp (str_value, "y") == 0 + || strcasecmp (str_value, "yes") == 0) + { + active = TRUE; + } + + g_free (str_value); + + gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (w), active); +} diff --git a/src/formelementcheck.h b/src/formelementcheck.h new file mode 100644 index 0000000..48a80f4 --- /dev/null +++ b/src/formelementcheck.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2015 Andrea Zagli + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef __ZAK_FORM_GTK_FORM_ELEMENT_CHECK_H__ +#define __ZAK_FORM_GTK_FORM_ELEMENT_CHECK_H__ + + +#include + +#include "formelement.h" + + +G_BEGIN_DECLS + + +#define ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK (zak_form_gtk_form_element_check_get_type ()) +#define ZAK_FORM_GTK_FORM_ELEMENT_CHECK(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK, ZakFormGtkFormElementCheck)) +#define ZAK_FORM_GTK_FORM_ELEMENT_CHECK_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK, ZakFormGtkFormElementCheckClass)) +#define ZAK_FORM_GTK_IS_FORM_ELEMENT_CHECK(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK)) +#define ZAK_FORM_GTK_IS_FORM_ELEMENT_CHECK_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK)) +#define ZAK_FORM_GTK_FORM_ELEMENT_CHECK_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), ZAK_FORM_GTK_TYPE_FORM_ELEMENT_CHECK, ZakFormGtkFormElementCheckClass)) + +typedef struct _ZakFormGtkFormElementCheck ZakFormGtkFormElementCheck; +typedef struct _ZakFormGtkFormElementCheckClass ZakFormGtkFormElementCheckClass; + +struct _ZakFormGtkFormElementCheck + { + ZakFormGtkFormElement parent_instance; + }; + +struct _ZakFormGtkFormElementCheckClass + { + ZakFormGtkFormElementClass parent_class; + }; + + +GType zak_form_gtk_form_element_check_get_type (void); + +ZakFormGtkFormElement *zak_form_gtk_form_element_check_new (void); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_GTK_FORM_ELEMENT_CHECK_H__ */ diff --git a/src/libzakformgtk.h b/src/libzakformgtk.h index 8dc3676..8c8b4a0 100644 --- a/src/libzakformgtk.h +++ b/src/libzakformgtk.h @@ -23,6 +23,7 @@ #include #include +#include #endif /* __LIBZAKFORMGTK_H__ */ -- 2.49.0