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