1 /****************************************************************************
2 * Copyright 2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2010,2011 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
32 * Gerhard Fuernkranz 1993 (original) *
33 * Zeyd M. Ben-Halim 1992,1995 (sic) *
35 * Juergen Pfeifer 1996-on *
37 ****************************************************************************/
44 #include <curses.priv.h>
48 #define CUR SP_TERMTYPE
51 MODULE_ID("$Id: lib_slk.c,v 1.50 2022/08/20 18:29:22 tom Exp $")
53 #ifdef USE_TERM_DRIVER
54 #define NumLabels InfoOf(SP_PARM).numlabels
55 #define NoColorVideo InfoOf(SP_PARM).nocolorvideo
56 #define LabelWidth InfoOf(SP_PARM).labelwidth
57 #define LabelHeight InfoOf(SP_PARM).labelheight
59 #define NumLabels num_labels
60 #define NoColorVideo no_color_video
61 #define LabelWidth label_width
62 #define LabelHeight label_height
66 * Free any memory related to soft labels, return an error.
69 slk_failed(NCURSES_SP_DCL0)
71 if ((0 != SP_PARM) && SP_PARM->_slk) {
72 FreeIfNeeded(SP_PARM->_slk->ent);
74 SP_PARM->_slk = (SLK *) 0;
80 _nc_format_slks(NCURSES_SP_DCLx int cols)
85 if (!SP_PARM || !SP_PARM->_slk)
88 max_length = SP_PARM->_slk->maxlen;
89 if (SP_PARM->slk_format >= 3) { /* PC style */
90 gap = (cols - 3 * (3 + 4 * max_length)) / 2;
95 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
96 SP_PARM->_slk->ent[i].ent_x = x;
98 x += (i == 3 || i == 7) ? gap : 1;
101 if (SP_PARM->slk_format == 2) { /* 4-4 */
102 gap = cols - (int) (SP_PARM->_slk->maxlab * max_length) - 6;
106 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
107 SP_PARM->_slk->ent[i].ent_x = x;
109 x += (i == 3) ? gap : 1;
112 if (SP_PARM->slk_format == 1) { /* 1 -> 3-2-3 */
113 gap = (cols - (SP_PARM->_slk->maxlab * max_length) - 5)
118 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
119 SP_PARM->_slk->ent[i].ent_x = x;
121 x += (i == 2 || i == 4) ? gap : 1;
124 return slk_failed(NCURSES_SP_ARG);
128 SP_PARM->_slk->dirty = TRUE;
134 * Initialize soft labels.
135 * Called from newterm()
138 _nc_slk_initialize(WINDOW *stwin, int cols)
146 T((T_CALLED("_nc_slk_initialize()")));
150 sp = _nc_screen_of(stwin);
154 assert(TerminalOf(SP_PARM));
158 if (SP_PARM->_slk) { /* we did this already, so simply return */
160 } else if ((SP_PARM->_slk = typeCalloc(SLK, 1)) == 0)
163 if (!SP_PARM->slk_format)
164 SP_PARM->slk_format = _nc_globals.slk_format;
167 * If we use colors, vidputs() will suppress video attributes that conflict
168 * with colors. In that case, we're still guaranteed that "reverse" would
171 if ((NoColorVideo & 1) == 0)
172 SetAttr(SP_PARM->_slk->attr, A_STANDOUT);
174 SetAttr(SP_PARM->_slk->attr, A_REVERSE);
176 SP_PARM->_slk->maxlab = (short) ((numlab > 0)
178 : MAX_SKEY(SP_PARM->slk_format));
179 SP_PARM->_slk->maxlen = (short) ((numlab > 0)
180 ? LabelWidth * LabelHeight
181 : MAX_SKEY_LEN(SP_PARM->slk_format));
182 SP_PARM->_slk->labcnt = (short) ((SP_PARM->_slk->maxlab < MAX_SKEY(SP_PARM->slk_format))
183 ? MAX_SKEY(SP_PARM->slk_format)
184 : SP_PARM->_slk->maxlab);
186 if (SP_PARM->_slk->maxlen <= 0
187 || SP_PARM->_slk->labcnt <= 0
188 || (SP_PARM->_slk->ent = typeCalloc(slk_ent,
189 (size_t) SP_PARM->_slk->labcnt))
191 free(SP_PARM->_slk->ent);
192 returnCode(slk_failed(NCURSES_SP_ARG));
195 max_length = (size_t) SP_PARM->_slk->maxlen;
196 for (i = 0; i < SP_PARM->_slk->labcnt; i++) {
197 size_t used = max_length + 1;
199 SP_PARM->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used);
200 if (SP_PARM->_slk->ent[i].ent_text == 0)
201 returnCode(slk_failed(NCURSES_SP_ARG));
202 memset(SP_PARM->_slk->ent[i].ent_text, 0, used);
204 SP_PARM->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used);
205 if (SP_PARM->_slk->ent[i].form_text == 0)
206 returnCode(slk_failed(NCURSES_SP_ARG));
209 memset(SP_PARM->_slk->ent[i].form_text, ' ', used - 1);
211 SP_PARM->_slk->ent[i].form_text[used - 1] = '\0';
213 SP_PARM->_slk->ent[i].visible = (char) (i < SP_PARM->_slk->maxlab);
216 res = _nc_format_slks(NCURSES_SP_ARGx cols);
218 if ((SP_PARM->_slk->win = stwin) == NULL) {
219 returnCode(slk_failed(NCURSES_SP_ARG));
222 /* We now reset the format so that the next newterm has again
223 * per default no SLK keys and may call slk_init again to
224 * define a new layout. (juergen 03-Mar-1999)
226 _nc_globals.slk_format = 0;
231 * Restore the soft labels on the screen.
234 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_DCL0)
236 T((T_CALLED("slk_restore(%p)"), (void *) SP_PARM));
240 if (SP_PARM->_slk == NULL)
242 SP_PARM->_slk->hidden = FALSE;
243 SP_PARM->_slk->dirty = TRUE;
245 returnCode(NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG));
252 return NCURSES_SP_NAME(slk_restore) (CURRENT_SCREEN);