changeset 15561:1f61dc11fd8c draft

(svn r20221) -Codechange: Move unscrolled row calculation into a function.
author alberth <alberth@openttd.org>
date Mon, 26 Jul 2010 13:02:28 +0000
parents 787178c1c88f
children f5d2b2fea695
files src/music_gui.cpp src/town_gui.cpp src/window.cpp src/window_gui.h
diffstat 4 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/music_gui.cpp
+++ b/src/music_gui.cpp
@@ -401,7 +401,7 @@
 	{
 		switch (widget) {
 			case MTSW_LIST_LEFT: { // add to playlist
-				int y = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y) / FONT_HEIGHT_SMALL;
+				int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
 
 				if (msf.playlist < 4) return;
 				if (!IsInsideMM(y, 0, BaseMusic::GetUsedSet()->num_available)) return;
@@ -425,7 +425,7 @@
 			} break;
 
 			case MTSW_LIST_RIGHT: { // remove from playlist
-				int y = (pt.y - this->GetWidget<NWidgetBase>(widget)->pos_y) / FONT_HEIGHT_SMALL;
+				int y = this->GetRowFromWidget(pt.y, widget, 0, FONT_HEIGHT_SMALL);
 
 				if (msf.playlist < 4) return;
 				if (!IsInsideMM(y, 0, NUM_SONGS_PLAYLIST)) return;
--- a/src/town_gui.cpp
+++ b/src/town_gui.cpp
@@ -259,8 +259,7 @@
 	{
 		switch (widget) {
 			case TWA_COMMAND_LIST: {
-				int y = (pt.y - this->GetWidget<NWidgetBase>(TWA_COMMAND_LIST)->pos_y - 1) / FONT_HEIGHT_NORMAL;
-
+				int y = this->GetRowFromWidget(pt.y, TWA_COMMAND_LIST, 1, FONT_HEIGHT_NORMAL);
 				if (!IsInsideMM(y, 0, 5)) return;
 
 				y = GetNthSetBit(GetMaskOfTownActions(NULL, _local_company, this->town), y + this->vscroll.GetPosition() - 1);
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -82,6 +82,22 @@
 }
 
 /**
+ * Compute the row of a widget that a user clicked in.
+ * @param clickpos    Vertical position of the mouse click.
+ * @param widget      Widget number of the widget clicked in.
+ * @param padding     Amount of empty space between the widget edge and the top of the first row.
+ * @param line_height Height of a single row.
+ * @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
+ * @note The widget does not know where a list printed at the widget ends, so below a list is not a wrong position.
+ */
+int Window::GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const
+{
+	const NWidgetBase *wid = this->GetWidget<NWidgetBase>(widget);
+	if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
+	return (clickpos - (int)wid->pos_y - padding) / line_height;
+}
+
+/**
  * Set capacity of visible elements from the size and resize properties of a widget.
  * @param w       Window.
  * @param widget  Widget with size and resize properties.
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -528,6 +528,7 @@
 	bool SetFocusedWidget(byte widget_index);
 
 	void HandleButtonClick(byte widget);
+	int GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const;
 
 	void RaiseButtons(bool autoraise = false);
 	void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);