1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
31 * Gerhard Fuernkranz 1993 (original) *
32 * Zeyd M. Ben-Halim 1992,1995 (sic) *
34 * Juergen Pfeifer 1996-on *
36 ****************************************************************************/
43 #include <curses.priv.h>
47 #define CUR SP_TERMTYPE
50 MODULE_ID("$Id: lib_slk.c,v 1.39 2009/07/04 18:37:57 tom Exp $")
53 * Free any memory related to soft labels, return an error.
56 slk_failed(NCURSES_SP_DCL0)
58 if ((0 != SP_PARM) && SP_PARM->_slk) {
59 FreeIfNeeded(SP_PARM->_slk->ent);
61 SP_PARM->_slk = (SLK *) 0;
67 _nc_format_slks(NCURSES_SP_DCLx int cols)
70 unsigned max_length = SP_PARM->_slk->maxlen;
75 if (SP_PARM->slk_format >= 3) { /* PC style */
76 gap = (cols - 3 * (3 + 4 * max_length)) / 2;
81 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
82 SP_PARM->_slk->ent[i].ent_x = x;
84 x += (i == 3 || i == 7) ? gap : 1;
87 if (SP_PARM->slk_format == 2) { /* 4-4 */
88 gap = cols - (SP_PARM->_slk->maxlab * max_length) - 6;
92 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
93 SP_PARM->_slk->ent[i].ent_x = x;
95 x += (i == 3) ? gap : 1;
98 if (SP_PARM->slk_format == 1) { /* 1 -> 3-2-3 */
99 gap = (cols - (SP_PARM->_slk->maxlab * max_length) - 5)
104 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
105 SP_PARM->_slk->ent[i].ent_x = x;
107 x += (i == 2 || i == 4) ? gap : 1;
110 returnCode(slk_failed(NCURSES_SP_ARG));
113 SP_PARM->_slk->dirty = TRUE;
119 * Initialize soft labels.
120 * Called from newterm()
123 _nc_slk_initialize(WINDOW *stwin, int cols)
132 T((T_CALLED("_nc_slk_initialize()")));
136 sp = _nc_screen_of(stwin);
140 term = TerminalOf(SP_PARM);
143 numlab = InfoOf(SP_PARM).numlabels;
145 if (SP_PARM->_slk) { /* we did this already, so simply return */
147 } else if ((SP_PARM->_slk = typeCalloc(SLK, 1)) == 0)
150 SP_PARM->_slk->hidden = TRUE;
151 SP_PARM->_slk->ent = NULL;
152 if (!SP_PARM->slk_format)
153 SP_PARM->slk_format = _nc_globals.slk_format;
156 * If we use colors, vidputs() will suppress video attributes that conflict
157 * with colors. In that case, we're still guaranteed that "reverse" would
160 if ((InfoOf(SP_PARM).nocolorvideo & 1) == 0)
161 SetAttr(SP_PARM->_slk->attr, A_STANDOUT);
163 SetAttr(SP_PARM->_slk->attr, A_REVERSE);
165 SP_PARM->_slk->maxlab = ((numlab > 0)
167 : MAX_SKEY(SP_PARM->slk_format));
168 SP_PARM->_slk->maxlen = ((numlab > 0)
169 ? InfoOf(SP_PARM).labelwidth * InfoOf(SP_PARM).labelheight
170 : MAX_SKEY_LEN(SP_PARM->slk_format));
171 SP_PARM->_slk->labcnt = ((SP_PARM->_slk->maxlab < MAX_SKEY(SP_PARM->slk_format))
172 ? MAX_SKEY(SP_PARM->slk_format)
173 : SP_PARM->_slk->maxlab);
175 if (SP_PARM->_slk->maxlen <= 0
176 || SP_PARM->_slk->labcnt <= 0
177 || (SP_PARM->_slk->ent = typeCalloc(slk_ent,
178 (unsigned) SP_PARM->_slk->labcnt))
180 returnCode(slk_failed(NCURSES_SP_ARG));
182 max_length = SP_PARM->_slk->maxlen;
183 for (i = 0; i < SP_PARM->_slk->labcnt; i++) {
184 size_t used = max_length + 1;
186 SP_PARM->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used);
187 if (SP_PARM->_slk->ent[i].ent_text == 0)
188 returnCode(slk_failed(NCURSES_SP_ARG));
189 memset(SP_PARM->_slk->ent[i].ent_text, 0, used);
191 SP_PARM->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used);
192 if (SP_PARM->_slk->ent[i].form_text == 0)
193 returnCode(slk_failed(NCURSES_SP_ARG));
194 memset(SP_PARM->_slk->ent[i].form_text, 0, used);
196 memset(SP_PARM->_slk->ent[i].form_text, ' ', max_length);
197 SP_PARM->_slk->ent[i].visible = (char) (i < SP_PARM->_slk->maxlab);
200 res = _nc_format_slks(NCURSES_SP_ARGx cols);
202 if ((SP_PARM->_slk->win = stwin) == NULL) {
203 returnCode(slk_failed(NCURSES_SP_ARG));
206 /* We now reset the format so that the next newterm has again
207 * per default no SLK keys and may call slk_init again to
208 * define a new layout. (juergen 03-Mar-1999)
210 _nc_globals.slk_format = 0;
215 * Restore the soft labels on the screen.
218 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_DCL0)
220 T((T_CALLED("slk_restore(%p)"), SP_PARM));
224 if (SP_PARM->_slk == NULL)
226 SP_PARM->_slk->hidden = FALSE;
227 SP_PARM->_slk->dirty = TRUE;
229 returnCode(NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG));
236 return NCURSES_SP_NAME(slk_restore) (CURRENT_SCREEN);