Code

Update copyright notices
[ncmpc.git] / src / screen_text.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2009 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.
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.
18 */
20 #include "screen_text.h"
21 #include "screen_utils.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 }
38 void
39 screen_text_set(struct screen_text *text, const GString *str)
40 {
41         const char *p, *eol, *next;
43         assert(str != NULL);
45         screen_text_clear(text);
47         p = str->str;
48         while ((eol = strchr(p, '\n')) != NULL) {
49                 char *line;
51                 next = eol + 1;
53                 /* strip whitespace at end */
55                 while (eol > p && (unsigned char)eol[-1] <= 0x20)
56                         --eol;
58                 /* create copy and append it to text->lines */
60                 line = g_malloc(eol - p + 1);
61                 memcpy(line, p, eol - p);
62                 line[eol - p] = 0;
64                 g_ptr_array_add(text->lines, line);
66                 /* reset control characters */
68                 for (eol = line + (eol - p); line < eol; ++line)
69                         if ((unsigned char)*line < 0x20)
70                                 *line = ' ';
72                 p = next;
73         }
75         if (*p != 0)
76                 g_ptr_array_add(text->lines, g_strdup(p));
77 }
79 const char *
80 screen_text_list_callback(unsigned idx, G_GNUC_UNUSED bool *highlight,
81                           void *data)
82 {
83         const struct screen_text *text = data;
84         static char buffer[256];
85         char *value;
87         if (idx >= text->lines->len)
88                 return NULL;
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, text->lines->len, cmd)) {
102                 screen_text_repaint(text);
103                 return true;
104         }
106         text->lw->selected = text->lw->start + text->lw->rows;
107         if (screen_find(text->lw, text->lines->len,
108                         cmd, screen_text_list_callback, text)) {
109                 /* center the row */
110                 list_window_center(text->lw, text->lines->len,
111                                    text->lw->selected);
112                 screen_text_repaint(text);
113                 return true;
114         }
116         return false;