configure.ac: require libmpdclient 2.2
[ncmpc.git] / src / hscroll.c
blob164f178fa3fdff826897e1234f60a88c3bc6f1fd
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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"
22 #include "glib_compat.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)
31 gchar *tmp, *buf;
33 assert(hscroll != NULL);
34 assert(str != NULL);
35 assert(separator != NULL);
37 /* create a buffer containing the string and the separator */
38 tmp = replace_locale_to_utf8(g_strconcat(str, separator,
39 str, separator, NULL));
41 if (hscroll->offset >= (unsigned)g_utf8_strlen(tmp, -1) / 2)
42 hscroll->offset = 0;
44 /* create the new scrolled string */
45 buf = g_utf8_offset_to_pointer(tmp, hscroll->offset);
46 utf8_cut_width(buf, width);
48 /* convert back to locale */
49 buf = utf8_to_locale(buf);
50 g_free(tmp);
51 return buf;
54 /**
55 * This timer scrolls the area by one and redraws.
57 static gboolean
58 hscroll_timer_callback(gpointer data)
60 struct hscroll *hscroll = data;
62 hscroll_step(hscroll);
63 hscroll_draw(hscroll);
64 wrefresh(hscroll->w);
65 return true;
68 void
69 hscroll_set(struct hscroll *hscroll, unsigned x, unsigned y, unsigned width,
70 const char *text)
72 assert(hscroll != NULL);
73 assert(hscroll->w != NULL);
74 assert(text != NULL);
76 if (hscroll->text != NULL && hscroll->x == x && hscroll->y == y &&
77 hscroll->width == width && strcmp(hscroll->text, text) == 0)
78 /* no change, do nothing (and, most importantly, do
79 not reset the current offset!) */
80 return;
82 hscroll_clear(hscroll);
84 hscroll->x = x;
85 hscroll->y = y;
86 hscroll->width = width;
88 /* obtain the ncurses attributes and the current color, store
89 them */
90 wattr_get(hscroll->w, &hscroll->attrs, &hscroll->pair, NULL);
92 hscroll->text = g_strdup(text);
93 hscroll->offset = 0;
94 hscroll->source_id = g_timeout_add_seconds(1, hscroll_timer_callback,
95 hscroll);
98 void
99 hscroll_clear(struct hscroll *hscroll)
101 assert(hscroll != NULL);
103 if (hscroll->text == NULL)
104 return;
106 g_source_remove(hscroll->source_id);
108 g_free(hscroll->text);
109 hscroll->text = NULL;
112 void
113 hscroll_draw(struct hscroll *hscroll)
115 attr_t old_attrs;
116 short old_pair;
117 char *p;
119 assert(hscroll != NULL);
120 assert(hscroll->w != NULL);
121 assert(hscroll->text != NULL);
123 /* set stored attributes and color */
124 wattr_get(hscroll->w, &old_attrs, &old_pair, NULL);
125 wattr_set(hscroll->w, hscroll->attrs, hscroll->pair, NULL);
127 /* scroll the string, and draw it */
128 p = strscroll(hscroll, hscroll->text, hscroll->separator,
129 hscroll->width);
130 mvwaddstr(hscroll->w, hscroll->y, hscroll->x, p);
131 g_free(p);
133 /* restore previous attributes and color */
134 wattr_set(hscroll->w, old_attrs, old_pair, NULL);