Code

list_window: added attribute "length"
authorMax Kellermann <max@duempel.org>
Sat, 10 Oct 2009 14:07:33 +0000 (16:07 +0200)
committerMax Kellermann <max@duempel.org>
Sat, 10 Oct 2009 14:07:33 +0000 (16:07 +0200)
Don't pass the number of list items to each and every function.
Instead, call list_window_set_length() each time the list contents
change.

14 files changed:
src/list_window.c
src/list_window.h
src/screen_artist.c
src/screen_browser.c
src/screen_file.c
src/screen_find.c
src/screen_find.h
src/screen_help.c
src/screen_keydef.c
src/screen_outputs.c
src/screen_play.c
src/screen_search.c
src/screen_song.c
src/screen_text.c

index be4648df54cc8a02aec95a1ab2dd46836f98f70c..3c7c29adbcf0007d8a73b9860a54ce1e744bb327 100644 (file)
@@ -66,12 +66,12 @@ list_window_reset(struct list_window *lw)
        lw->start = 0;
 }
 
-void
-list_window_check_selected(struct list_window *lw, unsigned length)
+static void
+list_window_check_selected(struct list_window *lw)
 {
-       if (lw->start + lw->rows > length) {
-               if (length > lw->rows)
-                       lw->start = length - lw->rows;
+       if (lw->start + lw->rows > lw->length) {
+               if (lw->length > lw->rows)
+                       lw->start = lw->length - lw->rows;
                else
                        lw->start = 0;
        }
@@ -79,24 +79,24 @@ list_window_check_selected(struct list_window *lw, unsigned length)
        if (lw->selected < lw->start)
                lw->selected = lw->start;
 
-       if (length == 0)
+       if (lw->length == 0)
                lw->selected = 0;
-       else if (lw->selected >= length)
-               lw->selected = length - 1;
+       else if (lw->selected >= lw->length)
+               lw->selected = lw->length - 1;
 
        if(lw->range_selection)
        {
-               if (length == 0) {
+               if (lw->length == 0) {
                        lw->selected_start = 0;
                        lw->selected_end = 0;
                        lw->range_base = 0;
                } else {
-                       if (lw->selected_start >= length)
-                               lw->selected_start = length - 1;
-                       if (lw->selected_end >= length)
-                               lw->selected_end = length - 1;
-                       if (lw->range_base >= length)
-                               lw->range_base = length - 1;
+                       if (lw->selected_start >= lw->length)
+                               lw->selected_start = lw->length - 1;
+                       if (lw->selected_end >= lw->length)
+                               lw->selected_end = lw->length - 1;
+                       if (lw->range_base >= lw->length)
+                               lw->range_base = lw->length - 1;
                }
 
                if(lw->range_base > lw->selected_end)
@@ -112,16 +112,24 @@ list_window_check_selected(struct list_window *lw, unsigned length)
 }
 
 void
-list_window_center(struct list_window *lw, unsigned rows, unsigned n)
+list_window_set_length(struct list_window *lw, unsigned length)
+{
+       lw->length = length;
+
+       list_window_check_selected(lw);
+}
+
+void
+list_window_center(struct list_window *lw, unsigned n)
 {
        if (n > lw->rows / 2)
                lw->start = n - lw->rows / 2;
        else
                lw->start = 0;
 
-       if (lw->start + lw->rows > rows) {
-               if (lw->rows < rows)
-                       lw->start = rows - lw->rows;
+       if (lw->start + lw->rows > lw->length) {
+               if (lw->rows < lw->length)
+                       lw->start = lw->length - lw->rows;
                else
                        lw->start = 0;
        }
@@ -134,6 +142,8 @@ list_window_set_cursor(struct list_window *lw, unsigned i)
        lw->selected = i;
        lw->selected_start = i;
        lw->selected_end = i;
+
+       list_window_check_selected(lw);
 }
 
 void
@@ -161,7 +171,7 @@ list_window_move_cursor(struct list_window *lw, unsigned n)
 }
 
 void
-list_window_fetch_cursor(struct list_window *lw, unsigned length)
+list_window_fetch_cursor(struct list_window *lw)
 {
        if (lw->selected < lw->start + options.scroll_offset) {
                if (lw->start > 0)
@@ -178,7 +188,7 @@ list_window_fetch_cursor(struct list_window *lw, unsigned length)
                        lw->selected_end = lw->selected;
                }
        } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
-               if (lw->start + lw->rows < length)
+               if (lw->start + lw->rows < lw->length)
                        lw->selected = lw->start + lw->rows - 1 - options.scroll_offset;
                if (lw->range_selection) {
                        if (lw->selected < lw->range_base) {
@@ -195,21 +205,21 @@ list_window_fetch_cursor(struct list_window *lw, unsigned length)
 }
 
 static void
-list_window_next(struct list_window *lw, unsigned length)
+list_window_next(struct list_window *lw)
 {
-       if (lw->selected + 1 < length)
+       if (lw->selected + 1 < lw->length)
                list_window_move_cursor(lw, lw->selected + 1);
        else if (options.list_wrap)
                list_window_move_cursor(lw, 0);
 }
 
 static void
-list_window_previous(struct list_window *lw, unsigned length)
+list_window_previous(struct list_window *lw)
 {
        if (lw->selected > 0)
                list_window_move_cursor(lw, lw->selected - 1);
        else if (options.list_wrap)
-               list_window_move_cursor(lw, length-1);
+               list_window_move_cursor(lw, lw->length - 1);
 }
 
 static void
@@ -225,27 +235,27 @@ list_window_top(struct list_window *lw)
 }
 
 static void
-list_window_middle(struct list_window *lw, unsigned length)
+list_window_middle(struct list_window *lw)
 {
-       if (length >= lw->rows)
+       if (lw->length >= lw->rows)
                list_window_move_cursor(lw, lw->start + lw->rows / 2);
        else
-               list_window_move_cursor(lw, length / 2);
+               list_window_move_cursor(lw, lw->length / 2);
 }
 
 static void
-list_window_bottom(struct list_window *lw, unsigned length)
+list_window_bottom(struct list_window *lw)
 {
-       if (length >= lw->rows)
+       if (lw->length >= lw->rows)
                if ((unsigned) options.scroll_offset * 2 >= lw->rows)
                        list_window_move_cursor(lw, lw->start + lw->rows / 2);
                else
-                       if (lw->start + lw->rows == length)
-                               list_window_move_cursor(lw, length - 1);
+                       if (lw->start + lw->rows == lw->length)
+                               list_window_move_cursor(lw, lw->length - 1);
                        else
                                list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
        else
-               list_window_move_cursor(lw, length - 1);
+               list_window_move_cursor(lw, lw->length - 1);
 }
 
 static void
@@ -255,23 +265,23 @@ list_window_first(struct list_window *lw)
 }
 
 static void
-list_window_last(struct list_window *lw, unsigned length)
+list_window_last(struct list_window *lw)
 {
-       if (length > 0)
-               list_window_move_cursor(lw, length - 1);
+       if (lw->length > 0)
+               list_window_move_cursor(lw, lw->length - 1);
        else
                list_window_move_cursor(lw, 0);
 }
 
 static void
-list_window_next_page(struct list_window *lw, unsigned length)
+list_window_next_page(struct list_window *lw)
 {
        if (lw->rows < 2)
                return;
-       if (lw->selected + lw->rows < length)
+       if (lw->selected + lw->rows < lw->length)
                list_window_move_cursor(lw, lw->selected + lw->rows - 1);
        else
-               list_window_last(lw, length);
+               list_window_last(lw);
 }
 
 static void
@@ -286,7 +296,7 @@ list_window_previous_page(struct list_window *lw)
 }
 
 static void
-list_window_scroll_up(struct list_window *lw, unsigned length, unsigned n)
+list_window_scroll_up(struct list_window *lw, unsigned n)
 {
        if (lw->start > 0) {
                if (n > lw->start)
@@ -294,21 +304,21 @@ list_window_scroll_up(struct list_window *lw, unsigned length, unsigned n)
                else
                        lw->start -= n;
 
-               list_window_fetch_cursor(lw, length);
+               list_window_fetch_cursor(lw);
        }
 }
 
 static void
-list_window_scroll_down(struct list_window *lw, unsigned length, unsigned n)
+list_window_scroll_down(struct list_window *lw, unsigned n)
 {
-       if (lw->start + lw->rows < length)
+       if (lw->start + lw->rows < lw->length)
        {
-               if ( lw->start + lw->rows + n > length - 1)
-                       lw->start = length - lw->rows;
+               if ( lw->start + lw->rows + n > lw->length - 1)
+                       lw->start = lw->length - lw->rows;
                else
                        lw->start += n;
 
-               list_window_fetch_cursor(lw, length);
+               list_window_fetch_cursor(lw);
        }
 }
 
@@ -390,14 +400,14 @@ list_window_paint(struct list_window *lw,
                                start = lw->selected - lw->rows + 1 + options.scroll_offset;
                        }
                }
