xref: /illumos-gate/usr/src/lib/libeti/menu/common/link.c (revision 1da57d55)
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 /*LINTLIBRARY*/
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
34*7c478bd9Sstevel@tonic-gate #include "private.h"
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate static void
link_col_major(MENU * m)37*7c478bd9Sstevel@tonic-gate link_col_major(MENU *m)
38*7c478bd9Sstevel@tonic-gate {
39*7c478bd9Sstevel@tonic-gate 	ITEM *i;
40*7c478bd9Sstevel@tonic-gate 	int n;
41*7c478bd9Sstevel@tonic-gate 	short c, r;
42*7c478bd9Sstevel@tonic-gate 	int left, up;
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate 	r = 0;
45*7c478bd9Sstevel@tonic-gate 	c = 0;
46*7c478bd9Sstevel@tonic-gate 	for (i = IthItem(m, 0), n = 0; i; i = IthItem(m, ++n)) {
47*7c478bd9Sstevel@tonic-gate 		X(i) = c;
48*7c478bd9Sstevel@tonic-gate 		Y(i) = r;
49*7c478bd9Sstevel@tonic-gate 		Left(i) = c ? IthItem(m, n-Rows(m)) : (ITEM *) NULL;
50*7c478bd9Sstevel@tonic-gate 		if (n + Rows(m) >= Nitems(m)) {
51*7c478bd9Sstevel@tonic-gate 			Right(i) = (ITEM *) NULL;
52*7c478bd9Sstevel@tonic-gate 		} else {
53*7c478bd9Sstevel@tonic-gate 			Right(i) = IthItem(m, n + Rows(m));
54*7c478bd9Sstevel@tonic-gate 		}
55*7c478bd9Sstevel@tonic-gate 		Up(i) = r ? IthItem(m, n-1) : (ITEM *) NULL;
56*7c478bd9Sstevel@tonic-gate 		Down(i) = (r == Rows(m)-1) ? (ITEM *)0 : IthItem(m, n+1);
57*7c478bd9Sstevel@tonic-gate 		if (++r == Rows(m)) {
58*7c478bd9Sstevel@tonic-gate 			r = 0;
59*7c478bd9Sstevel@tonic-gate 			c += 1;
60*7c478bd9Sstevel@tonic-gate 		}
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 	if (r) {
63*7c478bd9Sstevel@tonic-gate 		Down(IthItem(m, n-1)) = IthItem(m, n - Rows(m));
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	if (Cyclic(m)) {
67*7c478bd9Sstevel@tonic-gate 		/* Set up left and right links at edge of menu */
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 		r = Rows(m) * (Nitems(m)/Rows(m));
70*7c478bd9Sstevel@tonic-gate 		for (n = 0; n < Rows(m); n++) {
71*7c478bd9Sstevel@tonic-gate 			left = n + r;
72*7c478bd9Sstevel@tonic-gate 			if (left >= Nitems(m)) {
73*7c478bd9Sstevel@tonic-gate 				left -= Rows(m);
74*7c478bd9Sstevel@tonic-gate 			}
75*7c478bd9Sstevel@tonic-gate 			Left(IthItem(m, n)) = IthItem(m, left);
76*7c478bd9Sstevel@tonic-gate 			Right(IthItem(m, left)) = IthItem(m, n);
77*7c478bd9Sstevel@tonic-gate 		}
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 		/* Setup up and down links at edge of menu */
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate 		for (n = 0; n < Nitems(m); n += Rows(m)) {
82*7c478bd9Sstevel@tonic-gate 			up = n + Rows(m) - 1;
83*7c478bd9Sstevel@tonic-gate 			if (up >= Nitems(m)) {
84*7c478bd9Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, n-1);
85*7c478bd9Sstevel@tonic-gate 			} else {
86*7c478bd9Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, up);
87*7c478bd9Sstevel@tonic-gate 				Down(IthItem(m, up)) = IthItem(m, n);
88*7c478bd9Sstevel@tonic-gate 			}
89*7c478bd9Sstevel@tonic-gate 		}
90*7c478bd9Sstevel@tonic-gate 	}
91*7c478bd9Sstevel@tonic-gate }
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate static void
link_row_major(MENU * m)94*7c478bd9Sstevel@tonic-gate link_row_major(MENU *m)
95*7c478bd9Sstevel@tonic-gate {
96*7c478bd9Sstevel@tonic-gate 	int n;
97*7c478bd9Sstevel@tonic-gate 	short c, r;
98*7c478bd9Sstevel@tonic-gate 	ITEM *i;
99*7c478bd9Sstevel@tonic-gate 	int left, up;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	r = 0;
102*7c478bd9Sstevel@tonic-gate 	c = 0;
103*7c478bd9Sstevel@tonic-gate 	for (i = IthItem(m, 0), n = 0; i; i = IthItem(m, ++n)) {
104*7c478bd9Sstevel@tonic-gate 		X(i) = c;
105*7c478bd9Sstevel@tonic-gate 		Y(i) = r;
106*7c478bd9Sstevel@tonic-gate 		Left(i) = c ? IthItem(m, n-1) : (ITEM *) NULL;
107*7c478bd9Sstevel@tonic-gate 		Right(i) = (c == Cols(m)-1 || n == Nitems(m)-1) ? (ITEM *)0 :
108*7c478bd9Sstevel@tonic-gate 				IthItem(m, n+1);
109*7c478bd9Sstevel@tonic-gate 		Up(i) = r ? IthItem(m, n-Cols(m)) : (ITEM *) NULL;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 		if (n+Cols(m) < Nitems(m)) {
112*7c478bd9Sstevel@tonic-gate 			Down(i) = IthItem(m, n + Cols(m));
113*7c478bd9Sstevel@tonic-gate 		} else {
114*7c478bd9Sstevel@tonic-gate 			if (r == Rows(m)-1) {
115*7c478bd9Sstevel@tonic-gate 				/* Down is undefined if this is a complete */
116*7c478bd9Sstevel@tonic-gate 				/* last column */
117*7c478bd9Sstevel@tonic-gate 				Down(i) = (ITEM *) NULL;
118*7c478bd9Sstevel@tonic-gate 			} else {
119*7c478bd9Sstevel@tonic-gate 				/* Down is set to last item if the last */
120*7c478bd9Sstevel@tonic-gate 				/* column isn't full */
121*7c478bd9Sstevel@tonic-gate 				Down(i) = IthItem(m, Nitems(m)-1);
122*7c478bd9Sstevel@tonic-gate 			}
123*7c478bd9Sstevel@tonic-gate 		}
124*7c478bd9Sstevel@tonic-gate 		if (++c == Cols(m)) {
125*7c478bd9Sstevel@tonic-gate 			c = 0;
126*7c478bd9Sstevel@tonic-gate 			r += 1;
127*7c478bd9Sstevel@tonic-gate 		}
128*7c478bd9Sstevel@tonic-gate 	}
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	if (Cyclic(m)) {
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 		/* Setup left and right links at edge of menu */
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 		for (n = 0; n < Nitems(m); n += Cols(m)) {
135*7c478bd9Sstevel@tonic-gate 			left = n + Cols(m) - 1;
136*7c478bd9Sstevel@tonic-gate 			if (left >= Nitems(m)) {
137*7c478bd9Sstevel@tonic-gate 				left = Nitems(m) - 1;
138*7c478bd9Sstevel@tonic-gate 			}
139*7c478bd9Sstevel@tonic-gate 			Left(IthItem(m, n)) = IthItem(m, left);
140*7c478bd9Sstevel@tonic-gate 			Right(IthItem(m, left)) = IthItem(m, n);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 		/* Setup up and down links at edge of menu */
144*7c478bd9Sstevel@tonic-gate 
145*7c478bd9Sstevel@tonic-gate 		r = (Rows(m) - 1) * Cols(m);
146*7c478bd9Sstevel@tonic-gate 		for (n = 0; n < Cols(m); n++) {
147*7c478bd9Sstevel@tonic-gate 			up = n + r;
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 			/* If there is an incomplete line below this one */
150*7c478bd9Sstevel@tonic-gate 			/* the point to the last item in the menu. */
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 			if (up >= Nitems(m)) {
153*7c478bd9Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, Nitems(m)-1);
154*7c478bd9Sstevel@tonic-gate 			} else {
155*7c478bd9Sstevel@tonic-gate 				Up(IthItem(m, n)) = IthItem(m, up);
156*7c478bd9Sstevel@tonic-gate 				Down(IthItem(m, up)) = IthItem(m, n);
157*7c478bd9Sstevel@tonic-gate 			}
158*7c478bd9Sstevel@tonic-gate 		}
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate void
_link_items(MENU * m)163*7c478bd9Sstevel@tonic-gate _link_items(MENU *m)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	if (Items(m) && IthItem(m, 0)) {
166*7c478bd9Sstevel@tonic-gate 		ResetLink(m);
167*7c478bd9Sstevel@tonic-gate 		if (RowMajor(m)) {
168*7c478bd9Sstevel@tonic-gate 			link_row_major(m);
169*7c478bd9Sstevel@tonic-gate 		} else {
170*7c478bd9Sstevel@tonic-gate 			link_col_major(m);
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate }
174