]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_slk.c
ncurses 5.7 - patch 20090627
[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
45 #include <ctype.h>
46 #include <term.h>               /* num_labels, label_*, plab_norm */
47
48 #ifndef CUR
49 #define CUR SP_TERMTYPE 
50 #endif
51
52 MODULE_ID("$Id: lib_slk.c,v 1.38 2009/05/10 00:48:29 tom Exp $")
53
54 /*
55  * Free any memory related to soft labels, return an error.
56  */
57 static int
58 slk_failed(void)
59 {
60     if (SP->_slk) {
61         FreeIfNeeded(SP->_slk->ent);
62         free(SP->_slk);
63         SP->_slk = (SLK *) 0;
64     }
65     return ERR;
66 }
67
68 /*
69  * Initialize soft labels.
70  * Called from newterm()
71  */
72 NCURSES_EXPORT(int)
73 _nc_slk_initialize(WINDOW *stwin, int cols)
74 {
75 #if NCURSES_SP_FUNCS
76     SCREEN *sp = CURRENT_SCREEN;
77 #endif
78     int i, x;
79     int res = OK;
80     unsigned max_length;
81
82     T((T_CALLED("_nc_slk_initialize()")));
83
84     if (SP->_slk) {             /* we did this already, so simply return */
85         returnCode(OK);
86     } else if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
87         returnCode(ERR);
88
89     SP->_slk->ent = NULL;
90
91     /*
92      * If we use colors, vidputs() will suppress video attributes that conflict
93      * with colors.  In that case, we're still guaranteed that "reverse" would
94      * work.
95      */
96     if ((no_color_video & 1) == 0)
97         SetAttr(SP->_slk->attr, A_STANDOUT);
98     else
99         SetAttr(SP->_slk->attr, A_REVERSE);
100
101     SP->_slk->maxlab = ((num_labels > 0)
102                         ? num_labels
103                         : MAX_SKEY(_nc_globals.slk_format));
104     SP->_slk->maxlen = ((num_labels > 0)
105                         ? label_width * label_height
106                         : MAX_SKEY_LEN(_nc_globals.slk_format));
107     SP->_slk->labcnt = ((SP->_slk->maxlab < MAX_SKEY(_nc_globals.slk_format))
108                         ? MAX_SKEY(_nc_globals.slk_format)
109                         : SP->_slk->maxlab);
110
111     if (SP->_slk->maxlen <= 0
112         || SP->_slk->labcnt <= 0
113         || (SP->_slk->ent = typeCalloc(slk_ent,
114                                        (unsigned) SP->_slk->labcnt)) == NULL)
115         returnCode(slk_failed());
116
117     max_length = SP->_slk->maxlen;
118     for (i = 0; i < SP->_slk->labcnt; i++) {
119         size_t used = max_length + 1;
120
121         if ((SP->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used)) == 0)
122             returnCode(slk_failed());
123         memset(SP->_slk->ent[i].ent_text, 0, used);
124
125         if ((SP->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used)) == 0)
126             returnCode(slk_failed());
127         memset(SP->_slk->ent[i].form_text, 0, used);
128
129         memset(SP->_slk->ent[i].form_text, ' ', max_length);
130         SP->_slk->ent[i].visible = (char) (i < SP->_slk->maxlab);
131     }
132     if (_nc_globals.slk_format >= 3) {  /* PC style */
133         int gap = (cols - 3 * (3 + 4 * max_length)) / 2;
134
135         if (gap < 1)
136             gap = 1;
137
138         for (i = x = 0; i < SP->_slk->maxlab; i++) {
139             SP->_slk->ent[i].ent_x = x;
140             x += max_length;
141             x += (i == 3 || i == 7) ? gap : 1;
142         }
143     } else {
144         if (_nc_globals.slk_format == 2) {      /* 4-4 */
145             int gap = cols - (SP->_slk->maxlab * max_length) - 6;
146
147             if (gap < 1)
148                 gap = 1;
149             for (i = x = 0; i < SP->_slk->maxlab; i++) {
150                 SP->_slk->ent[i].ent_x = x;
151                 x += max_length;
152                 x += (i == 3) ? gap : 1;
153             }
154         } else {
155             if (_nc_globals.slk_format == 1) {  /* 1 -> 3-2-3 */
156                 int gap = (cols - (SP->_slk->maxlab * max_length) - 5)
157                 / 2;
158
159                 if (gap < 1)
160                     gap = 1;
161                 for (i = x = 0; i < SP->_slk->maxlab; i++) {
162                     SP->_slk->ent[i].ent_x = x;
163                     x += max_length;
164                     x += (i == 2 || i == 4) ? gap : 1;
165                 }
166             } else
167                 returnCode(slk_failed());
168         }
169     }
170     SP->_slk->dirty = TRUE;
171     if ((SP->_slk->win = stwin) == NULL) {
172         returnCode(slk_failed());
173     }
174
175     /* We now reset the format so that the next newterm has again
176      * per default no SLK keys and may call slk_init again to
177      * define a new layout. (juergen 03-Mar-1999)
178      */
179     SP->slk_format = _nc_globals.slk_format;
180     _nc_globals.slk_format = 0;
181     returnCode(res);
182 }
183
184 /*
185  * Restore the soft labels on the screen.
186  */
187 NCURSES_EXPORT(int)
188 NCURSES_SP_NAME(slk_restore) (NCURSES_SP_DCL0)
189 {
190     T((T_CALLED("slk_restore()")));
191
192     if (SP_PARM->_slk == NULL)
193         return (ERR);
194     SP_PARM->_slk->hidden = FALSE;
195     SP_PARM->_slk->dirty = TRUE;
196
197     returnCode(slk_refresh());
198 }
199
200 #if NCURSES_SP_FUNCS
201 NCURSES_EXPORT(int)
202 slk_restore(void)
203 {
204     return NCURSES_SP_NAME(slk_restore) (CURRENT_SCREEN);
205 }
206 #endif