hscroll: reimplemented the hscroll library
[ncmpc.git] / src / hscroll.c
blob32988d0fdf6b1b895415079a7610e56577413114
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
20 #include "hscroll.h"
21 #include "charset.h"
23 #include <assert.h>
24 #include <string.h>
26 char *
27 strscroll(struct hscroll *hscroll, const char *str, const char *separator,
28 unsigned width)
30 gchar *tmp, *buf;
32 assert(hscroll != NULL);
33 assert(str != NULL);
34 assert(separator != NULL);
36 /* create a buffer containing the string and the separator */
37 tmp = replace_locale_to_utf8(g_strconcat(str, separator,
38 str, separator, NULL));
40 if (hscroll->offset >= (unsigned)g_utf8_strlen(tmp, -1) / 2)
41 hscroll->offset = 0;
43 /* create the new scrolled string */
44 buf = g_utf8_offset_to_pointer(tmp, hscroll->offset);
45 utf8_cut_width(buf, width);
47 /* convert back to locale */
48 buf = utf8_to_locale(buf);
49 g_free(tmp);
50 return buf;
53 /**
54 * This timer scrolls the area by one and redraws.
56 static gboolean
57 hscroll_timer_callback(gpointer data)
59 struct hscroll *hscroll = data;
61 hscroll_step(hscroll);
62 hscroll_draw(hscroll);
63 wrefresh(hscroll->w);
64 return true;
67 void
68 hscroll_set(struct hscroll *hscroll, unsigned x, unsigned y, unsigned width,
69 const char *text)
71 assert(hscroll != NULL);
72 assert(hscroll->w != NULL);
73 assert(text != NULL);
75 if (hscroll->text != NULL && hscroll->x == x && hscroll->y == y &&
76 hscroll->width == width && strcmp(hscroll->text, text) == 0)
77 /* no change, do nothing (and, most importantly, do
78 not reset the current offset!) */
79 return;
81 hscroll_clear(hscroll);
83 hscroll->x = x;
84 hscroll->y = y;
85 hscroll->width = width;
87 /* obtain the ncurses attributes and the current color, store
88 them */
89 wattr_get(hscroll->w, &hscroll->attrs, &hscroll->pair, NULL);
91 hscroll->text = g_strdup(text);
92 hscroll->offset = 0;
93 hscroll->source_id = g_timeout_add_seconds(1, hscroll_timer_callback,
94 hscroll);
97 void
98 hscroll_clear(struct hscroll *hscroll)
100 assert(hscroll != NULL);
102 if (hscroll->text == NULL)
103 return;
105 g_source_remove(hscroll->source_id);
107 g_free(hscroll->text);
108 hscroll->text = NULL;
111 void
112 hscroll_draw(struct hscroll *hscroll)
114 attr_t old_attrs;
115 short old_pair;
116 char *p;
118 assert(hscroll != NULL);
119 assert(hscroll->w != NULL);
120 assert(hscroll->text != NULL);
122 /* set stored attributes and color */
123 wattr_get(hscroll->w, &old_attrs, &old_pair, NULL);
124 wattr_set(hscroll->w, hscroll->attrs, hscroll->pair, NULL);
126 /* scroll the string, and draw it */
127 p = strscroll(hscroll, hscroll->text, hscroll->separator,
128 hscroll->width);
129 mvwaddstr(hscroll->w, hscroll->y, hscroll->x, p);
130 g_free(p);
132 /* restore previous attributes and color */
133 wattr_set(hscroll->w, old_attrs, old_pair, NULL);