summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPatrick McDermott <pj@pehjota.net>2017-10-07 13:16:50 (EDT)
committer Patrick McDermott <pj@pehjota.net>2017-10-07 13:27:34 (EDT)
commit8a4d88a3ce54ecd6692d66733540fe3dfa957147 (patch)
tree527c04e4e2e5f96edf2daa37398b730ed3dbed34 /src
parent22a2895178fcd23cb72ce62db9297bb25f9d09a9 (diff)
downloadmarquee-8a4d88a3ce54ecd6692d66733540fe3dfa957147.zip
marquee-8a4d88a3ce54ecd6692d66733540fe3dfa957147.tar.gz
marquee-8a4d88a3ce54ecd6692d66733540fe3dfa957147.tar.bz2
mq_html_input_select(): New function
Diffstat (limited to 'src')
-rw-r--r--src/html.c75
-rw-r--r--src/html.h5
2 files changed, 80 insertions, 0 deletions
diff --git a/src/html.c b/src/html.c
index fae9dca..e8f8e4c 100644
--- a/src/html.c
+++ b/src/html.c
@@ -604,3 +604,78 @@ mq_html_input_checkbox(const gchar *name, const gchar *label, gboolean checked)
name, label, name, name, name,
checked ? " checked=\"checked\"" : "");
}
+
+gchar *
+mq_html_input_select(const gchar *name, const gchar *label,
+ const gchar *selected,
+ GDestroyNotify destroy_value, GDestroyNotify destroy_label, ...)
+{
+ gsize len;
+ va_list ap;
+ gchar *option_value;
+ gchar *option_label;
+ gchar *select;
+ gchar *ptr;
+
+ g_assert(selected && selected[0]); /* Would cause buffer overflow */
+
+ /* Calculate length. */
+ len = strlen("<label for=\"");
+ len += strlen(name);
+ len += strlen("\">\n<span>");
+ len += strlen(label);
+ len += strlen("</span>\n<select name=\"");
+ len += strlen(name);
+ len += strlen("\" id=\"");
+ len += strlen(name);
+ len += strlen("\">\n");
+ va_start(ap, destroy_label);
+ while ((option_value = va_arg(ap, gchar *)) && (option_label =
+ va_arg(ap, gchar *))) {
+ len += strlen("<option value=\"");
+ len += strlen(option_value);
+ len += strlen("\"");
+ len += strlen(">");
+ len += strlen(option_label);
+ len += strlen("</option>\n");
+ }
+ va_end(ap);
+ len += strlen(" selected=\"selected\"");
+ len += strlen("</select>\n</label>\n");
+ ++len; /* NUL byte */
+
+ /* Build string. */
+ select = g_new(gchar, len);
+ ptr = g_stpcpy(select, "<label for=\"");
+ ptr = g_stpcpy(ptr, name);
+ ptr = g_stpcpy(ptr, "\">\n<span>");
+ ptr = g_stpcpy(ptr, label);
+ ptr = g_stpcpy(ptr, "</span>\n<select name=\"");
+ ptr = g_stpcpy(ptr, name);
+ ptr = g_stpcpy(ptr, "\" id=\"");
+ ptr = g_stpcpy(ptr, name);
+ ptr = g_stpcpy(ptr, "\">\n");
+ va_start(ap, destroy_label);
+ while ((option_value = va_arg(ap, gchar *)) && (option_label =
+ va_arg(ap, gchar *))) {
+ ptr = g_stpcpy(ptr, "<option value=\"");
+ ptr = g_stpcpy(ptr, option_value);
+ ptr = g_stpcpy(ptr, "\"");
+ if (g_strcmp0(option_value, selected) == 0) {
+ ptr = g_stpcpy(ptr, " selected=\"selected\"");
+ }
+ ptr = g_stpcpy(ptr, ">");
+ ptr = g_stpcpy(ptr, option_label);
+ if (destroy_value) {
+ destroy_value(option_value);
+ }
+ if (destroy_label) {
+ destroy_label(option_label);
+ }
+ ptr = g_stpcpy(ptr, "</option>\n");
+ }
+ va_end(ap);
+ ptr = g_stpcpy(ptr, "</select>\n</label>\n"); /* g_stpcpy() adds NUL */
+
+ return select;
+}
diff --git a/src/html.h b/src/html.h
index 24deb8a..6fe35a8 100644
--- a/src/html.h
+++ b/src/html.h
@@ -71,4 +71,9 @@ mq_html_input_radio(const gchar *name, const gchar *label, gboolean checked);
gchar *
mq_html_input_checkbox(const gchar *name, const gchar *label, gboolean checked);
+gchar *
+mq_html_input_select(const gchar *name, const gchar *label,
+ const gchar *selected,
+ GDestroyNotify destroy_value, GDestroyNotify destroy_label, ...);
+
#endif