configure.ac: require libmpdclient 2.2
[ncmpc.git] / src / screen_text.c
blob617d38e2b592d0cf27653f09f651d667fd4f8564
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "screen_text.h"
21 #include "screen_find.h"
22 #include "charset.h"
24 #include <assert.h>
25 #include <string.h>
27 void
28 screen_text_clear(struct screen_text *text)
30 list_window_reset(text->lw);
32 for (guint i = 0; i < text->lines->len; ++i)
33 g_free(g_ptr_array_index(text->lines, i));
35 g_ptr_array_set_size(text->lines, 0);
36 list_window_set_length(text->lw, 0);
39 void
40 screen_text_set(struct screen_text *text, const GString *str)
42 const char *p, *eol, *next;
44 assert(str != NULL);
46 screen_text_clear(text);
48 p = str->str;
49 while ((eol = strchr(p, '\n')) != NULL) {
50 char *line;
52 next = eol + 1;
54 /* strip whitespace at end */
56 while (eol > p && (unsigned char)eol[-1] <= 0x20)
57 --eol;
59 /* create copy and append it to text->lines */
61 line = g_malloc(eol - p + 1);
62 memcpy(line, p, eol - p);
63 line[eol - p] = 0;
65 g_ptr_array_add(text->lines, line);
67 /* reset control characters */
69 for (eol = line + (eol - p); line < eol; ++line)
70 if ((unsigned char)*line < 0x20)
71 *line = ' ';
73 p = next;
76 if (*p != 0)
77 g_ptr_array_add(text->lines, g_strdup(p));
79 list_window_set_length(text->lw, text->lines->len);
82 const char *
83 screen_text_list_callback(unsigned idx, void *data)
85 const struct screen_text *text = data;
86 static char buffer[256];
87 char *value;
89 assert(idx < text->lines->len);
91 value = utf8_to_locale(g_ptr_array_index(text->lines, idx));
92 g_strlcpy(buffer, value, sizeof(buffer));
93 g_free(value);
95 return buffer;
98 bool
99 screen_text_cmd(struct screen_text *text,
100 G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
102 if (list_window_scroll_cmd(text->lw, cmd)) {
103 screen_text_repaint(text);
104 return true;
107 list_window_set_cursor(text->lw, text->lw->start);
108 if (screen_find(text->lw, cmd, screen_text_list_callback, text)) {
109 /* center the row */
110 list_window_center(text->lw, text->lw->selected);
111 screen_text_repaint(text);
112 return true;
115 return false;