arrows.c revision 224014
1217309Snwhitehorn/*
2224014Snwhitehorn *  $Id: arrows.c,v 1.36 2011/06/27 09:13:56 tom Exp $
3217309Snwhitehorn *
4217309Snwhitehorn *  arrows.c -- draw arrows to indicate end-of-range for lists
5217309Snwhitehorn *
6220749Snwhitehorn *  Copyright 2000-2010,2011	Thomas E. Dickey
7217309Snwhitehorn *
8217309Snwhitehorn *  This program is free software; you can redistribute it and/or modify
9217309Snwhitehorn *  it under the terms of the GNU Lesser General Public License, version 2.1
10217309Snwhitehorn *  as published by the Free Software Foundation.
11217309Snwhitehorn *
12217309Snwhitehorn *  This program is distributed in the hope that it will be useful, but
13217309Snwhitehorn *  WITHOUT ANY WARRANTY; without even the implied warranty of
14217309Snwhitehorn *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15217309Snwhitehorn *  Lesser General Public License for more details.
16217309Snwhitehorn *
17217309Snwhitehorn *  You should have received a copy of the GNU Lesser General Public
18217309Snwhitehorn *  License along with this program; if not, write to
19217309Snwhitehorn *	Free Software Foundation, Inc.
20217309Snwhitehorn *	51 Franklin St., Fifth Floor
21217309Snwhitehorn *	Boston, MA 02110, USA.
22217309Snwhitehorn */
23217309Snwhitehorn
24217309Snwhitehorn#include <dialog.h>
25217309Snwhitehorn
26217309Snwhitehorn#ifdef USE_WIDE_CURSES
27220749Snwhitehorn#if defined(CURSES_WACS_ARRAY) && !defined(CURSES_WACS_SYMBOLS)
28220749Snwhitehorn/* workaround for NetBSD 5.1 curses */
29220749Snwhitehorn#undef WACS_DARROW
30220749Snwhitehorn#undef WACS_UARROW
31220749Snwhitehorn#define WACS_DARROW &(CURSES_WACS_ARRAY['.'])
32220749Snwhitehorn#define WACS_UARROW &(CURSES_WACS_ARRAY['-'])
33220749Snwhitehorn#endif
34217309Snwhitehorn#define add_acs(win, code) wadd_wch(win, W ## code)
35217309Snwhitehorn#else
36217309Snwhitehorn#define add_acs(win, code) waddch(win, dlg_boxchar(code))
37217309Snwhitehorn#endif
38217309Snwhitehorn
39224014Snwhitehorn/* size of decorations */
40224014Snwhitehorn#define ON_LEFT 4
41224014Snwhitehorn#define ON_RIGHT 3
42224014Snwhitehorn
43217309Snwhitehorn#ifdef HAVE_COLOR
44217309Snwhitehornstatic chtype
45217309Snwhitehornmerge_colors(chtype foreground, chtype background)
46217309Snwhitehorn{
47217309Snwhitehorn    chtype result = foreground;
48217309Snwhitehorn    if ((foreground & A_COLOR) != (background & A_COLOR)) {
49217309Snwhitehorn	short fg_f, bg_f;
50217309Snwhitehorn	short fg_b, bg_b;
51217309Snwhitehorn	short fg_pair = (short) PAIR_NUMBER(foreground);
52217309Snwhitehorn	short bg_pair = (short) PAIR_NUMBER(background);
53217309Snwhitehorn
54217309Snwhitehorn	if (pair_content(fg_pair, &fg_f, &bg_f) != ERR
55217309Snwhitehorn	    && pair_content(bg_pair, &fg_b, &bg_b) != ERR) {
56217309Snwhitehorn	    result &= ~A_COLOR;
57217309Snwhitehorn	    result |= dlg_color_pair(fg_f, bg_b);
58217309Snwhitehorn	}
59217309Snwhitehorn    }
60217309Snwhitehorn    return result;
61217309Snwhitehorn}
62217309Snwhitehorn#else
63217309Snwhitehorn#define merge_colors(f,b) (f)
64217309Snwhitehorn#endif
65217309Snwhitehorn
66224014Snwhitehorn/*
67224014Snwhitehorn * If we have help-line text, e.g., from "--hline", draw it between the other
68224014Snwhitehorn * decorations at the bottom of the dialog window.
69224014Snwhitehorn */
70217309Snwhitehornvoid
71224014Snwhitehorndlg_draw_helpline(WINDOW *win, bool decorations)
72224014Snwhitehorn{
73224014Snwhitehorn    int cur_x, cur_y;
74224014Snwhitehorn    int bottom;
75224014Snwhitehorn
76224014Snwhitehorn    if (dialog_vars.help_line != 0
77224014Snwhitehorn	&& (bottom = getmaxy(win) - 1) > 0) {
78224014Snwhitehorn	chtype attr = A_NORMAL;
79224014Snwhitehorn	const int *cols = dlg_index_columns(dialog_vars.help_line);
80224014Snwhitehorn	int other = decorations ? (ON_LEFT + ON_RIGHT) : 0;
81224014Snwhitehorn	int avail = (getmaxx(win) - other - 2);
82224014Snwhitehorn	int limit = dlg_limit_columns(dialog_vars.help_line, avail, 0);
83224014Snwhitehorn
84224014Snwhitehorn	if (limit > 0) {
85224014Snwhitehorn	    getyx(win, cur_y, cur_x);
86224014Snwhitehorn	    other = decorations ? ON_LEFT : 0;
87224014Snwhitehorn	    (void) wmove(win, bottom, other + (avail - limit) / 2);
88224014Snwhitehorn	    waddch(win, '[');
89224014Snwhitehorn	    dlg_print_text(win, dialog_vars.help_line, cols[limit], &attr);
90224014Snwhitehorn	    waddch(win, ']');
91224014Snwhitehorn	    wmove(win, cur_y, cur_x);
92224014Snwhitehorn	}
93224014Snwhitehorn    }
94224014Snwhitehorn}
95224014Snwhitehorn
96224014Snwhitehornvoid
97217309Snwhitehorndlg_draw_arrows2(WINDOW *win,
98217309Snwhitehorn		 int top_arrow,
99217309Snwhitehorn		 int bottom_arrow,
100217309Snwhitehorn		 int x,
101217309Snwhitehorn		 int top,
102217309Snwhitehorn		 int bottom,
103217309Snwhitehorn		 chtype attr,
104217309Snwhitehorn		 chtype borderattr)
105217309Snwhitehorn{
106220749Snwhitehorn    chtype save = dlg_get_attrs(win);
107217309Snwhitehorn    int cur_x, cur_y;
108217309Snwhitehorn    int limit_x = getmaxx(win);
109217309Snwhitehorn    bool draw_top = TRUE;
110217309Snwhitehorn
111217309Snwhitehorn    getyx(win, cur_y, cur_x);
112217309Snwhitehorn
113217309Snwhitehorn    /*
114217309Snwhitehorn     * If we're drawing a centered title, do not overwrite with the arrows.
115217309Snwhitehorn     */
116217309Snwhitehorn    if (dialog_vars.title) {
117217309Snwhitehorn	int have = (limit_x - dlg_count_columns(dialog_vars.title)) / 2;
118217309Snwhitehorn	int need = x + 5;
119217309Snwhitehorn	if (need > have)
120217309Snwhitehorn	    draw_top = FALSE;
121217309Snwhitehorn    }
122217309Snwhitehorn
123217309Snwhitehorn    if (draw_top) {
124217309Snwhitehorn	(void) wmove(win, top, x);
125217309Snwhitehorn	if (top_arrow) {
126217309Snwhitehorn	    wattrset(win, merge_colors(uarrow_attr, attr));
127217309Snwhitehorn	    (void) add_acs(win, ACS_UARROW);
128217309Snwhitehorn	    (void) waddstr(win, "(-)");
129217309Snwhitehorn	} else {
130217309Snwhitehorn	    wattrset(win, attr);
131224014Snwhitehorn	    (void) whline(win, dlg_boxchar(ACS_HLINE), ON_LEFT);
132217309Snwhitehorn	}
133217309Snwhitehorn    }
134217309Snwhitehorn    mouse_mkbutton(top, x - 1, 6, KEY_PPAGE);
135217309Snwhitehorn
136217309Snwhitehorn    (void) wmove(win, bottom, x);
137217309Snwhitehorn    if (bottom_arrow) {
138217309Snwhitehorn	wattrset(win, merge_colors(darrow_attr, attr));
139217309Snwhitehorn	(void) add_acs(win, ACS_DARROW);
140217309Snwhitehorn	(void) waddstr(win, "(+)");
141217309Snwhitehorn    } else {
142217309Snwhitehorn	wattrset(win, borderattr);
143224014Snwhitehorn	(void) whline(win, dlg_boxchar(ACS_HLINE), ON_LEFT);
144217309Snwhitehorn    }
145217309Snwhitehorn    mouse_mkbutton(bottom, x - 1, 6, KEY_NPAGE);
146217309Snwhitehorn
147217309Snwhitehorn    (void) wmove(win, cur_y, cur_x);
148217309Snwhitehorn    wrefresh(win);
149217309Snwhitehorn
150217309Snwhitehorn    wattrset(win, save);
151217309Snwhitehorn}
152217309Snwhitehorn
153217309Snwhitehornvoid
154217309Snwhitehorndlg_draw_scrollbar(WINDOW *win,
155217309Snwhitehorn		   long first_data,
156217309Snwhitehorn		   long this_data,
157217309Snwhitehorn		   long next_data,
158217309Snwhitehorn		   long total_data,
159217309Snwhitehorn		   int left,
160217309Snwhitehorn		   int right,
161217309Snwhitehorn		   int top,
162217309Snwhitehorn		   int bottom,
163217309Snwhitehorn		   chtype attr,
164217309Snwhitehorn		   chtype borderattr)
165217309Snwhitehorn{
166217309Snwhitehorn    char buffer[80];
167217309Snwhitehorn    int percent;
168217309Snwhitehorn    int len;
169217309Snwhitehorn    int oldy, oldx, maxy, maxx;
170217309Snwhitehorn
171220749Snwhitehorn    chtype save = dlg_get_attrs(win);
172217309Snwhitehorn    int top_arrow = (first_data != 0);
173217309Snwhitehorn    int bottom_arrow = (next_data < total_data);
174217309Snwhitehorn
175217309Snwhitehorn    getyx(win, oldy, oldx);
176217309Snwhitehorn    getmaxyx(win, maxy, maxx);
177217309Snwhitehorn
178224014Snwhitehorn    dlg_draw_helpline(win, TRUE);
179217309Snwhitehorn    if (bottom_arrow || top_arrow || dialog_state.use_scrollbar) {
180217309Snwhitehorn	percent = (!total_data
181217309Snwhitehorn		   ? 100
182217309Snwhitehorn		   : (int) ((next_data * 100)
183217309Snwhitehorn			    / total_data));
184217309Snwhitehorn
185217309Snwhitehorn	if (percent < 0)
186217309Snwhitehorn	    percent = 0;
187217309Snwhitehorn	else if (percent > 100)
188217309Snwhitehorn	    percent = 100;
189217309Snwhitehorn
190217309Snwhitehorn	wattrset(win, position_indicator_attr);
191217309Snwhitehorn	(void) sprintf(buffer, "%d%%", percent);
192217309Snwhitehorn	(void) wmove(win, bottom, right - 7);
193217309Snwhitehorn	(void) waddstr(win, buffer);
194217309Snwhitehorn	if ((len = dlg_count_columns(buffer)) < 4) {
195217309Snwhitehorn	    wattrset(win, border_attr);
196217309Snwhitehorn	    whline(win, dlg_boxchar(ACS_HLINE), 4 - len);
197217309Snwhitehorn	}
198217309Snwhitehorn    }
199220749Snwhitehorn#define BARSIZE(num) (int) (((all_high * (num)) + all_high - 1) / total_data)
200217309Snwhitehorn
201217309Snwhitehorn    if (dialog_state.use_scrollbar) {
202217309Snwhitehorn	int all_high = (bottom - top - 1);
203217309Snwhitehorn
204217309Snwhitehorn	if (total_data > 0 && all_high > 0) {
205217309Snwhitehorn	    int bar_high;
206217309Snwhitehorn	    int bar_y;
207217309Snwhitehorn
208217309Snwhitehorn	    bar_high = BARSIZE(next_data - this_data);
209217309Snwhitehorn	    if (bar_high <= 0)
210217309Snwhitehorn		bar_high = 1;
211217309Snwhitehorn
212217309Snwhitehorn	    if (bar_high < all_high) {
213217309Snwhitehorn		wmove(win, top + 1, right);
214217309Snwhitehorn
215217309Snwhitehorn		wattrset(win, save);
216217309Snwhitehorn		wvline(win, ACS_VLINE | A_REVERSE, all_high);
217217309Snwhitehorn
218217309Snwhitehorn		bar_y = BARSIZE(this_data);
219217309Snwhitehorn		if (bar_y > all_high - bar_high)
220217309Snwhitehorn		    bar_y = all_high - bar_high;
221217309Snwhitehorn
222217309Snwhitehorn		wmove(win, top + 1 + bar_y, right);
223217309Snwhitehorn
224217309Snwhitehorn		wattrset(win, position_indicator_attr);
225217309Snwhitehorn		wattron(win, A_REVERSE);
226217309Snwhitehorn		wvline(win, ACS_BLOCK, bar_high);
227217309Snwhitehorn	    }
228217309Snwhitehorn	}
229217309Snwhitehorn    }
230217309Snwhitehorn    dlg_draw_arrows2(win,
231217309Snwhitehorn		     top_arrow,
232217309Snwhitehorn		     bottom_arrow,
233217309Snwhitehorn		     left + ARROWS_COL,
234217309Snwhitehorn		     top,
235217309Snwhitehorn		     bottom,
236217309Snwhitehorn		     attr,
237217309Snwhitehorn		     borderattr);
238217309Snwhitehorn
239217309Snwhitehorn    wattrset(win, save);
240217309Snwhitehorn    wmove(win, oldy, oldx);
241217309Snwhitehorn}
242217309Snwhitehorn
243217309Snwhitehornvoid
244217309Snwhitehorndlg_draw_arrows(WINDOW *win,
245217309Snwhitehorn		int top_arrow,
246217309Snwhitehorn		int bottom_arrow,
247217309Snwhitehorn		int x,
248217309Snwhitehorn		int top,
249217309Snwhitehorn		int bottom)
250217309Snwhitehorn{
251224014Snwhitehorn    dlg_draw_helpline(win, TRUE);
252217309Snwhitehorn    dlg_draw_arrows2(win,
253217309Snwhitehorn		     top_arrow,
254217309Snwhitehorn		     bottom_arrow,
255217309Snwhitehorn		     x,
256217309Snwhitehorn		     top,
257217309Snwhitehorn		     bottom,
258217309Snwhitehorn		     menubox_attr,
259217309Snwhitehorn		     menubox_border_attr);
260217309Snwhitehorn}
261