From b1823faf24501fce566fde182a5f56217c3b8dcc Mon Sep 17 00:00:00 2001 From: corvid Date: Sat, 20 Apr 2013 23:04:29 +0000 Subject: implement OPTGROUP in src/ I wrote nearly all of this last year, and I've just been going back over it now. As I recall, I initially also tried: select holds optgroups and options, and optgroups hold options (instead of this flat array of options, optgroups, and close-optgroups), but the code wasn't coming out any cleaner, so... --- src/form.cc | 206 ++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 158 insertions(+), 48 deletions(-) (limited to 'src/form.cc') diff --git a/src/form.cc b/src/form.cc index d9537036..10b4347f 100644 --- a/src/form.cc +++ b/src/form.cc @@ -36,7 +36,6 @@ using namespace dw::core::ui; class DilloHtmlReceiver; class DilloHtmlSelect; -class DilloHtmlOption; static Embed *Html_input_image(DilloHtml *html, const char *tag, int tagsize); @@ -166,29 +165,64 @@ public: void setEnabled(bool enabled) {if (embed) embed->setEnabled(enabled); }; }; -class DilloHtmlSelect { - friend class DilloHtmlInput; +class DilloHtmlOptbase +{ +public: + virtual ~DilloHtmlOptbase () {}; + virtual bool isSelected() {return false;} + virtual bool select() {return false;} + virtual const char *getValue() {return NULL;} + virtual void setContent(const char *str, int len) + {MSG_ERR("Form: Optbase setContent()\n");} + virtual void addSelf(SelectionResource *res) = 0; +}; + +class DilloHtmlOptgroup : public DilloHtmlOptbase { private: - lout::misc::SimpleVector *options; - DilloHtmlSelect (); - ~DilloHtmlSelect (); + char *label; + bool enabled; public: - DilloHtmlOption *getCurrentOption (); - void addOption (char *value, bool selected, bool enabled); - void ensureSelection (); - void addOptionsTo (SelectionResource *res); - void reset (SelectionResource *res); - void appendValuesTo (Dlist *values, SelectionResource *res); + DilloHtmlOptgroup (char *label, bool enabled); + virtual ~DilloHtmlOptgroup (); + void addSelf (SelectionResource *res) + {res->pushGroup(label, enabled);} +}; + +class DilloHtmlOptgroupClose : public DilloHtmlOptbase { +public: + virtual ~DilloHtmlOptgroupClose () {}; + void addSelf (SelectionResource *res) + {res->popGroup();} }; -class DilloHtmlOption { +class DilloHtmlOption : public DilloHtmlOptbase { friend class DilloHtmlSelect; public: char *value, *content; bool selected, enabled; -private: DilloHtmlOption (char *value, bool selected, bool enabled); - ~DilloHtmlOption (); + virtual ~DilloHtmlOption (); + bool isSelected() {return selected;} + bool select() {return (selected = true);} + const char *getValue() {return value ? value : content;} + void setContent(const char *str, int len) {content = dStrndup(str, len);} + void addSelf (SelectionResource *res) + {res->addItem(content, enabled, selected);} +}; + +class DilloHtmlSelect { + friend class DilloHtmlInput; +private: + lout::misc::SimpleVector *opts; + DilloHtmlSelect (); + ~DilloHtmlSelect (); +public: + DilloHtmlOptbase *getCurrentOpt (); + void addOpt (DilloHtmlOptbase *opt); + void ensureSelection (); + void addOptsTo (SelectionResource *res); + void reset (SelectionResource *res); + void appendValuesTo (Dlist *values, SelectionResource *res); }; /* @@ -767,7 +801,66 @@ void Html_tag_close_select(DilloHtml *html) select->ensureSelection (); } SelectionResource *res = (SelectionResource*)input->embed->getResource(); - select->addOptionsTo (res); + select->addOptsTo (res); + } +} + +void Html_tag_open_optgroup(DilloHtml *html, const char *tag, int tagsize) +{ + MSG("OPEN OPTGROUP\n"); + + if (!(html->InFlags & IN_SELECT)) { + BUG_MSG(" element outside