From 85e17915a1568396c5810408cda9340ba3aacc07 Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 6 May 2017 17:15:27 +0200 Subject: [PATCH] Added properties text and inline to ZakFormCgiFormElementCheck. --- src/formelement.c | 8 +-- src/formelementcheck.c | 120 +++++++++++++++++++++++++++++++++-------- src/formelementcheck.h | 8 ++- src/formelementradio.c | 6 +-- 4 files changed, 111 insertions(+), 31 deletions(-) diff --git a/src/formelement.c b/src/formelement.c index f17829c..ef61958 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -138,7 +138,7 @@ zak_form_cgi_form_element_set_label (ZakFormCgiFormElement *element, const gchar g_hash_table_replace (priv->ht_label_attrs, "zak-cgi-content", g_strdup (label)); g_hash_table_replace (priv->ht_label_attrs, "for", priv->id != NULL ? g_strdup (priv->id) : g_strdup ("")); - g_hash_table_replace (priv->ht_label_attrs, "class", "control-label"); + g_hash_table_replace (priv->ht_label_attrs, "class", g_strdup ("control-label")); zak_form_element_set_long_name (ZAK_FORM_ELEMENT (element), label); } @@ -174,6 +174,8 @@ gchar guint i; + gchar *attr_class; + ZakFormCgiFormElementPrivate *priv; priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element); @@ -193,8 +195,6 @@ gchar if (ZAK_FORM_CGI_IS_FORM_ELEMENT (element) && ZAK_FORM_CGI_FORM_ELEMENT_GET_CLASS (element)->render != NULL) { - gchar *attr_class; - attr_class = g_hash_table_lookup (priv->ht_attrs, "class"); if (attr_class != NULL) { @@ -206,7 +206,7 @@ gchar } else { - g_hash_table_replace (priv->ht_attrs, g_strdup ("class"), g_strdup ("form-control")); + g_hash_table_replace (priv->ht_attrs, "class", g_strdup ("form-control")); } if (!zak_form_element_get_editable (ZAK_FORM_ELEMENT (element))) diff --git a/src/formelementcheck.c b/src/formelementcheck.c index 8042095..a8c1b83 100644 --- a/src/formelementcheck.c +++ b/src/formelementcheck.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015-2016 Andrea Zagli + * Copyright (C) 2015-2017 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -31,13 +31,13 @@ static void zak_form_cgi_form_element_check_init (ZakFormCgiFormElementCheck *za static gchar *zak_form_cgi_form_element_check_render (ZakFormCgiFormElement *element); static void zak_form_cgi_form_element_check_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec); + guint property_id, + const GValue *value, + GParamSpec *pspec); static void zak_form_cgi_form_element_check_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec); + guint property_id, + GValue *value, + GParamSpec *pspec); static void zak_form_cgi_form_element_check_dispose (GObject *gobject); static void zak_form_cgi_form_element_check_finalize (GObject *gobject); @@ -47,7 +47,8 @@ static void zak_form_cgi_form_element_check_finalize (GObject *gobject); typedef struct _ZakFormCgiFormElementCheckPrivate ZakFormCgiFormElementCheckPrivate; struct _ZakFormCgiFormElementCheckPrivate { - gpointer empty; + gchar *text; + gboolean in_line; }; G_DEFINE_TYPE (ZakFormCgiFormElementCheck, zak_form_cgi_form_element_check, ZAK_FORM_CGI_TYPE_FORM_ELEMENT) @@ -73,6 +74,8 @@ zak_form_cgi_form_element_check_init (ZakFormCgiFormElementCheck *zak_form_cgi_f { ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_cgi_form_element_check); + priv->text = g_strdup (""); + priv->in_line = TRUE; } /** @@ -99,7 +102,7 @@ ZakFormCgiFormElement */ ZakFormCgiFormElement *zak_form_cgi_form_element_check_new_attrs (const gchar *id, - ...) + ...) { va_list ap; @@ -115,12 +118,53 @@ ZakFormCgiFormElement g_hash_table_replace (ht_attrs, "type", "checkbox"); ZAK_FORM_CGI_FORM_ELEMENT_CLASS (zak_form_cgi_form_element_check_parent_class)->construct (zak_form_cgi_form_element_check, - id, - ht_attrs); + id, + ht_attrs); return zak_form_cgi_form_element_check; } +gchar +*zak_form_cgi_form_element_check_get_text (ZakFormCgiFormElementCheck *element) +{ + ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (element); + g_warning("TEXT %s",priv->text); + return g_strdup (priv->text); +} + +void +zak_form_cgi_form_element_check_set_text (ZakFormCgiFormElementCheck *element, const gchar *text) +{ + ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (element); + + g_free (priv->text); + + if (text == NULL) + { + priv->text = g_strdup (""); + } + else + { + priv->text = g_strdup (text); + } +} + +gboolean +zak_form_cgi_form_element_check_get_in_line (ZakFormCgiFormElementCheck *element) +{ + ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (element); + + return priv->in_line; +} + +void +zak_form_cgi_form_element_check_set_in_line (ZakFormCgiFormElementCheck *element, gboolean in_line) +{ + ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (element); + + priv->in_line = in_line; +} + gboolean zak_form_cgi_form_element_check_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode) { @@ -142,16 +186,26 @@ zak_form_cgi_form_element_check_xml_parsing (ZakFormElement *element, xmlNodePtr cur = xmlnode->children; while (cur != NULL) { - if (xmlStrcmp (cur->name, (const xmlChar *)"id") == 0) + if (xmlStrEqual (cur->name, (const xmlChar *)"id")) { id = (gchar *)xmlNodeGetContent (cur); } - else if (xmlStrcmp (cur->name, (const xmlChar *)"label") == 0) + else if (xmlStrEqual (cur->name, (const xmlChar *)"label")) { zak_form_cgi_form_element_set_label (ZAK_FORM_CGI_FORM_ELEMENT (element), (gchar *)xmlNodeGetContent (cur), NULL); } - else if (xmlStrcmp (cur->name, (const xmlChar *)"text") == 0) + else if (xmlStrEqual (cur->name, (const xmlChar *)"text")) + { + + } + else if (xmlStrEqual (cur->name, (const xmlChar *)"zak-cgi-text")) + { + zak_form_cgi_form_element_check_set_text (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element), (gchar *)xmlNodeGetContent (cur)); + } + else if (xmlStrEqual (cur->name, (const xmlChar *)"zak-cgi-inline")) { + zak_form_cgi_form_element_check_set_in_line (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element), + (gboolean)xmlStrEqual (xmlNodeGetContent (cur), (const xmlChar *)"TRUE")); } else { @@ -164,8 +218,8 @@ zak_form_cgi_form_element_check_xml_parsing (ZakFormElement *element, xmlNodePtr if (id != NULL) { ZAK_FORM_CGI_FORM_ELEMENT_CLASS (zak_form_cgi_form_element_check_parent_class)->construct (ZAK_FORM_CGI_FORM_ELEMENT (element), - id, - ht_attrs); + id, + ht_attrs); ret = TRUE; } else @@ -180,13 +234,17 @@ static gchar *zak_form_cgi_form_element_check_render (ZakFormCgiFormElement *element) { gchar *ret; + GString *str; GHashTable *ht_attrs; gchar *value; + gchar *attr_class; ZakFormCgiFormElementClass *klass; + ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (element); + klass = (ZakFormCgiFormElementClass *)g_type_class_peek_parent (ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_CLASS (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element))); ht_attrs = klass->get_ht_attrs (element); @@ -198,7 +256,23 @@ static gchar g_hash_table_insert (ht_attrs, (gpointer)"checked", (gpointer)"checked"); } - ret = zak_cgi_tag_tag_ht ("input", zak_form_cgi_form_element_get_id (element), ht_attrs); + attr_class = g_hash_table_lookup (ht_attrs, "class"); + if (attr_class != NULL) + { + g_hash_table_insert (ht_attrs, "class", g_strjoinv ("", g_strsplit (attr_class, "form-control", -1))); + g_free (attr_class); + } + + str = g_string_new (""); + g_string_append_printf (str, "\n%s\n%s %s%s", + priv->in_line ? "" : "
\n", + priv->in_line ? " class=\"checkbox-inline\"" : "", + zak_cgi_tag_tag_ht ("input", zak_form_cgi_form_element_get_id (element), ht_attrs), + zak_form_cgi_form_element_check_get_text (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element)), + priv->in_line ? "" : "\n

"); + + ret = g_strdup (str->str); + g_string_free (str, TRUE); return ret; } @@ -206,9 +280,9 @@ static gchar /* PRIVATE */ static void zak_form_cgi_form_element_check_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) + guint property_id, + const GValue *value, + GParamSpec *pspec) { ZakFormCgiFormElementCheck *zak_form_cgi_form_element_check = (ZakFormCgiFormElementCheck *)object; ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_cgi_form_element_check); @@ -223,9 +297,9 @@ zak_form_cgi_form_element_check_set_property (GObject *object, static void zak_form_cgi_form_element_check_get_property (GObject *object, - guint property_id, - GValue *value, - GParamSpec *pspec) + guint property_id, + GValue *value, + GParamSpec *pspec) { ZakFormCgiFormElementCheck *zak_form_cgi_form_element_check = (ZakFormCgiFormElementCheck *)object; ZakFormCgiFormElementCheckPrivate *priv = ZAK_FORM_CGI_FORM_ELEMENT_CHECK_GET_PRIVATE (zak_form_cgi_form_element_check); diff --git a/src/formelementcheck.h b/src/formelementcheck.h index 538e654..666a3c3 100644 --- a/src/formelementcheck.h +++ b/src/formelementcheck.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Andrea Zagli + * Copyright (C) 2015-2017 Andrea Zagli * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -56,6 +56,12 @@ GType zak_form_cgi_form_element_check_get_type (void); ZakFormCgiFormElement *zak_form_cgi_form_element_check_new (void); ZakFormCgiFormElement *zak_form_cgi_form_element_check_new_attrs (const gchar *id, ...); +gchar *zak_form_cgi_form_element_check_get_text (ZakFormCgiFormElementCheck *element); +void zak_form_cgi_form_element_check_set_text (ZakFormCgiFormElementCheck *element, const gchar *text); + +gboolean zak_form_cgi_form_element_check_get_in_line (ZakFormCgiFormElementCheck *element); +void zak_form_cgi_form_element_check_set_in_line (ZakFormCgiFormElementCheck *element, gboolean in_line); + gboolean zak_form_cgi_form_element_check_xml_parsing (ZakFormElement *element, xmlNodePtr xmlnode); diff --git a/src/formelementradio.c b/src/formelementradio.c index 11a1dbc..93465af 100644 --- a/src/formelementradio.c +++ b/src/formelementradio.c @@ -233,6 +233,8 @@ static gchar GHashTable *ht_label_attrs; GHashTable *ht_attrs_option; + gchar *attr_class; + gchar *element_value; guint new_id; @@ -262,8 +264,6 @@ static gchar g_hash_table_iter_init (&iter, priv->ht_options); while (g_hash_table_iter_next (&iter, &key, &value)) { - gchar *attr_class; - ht_attrs_option = g_hash_table_new (g_str_hash, g_str_equal); zak_utils_ghashtable_copy (ht_attrs, ht_attrs_option); @@ -287,7 +287,7 @@ static gchar priv->in_line ? " class=\"radio-inline\"" : "", zak_cgi_tag_tag_ht ("input", g_strdup_printf ("%s_%d", - zak_form_cgi_form_element_get_id (element), + zak_form_cgi_form_element_get_id (element), ++new_id), ht_attrs_option), value, -- 2.49.0