Code

list_window: added list_window_scroll_cmd()
authorMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 10:18:00 +0000 (12:18 +0200)
committerMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 10:18:00 +0000 (12:18 +0200)
It is similar to list_window_cmd(), but it scrolls the window instead
of moving the selection.  It is used by the "lyrics" and the "help"
screen.

src/list_window.c
src/list_window.h
src/screen_help.c
src/screen_lyrics.c

index 88f0a690c739f74316872331cb536785f568cda2..f26088da482d1cda8a48475bc3a90abe155e9f58 100644 (file)
@@ -296,6 +296,56 @@ list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd)
        return 1;
 }
 
+int
+list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd)
+{
+       switch (cmd) {
+       case CMD_LIST_PREVIOUS:
+               if (lw->start > 0)
+                       lw->start--;
+               break;
+
+       case CMD_LIST_NEXT:
+               if (lw->start + lw->rows < rows)
+                       lw->start++;
+               break;
+
+       case CMD_LIST_FIRST:
+               lw->start = 0;
+               break;
+
+       case CMD_LIST_LAST:
+               if (rows > lw->rows)
+                       lw->start = rows - 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;
+                       else
+                               lw->start = 0;
+               }
+               break;
+
+       case CMD_LIST_PREVIOUS_PAGE:
+               if (lw->start > lw->rows)
+                       lw->start -= lw->rows;
+               else
+                       lw->start = 0;
+               break;
+
+       default:
+               return 0;
+       }
+
+       lw->repaint = lw->clear = 1;
+       return 1;
+}
+
 list_window_state_t *
 list_window_init_state(void)
 {
index 2efff8a5ca7857ba0109db2cbd7b11665cbc7467..ab02f4bdd759a29c0e7300b861e16530f9597316 100644 (file)
@@ -49,6 +49,13 @@ void list_window_paint(struct list_window *lw,
 /* perform basic list window commands (movement) */
 int list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd);
 
+/**
+ * Scroll the window.  Returns non-zero if the command has been
+ * consumed.
+ */
+int
+list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd);
+
 
 /* select functions */
 void list_window_set_selected(struct list_window *lw, unsigned n);
index 7c5134fc6040986d3ccf1b1850841572f9346a60..850608181cb123a6598a34310dc92b4d3f9f7f64 100644 (file)
@@ -240,44 +240,8 @@ help_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
 static int
 help_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
 {
-       lw->repaint=1;
-       lw->clear=1;
-       switch(cmd) {
-       case CMD_LIST_NEXT:
-               if (lw->start + lw->rows < (unsigned)help_text_rows)
-                       lw->start++;
+       if (list_window_scroll_cmd(lw, help_text_rows, cmd))
                return 1;
-       case CMD_LIST_PREVIOUS:
-               if (lw->start > 0)
-                       lw->start--;
-               return 1;
-       case CMD_LIST_FIRST:
-               lw->start = 0;
-               return 1;
-       case CMD_LIST_LAST:
-               if ((unsigned)help_text_rows > lw->rows)
-                       lw->start = help_text_rows - lw->rows;
-               else
-                       lw->start = 0;
-               return 1;
-       case CMD_LIST_NEXT_PAGE:
-               lw->start = lw->start + lw->rows;
-               if (lw->start + lw->rows >= (unsigned)help_text_rows) {
-                       if ((unsigned)help_text_rows > lw->rows)
-                               lw->start = help_text_rows - lw->rows;
-                       else
-                               lw->start = 0;
-               }
-               return 1;
-       case CMD_LIST_PREVIOUS_PAGE:
-               if (lw->start > lw->rows)
-                       lw->start -= lw->rows;
-               else
-                       lw->start = 0;
-               return 1;
-       default:
-               break;
-       }
 
        lw->selected = lw->start+lw->rows;
        if (screen_find(screen,
index e6e2cf0dff9598285e5887fea091407e77b9a020..5356548c90b5ef691f09196696ceaf28672a9350 100644 (file)
@@ -289,40 +289,12 @@ lyrics_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
 static int
 lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
 {
+       if (list_window_scroll_cmd(lw, current.lines->len, cmd))
+               return 1;
+
        lw->repaint=1;
+
        switch(cmd) {
-       case CMD_LIST_NEXT:
-               if (current.lines != NULL && lw->start+lw->rows < current.lines->len+1)
-                       lw->start++;
-               return 1;
-       case CMD_LIST_PREVIOUS:
-               if( lw->start >0 )
-                       lw->start--;
-               return 1;
-       case CMD_LIST_FIRST:
-               lw->start = 0;
-               return 1;
-       case CMD_LIST_LAST:
-               if ((unsigned)lyrics_text_rows > lw->rows)
-                       lw->start = lyrics_text_rows - lw->rows;
-               else
-                       lw->start = 0;
-               return 1;
-       case CMD_LIST_NEXT_PAGE:
-               lw->start = lw->start + lw->rows - 1;
-               if (lw->start + lw->rows >= (unsigned)lyrics_text_rows + 1) {
-                       if ((unsigned)lyrics_text_rows + 1 > lw->rows)
-                               lw->start = lyrics_text_rows + 1 - lw->rows;
-                       else
-                               lw->start = 0;
-               }
-               return 1;
-       case CMD_LIST_PREVIOUS_PAGE:
-               if (lw->start > lw->rows)
-                       lw->start -= lw->rows;
-               else
-                       lw->start = 0;
-               return 1;
        case CMD_SELECT:
                /* XXX */
                if (current.loader != NULL) {