Code

screen_text: (API) use (char *) for strings
[ncmpc.git] / src / screen_text.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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.
18  */
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)
29 {
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);
37 }
39 void
40 screen_text_set(struct screen_text *text, const char *str)
41 {
42         const char *eol, *next;
44         assert(str != NULL);
46         screen_text_clear(text);
48         while ((eol = strchr(str, '\n')) != NULL) {
49                 char *line;
51                 next = eol + 1;
53                 /* strip whitespace at end */
55                 while (eol > str && (unsigned char)eol[-1] <= 0x20)
56                         --eol;
58                 /* create copy and append it to text->lines */
60                 line = g_malloc(eol - str + 1);
61                 memcpy(line, str, eol - str);
62                 line[eol - str] = 0;
64                 g_ptr_array_add(text->lines, line);
66                 /* reset control characters */
68                 for (eol = line + (eol - str); line < eol; ++line)
69                         if ((unsigned char)*line < 0x20)
70                                 *line = ' ';
72                 str = next;
73         }
75         if (*str != 0)
76                 g_ptr_array_add(text->lines, g_strdup(str));
78         list_window_set_length(text->lw, text->lines->len);
79 }
81 const char *
82 screen_text_list_callback(unsigned idx, void *data)
83 {
84         const struct screen_text *text = data;
85         static char buffer[256];
86         char *value;
88         assert(idx < text->lines->len);
90         value = utf8_to_locale(g_ptr_array_index(text->lines, idx));
91         g_strlcpy(buffer, value, sizeof(buffer));
92         g_free(value);
94         return buffer;
95 }
97 bool
98 screen_text_cmd(struct screen_text *text,
99                 G_GNUC_UNUSED struct mpdclient *c, command_t cmd)
101         if (list_window_scroll_cmd(text->lw, cmd)) {
102                 screen_text_repaint(text);
103                 return true;
104         }
106         list_window_set_cursor(text->lw, text->lw->start);
107         if (screen_find(text->lw, cmd, screen_text_list_callback, text)) {
108                 /* center the row */
109                 list_window_center(text->lw, text->lw->selected);
110                 screen_text_repaint(text);
111                 return true;
112         }
114         return false;