[patch] fix size of select widgets
Here is a small patch to compute the size of select widgets similar to the way it is computed for text input widgets. The width is hardcoded to 10 * size of 'M' in the current font. Regards, Johannes PS: The colored widgets are nice! diff -r 68ee0a55704c dw/fltkui.cc --- a/dw/fltkui.cc Sat Feb 02 18:36:32 2008 +0100 +++ b/dw/fltkui.cc Sat Feb 02 19:20:16 2008 +0100 @@ -1048,10 +1048,21 @@ void FltkOptionMenuResource::widgetCallb void FltkOptionMenuResource::sizeRequest (core::Requisition *requisition) { - /** \bug Random values. */ - requisition->width = 100; - requisition->ascent = 18; - requisition->descent = 5; + if (style) { + FltkFont *font = (FltkFont*)style->font; + ::fltk::setfont(font->font,font->size); + requisition->width = + (int)::fltk::getwidth ("M", 1) * 10 + + 2 * RELIEF_X_THICKNESS; + requisition->ascent = + font->ascent + RELIEF_Y_THICKNESS; + requisition->descent = + font->descent + RELIEF_Y_THICKNESS; + } else { + requisition->width = 1; + requisition->ascent = 1; + requisition->descent = 0; + } }
On Sat, Feb 02, 2008 at 07:27:20PM +0100, Johannes Hofmann wrote:
Here is a small patch to compute the size of select widgets similar to the way it is computed for text input widgets. The width is hardcoded to 10 * size of 'M' in the current font.
OK. It'd be good to use the length of the longest string (as dillo1 and FF). -- Cheers Jorge.-
On Sat, Feb 02, 2008 at 07:27:20PM +0100, Johannes Hofmann wrote:
Here is a small patch to compute the size of select widgets similar to the way it is computed for text input widgets. The width is hardcoded to 10 * size of 'M' in the current font.
I committed a different version that measures the string length (as dillo1 and FF). You can test with: http://www.dillo.org/bugtrack/Dinsert.html (adding a long string to a local copy is useful). I doubt that's the right way to do it, but at least it works. Please consider it experimental and give it a look. -- Cheers Jorge.-
Hi Jorge, On Sat, Feb 02, 2008 at 07:23:17PM -0300, Jorge Arellano Cid wrote:
On Sat, Feb 02, 2008 at 07:27:20PM +0100, Johannes Hofmann wrote:
Here is a small patch to compute the size of select widgets similar to the way it is computed for text input widgets. The width is hardcoded to 10 * size of 'M' in the current font.
I committed a different version that measures the string length (as dillo1 and FF). You can test with:
http://www.dillo.org/bugtrack/Dinsert.html
(adding a long string to a local copy is useful).
I doubt that's the right way to do it, but at least it works. Please consider it experimental and give it a look.
I don't think it is valid to compute the size in addItem(). The style may change later, especially in the future with CSS. I would propose to do the size computation in sizeRequest(), even if that looks inefficient (patch below). That way it is consistent with e.g. FltkLabelButtonResource. While at it, I replaced the hardcoded 20 for the small rectangle on the right with what FLTK uses for the width of the rectangle. You can see a difference with e.g: <html> <body> <form> <select> <option>foo</option> <option>bar</option> </select> <h1> <select> <option>foo</option> <option>bar</option> </select> </h1> </form> </body> </html> diff -r 1c41f9a73971 dw/fltkui.cc --- a/dw/fltkui.cc Sun Feb 03 21:13:11 2008 +0100 +++ b/dw/fltkui.cc Mon Feb 04 16:13:00 2008 +0100 @@ -1021,7 +1021,6 @@ FltkOptionMenuResource::FltkOptionMenuRe { init (platform); selection = NULL; - maxStringWidth = 0; } FltkOptionMenuResource::~FltkOptionMenuResource () @@ -1052,9 +1051,20 @@ void FltkOptionMenuResource::sizeRequest if (style) { FltkFont *font = (FltkFont*)style->font; ::fltk::setfont(font->font,font->size); - requisition->width = maxStringWidth + 20 + 2 * RELIEF_X_THICKNESS; + + int maxStringWidth = 0; + for (int i = 0; i < getNumberOfItems (); i++) { + int len = (int)::fltk::getwidth (getItem(i)); + if (len > maxStringWidth) { + maxStringWidth = len; + } + } + requisition->ascent = font->ascent + RELIEF_Y_THICKNESS; requisition->descent = font->descent + RELIEF_Y_THICKNESS; + requisition->width = maxStringWidth + + (requisition->ascent + requisition->descent) * 4 / 5 + + 2 * RELIEF_X_THICKNESS; } else { requisition->width = 1; requisition->ascent = 1; @@ -1065,20 +1075,12 @@ void FltkOptionMenuResource::addItem (co void FltkOptionMenuResource::addItem (const char *str, bool enabled, bool selected) { - if (str) { - FltkFont *font = (FltkFont*)style->font; - ::fltk::setfont(font->font,font->size); - int len = (int)::fltk::getwidth (str); - if (len > maxStringWidth) { - maxStringWidth = len; - queueResize (true); - } - } - FltkSelectionResource<dw::core::ui::OptionMenuResource>::addItem (str,enabled,selected); if (selected) selection = items->get (items->size()-1); + + queueResize (true); } bool FltkOptionMenuResource::isSelected (int index) diff -r 1c41f9a73971 dw/fltkui.hh --- a/dw/fltkui.hh Sun Feb 03 21:13:11 2008 +0100 +++ b/dw/fltkui.hh Mon Feb 04 16:13:00 2008 +0100 @@ -507,7 +507,6 @@ private: private: static void widgetCallback (::fltk::Widget *widget, void *data); void *selection; - int maxStringWidth; public: FltkOptionMenuResource (FltkPlatform *platform);
-- Cheers Jorge.-
_______________________________________________ Dillo-dev mailing list Dillo-dev@dillo.org http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/dillo-dev
Hi Johannes, On Mon, Feb 04, 2008 at 04:19:45PM +0100, Johannes Hofmann wrote:
Hi Jorge,
On Sat, Feb 02, 2008 at 07:23:17PM -0300, Jorge Arellano Cid wrote:
On Sat, Feb 02, 2008 at 07:27:20PM +0100, Johannes Hofmann wrote:
Here is a small patch to compute the size of select widgets similar to the way it is computed for text input widgets. The width is hardcoded to 10 * size of 'M' in the current font.
I committed a different version that measures the string length (as dillo1 and FF). You can test with:
http://www.dillo.org/bugtrack/Dinsert.html
(adding a long string to a local copy is useful).
I doubt that's the right way to do it, but at least it works. Please consider it experimental and give it a look.
I don't think it is valid to compute the size in addItem(). The style may change later, especially in the future with CSS. I would propose to do the size computation in sizeRequest(), even if that looks inefficient (patch below). That way it is consistent with e.g. FltkLabelButtonResource.
Committed with the following comment: // Looks like O(n^2) but the loop is executed one time -> O(n). :-)
While at it, I replaced the hardcoded 20 for the small rectangle on the right with what FLTK uses for the width of the rectangle.
Good. -- Cheers Jorge.-
participants (2)
-
jcid@dillo.org
-
Johannes.Hofmann@gmx.de