From 271507f1f1b9a84a68e81a66a0c9440112a0ed20 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 11 Oct 2009 14:15:38 +0200 Subject: [PATCH] list_window: moved painting utilities to paint.h 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 | 1 + src/list_window.c | 22 +++---------- src/paint.h | 81 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 17 deletions(-) create mode 100644 src/paint.h diff --git a/Makefile.am b/Makefile.am index 93fdf31..ca5264f 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 \ diff --git a/src/list_window.c b/src/list_window.c index 0150fd6..0b60314 100644 --- a/src/list_window.c +++ b/src/list_window.c @@ -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 index 0000000..4d8ef71 --- /dev/null +++ b/src/paint.h @@ -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 -- 2.30.2