xref: /titanic_41/usr/src/lib/libeti/menu/common/post.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Mircrosystems, Inc.
28*7c478bd9Sstevel@tonic-gate  * All rights reserved.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.12	*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
36*7c478bd9Sstevel@tonic-gate #include "private.h"
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate void
_post_item(MENU * m,ITEM * k)39*7c478bd9Sstevel@tonic-gate _post_item(MENU *m, ITEM *k)
40*7c478bd9Sstevel@tonic-gate {
41*7c478bd9Sstevel@tonic-gate 	int foreon = FALSE;
42*7c478bd9Sstevel@tonic-gate 	int backon = FALSE;
43*7c478bd9Sstevel@tonic-gate 	int greyon = FALSE;
44*7c478bd9Sstevel@tonic-gate 	chtype c;
45*7c478bd9Sstevel@tonic-gate 	int i;
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 	/* Display the mark region of the item */
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate 	if (!Selectable(k)) {
50*7c478bd9Sstevel@tonic-gate 		(void) wattron(Win(m), Grey(m));
51*7c478bd9Sstevel@tonic-gate 		greyon = TRUE;
52*7c478bd9Sstevel@tonic-gate 		for (i = Marklen(m); i > 0; i--) {
53*7c478bd9Sstevel@tonic-gate 			(void) waddch(Win(m), ' ');
54*7c478bd9Sstevel@tonic-gate 		}
55*7c478bd9Sstevel@tonic-gate 	} else {
56*7c478bd9Sstevel@tonic-gate 		if (Value(k) || k == Current(m)) {
57*7c478bd9Sstevel@tonic-gate 			(void) wattron(Win(m), Fore(m));
58*7c478bd9Sstevel@tonic-gate 			foreon = TRUE;
59*7c478bd9Sstevel@tonic-gate 		} else {
60*7c478bd9Sstevel@tonic-gate 			(void) wattron(Win(m), Back(m));
61*7c478bd9Sstevel@tonic-gate 			backon = TRUE;
62*7c478bd9Sstevel@tonic-gate 		}
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate 		/* Display the mark */
65*7c478bd9Sstevel@tonic-gate 		if (Value(k) || (OneValue(m) && k == Current(m))) {
66*7c478bd9Sstevel@tonic-gate 			if (Marklen(m)) {
67*7c478bd9Sstevel@tonic-gate 				(void) waddstr(Win(m), Mark(m));
68*7c478bd9Sstevel@tonic-gate 			}
69*7c478bd9Sstevel@tonic-gate 		} else {
70*7c478bd9Sstevel@tonic-gate 			for (i = Marklen(m); i > 0; i--) {
71*7c478bd9Sstevel@tonic-gate 				(void) waddch(Win(m), ' ');
72*7c478bd9Sstevel@tonic-gate 			}
73*7c478bd9Sstevel@tonic-gate 		}
74*7c478bd9Sstevel@tonic-gate 	}
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	/* Display the name */
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	(void) waddnstr(Win(m), Name(k), MaxName(m));
79*7c478bd9Sstevel@tonic-gate 	if (ShowDesc(m) && MaxDesc(m) != 0) {
80*7c478bd9Sstevel@tonic-gate 		c = Pad(m);
81*7c478bd9Sstevel@tonic-gate 	} else {
82*7c478bd9Sstevel@tonic-gate 		c = ' ';
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	for (i = MaxName(m) - NameLen(k); i > 0; i--) {
85*7c478bd9Sstevel@tonic-gate 		(void) waddch(Win(m), c);
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	/* Display the description */
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	if (ShowDesc(m) && MaxDesc(m) != 0) {
91*7c478bd9Sstevel@tonic-gate 		(void) waddch(Win(m), Pad(m));
92*7c478bd9Sstevel@tonic-gate 		if (DescriptionLen(k) != 0) {
93*7c478bd9Sstevel@tonic-gate 			(void) waddstr(Win(m), Description(k));
94*7c478bd9Sstevel@tonic-gate 		}
95*7c478bd9Sstevel@tonic-gate 		for (i = MaxDesc(m) - DescriptionLen(k); i > 0; i--) {
96*7c478bd9Sstevel@tonic-gate 			(void) waddch(Win(m), ' ');
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	if (foreon) {
100*7c478bd9Sstevel@tonic-gate 		(void) wattroff(Win(m), Fore(m));
101*7c478bd9Sstevel@tonic-gate 	}
102*7c478bd9Sstevel@tonic-gate 	if (backon) {
103*7c478bd9Sstevel@tonic-gate 		(void) wattroff(Win(m), Back(m));
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 	if (greyon) {
106*7c478bd9Sstevel@tonic-gate 		(void) wattroff(Win(m), Grey(m));
107*7c478bd9Sstevel@tonic-gate 	}
108*7c478bd9Sstevel@tonic-gate }
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate void
_move_post_item(MENU * m,ITEM * k)111*7c478bd9Sstevel@tonic-gate _move_post_item(MENU *m, ITEM *k)
112*7c478bd9Sstevel@tonic-gate {
113*7c478bd9Sstevel@tonic-gate 	(void) wmove(Win(m), Y(k), X(k) * (Itemlen(m)+1));
114*7c478bd9Sstevel@tonic-gate 	_post_item(m, k);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate int
unpost_menu(MENU * m)118*7c478bd9Sstevel@tonic-gate unpost_menu(MENU *m)
119*7c478bd9Sstevel@tonic-gate {
120*7c478bd9Sstevel@tonic-gate 	if (!m) {
121*7c478bd9Sstevel@tonic-gate 		return (E_BAD_ARGUMENT);
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 	if (Indriver(m)) {
124*7c478bd9Sstevel@tonic-gate 		return (E_BAD_STATE);
125*7c478bd9Sstevel@tonic-gate 	}
126*7c478bd9Sstevel@tonic-gate 	if (!Posted(m)) {
127*7c478bd9Sstevel@tonic-gate 		return (E_NOT_POSTED);
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 	Iterm(m);
130*7c478bd9Sstevel@tonic-gate 	Mterm(m);
131*7c478bd9Sstevel@tonic-gate 	(void) werase(US(m));
132*7c478bd9Sstevel@tonic-gate 	wsyncup(US(m));
133*7c478bd9Sstevel@tonic-gate 	(void) delwin(Sub(m));
134*7c478bd9Sstevel@tonic-gate 	Sub(m) = (WINDOW *) NULL;
135*7c478bd9Sstevel@tonic-gate 	(void) delwin(Win(m));
136*7c478bd9Sstevel@tonic-gate 	Win(m) = (WINDOW *) NULL;
137*7c478bd9Sstevel@tonic-gate 	ResetPost(m);
138*7c478bd9Sstevel@tonic-gate 	return (E_OK);
139*7c478bd9Sstevel@tonic-gate }
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate /*
142*7c478bd9Sstevel@tonic-gate  * This routine draws the item indicated by oldcur first and then
143*7c478bd9Sstevel@tonic-gate  * draws the item indicated by Current.  This will have the affect
144*7c478bd9Sstevel@tonic-gate  * of unselecting the first item and selecting the next.
145*7c478bd9Sstevel@tonic-gate  */
146*7c478bd9Sstevel@tonic-gate void
_movecurrent(MENU * m,ITEM * oldcur)147*7c478bd9Sstevel@tonic-gate _movecurrent(MENU *m, ITEM *oldcur)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	if (oldcur != Current(m)) {
150*7c478bd9Sstevel@tonic-gate 		_move_post_item(m, oldcur);
151*7c478bd9Sstevel@tonic-gate 		_move_post_item(m, Current(m));
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate /*
156*7c478bd9Sstevel@tonic-gate  * Draw the entire menu into the super window
157*7c478bd9Sstevel@tonic-gate  * This routine assumes all items have been linked and
158*7c478bd9Sstevel@tonic-gate  * that the menu is in at least a pre-post state.
159*7c478bd9Sstevel@tonic-gate  */
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate void
_draw(MENU * m)162*7c478bd9Sstevel@tonic-gate _draw(MENU *m)
163*7c478bd9Sstevel@tonic-gate {
164*7c478bd9Sstevel@tonic-gate 	int k;
165*7c478bd9Sstevel@tonic-gate 	ITEM *i, *j;
166*7c478bd9Sstevel@tonic-gate 	ITEM *si, *sj;
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate 	k = 0;		/* Line number */
169*7c478bd9Sstevel@tonic-gate 	i = IthItem(m, 0);
170*7c478bd9Sstevel@tonic-gate 	si = Cyclic(m) ? i : (ITEM *) NULL;
171*7c478bd9Sstevel@tonic-gate 	do {
172*7c478bd9Sstevel@tonic-gate 		(void) wmove(Win(m), k++, 0);
173*7c478bd9Sstevel@tonic-gate 		j = i;
174*7c478bd9Sstevel@tonic-gate 		sj = Cyclic(m) ? j : (ITEM *) NULL;
175*7c478bd9Sstevel@tonic-gate 		do {
176*7c478bd9Sstevel@tonic-gate 			_post_item(m, j);
177*7c478bd9Sstevel@tonic-gate 			if ((j = Right(j)) != sj) {
178*7c478bd9Sstevel@tonic-gate 				(void) waddch(Win(m), ' ');
179*7c478bd9Sstevel@tonic-gate 			}
180*7c478bd9Sstevel@tonic-gate 		} while (j != sj);
181*7c478bd9Sstevel@tonic-gate 	} while ((i = Down(i)) != si);
182*7c478bd9Sstevel@tonic-gate }
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate int
post_menu(MENU * m)185*7c478bd9Sstevel@tonic-gate post_menu(MENU *m)
186*7c478bd9Sstevel@tonic-gate {
187*7c478bd9Sstevel@tonic-gate 	ITEM **ip;
188*7c478bd9Sstevel@tonic-gate 	int r, c;		/* visible # of rows and cols */
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 	if (!m) {
191*7c478bd9Sstevel@tonic-gate 		return (E_BAD_ARGUMENT);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	if (Indriver(m)) {
194*7c478bd9Sstevel@tonic-gate 		return (E_BAD_STATE);
195*7c478bd9Sstevel@tonic-gate 	}
196*7c478bd9Sstevel@tonic-gate 	if (Posted(m)) {
197*7c478bd9Sstevel@tonic-gate 		return (E_POSTED);
198*7c478bd9Sstevel@tonic-gate 	}
199*7c478bd9Sstevel@tonic-gate 	/* Make sure there is at least one item present */
200*7c478bd9Sstevel@tonic-gate 	if (Items(m) && IthItem(m, 0)) {
201*7c478bd9Sstevel@tonic-gate 		getmaxyx(US(m), r, c);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		/* Make sure the menu fits into the window horizontally */
204*7c478bd9Sstevel@tonic-gate 		if (c < Width(m) || r < Height(m)) {
205*7c478bd9Sstevel@tonic-gate 			return (E_NO_ROOM);
206*7c478bd9Sstevel@tonic-gate 		}
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 		/* Create the menu window and derived windows */
209*7c478bd9Sstevel@tonic-gate 		if ((Win(m) = newwin(Rows(m), Width(m), 0, 0)) ==
210*7c478bd9Sstevel@tonic-gate 		    (WINDOW *) NULL) {
211*7c478bd9Sstevel@tonic-gate 			return (E_SYSTEM_ERROR);
212*7c478bd9Sstevel@tonic-gate 		}
213*7c478bd9Sstevel@tonic-gate 
214*7c478bd9Sstevel@tonic-gate 		/*
215*7c478bd9Sstevel@tonic-gate 		 * Take the minimum of the height of the menu (Height), the
216*7c478bd9Sstevel@tonic-gate 		 * physical height of the window (r), and the number of rows
217*7c478bd9Sstevel@tonic-gate 		 * in the menu (Rows).
218*7c478bd9Sstevel@tonic-gate 		 */
219*7c478bd9Sstevel@tonic-gate 		r = min(min(r, Rows(m)), Height(m));
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 		if ((Sub(m) = derwin(Win(m), r, Width(m), 0, 0)) ==
222*7c478bd9Sstevel@tonic-gate 		    (WINDOW *)  NULL) {
223*7c478bd9Sstevel@tonic-gate 			return (E_SYSTEM_ERROR);
224*7c478bd9Sstevel@tonic-gate 		}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 		/* If needed, link all items in the menu */
227*7c478bd9Sstevel@tonic-gate 		if (LinkNeeded(m)) {
228*7c478bd9Sstevel@tonic-gate 			_link_items(m);
229*7c478bd9Sstevel@tonic-gate 		}
230*7c478bd9Sstevel@tonic-gate 
231*7c478bd9Sstevel@tonic-gate 		SetPost(m);
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate 		/* If only one value can be set then unset all values */
234*7c478bd9Sstevel@tonic-gate 		/* to start. */
235*7c478bd9Sstevel@tonic-gate 		if (OneValue(m)) {
236*7c478bd9Sstevel@tonic-gate 			for (ip = Items(m); *ip; ip++) {
237*7c478bd9Sstevel@tonic-gate 				Value(*ip) = FALSE;
238*7c478bd9Sstevel@tonic-gate 			}
239*7c478bd9Sstevel@tonic-gate 		}
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 		/* Go do the drawing of the menu */
242*7c478bd9Sstevel@tonic-gate 		_draw(m);
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 		Minit(m);
245*7c478bd9Sstevel@tonic-gate 		Iinit(m);
246*7c478bd9Sstevel@tonic-gate 		_show(m);		/* Display the menu */
247*7c478bd9Sstevel@tonic-gate 		return (E_OK);
248*7c478bd9Sstevel@tonic-gate 	}
249*7c478bd9Sstevel@tonic-gate 	return (E_NOT_CONNECTED);
250*7c478bd9Sstevel@tonic-gate }
251