]> saetta.ns0.it Git - libzakcgi/commitdiff
Added ZakCgiFormElement::set_label.
authorAndrea Zagli <azagli@libero.it>
Sat, 12 Sep 2015 09:57:09 +0000 (11:57 +0200)
committerAndrea Zagli <azagli@libero.it>
Sat, 12 Sep 2015 09:57:09 +0000 (11:57 +0200)
Changed content special attribute in zak-cgi-content for tags.

src/formelement.c
src/formelement.h
src/tag.c
tests/form.c
tests/tag.c

index 060522d9ae6ab364752c150047dd994b99ec39cd..aea8afa075e8193cd9db2bb30290d515e8846a3f 100644 (file)
@@ -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;
 }
 
index 8602e4b9039882643ebce90e83632a7dc4fec3c9..709d32c867191d2bfc39b6dfa7467ae6459d3446 100644 (file)
@@ -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);
index 2fda7d7346709e16905e3569e661ad5ee617a535..9dbdf1708451248e00cf87ed2c39e17a13affd9b 100644 (file)
--- 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)
index e7d5837290d66018df3b42db56539a6467e264cd..cd48a8fcc4e9f50ca5c8de4c9607ff0f9297dabf 100644 (file)
@@ -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, "<h1>big big big</h1>");
index 73f418748c26022a5f08a1ea24178c17f9356884..273ced306d45f4944e8e3f3cc26ec5dfe1ba44e5 100644 (file)
@@ -33,7 +33,7 @@ main (int argc, char *argv[])
                                                                          NULL));
 
        g_string_append_printf (str, "%s<br/>\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<br/>\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<br/>\n", zak_cgi_tag_tag ("select", "idselect", "content", select_content->str, NULL));
+       g_string_append_printf (str, "%s<br/>\n", zak_cgi_tag_tag ("select", "idselect", "zak-cgi-content", select_content->str, NULL));
 
        zak_cgi_main_out (NULL, str->str);