1/****************************************************************************
2 * Copyright (c) 1998-2005,2008 Free Software Foundation, Inc.              *
3 *                                                                          *
4 * Permission is hereby granted, free of charge, to any person obtaining a  *
5 * copy of this software and associated documentation files (the            *
6 * "Software"), to deal in the Software without restriction, including      *
7 * without limitation the rights to use, copy, modify, merge, publish,      *
8 * distribute, distribute with modifications, sublicense, and/or sell       *
9 * copies of the Software, and to permit persons to whom the Software is    *
10 * furnished to do so, subject to the following conditions:                 *
11 *                                                                          *
12 * The above copyright notice and this permission notice shall be included  *
13 * in all copies or substantial portions of the Software.                   *
14 *                                                                          *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22 *                                                                          *
23 * Except as contained in this notice, the name(s) of the above copyright   *
24 * holders shall not be used in advertising or otherwise to promote the     *
25 * sale, use or other dealings in this Software without prior written       *
26 * authorization.                                                           *
27 ****************************************************************************/
28
29/****************************************************************************
30 *  Authors:                                                                *
31 *          Gerhard Fuernkranz                      1993 (original)         *
32 *          Zeyd M. Ben-Halim                       1992,1995 (sic)         *
33 *          Eric S. Raymond                                                 *
34 *          Juergen Pfeifer                         1996-on                 *
35 *          Thomas E. Dickey                                                *
36 ****************************************************************************/
37
38/*
39 *	lib_slk.c
40 *	Soft key routines.
41 */
42
43#include <curses.priv.h>
44
45#include <ctype.h>
46#include <term.h>		/* num_labels, label_*, plab_norm */
47
48MODULE_ID("$Id: lib_slk.c,v 1.35 2008/09/27 14:07:33 juergen Exp $")
49
50/*
51 * Free any memory related to soft labels, return an error.
52 */
53static int
54slk_failed(void)
55{
56    if (SP->_slk) {
57	FreeIfNeeded(SP->_slk->ent);
58	free(SP->_slk);
59	SP->_slk = (SLK *) 0;
60    }
61    return ERR;
62}
63
64/*
65 * Initialize soft labels.
66 * Called from newterm()
67 */
68NCURSES_EXPORT(int)
69_nc_slk_initialize(WINDOW *stwin, int cols)
70{
71    int i, x;
72    int res = OK;
73    unsigned max_length;
74
75    T((T_CALLED("_nc_slk_initialize()")));
76
77    if (SP->_slk) {		/* we did this already, so simply return */
78	returnCode(OK);
79    } else if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
80	returnCode(ERR);
81
82    SP->_slk->ent = NULL;
83
84    /*
85     * If we use colors, vidputs() will suppress video attributes that conflict
86     * with colors.  In that case, we're still guaranteed that "reverse" would
87     * work.
88     */
89    if ((no_color_video & 1) == 0)
90	SetAttr(SP->_slk->attr, A_STANDOUT);
91    else
92	SetAttr(SP->_slk->attr, A_REVERSE);
93
94    SP->_slk->maxlab = ((num_labels > 0)
95			? num_labels
96			: MAX_SKEY(_nc_globals.slk_format));
97    SP->_slk->maxlen = ((num_labels > 0)
98			? label_width * label_height
99			: MAX_SKEY_LEN(_nc_globals.slk_format));
100    SP->_slk->labcnt = ((SP->_slk->maxlab < MAX_SKEY(_nc_globals.slk_format))
101			? MAX_SKEY(_nc_globals.slk_format)
102			: SP->_slk->maxlab);
103
104    if (SP->_slk->maxlen <= 0
105	|| SP->_slk->labcnt <= 0
106	|| (SP->_slk->ent = typeCalloc(slk_ent,
107				       (unsigned) SP->_slk->labcnt)) == NULL)
108	returnCode(slk_failed());
109
110    max_length = SP->_slk->maxlen;
111    for (i = 0; i < SP->_slk->labcnt; i++) {
112	size_t used = max_length + 1;
113
114	if ((SP->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used)) == 0)
115	    returnCode(slk_failed());
116	memset(SP->_slk->ent[i].ent_text, 0, used);
117
118	if ((SP->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used)) == 0)
119	    returnCode(slk_failed());
120	memset(SP->_slk->ent[i].form_text, 0, used);
121
122	memset(SP->_slk->ent[i].form_text, ' ', max_length);
123	SP->_slk->ent[i].visible = (char) (i < SP->_slk->maxlab);
124    }
125    if (_nc_globals.slk_format >= 3) {	/* PC style */
126	int gap = (cols - 3 * (3 + 4 * max_length)) / 2;
127
128	if (gap < 1)
129	    gap = 1;
130
131	for (i = x = 0; i < SP->_slk->maxlab; i++) {
132	    SP->_slk->ent[i].ent_x = x;
133	    x += max_length;
134	    x += (i == 3 || i == 7) ? gap : 1;
135	}
136    } else {
137	if (_nc_globals.slk_format == 2) {	/* 4-4 */
138	    int gap = cols - (SP->_slk->maxlab * max_length) - 6;
139
140	    if (gap < 1)
141		gap = 1;
142	    for (i = x = 0; i < SP->_slk->maxlab; i++) {
143		SP->_slk->ent[i].ent_x = x;
144		x += max_length;
145		x += (i == 3) ? gap : 1;
146	    }
147	} else {
148	    if (_nc_globals.slk_format == 1) {	/* 1 -> 3-2-3 */
149		int gap = (cols - (SP->_slk->maxlab * max_length) - 5)
150		/ 2;
151
152		if (gap < 1)
153		    gap = 1;
154		for (i = x = 0; i < SP->_slk->maxlab; i++) {
155		    SP->_slk->ent[i].ent_x = x;
156		    x += max_length;
157		    x += (i == 2 || i == 4) ? gap : 1;
158		}
159	    } else
160		returnCode(slk_failed());
161	}
162    }
163    SP->_slk->dirty = TRUE;
164    if ((SP->_slk->win = stwin) == NULL) {
165	returnCode(slk_failed());
166    }
167
168    /* We now reset the format so that the next newterm has again
169     * per default no SLK keys and may call slk_init again to
170     * define a new layout. (juergen 03-Mar-1999)
171     */
172    SP->slk_format = _nc_globals.slk_format;
173    _nc_globals.slk_format = 0;
174    returnCode(res);
175}
176
177/*
178 * Restore the soft labels on the screen.
179 */
180NCURSES_EXPORT(int)
181slk_restore(void)
182{
183    T((T_CALLED("slk_restore()")));
184
185    if (SP->_slk == NULL)
186	return (ERR);
187    SP->_slk->hidden = FALSE;
188    SP->_slk->dirty = TRUE;
189
190    returnCode(slk_refresh());
191}
192