changeset 903:4d76ce512830

Implemented size for SELECT lists
author corvid <corvid@lavabit.com>
date Fri, 06 Feb 2009 12:16:28 -0300
parents f44e88b06200
children df01aab1a390
files dw/fltkplatform.cc dw/fltkplatform.hh dw/fltkui.cc dw/fltkui.hh dw/ui.hh src/form.cc test/dw_resource_test.cc test/dw_ui_test.cc
diffstat 8 files changed, 53 insertions(+), 47 deletions(-) [+]
line wrap: on
line diff
--- a/dw/fltkplatform.cc	Fri Feb 06 12:36:31 2009 +0100
+++ b/dw/fltkplatform.cc	Fri Feb 06 12:16:28 2009 -0300
@@ -182,9 +182,9 @@
 FltkPlatform::FltkResourceFactory::createListResource (core::ui
                                                        ::ListResource
                                                        ::SelectionMode
-                                                       selectionMode)
+                                                       selectionMode, int rows)
 {
-   return new ui::FltkListResource (platform, selectionMode);
+   return new ui::FltkListResource (platform, selectionMode, rows);
 }
 
 core::ui::OptionMenuResource *
--- a/dw/fltkplatform.hh	Fri Feb 06 12:36:31 2009 +0100
+++ b/dw/fltkplatform.hh	Fri Feb 06 12:16:28 2009 -0300
@@ -78,7 +78,8 @@
       core::ui::ComplexButtonResource *
       createComplexButtonResource (core::Widget *widget, bool relief);
       core::ui::ListResource *
-      createListResource (core::ui::ListResource::SelectionMode selectionMode);
+      createListResource (core::ui::ListResource::SelectionMode selectionMode,
+                          int rows);
       core::ui::OptionMenuResource *createOptionMenuResource ();
       core::ui::EntryResource *createEntryResource (int maxLength,
                                                     bool password,
--- a/dw/fltkui.cc	Fri Feb 06 12:36:31 2009 +0100
+++ b/dw/fltkui.cc	Fri Feb 06 12:16:28 2009 -0300
@@ -36,7 +36,6 @@
 #include <fltk/CheckButton.h>
 #include <fltk/Choice.h>
 #include <fltk/Browser.h>
-#include <fltk/MultiBrowser.h>
 #include <fltk/Font.h>
 #include <fltk/draw.h>
 #include <fltk/Symbol.h>
@@ -1226,10 +1225,12 @@
 
 FltkListResource::FltkListResource (FltkPlatform *platform,
                                     core::ui::ListResource::SelectionMode
-                                    selectionMode):
+                                    selectionMode, int rowCount):
    FltkSelectionResource <dw::core::ui::ListResource> (platform),
    itemsSelected(8)
 {
+   mode = selectionMode;
+   showRows = rowCount;
    init (platform);
 }
 
