diff options
author | rubidium <rubidium@openttd.org> | 2013-11-24 14:46:26 +0000 |
---|---|---|
committer | rubidium <rubidium@openttd.org> | 2013-11-24 14:46:26 +0000 |
commit | 83eeba28b7272a870f777f2f21bfc1def36e873f (patch) | |
tree | 592e23a29d5235307e2984cbd8220d0cd3a1ec76 /src/widgets | |
parent | db894b0b3fa13413cdb76989cfcd2bb789243b77 (diff) | |
download | openttd-83eeba28b7272a870f777f2f21bfc1def36e873f.tar.xz |
(svn r26086) -Codechange: use AutoDeleteSmallVector instead std::list for dropdowns
Diffstat (limited to 'src/widgets')
-rw-r--r-- | src/widgets/dropdown.cpp | 57 | ||||
-rw-r--r-- | src/widgets/dropdown_type.h | 10 |
2 files changed, 27 insertions, 40 deletions
diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp index fd9d765e8..400514822 100644 --- a/src/widgets/dropdown.cpp +++ b/src/widgets/dropdown.cpp @@ -48,12 +48,12 @@ void DropDownListStringItem::Draw(int left, int right, int top, int bottom, bool * @return true if \a first precedes \a second. * @warning All items in the list need to be derivates of DropDownListStringItem. */ -/* static */ bool DropDownListStringItem::NatSortFunc(const DropDownListItem *first, const DropDownListItem *second) +/* static */ int DropDownListStringItem::NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const * second) { char buffer1[512], buffer2[512]; - GetString(buffer1, static_cast<const DropDownListStringItem*>(first)->String(), lastof(buffer1)); - GetString(buffer2, static_cast<const DropDownListStringItem*>(second)->String(), lastof(buffer2)); - return strnatcmp(buffer1, buffer2) < 0; + GetString(buffer1, static_cast<const DropDownListStringItem*>(*first)->String(), lastof(buffer1)); + GetString(buffer2, static_cast<const DropDownListStringItem*>(*second)->String(), lastof(buffer2)); + return strnatcmp(buffer1, buffer2); } StringID DropDownListParamStringItem::String() const @@ -68,19 +68,6 @@ StringID DropDownListCharStringItem::String() const return this->string; } -/** - * Delete all items of a drop down list and the list itself - * @param list List to delete. - */ -static void DeleteDropDownList(DropDownList *list) -{ - for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) { - DropDownListItem *item = *it; - delete item; - } - delete list; -} - static const NWidgetPart _nested_dropdown_menu_widgets[] = { NWidget(NWID_HORIZONTAL), NWidget(WWT_PANEL, COLOUR_END, WID_DM_ITEMS), SetMinimalSize(1, 1), SetScrollbar(WID_DM_SCROLL), EndContainer(), @@ -102,7 +89,7 @@ struct DropdownWindow : Window { WindowClass parent_wnd_class; ///< Parent window class. WindowNumber parent_wnd_num; ///< Parent window number. int parent_button; ///< Parent widget number where the window is dropped from. - DropDownList *list; ///< List with dropdown menu items. + const DropDownList *list; ///< List with dropdown menu items. int selected_index; ///< Index of the selected item in the list. byte click_delay; ///< Timer to delay selection. bool drag_mode; @@ -124,7 +111,7 @@ struct DropdownWindow : Window { * @param scroll Dropdown menu has a scrollbar. * @param widget Widgets of the dropdown menu window. */ - DropdownWindow(Window *parent, DropDownList *list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll) + DropdownWindow(Window *parent, const DropDownList *list, int selected, int button, bool instant_close, const Point &position, const Dimension &size, Colours wi_colour, bool scroll) : Window(&_dropdown_desc) { this->position = position; @@ -148,14 +135,14 @@ struct DropdownWindow : Window { /* Total length of list */ int list_height = 0; - for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) { - DropDownListItem *item = *it; + for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) { + const DropDownListItem *item = *it; list_height += item->Height(items_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->vscroll->SetCapacity(size.height * (uint16)list->Length() / list_height); + this->vscroll->SetCount((uint16)list->Length()); this->parent_wnd_class = parent->window_class; this->parent_wnd_num = parent->window_number; @@ -181,7 +168,7 @@ struct DropdownWindow : Window { pt.y -= w2->top; w2->OnDropdownClose(pt, this->parent_button, this->selected_index, this->instant_close); } - DeleteDropDownList(this->list); + delete this->list; } virtual Point OnInitialPosition(int16 sm_width, int16 sm_height, int window_number) @@ -205,7 +192,7 @@ struct DropdownWindow : Window { const DropDownList *list = this->list; - for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) { + for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) { /* Skip items that are scrolled up */ if (--pos >= 0) continue; @@ -232,7 +219,7 @@ struct DropdownWindow : Window { int y = r.top + 2; int pos = this->vscroll->GetPosition(); - for (DropDownList::const_iterator it = this->list->begin(); it != this->list->end(); ++it) { + for (const DropDownListItem * const *it = this->list->Begin(); it != this->list->End(); ++it) { const DropDownListItem *item = *it; int item_height = item->Height(r.right - r.left + 1); @@ -343,7 +330,7 @@ struct DropdownWindow : Window { * @param instant_close Set to true if releasing mouse button should close the * list regardless of where the cursor is. */ -void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width, bool instant_close) +void ShowDropDownListAt(Window *w, const DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width, bool instant_close) { DeleteWindowById(WC_DROPDOWN_MENU, 0); @@ -357,7 +344,7 @@ void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, if (auto_width) { /* Find the longest item in the list */ - for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) { + for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) { const DropDownListItem *item = *it; max_item_width = max(max_item_width, item->Width() + 5); } @@ -366,8 +353,8 @@ void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, /* Total length of list */ int list_height = 0; - for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) { - DropDownListItem *item = *it; + for (const DropDownListItem * const *it = list->Begin(); it != list->End(); ++it) { + const DropDownListItem *item = *it; list_height += item->Height(width); } @@ -386,7 +373,7 @@ void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, } else { /* ... and lastly if it won't, enable the scroll bar and fit the * list in below the widget */ - int avg_height = list_height / (int)list->size(); + int avg_height = list_height / (int)list->Length(); int rows = (screen_bottom - 4 - top) / avg_height; height = rows * avg_height; scroll = true; @@ -416,7 +403,7 @@ void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, * @param instant_close Set to true if releasing mouse button should close the * list regardless of where the cursor is. */ -void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close) +void ShowDropDownList(Window *w, const DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close) { /* Our parent's button widget is used to determine where to place the drop * down list window. */ @@ -463,13 +450,13 @@ void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int butt for (uint i = 0; strings[i] != INVALID_STRING_ID; i++) { if (!HasBit(hidden_mask, i)) { - list->push_back(new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i))); + *list->Append() = new DropDownListStringItem(strings[i], i, HasBit(disabled_mask, i)); } } /* No entries in the list? */ - if (list->size() == 0) { - DeleteDropDownList(list); + if (list->Length() == 0) { + delete list; return; } diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h index b923445bb..90ecef1fb 100644 --- a/src/widgets/dropdown_type.h +++ b/src/widgets/dropdown_type.h @@ -14,8 +14,8 @@ #include "../window_type.h" #include "../gfx_func.h" +#include "../core/smallvec_type.hpp" #include "table/strings.h" -#include <list> /** * Base list item class from which others are derived. If placed in a list it @@ -50,7 +50,7 @@ public: virtual void Draw(int left, int right, int top, int bottom, bool sel, int bg_colour) const; virtual StringID String() const { return this->string; } - static bool NatSortFunc(const DropDownListItem *first, const DropDownListItem *second); + static CDECL int NatSortFunc(const DropDownListItem * const *first, const DropDownListItem * const *second); }; /** @@ -83,10 +83,10 @@ public: /** * A drop down list is a collection of drop down list items. */ -typedef std::list<DropDownListItem *> DropDownList; +typedef AutoDeleteSmallVector<const DropDownListItem *, 4> DropDownList; -void ShowDropDownListAt(Window *w, DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width = false, bool instant_close = false); +void ShowDropDownListAt(Window *w, const DropDownList *list, int selected, int button, Rect wi_rect, Colours wi_colour, bool auto_width = false, bool instant_close = false); -void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width = 0, bool auto_width = false, bool instant_close = false); +void ShowDropDownList(Window *w, const DropDownList *list, int selected, int button, uint width = 0, bool auto_width = false, bool instant_close = false); #endif /* WIDGETS_DROPDOWN_TYPE_H */ |