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