Code

release v0.29
[ncmpc.git] / src / hscroll.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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.
9  *
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.
14  *
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"
24 #include <assert.h>
25 #include <string.h>
27 char *
28 strscroll(struct hscroll *hscroll, const char *str, const char *separator,
29           unsigned width)
30 {
31         assert(hscroll != NULL);
32         assert(str != NULL);
33         assert(separator != NULL);
35         /* create a buffer containing the string and the separator */
36         char *tmp = replace_locale_to_utf8(g_strconcat(str, separator,
37                                                        str, separator, NULL));
38         if (hscroll->offset >= (unsigned)g_utf8_strlen(tmp, -1) / 2)
39                 hscroll->offset = 0;
41         /* create the new scrolled string */
42         char *buf = g_utf8_offset_to_pointer(tmp, hscroll->offset);
43         utf8_cut_width(buf, width);
45         /* convert back to locale */
46         buf = utf8_to_locale(buf);
47         g_free(tmp);
48         return buf;
49 }
51 /**
52  * This timer scrolls the area by one and redraws.
53  */
54 static gboolean
55 hscroll_timer_callback(gpointer data)
56 {
57         struct hscroll *hscroll = data;
59         hscroll_step(hscroll);
60         hscroll_draw(hscroll);
61         wrefresh(hscroll->w);
62         return true;
63 }
65 void
66 hscroll_set(struct hscroll *hscroll, unsigned x, unsigned y, unsigned width,
67             const char *text)
68 {
69         assert(hscroll != NULL);
70         assert(hscroll->w != NULL);
71         assert(text != NULL);
73         if (hscroll->text != NULL && hscroll->x == x && hscroll->y == y &&
74             hscroll->width == width && strcmp(hscroll->text, text) == 0)
75                 /* no change, do nothing (and, most importantly, do
76                    not reset the current offset!) */
77                 return;
79         hscroll_clear(hscroll);
81         hscroll->x = x;
82         hscroll->y = y;
83         hscroll->width = width;
85         /* obtain the ncurses attributes and the current color, store
86            them */
87         fix_wattr_get(hscroll->w, &hscroll->attrs, &hscroll->pair, NULL);
89         hscroll->text = g_strdup(text);
90         hscroll->offset = 0;
91         hscroll->source_id = g_timeout_add_seconds(1, hscroll_timer_callback,
92                                                    hscroll);
93 }
95 void
96 hscroll_clear(struct hscroll *hscroll)
97 {
98         assert(hscroll != NULL);
100         if (hscroll->text == NULL)
101                 return;
103         g_source_remove(hscroll->source_id);
105         g_free(hscroll->text);
106         hscroll->text = NULL;
109 void
110 hscroll_draw(struct hscroll *hscroll)
112         assert(hscroll != NULL);
113         assert(hscroll->w != NULL);
114         assert(hscroll->text != NULL);
116         /* set stored attributes and color */
117         attr_t old_attrs;
118         short old_pair;
119         fix_wattr_get(hscroll->w, &old_attrs, &old_pair, NULL);
120         wattr_set(hscroll->w, hscroll->attrs, hscroll->pair, NULL);
122         /* scroll the string, and draw it */
123         char *p = strscroll(hscroll, hscroll->text, hscroll->separator,
124                             hscroll->width);
125         mvwaddstr(hscroll->w, hscroll->y, hscroll->x, p);
126         g_free(p);
128         /* restore previous attributes and color */
129         wattr_set(hscroll->w, old_attrs, old_pair, NULL);