Code

Merge remote branches 'jn/cosmetics', 'jn/doxygen' and 'jn/renames'
[ncmpc.git] / src / list_window.c
index d7da155fdb6e50788af3e52052b4ec9b7f93ffce..ad2ae77cc1f588b7e691d17b2079756f148dda62 100644 (file)
@@ -1,21 +1,21 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2010 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
-
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
-
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
-
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
-*/
+ */
 
 #include "list_window.h"
 #include "config.h"
@@ -24,7 +24,8 @@
 #include "match.h"
 #include "command.h"
 #include "colors.h"
-#include "screen_message.h"
+#include "paint.h"
+#include "screen_status.h"
 #include "i18n.h"
 
 #include <assert.h>
@@ -59,64 +60,61 @@ void
 list_window_reset(struct list_window *lw)
 {
        lw->selected = 0;
-       lw->selected_start = 0;
-       lw->selected_end = 0;
        lw->range_selection = false;
        lw->range_base = 0;
        lw->start = 0;
 }
 
+static unsigned
+list_window_validate_index(const struct list_window *lw, unsigned i)
+{
+       if (lw->length == 0)
+               return 0;
+       else if (i >= lw->length)
+               return lw->length - 1;
+       else
+               return i;
+}
+
 static void
 list_window_check_selected(struct list_window *lw)
 {
-       if (lw->start + lw->rows > lw->length) {
-               if (lw->length > lw->rows)
-                       lw->start = lw->length - lw->rows;
-               else
-                       lw->start = 0;
-       }
+       lw->selected = list_window_validate_index(lw, lw->selected);
 
-       if (lw->selected < lw->start)
-               lw->selected = lw->start;
+       if(lw->range_selection)
+               lw->range_base =
+                       list_window_validate_index(lw, lw->range_base);
+}
 
-       if (lw->length == 0)
-               lw->selected = 0;
-       else if (lw->selected >= lw->length)
-               lw->selected = lw->length - 1;
+/**
+ * Scroll after the cursor was moved, the list was changed or the
+ * window was resized.
+ */
+static void
+list_window_check_origin(struct list_window *lw)
+{
+       list_window_scroll_to(lw, lw->selected);
+}
 
-       if(lw->range_selection)
-       {
-               if (lw->length == 0) {
-                       lw->selected_start = 0;
-                       lw->selected_end = 0;
-                       lw->range_base = 0;
-               } else {
-                       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;
-               }
+void
+list_window_resize(struct list_window *lw, unsigned width, unsigned height)
+{
+       lw->cols = width;
+       lw->rows = height;
 
-               if(lw->range_base > lw->selected_end)
-                         lw->selected_end = lw->selected;
-               if(lw->range_base < lw->selected_start)
-                         lw->selected_start = lw->selected;
-       }
-       else
-       {
-               lw->selected_start = lw->selected;
-               lw->selected_end = lw->selected;
-       }
+       list_window_check_origin(lw);
 }
 
 void
 list_window_set_length(struct list_window *lw, unsigned length)
 {
+       if (length == lw->length)
+               return;
+
        lw->length = length;
 
        list_window_check_selected(lw);
+       list_window_check_origin(lw);
 }
 
 void
@@ -135,72 +133,82 @@ 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)
 {
        lw->range_selection = false;
        lw->selected = i;
-       lw->selected_start = i;
-       lw->selected_end = i;
 
        list_window_check_selected(lw);
+       list_window_check_origin(lw);
 }
 
 void
 list_window_move_cursor(struct list_window *lw, unsigned n)
 {
        lw->selected = n;
-       if(lw->range_selection)
-       {
-               if(n >= lw->range_base)
-               {
-                       lw->selected_end = n;
-                       lw->selected_start = lw->range_base;
-               }
-               if(n <= lw->range_base)
-               {
-                       lw->selected_start = n;
-                       lw->selected_end = lw->range_base;
-               }
-       }
-       else
-       {
-               lw->selected_start = n;
-               lw->selected_end = n;
-       }
+
+       list_window_check_selected(lw);
+       list_window_check_origin(lw);
 }
 
 void
 list_window_fetch_cursor(struct list_window *lw)
 {
-       if (lw->selected < lw->start + options.scroll_offset) {
-               if (lw->start > 0)
-                       lw->selected = lw->start + options.scroll_offset;
-               if (lw->range_selection) {
-                       if (lw->selected > lw->range_base) {
-                               lw->selected_end = lw->selected;
-                               lw->selected_start = lw->range_base;
-                       } else {
-                               lw->selected_start = lw->selected;
-                       }
-               } else {
-                       lw->selected_start = lw->selected;
-                       lw->selected_end = lw->selected;
-               }
-       } else if (lw->selected > lw->start + lw->rows - 1 - options.scroll_offset) {
-               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) {
-                               lw->selected_start = lw->selected;
-                               lw->selected_end = lw->range_base;
-                       } else {
-                               lw->selected_end = lw->selected;
-                       }
+       if (lw->start > 0 &&
+           lw->selected < lw->start + options.scroll_offset)
+               list_window_move_cursor(lw, lw->start + options.scroll_offset);
+       else if (lw->start + lw->rows < lw->length &&
+                lw->selected > lw->start + lw->rows - 1 - options.scroll_offset)
+               list_window_move_cursor(lw, lw->start + lw->rows - 1 - options.scroll_offset);
+}
+
+void
+list_window_get_range(const struct list_window *lw,
+                     struct list_window_range *range)
+{
+       if (lw->length == 0) {
+               /* empty list - no selection */
+               range->start = 0;
+               range->end = 0;
+       } else if (lw->range_selection) {
+               /* a range selection */
+               if (lw->range_base < lw->selected) {
+                       range->start = lw->range_base;
+                       range->end = lw->selected + 1;
                } else {
-                       lw->selected_start = lw->selected;
-                       lw->selected_end = lw->selected;
+                       range->start = lw->selected;
+                       range->end = lw->range_base + 1;
                }
+       } else {
+               /* no range, just the cursor */
+               range->start = lw->selected;
+               range->end = lw->selected + 1;
        }
 }
 
