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.
22 #include "glib_compat.h"
28 strscroll(struct hscroll
*hscroll
, const char *str
, const char *separator
,
33 assert(hscroll
!= 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)
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
);
55 * This timer scrolls the area by one and redraws.
58 hscroll_timer_callback(gpointer data
)
60 struct hscroll
*hscroll
= data
;
62 hscroll_step(hscroll
);
63 hscroll_draw(hscroll
);
69 hscroll_set(struct hscroll
*hscroll
, unsigned x
, unsigned y
, unsigned width
,
72 assert(hscroll
!= NULL
);
73 assert(hscroll
->w
!= 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!) */
82 hscroll_clear(hscroll
);
86 hscroll
->width
= width
;
88 /* obtain the ncurses attributes and the current color, store
90 wattr_get(hscroll
->w
, &hscroll
->attrs
, &hscroll
->pair
, NULL
);
92 hscroll
->text
= g_strdup(text
);
94 hscroll
->source_id
= g_timeout_add_seconds(1, hscroll_timer_callback
,
99 hscroll_clear(struct hscroll
*hscroll
)
101 assert(hscroll
!= NULL
);
103 if (hscroll
->text
== NULL
)
106 g_source_remove(hscroll
->source_id
);
108 g_free(hscroll
->text
);
109 hscroll
->text
= NULL
;
113 hscroll_draw(struct hscroll
*hscroll
)
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
,
130 mvwaddstr(hscroll
->w
, hscroll
->y
, hscroll
->x
, p
);
133 /* restore previous attributes and color */
134 wattr_set(hscroll
->w
, old_attrs
, old_pair
, NULL
);