1 /****************************************************************************
2  * Copyright (c) 1998,1999 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  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33 
34 /*
35  *	lib_slk.c
36  *	Soft key routines.
37  */
38 
39 #include <curses.priv.h>
40 
41 #include <ctype.h>
42 #include <term.h>	/* num_labels, label_*, plab_norm */
43 
44 MODULE_ID("$Id: lib_slk.c,v 1.17 1999/10/30 23:00:16 tom Exp $")
45 
46 /*
47  * We'd like to move these into the screen context structure, but cannot,
48  * because slk_init() is called before initscr()/newterm().
49  */
50 int _nc_slk_format = 0;  /* one more than format specified in slk_init() */
51 
52 /*
53  * Paint the info line for the PC style SLK emulation.
54  *
55  */
56 static void
57 slk_paint_info(WINDOW *win)
58 {
59   if (win && SP->slk_format==4)
60     {
61       int i;
62 
63       mvwhline (win,0,0,0,getmaxx(win));
64       wmove (win,0,0);
65 
66       for (i = 0; i < SP->_slk->maxlab; i++) {
67 	if (win && SP->slk_format==4)
68 	  {
69 	    mvwaddch(win,0,SP->_slk->ent[i].x,'F');
70 	    if (i<9)
71 	      waddch(win,'1'+i);
72 	    else
73 	      {
74 		waddch(win,'1');
75 		waddch(win,'0' + (i-9));
76 	      }
77 	  }
78       }
79     }
80 }
81 
82 /*
83  * Initialize soft labels.
84  * Called from newterm()
85  */
86 int
87 _nc_slk_initialize(WINDOW *stwin, int cols)
88 {
89 int i, x;
90 int res = OK;
91 char *p;
92 
93 	T(("slk_initialize()"));
94 
95 	if (SP->_slk)
96 	  { /* we did this already, so simply return */
97 	    return(OK);
98 	  }
99 	else
100 	  if ((SP->_slk = typeCalloc(SLK, 1)) == 0)
101 	    return(ERR);
102 
103 	SP->_slk->ent    = NULL;
104 	SP->_slk->buffer = NULL;
105 	SP->_slk->attr   = A_STANDOUT;
106 
107 	SP->_slk->maxlab = (num_labels > 0) ?
108 	  num_labels : MAX_SKEY(_nc_slk_format);
109 	SP->_slk->maxlen = (num_labels > 0) ?
110 	  label_width * label_height : MAX_SKEY_LEN(_nc_slk_format);
111 	SP->_slk->labcnt = (SP->_slk->maxlab < MAX_SKEY(_nc_slk_format)) ?
112 	  MAX_SKEY(_nc_slk_format) : SP->_slk->maxlab;
113 
114 	SP->_slk->ent = typeCalloc(slk_ent, SP->_slk->labcnt);
115 	if (SP->_slk->ent == NULL)
116 	  goto exception;
117 
118 	p = SP->_slk->buffer = (char*) calloc(2*SP->_slk->labcnt,(1+SP->_slk->maxlen));
119 	if (SP->_slk->buffer == NULL)
120 	  goto exception;
121 
122 	for (i = 0; i < SP->_slk->labcnt; i++) {
123 		SP->_slk->ent[i].text = p;
124 		p += (1 + SP->_slk->maxlen);
125 		SP->_slk->ent[i].form_text = p;
126 		p += (1 + SP->_slk->maxlen);
127 		memset(SP->_slk->ent[i].form_text, ' ', (unsigned)(SP->_slk->maxlen));
128 		SP->_slk->ent[i].visible = (i < SP->_slk->maxlab);
129 	}
130 	if (_nc_slk_format >= 3) /* PC style */
131 	  {
132 	    int gap = (cols - 3 * (3 + 4*SP->_slk->maxlen))/2;
133 
134 	    if (gap < 1)
135 	      gap = 1;
136 
137 	    for (i = x = 0; i < SP->_slk->maxlab; i++) {
138 	      SP->_slk->ent[i].x = x;
139 	      x += SP->_slk->maxlen;
140 	      x += (i==3 || i==7) ? gap : 1;
141 	    }
142 	    if (_nc_slk_format == 4)
143 	      slk_paint_info (stwin);
144 	  }
145 	else {
146 	  if (_nc_slk_format == 2) {	/* 4-4 */
147 	    int gap = cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 6;
148 
149 	    if (gap < 1)
150 			gap = 1;
151 	    for (i = x = 0; i < SP->_slk->maxlab; i++) {
152 	      SP->_slk->ent[i].x = x;
153 	      x += SP->_slk->maxlen;
154 	      x += (i == 3) ? gap : 1;
155 	    }
156 	  }
157 	  else
158 	    {
159 	      if (_nc_slk_format == 1) { /* 1 -> 3-2-3 */
160 		int gap = (cols - (SP->_slk->maxlab * SP->_slk->maxlen) - 5) / 2;
161 
162 		if (gap < 1)
163 		  gap = 1;
164 		for (i = x = 0; i < SP->_slk->maxlab; i++) {
165 		  SP->_slk->ent[i].x = x;
166 		  x += SP->_slk->maxlen;
167 		  x += (i == 2 || i == 4) ? gap : 1;
168 		}
169 	      }
170 	      else
171 		goto exception;
172 	    }
173 	}
174 	SP->_slk->dirty = TRUE;
175 	if ((SP->_slk->win = stwin) == NULL)
176 	{
177 	exception:
178 		if (SP->_slk)
179 		{
180 		   FreeIfNeeded(SP->_slk->buffer);
181 		   FreeIfNeeded(SP->_slk->ent);
182 		   free(SP->_slk);
183 		   SP->_slk = (SLK*)0;
184 		   res = (ERR);
185 		}
186 	}
187 
188 	/* We now reset the format so that the next newterm has again
189 	 * per default no SLK keys and may call slk_init again to
190 	 * define a new layout. (juergen 03-Mar-1999)
191 	 */
192 	SP->slk_format = _nc_slk_format;
193 	_nc_slk_format = 0;
194 	return(res);
195 }
196 
197 
198 /*
199  * Restore the soft labels on the screen.
200  */
201 int
202 slk_restore(void)
203 {
204 	T((T_CALLED("slk_restore()")));
205 
206 	if (SP->_slk == NULL)
207 		return(ERR);
208 	SP->_slk->hidden = FALSE;
209 	SP->_slk->dirty = TRUE;
210 	/* we have to repaint info line eventually */
211 	slk_paint_info(SP->_slk->win);
212 
213 	returnCode(slk_refresh());
214 }
215