lib_slk.c revision 76726
11541Srgrimes/****************************************************************************
21541Srgrimes * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
31541Srgrimes *                                                                          *
41541Srgrimes * Permission is hereby granted, free of charge, to any person obtaining a  *
51541Srgrimes * copy of this software and associated documentation files (the            *
61541Srgrimes * "Software"), to deal in the Software without restriction, including      *
71541Srgrimes * without limitation the rights to use, copy, modify, merge, publish,      *
81541Srgrimes * distribute, distribute with modifications, sublicense, and/or sell       *
91541Srgrimes * copies of the Software, and to permit persons to whom the Software is    *
101541Srgrimes * furnished to do so, subject to the following conditions:                 *
111541Srgrimes *                                                                          *
121541Srgrimes * The above copyright notice and this permission notice shall be included  *
131541Srgrimes * in all copies or substantial portions of the Software.                   *
141541Srgrimes *                                                                          *
151541Srgrimes * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
161541Srgrimes * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
171541Srgrimes * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
181541Srgrimes * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
191541Srgrimes * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
201541Srgrimes * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
211541Srgrimes * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
221541Srgrimes *                                                                          *
231541Srgrimes * Except as contained in this notice, the name(s) of the above copyright   *
241541Srgrimes * holders shall not be used in advertising or otherwise to promote the     *
251541Srgrimes * sale, use or other dealings in this Software without prior written       *
261541Srgrimes * authorization.                                                           *
271541Srgrimes ****************************************************************************/
281541Srgrimes
291541Srgrimes/****************************************************************************
301541Srgrimes *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
311541Srgrimes *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32116182Sobrien ****************************************************************************/
33116182Sobrien
34116182Sobrien/*
3531778Seivind *	lib_slk.c
3631778Seivind *	Soft key routines.
371541Srgrimes */
381541Srgrimes
3912221Sbde#include <curses.priv.h>
401541Srgrimes
411541Srgrimes#include <ctype.h>
4282717Sdillon#include <term.h>		/* num_labels, label_*, plab_norm */
4382717Sdillon
441541SrgrimesMODULE_ID("$Id: lib_slk.c,v 1.20 2000/12/10 02:43:27 tom Exp $")
451549Srgrimes
461541Srgrimes/*
478876Srgrimes * We'd like to move these into the screen context structure, but cannot,
48130344Sphk * because slk_init() is called before initscr()/newterm().
491541Srgrimes */
5012221SbdeNCURSES_EXPORT_VAR(int)
511541Srgrimes_nc_slk_format = 0;		/* one more than format specified in slk_init() */
521541Srgrimes
531541Srgrimes/*
541541Srgrimes * Paint the info line for the PC style SLK emulation.
5512221Sbde *
5682717Sdillon */
5782717Sdillon     static void
5882717Sdillon       slk_paint_info(WINDOW *win)
591541Srgrimes{
601549Srgrimes    if (win && SP->slk_format == 4) {
6183366Sjulian	int i;
6283366Sjulian
631541Srgrimes	mvwhline(win, 0, 0, 0, getmaxx(win));
641541Srgrimes	wmove(win, 0, 0);
6512171Sphk
6682717Sdillon	for (i = 0; i < SP->_slk->maxlab; i++) {
6738517Sdfr	    if (win && SP->slk_format == 4) {
681541Srgrimes		mvwaddch(win, 0, SP->_slk->ent[i].x, (chtype) 'F');
6912171Sphk		if (i < 9)
7012171Sphk		    waddch(win, (chtype) '1' + i);
7182717Sdillon		else {
7283366Sjulian		    waddch(win, (chtype) '1');
7382717Sdillon		    waddch(win, (chtype) '0' + (i - 9));
7482717Sdillon		}
751541Srgrimes	    }
761541Srgrimes	}
7712221Sbde    }
781541Srgrimes}
791541Srgrimes
801541Srgrimes/*
811541Srgrimes * Initialize soft labels.
8212221Sbde * Called from newterm()
8382717Sdillon */
8482717SdillonNCURSES_EXPORT(int)
8582717Sdillon_nc_slk_initialize(WINDOW *stwin, int cols)
861541Srgrimes{
871549Srgrimes    int i, x;
8883366Sjulian    int res = OK;
8983366Sjulian    char *p;
901541Srgrimes
911541Srgrimes    T(("slk_initialize()"));
9212171Sphk
931541Srgrimes    if (SP->_slk) {		/* we did this already, so simply return */
941541Srgrimes	return (OK);
9512171Sphk    } else if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
9612171Sphk	return (ERR);
9782717Sdillon
98132653Scperciva    SP->_slk->ent = NULL;
9983366Sjulian    SP->_slk->buffer = NULL;
10082717Sdillon    SP->_slk->attr = A_STANDOUT;
10182717Sdillon
10282717Sdillon    SP->_slk->maxlab = (num_labels > 0) ?
10382717Sdillon	num_labels : MAX_SKEY(_nc_slk_format);
1041541Srgrimes    SP->_slk->maxlen = (num_labels > 0) ?
1051541Srgrimes	label_width * label_height : MAX_SKEY_LEN(_nc_slk_format);
10612221Sbde    SP->_slk->labcnt = (SP->_slk->maxlab < MAX_SKEY(_nc_slk_format)) ?
10712200Sbde	MAX_SKEY(_nc_slk_format) : SP->_slk->maxlab;
1081541Srgrimes
1091541Srgrimes    SP->_slk->ent = typeCalloc(slk_ent, SP->_slk->labcnt);
11012221Sbde    if (SP->_slk->ent == NULL)
11182717Sdillon	goto exception;
11282717Sdillon
11382717Sdillon    p = SP->_slk->buffer = (char *) calloc(2 * SP->_slk->labcnt, (1 + SP->_slk->maxlen));
1141541Srgrimes    if (SP->_slk->buffer == NULL)
1151549Srgrimes	goto exception;
11683366Sjulian
11783366Sjulian    for (i = 0; i < SP->_slk->labcnt; i++) {
11812200Sbde	SP->_slk->ent[i].text = p;
1191541Srgrimes	p += (1 + SP->_slk->maxlen);
1201541Srgrimes	SP->_slk->ent[i].form_text = p;
12183366Sjulian	p += (1 + SP->_slk->maxlen);
1221541Srgrimes	memset(SP->_slk->ent[i].form_text, ' ', (unsigned) (SP->_slk->maxlen));
1231541Srgrimes	SP->_slk->ent[i].visible = (i < SP->_slk->maxlab);
124130344Sphk    }
1251541Srgrimes    if (_nc_slk_format >= 3) {	/* PC style */
1261541Srgrimes	int gap = (cols - 3 * (3 + 4 * SP->_slk->maxlen)) / 2;
12712221Sbde
12812200Sbde	if (gap < 1)
1291541Srgrimes	    gap = 1;
1301541Srgrimes
13112221Sbde	for (i = x = 0; i < SP->_slk->maxlab; i++) {
13282717Sdillon	    SP->_slk->ent[i].x = x;
13382717Sdillon	    x += SP->_slk->maxlen;
13482717Sdillon	    x += (i == 3 || i == 7) ? gap : 1;
1351541Srgrimes	}
1361549Srgrimes	if (_nc_slk_format == 4)
13783366Sjulian	    slk_paint_info(stwin);
13883366Sjulian    } else {
13912200Sbde	if (_nc_slk_format == 2) {	/* 4-4 */
1401541Srgrimes	    int gap = cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 6;
1411541Srgrimes
1421541Srgrimes	    if (gap < 1)
143119364Stjr		gap = 1;
144119364Stjr	    for (i = x = 0; i < SP->_slk->maxlab; i++) {
14582717Sdillon		SP->_slk->ent[i].x = x;
146119364Stjr		x += SP->_slk->maxlen;
14782717Sdillon		x += (i == 3) ? gap : 1;
148119364Stjr	    }
1491541Srgrimes	} else {
1501541Srgrimes	    if (_nc_slk_format == 1) {	/* 1 -> 3-2-3 */
15182717Sdillon		int gap = (cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 5)
15282717Sdillon		/ 2;
15382717Sdillon
1541549Srgrimes		if (gap < 1)
15583366Sjulian		    gap = 1;
15683366Sjulian		for (i = x = 0; i < SP->_slk->maxlab; i++) {
15712200Sbde		    SP->_slk->ent[i].x = x;
1581541Srgrimes		    x += SP->_slk->maxlen;
1591541Srgrimes		    x += (i == 2 || i == 4) ? gap : 1;
1601541Srgrimes		}
1611541Srgrimes	    } else
1621549Srgrimes		goto exception;
16374729Speter	}
16474729Speter    }
16574729Speter    SP->_slk->dirty = TRUE;
16674729Speter    if ((SP->_slk->win = stwin) == NULL) {
16774729Speter      exception:
16874729Speter	if (SP->_slk) {
16974729Speter	    FreeIfNeeded(SP->_slk->buffer);
17074729Speter	    FreeIfNeeded(SP->_slk->ent);
17112221Sbde	    free(SP->_slk);
1721549Srgrimes	    SP->_slk = (SLK *) 0;
1731549Srgrimes	    res = (ERR);
1741549Srgrimes	}
17512221Sbde    }
1761549Srgrimes
17782717Sdillon    /* We now reset the format so that the next newterm has again
17882717Sdillon     * per default no SLK keys and may call slk_init again to
17982717Sdillon     * define a new layout. (juergen 03-Mar-1999)
1801549Srgrimes     */
1811549Srgrimes    SP->slk_format = _nc_slk_format;
18283366Sjulian    _nc_slk_format = 0;
18383366Sjulian    return (res);
1841549Srgrimes}
1851549Srgrimes
18682717Sdillon/*
18738517Sdfr * Restore the soft labels on the screen.
1881549Srgrimes */
1891549SrgrimesNCURSES_EXPORT(int)
19012171Sphkslk_restore(void)
19112171Sphk{
19282717Sdillon    T((T_CALLED("slk_restore()")));
19382717Sdillon
19483366Sjulian    if (SP->_slk == NULL)
195136404Speter	return (ERR);
19682717Sdillon    SP->_slk->hidden = FALSE;
19782717Sdillon    SP->_slk->dirty = TRUE;
1981549Srgrimes    /* we have to repaint info line eventually */
1991549Srgrimes    slk_paint_info(SP->_slk->win);
20012171Sphk
2011549Srgrimes    returnCode(slk_refresh());
20283366Sjulian}
203136404Speter