From: Jonathan Neuschäfer Date: Sat, 17 Sep 2011 20:11:04 +0000 (+0200) Subject: list_window: export scroll-after-cursor code X-Git-Tag: release-0.20~87^2~1 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=0ffb6f641f8eeab728133973abef885db2860687;p=ncmpc.git list_window: export scroll-after-cursor code This moves the list_window_check_origin code into a new, more generic function, which is exported. --- diff --git a/src/list_window.c b/src/list_window.c index 8e0b968..ecd786b 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -93,26 +93,7 @@ list_window_check_selected(struct list_window *lw) static void list_window_check_origin(struct list_window *lw) { - int start = lw->start; - - if ((unsigned) options.scroll_offset * 2 >= lw->rows) - // Center if the offset is more than half the screen - start = lw->selected - lw->rows / 2; - else { - if (lw->selected < lw->start + options.scroll_offset) - start = lw->selected - options.scroll_offset; - - if (lw->selected >= lw->start + lw->rows - options.scroll_offset) - start = lw->selected - lw->rows + 1 + options.scroll_offset; - } - - if (start + lw->rows > lw->length) - start = lw->length - lw->rows; - - if (start < 0 || lw->length == 0) - start = 0; - - lw->start = start; + list_window_scroll_to(lw, lw->selected); } void @@ -152,6 +133,31 @@ list_window_center(struct list_window *lw, unsigned n) } } +void +list_window_scroll_to(struct list_window *lw, unsigned n) +{ + int start = lw->start; + + if ((unsigned) options.scroll_offset * 2 >= lw->rows) + // Center if the offset is more than half the screen + start = n - lw->rows / 2; + else { + if (n < lw->start + options.scroll_offset) + start = n - options.scroll_offset; + + if (n >= lw->start + lw->rows - options.scroll_offset) + start = n - lw->rows + 1 + options.scroll_offset; + } + + if (start + lw->rows > lw->length) + start = lw->length - lw->rows; + + if (start < 0 || lw->length == 0) + start = 0; + + lw->start = start; +} + void list_window_set_cursor(struct list_window *lw, unsigned i) { diff --git a/src/list_window.h b/src/list_window.h index 83beeac..8f67c85 100644 --- a/src/list_window.h +++ b/src/list_window.h @@ -127,6 +127,13 @@ list_window_mouse(struct list_window *lw, unsigned long bstate, int y); void list_window_center(struct list_window *lw, unsigned n); +/** + * Scrolls the view to item n, as if the cursor would have been moved + * to the position. + */ +void +list_window_scroll_to(struct list_window *lw, unsigned n); + /** * Sets the position of the cursor. Disables range selection. */