]> saetta.ns0.it Git - zakform/cgi/commitdiff
Different way to render some control on Bootstrap v4.
authorAndrea Zagli <azagli@libero.it>
Mon, 29 Jun 2020 15:43:12 +0000 (17:43 +0200)
committerAndrea Zagli <azagli@libero.it>
Mon, 29 Jun 2020 15:43:12 +0000 (17:43 +0200)
src/form.c
src/form.h
src/formelement.c
src/formelement.h
src/formelementcheck.c
src/formelementradio.c
src/formelementsubmit.c

index 1b2e494d506f566951d3c684aa2ed2969b62165f..cc01b2d9bd1e9a587051f98ad890c1ab851d6001 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -50,6 +50,7 @@ struct _ZakFormCgiFormPrivate
                ZakCgiMain *zakcgimain;
                GHashTable *ht_attrs;
                GPtrArray *ar_elems;
+               gchar *bootstrap_version;
        };
 
 G_DEFINE_TYPE (ZakFormCgiForm, zak_form_cgi_form, ZAK_FORM_TYPE_FORM)
@@ -99,6 +100,7 @@ zak_form_cgi_form_init (ZakFormCgiForm *zak_form_cgi_form)
        priv->zakcgimain = NULL;
        priv->ht_attrs = NULL;
        priv->ar_elems = ZAK_FORM_FORM_CLASS (zak_form_cgi_form_parent_class)->get_elements (ZAK_FORM_FORM (zak_form_cgi_form));
+       priv->bootstrap_version = g_strdup ("3");
 }
 
 /**
@@ -335,6 +337,40 @@ gchar
        return g_strdup (str->str);
 }
 
+void
+zak_form_cgi_form_set_bootstrap_version (ZakFormCgiForm *zakcgiform, const gchar *version)
+{
+       guint i;
+
+       ZakFormCgiFormPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+       if (priv->bootstrap_version != NULL)
+               {
+                       g_free (priv->bootstrap_version);
+               }
+       priv->bootstrap_version = g_strdup (version);
+
+       /* for each element */
+       for (i = 0; i < priv->ar_elems->len; i++)
+               {
+                       ZakFormCgiFormElement *element = (ZakFormCgiFormElement *)g_ptr_array_index (priv->ar_elems, i);
+
+                       zak_form_cgi_form_element_set_bootstrap_version (element, priv->bootstrap_version);
+               }
+}
+
+gchar
+*zak_form_cgi_form_get_bootstrap_version (ZakFormCgiForm *zakcgiform)
+{
+       ZakFormCgiFormPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_GET_PRIVATE (zakcgiform);
+
+       return g_strdup (priv->bootstrap_version);
+}
+
 /* PRIVATE */
 static void
 zak_form_cgi_form_set_property (GObject *object,
index bb988c81606d14dc648c1fd410bee432954adf60..c152906d172780748d5e9a6b82db5a81d138526f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2016 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -47,6 +47,9 @@ void zak_form_cgi_form_bind (ZakFormCgiForm *zakcgiform);
 gchar *zak_form_cgi_form_render_start (ZakFormCgiForm *zakcgiform);
 gchar *zak_form_cgi_form_render (ZakFormCgiForm *zakcgiform);
 
+void zak_form_cgi_form_set_bootstrap_version (ZakFormCgiForm *zakcgiform, const gchar *version);
+gchar *zak_form_cgi_form_get_bootstrap_version (ZakFormCgiForm *zakcgiform);
+
 
 G_END_DECLS
 
index 7ee4a2fdc65167ea78405219e623d56762e9b805..9934afb2383e4b77104675c4e4232a8aa35d97d2 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -24,6 +24,8 @@
 #include <libzakcgi/libzakcgi.h>
 
 #include "formelement.h"
+#include "formelementcheck.h"
+#include "formelementradio.h"
 
 enum
 {
@@ -65,6 +67,7 @@ struct _ZakFormCgiFormElementPrivate
                GHashTable *ht_attrs;
                GHashTable *ht_label_attrs;
                GValue *value;
+               gchar *bootstrap_version;
        };
 
 G_DEFINE_TYPE (ZakFormCgiFormElement, zak_form_cgi_form_element, ZAK_FORM_TYPE_ELEMENT)
@@ -99,6 +102,7 @@ zak_form_cgi_form_element_init (ZakFormCgiFormElement *zak_form_cgi_form_element
 
        priv->ht_attrs = NULL;
        priv->ht_label_attrs = NULL;
+       priv->bootstrap_version = g_strdup ("3");
 }
 
 gchar
@@ -196,7 +200,10 @@ gchar
        if (messages != NULL
            && messages->len > 0)
                {
-                       g_string_append (str, " has-error");
+                       if (g_strcmp0 (priv->bootstrap_version, "3") == 0)
+                               {
+                                       g_string_append (str, " has-error");
+                               }
                }
 
        g_string_append (str, "\">\n");
@@ -227,11 +234,35 @@ gchar
                                        g_hash_table_insert (priv->ht_attrs, "class", g_strdup ("form-control"));
                                }
 