-               if (start < 0)
-                       lw->start = 0;
-               else
-               {
-                       while ( start > 0 && callback(start + lw->rows - 1, &highlight, NULL, callback_data) == NULL)
-                               start--;
-                       lw->start = start;
-               }
+
+               if (start + lw->rows > lw->length)
+                       start = lw->length - lw->rows;
+
+               if (start < 0 || lw->length == 0)
+                       start = 0;
+
+               lw->start = start;
        }
 
        show_cursor = show_cursor &&
@@ -477,14 +487,13 @@ list_window_rfind(struct list_window *lw,
                  void *callback_data,
                  const char *str,
                  bool wrap,
-                 bool bell_on_wrap,
-                 unsigned rows)
+                 bool bell_on_wrap)
 {
        bool h;
        int i = lw->selected - 1;
        const char *label;
 
-       if (rows == 0)
+       if (lw->length == 0)
                return false;
 
        do {
@@ -502,7 +511,7 @@ list_window_rfind(struct list_window *lw,
                        i--;
                }
                if (wrap) {
-                       i = rows - 1; /* last item */
+                       i = lw->length - 1; /* last item */
                        if (bell_on_wrap) {
                                screen_bell();
                        }
@@ -547,32 +556,32 @@ list_window_jump(struct list_window *lw,
 
 /* perform basic list window commands (movement) */
 bool
-list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
+list_window_cmd(struct list_window *lw, command_t cmd)
 {
        switch (cmd) {
        case CMD_LIST_PREVIOUS:
-               list_window_previous(lw, rows);
+               list_window_previous(lw);
                break;
        case CMD_LIST_NEXT:
-               list_window_next(lw, rows);
+               list_window_next(lw);
                break;
        case CMD_LIST_TOP:
                list_window_top(lw);
                break;
        case CMD_LIST_MIDDLE:
-               list_window_middle(lw,rows);
+               list_window_middle(lw);
                break;
        case CMD_LIST_BOTTOM:
-               list_window_bottom(lw,rows);
+               list_window_bottom(lw);
                break;
        case CMD_LIST_FIRST:
                list_window_first(lw);
                break;
        case CMD_LIST_LAST:
-               list_window_last(lw, rows);
+               list_window_last(lw);
                break;
        case CMD_LIST_NEXT_PAGE:
-               list_window_next_page(lw, rows);
+               list_window_next_page(lw);
                break;
        case CMD_LIST_PREVIOUS_PAGE:
                list_window_previous_page(lw);
@@ -591,16 +600,16 @@ list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
                }
                break;
        case CMD_LIST_SCROLL_UP_LINE:
-               list_window_scroll_up(lw, rows, 1);
+               list_window_scroll_up(lw, 1);
                break;
        case CMD_LIST_SCROLL_DOWN_LINE:
-               list_window_scroll_down(lw, rows, 1);
+               list_window_scroll_down(lw, 1);
                break;
        case CMD_LIST_SCROLL_UP_HALF:
-               list_window_scroll_up(lw, rows, (lw->rows - 1) / 2);
+               list_window_scroll_up(lw, (lw->rows - 1) / 2);
                break;
        case CMD_LIST_SCROLL_DOWN_HALF:
-               list_window_scroll_down(lw, rows, (lw->rows - 1) / 2);
+               list_window_scroll_down(lw, (lw->rows - 1) / 2);
                break;
        default:
                return false;
@@ -610,7 +619,7 @@ list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
 }
 
 bool
-list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
+list_window_scroll_cmd(struct list_window *lw, command_t cmd)
 {
        switch (cmd) {
        case CMD_LIST_SCROLL_UP_LINE:
@@ -621,7 +630,7 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
 
        case CMD_LIST_SCROLL_DOWN_LINE:
        case CMD_LIST_NEXT:
-               if (lw->start + lw->rows < rows)
+               if (lw->start + lw->rows < lw->length)
                        lw->start++;
                break;
 
@@ -630,17 +639,17 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
                break;
 
        case CMD_LIST_LAST:
-               if (rows > lw->rows)
-                       lw->start = rows - lw->rows;
+               if (lw->length > lw->rows)
+                       lw->start = lw->length - lw->rows;
                else
                        lw->start = 0;
                break;
 
        case CMD_LIST_NEXT_PAGE:
                lw->start += lw->rows - 1;
-               if (lw->start + lw->rows > rows) {
-                       if (rows > lw->rows)
-                               lw->start = rows - lw->rows;
+               if (lw->start + lw->rows > lw->length) {
+                       if (lw->length > lw->rows)
+                               lw->start = lw->length - lw->rows;
                        else
                                lw->start = 0;
                }
@@ -662,9 +671,9 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
 
        case CMD_LIST_SCROLL_DOWN_HALF:
                lw->start += (lw->rows - 1) / 2;
-               if (lw->start + lw->rows > rows) {
-                       if (rows > lw->rows)
-                               lw->start = rows - lw->rows;
+               if (lw->start + lw->rows > lw->length) {
+                       if (lw->length > lw->rows)
+                               lw->start = lw->length - lw->rows;
                        else
                                lw->start = 0;
                }
@@ -679,8 +688,7 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
 
 #ifdef HAVE_GETMOUSE
 bool
-list_window_mouse(struct list_window *lw, unsigned rows,
-                 unsigned long bstate, int y)
+list_window_mouse(struct list_window *lw, unsigned long bstate, int y)
 {
        assert(lw != NULL);
 
@@ -694,11 +702,11 @@ list_window_mouse(struct list_window *lw, unsigned rows,
        }
 
        /* if the even occurred below the list window move down */
-       if ((unsigned)y >= rows) {
+       if ((unsigned)y >= lw->length) {
                if (bstate & BUTTON3_CLICKED)
-                       list_window_last(lw, rows);
+                       list_window_last(lw);
                else
-                       list_window_next_page(lw, rows);
+                       list_window_next_page(lw);
                return true;
        }
 
index d0ba6c14a671b9760ed18740aada5eee98b6b1b4..99f702882a40e1834c27286e6b6e252718ce8eab 100644 (file)
@@ -41,6 +41,11 @@ struct list_window {
        WINDOW *w;
        unsigned rows, cols;
 
+       /**
+        * Number of items in this list.
+        */
+       unsigned length;
+
        unsigned start;
        unsigned selected;
        unsigned selected_start;     /* for range selection, first selected item */
@@ -61,6 +66,9 @@ void list_window_free(struct list_window *lw);
 /* reset a list window (selected=0, start=0) */
 void list_window_reset(struct list_window *lw);
 
+void
+list_window_set_length(struct list_window *lw, unsigned length);
+
 /* paint a list window */
 void list_window_paint(struct list_window *lw,
                       list_window_callback_fn_t callback,
@@ -68,14 +76,14 @@ void list_window_paint(struct list_window *lw,
 
 /* perform basic list window commands (movement) */
 bool
-list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd);
+list_window_cmd(struct list_window *lw, command_t cmd);
 
 /**
  * Scroll the window.  Returns non-zero if the command has been
  * consumed.
  */
 bool
-list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd);
+list_window_scroll_cmd(struct list_window *lw, command_t cmd);
 
 #ifdef HAVE_GETMOUSE
 /**
@@ -83,15 +91,11 @@ list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd);
  * Returns non-zero if the mouse event has been handled.
  */
 bool
-list_window_mouse(struct list_window *lw, unsigned rows,
-                 unsigned long bstate, int y);
+list_window_mouse(struct list_window *lw, unsigned long bstate, int y);
 #endif
 
 void
-list_window_center(struct list_window *lw, unsigned rows, unsigned n);
-
-/* select functions */
-void list_window_check_selected(struct list_window *lw, unsigned length);
+list_window_center(struct list_window *lw, unsigned n);
 
 /**
  * Sets the position of the cursor.  Disables range selection.
@@ -111,7 +115,7 @@ list_window_move_cursor(struct list_window *lw, unsigned n);
  * outside the current scrolling range.
  */
 void
-list_window_fetch_cursor(struct list_window *lw, unsigned length);
+list_window_fetch_cursor(struct list_window *lw);
 
 /* find a string in a list window */
 bool
@@ -129,8 +133,7 @@ list_window_rfind(struct list_window *lw,
                  void *callback_data,
                  const char *str,
                  bool wrap,
-                 bool bell_on_wrap,
-                 unsigned rows);
+                 bool bell_on_wrap);
 
 /* find a string in a list window which begins with the given characters in *str */
 bool
index 3ffc533021eff02a428811574fe9741902340053..6169775573bd768ffa4bce3a40db37454db78025 100644 (file)
@@ -166,6 +166,7 @@ load_artist_list(struct mpdclient *c)
        list = g_list_sort(list, compare_utf8);
 
        artist_list = g_list_to_ptr_array(list);
+       list_window_set_length(browser.lw, artist_list->len);
 }
 
 static void
@@ -184,6 +185,7 @@ load_album_list(struct mpdclient *c)
        list = g_list_sort(list, compare_utf8);
 
        album_list = g_list_to_ptr_array(list);
+       list_window_set_length(browser.lw, album_list->len + 2);
 }
 
 static void
@@ -217,6 +219,7 @@ load_song_list(struct mpdclient *c)
        /* fix highlights */
        screen_browser_sync_highlights(browser.filelist, &c->playlist);
 #endif
+       list_window_set_length(browser.lw, filelist_length(browser.filelist));
 }
 
 static void
@@ -420,24 +423,13 @@ add_query(struct mpdclient *c, enum mpd_tag_type table, char *_filter)
        filelist_free(addlist);
 }
 
-static unsigned
-metalist_length(void)
-{
-       assert(mode != LIST_ARTISTS || artist_list != NULL);
-       assert(mode != LIST_ALBUMS || album_list != NULL);
-
-       return mode == LIST_ALBUMS
-               ? album_list->len + 2
-               : artist_list->len;
-}
-
 static int
 screen_artist_lw_cmd(struct mpdclient *c, command_t cmd)
 {
        switch (mode) {
        case LIST_ARTISTS:
        case LIST_ALBUMS:
-               return list_window_cmd(browser.lw, metalist_length(), cmd);
+               return list_window_cmd(browser.lw, cmd);
 
        case LIST_SONGS:
                return browser_cmd(&browser, c, cmd);
@@ -495,8 +487,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
 
                                if (idx >= 0) {
                                        list_window_set_cursor(browser.lw, idx);
-                                       list_window_center(browser.lw,
-                                                          artist_list->len, idx);
+                                       list_window_center(browser.lw, idx);
                                }
                        } else if (browser.lw->selected == album_list->len + 1) {
                                /* handle "show all" */
@@ -529,8 +520,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                                if (idx >= 0) {
                                        ++idx;
                                        list_window_set_cursor(browser.lw, idx);
-                                       list_window_center(browser.lw,
-                                                          album_list->len, idx);
+                                       list_window_center(browser.lw, idx);
                                }
 
                                artist_repaint();
@@ -558,8 +548,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
 
                        if (idx >= 0) {
                                list_window_set_cursor(browser.lw, idx);
-                               list_window_center(browser.lw,
-                                                  artist_list->len, idx);
+                               list_window_center(browser.lw, idx);
                        }
                        break;
 
@@ -577,8 +566,7 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
                        if (idx >= 0) {
                                ++idx;
                                list_window_set_cursor(browser.lw, idx);
-                               list_window_center(browser.lw,
-                                                  album_list->len, idx);
+                               list_window_center(browser.lw, idx);
                        }
                        break;
                }
@@ -650,16 +638,14 @@ screen_artist_cmd(struct mpdclient *c, command_t cmd)
        case CMD_LIST_RFIND_NEXT:
                switch (mode) {
                case LIST_ARTISTS:
-                       screen_find(browser.lw, artist_list->len,
-                                   cmd, screen_artist_lw_callback,
-                                   artist_list);
+                       screen_find(browser.lw, cmd,
+                                   screen_artist_lw_callback, artist_list);
                        artist_repaint();
                        return true;
 
                case LIST_ALBUMS:
-                       screen_find(browser.lw, album_list->len + 2,
-                                   cmd, screen_artist_lw_callback,
-                                   album_list);
+                       screen_find(browser.lw, cmd,
+                                   screen_artist_lw_callback, album_list);
                        artist_repaint();
                        return true;
 
index 492fdd44b8e2bb21259d56bfe46d62649b3e059f..dd97bb22f0f91815dd1e8eda326db64f72ad6dfb 100644 (file)
@@ -372,19 +372,12 @@ browser_handle_mouse_event(struct screen_browser *browser, struct mpdclient *c)
        int row;
        unsigned prev_selected = browser->lw->selected;
        unsigned long bstate;
-       int length;
-
-       if (browser->filelist)
-               length = filelist_length(browser->filelist);
-       else
-               length = 0;
 
        if (screen_get_mouse_event(c, &bstate, &row) ||
-           list_window_mouse(browser->lw, length, bstate, row))
+           list_window_mouse(browser->lw, bstate, row))
                return 1;
 
        list_window_set_cursor(browser->lw, browser->lw->start + row);
-       list_window_check_selected(browser->lw, length);
 
        if( bstate & BUTTON1_CLICKED ) {
                if (prev_selected == browser->lw->selected)
@@ -407,8 +400,7 @@ browser_cmd(struct screen_browser *browser,
        if (browser->filelist == NULL)
                return false;
 
-       if (list_window_cmd(browser->lw, filelist_length(browser->filelist),
-                           cmd))
+       if (list_window_cmd(browser->lw, cmd))
                return true;
 
        switch (cmd) {
@@ -416,8 +408,7 @@ browser_cmd(struct screen_browser *browser,
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(browser->lw, filelist_length(browser->filelist),
-                           cmd, browser_lw_callback,
+               screen_find(browser->lw, cmd, browser_lw_callback,
                            browser->filelist);
                return true;
        case CMD_LIST_JUMP:
@@ -467,16 +458,12 @@ browser_cmd(struct screen_browser *browser,
 
        case CMD_SELECT:
                if (browser_handle_select(browser, c))
-                       list_window_cmd(browser->lw,
-                                       filelist_length(browser->filelist),
-                                       CMD_LIST_NEXT);
+                       list_window_cmd(browser->lw, CMD_LIST_NEXT);
                return true;
 
        case CMD_ADD:
                if (browser_handle_add(browser, c))
-                       list_window_cmd(browser->lw,
-                                       filelist_length(browser->filelist),
-                                       CMD_LIST_NEXT);
+                       list_window_cmd(browser->lw, CMD_LIST_NEXT);
                return true;
 
        case CMD_SELECT_ALL:
index 5a37c0c6a1b84b51a2275926974d66550cd9bb08..8e1cf5b87f46dc6d097d0a9f3d6493ab07052c20 100644 (file)
@@ -77,6 +77,9 @@ screen_file_reload(struct mpdclient *c)
                                       compare_filelist_entry_path);
        else
                mpdclient_handle_error(c);
+
+       list_window_set_length(browser.lw,
+                              filelist_length(browser.filelist));
 }
 
 /**
@@ -127,8 +130,7 @@ change_to_parent(struct mpdclient *c)
        if (success && idx >= 0) {
                /* set the cursor on the previous working directory */
                list_window_set_cursor(browser.lw, idx);
-               list_window_center(browser.lw,
-                                  filelist_length(browser.filelist), idx);
+               list_window_center(browser.lw, idx);
        }
 
        return success;
@@ -319,8 +321,6 @@ screen_file_update(struct mpdclient *c)
        if (c->events & (MPD_IDLE_DATABASE | MPD_IDLE_STORED_PLAYLIST)) {
                /* the db has changed -> update the filelist */
                screen_file_reload(c);
-               list_window_check_selected(browser.lw,
-                                          filelist_length(browser.filelist));
        }
 
 #ifndef NCMPC_MINI
@@ -369,8 +369,6 @@ screen_file_cmd(struct mpdclient *c, command_t cmd)
 #ifndef NCMPC_MINI
                screen_browser_sync_highlights(browser.filelist, &c->playlist);
 #endif
-               list_window_check_selected(browser.lw,
-                                          filelist_length(browser.filelist));
                screen_file_repaint();
                return false;
 
index 0e93b4ff2f96d4b3aeddbc66b092c9f63ad8e9a1..79115edfa07f92573bc03b3eec6aa05b70612b0e 100644 (file)
@@ -31,9 +31,7 @@
 
 /* query user for a string and find it in a list window */
 int
-screen_find(struct list_window *lw,
-           int rows,
-           command_t findcmd,
+screen_find(struct list_window *lw, command_t findcmd,
            list_window_callback_fn_t callback_fn,
            void *callback_data)
 {
@@ -72,8 +70,7 @@ screen_find(struct list_window *lw,
                                            callback_fn, callback_data,
                                            screen.findbuf,
                                            options.find_wrap,
-                                           options.bell_on_wrap,
-                                           rows)
+                                           options.bell_on_wrap)
                        : list_window_find(lw,
                                           callback_fn, callback_data,
                                           screen.findbuf,
index 5148bd48ffa7ae6b13ad8b84e5abb865640a0b98..7753e228b87ffbe6dfbc85bec1dd9f831584f666 100644 (file)
@@ -25,7 +25,6 @@
 
 /* query user for a string and find it in a list window */
 int screen_find(struct list_window *lw,
-               int rows,
                command_t findcmd,
                list_window_callback_fn_t callback_fn,
                void *callback_data);
index 4d501742bb0f2cf533382d361a680af92affe46e..eb738c360ce76d4df4a85227791289b5b40c1331 100644 (file)
@@ -224,6 +224,7 @@ help_init(WINDOW *w, int cols, int rows)
 {
   lw = list_window_init(w, cols, rows);
        lw->hide_cursor = true;
+       list_window_set_length(lw, G_N_ELEMENTS(help_text));
 }
 
 static void
@@ -255,17 +256,16 @@ help_paint(void)
 static bool
 help_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
 {
-       if (list_window_scroll_cmd(lw, help_text_rows, cmd)) {
+       if (list_window_scroll_cmd(lw, cmd)) {
                list_window_paint(lw, list_callback, NULL);
                wrefresh(lw->w);
                return true;
        }
 
        list_window_set_cursor(lw, lw->start);
-       if (screen_find(lw,  help_text_rows,
-                       cmd, list_callback, NULL)) {
+       if (screen_find(lw,  cmd, list_callback, NULL)) {
                /* center the row */
-               list_window_center(lw, help_text_rows, lw->selected);
+               list_window_center(lw, lw->selected);
                list_window_paint(lw, list_callback, NULL);
                wrefresh(lw->w);
                return true;
index 3261bc936d52226f8d069fcc8cea06af97377879..6585bc45825e7aa331689eef64a9dd1f2405d371 100644 (file)
@@ -117,6 +117,7 @@ check_subcmd_length(void)
        } else
                subcmd_addpos = 0;
        subcmd_length += STATIC_SUB_ITEMS;
+       list_window_set_length(lw, subcmd_length);
 }
 
 static void
@@ -263,7 +264,7 @@ keydef_open(G_GNUC_UNUSED struct mpdclient *c)
        }
 
        subcmd = -1;
-       list_window_check_selected(lw, LIST_LENGTH());
+       list_window_set_length(lw, LIST_LENGTH());
 }
 
 static void
@@ -295,15 +296,10 @@ keydef_paint(void)
 static bool
 keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
 {
-       int length = LIST_LENGTH();
-
-       if (subcmd >= 0)
-               length = subcmd_length;
-
        if (cmd == CMD_LIST_RANGE_SELECT)
                return false;
 
-       if (list_window_cmd(lw, length, cmd)) {
+       if (list_window_cmd(lw, cmd)) {
                keydef_repaint();
                return true;
        }
@@ -325,6 +321,7 @@ keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
                        }
                } else {
                        if (lw->selected == 0) { /* up */
+                               list_window_set_length(lw, LIST_LENGTH());
                                list_window_set_cursor(lw, subcmd);
                                subcmd = -1;
 
@@ -336,6 +333,7 @@ keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
                return true;
        case CMD_GO_PARENT_DIRECTORY:
                if (subcmd >=0) {
+                       list_window_set_length(lw, LIST_LENGTH());
                        list_window_set_cursor(lw, subcmd);
                        subcmd = -1;
 
@@ -355,8 +353,7 @@ keydef_cmd(G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(lw, length,
-                           cmd, list_callback, NULL);
+               screen_find(lw, cmd, list_callback, NULL);
                keydef_repaint();
                return true;
 
index 4857e489297f71f8c52e9b65427fee3f4ee30f9a..846a22b50035bc09ae46f0f63e37f4493e20a543 100644 (file)
@@ -122,6 +122,8 @@ fill_outputs_list(struct mpdclient *c)
 
        if (!mpd_response_finish(connection))
                mpdclient_handle_error(c);
+
+       list_window_set_length(lw, mpd_outputs->len);
 }
 
 static const char *
@@ -205,7 +207,7 @@ outputs_cmd(struct mpdclient *c, command_t cmd)
 {
        assert(mpd_outputs != NULL);
 
-       if (list_window_cmd(lw, mpd_outputs->len, cmd)) {
+       if (list_window_cmd(lw, cmd)) {
                outputs_repaint();
                return true;
        }
index 784667b0bdbf143fb1202295e76e7db6a484e5ed..05457f76263e95fafbed718c8d80caa401bd1d40 100644 (file)
@@ -99,6 +99,8 @@ playlist_restore_selection(void)
        const struct mpd_song *song;
        int pos;
 
+       list_window_set_length(lw, playlist_length(playlist));
+
        if (selected_song_id < 0)
                /* there was no selection */
                return;
@@ -113,7 +115,6 @@ playlist_restore_selection(void)
        if (pos >= 0)
                list_window_set_cursor(lw, pos);
 
-       list_window_check_selected(lw, playlist_length(playlist));
        playlist_save_selection();
 }
 
@@ -216,7 +217,7 @@ center_playing_item(struct mpdclient *c, bool center_cursor)
                return;
        }
 
-       list_window_center(lw, length, idx);
+       list_window_center(lw, idx);
 
        if (center_cursor) {
                list_window_set_cursor(lw, idx);
@@ -224,7 +225,7 @@ center_playing_item(struct mpdclient *c, bool center_cursor)
        }
 
        /* make sure the cursor is in the window */
-       list_window_fetch_cursor(lw, length);
+       list_window_fetch_cursor(lw);
 }
 
 #ifndef NCMPC_MINI
@@ -585,7 +586,7 @@ handle_mouse_event(struct mpdclient *c)
        unsigned long bstate;
 
        if (screen_get_mouse_event(c, &bstate, &row) ||
-           list_window_mouse(lw, playlist_length(playlist), bstate, row)) {
+           list_window_mouse(lw, bstate, row)) {
                playlist_repaint();
                return true;
        }
@@ -609,7 +610,6 @@ handle_mouse_event(struct mpdclient *c)
        }
 
        list_window_set_cursor(lw, selected);
-       list_window_check_selected(lw, playlist_length(playlist));
        playlist_save_selection();
        playlist_repaint();
 
@@ -634,7 +634,7 @@ screen_playlist_cmd(struct mpdclient *c, command_t cmd)
                                                     timer_hide_cursor, c);
        }
 
-       if (list_window_cmd(lw, playlist_length(&c->playlist), cmd)) {
+       if (list_window_cmd(lw, cmd)) {
                playlist_save_selection();
                playlist_repaint();
                return true;
@@ -656,8 +656,7 @@ screen_playlist_cmd(struct mpdclient *c, command_t cmd)
        case CMD_LIST_RFIND:
        case CMD_LIST_FIND_NEXT:
        case CMD_LIST_RFIND_NEXT:
-               screen_find(lw, playlist_length(&c->playlist),
-                           cmd, list_callback, NULL);
+               screen_find(lw, cmd, list_callback, NULL);
                playlist_save_selection();
                playlist_repaint();
                return true;
index b2b6b113a9b719238c2cbfa9ac43e6caeb962a4c..dc4b89dd6f53173dff7989d891aef26a63301937 100644 (file)
@@ -147,6 +147,7 @@ search_clear(bool clear_pattern)
        if (browser.filelist) {
                filelist_free(browser.filelist);
                browser.filelist = filelist_new();
+               list_window_set_length(browser.lw, 0);
        }
        if (clear_pattern && pattern) {
                g_free(pattern);
@@ -328,10 +329,9 @@ screen_search_reload(struct mpdclient *c)
        browser.filelist = do_search(c, pattern);
        if (browser.filelist == NULL)
                browser.filelist = filelist_new();
+       list_window_set_length(browser.lw, filelist_length(browser.filelist));
 
        screen_browser_sync_highlights(browser.filelist, &c->playlist);
-       list_window_check_selected(browser.lw,
-                                  filelist_length(browser.filelist));
 }
 
 static void
@@ -360,6 +360,7 @@ static void
 screen_search_init(WINDOW *w, int cols, int rows)
 {
        browser.lw = list_window_init(w, cols, rows);
+       list_window_set_length(browser.lw, G_N_ELEMENTS(help_text));
 }
 
 static void
index 01e2e340ae72ded67d768ba0633e869b71376958..28d78028bf2b96b4dee579377a15fc11f556ce88 100644 (file)
@@ -375,13 +375,14 @@ screen_song_update(struct mpdclient *c)
            !screen_song_add_stats(mpdclient_get_connection(c)))
                mpdclient_handle_error(c);
 
+       list_window_set_length(lw, 0);
        screen_song_repaint();
 }
 
 static bool
 screen_song_cmd(struct mpdclient *c, command_t cmd)
 {
-       if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
+       if (list_window_scroll_cmd(lw, cmd)) {
                screen_song_repaint();
                return true;
        }
@@ -425,10 +426,9 @@ screen_song_cmd(struct mpdclient *c, command_t cmd)
                break;
        }
 
-       if (screen_find(lw, current.lines->len,
-                       cmd, screen_song_list_callback, NULL)) {
+       if (screen_find(lw, cmd, screen_song_list_callback, NULL)) {
                /* center the row */
-               list_window_center(lw, current.lines->len, lw->selected);
+               list_window_center(lw, lw->selected);
                screen_song_repaint();
                return true;
        }
index 56e41d3e07bba2b60cb565ca62f0b06f8882eb77..4660f2851c06b90cbafb079c3a02e52da6a99969 100644 (file)
@@ -33,6 +33,7 @@ screen_text_clear(struct screen_text *text)
                g_free(g_ptr_array_index(text->lines, i));
 
        g_ptr_array_set_size(text->lines, 0);
+       list_window_set_length(text->lw, 0);
 }
 
 void
@@ -74,6 +75,8 @@ screen_text_set(struct screen_text *text, const GString *str)
 
        if (*p != 0)
                g_ptr_array_add(text->lines, g_strdup(p));
+
+       list_window_set_length(text->lw, text->lines->len);
 }
 
 const char *
@@ -98,17 +101,15 @@ bool
 screen_text_cmd(struct screen_text *text,
                G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
 {
-       if (list_window_scroll_cmd(text->lw, text->lines->len, cmd)) {
+       if (list_window_scroll_cmd(text->lw, cmd)) {
                screen_text_repaint(text);
                return true;
        }
 
        list_window_set_cursor(text->lw, text->lw->start);
-       if (screen_find(text->lw, text->lines->len,
-                       cmd, screen_text_list_callback, text)) {
+       if (screen_find(text->lw, cmd, screen_text_list_callback, text)) {
                /* center the row */
-               list_window_center(text->lw, text->lines->len,
-                                  text->lw->selected);
+               list_window_center(text->lw, text->lw->selected);
                screen_text_repaint(text);
                return true;
        }