]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/base/lib_slk.c
ncurses 5.6 - patch 20080823
[ncurses.git] / ncurses / base / lib_slk.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2008 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 MODULE_ID("$Id: lib_slk.c,v 1.34 2008/08/16 19:20:04 tom Exp $")
49
50 /*
51  * We'd like to move these into the screen context structure, but cannot,
52  * because slk_init() is called before initscr()/newterm().
53  */
54 NCURSES_EXPORT_VAR(int)
55 _nc_slk_format = 0;             /* one more than format specified in slk_init() */
56
57 /*
58  * Paint the info line for the PC style SLK emulation.
59  */
60 static void
61 slk_paint_info(WINDOW *win)
62 {
63     SCREEN *sp = _nc_screen_of(win);
64
65     if (win && sp && (sp->slk_format == 4)) {
66         int i;
67
68         mvwhline(win, 0, 0, 0, getmaxx(win));
69         wmove(win, 0, 0);
70
71         for (i = 0; i < sp->_slk->maxlab; i++) {
72             mvwprintw(win, 0, sp->_slk->ent[i].ent_x, "F%d", i + 1);
73         }
74     }
75 }
76
77 /*
78  * Free any memory related to soft labels, return an error.
79  */
80 static int
81 slk_failed(void)
82 {
83     if (SP->_slk) {
84         FreeIfNeeded(SP->_slk->ent);
85         free(SP->_slk);
86         SP->_slk = (SLK *) 0;
87     }
88     return ERR;
89 }
90
91 /*
92  * Initialize soft labels.
93  * Called from newterm()
94  */
95 NCURSES_EXPORT(int)
96 _nc_slk_initialize(WINDOW *stwin, int cols)
97 {
98     int i, x;
99     int res = OK;
100     unsigned max_length;
101
102     T((T_CALLED("_nc_slk_initialize()")));
103
104     if (SP->_slk) {             /* we did this already, so simply return */
105         returnCode(OK);
106     } else if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
107         returnCode(ERR);
108
109     SP->_slk->ent = NULL;
110
111     /*
112      * If we use colors, vidputs() will suppress video attributes that conflict
113      * with colors.  In that case, we're still guaranteed that "reverse" would
114      * work.
115      */
116     if ((no_color_video & 1) == 0)
117         SetAttr(SP->_slk->attr, A_STANDOUT);
118     else
119         SetAttr(SP->_slk->attr, A_REVERSE);
120
121     SP->_slk->maxlab = ((num_labels > 0)
122                         ? num_labels
123                         : MAX_SKEY(_nc_globals.slk_format));
124     SP->_slk->maxlen = ((num_labels > 0)
125                         ? label_width * label_height
126                         : MAX_SKEY_LEN(_nc_globals.slk_format));
127     SP->_slk->labcnt = ((SP->_slk->maxlab < MAX_SKEY(_nc_globals.slk_format))
128                         ? MAX_SKEY(_nc_globals.slk_format)
129                         : SP->_slk->maxlab);
130
131     if (SP->_slk->maxlen <= 0
132         || SP->_slk->labcnt <= 0
133         || (SP->_slk->ent = typeCalloc(slk_ent,
134                                        (unsigned) SP->_slk->labcnt)) == NULL)
135         returnCode(slk_failed());
136
137     max_length = SP->_slk->maxlen;
138     for (i = 0; i < SP->_slk->labcnt; i++) {
139         size_t used = max_length + 1;
140
141         if ((SP->_slk->ent[i].ent_text = (char *) _nc_doalloc(0, used)) == 0)
142             returnCode(slk_failed());
143         memset(SP->_slk->ent[i].ent_text, 0, used);
144
145         if ((SP->_slk->ent[i].form_text = (char *) _nc_doalloc(0, used)) == 0)
146             returnCode(slk_failed());
147         memset(SP->_slk->ent[i].form_text, 0, used);
148
149         memset(SP->_slk->ent[i].form_text, ' ', max_length);
150         SP->_slk->ent[i].visible = (char) (i < SP->_slk->maxlab);
151     }
152     if (_nc_globals.slk_format >= 3) {  /* PC style */
153         int gap = (cols - 3 * (3 + 4 * max_length)) / 2;
154
155         if (gap < 1)
156             gap = 1;
157
158         for (i = x = 0; i < SP->_slk->maxlab; i++) {
159             SP->_slk->ent[i].ent_x = x;
160             x += max_length;
161             x += (i == 3 || i == 7) ? gap : 1;
162         }
163         slk_paint_info(stwin);
164     } else {
165         if (_nc_globals.slk_format == 2) {      /* 4-4 */
166             int gap = cols - (SP->_slk->maxlab * max_length) - 6;
167
168             if (gap < 1)
169                 gap = 1;
170             for (i = x = 0; i < SP->_slk->maxlab; i++) {
171                 SP->_slk->ent[i].ent_x = x;
172                 x += max_length;
173                 x += (i == 3) ? gap : 1;
174             }
175         } else {
176             if (_nc_globals.slk_format == 1) {  /* 1 -> 3-2-3 */
177                 int gap = (cols - (SP->_slk->maxlab * max_length) - 5)
178                 / 2;
179
180                 if (gap < 1)
181                     gap = 1;
182                 for (i = x = 0; i < SP->_slk->maxlab; i++) {
183                     SP->_slk->ent[i].ent_x = x;
184                     x += max_length;
185                     x += (i == 2 || i == 4) ? gap : 1;
186                 }
187             } else
188                 returnCode(slk_failed());
189         }
190     }
191     SP->_slk->dirty = TRUE;
192     if ((SP->_slk->win = stwin) == NULL) {
193         returnCode(slk_failed());
194     }
195
196     /* We now reset the format so that the next newterm has again
197      * per default no SLK keys and may call slk_init again to
198      * define a new layout. (juergen 03-Mar-1999)
199      */
200     SP->slk_format = _nc_globals.slk_format;
201     _nc_globals.slk_format = 0;
202     returnCode(res);
203 }
204
205 /*
206  * Restore the soft labels on the screen.
207  */
208 NCURSES_EXPORT(int)
209 slk_restore(void)
210 {
211     T((T_CALLED("slk_restore()")));
212
213     if (SP->_slk == NULL)
214         return (ERR);
215     SP->_slk->hidden = FALSE;
216     SP->_slk->dirty = TRUE;
217     /* we have to repaint info line eventually */
218     slk_paint_info(SP->_slk->win);
219
220     returnCode(slk_refresh());
221 }