Code

list_window: check for unchanged value in set_length()
[ncmpc.git] / src / list_window.c
index 0150fd6c3cee16fcb56a3c26e0a644051f6a707d..4fe984dea796425c121e23ce3dc4e3df94aac3fd 100644 (file)
@@ -24,6 +24,7 @@
 #include "match.h"
 #include "command.h"
 #include "colors.h"
+#include "paint.h"
 #include "screen_message.h"
 #include "i18n.h"
 
@@ -126,6 +127,9 @@ list_window_resize(struct list_window *lw, unsigned width, unsigned height)
 void
 list_window_set_length(struct list_window *lw, unsigned length)
 {
+       if (length == lw->length)
+               return;
+
        lw->length = length;
 
        list_window_check_selected(lw);
@@ -321,53 +325,11 @@ 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;
-       highlight = false;
-#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);
-
-       /* erase the unused space after the text */
-       if (text_width < width) {
-               if (options.wide_cursor)
-                       whline(w, ' ', width - text_width);
-               else
-                       wclrtoeol(w);
-       }
-
-       if (second_column_width > 0) {
-               wmove(w, y, width);
-               waddch(w, ' ');
-               waddstr(w, second_column);
-       }
-
-       if (selected)
-               wattroff(w, A_REVERSE);
+       row_paint_text(w, width, COLOR_LIST,
+                      selected, text);
 }
 
 void
@@ -384,8 +346,6 @@ list_window_paint(const struct list_window *lw,
 
        for (unsigned i = 0; i < lw->rows; i++) {
                const char *label;
-               bool highlight = false;
-               char *second_column = NULL;
 
                wmove(lw->w, i, 0);
 
@@ -394,23 +354,53 @@ list_window_paint(const struct list_window *lw,
                        break;
                }
 
-               label = callback(lw->start + i, &highlight, &second_column, callback_data);
+               label = callback(lw->start + i, callback_data);
                assert(label != NULL);
 
-#ifdef NCMPC_MINI
-               highlight = false;
-               second_column = NULL;
-#endif /* NCMPC_MINI */
-
-               list_window_paint_row(lw->w, i, lw->cols,
+               list_window_paint_row(lw->w, lw->cols,
                                      show_cursor &&
                                      lw->start + i >= range.start &&
                                      lw->start + i < range.end,
-                                     highlight,
-                                     label, second_column);
+                                     label);
+       }
+
+       row_color_end(lw->w);
+
+       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);
+       }
+}
+
+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;
+
+       if (show_cursor)
+               list_window_get_range(lw, &range);
 
-               if (second_column != NULL)
-                       g_free(second_column);
+       for (unsigned i = 0; i < lw->rows; i++) {
+               bool selected;
+
+               wmove(lw->w, i, 0);
+
+               if (lw->start + i >= lw->length) {
+                       wclrtobot(lw->w);
+                       break;
+               }
+
+               selected = show_cursor &&
+                       lw->start + i >= range.start &&
+                       lw->start + i < range.end;
+
+               paint_callback(lw->w, lw->start + i, i, lw->cols,
+                              selected, callback_data);
        }
 
        if (options.hardware_cursor && lw->selected >= lw->start &&
@@ -428,7 +418,6 @@ list_window_find(struct list_window *lw,
                 bool wrap,
                 bool bell_on_wrap)
 {
-       bool h;
        unsigned i = lw->selected + 1;
        const char *label;
 
@@ -436,7 +425,7 @@ list_window_find(struct list_window *lw,
 
        do {
                while (i < lw->length) {
-                       label = callback(i, &h, NULL, callback_data);
+                       label = callback(i, callback_data);
                        assert(label != NULL);
 
                        if (match_line(label, str)) {
@@ -468,7 +457,6 @@ list_window_rfind(struct list_window *lw,
                  bool wrap,
                  bool bell_on_wrap)
 {
-       bool h;
        int i = lw->selected - 1;
        const char *label;
 
@@ -479,7 +467,7 @@ list_window_rfind(struct list_window *lw,
 
        do {
                while (i >= 0) {
-                       label = callback(i, &h, NULL, callback_data);
+                       label = callback(i, callback_data);
                        assert(label != NULL);
 
                        if (match_line(label, str)) {
@@ -524,18 +512,14 @@ list_window_jump(struct list_window *lw,
                 void *callback_data,
                 const char *str)
 {
-       bool h;
        const char *label;
 
        assert(str != NULL);
 
        for (unsigned i = 0; i < lw->length; ++i) {
-               label = callback(i, &h, NULL, callback_data);
+               label = callback(i, callback_data);
                assert(label != NULL);
 
-               if (label[0] == '[')
-                       label++;
-
                if (jump_match(label, str)) {
                        list_window_move_cursor(lw, i);
                        return true;