From dc809f3f08be02e701f643aa96a7378e4ade573e Mon Sep 17 00:00:00 2001 From: Andrea Zagli Date: Sat, 12 Sep 2015 11:57:09 +0200 Subject: [PATCH] Added ZakCgiFormElement::set_label. Changed content special attribute in zak-cgi-content for tags. --- src/formelement.c | 52 +++++++++++++++++++++++++++++++++++++++++++++-- src/formelement.h | 2 ++ src/tag.c | 2 +- tests/form.c | 1 + tests/tag.c | 10 ++++----- 5 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/formelement.c b/src/formelement.c index 060522d..aea8afa 100644 --- a/src/formelement.c +++ b/src/formelement.c @@ -24,6 +24,7 @@ #include "commons.h" #include "formelement.h" +#include "tag.h" enum { @@ -62,6 +63,7 @@ struct _ZakCgiFormElementPrivate gchar *validation_regex; GHashTable *ht_attrs; GValue *value; + GHashTable *ht_label_attrs; }; G_DEFINE_TYPE (ZakCgiFormElement, zak_cgi_form_element, G_TYPE_OBJECT) @@ -97,6 +99,7 @@ zak_cgi_form_element_init (ZakCgiFormElement *zak_cgi_form_element) priv->validation_regex = NULL; priv->ht_attrs = NULL; priv->value = NULL; + priv->ht_label_attrs = NULL; } gchar @@ -182,6 +185,34 @@ GValue return priv->value; } +/** + * zak_cgi_form_element_set_label: + * @element: + * @label: + * @...: + * + */ +void +zak_cgi_form_element_set_label (ZakCgiFormElement *element, const gchar *label, ...) +{ + va_list ap; + + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + if (priv->ht_label_attrs != NULL) + { + g_hash_table_destroy (priv->ht_label_attrs); + } + + va_start (ap, label); + priv->ht_label_attrs = zak_cgi_commons_valist_to_ghashtable (ap); + + g_hash_table_replace (priv->ht_label_attrs, "zak-cgi-content", g_strdup (label)); + g_hash_table_replace (priv->ht_label_attrs, "for", g_hash_table_lookup (priv->ht_attrs, "name")); +} + /** * zak_cgi_form_element_render: * @element: @@ -190,15 +221,32 @@ GValue gchar *zak_cgi_form_element_render (ZakCgiFormElement *element) { + GString *str; gchar *ret; - ret = g_strdup (""); + ZakCgiFormElementPrivate *priv; + + priv = ZAK_CGI_FORM_ELEMENT_GET_PRIVATE (element); + + str = g_string_new (""); + + if (priv->ht_label_attrs != NULL) + { + gchar *lbl_id; + + lbl_id = g_strdup_printf ("lbl_%s", priv->id); + g_string_append (str, zak_cgi_tag_tag_ht ("label", lbl_id, priv->ht_label_attrs)); + g_free (lbl_id); + } if (ZAK_CGI_IS_FORM_ELEMENT (element) && ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render != NULL) { - ret = ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render (element); + g_string_append (str, ZAK_CGI_FORM_ELEMENT_GET_CLASS (element)->render (element)); } + ret = g_strdup (str->str); + g_string_free (str, TRUE); + return ret; } diff --git a/src/formelement.h b/src/formelement.h index 8602e4b..709d32c 100644 --- a/src/formelement.h +++ b/src/formelement.h @@ -65,6 +65,8 @@ gchar *zak_cgi_form_element_get_validation_regex (ZakCgiFormElement *element); void zak_cgi_form_element_set_value (ZakCgiFormElement *element, GValue *value); GValue *zak_cgi_form_element_get_value (ZakCgiFormElement *element); +void zak_cgi_form_element_set_label (ZakCgiFormElement *element, const gchar *label, ...); + gchar *zak_cgi_form_element_render (ZakCgiFormElement *element); gboolean zak_cgi_form_element_is_valid (ZakCgiFormElement *element); diff --git a/src/tag.c b/src/tag.c index 2fda7d7..9dbdf17 100644 --- a/src/tag.c +++ b/src/tag.c @@ -61,7 +61,7 @@ static gchar g_hash_table_iter_init (&iter, ht_attrs); while (g_hash_table_iter_next (&iter, &key, &val)) { - if (g_strcmp0 ((gchar *)key, "content") == 0) + if (g_strcmp0 ((gchar *)key, "zak-cgi-content") == 0) { with_content = TRUE; if (content != NULL) diff --git a/tests/form.c b/tests/form.c index e7d5837..cd48a8f 100644 --- a/tests/form.c +++ b/tests/form.c @@ -43,6 +43,7 @@ main (int argc, char *argv[]) NULL); element = zak_cgi_form_element_text_new ("first", "aaa", NULL); + zak_cgi_form_element_set_label (element, "The Label for first", NULL); zak_cgi_form_add_element (form, element); zak_cgi_form_add_str (form, "

big big big

"); diff --git a/tests/tag.c b/tests/tag.c index 73f4187..273ced3 100644 --- a/tests/tag.c +++ b/tests/tag.c @@ -33,7 +33,7 @@ main (int argc, char *argv[]) NULL)); g_string_append_printf (str, "%s
\n", zak_cgi_tag_tag ("textarea", "textarea", - "content", "the content of the text area", + "zak-cgi-content", "the content of the text area", NULL)); g_string_append_printf (str, "%s
\n", zak_cgi_tag_img ("theimage", "src", "pippo.png", NULL)); @@ -46,19 +46,19 @@ main (int argc, char *argv[]) g_string_append_printf (select_content, zak_cgi_tag_tag ("option", NULL, "value", "1", - "content", "One", + "zak-cgi-content", "One", NULL)); g_string_append_printf (select_content, zak_cgi_tag_tag ("option", NULL, "value", "2", - "content", "Two", + "zak-cgi-content", "Two", NULL)); g_string_append_printf (select_content, zak_cgi_tag_tag ("option", NULL, "value", "3", - "content", "Three", + "zak-cgi-content", "Three", NULL)); - g_string_append_printf (str, "%s
\n", zak_cgi_tag_tag ("select", "idselect", "content", select_content->str, NULL)); + g_string_append_printf (str, "%s
\n", zak_cgi_tag_tag ("select", "idselect", "zak-cgi-content", select_content->str, NULL)); zak_cgi_main_out (NULL, str->str); -- 2.49.0