@@ -313,7 +321,7 @@ list_window_scroll_down(struct list_window *lw, unsigned n)
 {
        if (lw->start + lw->rows < lw->length)
        {
-               if ( lw->start + lw->rows + n > lw->length - 1)
+               if (lw->start + lw->rows + n > lw->length - 1)
                        lw->start = lw->length - lw->rows;
                else
                        lw->start += n;
@@ -323,100 +331,68 @@ list_window_scroll_down(struct list_window *lw, unsigned n)
 }
 
 static void
-list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
-                     bool selected, bool highlight,
-                     const char *text, const char *second_column)
+list_window_paint_row(WINDOW *w, unsigned width, bool selected,
+                     const char *text)
 {
-       unsigned text_width = utf8_width(text);
-       unsigned second_column_width;
-
-#ifdef NCMPC_MINI
-       second_column = NULL;
-#endif /* NCMPC_MINI */
-
-       if (second_column != NULL) {
-               second_column_width = utf8_width(second_column) + 1;
-               if (second_column_width < width)
-                       width -= second_column_width;
-               else
-                       second_column_width = 0;
-       } else
-               second_column_width = 0;
-
-       if (highlight)
-               colors_use(w, COLOR_LIST_BOLD);
-       else
-               colors_use(w, COLOR_LIST);
-
-       if (selected)
-               wattron(w, A_REVERSE);
-
-       waddstr(w, text);
-       if (options.wide_cursor && text_width < width)
-               whline(w, ' ', width - text_width);
-
-       if (second_column_width > 0) {
-               wmove(w, y, width);
-               waddch(w, ' ');
-               waddstr(w, second_column);
-       }
-
-       if (selected)
-               wattroff(w, A_REVERSE);
-
-       if (!options.wide_cursor && text_width < width) {
-               if (second_column_width == 0)
-                       /* the cursor is at the end of the text; clear
-                          the rest of this row */
-                       wclrtoeol(w);
-               else
-                       /* there's a second column: clear the space
-                          between the first and the second column */
-                       mvwhline(w, y, text_width, ' ', width - text_width);
-       }
+       row_paint_text(w, width, COLOR_LIST,
+                      selected, text);
 }
 
 void
