lib_slk.c revision 1.1
1/*	$OpenBSD: lib_slk.c,v 1.1 1999/01/18 19:09:59 millert Exp $	*/
2
3/****************************************************************************
4 * Copyright (c) 1998 Free Software Foundation, Inc.                        *
5 *                                                                          *
6 * Permission is hereby granted, free of charge, to any person obtaining a  *
7 * copy of this software and associated documentation files (the            *
8 * "Software"), to deal in the Software without restriction, including      *
9 * without limitation the rights to use, copy, modify, merge, publish,      *
10 * distribute, distribute with modifications, sublicense, and/or sell       *
11 * copies of the Software, and to permit persons to whom the Software is    *
12 * furnished to do so, subject to the following conditions:                 *
13 *                                                                          *
14 * The above copyright notice and this permission notice shall be included  *
15 * in all copies or substantial portions of the Software.                   *
16 *                                                                          *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24 *                                                                          *
25 * Except as contained in this notice, the name(s) of the above copyright   *
26 * holders shall not be used in advertising or otherwise to promote the     *
27 * sale, use or other dealings in this Software without prior written       *
28 * authorization.                                                           *
29 ****************************************************************************/
30
31/****************************************************************************
32 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34 ****************************************************************************/
35
36/*
37 *	lib_slk.c
38 *	Soft key routines.
39 */
40
41#include <curses.priv.h>
42
43#include <ctype.h>
44#include <term.h>	/* num_labels, label_*, plab_norm */
45
46MODULE_ID("$From: lib_slk.c,v 1.15 1999/01/02 22:56:30 tom Exp $")
47
48/*
49 * We'd like to move these into the screen context structure, but cannot,
50 * because slk_init() is called before initscr()/newterm().
51 */
52int _nc_slk_format;  /* one more than format specified in slk_init() */
53
54/*
55 * Paint the info line for the PC style SLK emulation.
56 *
57 */
58static void
59slk_paint_info(WINDOW *win)
60{
61  if (win && _nc_slk_format==4)
62    {
63      int i;
64
65      mvwhline (win,0,0,0,getmaxx(win));
66      wmove (win,0,0);
67
68      for (i = 0; i < SP->_slk->maxlab; i++) {
69	if (win && _nc_slk_format==4)
70	  {
71	    mvwaddch(win,0,SP->_slk->ent[i].x,'F');
72	    if (i<9)
73	      waddch(win,'1'+i);
74	    else
75	      {
76		waddch(win,'1');
77		waddch(win,'0' + (i-9));
78	      }
79	  }
80      }
81    }
82}
83
84/*
85 * Initialize soft labels.
86 * Called from newterm()
87 */
88int
89_nc_slk_initialize(WINDOW *stwin, int cols)
90{
91int i, x;
92char *p;
93
94	T(("slk_initialize()"));
95
96	if (SP->_slk)
97	  { /* we did this already, so simply return */
98	    return(OK);
99	  }
100	else
101	  if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
102	    return(ERR);
103
104	SP->_slk->ent    = NULL;
105	SP->_slk->buffer = NULL;
106	SP->_slk->attr   = A_STANDOUT;
107
108	SP->_slk->maxlab = (num_labels > 0) ? num_labels : MAX_SKEY;
109	SP->_slk->maxlen = (num_labels > 0) ? label_width * label_height : MAX_SKEY_LEN;
110	SP->_slk->labcnt = (SP->_slk->maxlab < MAX_SKEY) ? MAX_SKEY : SP->_slk->maxlab;
111
112	SP->_slk->ent = typeCalloc(slk_ent, SP->_slk->labcnt);
113	if (SP->_slk->ent == NULL)
114	  goto exception;
115
116	p = SP->_slk->buffer = (char*) calloc(2*SP->_slk->labcnt,(1+SP->_slk->maxlen));
117	if (SP->_slk->buffer == NULL)
118	  goto exception;
119
120	for (i = 0; i < SP->_slk->labcnt; i++) {
121		SP->_slk->ent[i].text = p;
122		p += (1 + SP->_slk->maxlen);
123		SP->_slk->ent[i].form_text = p;
124		p += (1 + SP->_slk->maxlen);
125		memset(SP->_slk->ent[i].form_text, ' ', (unsigned)(SP->_slk->maxlen));
126		SP->_slk->ent[i].visible = (i < SP->_slk->maxlab);
127	}
128	if (_nc_slk_format >= 3) /* PC style */
129	  {
130	    int gap = (cols - 3 * (3 + 4*SP->_slk->maxlen))/2;
131
132	    if (gap < 1)
133	      gap = 1;
134
135	    for (i = x = 0; i < SP->_slk->maxlab; i++) {
136	      SP->_slk->ent[i].x = x;
137	      x += SP->_slk->maxlen;
138	      x += (i==3 || i==7) ? gap : 1;
139	    }
140	    if (_nc_slk_format == 4)
141	      slk_paint_info (stwin);
142	  }
143	else {
144	  if (_nc_slk_format == 2) {	/* 4-4 */
145	    int gap = cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 6;
146
147	    if (gap < 1)
148			gap = 1;
149	    for (i = x = 0; i < SP->_slk->maxlab; i++) {
150	      SP->_slk->ent[i].x = x;
151	      x += SP->_slk->maxlen;
152	      x += (i == 3) ? gap : 1;
153	    }
154	  }
155	  else
156	    {
157	      if (_nc_slk_format == 1) { /* 1 -> 3-2-3 */
158		int gap = (cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 5) / 2;
159
160		if (gap < 1)
161		  gap = 1;
162		for (i = x = 0; i < SP->_slk->maxlab; i++) {
163		  SP->_slk->ent[i].x = x;
164		  x += SP->_slk->maxlen;
165		  x += (i == 2 || i == 4) ? gap : 1;
166		}
167	      }
168	      else
169		goto exception;
170	    }
171	}
172	SP->_slk->dirty = TRUE;
173	if ((SP->_slk->win = stwin) == NULL)
174	{
175	exception:
176		if (SP->_slk)
177		{
178		   FreeIfNeeded(SP->_slk->buffer);
179		   FreeIfNeeded(SP->_slk->ent);
180		   free(SP->_slk);
181		   SP->_slk = (SLK*)0;
182		   return(ERR);
183		}
184	}
185
186	return(OK);
187}
188
189
190/*
191 * Restore the soft labels on the screen.
192 */
193int
194slk_restore(void)
195{
196	T((T_CALLED("slk_restore()")));
197
198	if (SP->_slk == NULL)
199		return(ERR);
200	SP->_slk->hidden = FALSE;
201	SP->_slk->dirty = TRUE;
202	/* we have to repaint info line eventually */
203	slk_paint_info(SP->_slk->win);
204
205	returnCode(slk_refresh());
206}
207