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.
23 #include "glib_compat.h"
29 strscroll(struct hscroll
*hscroll
, const char *str
, const char *separator
,
34 assert(hscroll
!= 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)
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
);
56 * This timer scrolls the area by one and redraws.
59 hscroll_timer_callback(gpointer data
)
61 struct hscroll
*hscroll
= data
;
63 hscroll_step(hscroll
);
64 hscroll_draw(hscroll
);
70 hscroll_set(struct hscroll
*hscroll
, unsigned x
, unsigned y
, unsigned width
,
73 assert(hscroll
!= NULL
);
74 assert(hscroll
->w
!= 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!) */
83 hscroll_clear(hscroll
);
87 hscroll
->width
= width
;
89 /* obtain the ncurses attributes and the current color, store
91 fix_wattr_get(hscroll
->w
, &hscroll
->attrs
, &hscroll
->pair
, NULL
);
93 hscroll
->text
= g_strdup(text
);
95 hscroll
->source_id
= g_timeout_add_seconds(1, hscroll_timer_callback
,
100 hscroll_clear(struct hscroll
*hscroll
)
102 assert(hscroll
!= NULL
);
104 if (hscroll
->text
== NULL
)
107 g_source_remove(hscroll
->source_id
);
109 g_free(hscroll
->text
);
110 hscroll
->text
= NULL
;
114 hscroll_draw(struct hscroll
*hscroll
)
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
,
131 mvwaddstr(hscroll
->w
, hscroll
->y
, hscroll
->x
, p
);
134 /* restore previous attributes and color */
135 wattr_set(hscroll
->w
, old_attrs
, old_pair
, NULL
);