src/widgets/dropdown.cpp
changeset 9868 c529be2a15c5
parent 9859 28606a486c52
child 9869 548d99e11ad7
equal deleted inserted replaced
9867:f5d0c061f048 9868:c529be2a15c5
    22 	int c1 = _colour_gradient[bg_colour][3];
    22 	int c1 = _colour_gradient[bg_colour][3];
    23 	int c2 = _colour_gradient[bg_colour][7];
    23 	int c2 = _colour_gradient[bg_colour][7];
    24 
    24 
    25 	GfxFillRect(x + 1, y + 3, x + width - 2, y + 3, c1);
    25 	GfxFillRect(x + 1, y + 3, x + width - 2, y + 3, c1);
    26 	GfxFillRect(x + 1, y + 4, x + width - 2, y + 4, c2);
    26 	GfxFillRect(x + 1, y + 4, x + width - 2, y + 4, c2);
       
    27 }
       
    28 
       
    29 uint DropDownListStringItem::Width() const
       
    30 {
       
    31 	char buffer[512];
       
    32 	GetString(buffer, this->String(), lastof(buffer));
       
    33 	return GetStringBoundingBox(buffer).width;
    27 }
    34 }
    28 
    35 
    29 void DropDownListStringItem::Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
    36 void DropDownListStringItem::Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
    30 {
    37 {
    31 	DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, width);
    38 	DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, width);
   230 	const Widget *wi = &w->widget[button];
   237 	const Widget *wi = &w->widget[button];
   231 
   238 
   232 	/* The preferred position is just below the dropdown calling widget */
   239 	/* The preferred position is just below the dropdown calling widget */
   233 	int top = w->top + wi->bottom + 1;
   240 	int top = w->top + wi->bottom + 1;
   234 
   241 
       
   242 	bool auto_width = (width == UINT_MAX);
       
   243 
       
   244 	if (auto_width) {
       
   245 		/* Find the longest item in the list */
       
   246 		width = 0;
       
   247 		for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
       
   248 			const DropDownListItem *item = *it;
       
   249 			width = max(width, item->Width() + 5);
       
   250 		}
       
   251 	} else if (width == 0) {
       
   252 		width = wi->right - wi->left + 1;
       
   253 	}
       
   254 
   235 	/* Total length of list */
   255 	/* Total length of list */
   236 	int list_height = 0;
   256 	int list_height = 0;
   237 
   257 
   238 	for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
   258 	for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
   239 		DropDownListItem *item = *it;
   259 		DropDownListItem *item = *it;
   262 			 * list in below the widget */
   282 			 * list in below the widget */
   263 			int avg_height = list_height / (int)list->size();
   283 			int avg_height = list_height / (int)list->size();
   264 			int rows = (screen_bottom - 4 - top) / avg_height;
   284 			int rows = (screen_bottom - 4 - top) / avg_height;
   265 			height = rows * avg_height;
   285 			height = rows * avg_height;
   266 			scroll = true;
   286 			scroll = true;
   267 		}
   287 			/* Add space for the scroll bar if we automatically determined
   268 	}
   288 			 * the width of the list. */
   269 
   289 			if (auto_width) width += 12;
   270 	if (width == 0) width = wi->right - wi->left + 1;
   290 		}
       
   291 	}
   271 
   292 
   272 	DropdownWindow *dw = new DropdownWindow(
   293 	DropdownWindow *dw = new DropdownWindow(
   273 		w->left + wi->left,
   294 		w->left + wi->left,
   274 		top,
   295 		top,
   275 		width,
   296 		width,