Back to index

tetex-bin  3.0
lib_slk.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
00031  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
00032  ****************************************************************************/
00033 
00034 /*
00035  *     lib_slk.c
00036  *     Soft key routines.
00037  */
00038 
00039 #include <curses.priv.h>
00040 
00041 #include <ctype.h>
00042 #include <term.h>           /* num_labels, label_*, plab_norm */
00043 
00044 MODULE_ID("$Id: lib_slk.c,v 1.28 2003/05/24 21:10:28 tom Exp $")
00045 
00046 /*
00047  * We'd like to move these into the screen context structure, but cannot,
00048  * because slk_init() is called before initscr()/newterm().
00049  */
00050 NCURSES_EXPORT_VAR(int)
00051 _nc_slk_format = 0;         /* one more than format specified in slk_init() */
00052 
00053 /*
00054  * Paint the info line for the PC style SLK emulation.
00055  *
00056  */
00057 static void
00058 slk_paint_info(WINDOW *win)
00059 {
00060     if (win && SP->slk_format == 4) {
00061        int i;
00062 
00063        mvwhline(win, 0, 0, 0, getmaxx(win));
00064        wmove(win, 0, 0);
00065 
00066        for (i = 0; i < SP->_slk->maxlab; i++) {
00067            mvwprintw(win, 0, SP->_slk->ent[i].ent_x, "F%d", i + 1);
00068        }
00069     }
00070 }
00071 
00072 /*
00073  * Free any memory related to soft labels, return an error.
00074  */
00075 static int
00076 slk_failed(void)
00077 {
00078     if (SP->_slk) {
00079        FreeIfNeeded(SP->_slk->ent);
00080        free(SP->_slk);
00081        SP->_slk = (SLK *) 0;
00082     }
00083     return ERR;
00084 }
00085 
00086 /*
00087  * Initialize soft labels.
00088  * Called from newterm()
00089  */
00090 NCURSES_EXPORT(int)
00091 _nc_slk_initialize(WINDOW *stwin, int cols)
00092 {
00093     int i, x;
00094     int res = OK;
00095     unsigned max_length;
00096 
00097     T((T_CALLED("_nc_slk_initialize()")));
00098 
00099     if (SP->_slk) {         /* we did this already, so simply return */
00100        returnCode(OK);
00101     } else if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
00102        returnCode(ERR);
00103 
00104     SP->_slk->ent = NULL;
00105 
00106     /*
00107      * If we use colors, vidputs() will suppress video attributes that conflict
00108      * with colors.  In that case, we're still guaranteed that "reverse" would
00109      * work.
00110      */
00111     if ((no_color_video & 1) == 0)
00112        SP->_slk->attr = A_STANDOUT;
00113     else
00114        SP->_slk->attr = A_REVERSE;
00115 
00116     SP->_slk->maxlab = ((num_labels > 0)
00117                      ? num_labels
00118                      : MAX_SKEY(_nc_slk_format));
00119     SP->_slk->maxlen = ((num_labels > 0)
00120                      ? label_width * label_height
00121                      : MAX_SKEY_LEN(_nc_slk_format));
00122     SP->_slk->labcnt = ((SP->_slk->maxlab < MAX_SKEY(_nc_slk_format))
00123                      ? MAX_SKEY(_nc_slk_format)
00124                      : SP->_slk->maxlab);
00125 
00126     if (SP->_slk->maxlen <= 0
00127        || SP->_slk->labcnt <= 0
00128        || (SP->_slk->ent = typeCalloc(slk_ent,
00129                                    (unsigned) SP->_slk->labcnt)) == NULL)
00130        returnCode(slk_failed());
00131 
00132     max_length = SP->_slk->maxlen;
00133     for (i = 0; i < SP->_slk->labcnt; i++) {
00134 
00135        if ((SP->_slk->ent[i].ent_text = (char *)_nc_doalloc(0, max_length + 1)) == 0)
00136            returnCode(slk_failed());
00137        memset(SP->_slk->ent[i].ent_text, 0, max_length + 1);
00138 
00139        if ((SP->_slk->ent[i].form_text = (char *)_nc_doalloc(0, max_length + 1)) == 0)
00140            returnCode(slk_failed());
00141        memset(SP->_slk->ent[i].form_text, 0, max_length + 1);
00142 
00143        memset(SP->_slk->ent[i].form_text, ' ', max_length);
00144        SP->_slk->ent[i].visible = (i < SP->_slk->maxlab);
00145     }
00146     if (_nc_slk_format >= 3) {     /* PC style */
00147        int gap = (cols - 3 * (3 + 4 * max_length)) / 2;
00148 
00149        if (gap < 1)
00150            gap = 1;
00151 
00152        for (i = x = 0; i < SP->_slk->maxlab; i++) {
00153            SP->_slk->ent[i].ent_x = x;
00154            x += max_length;
00155            x += (i == 3 || i == 7) ? gap : 1;
00156        }
00157        slk_paint_info(stwin);
00158     } else {
00159        if (_nc_slk_format == 2) {  /* 4-4 */
00160            int gap = cols - (SP->_slk->maxlab * max_length) - 6;
00161 
00162            if (gap < 1)
00163               gap = 1;
00164            for (i = x = 0; i < SP->_slk->maxlab; i++) {
00165               SP->_slk->ent[i].ent_x = x;
00166               x += max_length;
00167               x += (i == 3) ? gap : 1;
00168            }
00169        } else {
00170            if (_nc_slk_format == 1) {     /* 1 -> 3-2-3 */
00171               int gap = (cols - (SP->_slk->maxlab * max_length) - 5)
00172               / 2;
00173 
00174               if (gap < 1)
00175                   gap = 1;
00176               for (i = x = 0; i < SP->_slk->maxlab; i++) {
00177                   SP->_slk->ent[i].ent_x = x;
00178                   x += max_length;
00179                   x += (i == 2 || i == 4) ? gap : 1;
00180               }
00181            } else
00182               returnCode(slk_failed());
00183        }
00184     }
00185     SP->_slk->dirty = TRUE;
00186     if ((SP->_slk->win = stwin) == NULL) {
00187        returnCode(slk_failed());
00188     }
00189 
00190     /* We now reset the format so that the next newterm has again
00191      * per default no SLK keys and may call slk_init again to
00192      * define a new layout. (juergen 03-Mar-1999)
00193      */
00194     SP->slk_format = _nc_slk_format;
00195     _nc_slk_format = 0;
00196     returnCode(res);
00197 }
00198 
00199 /*
00200  * Restore the soft labels on the screen.
00201  */
00202 NCURSES_EXPORT(int)
00203 slk_restore(void)
00204 {
00205     T((T_CALLED("slk_restore()")));
00206 
00207     if (SP->_slk == NULL)
00208        return (ERR);
00209     SP->_slk->hidden = FALSE;
00210     SP->_slk->dirty = TRUE;
00211     /* we have to repaint info line eventually */
00212     slk_paint_info(SP->_slk->win);
00213 
00214     returnCode(slk_refresh());
00215 }