150276Speter/****************************************************************************
2262629Sdelphij * Copyright (c) 1998-2010,2011 Free Software Foundation, Inc.              *
350276Speter *                                                                          *
450276Speter * Permission is hereby granted, free of charge, to any person obtaining a  *
550276Speter * copy of this software and associated documentation files (the            *
650276Speter * "Software"), to deal in the Software without restriction, including      *
750276Speter * without limitation the rights to use, copy, modify, merge, publish,      *
850276Speter * distribute, distribute with modifications, sublicense, and/or sell       *
950276Speter * copies of the Software, and to permit persons to whom the Software is    *
1050276Speter * furnished to do so, subject to the following conditions:                 *
1150276Speter *                                                                          *
1250276Speter * The above copyright notice and this permission notice shall be included  *
1350276Speter * in all copies or substantial portions of the Software.                   *
1450276Speter *                                                                          *
1550276Speter * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
1650276Speter * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
1750276Speter * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
1850276Speter * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
1950276Speter * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
2050276Speter * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
2150276Speter * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
2250276Speter *                                                                          *
2350276Speter * Except as contained in this notice, the name(s) of the above copyright   *
2450276Speter * holders shall not be used in advertising or otherwise to promote the     *
2550276Speter * sale, use or other dealings in this Software without prior written       *
2650276Speter * authorization.                                                           *
2750276Speter ****************************************************************************/
2850276Speter
2950276Speter/****************************************************************************
30166124Srafan *  Authors:                                                                *
31166124Srafan *          Gerhard Fuernkranz                      1993 (original)         *
32166124Srafan *          Zeyd M. Ben-Halim                       1992,1995 (sic)         *
33166124Srafan *          Eric S. Raymond                                                 *
34166124Srafan *          Juergen Pfeifer                         1996-on                 *
35166124Srafan *          Thomas E. Dickey                                                *
3650276Speter ****************************************************************************/
3750276Speter
3850276Speter/*
3950276Speter *	lib_slk.c
4050276Speter *	Soft key routines.
4150276Speter */
4250276Speter
4350276Speter#include <curses.priv.h>
4450276Speter#include <ctype.h>
4550276Speter
46262629Sdelphij#ifndef CUR
47262629Sdelphij#define CUR SP_TERMTYPE
48262629Sdelphij#endif
4950276Speter
50262629SdelphijMODULE_ID("$Id: lib_slk.c,v 1.48 2011/03/05 21:21:52 tom Exp $")
51262629Sdelphij
52262629Sdelphij#ifdef USE_TERM_DRIVER
53262629Sdelphij#define NumLabels    InfoOf(SP_PARM).numlabels
54262629Sdelphij#define NoColorVideo InfoOf(SP_PARM).nocolorvideo
55262629Sdelphij#define LabelWidth   InfoOf(SP_PARM).labelwidth
56262629Sdelphij#define LabelHeight  InfoOf(SP_PARM).labelheight
57262629Sdelphij#else
58262629Sdelphij#define NumLabels    num_labels
59262629Sdelphij#define NoColorVideo no_color_video
60262629Sdelphij#define LabelWidth   label_width
61262629Sdelphij#define LabelHeight  label_height
62262629Sdelphij#endif
63262629Sdelphij
6450276Speter/*
65166124Srafan * Free any memory related to soft labels, return an error.
66166124Srafan */
67166124Srafanstatic int
68262629Sdelphijslk_failed(NCURSES_SP_DCL0)
69166124Srafan{
70262629Sdelphij    if ((0 != SP_PARM) && SP_PARM->_slk) {
71262629Sdelphij	FreeIfNeeded(SP_PARM->_slk->ent);
72262629Sdelphij	free(SP_PARM->_slk);
73262629Sdelphij	SP_PARM->_slk = (SLK *) 0;
74166124Srafan    }
75166124Srafan    return ERR;
76166124Srafan}
77166124Srafan
78262629SdelphijNCURSES_EXPORT(int)
79262629Sdelphij_nc_format_slks(NCURSES_SP_DCLx int cols)
80262629Sdelphij{
81262629Sdelphij    int gap, i, x;
82262629Sdelphij    int max_length;
83262629Sdelphij
84262629Sdelphij    if (!SP_PARM || !SP_PARM->_slk)
85262629Sdelphij	return ERR;
86262629Sdelphij
87262629Sdelphij    max_length = SP_PARM->_slk->maxlen;
88262629Sdelphij    if (SP_PARM->slk_format >= 3) {	/* PC style */
89262629Sdelphij	gap = (cols - 3 * (3 + 4 * max_length)) / 2;
90262629Sdelphij
91262629Sdelphij	if (gap < 1)
92262629Sdelphij	    gap = 1;
93262629Sdelphij
94262629Sdelphij	for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
95262629Sdelphij	    SP_PARM->_slk->ent[i].ent_x = x;
96262629Sdelphij	    x += max_length;
97262629Sdelphij	    x += (i == 3 || i == 7) ? gap : 1;
98262629Sdelphij	}
99262629Sdelphij    } else {
100262629Sdelphij	if (SP_PARM->slk_format == 2) {		/* 4-4 */
101262629Sdelphij	    gap = cols - (int) (SP_PARM->_slk->maxlab * max_length) - 6;
102262629Sdelphij
103262629Sdelphij	    if (gap < 1)
104262629Sdelphij		gap = 1;
105262629Sdelphij	    for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
106262629Sdelphij		SP_PARM->_slk->ent[i].ent_x = x;
107262629Sdelphij		x += max_length;
108262629Sdelphij		x += (i == 3) ? gap : 1;
109262629Sdelphij	    }
110262629Sdelphij	} else {
111262629Sdelphij	    if (SP_PARM->slk_format == 1) {	/* 1 -> 3-2-3 */
112262629Sdelphij		gap = (cols - (SP_PARM->_slk->maxlab * max_length) - 5)
113262629Sdelphij		    / 2;
114262629Sdelphij
115262629Sdelphij		if (gap < 1)
116262629Sdelphij		    gap = 1;
117262629Sdelphij		for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
118262629Sdelphij		    SP_PARM->_slk->ent[i].ent_x = x;
119262629Sdelphij		    x += max_length;
120262629Sdelphij		    x += (i == 2 || i == 4) ? gap : 1;
121262629Sdelphij		}
122262629Sdelphij	    } else {
123262629Sdelphij		return slk_failed(NCURSES_SP_ARG);
124262629Sdelphij	    }
125262629Sdelphij	}
126262629Sdelphij    }
127262629Sdelphij    SP_PARM->_slk->dirty = TRUE;
128262629Sdelphij
129262629Sdelphij    return OK;
130262629Sdelphij}
131262629Sdelphij
132166124Srafan/*
13350276Speter * Initialize soft labels.
13450276Speter * Called from newterm()
13550276Speter */
13676726SpeterNCURSES_EXPORT(int)
13750276Speter_nc_slk_initialize(WINDOW *stwin, int cols)
13850276Speter{
139262629Sdelphij    int i;
14076726Speter    int res = OK;
141262629Sdelphij    size_t max_length;
142262629Sdelphij    SCREEN *sp;
143262629Sdelphij    int numlab;
14450276Speter
145166124Srafan    T((T_CALLED("_nc_slk_initialize()")));
14650276Speter
147262629Sdelphij    assert(stwin);
148262629Sdelphij
149262629Sdelphij    sp = _nc_screen_of(stwin);
150262629Sdelphij    if (0 == sp)
151262629Sdelphij	returnCode(ERR);
152262629Sdelphij
153262629Sdelphij    assert(TerminalOf(SP_PARM));
154262629Sdelphij
155262629Sdelphij    numlab = NumLabels;
156262629Sdelphij
157262629Sdelphij    if (SP_PARM->_slk) {	/* we did this already, so simply return */
158166124Srafan	returnCode(OK);
159262629Sdelphij    } else if ((SP_PARM->_slk = typeCalloc(SLK, 1)) == 0)
160166124Srafan	returnCode(ERR);
16150276Speter
162262629Sdelphij    if (!SP_PARM->slk_format)
163262629Sdelphij	SP_PARM->slk_format = _nc_globals.slk_format;
16450276Speter
165166124Srafan    /*
166166124Srafan     * If we use colors, vidputs() will suppress video attributes that conflict
167166124Srafan     * with colors.  In that case, we're still guaranteed that "reverse" would
168166124Srafan     * work.
169166124Srafan     */
170262629Sdelphij    if ((NoColorVideo & 1) == 0)
171262629Sdelphij	SetAttr(SP_PARM->_slk->attr, A_STANDOUT);
172166124Srafan    else
173262629Sdelphij	SetAttr(SP_PARM->_slk->attr, A_REVERSE);
17450276Speter
175262629Sdelphij    SP_PARM->_slk->maxlab = (short) ((numlab > 0)
176262629Sdelphij				     ? numlab
177262629Sdelphij				     : MAX_SKEY(SP_PARM->slk_format));
178262629Sdelphij    SP_PARM->_slk->maxlen = (short) ((numlab > 0)
179262629Sdelphij				     ? LabelWidth * LabelHeight
180262629Sdelphij				     : MAX_SKEY_LEN(SP_PARM->slk_format));
181262629Sdelphij    SP_PARM->_slk->labcnt = (short) ((SP_PARM->_slk->maxlab < MAX_SKEY(SP_PARM->slk_format))
182262629Sdelphij				     ? MAX_SKEY(SP_PARM->slk_format)
183262629Sdelphij				     : SP_PARM->_slk->maxlab);
18450276Speter
185262629Sdelphij    if (SP_PARM->_slk->maxlen <= 0
186262629Sdelphij	|| SP_PARM->_slk->labcnt <= 0
187262629Sdelphij	|| (SP_PARM->_slk->ent = typeCalloc(slk_ent,
188262629Sdelphij					    (size_t) SP_PARM->_slk->labcnt))
189262629Sdelphij	== NULL)
190262629Sdelphij	returnCode(slk_failed(NCURSES_SP_ARG));
19150276Speter
192262629Sdelphij    max_length = (size_t) SP_PARM->_slk->maxlen;
193262629Sdelphij    for (i = 0; i < SP_PARM->_slk->labcnt; i++) {
194166124Srafan	size_t used = max_length + 1;
195166124Srafan
196262629Sdelphij	SP_PARM->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used);
197262629Sdelphij	if (SP_PARM->_slk->ent[i].ent_text == 0)
198262629Sdelphij	    returnCode(slk_failed(NCURSES_SP_ARG));
199262629Sdelphij	memset(SP_PARM->_slk->ent[i].ent_text, 0, used);
200166124Srafan
201262629Sdelphij	SP_PARM->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used);
202262629Sdelphij	if (SP_PARM->_slk->ent[i].form_text == 0)
203262629Sdelphij	    returnCode(slk_failed(NCURSES_SP_ARG));
204166124Srafan
205262629Sdelphij	if (used > 1) {
206262629Sdelphij	    memset(SP_PARM->_slk->ent[i].form_text, ' ', used - 1);
207262629Sdelphij	}
208262629Sdelphij	SP_PARM->_slk->ent[i].form_text[used - 1] = '\0';
209262629Sdelphij
210262629Sdelphij	SP_PARM->_slk->ent[i].visible = (char) (i < SP_PARM->_slk->maxlab);
21176726Speter    }
21250276Speter
213262629Sdelphij    res = _nc_format_slks(NCURSES_SP_ARGx cols);
21450276Speter
215262629Sdelphij    if ((SP_PARM->_slk->win = stwin) == NULL) {
216262629Sdelphij	returnCode(slk_failed(NCURSES_SP_ARG));
21776726Speter    }
21850276Speter
21976726Speter    /* We now reset the format so that the next newterm has again
22076726Speter     * per default no SLK keys and may call slk_init again to
22176726Speter     * define a new layout. (juergen 03-Mar-1999)
22276726Speter     */
223176187Srafan    _nc_globals.slk_format = 0;
224166124Srafan    returnCode(res);
22550276Speter}
22650276Speter
22750276Speter/*
22850276Speter * Restore the soft labels on the screen.
22950276Speter */
23076726SpeterNCURSES_EXPORT(int)
231262629SdelphijNCURSES_SP_NAME(slk_restore) (NCURSES_SP_DCL0)
23250276Speter{
233262629Sdelphij    T((T_CALLED("slk_restore(%p)"), (void *) SP_PARM));
23450276Speter
235262629Sdelphij    if (0 == SP_PARM)
236262629Sdelphij	returnCode(ERR);
237262629Sdelphij    if (SP_PARM->_slk == NULL)
238262629Sdelphij	returnCode(ERR);
239262629Sdelphij    SP_PARM->_slk->hidden = FALSE;
240262629Sdelphij    SP_PARM->_slk->dirty = TRUE;
24150276Speter
242262629Sdelphij    returnCode(NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG));
24350276Speter}
244262629Sdelphij
245262629Sdelphij#if NCURSES_SP_FUNCS
246262629SdelphijNCURSES_EXPORT(int)
247262629Sdelphijslk_restore(void)
248262629Sdelphij{
249262629Sdelphij    return NCURSES_SP_NAME(slk_restore) (CURRENT_SCREEN);
250262629Sdelphij}
251262629Sdelphij#endif
252