Code

workaround for libncurses macro warnings
[ncmpc.git] / src / hscroll.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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"
22 #include "ncfix.h"
23 #include "glib_compat.h"
25 #include <assert.h>
26 #include <string.h>
28 char *
29 strscroll(struct hscroll *hscroll, const char *str, const char *separator,
30           unsigned width)
31 {
32         gchar *tmp, *buf;
34         assert(hscroll != NULL);
35         assert(str != NULL);
36         assert(separator != NULL);
38         /* create a buffer containing the string and the separator */
39         tmp = replace_locale_to_utf8(g_strconcat(str, separator,
40                                                  str, separator, NULL));
42         if (hscroll->offset >= (unsigned)g_utf8_strlen(tmp, -1) / 2)
43                 hscroll->offset = 0;
45         /* create the new scrolled string */
46         buf = g_utf8_offset_to_pointer(tmp, hscroll->offset);
47         utf8_cut_width(buf, width);
49         /* convert back to locale */
50         buf = utf8_to_locale(buf);
51         g_free(tmp);
52         return buf;
53 }
55 /**
56  * This timer scrolls the area by one and redraws.
57  */
58 static gboolean
59 hscroll_timer_callback(gpointer data)
60 {
61         struct hscroll *hscroll = data;
63         hscroll_step(hscroll);
64         hscroll_draw(hscroll);
65         wrefresh(hscroll->w);
66         return true;
67 }
69 void
70 hscroll_set(struct hscroll *hscroll, unsigned x, unsigned y, unsigned width,
71             const char *text)
72 {
73         assert(hscroll != NULL);
74         assert(hscroll->w != NULL);
75         assert(text != NULL);
77         if (hscroll->text != NULL && hscroll->x == x && hscroll->y == y &&
78             hscroll->width == width && strcmp(hscroll->text, text) == 0)
79                 /* no change, do nothing (and, most importantly, do
80                    not reset the current offset!) */
81                 return;
83         hscroll_clear(hscroll);
85         hscroll->x = x;
86         hscroll->y = y;
87         hscroll->width = width;
89         /* obtain the ncurses attributes and the current color, store
90            them */
91         fix_wattr_get(hscroll->w, &hscroll->attrs, &hscroll->pair, NULL);
93         hscroll->text = g_strdup(text);
94         hscroll->offset = 0;
95         hscroll->source_id = g_timeout_add_seconds(1, hscroll_timer_callback,
96                                                    hscroll);
97 }
99 void
100 hscroll_clear(struct hscroll *hscroll)
102         assert(hscroll != NULL);
104         if (hscroll->text == NULL)
105                 return;
107         g_source_remove(hscroll->source_id);
109         g_free(hscroll->text);
110         hscroll->text = NULL;
113 void
114 hscroll_draw(struct hscroll *hscroll)
116         attr_t old_attrs;
117         short old_pair;
118         char *p;
120         assert(hscroll != NULL);
121         assert(hscroll->w != NULL);
122         assert(hscroll->text != NULL);
124         /* set stored attributes and color */
125         fix_wattr_get(hscroll->w, &old_attrs, &old_pair, NULL);
126         wattr_set(hscroll->w, hscroll->attrs, hscroll->pair, NULL);
128         /* scroll the string, and draw it */
129         p = strscroll(hscroll, hscroll->text, hscroll->separator,
130                       hscroll->width);
131         mvwaddstr(hscroll->w, hscroll->y, hscroll->x, p);
132         g_free(p);
134         /* restore previous attributes and color */
135         wattr_set(hscroll->w, old_attrs, old_pair, NULL);