@@ -1241,9 +1242,10 @@
 ::fltk::Menu *FltkListResource::createNewMenu (core::Allocation *allocation)
 {
    ::fltk::Menu *menu =
-      new ::fltk::MultiBrowser (allocation->x, allocation->y,
-                                allocation->width,
-                                allocation->ascent + allocation->descent);
+      new ::fltk::Browser (allocation->x, allocation->y, allocation->width,
+                           allocation->ascent + allocation->descent);
+   if (mode == SELECTION_MULTIPLE)
+      menu->type(::fltk::Browser::MULTI);
    menu->set_flag (::fltk::RAW_LABEL);
    menu->callback(widgetCallback,this);
    menu->when(::fltk::WHEN_CHANGED);
@@ -1253,7 +1255,9 @@
 void FltkListResource::widgetCallback (::fltk::Widget *widget, void *data)
 {
    ::fltk::Widget *fltkItem = ((::fltk::Menu *) widget)->item ();
-   int index = (long) (fltkItem->user_data ());
+   int index = -1;
+   if (fltkItem)
+      index = (long) (fltkItem->user_data ());
    if (index > -1) {
       bool selected = fltkItem->selected ();
       ((FltkListResource *) data)->itemsSelected.set (index, selected);
@@ -1275,13 +1279,19 @@
    if (style) {
       FltkFont *font = (FltkFont*)style->font;
       ::fltk::setfont(font->font,font->size);
-      int maxStringWidth = getMaxStringWidth ();
-      requisition->ascent = font->ascent + RELIEF_Y_THICKNESS;
-      requisition->descent =
-         (getNumberOfItems () - 1) * (font->ascent) + font->descent;
-      requisition->width = maxStringWidth
-         + (font->ascent + font->descent) * 4 / 5
-         + 2 * RELIEF_X_THICKNESS;
+      int rows = getNumberOfItems();
+      if (showRows < rows) {
+         rows = showRows;
+      }
+      /*
+       * The widget sometimes shows scrollbars when they are not required.
+       * The following values try to keep any scrollbars from obscuring
+       * options, at the cost of showing too much whitespace at times.
+       */
+      requisition->width = getMaxStringWidth() + 24;
+      requisition->ascent = font->ascent + 2;
+      requisition->descent = font->descent + 3 +
+                             (rows - 1) * (font->ascent + font->descent + 1);
    } else {
       requisition->width = 1;
       requisition->ascent = 1;
--- a/dw/fltkui.hh	Fri Feb 06 12:36:31 2009 +0100
+++ b/dw/fltkui.hh	Fri Feb 06 12:16:28 2009 -0300
@@ -538,10 +538,12 @@
 private:
    static void widgetCallback (::fltk::Widget *widget, void *data);
    misc::SimpleVector <bool> itemsSelected;
-
+   int showRows;
+   ListResource::SelectionMode mode;
 public:
    FltkListResource (FltkPlatform *platform,
-                     core::ui::ListResource::SelectionMode selectionMode);
+                     core::ui::ListResource::SelectionMode selectionMode,
+                     int rows);
    ~FltkListResource ();
 
    void addItem (const char *str, bool enabled, bool selected);
--- a/dw/ui.hh	Fri Feb 06 12:36:31 2009 +0100
+++ b/dw/ui.hh	Fri Feb 06 12:16:28 2009 -0300
@@ -539,7 +539,7 @@
                                                                bool relief)
       = 0;
    virtual ListResource *createListResource (ListResource::SelectionMode
-                                             selectionMode) = 0;
+                                             selectionMode, int rows) = 0;
    virtual OptionMenuResource *createOptionMenuResource () = 0;
    virtual EntryResource *createEntryResource (int maxLength, bool password,
                                                const char *label) = 0;
--- a/src/form.cc	Fri Feb 06 12:36:31 2009 +0100
+++ b/src/form.cc	Fri Feb 06 12:16:28 2009 -0300
@@ -713,8 +713,8 @@
 /* The select tag is quite tricky, because of gorpy html syntax. */
 void Html_tag_open_select(DilloHtml *html, const char *tag, int tagsize)
 {
-// const char *attrbuf;
-// int size, type, multi;
+   const char *attrbuf;
+   int rows = 0;
 
    if (html->InFlags & IN_SELECT) {
       BUG_MSG("nested <select>\n");
@@ -727,12 +727,25 @@
    ResourceFactory *factory = HT2LT(html)->getResourceFactory ();
    DilloHtmlInputType type;
    SelectionResource *res;
-   if (a_Html_get_attr(html, tag, tagsize, "multiple")) {
-      type = DILLO_HTML_INPUT_SEL_LIST;
-      res = factory->createListResource (ListResource::SELECTION_MULTIPLE);
-   } else {
+   bool multi = a_Html_get_attr(html, tag, tagsize, "multiple") != NULL;
+
+   if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "size"))) {
+      rows = strtol(attrbuf, NULL, 10);
+      if (rows > 100)
+         rows = 100;
+   }
+   if (rows < 1)
+      rows = multi ? 10 : 1;
+
+   if (rows == 1 && multi == false) {
       type = DILLO_HTML_INPUT_SELECT;
       res = factory->createOptionMenuResource ();
+   } else {
+      type = DILLO_HTML_INPUT_SEL_LIST;
+      res = factory->createListResource (multi ?
+                                         ListResource::SELECTION_MULTIPLE :
+                                         ListResource::SELECTION_EXACTLY_ONE,
+                                         rows);
    }
    Embed *embed = new Embed(res);
 
@@ -748,26 +761,6 @@
                      Color::createShaded (HT2LT(html), bg));
    DW2TB(html->dw)->addWidget (embed, html->styleEngine->style ());
 
-// size = 0;
-// if ((attrbuf = a_Html_get_attr(html, tag, tagsize, "size")))
-//    size = strtol(attrbuf, NULL, 10);
-//
-// multi = (a_Html_get_attr(html, tag, tagsize, "multiple")) ? 1 : 0;
-// if (size < 1)
-//    size = multi ? 10 : 1;
-//
-// if (size == 1) {
-//    menu = gtk_menu_new();
-//    widget = gtk_option_menu_new();
-//    type = DILLO_HTML_INPUT_SELECT;
-// } else {
-//    menu = gtk_list_new();
-//    widget = menu;
-//    if (multi)
-//       gtk_list_set_selection_mode(GTK_LIST(menu), GTK_SELECTION_MULTIPLE);
-//    type = DILLO_HTML_INPUT_SEL_LIST;
-// }
-
    Html_add_input(html, type, embed, name, NULL, false);
    a_Html_stash_init(html);
    dFree(name);
--- a/test/dw_resource_test.cc	Fri Feb 06 12:36:31 2009 +0100
+++ b/test/dw_resource_test.cc	Fri Feb 06 12:16:28 2009 -0300
@@ -72,7 +72,7 @@
    styleAttrs.backgroundColor = NULL;
 
    SelectionResource *res = layout->getResourceFactory()->createListResource
-      (ListResource::SELECTION_AT_MOST_ONE);
+      (ListResource::SELECTION_AT_MOST_ONE, 4);
    //SelectionResource *res =
    //   layout->getResourceFactory()->createOptionMenuResource ();
    
--- a/test/dw_ui_test.cc	Fri Feb 06 12:36:31 2009 +0100
+++ b/test/dw_ui_test.cc	Fri Feb 06 12:16:28 2009 -0300
@@ -95,7 +95,7 @@
    SelectionResource *selres[2];
    selres[0] = layout->getResourceFactory()->createOptionMenuResource ();
    selres[1] = layout->getResourceFactory()->createListResource
-      (ListResource::SELECTION_AT_MOST_ONE);
+      (ListResource::SELECTION_AT_MOST_ONE, 4);
    LabelButtonResource *buttonres =
       layout->getResourceFactory()->createLabelButtonResource ("Run!");