Code

list_window: added list_window_center()
authorMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 10:20:42 +0000 (12:20 +0200)
committerMax Kellermann <max@duempel.org>
Wed, 17 Sep 2008 10:20:42 +0000 (12:20 +0200)
Merge code which used to be duplicated in the screens "play", "help",
"lyrics".

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

index f26088da482d1cda8a48475bc3a90abe155e9f58..ad671f0cf51ad1a36715e6e1623731d36cb1acc5 100644 (file)
@@ -81,6 +81,24 @@ list_window_check_selected(struct list_window *lw, unsigned length)
                lw->selected = length - 1;
 }
 
+void
+list_window_center(struct list_window *lw, unsigned rows, 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;
+               else
+                       lw->start = 0;
+       }
+
+       lw->repaint = lw->clear = 1;
+}
+
 void
 list_window_set_selected(struct list_window *lw, unsigned n)
 {
index ab02f4bdd759a29c0e7300b861e16530f9597316..3a9fba4c313cb5bda9d3ea3d0e592f42c6eb952e 100644 (file)
@@ -56,6 +56,8 @@ int list_window_cmd(struct list_window *lw, unsigned rows, command_t cmd);
 int
 list_window_scroll_cmd(struct list_window *lw, unsigned rows, command_t cmd);
 
+void
+list_window_center(struct list_window *lw, unsigned rows, unsigned n);
 
 /* select functions */
 void list_window_set_selected(struct list_window *lw, unsigned n);
index 850608181cb123a6598a34310dc92b4d3f9f7f64..363b440cf8f804bb05b7a3eb94331f747b3b0698 100644 (file)
@@ -248,16 +248,7 @@ help_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
                        lw,  help_text_rows,
                        cmd, list_callback, NULL)) {
                /* center the row */
-               if (lw->selected > lw->rows / 2)
-                       lw->start = lw->selected - lw->rows / 2;
-               else
-                       lw->start = 0;
-               if (lw->start + lw->rows > (unsigned)help_text_rows) {
-                       if (lw->rows < (unsigned)help_text_rows)
-                               lw->start = help_text_rows - lw->rows;
-                       else
-                               lw->start = 0;
-               }
+               list_window_center(lw, help_text_rows, lw->selected);
                return 1;
        }
 
index 5356548c90b5ef691f09196696ceaf28672a9350..6853fecd72bb2a9dc494d2ca3fb435d1d51910e1 100644 (file)
@@ -292,8 +292,6 @@ 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_SELECT:
                /* XXX */
@@ -329,13 +327,7 @@ lyrics_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
                        lw,  lyrics_text_rows,
                        cmd, list_callback, NULL)) {
                /* center the row */
-               lw->start = lw->selected - (lw->rows / 2);
-               if (lw->start + lw->rows > (unsigned)lyrics_text_rows) {
-                       if (lw->rows < (unsigned)lyrics_text_rows)
-                               lw->start = lyrics_text_rows - lw->rows;
-                       else
-                               lw->start = 0;
-               }
+               list_window_center(lw, current.lines->len, lw->selected);
                return 1;
        }
 
index 6b4de78932a2990ef78ebfa5df1e1a7243453e76..0426176d602b52898540cc95384c607c723f97c8 100644 (file)
@@ -106,20 +106,15 @@ center_playing_item(mpdclient_t *c)
        /* try to center the song that are playing */
        idx = playlist_get_index(c, c->song);
        D("Autocenter song id:%d pos:%d index:%d\n", c->song->id,c->song->pos,idx);
-       idx -= (lw->rows / 2);
-       if (idx + (int)lw->rows > (int)length)
-               idx = length - lw->rows;
        if (idx < 0)
-               idx = 0;
-       lw->start = idx;
+               return 0;
+
+       list_window_center(lw, length, idx);
 
        /* make sure the cursor is in the window */
        lw->selected = lw->start+offset;
        list_window_check_selected(lw, length);
 
-       lw->clear = 1;
-       lw->repaint = 1;
-
        return 0;
 }