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 /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1995-1999 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /* LINTLIBRARY */
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  * slk.c
31*7c478bd9Sstevel@tonic-gate  *
32*7c478bd9Sstevel@tonic-gate  * XCurses Library
33*7c478bd9Sstevel@tonic-gate  *
34*7c478bd9Sstevel@tonic-gate  * Copyright 1990, 1995 by Mortice Kern Systems Inc.  All rights reserved.
35*7c478bd9Sstevel@tonic-gate  *
36*7c478bd9Sstevel@tonic-gate  */
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #if M_RCSID
39*7c478bd9Sstevel@tonic-gate #ifndef lint
40*7c478bd9Sstevel@tonic-gate static char rcsID[] =
41*7c478bd9Sstevel@tonic-gate "$Header: /team/ps/sun_xcurses/archive/local_changes/xcurses/src/lib/"
42*7c478bd9Sstevel@tonic-gate "libxcurses/src/libc/xcurses/rcs/slk.c 1.9 1998/05/20 17:26:23 "
43*7c478bd9Sstevel@tonic-gate "cbates Exp $";
44*7c478bd9Sstevel@tonic-gate #endif
45*7c478bd9Sstevel@tonic-gate #endif
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate #include <private.h>
48*7c478bd9Sstevel@tonic-gate #include <string.h>
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate int	__m_slk_labels_on;
51*7c478bd9Sstevel@tonic-gate int	__m_slk_touched = 0;
52*7c478bd9Sstevel@tonic-gate /*
53*7c478bd9Sstevel@tonic-gate  * Flag for initialisation soft label keys once setupterm() has been called.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate int
slk_init(int fmt)56*7c478bd9Sstevel@tonic-gate slk_init(int fmt)
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	int	code = ERR;
59*7c478bd9Sstevel@tonic-gate 
60*7c478bd9Sstevel@tonic-gate 	if (0 <= fmt && fmt <= 1) {
61*7c478bd9Sstevel@tonic-gate 		__m_slk_format = fmt;
62*7c478bd9Sstevel@tonic-gate 		__m_slk_labels_on = 1;
63*7c478bd9Sstevel@tonic-gate 		code = OK;
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate 	return (code);
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate int
slk_attron(const chtype at)70*7c478bd9Sstevel@tonic-gate slk_attron(const chtype at)
71*7c478bd9Sstevel@tonic-gate {
72*7c478bd9Sstevel@tonic-gate 	int	code = OK;
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
75*7c478bd9Sstevel@tonic-gate 		code = wattron(__m_screen->_slk._w, (int) at);
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	return (code);
78*7c478bd9Sstevel@tonic-gate }
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate int
slk_attroff(const chtype at)81*7c478bd9Sstevel@tonic-gate slk_attroff(const chtype at)
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	int	code = OK;
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
86*7c478bd9Sstevel@tonic-gate 		code = wattroff(__m_screen->_slk._w, (int) at);
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 	return (code);
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate int
slk_attrset(const chtype at)92*7c478bd9Sstevel@tonic-gate slk_attrset(const chtype at)
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	int	code = OK;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
97*7c478bd9Sstevel@tonic-gate 		code = wattrset(__m_screen->_slk._w, (int) at);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	return (code);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate int
slk_attr_off(const attr_t at,void * opts)103*7c478bd9Sstevel@tonic-gate slk_attr_off(const attr_t at, void *opts)
104*7c478bd9Sstevel@tonic-gate {
105*7c478bd9Sstevel@tonic-gate 	int	code = OK;
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
108*7c478bd9Sstevel@tonic-gate 		code = wattr_off(__m_screen->_slk._w, at, opts);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	return (code);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate int
slk_attr_on(const attr_t at,void * opts)114*7c478bd9Sstevel@tonic-gate slk_attr_on(const attr_t at, void *opts)
115*7c478bd9Sstevel@tonic-gate {
116*7c478bd9Sstevel@tonic-gate 	int	code = OK;
117*7c478bd9Sstevel@tonic-gate 
118*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
119*7c478bd9Sstevel@tonic-gate 		code = wattr_on(__m_screen->_slk._w, at, opts);
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	return (code);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate int
slk_attr_set(const attr_t at,short co,void * opts)125*7c478bd9Sstevel@tonic-gate slk_attr_set(const attr_t at, short co, void *opts)
126*7c478bd9Sstevel@tonic-gate {
127*7c478bd9Sstevel@tonic-gate 	int	code = OK;
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
130*7c478bd9Sstevel@tonic-gate 		code = wattr_set(__m_screen->_slk._w, at, co, opts);
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate 	return (code);
133*7c478bd9Sstevel@tonic-gate }
134*7c478bd9Sstevel@tonic-gate 
135*7c478bd9Sstevel@tonic-gate int
slk_color(short co)136*7c478bd9Sstevel@tonic-gate slk_color(short co)
137*7c478bd9Sstevel@tonic-gate {
138*7c478bd9Sstevel@tonic-gate 	int	code = OK;
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
141*7c478bd9Sstevel@tonic-gate 		code = wcolor_set(__m_screen->_slk._w, co, (void *) 0);
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	return (code);
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate int
slk_touch(void)147*7c478bd9Sstevel@tonic-gate slk_touch(void)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	int	code = OK;
150*7c478bd9Sstevel@tonic-gate 	WINDOW	*w = __m_screen->_slk._w;
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	if (w != NULL) {
153*7c478bd9Sstevel@tonic-gate 		code = wtouchln(w, 0, 1, 1);
154*7c478bd9Sstevel@tonic-gate 		wtouchln_hard(w, 0, 1);
155*7c478bd9Sstevel@tonic-gate 	} else
156*7c478bd9Sstevel@tonic-gate 		__m_slk_touched = 1;
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate 	return (code);
159*7c478bd9Sstevel@tonic-gate }
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate /*
162*7c478bd9Sstevel@tonic-gate  * These label start columns assume 80 columns in order to
163*7c478bd9Sstevel@tonic-gate  * fit 8 _slk._labels of 8 columns.
164*7c478bd9Sstevel@tonic-gate  */
165*7c478bd9Sstevel@tonic-gate static const int	format[][8] = {
166*7c478bd9Sstevel@tonic-gate 	{ 0, 9, 18, 31, 40, 53, 62, 71 },
167*7c478bd9Sstevel@tonic-gate 	{ 0, 9, 18, 27, 44, 53, 62, 71 },
168*7c478bd9Sstevel@tonic-gate };
169*7c478bd9Sstevel@tonic-gate 
170*7c478bd9Sstevel@tonic-gate #define	_LABEL_LENGTH_MALLOC	\
171*7c478bd9Sstevel@tonic-gate 	(MB_LEN_MAX * ((1 + _M_CCHAR_MAX) * 8) + 1)
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate void
__m_slk_set_all(void)174*7c478bd9Sstevel@tonic-gate __m_slk_set_all(void)
175*7c478bd9Sstevel@tonic-gate {
176*7c478bd9Sstevel@tonic-gate 	int	i;
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 8; ++i) {
179*7c478bd9Sstevel@tonic-gate 		if (__m_screen->_slk._labels[i] != NULL) {
180*7c478bd9Sstevel@tonic-gate 			(void) slk_set(i + 1, __m_screen->_slk._labels[i],
181*7c478bd9Sstevel@tonic-gate 				__m_screen->_slk._justify[i]);
182*7c478bd9Sstevel@tonic-gate 		}
183*7c478bd9Sstevel@tonic-gate 	}
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate int
__m_slk_clear(int kluge)187*7c478bd9Sstevel@tonic-gate __m_slk_clear(int kluge)
188*7c478bd9Sstevel@tonic-gate {
189*7c478bd9Sstevel@tonic-gate 	int	i;
190*7c478bd9Sstevel@tonic-gate 	int	index;
191*7c478bd9Sstevel@tonic-gate 	int	code = ERR;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL) {
194*7c478bd9Sstevel@tonic-gate 		cchar_t	_bg = __m_screen->_slk._w->_bg;
195*7c478bd9Sstevel@tonic-gate 		if (kluge) {
196*7c478bd9Sstevel@tonic-gate 			/* Test suite expects spaces to have FG attributes */
197*7c478bd9Sstevel@tonic-gate 			__m_screen->_slk._w->_bg = __m_screen->_slk._w->_fg;
198*7c478bd9Sstevel@tonic-gate 		}
199*7c478bd9Sstevel@tonic-gate 		for (index = 0; index < 8; ++index) {
200*7c478bd9Sstevel@tonic-gate 			i = format[__m_slk_format][index];
201*7c478bd9Sstevel@tonic-gate 			(void) __m_cc_erase(__m_screen->_slk._w,
202*7c478bd9Sstevel@tonic-gate 				0, i, 0, i + 7);
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 		__m_screen->_slk._w->_bg = _bg;		/* Restore ... */
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	} else if (plab_norm != NULL) {
207*7c478bd9Sstevel@tonic-gate 		for (index = 0; index < 8; ++index) {
208*7c478bd9Sstevel@tonic-gate 			char	*p;
209*7c478bd9Sstevel@tonic-gate 			p = __m_screen->_slk._saved[index];
210*7c478bd9Sstevel@tonic-gate 			if (!p) {
211*7c478bd9Sstevel@tonic-gate 				p = (char *)malloc(_LABEL_LENGTH_MALLOC);
212*7c478bd9Sstevel@tonic-gate 				if (p == NULL)
213*7c478bd9Sstevel@tonic-gate 					goto error;
214*7c478bd9Sstevel@tonic-gate 				__m_screen->_slk._saved[index] = p;
215*7c478bd9Sstevel@tonic-gate 			}
216*7c478bd9Sstevel@tonic-gate 			(void) strcpy(p, (kluge) ? "" : "        ");
217*7c478bd9Sstevel@tonic-gate 		}
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL) {
220*7c478bd9Sstevel@tonic-gate 		code = wrefresh(__m_screen->_slk._w);
221*7c478bd9Sstevel@tonic-gate 	} else {
222*7c478bd9Sstevel@tonic-gate 		__m_slk_labels_on = 0;
223*7c478bd9Sstevel@tonic-gate 		code = slk_refresh();
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate error:
227*7c478bd9Sstevel@tonic-gate 	return (code);
228*7c478bd9Sstevel@tonic-gate }
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate int
slk_clear(void)231*7c478bd9Sstevel@tonic-gate slk_clear(void)
232*7c478bd9Sstevel@tonic-gate {
233*7c478bd9Sstevel@tonic-gate 	return (__m_slk_clear(0));
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate int
slk_restore(void)237*7c478bd9Sstevel@tonic-gate slk_restore(void)
238*7c478bd9Sstevel@tonic-gate {
239*7c478bd9Sstevel@tonic-gate 	int	code;
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate 	__m_slk_set_all();
242*7c478bd9Sstevel@tonic-gate 	__m_slk_labels_on = 1;
243*7c478bd9Sstevel@tonic-gate 	code = slk_refresh();
244*7c478bd9Sstevel@tonic-gate 	return (code);
245*7c478bd9Sstevel@tonic-gate }
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate int
slk_noutrefresh(void)248*7c478bd9Sstevel@tonic-gate slk_noutrefresh(void)
249*7c478bd9Sstevel@tonic-gate {
250*7c478bd9Sstevel@tonic-gate 	int	code;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL)
253*7c478bd9Sstevel@tonic-gate 		code = wnoutrefresh(__m_screen->_slk._w);
254*7c478bd9Sstevel@tonic-gate 	else {
255*7c478bd9Sstevel@tonic-gate 		if (__m_slk_touched) {
256*7c478bd9Sstevel@tonic-gate 			__m_slk_set_all();
257*7c478bd9Sstevel@tonic-gate 			__m_slk_touched = 0;
258*7c478bd9Sstevel@tonic-gate 		}
259*7c478bd9Sstevel@tonic-gate 		if (__m_slk_labels_on) {
260*7c478bd9Sstevel@tonic-gate 			if (label_on != NULL) {
261*7c478bd9Sstevel@tonic-gate 				(void) TPUTS(label_on, 1, __m_outc);
262*7c478bd9Sstevel@tonic-gate 			}
263*7c478bd9Sstevel@tonic-gate 		} else {
264*7c478bd9Sstevel@tonic-gate 			if (label_off != NULL) {
265*7c478bd9Sstevel@tonic-gate 				(void) TPUTS(label_off, 1, __m_outc);
266*7c478bd9Sstevel@tonic-gate 			}
267*7c478bd9Sstevel@tonic-gate 		}
268*7c478bd9Sstevel@tonic-gate 		(void) fflush(__m_screen->_of);
269*7c478bd9Sstevel@tonic-gate 		code = OK;
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate 
272*7c478bd9Sstevel@tonic-gate 	return (code);
273*7c478bd9Sstevel@tonic-gate }
274*7c478bd9Sstevel@tonic-gate 
275*7c478bd9Sstevel@tonic-gate int
slk_refresh(void)276*7c478bd9Sstevel@tonic-gate slk_refresh(void)
277*7c478bd9Sstevel@tonic-gate {
278*7c478bd9Sstevel@tonic-gate 	int	code;
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	if ((code = slk_noutrefresh()) == OK)
281*7c478bd9Sstevel@tonic-gate 		code = doupdate();
282*7c478bd9Sstevel@tonic-gate 
283*7c478bd9Sstevel@tonic-gate 	return (code);
284*7c478bd9Sstevel@tonic-gate }
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate void
__m_slk_doupdate(void)287*7c478bd9Sstevel@tonic-gate __m_slk_doupdate(void)
288*7c478bd9Sstevel@tonic-gate {
289*7c478bd9Sstevel@tonic-gate 	if ((__m_screen->_slk._w == NULL) && plab_norm) {
290*7c478bd9Sstevel@tonic-gate 		int	index;
291*7c478bd9Sstevel@tonic-gate 		for (index = 0; index < 8; index++) {
292*7c478bd9Sstevel@tonic-gate 			char	*s = __m_screen->_slk._saved[index];
293*7c478bd9Sstevel@tonic-gate 			if (s) {
294*7c478bd9Sstevel@tonic-gate 				(void) TPUTS(tparm(plab_norm, (long) index+1,
295*7c478bd9Sstevel@tonic-gate 					(long) s, 0L, 0L, 0L, 0L, 0L, 0L, 0L),
296*7c478bd9Sstevel@tonic-gate 					1, __m_outc);
297*7c478bd9Sstevel@tonic-gate 			}
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 	}
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate char *
slk_label(int index)303*7c478bd9Sstevel@tonic-gate slk_label(int index)
304*7c478bd9Sstevel@tonic-gate {
305*7c478bd9Sstevel@tonic-gate 	char	*label;
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	if (index < 1 || 8 < index) {
308*7c478bd9Sstevel@tonic-gate 		label = NULL;
309*7c478bd9Sstevel@tonic-gate 	} else {
310*7c478bd9Sstevel@tonic-gate 		label = __m_screen->_slk._labels[index-1];
311*7c478bd9Sstevel@tonic-gate 	}
312*7c478bd9Sstevel@tonic-gate 	return (label);
313*7c478bd9Sstevel@tonic-gate }
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate int
slk_set(int index,const char * label,int justify)316*7c478bd9Sstevel@tonic-gate slk_set(int index, const char *label, int justify)
317*7c478bd9Sstevel@tonic-gate {
318*7c478bd9Sstevel@tonic-gate 	int	code = ERR;
319*7c478bd9Sstevel@tonic-gate 	wchar_t	wcs[_M_CCHAR_MAX * 8 + 1];
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 	if ((label == NULL) || *label == '\0')
322*7c478bd9Sstevel@tonic-gate 		label = "        ";
323*7c478bd9Sstevel@tonic-gate 	if (mbstowcs(wcs, label, sizeof (wcs)) != (size_t)-1)
324*7c478bd9Sstevel@tonic-gate 		code = slk_wset(index, wcs, justify);
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	return (code);
327*7c478bd9Sstevel@tonic-gate }
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate int
slk_wset(int index,const wchar_t * label,int justify)330*7c478bd9Sstevel@tonic-gate slk_wset(int index, const wchar_t *label, int justify)
331*7c478bd9Sstevel@tonic-gate {
332*7c478bd9Sstevel@tonic-gate 	cchar_t	cc;
333*7c478bd9Sstevel@tonic-gate 	int	i, width, code = ERR;
334*7c478bd9Sstevel@tonic-gate 	wchar_t	wcs[_M_CCHAR_MAX * 8 + 1], *wp;
335*7c478bd9Sstevel@tonic-gate 	char	mbs[_LABEL_LENGTH_MALLOC];
336*7c478bd9Sstevel@tonic-gate 	char	tmbs[_LABEL_LENGTH_MALLOC];
337*7c478bd9Sstevel@tonic-gate 	int	ww = 0;
338*7c478bd9Sstevel@tonic-gate 	int	left1, left2;
339*7c478bd9Sstevel@tonic-gate 	static const char	*spcs = "        ";
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	if (index < 1 || 8 < index || justify < 0 || 2 < justify)
342*7c478bd9Sstevel@tonic-gate 		goto error1;
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate 	index--;	/* Shift from {1..8} to {0..7} */
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	if (label == NULL)
347*7c478bd9Sstevel@tonic-gate 		label = L"";
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	/* Copy the characters that fill the first 8 columns of the label. */
350*7c478bd9Sstevel@tonic-gate 	for (wp = wcs, width = 0; *label != '\0'; label += i, wp += cc._n) {
351*7c478bd9Sstevel@tonic-gate 		if ((i = __m_wcs_cc(label, A_NORMAL, 0, &cc)) < 0)
352*7c478bd9Sstevel@tonic-gate 			goto error1;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 		ww += __m_cc_width(&cc);
355*7c478bd9Sstevel@tonic-gate 		if (ww > 8)
356*7c478bd9Sstevel@tonic-gate 			break;
357*7c478bd9Sstevel@tonic-gate 		else
358*7c478bd9Sstevel@tonic-gate 			width = ww;
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 		(void) wcsncpy(wp, cc._wc, cc._n);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 	*wp = '\0';
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	if (wcstombs(tmbs, wcs, sizeof (mbs)) == (size_t) -1)
365*7c478bd9Sstevel@tonic-gate 		goto error1;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	if (width == 8) {
368*7c478bd9Sstevel@tonic-gate 		(void) strcpy(mbs, tmbs);
369*7c478bd9Sstevel@tonic-gate 	} else {
370*7c478bd9Sstevel@tonic-gate 		switch (justify) {
371*7c478bd9Sstevel@tonic-gate 		case 0:
372*7c478bd9Sstevel@tonic-gate 			(void) strcpy(mbs, tmbs);
373*7c478bd9Sstevel@tonic-gate 			(void) strncat(mbs, spcs, (8 - width));
374*7c478bd9Sstevel@tonic-gate 			*(mbs + strlen(tmbs) + (8 - width)) = '\0';
375*7c478bd9Sstevel@tonic-gate 			break;
376*7c478bd9Sstevel@tonic-gate 		case 1:
377*7c478bd9Sstevel@tonic-gate 			left1 = (8 - width) / 2;
378*7c478bd9Sstevel@tonic-gate 			(void) strncpy(mbs, spcs, left1);
379*7c478bd9Sstevel@tonic-gate 			(void) strcpy(mbs + left1, tmbs);
380*7c478bd9Sstevel@tonic-gate 			left2 = 8 - width - left1;
381*7c478bd9Sstevel@tonic-gate 			(void) strncat(mbs, spcs, left2);
382*7c478bd9Sstevel@tonic-gate 			*(mbs + left1 + strlen(tmbs) + left2) = '\0';
383*7c478bd9Sstevel@tonic-gate 			break;
384*7c478bd9Sstevel@tonic-gate 		case 2:
385*7c478bd9Sstevel@tonic-gate 			left1 = 8 - width;
386*7c478bd9Sstevel@tonic-gate 			(void) strncpy(mbs, spcs, left1);
387*7c478bd9Sstevel@tonic-gate 			(void) strcpy(mbs + left1, tmbs);
388*7c478bd9Sstevel@tonic-gate 			break;
389*7c478bd9Sstevel@tonic-gate 		}
390*7c478bd9Sstevel@tonic-gate 	}
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	/* Remember the new label. */
393*7c478bd9Sstevel@tonic-gate 	__m_screen->_slk._justify[index] = (short) justify;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._labels[index] != NULL)
396*7c478bd9Sstevel@tonic-gate 		free(__m_screen->_slk._labels[index]);
397*7c478bd9Sstevel@tonic-gate 	if ((__m_screen->_slk._labels[index] = strdup(tmbs)) == NULL)
398*7c478bd9Sstevel@tonic-gate 		goto error1;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	if (plab_norm != NULL) {
401*7c478bd9Sstevel@tonic-gate 		char	*p;
402*7c478bd9Sstevel@tonic-gate 		p = __m_screen->_slk._saved[index];
403*7c478bd9Sstevel@tonic-gate 		if (!p) {
404*7c478bd9Sstevel@tonic-gate 			p = (char *)malloc(_LABEL_LENGTH_MALLOC);
405*7c478bd9Sstevel@tonic-gate 			if (p == NULL)
406*7c478bd9Sstevel@tonic-gate 				goto error1;
407*7c478bd9Sstevel@tonic-gate 			__m_screen->_slk._saved[index] = p;
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 		(void) strcpy(p, mbs);
410*7c478bd9Sstevel@tonic-gate 	}
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate 	__m_slk_labels_on = 1;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	if (__m_screen->_slk._w != NULL) {
415*7c478bd9Sstevel@tonic-gate 		cchar_t	_bg = __m_screen->_slk._w->_bg;
416*7c478bd9Sstevel@tonic-gate 		/* Write the justified label into the slk window. */
417*7c478bd9Sstevel@tonic-gate 		i = format[__m_slk_format][index];
418*7c478bd9Sstevel@tonic-gate 		__m_screen->_slk._w->_bg = __m_screen->_slk._w->_fg;
419*7c478bd9Sstevel@tonic-gate 		(void) __m_cc_erase(__m_screen->_slk._w, 0, i, 0, i + 7);
420*7c478bd9Sstevel@tonic-gate 		__m_screen->_slk._w->_bg = _bg;		/* Restore ... */
421*7c478bd9Sstevel@tonic-gate 
422*7c478bd9Sstevel@tonic-gate 		(void) mvwaddstr(__m_screen->_slk._w, 0, i, mbs);
423*7c478bd9Sstevel@tonic-gate 	}
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate 	code = OK;
426*7c478bd9Sstevel@tonic-gate error1:
427*7c478bd9Sstevel@tonic-gate 	return (code);
428*7c478bd9Sstevel@tonic-gate }
429