+                       if (messages != NULL
+                           && messages->len > 0)
+                               {
+                                       if (g_strcmp0 (priv->bootstrap_version, "4") == 0)
+                                               {
+                                                       g_hash_table_insert (priv->ht_attrs, "class", g_strdup_printf ("%s is-invalid", g_hash_table_lookup (priv->ht_attrs, "class")));
+                                               }
+                               }
+
                        if (!zak_form_element_get_editable (ZAK_FORM_ELEMENT (element)))
                                {
                                        g_hash_table_insert (priv->ht_attrs, (gpointer)"readonly", (gpointer)"readonly");
                                }
 
+                       if ((ZAK_FORM_CGI_IS_FORM_ELEMENT_CHECK (element)
+                            || ZAK_FORM_CGI_IS_FORM_ELEMENT_RADIO (element))
+                           && g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "4") == 0)
+                               {
+                                       g_string_append (str, "<div class=\"form-check");
+                                       if ((ZAK_FORM_CGI_IS_FORM_ELEMENT_CHECK (element)
+                                            && zak_form_cgi_form_element_check_get_in_line (ZAK_FORM_CGI_FORM_ELEMENT_CHECK (element)))
+                                           || (ZAK_FORM_CGI_IS_FORM_ELEMENT_RADIO (element)
+                                               && zak_form_cgi_form_element_radio_get_in_line (ZAK_FORM_CGI_FORM_ELEMENT_RADIO (element))))
+                                               {
+                                                       g_string_append (str, " form-check-inline");
+                                               }
+                                       g_string_append (str, "\">\n");
+                               }
+
                        g_string_append (str, ZAK_FORM_CGI_FORM_ELEMENT_GET_CLASS (element)->render (element));
                }
 
@@ -240,13 +271,21 @@ gchar
                        for (i = 0; i < messages->len; i++)
                                {
                                        g_string_append_printf (str,
-                                                               "\n<span id=\"helpBox_%s_%d\" class=\"help-block\">%s</span>",
+                                                               "\n<span id=\"helpBox_%s_%d\" class=\"%s\">%s</span>",
                                                                priv->id,
                                                                i + 1,
+                                                               g_strcmp0 (priv->bootstrap_version, "4") == 0 ? "invalid-feedback" : "help-block",
                                                                (gchar *)g_ptr_array_index (messages, i));
                                }
                }
 
+       if ((ZAK_FORM_CGI_IS_FORM_ELEMENT_CHECK (element)
+            || ZAK_FORM_CGI_IS_FORM_ELEMENT_RADIO (element))
+           && g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "4") == 0)
+               {
+                       g_string_append (str, "</div>\n");
+               }
+
        g_string_append (str, "\n</div>\n");
 
        ret = g_strdup (str->str);
@@ -300,6 +339,32 @@ gchar
        return ret;
 }
 
