]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_slk.c
ncurses 5.7 - patch 20090803
[ncurses.git] / ncurses / base / lib_slk.c
1 /****************************************************************************
2  * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Authors:                                                                *
31  *          Gerhard Fuernkranz                      1993 (original)         *
32  *          Zeyd M. Ben-Halim                       1992,1995 (sic)         *
33  *          Eric S. Raymond                                                 *
34  *          Juergen Pfeifer                         1996-on                 *
35  *          Thomas E. Dickey                                                *
36  ****************************************************************************/
37
38 /*
39  *      lib_slk.c
40  *      Soft key routines.
41  */
42
43 #include <curses.priv.h>
44 #include <ctype.h>
45
46 #ifndef CUR
47 #define CUR SP_TERMTYPE
48 #endif
49
50 MODULE_ID("$Id: lib_slk.c,v 1.40 2009/07/19 00:34:07 tom Exp $")
51
52 /*
53  * Free any memory related to soft labels, return an error.
54  */
55 static int
56 slk_failed(NCURSES_SP_DCL0)
57 {
58     if ((0 != SP_PARM) && SP_PARM->_slk) {
59         FreeIfNeeded(SP_PARM->_slk->ent);
60         free(SP_PARM->_slk);
61         SP_PARM->_slk = (SLK *) 0;
62     }
63     return ERR;
64 }
65
66 NCURSES_EXPORT(int)
67 _nc_format_slks(NCURSES_SP_DCLx int cols)
68 {
69     int gap, i, x;
70     unsigned max_length;
71
72     if (!SP_PARM || !SP_PARM->_slk)
73         return ERR;
74
75     max_length = SP_PARM->_slk->maxlen;
76     if (SP_PARM->slk_format >= 3) {     /* PC style */
77         gap = (cols - 3 * (3 + 4 * max_length)) / 2;
78
79         if (gap < 1)
80             gap = 1;
81
82         for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
83             SP_PARM->_slk->ent[i].ent_x = x;
84             x += max_length;
85             x += (i == 3 || i == 7) ? gap : 1;
86         }
87     } else {
88         if (SP_PARM->slk_format == 2) {         /* 4-4 */
89             gap = cols - (SP_PARM->_slk->maxlab * max_length) - 6;
90
91             if (gap < 1)
92                 gap = 1;
93             for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
94                 SP_PARM->_slk->ent[i].ent_x = x;
95                 x += max_length;
96                 x += (i == 3) ? gap : 1;
97             }
98         } else {
99             if (SP_PARM->slk_format == 1) {     /* 1 -> 3-2-3 */
100                 gap = (cols - (SP_PARM->_slk->maxlab * max_length) - 5)
101                     / 2;
102
103                 if (gap < 1)
104                     gap = 1;
105                 for (i = x = 0; i < SP_PARM->_slk->maxlab; i++) {
106                     SP_PARM->_slk->ent[i].ent_x = x;
107                     x += max_length;
108                     x += (i == 2 || i == 4) ? gap : 1;
109                 }
110             } else
111                 returnCode(slk_failed(NCURSES_SP_ARG));
112         }
113     }
114     SP_PARM->_slk->dirty = TRUE;
115
116     return OK;
117 }
118
119 /*
120  * Initialize soft labels.
121  * Called from newterm()
122  */
123 NCURSES_EXPORT(int)
124 _nc_slk_initialize(WINDOW *stwin, int cols)
125 {
126     int i;
127     int res = OK;
128     unsigned max_length;
129     SCREEN *sp;
130     TERMINAL *term;
131     int numlab;
132
133     T((T_CALLED("_nc_slk_initialize()")));
134
135     assert(stwin);
136
137     sp = _nc_screen_of(stwin);
138     if (0 == sp)
139         returnCode(ERR);
140
141     term = TerminalOf(SP_PARM);
142     assert(term);
143
144     numlab = InfoOf(SP_PARM).numlabels;
145
146     if (SP_PARM->_slk) {        /* we did this already, so simply return */
147         returnCode(OK);
148     } else if ((SP_PARM->_slk = typeCalloc(SLK, 1)) == 0)
149         returnCode(ERR);
150
151     SP_PARM->_slk->hidden = TRUE;
152     SP_PARM->_slk->ent = NULL;
153     if (!SP_PARM->slk_format)
154         SP_PARM->slk_format = _nc_globals.slk_format;
155
156     /*
157      * If we use colors, vidputs() will suppress video attributes that conflict
158      * with colors.  In that case, we're still guaranteed that "reverse" would
159      * work.
160      */
161     if ((InfoOf(SP_PARM).nocolorvideo & 1) == 0)
162         SetAttr(SP_PARM->_slk->attr, A_STANDOUT);
163     else
164         SetAttr(SP_PARM->_slk->attr, A_REVERSE);
165
166     SP_PARM->_slk->maxlab = ((numlab > 0)
167                              ? numlab
168                              : MAX_SKEY(SP_PARM->slk_format));
169     SP_PARM->_slk->maxlen = ((numlab > 0)
170                              ? InfoOf(SP_PARM).labelwidth * InfoOf(SP_PARM).labelheight
171                              : MAX_SKEY_LEN(SP_PARM->slk_format));
172     SP_PARM->_slk->labcnt = ((SP_PARM->_slk->maxlab < MAX_SKEY(SP_PARM->slk_format))
173                              ? MAX_SKEY(SP_PARM->slk_format)
174                              : SP_PARM->_slk->maxlab);
175
176     if (SP_PARM->_slk->maxlen <= 0
177         || SP_PARM->_slk->labcnt <= 0
178         || (SP_PARM->_slk->ent = typeCalloc(slk_ent,
179                                             (unsigned) SP_PARM->_slk->labcnt))
180         == NULL)
181         returnCode(slk_failed(NCURSES_SP_ARG));
182
183     max_length = SP_PARM->_slk->maxlen;
184     for (i = 0; i < SP_PARM->_slk->labcnt; i++) {
185         size_t used = max_length + 1;
186
187         SP_PARM->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used);
188         if (SP_PARM->_slk->ent[i].ent_text == 0)
189             returnCode(slk_failed(NCURSES_SP_ARG));
190         memset(SP_PARM->_slk->ent[i].ent_text, 0, used);
191
192         SP_PARM->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used);
193         if (SP_PARM->_slk->ent[i].form_text == 0)
194             returnCode(slk_failed(NCURSES_SP_ARG));
195         memset(SP_PARM->_slk->ent[i].form_text, 0, used);
196
197         memset(SP_PARM->_slk->ent[i].form_text, ' ', max_length);
198         SP_PARM->_slk->ent[i].visible = (char) (i < SP_PARM->_slk->maxlab);
199     }
200
201     res = _nc_format_slks(NCURSES_SP_ARGx cols);
202
203     if ((SP_PARM->_slk->win = stwin) == NULL) {
204         returnCode(slk_failed(NCURSES_SP_ARG));
205     }
206
207     /* We now reset the format so that the next newterm has again
208      * per default no SLK keys and may call slk_init again to
209      * define a new layout. (juergen 03-Mar-1999)
210      */
211     _nc_globals.slk_format = 0;
212     returnCode(res);
213 }
214
215 /*
216  * Restore the soft labels on the screen.
217  */
218 NCURSES_EXPORT(int)
219 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_DCL0)
220 {
221     T((T_CALLED("slk_restore(%p)"), SP_PARM));
222
223     if (0 == SP_PARM)
224         returnCode(ERR);
225     if (SP_PARM->_slk == NULL)
226         returnCode(ERR);
227     SP_PARM->_slk->hidden = FALSE;
228     SP_PARM->_slk->dirty = TRUE;
229
230     returnCode(NCURSES_SP_NAME(slk_refresh) (NCURSES_SP_ARG));
231 }
232
233 #if NCURSES_SP_FUNCS
234 NCURSES_EXPORT(int)
235 slk_restore(void)
236 {
237     return NCURSES_SP_NAME(slk_restore) (CURRENT_SCREEN);
238 }
239 #endif