summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2008-08-07 18:11:09 +0000
committerpeter1138 <peter1138@openttd.org>2008-08-07 18:11:09 +0000
commit6bae045b4af118f19946ecb4d160a9d69c6638f8 (patch)
tree76b1fd3fb3093f820e5563d14e9e1cb2e1a39993 /src
parentbf65e7e8f9767e2424c34e123d24b7d8da5ba037 (diff)
downloadopenttd-6bae045b4af118f19946ecb4d160a9d69c6638f8.tar.xz
(svn r14014) -Codechange: Add support for automatically sizing drop down lists to the widest list item.
Diffstat (limited to 'src')
-rw-r--r--src/widgets/dropdown.cpp25
-rw-r--r--src/widgets/dropdown_type.h4
2 files changed, 27 insertions, 2 deletions
diff --git a/src/widgets/dropdown.cpp b/src/widgets/dropdown.cpp
index 41975a59f..f1d39dfff 100644
--- a/src/widgets/dropdown.cpp
+++ b/src/widgets/dropdown.cpp
@@ -26,6 +26,13 @@ void DropDownListItem::Draw(int x, int y, uint width, uint height, bool sel, int
GfxFillRect(x + 1, y + 4, x + width - 2, y + 4, c2);
}
+uint DropDownListStringItem::Width() const
+{
+ char buffer[512];
+ GetString(buffer, this->String(), lastof(buffer));
+ return GetStringBoundingBox(buffer).width;
+}
+
void DropDownListStringItem::Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const
{
DrawStringTruncated(x + 2, y, this->String(), sel ? TC_WHITE : TC_BLACK, width);
@@ -232,6 +239,19 @@ void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, u
/* The preferred position is just below the dropdown calling widget */
int top = w->top + wi->bottom + 1;
+ bool auto_width = (width == UINT_MAX);
+
+ if (auto_width) {
+ /* Find the longest item in the list */
+ width = 0;
+ for (DropDownList::const_iterator it = list->begin(); it != list->end(); ++it) {
+ const DropDownListItem *item = *it;
+ width = max(width, item->Width() + 5);
+ }
+ } else if (width == 0) {
+ width = wi->right - wi->left + 1;
+ }
+
/* Total length of list */
int list_height = 0;
@@ -264,11 +284,12 @@ void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, u
int rows = (screen_bottom - 4 - top) / avg_height;
height = rows * avg_height;
scroll = true;
+ /* Add space for the scroll bar if we automatically determined
+ * the width of the list. */
+ if (auto_width) width += 12;
}
}
- if (width == 0) width = wi->right - wi->left + 1;
-
DropdownWindow *dw = new DropdownWindow(
w->left + wi->left,
top,
diff --git a/src/widgets/dropdown_type.h b/src/widgets/dropdown_type.h
index aa4baafe1..c9686a178 100644
--- a/src/widgets/dropdown_type.h
+++ b/src/widgets/dropdown_type.h
@@ -22,6 +22,7 @@ public:
virtual bool Selectable() const { return false; }
virtual uint Height(uint width) const { return 10; }
+ virtual uint Width() const { return 0; }
virtual void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const;
};
@@ -36,6 +37,7 @@ public:
virtual ~DropDownListStringItem() {}
virtual bool Selectable() const { return true; }
+ virtual uint Width() const;
virtual void Draw(int x, int y, uint width, uint height, bool sel, int bg_colour) const;
virtual StringID String() const { return this->string; }
};
@@ -68,6 +70,8 @@ typedef std::list<DropDownListItem *> DropDownList;
* @param button The widget within the parent window that is used to determine
* the list's location.
* @param width Override the width determined by the selected widget.
+ * If UINT_MAX then the width is determined by the widest item
+ * in the list.
*/
void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width = 0);