Code

list_window: moved painting utilities to paint.h
authorMax Kellermann <max@duempel.org>
Sun, 11 Oct 2009 12:15:38 +0000 (14:15 +0200)
committerMax Kellermann <max@duempel.org>
Sun, 11 Oct 2009 12:15:38 +0000 (14:15 +0200)
Minor optimization: pass the full row width to whline().  This way,
ncurses performs clipping for us, and we don't have to call
utf8_width().

Makefile.am
src/list_window.c
src/paint.h [new file with mode: 0644]

index 93fdf3162f6183b5b738ad3e382d74dd5deaf803..ca5264fab2767b2a68bdb8ca5564b5cc6f08c74d 100644 (file)
@@ -35,6 +35,7 @@ ncmpc_headers = \
        src/screen_client.h \
        src/list_window.h \
        src/colors.h \
+       src/paint.h \
        src/hscroll.h \
        src/charset.h \
        src/match.h \
index 0150fd6c3cee16fcb56a3c26e0a644051f6a707d..0b6031483e8854fc86bc410070fe9089bd45d540 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"
 
@@ -325,7 +326,6 @@ list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
                      bool selected, bool highlight,
                      const char *text, const char *second_column)
 {
-       unsigned text_width = utf8_width(text);
        unsigned second_column_width;
 
 #ifdef NCMPC_MINI
@@ -342,32 +342,18 @@ list_window_paint_row(WINDOW *w, unsigned y, unsigned width,
        } 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);
+       row_color(w, highlight ? COLOR_LIST_BOLD : COLOR_LIST, selected);
 
        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);
-       }
+       row_clear_to_eol(w, width, selected);
 
        if (second_column_width > 0) {
                wmove(w, y, width);
                waddch(w, ' ');
                waddstr(w, second_column);
        }
-
-       if (selected)
-               wattroff(w, A_REVERSE);
 }
 
 void
@@ -413,6 +399,8 @@ list_window_paint(const struct list_window *lw,
                        g_free(second_column);
        }
 
+       row_color_end(lw->w);
+
        if (options.hardware_cursor && lw->selected >= lw->start &&
            lw->selected < lw->start + lw->rows) {
                curs_set(1);
diff --git a/src/paint.h b/src/paint.h
new file mode 100644 (file)
index 0000000..4d8ef71
--- /dev/null
@@ -0,0 +1,81 @@
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2009 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.
+*/
+
+#ifndef NCMPC_PAINT_H
+#define NCMPC_PAINT_H
+
+#include "colors.h"
+#include "options.h"
+
+/**
+ * Sets the specified color, and enables "reverse" mode if selected is
+ * true.
+ */
+static inline void
+row_color(WINDOW *w, enum color color, bool selected)
+{
+       colors_use(w, color);
+
+       if (selected)
+               wattron(w, A_REVERSE);
+       else
+               wattroff(w, A_REVERSE);
+}
+
+/**
+ * Call this when you are done with painting rows.  It resets the
+ * "reverse" mode.
+ */
+static inline void
+row_color_end(WINDOW *w)
+{
+       wattroff(w, A_REVERSE);
+}
+
+/**
+ * Clears the remaining space on the current row.  If the row is
+ * selected and the wide_cursor option is enabled, it draws the cursor
+ * on the space.
+ */
+static inline void
+row_clear_to_eol(WINDOW *w, unsigned width, bool selected)
+{
+       if (selected && options.wide_cursor)
+               whline(w, ' ', width);
+       else
+               wclrtoeol(w);
+}
+
+/**
+ * Paint a plain-text row.
+ */
+static inline void
+row_paint_text(WINDOW *w, unsigned width,
+              enum color color, bool selected,
+              const char *text)
+{
+       row_color(w, color, selected);
+
+       waddstr(w, text);
+
+       /* erase the unused space after the text */
+       row_clear_to_eol(w, width, selected);
+}
+
+#endif