xref: /onnv-gate/usr/src/lib/libeti/menu/common/link.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1997, by Sun Mircrosystems, Inc.
28*0Sstevel@tonic-gate  * All rights reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.6	*/
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*LINTLIBRARY*/
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <sys/types.h>
36*0Sstevel@tonic-gate #include "private.h"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate static void
link_col_major(MENU * m)39*0Sstevel@tonic-gate link_col_major(MENU *m)
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	ITEM *i;
42*0Sstevel@tonic-gate 	int n;
43*0Sstevel@tonic-gate 	short c, r;
44*0Sstevel@tonic-gate 	int left, up;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate 	r = 0;
47*0Sstevel@tonic-gate 	c = 0;
48*0Sstevel@tonic-gate 	for (i = IthItem(m, 0), n = 0; i; i = IthItem(m, ++n)) {
49*0Sstevel@tonic-gate 		X(i) = c;
50*0Sstevel@tonic-gate 		Y(i) = r;
51*0Sstevel@tonic-gate 		Left(i) = c ? IthItem(m, n-Rows(m)) : (ITEM *) NULL;
52*0Sstevel@tonic-gate 		if (n + Rows(m) >= Nitems(m)) {
53*0Sstevel@tonic-gate 			Right(i) = (ITEM *) NULL;
54*0Sstevel@tonic-gate 		} else {
55*0Sstevel@tonic-gate 			Right(i) = IthItem(m, n + Rows(m));
56*0Sstevel@tonic-gate 		}
57*0Sstevel@tonic-gate 		Up(i) = r ? IthItem(m, n-1) : (ITEM *) NULL;
58*0Sstevel@tonic-gate 		Down(i) = (r == Rows(m)-1) ? (ITEM *)0 : IthItem(m, n+1);
59*0Sstevel@tonic-gate 		if (++r == Rows(m)) {
60*0Sstevel@tonic-gate 			r = 0;
61*0Sstevel@tonic-gate 			c += 1;
62*0Sstevel@tonic-gate 		}
63*0Sstevel@tonic-gate 	}
64*0Sstevel@tonic-gate 	if (r) {
65*0Sstevel@tonic-gate 		Down(IthItem(m, n-1)) = IthItem(m, n - Rows(m));
66*0Sstevel@tonic-gate 	}
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 	if (Cyclic(m)) {
69*0Sstevel@tonic-gate 		/* Set up left and right links at edge of menu */
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 		r = Rows(m) * (Nitems(m)/Rows(m));
72*0Sstevel@tonic-gate 		for (n = 0; n < Rows(m); n++) {
73*0Sstevel@tonic-gate 			left = n + r;
74*0Sstevel@tonic-gate 			if (left >= Nitems(m)) {
75*0Sstevel@tonic-gate 				left -= Rows(m);
76*0Sstevel@tonic-gate 			}
77*0Sstevel@tonic-gate 			Left(IthItem(m, n)) = IthItem(m, left);
78*0Sstevel@tonic-gate 			Right(IthItem(m, left)) = IthItem(m, n);
79*0Sstevel@tonic-gate 		}
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		/* Setup up and down links at edge of menu */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 		for (n = 0; n < Nitems(m); n += Rows(m)) {
84*0Sstevel@tonic-gate 			up = n + Rows(m) - 1;
85*0Sstevel@tonic-gate 			if (up >= Nitems(m)) {
86*0Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, n-1);
87*0Sstevel@tonic-gate 			} else {
88*0Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, up);
89*0Sstevel@tonic-gate 				Down(IthItem(m, up)) = IthItem(m, n);
90*0Sstevel@tonic-gate 			}
91*0Sstevel@tonic-gate 		}
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate }
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static void
link_row_major(MENU * m)96*0Sstevel@tonic-gate link_row_major(MENU *m)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	int n;
99*0Sstevel@tonic-gate 	short c, r;
100*0Sstevel@tonic-gate 	ITEM *i;
101*0Sstevel@tonic-gate 	int left, up;
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate 	r = 0;
104*0Sstevel@tonic-gate 	c = 0;
105*0Sstevel@tonic-gate 	for (i = IthItem(m, 0), n = 0; i; i = IthItem(m, ++n)) {
106*0Sstevel@tonic-gate 		X(i) = c;
107*0Sstevel@tonic-gate 		Y(i) = r;
108*0Sstevel@tonic-gate 		Left(i) = c ? IthItem(m, n-1) : (ITEM *) NULL;
109*0Sstevel@tonic-gate 		Right(i) = (c == Cols(m)-1 || n == Nitems(m)-1) ? (ITEM *)0 :
110*0Sstevel@tonic-gate 				IthItem(m, n+1);
111*0Sstevel@tonic-gate 		Up(i) = r ? IthItem(m, n-Cols(m)) : (ITEM *) NULL;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 		if (n+Cols(m) < Nitems(m)) {
114*0Sstevel@tonic-gate 			Down(i) = IthItem(m, n + Cols(m));
115*0Sstevel@tonic-gate 		} else {
116*0Sstevel@tonic-gate 			if (r == Rows(m)-1) {
117*0Sstevel@tonic-gate 				/* Down is undefined if this is a complete */
118*0Sstevel@tonic-gate 				/* last column */
119*0Sstevel@tonic-gate 				Down(i) = (ITEM *) NULL;
120*0Sstevel@tonic-gate 			} else {
121*0Sstevel@tonic-gate 				/* Down is set to last item if the last */
122*0Sstevel@tonic-gate 				/* column isn't full */
123*0Sstevel@tonic-gate 				Down(i) = IthItem(m, Nitems(m)-1);
124*0Sstevel@tonic-gate 			}
125*0Sstevel@tonic-gate 		}
126*0Sstevel@tonic-gate 		if (++c == Cols(m)) {
127*0Sstevel@tonic-gate 			c = 0;
128*0Sstevel@tonic-gate 			r += 1;
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (Cyclic(m)) {
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 		/* Setup left and right links at edge of menu */
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 		for (n = 0; n < Nitems(m); n += Cols(m)) {
137*0Sstevel@tonic-gate 			left = n + Cols(m) - 1;
138*0Sstevel@tonic-gate 			if (left >= Nitems(m)) {
139*0Sstevel@tonic-gate 				left = Nitems(m) - 1;
140*0Sstevel@tonic-gate 			}
141*0Sstevel@tonic-gate 			Left(IthItem(m, n)) = IthItem(m, left);
142*0Sstevel@tonic-gate 			Right(IthItem(m, left)) = IthItem(m, n);
143*0Sstevel@tonic-gate 		}
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 		/* Setup up and down links at edge of menu */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 		r = (Rows(m) - 1) * Cols(m);
148*0Sstevel@tonic-gate 		for (n = 0; n < Cols(m); n++) {
149*0Sstevel@tonic-gate 			up = n + r;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 			/* If there is an incomplete line below this one */
152*0Sstevel@tonic-gate 			/* the point to the last item in the menu. */
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 			if (up >= Nitems(m)) {
155*0Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, Nitems(m)-1);
156*0Sstevel@tonic-gate 			} else {
157*0Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, up);
158*0Sstevel@tonic-gate 				Down(IthItem(m, up)) = IthItem(m, n);
159*0Sstevel@tonic-gate 			}
160*0Sstevel@tonic-gate 		}
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate }
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate void
_link_items(MENU * m)165*0Sstevel@tonic-gate _link_items(MENU *m)
166*0Sstevel@tonic-gate {
167*0Sstevel@tonic-gate 	if (Items(m) && IthItem(m, 0)) {
168*0Sstevel@tonic-gate 		ResetLink(m);
169*0Sstevel@tonic-gate 		if (RowMajor(m)) {
170*0Sstevel@tonic-gate 			link_row_major(m);
171*0Sstevel@tonic-gate 		} else {
172*0Sstevel@tonic-gate 			link_col_major(m);
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 	}
175*0Sstevel@tonic-gate }
176