1
0
Fork 0

Codechange #6060: Allow drawing dropdown lists with scrollbars above the widgets

pull/7076/head
Juanjo 2014-10-15 20:10:01 +02:00 committed by PeterN
parent 9d75600ac0
commit 226dbcb422
1 changed files with 31 additions and 31 deletions

View File

@ -352,8 +352,8 @@ void ShowDropDownListAt(Window *w, const DropDownList *list, int selected, int b
/* Longest item in the list, if auto_width is enabled */ /* Longest item in the list, if auto_width is enabled */
uint max_item_width = 0; uint max_item_width = 0;
/* Total length of list */ /* Total height of list */
int height = 0; uint height = 0;
for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) { for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) {
const DropDownListItem *item = *it; const DropDownListItem *item = *it;
@ -361,52 +361,52 @@ void ShowDropDownListAt(Window *w, const DropDownList *list, int selected, int b
if (auto_width) max_item_width = max(max_item_width, item->Width() + 5); if (auto_width) max_item_width = max(max_item_width, item->Width() + 5);
} }
/* Check if the status bar is visible, as we don't want to draw over it */ /* Scrollbar needed? */
int screen_bottom = GetMainViewBottom();
bool scroll = false; bool scroll = false;
/* Check if the dropdown will fully fit below the widget */ /* Is it better to place the dropdown above the widget? */
if (top + height + 4 >= screen_bottom) { bool above = false;
/* If not, check if it will fit above the widget */
int screen_top = GetMainViewTop();
if (w->top + wi_rect.top > screen_top + height) {
top = w->top + wi_rect.top - height - 4;
} else {
/* If it doesn't fit above the widget, we need to enable a scrollbar... */
int avg_height = height / (int)list->Length();
scroll = true;
/* ... and choose whether to put the list above or below the widget. */ /* Available height below (or above, if the dropdown is placed above the widget). */
bool put_above = false; uint available_height = (uint)max(GetMainViewBottom() - top - 4, 0);
int available_height = screen_bottom - w->top - wi_rect.bottom;
if (w->top + wi_rect.top - screen_top > available_height) { /* If the dropdown doesn't fully fit below the widget... */
// Put it above. if (height > available_height) {
available_height = w->top + wi_rect.top - screen_top;
put_above = true; uint available_height_above = (uint)max(w->top + wi_rect.top - GetMainViewTop() - 4, 0);
/* Put the dropdown above if there is more available space. */
if (available_height_above > available_height) {
above = true;
available_height = available_height_above;
} }
/* If the dropdown doesn't fully fit, we need a dropdown. */
if (height > available_height) {
scroll = true;
uint avg_height = height / list->Length();
/* Check at least there is space for one item. */ /* Check at least there is space for one item. */
assert(available_height >= avg_height); assert(available_height >= avg_height);
/* And lastly, fit the list... */ /* Fit the list. */
int rows = available_height / avg_height; uint rows = available_height / avg_height;
height = rows * avg_height; height = rows * avg_height;
/* Add space for the scroll bar if we automatically determined /* Add space for the scrollbar. */
* the width of the list. */
max_item_width += NWidgetScrollbar::GetVerticalDimension().width; max_item_width += NWidgetScrollbar::GetVerticalDimension().width;
/* ... and set the top position if needed. */
if (put_above) {
top = w->top + wi_rect.top - height - 4;
} }
/* Set the top position if needed. */
if (above) {
top = w->top + wi_rect.top - height - 4;
} }
} }
if (auto_width) width = max(width, max_item_width); if (auto_width) width = max(width, max_item_width);
Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - (int)width : wi_rect.left), top}; Point dw_pos = { w->left + (_current_text_dir == TD_RTL ? wi_rect.right + 1 - (int)width : wi_rect.left), top};
Dimension dw_size = {width, (uint)height}; Dimension dw_size = {width, height};
new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll); new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll);
} }