-list_window_paint(struct list_window *lw,
+list_window_paint(const struct list_window *lw,
                  list_window_callback_fn_t callback,
                  void *callback_data)
 {
-       unsigned i;
-       bool show_cursor = !lw->hide_cursor;
-       bool highlight = false;
+       bool show_cursor = !lw->hide_cursor &&
+               (!options.hardware_cursor || lw->range_selection);
+       struct list_window_range range;
 
-       if (show_cursor) {
-               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 (show_cursor)
+               list_window_get_range(lw, &range);
 
-                       if (lw->selected >= lw->start + lw->rows - options.scroll_offset)
-                       {
-                               start = lw->selected - lw->rows + 1 + options.scroll_offset;
-                       }
+       for (unsigned i = 0; i < lw->rows; i++) {
+               const char *label;
+
+               wmove(lw->w, i, 0);
+
+               if (lw->start + i >= lw->length) {
+                       wclrtobot(lw->w);
+                       break;
                }
 
-               if (start + lw->rows > lw->length)
-                       start = lw->length - lw->rows;
+               label = callback(lw->start + i, callback_data);
+               assert(label != NULL);
 
-               if (start < 0 || lw->length == 0)
-                       start = 0;
+               list_window_paint_row(lw->w, lw->cols,
+                                     show_cursor &&
+                                     lw->start + i >= range.start &&
+                                     lw->start + i < range.end,
+                                     label);
+       }
+
+       row_color_end(lw->w);
 
-               lw->start = start;
+       if (options.hardware_cursor && lw->selected >= lw->start &&
+           lw->selected < lw->start + lw->rows) {
+               curs_set(1);
+               wmove(lw->w, lw->selected - lw->start, 0);
        }
+}
 
-       show_cursor = show_cursor &&
+void
+list_window_paint2(const struct list_window *lw,
+                  list_window_paint_callback_t paint_callback,
+                  void *callback_data)
+{
+       bool show_cursor = !lw->hide_cursor &&
                (!options.hardware_cursor || lw->range_selection);
+       struct list_window_range range;
 
-       for (i = 0; i < lw->rows; i++) {
-               const char *label;
-               char *second_column = NULL;
-               highlight = false;
+       if (show_cursor)
+               list_window_get_range(lw, &range);
+
+       for (unsigned i = 0; i < lw->rows; i++) {
+               bool selected;
 
                wmove(lw->w, i, 0);
 
@@ -425,18 +401,12 @@ list_window_paint(struct list_window *lw,
                        break;
                }
 
-               label = callback(lw->start + i, &highlight, &second_column, callback_data);
-               assert(label != NULL);
+               selected = show_cursor &&
+                       lw->start + i >= range.start &&
+                       lw->start + i < range.end;
 
-               list_window_paint_row(lw->w, i, lw->cols,
-                                     show_cursor &&
-                                     lw->start + i >= lw->selected_start &&
-                                     lw->start + i <= lw->selected_end,
-                                     highlight,
-                                     label, second_column);
-
-               if (second_column != NULL)
-                       g_free(second_column);
+               paint_callback(lw->w, lw->start + i, i, lw->cols,
+                              selected, callback_data);
        }
 
        if (options.hardware_cursor && lw->selected >= lw->start &&
@@ -454,16 +424,17 @@ list_window_find(struct list_window *lw,
                 bool wrap,
                 bool bell_on_wrap)
 {
-       bool h;
        unsigned i = lw->selected + 1;
        const char *label;
 
+       assert(str != NULL);
+
        do {
                while (i < lw->length) {
-                       label = callback(i, &h, NULL, callback_data);
+                       label = callback(i, callback_data);
                        assert(label != NULL);
 
-                       if (str && label && match_line(label, str)) {
+                       if (match_line(label, str)) {
                                list_window_move_cursor(lw, i);
                                return true;
                        }
@@ -492,19 +463,20 @@ list_window_rfind(struct list_window *lw,
                  bool wrap,
                  bool bell_on_wrap)
 {
-       bool h;
        int i = lw->selected - 1;
        const char *label;
 
+       assert(str != NULL);
+
        if (lw->length == 0)
                return false;
 
        do {
                while (i >= 0) {
-                       label = callback(i, &h, NULL, callback_data);
+                       label = callback(i, callback_data);
                        assert(label != NULL);
 
-                       if( str && label && match_line(label, str) ) {
+                       if (match_line(label, str)) {
                                list_window_move_cursor(lw, i);
                                return true;
                        }
@@ -523,35 +495,60 @@ list_window_rfind(struct list_window *lw,
        return false;
 }
 
+#ifdef NCMPC_MINI
 bool
 list_window_jump(struct list_window *lw,
                 list_window_callback_fn_t callback,
                 void *callback_data,
                 const char *str)
 {
-       bool h;
+       unsigned i;
        const char *label;
 
-       for (unsigned i = 0; i < lw->length; ++i) {
-               label = callback(i, &h, NULL, callback_data);
+       assert(str != NULL);
+
+       for (i = 0; i < lw->length; i++) {
+               label = callback(i, callback_data);
                assert(label != NULL);
 
-               if (label[0] == '[')
-                       label++;
-#ifndef NCMPC_MINI
-               if (str && label &&
-                               ((options.jump_prefix_only && g_ascii_strncasecmp(label, str, strlen(str)) == 0) ||
-                                (!options.jump_prefix_only && match_line(label, str))) )
+               if (g_ascii_strncasecmp(label, str, strlen(str)) == 0) {
+                       list_window_move_cursor(lw, i);
+                       return true;
+               }
+       }
+       return false;
+}
 #else
-               if (str && label && g_ascii_strncasecmp(label, str, strlen(str)) == 0)
-#endif
-               {
+bool
+list_window_jump(struct list_window *lw,
+                list_window_callback_fn_t callback,
+                void *callback_data,
+                const char *str)
+{
+       unsigned i;
+       const char *label;
+       GRegex *regex;
+
+       assert(str != NULL);
+
+       regex = compile_regex(str, options.jump_prefix_only);
+       if (regex == NULL)
+               return false;
+
+       for (i = 0; i < lw->length; i++) {
+               label = callback(i, callback_data);
+               assert(label != NULL);
+
+               if (match_regex(regex, label)) {
+                       g_regex_unref(regex);
                        list_window_move_cursor(lw, i);
                        return true;
                }
        }
+       g_regex_unref(regex);
        return false;
 }
+#endif
 
 /* perform basic list window commands (movement) */
 bool
@@ -645,7 +642,7 @@ list_window_scroll_cmd(struct list_window *lw, command_t cmd)
                break;
 
        case CMD_LIST_NEXT_PAGE:
-               lw->start += lw->rows - 1;
+               lw->start += lw->rows;
                if (lw->start + lw->rows > lw->length) {
                        if (lw->length > lw->rows)
                                lw->start = lw->length - lw->rows;