Code

2915a112ca93299984d2c57429abb5b714374d35
[ncmpc.git] / src / hscroll.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "hscroll.h"
20 #include "charset.h"
21 #include "config.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <string.h>
27 #ifndef NCMPC_MINI
29 // FIXME: utf-8 length
30 char *
31 strscroll(char *str, char *separator, int width, scroll_state_t *st)
32 {
33         gchar *tmp, *buf;
34         gsize len, size;
36         assert(str != NULL);
37         assert(separator != NULL);
38         assert(st != NULL);
40         if( st->offset==0 ) {
41                 st->offset++;
42                 return g_strdup(str);
43         }
45         /* create a buffer containing the string and the separator */
46         size = strlen(str)+strlen(separator)+1;
47         tmp = g_malloc(size);
48         g_strlcpy(tmp, str, size);
49         g_strlcat(tmp, separator, size);
50         len = utf8_width(tmp);
52         if (st->offset >= len)
53                 st->offset = 0;
55         /* create the new scrolled string */
56         size = width+1;
57         if (g_utf8_validate(tmp, -1, NULL) ) {
58                 int ulen;
59                 buf = g_malloc(size*6);// max length of utf8 char is 6
60                 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp,st->offset), size);
61                 if( (ulen = g_utf8_strlen(buf, -1)) < width )
62                         g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
63         } else {
64                 buf = g_malloc(size);
65                 g_strlcpy(buf, tmp+st->offset, size);
66                 if (strlen(buf) < (size_t)width)
67                         g_strlcat(buf, tmp, size);
68         }
69         if( time(NULL)-st->t >= 1 ) {
70                 st->t = time(NULL);
71                 st->offset++;
72         }
73         g_free(tmp);
74         return buf;
75 }
77 #endif