diff options
author | alberth <alberth@openttd.org> | 2009-11-07 11:44:38 +0000 |
---|---|---|
committer | alberth <alberth@openttd.org> | 2009-11-07 11:44:38 +0000 |
commit | d03bf874fcf50f5051e43f973a39198573358ea6 (patch) | |
tree | 9b18009557ae2114d811b0ba8c917b7cdf6290e4 /src | |
parent | 81796f10e54cb5a0bec73e55992dbef57f286349 (diff) | |
download | openttd-d03bf874fcf50f5051e43f973a39198573358ea6.tar.xz |
(svn r17993) -Codechange: Move widget and data initialization into the dropdown menu class.
Diffstat (limited to 'src')
-rw-r--r-- | src/widgets/dropdown.cpp | 88 |
1 files changed, 51 insertions, 37 deletions
diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index 15aec16b5..43550ef36 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -101,13 +101,59 @@ struct DropdownWindow : Window { int scrolling; ///< If non-zero, auto-scroll the item list (one time). /** Create a dropdown menu. - * @param pos Topleft position of the dropdown menu window. - * @param size Size of the dropdown menu window. - * @param widget Widgets of the dropdown menu window. + * @param parent Parent window. + * @param list Dropdown item list. + * @param selected Index of the selected item in the list. + * @param button Widget of the parent window doing the dropdown. + * @param instant_close ??? + * @param pos Topleft position of the dropdown menu window. + * @param size Size of the dropdown menu window. + * @param wi_colour Colour of the parent widget. + * @param scroll Dropdown menu has a scrollbar. + * @param widget Widgets of the dropdown menu window. */ - DropdownWindow(const Point &pos, const Dimension &size, const Widget *widget) : Window(pos.x, pos.y, size.width, size.height + 4, WC_DROPDOWN_MENU, widget) + DropdownWindow(Window *parent, DropDownList *list, int selected, int button, bool instant_close, const Point &pos, const Dimension &size, Colours wi_colour, bool scroll, const Widget *widget) : + Window(pos.x, pos.y, size.width, size.height + 4, WC_DROPDOWN_MENU, widget) { this->FindWindowPlacementAndResize(size.width, size.height + 4); + + this->widget[DDM_ITEMS].colour = wi_colour; + this->widget[DDM_ITEMS].right = size.width - 1; + this->widget[DDM_ITEMS].bottom = size.height + 3; + + this->SetWidgetHiddenState(DDM_SCROLL, !scroll); + if (scroll) { + /* We're scrolling, so enable the scroll bar and shrink the list by + * the scrollbar's width */ + this->widget[DDM_SCROLL].colour = wi_colour; + this->widget[DDM_SCROLL].right = this->widget[DDM_ITEMS].right; + this->widget[DDM_SCROLL].left = this->widget[DDM_SCROLL].right - (WD_VSCROLLBAR_WIDTH - 1); + this->widget[DDM_SCROLL].bottom = this->widget[DDM_ITEMS].bottom; + this->widget[DDM_ITEMS].right -= WD_VSCROLLBAR_WIDTH; + + /* Total length of list */ + int list_height = 0; + for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) { + DropDownListItem *item = *it; + list_height += item->Height(size.width - WD_VSCROLLBAR_WIDTH); + } + + /* Capacity is the average number of items visible */ + this->vscroll.SetCapacity(size.height * (uint16)list->size() / list_height); + this->vscroll.SetCount((uint16)list->size()); + } + + this->desc_flags = WDF_DEF_WIDGET; + this->flags4 &= ~WF_WHITE_BORDER_MASK; + + this->parent_wnd_class = parent->window_class; + this->parent_wnd_num = parent->window_number; + this->parent_button = button; + this->list = list; + this->selected_index = selected; + this->click_delay = 0; + this->drag_mode = true; + this->instant_close = instant_close; } ~DropdownWindow() @@ -359,39 +405,7 @@ void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, u _dropdown_menu_widgets, &generated_dropdown_menu_widgets); Point dw_pos = {w->left + wi_rect.left, top}; Dimension dw_size = {width, height}; - DropdownWindow *dw = new DropdownWindow(dw_pos, dw_size, wid); - - dw->widget[DDM_ITEMS].colour = wi_colour; - dw->widget[DDM_ITEMS].right = width - 1; - dw->widget[DDM_ITEMS].bottom = height + 3; - - dw->SetWidgetHiddenState(DDM_SCROLL, !scroll); - - if (scroll) { - /* We're scrolling, so enable the scroll bar and shrink the list by - * the scrollbar's width */ - dw->widget[DDM_SCROLL].colour = wi_colour; - dw->widget[DDM_SCROLL].right = dw->widget[DDM_ITEMS].right; - dw->widget[DDM_SCROLL].left = dw->widget[DDM_SCROLL].right - (WD_VSCROLLBAR_WIDTH - 1); - dw->widget[DDM_SCROLL].bottom = dw->widget[DDM_ITEMS].bottom; - dw->widget[DDM_ITEMS].right -= WD_VSCROLLBAR_WIDTH; - - /* Capacity is the average number of items visible */ - dw->vscroll.SetCapacity(height * (uint16)list->size() / list_height); - dw->vscroll.SetCount((uint16)list->size()); - } - - dw->desc_flags = WDF_DEF_WIDGET; - dw->flags4 &= ~WF_WHITE_BORDER_MASK; - - dw->parent_wnd_class = w->window_class; - dw->parent_wnd_num = w->window_number; - dw->parent_button = button; - dw->list = list; - dw->selected_index = selected; - dw->click_delay = 0; - dw->drag_mode = true; - dw->instant_close = instant_close; + new DropdownWindow(w, list, selected, button, instant_close, dw_pos, dw_size, wi_colour, scroll, wid); } /** Show a dropdown menu window near a widget of the parent window. |