Code

screen_play, status_bar: update scrolling with a GLib timer
[ncmpc.git] / src / hscroll.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 "hscroll.h"
21 #include "charset.h"
23 #include <assert.h>
24 #include <ctype.h>
25 #include <string.h>
27 char *
28 strscroll(struct hscroll *hscroll, char *str, char *separator, unsigned width)
29 {
30         gchar *tmp, *buf;
31         gsize len, size;
33         assert(hscroll != NULL);
34         assert(str != NULL);
35         assert(separator != NULL);
37         if (hscroll->offset == 0)
38                 return g_strdup(str);
40         /* create a buffer containing the string and the separator */
41         size = strlen(str)+strlen(separator)+1;
42         tmp = g_malloc(size);
43         g_strlcpy(tmp, str, size);
44         g_strlcat(tmp, separator, size);
45         len = utf8_width(tmp);
47         if (hscroll->offset >= len)
48                 hscroll->offset = 0;
50         /* create the new scrolled string */
51         size = width+1;
52         if (g_utf8_validate(tmp, -1, NULL) ) {
53                 size_t ulen;
54                 buf = g_malloc(size*6);// max length of utf8 char is 6
55                 g_utf8_strncpy(buf, g_utf8_offset_to_pointer(tmp, hscroll->offset), size);
56                 if( (ulen = g_utf8_strlen(buf, -1)) < width )
57                         g_utf8_strncpy(buf+strlen(buf), tmp, size - ulen - 1);
58         } else {
59                 buf = g_malloc(size);
60                 g_strlcpy(buf, tmp + hscroll->offset, size);
61                 if (strlen(buf) < (size_t)width)
62                         g_strlcat(buf, tmp, size);
63         }
65         g_free(tmp);
66         return buf;
67 }