From: Andrea Zagli Date: Wed, 25 Nov 2015 17:28:56 +0000 (+0100) Subject: Added class ZakFormGtkFormElement (abstract). X-Git-Tag: v0.1.0~19 X-Git-Url: https://saetta.ns0.it/gitweb?a=commitdiff_plain;h=8ee1fb032f08b165f898c9dace762822ea948009;p=zakform%2Fgtk Added class ZakFormGtkFormElement (abstract). --- diff --git a/configure.ac b/configure.ac index 2c38dbd..c875c6c 100644 --- a/configure.ac +++ b/configure.ac @@ -39,7 +39,8 @@ AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE", AM_GLIB_GNU_GETTEXT # Checks for libraries. -PKG_CHECK_MODULES(ZAKFORMGTK, [libzakform]) +PKG_CHECK_MODULES(ZAKFORMGTK, [libzakform + gtk+-3.0]) AC_SUBST(ZAKFORMGTK_CFLAGS) AC_SUBST(ZAKFORMGTK_LIBS) diff --git a/src/Makefile.am b/src/Makefile.am index 3f8cd4a..7fd8288 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -7,12 +7,14 @@ AM_CPPFLAGS = $(ZAKFORMGTK_CFLAGS) \ lib_LTLIBRARIES = libzakformgtk.la libzakformgtk_la_SOURCES = \ - form.c + form.c \ + formelement.c libzakformgtk_la_LDFLAGS = -no-undefined libzakformgtk_include_HEADERS = \ libzakformgtk.h \ - form.h + form.h \ + formelement.h libzakformgtk_includedir = $(includedir)/libzakformgtk diff --git a/src/formelement.c b/src/formelement.c new file mode 100644 index 0000000..ed63121 --- /dev/null +++ b/src/formelement.c @@ -0,0 +1,296 @@ +/* + * 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 "formelement.h" + +enum +{ + PROP_0, + PROP_WIDGET, + PROP_NAME, + PROP_LABEL +}; + +static void zak_form_gtk_form_element_class_init (ZakFormGtkFormElementClass *class); +static void zak_form_gtk_form_element_init (ZakFormGtkFormElement *zak_form_gtk_form_element); + +static void zak_form_gtk_form_element_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec); +static void zak_form_gtk_form_element_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec); + +static void zak_form_gtk_form_element_dispose (GObject *gobject); +static void zak_form_gtk_form_element_finalize (GObject *gobject); + +#define ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), ZAK_FORM_GTK_TYPE_FORM_ELEMENT, ZakFormGtkFormElementPrivate)) + +typedef struct _ZakFormGtkFormElementPrivate ZakFormGtkFormElementPrivate; +struct _ZakFormGtkFormElementPrivate + { + GtkWidget *widget; + gchar *name; + + GtkWidget *label; + }; + +G_DEFINE_ABSTRACT_TYPE (ZakFormGtkFormElement, zak_form_gtk_form_element, ZAK_FORM_TYPE_ELEMENT) + +static void +zak_form_gtk_form_element_class_init (ZakFormGtkFormElementClass *class) +{ + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->set_property = zak_form_gtk_form_element_set_property; + object_class->get_property = zak_form_gtk_form_element_get_property; + object_class->dispose = zak_form_gtk_form_element_dispose; + object_class->finalize = zak_form_gtk_form_element_finalize; + + g_type_class_add_private (object_class, sizeof (ZakFormGtkFormElementPrivate)); + + g_object_class_install_property (object_class, PROP_WIDGET, + g_param_spec_object ("widget", + "Widget", + "Widget", + GTK_TYPE_WIDGET, + G_PARAM_READABLE)); + + g_object_class_install_property (object_class, PROP_NAME, + g_param_spec_string ("name", + "Name", + "Name", + NULL, + G_PARAM_READABLE)); + + g_object_class_install_property (object_class, PROP_LABEL, + g_param_spec_object ("label", + "Label", + "Label", + GTK_TYPE_WIDGET, + G_PARAM_READWRITE | G_PARAM_CONSTRUCT)); +} + +static void +zak_form_gtk_form_element_init (ZakFormGtkFormElement *zak_form_gtk_form_element) +{ + ZakFormGtkFormElementPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (zak_form_gtk_form_element); + + priv->widget = NULL; + priv->name = NULL; + priv->label = NULL; +} + +/** + * zak_form_gtk_form_element_widget_set_from_gtkbuilder: + * @element: + * @gtkbuilder: + * @widget_name: + * + */ +gboolean +zak_form_gtk_form_element_widget_set_from_gtkbuilder (ZakFormGtkFormElement *element, + GtkBuilder *gtkbuilder, + const gchar *widget_name) +{ + ZakFormGtkFormElementPrivate *priv; + + gboolean ret; + GtkWidget *w; + + g_return_val_if_fail (GTK_IS_BUILDER (gtkbuilder), FALSE); + g_return_val_if_fail (widget_name != NULL, FALSE); + g_return_val_if_fail (g_strcmp0 (widget_name, "") != 0, FALSE); + + priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (element); + + ret = FALSE; + w = GTK_WIDGET (gtk_builder_get_object (gtkbuilder, widget_name)); + if (w != NULL) + { + priv->widget = g_object_ref (w); + priv->name = g_strdup (widget_name); + ret = TRUE; + } + else + { + g_warning (_("Widget «%s» not found."), widget_name); + } + + return TRUE; +} + +/** + * zak_form_gtk_form_element_get_widget: + * @element: + * + */ +GtkWidget +*zak_form_gtk_form_element_get_widget (ZakFormGtkFormElement *element) +{ + ZakFormGtkFormElementPrivate *priv; + + priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (element); + + return priv->widget; +} + +/** + * zak_form_get_form_element_set_widget: + * @element: + * @widget: + * @widget_name: + * + */ +void +zak_form_gtk_form_element_set_widget (ZakFormGtkFormElement *element, GtkWidget *widget, const gchar *widget_name) +{ + ZakFormGtkFormElementPrivate *priv; + + g_return_if_fail (GTK_IS_WIDGET (widget)); + g_return_if_fail (widget_name != NULL); + g_return_if_fail (g_strcmp0 (widget_name, "") != 0); + + priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (element); + + priv->widget = g_object_ref (widget); + priv->name = g_strdup (widget_name); +} + +/** + * zak_form_gtk_form_element_get_widget_name: + * @element: + * + */ +gchar +*zak_form_gtk_form_element_get_widget_name (ZakFormGtkFormElement *element) +{ + ZakFormGtkFormElementPrivate *priv; + + priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (element); + + return g_strdup (priv->name); +} + +/** + * zak_form_gtk_form_element_get_label: + * @element: + * + */ +GtkWidget +*zak_form_gtk_form_element_get_label (ZakFormGtkFormElement *element) +{ + ZakFormGtkFormElementPrivate *priv; + + priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (element); + + return priv->label; +} + +/** + * zak_form_gtk_form_element_set_label: + * @element: + * @label: + * + */ +void +zak_form_gtk_form_element_set_label (ZakFormGtkFormElement *element, GtkWidget *label) +{ + ZakFormGtkFormElementPrivate *priv; + + priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (element); + + if (priv->label != NULL) + { + g_object_unref (priv->label); + } + priv->label = g_object_ref (label); +} + +/* PRIVATE */ +static void +zak_form_gtk_form_element_set_property (GObject *object, + guint property_id, + const GValue *value, + GParamSpec *pspec) +{ + ZakFormGtkFormElement *zak_form_gtk_form_element = (ZakFormGtkFormElement *)object; + ZakFormGtkFormElementPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (zak_form_gtk_form_element); + + switch (property_id) + { + case PROP_LABEL: + zak_form_gtk_form_element_set_label (zak_form_gtk_form_element, (GtkWidget *)g_value_get_object (value)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_gtk_form_element_get_property (GObject *object, + guint property_id, + GValue *value, + GParamSpec *pspec) +{ + ZakFormGtkFormElement *zak_form_gtk_form_element = (ZakFormGtkFormElement *)object; + ZakFormGtkFormElementPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (zak_form_gtk_form_element); + + switch (property_id) + { + case PROP_LABEL: + g_value_set_object (value, (gpointer)zak_form_gtk_form_element_get_label (zak_form_gtk_form_element)); + break; + + default: + G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); + break; + } +} + +static void +zak_form_gtk_form_element_dispose (GObject *gobject) +{ + ZakFormGtkFormElement *zak_form_gtk_form_element = (ZakFormGtkFormElement *)gobject; + ZakFormGtkFormElementPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (zak_form_gtk_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->dispose (gobject); +} + +static void +zak_form_gtk_form_element_finalize (GObject *gobject) +{ + ZakFormGtkFormElement *zak_form_gtk_form_element = (ZakFormGtkFormElement *)gobject; + ZakFormGtkFormElementPrivate *priv = ZAK_FORM_GTK_FORM_ELEMENT_GET_PRIVATE (zak_form_gtk_form_element); + + + + GObjectClass *parent_class = g_type_class_peek_parent (G_OBJECT_GET_CLASS (gobject)); + parent_class->finalize (gobject); +} diff --git a/src/formelement.h b/src/formelement.h new file mode 100644 index 0000000..d193f1b --- /dev/null +++ b/src/formelement.h @@ -0,0 +1,59 @@ +/* + * 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_H__ +#define __ZAK_FORM_GTK_FORM_ELEMENT_H__ + + +#include +#include + +#include + +#include + + +G_BEGIN_DECLS + + +#define ZAK_FORM_GTK_TYPE_FORM_ELEMENT zak_form_gtk_form_element_get_type () +G_DECLARE_DERIVABLE_TYPE (ZakFormGtkFormElement, zak_form_gtk_form_element, ZAK_FORM_GTK, FORM_ELEMENT, ZakFormElement) + +struct _ZakFormGtkFormElementClass + { + ZakFormElementClass parent_class; + }; + + +gboolean zak_form_gtk_form_element_widget_set_from_gtkbuilder (ZakFormGtkFormElement *element, + GtkBuilder *gtkbuilder, + const gchar *widget_name); + +GtkWidget *zak_form_gtk_form_element_get_widget (ZakFormGtkFormElement *element); +void zak_form_gtk_form_element_set_widget (ZakFormGtkFormElement *element, GtkWidget *widget, const gchar *widget_name); + +gchar *zak_form_gtk_form_element_get_widget_name (ZakFormGtkFormElement *element); + +GtkWidget *zak_gtk_form_element_get_label (ZakFormGtkFormElement *element); +void zak_form_gtk_form_element_set_label (ZakFormGtkFormElement *element, GtkWidget *label); + + +G_END_DECLS + + +#endif /* __ZAK_FORM_GTK_FORM_ELEMENT_H__ */ diff --git a/src/libzakformgtk.h b/src/libzakformgtk.h index d34a10f..8dc3676 100644 --- a/src/libzakformgtk.h +++ b/src/libzakformgtk.h @@ -22,5 +22,7 @@ #include +#include + #endif /* __LIBZAKFORMGTK_H__ */