1 /****************************************************************************
2 * Copyright (c) 1998-2005,2008 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>
46 #include <term.h> /* num_labels, label_*, plab_norm */
48 MODULE_ID("$Id: lib_slk.c,v 1.31 2008/01/12 20:21:00 tom Exp $")
51 * We'd like to move these into the screen context structure, but cannot,
52 * because slk_init() is called before initscr()/newterm().
54 NCURSES_EXPORT_VAR(int)
55 _nc_slk_format = 0; /* one more than format specified in slk_init() */
58 * Paint the info line for the PC style SLK emulation.
61 slk_paint_info(WINDOW *win)
63 if (win && SP->slk_format == 4) {
66 mvwhline(win, 0, 0, 0, getmaxx(win));
69 for (i = 0; i < SP->_slk->maxlab; i++) {
70 mvwprintw(win, 0, SP->_slk->ent[i].ent_x, "F%d", i + 1);
76 * Free any memory related to soft labels, return an error.
82 FreeIfNeeded(SP->_slk->ent);
90 * Initialize soft labels.
91 * Called from newterm()
94 _nc_slk_initialize(WINDOW *stwin, int cols)
100 T((T_CALLED("_nc_slk_initialize()")));
102 if (SP->_slk) { /* we did this already, so simply return */
104 } else if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
107 SP->_slk->ent = NULL;
110 * If we use colors, vidputs() will suppress video attributes that conflict
111 * with colors. In that case, we're still guaranteed that "reverse" would
114 if ((no_color_video & 1) == 0)
115 SetAttr(SP->_slk->attr, A_STANDOUT);
117 SetAttr(SP->_slk->attr, A_REVERSE);
119 SP->_slk->maxlab = ((num_labels > 0)
121 : MAX_SKEY(_nc_globals.slk_format));
122 SP->_slk->maxlen = ((num_labels > 0)
123 ? label_width * label_height
124 : MAX_SKEY_LEN(_nc_globals.slk_format));
125 SP->_slk->labcnt = ((SP->_slk->maxlab < MAX_SKEY(_nc_globals.slk_format))
126 ? MAX_SKEY(_nc_globals.slk_format)
129 if (SP->_slk->maxlen <= 0
130 || SP->_slk->labcnt <= 0
131 || (SP->_slk->ent = typeCalloc(slk_ent,
132 (unsigned) SP->_slk->labcnt)) == NULL)
133 returnCode(slk_failed());
135 max_length = SP->_slk->maxlen;
136 for (i = 0; i < SP->_slk->labcnt; i++) {
137 size_t used = max_length + 1;
139 if ((SP->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used)) == 0)
140 returnCode(slk_failed());
141 memset(SP->_slk->ent[i].ent_text, 0, used);
143 if ((SP->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used)) == 0)
144 returnCode(slk_failed());
145 memset(SP->_slk->ent[i].form_text, 0, used);
147 memset(SP->_slk->ent[i].form_text, ' ', max_length);
148 SP->_slk->ent[i].visible = (i < SP->_slk->maxlab);
150 if (_nc_globals.slk_format >= 3) { /* PC style */
151 int gap = (cols - 3 * (3 + 4 * max_length)) / 2;
156 for (i = x = 0; i < SP->_slk->maxlab; i++) {
157 SP->_slk->ent[i].ent_x = x;
159 x += (i == 3 || i == 7) ? gap : 1;
161 slk_paint_info(stwin);
163 if (_nc_globals.slk_format == 2) { /* 4-4 */
164 int gap = cols - (SP->_slk->maxlab * max_length) - 6;
168 for (i = x = 0; i < SP->_slk->maxlab; i++) {
169 SP->_slk->ent[i].ent_x = x;
171 x += (i == 3) ? gap : 1;
174 if (_nc_globals.slk_format == 1) { /* 1 -> 3-2-3 */
175 int gap = (cols - (SP->_slk->maxlab * max_length) - 5)
180 for (i = x = 0; i < SP->_slk->maxlab; i++) {
181 SP->_slk->ent[i].ent_x = x;
183 x += (i == 2 || i == 4) ? gap : 1;
186 returnCode(slk_failed());
189 SP->_slk->dirty = TRUE;
190 if ((SP->_slk->win = stwin) == NULL) {
191 returnCode(slk_failed());
194 /* We now reset the format so that the next newterm has again
195 * per default no SLK keys and may call slk_init again to
196 * define a new layout. (juergen 03-Mar-1999)
198 SP->slk_format = _nc_globals.slk_format;
199 _nc_globals.slk_format = 0;
204 * Restore the soft labels on the screen.
209 T((T_CALLED("slk_restore()")));
211 if (SP->_slk == NULL)
213 SP->_slk->hidden = FALSE;
214 SP->_slk->dirty = TRUE;
215 /* we have to repaint info line eventually */
216 slk_paint_info(SP->_slk->win);
218 returnCode(slk_refresh());