summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoralberth <alberth@openttd.org>2010-07-26 13:03:40 +0000
committeralberth <alberth@openttd.org>2010-07-26 13:03:40 +0000
commit97f85f7bfba286bf9a7c692b390eec8231cac9ae (patch)
tree510a6cb18ef14914b1fa1bd757b8671200f17d35
parent3a1451644a0eae226545818adbde8a8a8c45eab7 (diff)
downloadopenttd-97f85f7bfba286bf9a7c692b390eec8231cac9ae.tar.xz
(svn r20222) -Add: Add functions to compute the row in a scrolled widget.
-rw-r--r--src/window.cpp18
-rw-r--r--src/window_gui.h4
2 files changed, 20 insertions, 2 deletions
diff --git a/src/window.cpp b/src/window.cpp
index c3cde4d5a..dfd9fa078 100644
--- a/src/window.cpp
+++ b/src/window.cpp
@@ -86,18 +86,34 @@ WindowDesc::~WindowDesc()
* @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.
+ * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
* @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 (line_height < 0) line_height = wid->resize_y;
if (clickpos < (int)wid->pos_y + padding) return INT_MAX;
return (clickpos - (int)wid->pos_y - padding) / line_height;
}
/**
+ * Compute the row of a scrolled widget that a user clicked in.
+ * @param clickpos Vertical position of the mouse click (without taking scrolling into account).
+ * @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. Default value is \c 0.
+ * @param line_height Height of a single row. A negative value means using the vertical resize step of the widget.
+ * @return Row number clicked at. If clicked at a wrong position, #INT_MAX is returned.
+ */
+int Scrollbar::GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding, int line_height) const
+{
+ uint pos = w->GetRowFromWidget(clickpos, widget, padding, line_height);
+ if (pos != INT_MAX) pos += this->GetPosition();
+ return (pos >= this->GetCount()) ? INT_MAX : pos;
+}
+
+/**
* 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.
diff --git a/src/window_gui.h b/src/window_gui.h
index d01e8e4af..49d69ec8b 100644
--- a/src/window_gui.h
+++ b/src/window_gui.h
@@ -305,6 +305,8 @@ public:
this->SetPosition(position - this->GetCapacity() + 1);
}
}
+
+ int GetScrolledRowFromWidget(int clickpos, const Window * const w, int widget, int padding = 0, int line_height = -1) const;
};
/**
@@ -528,7 +530,7 @@ public:
bool SetFocusedWidget(byte widget_index);
void HandleButtonClick(byte widget);
- int GetRowFromWidget(int clickpos, int widget, int padding, int line_height) const;
+ int GetRowFromWidget(int clickpos, int widget, int padding, int line_height = -1) const;
void RaiseButtons(bool autoraise = false);
void CDECL SetWidgetsDisabledState(bool disab_stat, int widgets, ...);