1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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.
18 */
20 #include "hscroll.h"
21 #include "charset.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <string.h>
27 // FIXME: utf-8 length
28 char *
29 strscroll(char *str, char *separator, int width, scroll_state_t *st)
30 {
31 gchar *tmp, *buf;
32 gsize len, size;
34 assert(str != NULL);
35 assert(separator != NULL);
36 assert(st != NULL);
38 if( st->offset==0 ) {
39 st->offset++;
40 return g_strdup(str);
41 }
43 /* create a buffer containing the string and the separator */
44 size = strlen(str)+strlen(separator)+1;
45 tmp = g_malloc(size);
46 g_strlcpy(tmp, str, size);
47 g_strlcat(tmp, separator, size);
48 len = utf8_width(tmp);
50 if (st->offset >= len)
51 st->offset = 0;
53 /* create the new scrolled string */
54 size = width+1;
55 if (g_utf8_validate(tmp, -1, NULL) ) {
56 int ulen;
57 buf = g_malloc(size*6);// max length of utf8 char is 6
58 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
59 if( (ulen = g_utf8_strlen(buf, -1)) < width )
60 g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
61 } else {
62 buf = g_malloc(size);
63 g_strlcpy(buf, tmp+st->offset, size);
64 if (strlen(buf) < (size_t)width)
65 g_strlcat(buf, tmp, size);
66 }
67 if( time(NULL)-st->t >= 1 ) {
68 st->t = time(NULL);
69 st->offset++;
70 }
71 g_free(tmp);
72 return buf;
73 }