blob: 47e57e0bc5bdc410704c1a612cab39ae0f3621f1 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
micky3879b9f5e72025-07-08 18:04:53 -04002 * Copyright 2020,2022 Thomas E. Dickey *
3 * Copyright 1998-2010,2011 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05304 * *
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: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
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. *
23 * *
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 *
27 * authorization. *
28 ****************************************************************************/
29
30/****************************************************************************
31 * Authors: *
32 * Gerhard Fuernkranz 1993 (original) *
33 * Zeyd M. Ben-Halim 1992,1995 (sic) *
34 * Eric S. Raymond *
35 * Juergen Pfeifer 1996-on *
36 * Thomas E. Dickey *
37 ****************************************************************************/
38
39/*
40 * lib_slk.c
41 * Soft key routines.
42 */
43
44#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053045#include <ctype.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053046
Steve Kondikae271bc2015-11-15 02:50:53 +010047#ifndef CUR
48#define CUR SP_TERMTYPE
49#endif
50
micky3879b9f5e72025-07-08 18:04:53 -040051MODULE_ID("$Id: lib_slk.c,v 1.50 2022/08/20 18:29:22 tom Exp $")
Steve Kondikae271bc2015-11-15 02:50:53 +010052
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
58#else
59#define NumLabels num_labels
60#define NoColorVideo no_color_video
61#define LabelWidth label_width
62#define LabelHeight label_height
63#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053064
65/*
66 * Free any memory related to soft labels, return an error.
67 */
68static int
Steve Kondikae271bc2015-11-15 02:50:53 +010069slk_failed(NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053070{
Steve Kondikae271bc2015-11-15 02:50:53 +010071 if ((0 != SP_PARM) && SP_PARM->_slk) {
72 FreeIfNeeded(SP_PARM->_slk->ent);
73 free(SP_PARM->_slk);
74 SP_PARM->_slk = (SLK *) 0;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053075 }
76 return ERR;
77}
78
Steve Kondikae271bc2015-11-15 02:50:53 +010079NCURSES_EXPORT(int)
80_nc_format_slks(NCURSES_SP_DCLx int cols)
81{
82 int gap, i, x;
83 int max_length;
84
85 if (!SP_PARM || !SP_PARM->_slk)
86 return ERR;
87
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;
91
92 if (gap < 1)
93 gap = 1;
94
95 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
96 SP_PARM->_slk->ent[i].ent_x = x;
97 x += max_length;
98 x += (i == 3 || i == 7) ? gap : 1;
99 }
100 } else {
101 if (SP_PARM->slk_format == 2) { /* 4-4 */
102 gap = cols - (int) (SP_PARM->_slk->maxlab * max_length) - 6;
103
104 if (gap < 1)
105 gap = 1;
106 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
107 SP_PARM->_slk->ent[i].ent_x = x;
108 x += max_length;
109 x += (i == 3) ? gap : 1;
110 }
111 } else {
112 if (SP_PARM->slk_format == 1) { /* 1 -> 3-2-3 */
113 gap = (cols - (SP_PARM->_slk->maxlab * max_length) - 5)
114 / 2;
115
116 if (gap < 1)
117 gap = 1;
118 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
119 SP_PARM->_slk->ent[i].ent_x = x;
120 x += max_length;
121 x += (i == 2 || i == 4) ? gap : 1;
122 }
123 } else {
124 return slk_failed(NCURSES_SP_ARG);
125 }
126 }
127 }
128 SP_PARM->_slk->dirty = TRUE;
129
130 return OK;
131}
132
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530133/*
134 * Initialize soft labels.
135 * Called from newterm()
136 */
137NCURSES_EXPORT(int)
138_nc_slk_initialize(WINDOW *stwin, int cols)
139{
Steve Kondikae271bc2015-11-15 02:50:53 +0100140 int i;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530141 int res = OK;
Steve Kondikae271bc2015-11-15 02:50:53 +0100142 size_t max_length;
143 SCREEN *sp;
144 int numlab;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530145
146 T((T_CALLED("_nc_slk_initialize()")));
147
Steve Kondikae271bc2015-11-15 02:50:53 +0100148 assert(stwin);
149
150 sp = _nc_screen_of(stwin);
151 if (0 == sp)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530152 returnCode(ERR);
153
Steve Kondikae271bc2015-11-15 02:50:53 +0100154 assert(TerminalOf(SP_PARM));
155
156 numlab = NumLabels;
157
158 if (SP_PARM->_slk) { /* we did this already, so simply return */
159 returnCode(OK);
160 } else if ((SP_PARM->_slk = typeCalloc(SLK, 1)) == 0)
161 returnCode(ERR);
162
163 if (!SP_PARM->slk_format)
164 SP_PARM->slk_format = _nc_globals.slk_format;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530165
166 /*
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
169 * work.
170 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100171 if ((NoColorVideo & 1) == 0)
172 SetAttr(SP_PARM->_slk->attr, A_STANDOUT);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530173 else
Steve Kondikae271bc2015-11-15 02:50:53 +0100174 SetAttr(SP_PARM->_slk->attr, A_REVERSE);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530175
Steve Kondikae271bc2015-11-15 02:50:53 +0100176 SP_PARM->_slk->maxlab = (short) ((numlab > 0)
177 ? numlab
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);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530185
Steve Kondikae271bc2015-11-15 02:50:53 +0100186 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))
micky3879b9f5e72025-07-08 18:04:53 -0400190 == NULL) {
191 free(SP_PARM->_slk->ent);
Steve Kondikae271bc2015-11-15 02:50:53 +0100192 returnCode(slk_failed(NCURSES_SP_ARG));
micky3879b9f5e72025-07-08 18:04:53 -0400193 }
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194
Steve Kondikae271bc2015-11-15 02:50:53 +0100195 max_length = (size_t) SP_PARM->_slk->maxlen;
196 for (i = 0; i < SP_PARM->_slk->labcnt; i++) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530197 size_t used = max_length + 1;
198
Steve Kondikae271bc2015-11-15 02:50:53 +0100199 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);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530203
Steve Kondikae271bc2015-11-15 02:50:53 +0100204 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));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530207
Steve Kondikae271bc2015-11-15 02:50:53 +0100208 if (used > 1) {
209 memset(SP_PARM->_slk->ent[i].form_text, ' ', used - 1);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530210 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100211 SP_PARM->_slk->ent[i].form_text[used - 1] = '\0';
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530212
Steve Kondikae271bc2015-11-15 02:50:53 +0100213 SP_PARM->_slk->ent[i].visible = (char) (i < SP_PARM->_slk->maxlab);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530214 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100215
216 res = _nc_format_slks(NCURSES_SP_ARGx cols);
217
218 if ((SP_PARM->_slk->win = stwin) == NULL) {
219 returnCode(slk_failed(NCURSES_SP_ARG));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530220 }
221
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)
225 */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530226 _nc_globals.slk_format = 0;
227 returnCode(res);
228}
229
230/*
231 * Restore the soft labels on the screen.
232 */
233NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100234NCURSES_SP_NAME(slk_restore) (NCURSES_SP_DCL0)
235{
236 T((T_CALLED("slk_restore(%p)"), (void *) SP_PARM));
237
238 if (0 == SP_PARM)
239 returnCode(ERR);
240 if (SP_PARM->_slk == NULL)
241 returnCode(ERR);
242 SP_PARM->_slk->hidden = FALSE;
243 SP_PARM->_slk->dirty = TRUE;
244
245 returnCode(NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG));
246}
247
248#if NCURSES_SP_FUNCS
249NCURSES_EXPORT(int)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530250slk_restore(void)
251{
Steve Kondikae271bc2015-11-15 02:50:53 +0100252 return NCURSES_SP_NAME(slk_restore) (CURRENT_SCREEN);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530253}
Steve Kondikae271bc2015-11-15 02:50:53 +0100254#endif