changeset 19405:c51f261bae79 draft

(svn r24308) -Add: ShowDropDownListAt() for drawing dropdown windows independent of dropdown widgets.
author frosch <frosch@openttd.org>
date Fri, 01 Jun 2012 10:43:50 +0000
parents 8fd53c583719
children 0731e1a0613a
files src/widgets/dropdown.cpp src/widgets/dropdown_type.h
diffstat 2 files changed, 62 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/widgets/dropdown.cpp
+++ b/src/widgets/dropdown.cpp
@@ -328,32 +328,29 @@
 	}
 };
 
-void ShowDropDownList(Window *w, DropDownList *list, int selected, int button, uint width, bool auto_width, bool instant_close)
+/**
+ * Show a drop down list.
+ * @param w        Parent window for the list.
+ * @param list     Prepopulated DropDownList. Will be deleted when the list is
+ *                 closed.
+ * @param selected The initially selected list item.
+ * @param button   The widget which is passed to Window::OnDropdownSelect and OnDropdownClose.
+ *                 Unless you override those functions, this should be then widget index of the dropdown button.
+ * @param wi_rect  Coord of the parent drop down button, used to position the dropdown menu.
+ * @param auto_width The width is determined by the widest item in the list,
+ *                   in this case only one of \a left or \a right is used (depending on text direction).
+ * @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)
 {
 	DeleteWindowById(WC_DROPDOWN_MENU, 0);
 
-	/* Our parent's button widget is used to determine where to place the drop
-	 * down list window. */
-	Rect wi_rect;
-	Colours wi_colour;
-	NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
-	wi_rect.left   = nwi->pos_x;
-	wi_rect.right  = nwi->pos_x + nwi->current_x - 1;
-	wi_rect.top    = nwi->pos_y;
-	wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
-	wi_colour = nwi->colour;
-
-	if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
-		nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
-	} else {
-		w->LowerWidget(button);
-	}
-	w->SetWidgetDirty(button);
-
 	/* The preferred position is just below the dropdown calling widget */
 	int top = w->top + wi_rect.bottom + 1;
 
-	if (width == 0) width = wi_rect.right - wi_rect.left + 1;
+	/* The preferred width equals the calling widget */
+	uint width = wi_rect.right - wi_rect.left + 1;
 
 	uint max_item_width = 0;
 
@@ -406,6 +403,49 @@
 }
 
 /**
+ * Show a drop down list.
+ * @param w        Parent window for the list.
+ * @param list     Prepopulated DropDownList. Will be deleted when the list is
+ *                 closed.
+ * @param selected The initially selected list item.
+ * @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.
+ * @param auto_width Maximum width is determined by the widest item in the list.
+ * @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)
+{
+	/* Our parent's button widget is used to determine where to place the drop
+	 * down list window. */
+	Rect wi_rect;
+	NWidgetCore *nwi = w->GetWidget<NWidgetCore>(button);
+	wi_rect.left   = nwi->pos_x;
+	wi_rect.right  = nwi->pos_x + nwi->current_x - 1;
+	wi_rect.top    = nwi->pos_y;
+	wi_rect.bottom = nwi->pos_y + nwi->current_y - 1;
+	Colours wi_colour = nwi->colour;
+
+	if ((nwi->type & WWT_MASK) == NWID_BUTTON_DROPDOWN) {
+		nwi->disp_flags |= ND_DROPDOWN_ACTIVE;
+	} else {
+		w->LowerWidget(button);
+	}
+	w->SetWidgetDirty(button);
+
+	if (width != 0) {
+		if (_current_text_dir == TD_RTL) {
+			wi_rect.left = wi_rect.right + 1 - width;
+		} else {
+			wi_rect.right = wi_rect.left + width - 1;
+		}
+	}
+
+	ShowDropDownListAt(w, list, selected, button, wi_rect, wi_colour, auto_width, instant_close);
+}
+
+/**
  * Show a dropdown menu window near a widget of the parent window.
  * The result code of the items is their index in the \a strings list.
  * @param w             Parent window that wants the dropdown menu.
--- a/src/widgets/dropdown_type.h
+++ b/src/widgets/dropdown_type.h
@@ -85,19 +85,8 @@
  */
 typedef std::list<DropDownListItem *> DropDownList;
 
-/**
- * Show a drop down list.
- * @param w        Parent window for the list.
- * @param list     Prepopulated DropDownList. Will be deleted when the list is
- *                 closed.
- * @param selected The initially selected list item.
- * @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.
- * @param auto_width Maximum width is determined by the widest item in the list.
- * @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 = 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);
 
 #endif /* WIDGETS_DROPDOWN_TYPE_H */