+void
+zak_form_cgi_form_element_set_bootstrap_version (ZakFormCgiFormElement *element, const gchar *version)
+{
+       guint i;
+
+       ZakFormCgiFormElementPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       if (priv->bootstrap_version != NULL)
+               {
+                       g_free (priv->bootstrap_version);
+               }
+       priv->bootstrap_version = g_strdup (version);
+}
+
+gchar
+*zak_form_cgi_form_element_get_bootstrap_version (ZakFormCgiFormElement *element)
+{
+       ZakFormCgiFormElementPrivate *priv;
+
+       priv = ZAK_FORM_CGI_FORM_ELEMENT_GET_PRIVATE (element);
+
+       return g_strdup (priv->bootstrap_version);
+}
+
 /* PRIVATE */
 static void
 zak_form_cgi_form_element_construct (ZakFormCgiFormElement *element,
index 79085710218308e7e5c31ce2ba2fb3167fa1510a..c2a0eeb61a6eb3339b54973f62ee41aa54e63625 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -55,6 +55,9 @@ void zak_form_cgi_form_element_bind (ZakFormCgiFormElement *element, GValue *val
 gchar *zak_form_cgi_form_element_render (ZakFormCgiFormElement *element);
 gchar *zak_form_cgi_form_element_render_label (ZakFormCgiFormElement *element);
 
+void zak_form_cgi_form_element_set_bootstrap_version (ZakFormCgiFormElement *element, const gchar *version);
+gchar *zak_form_cgi_form_element_get_bootstrap_version (ZakFormCgiFormElement *element);
+
 
 G_END_DECLS
 
index 157b23bf24e999cb69de4cac7a4336c9987c9b7b..770a29234274fcba19ce81655ae93c5319db3712 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -266,12 +266,23 @@ static gchar
                        g_free (attr_class);
                }
 
-       g_string_append_printf (str, "\n%s<label%s>\n%s %s</label>%s",
-                               priv->in_line ? "" : "<div class=\"checkbox\">\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</div><br/>");
+       if (g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0)
+               {
+                       g_string_append_printf (str, "\n%s<label%s>\n%s %s</label>%s",
+                                               priv->in_line ? "" : "<div class=\"checkbox\">\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</div><br/>");
+               }
+       else
+               {
+                       g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s form-check-input", g_hash_table_lookup (ht_attrs, "class")));
+
+                       g_string_append_printf (str, "\n%s<label class=\"form-check-label\">%s</label>",
+                                               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)));
+               }
 
        if (ht_label_attrs != NULL)
                {
index 8b00088223740ac606bba34f4224d1962dd9641a..eaff505bc622487ab295778cbb4619595bf94269 100644 (file)
@@ -276,16 +276,31 @@ gchar
                        g_hash_table_insert (ht_attrs_option, (gpointer)"checked", (gpointer)"checked");
                }
 
-       g_string_append_printf (ret, "\n%s<label%s>\n%s %s</label>%s",
-                               priv->in_line ? "" : "<div class=\"radio\">\n",
-                               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),
-                                                                    idx + 1),
-                                                   ht_attrs_option),
-                               opt->content,
-                               priv->in_line ? "" : "\n</div>");
+       if (g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0)
+               {
+                       g_string_append_printf (ret, "\n%s<label%s>\n%s %s</label>%s",
+                                               priv->in_line ? "" : "<div class=\"radio\">\n",
+                                               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),
+                                                                                    idx + 1),
+                                                                   ht_attrs_option),
+                                               opt->content,
+                                               priv->in_line ? "" : "\n</div>");
+               }
+       else
+               {
+                       /* g_hash_table_insert (ht_attrs_option, "class", g_strdup_printf ("%s form-check-input", g_hash_table_lookup (ht_attrs_option, "class"))); */
+
+                       g_string_append_printf (ret, "\n%s<label class=\"form-check-label\">&nbsp;%s</label>",
+                                               zak_cgi_tag_tag_ht ("input",
+                                                                   g_strdup_printf ("%s_%d",
+                                                                                    zak_form_cgi_form_element_get_id (element),
+                                                                                    idx + 1),
+                                                                   ht_attrs_option),
+                                               opt->content);
+               }
 
        g_hash_table_unref (ht_attrs_option);
 
index 67f091d14a480ce3efc089d5dd079e41d73112b9..349333937756ba9cad35231d2fe48fa739a87399 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015-2017 Andrea Zagli <azagli@libero.it>
+ * Copyright (C) 2015-2020 Andrea Zagli <azagli@libero.it>
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -187,9 +187,16 @@ static gchar
                                        g_string_erase (str_attr_class, form_control - str_attr_class->str, 12);
                                }
 
-                       if (g_strstr_len (attr_class, -1, "btn btn-default") == NULL)
+                       if (g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0)
                                {
-                                       g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s btn btn-default", str_attr_class->str));
+                                       if (g_strstr_len (attr_class, -1, "btn btn-default") == NULL)
+                                               {
+                                                       g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s btn btn-default", str_attr_class->str));
+                                               }
+                               }
+                       else
+                               {
+                                       g_hash_table_insert (ht_attrs, "class", g_strdup_printf ("%s btn", str_attr_class->str));
                                }
 
                        g_free (attr_class);
@@ -197,7 +204,9 @@ static gchar
                }
        else
                {
-                       g_hash_table_replace (ht_attrs, g_strdup ("class"), g_strdup ("btn btn-default"));
+                       g_hash_table_replace (ht_attrs, g_strdup ("class"),
+                                             g_strdup_printf ("btn%s",
+                                                              g_strcmp0 (zak_form_cgi_form_element_get_bootstrap_version (element), "3") == 0 ? " btn-default" : ""));
                }
 
        ret = zak_cgi_tag_submit_ht (zak_form_cgi_form_element_get_id (element), ht_attrs);