xref: /openbsd-src/lib/libmenu/m_post.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: m_post.c,v 1.6 2001/01/22 18:02:06 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,2000 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: Juergen Pfeifer <juergen.pfeifer@gmx.net> 1995,1997            *
33  ****************************************************************************/
34 
35 /***************************************************************************
36 * Module m_post                                                            *
37 * Write or erase menus from associated subwindows                          *
38 ***************************************************************************/
39 
40 #include "menu.priv.h"
41 
42 MODULE_ID("$From: m_post.c,v 1.17 2000/12/10 02:16:48 tom Exp $")
43 
44 /*---------------------------------------------------------------------------
45 |   Facility      :  libnmenu
46 |   Function      :  void _nc_Post_Item(MENU *menu, ITEM *item)
47 |
48 |   Description   :  Draw the item in the menus window at the current
49 |                    window position
50 |
51 |   Return Values :  -
52 +--------------------------------------------------------------------------*/
53 NCURSES_EXPORT(void)
54 _nc_Post_Item (const MENU * menu, const ITEM * item)
55 {
56   int i;
57   chtype ch;
58   int item_x, item_y;
59   int count = 0;
60   bool isfore = FALSE, isback=FALSE, isgrey = FALSE;
61 
62   assert(menu->win);
63 
64   getyx(menu->win,item_y,item_x);
65 
66   /* We need a marker iff
67      - it is a onevalued menu and it is the current item
68      - or it has a selection value
69      */
70   wattron(menu->win,menu->back);
71   if (item->value || (item==menu->curitem))
72     {
73       if (menu->marklen)
74 	{
75 	  /* In a multi selection menu we use the fore attribute
76 	     for a selected marker that is not the current one.
77 	     This improves visualization of the menu, because now
78 	     always the 'normal' marker denotes the current
79 	     item. */
80 	  if (!(menu->opt & O_ONEVALUE) && item->value && item!=menu->curitem)
81 	    {
82 	      wattron(menu->win,menu->fore);
83 	      isfore = TRUE;
84 	    }
85 	  waddstr(menu->win,menu->mark);
86 	  if (isfore)
87 	    {
88 	      wattron(menu->win,menu->fore);
89 	      isfore = FALSE;
90 	    }
91 	}
92     }
93   else			/* otherwise we have to wipe out the marker area */
94     for(ch=' ',i=menu->marklen;i>0;i--)
95       waddch(menu->win,ch);
96   wattroff(menu->win,menu->back);
97   count += menu->marklen;
98 
99   /* First we have to calculate the attribute depending on selectability
100      and selection status
101      */
102   if (!(item->opt & O_SELECTABLE))
103     {
104       wattron(menu->win,menu->grey);
105       isgrey = TRUE;
106     }
107   else
108     {
109       if (item->value || item==menu->curitem)
110 	{
111 	  wattron(menu->win,menu->fore);
112 	  isfore = TRUE;
113 	}
114       else
115 	{
116 	  wattron(menu->win,menu->back);
117 	  isback = TRUE;
118 	}
119     }
120 
121   waddnstr(menu->win,item->name.str,item->name.length);
122   for(ch=' ',i=menu->namelen-item->name.length;i>0;i--)
123     {
124       waddch(menu->win,ch);
125     }
126   count += menu->namelen;
127 
128   /* Show description if required and available */
129   if ( (menu->opt & O_SHOWDESC) && menu->desclen>0 )
130     {
131       int m = menu->spc_desc/2;
132       int cy = -1, cx = -1;
133 
134       for(ch=' ',i=0; i < menu->spc_desc; i++)
135 	{
136 	  if (i==m)
137 	    {
138 	      waddch(menu->win,menu->pad);
139 	      getyx(menu->win,cy,cx);
140 	    }
141 	  else
142 	    waddch(menu->win,ch);
143 	}
144       if (item->description.length)
145 	waddnstr(menu->win,item->description.str,item->description.length);
146       for(ch=' ',i=menu->desclen-item->description.length; i>0; i--)
147 	{
148 	  waddch(menu->win,ch);
149 	}
150       count += menu->desclen + menu->spc_desc;
151 
152       if (menu->spc_rows > 1)
153 	{
154 	  int j, k, ncy, ncx;
155 
156 	  assert(cx>=0 && cy>=0);
157 	  getyx(menu->win,ncy,ncx);
158 	  if (isgrey) wattroff(menu->win,menu->grey);
159 	  else if (isfore) wattroff(menu->win,menu->fore);
160 	  wattron(menu->win,menu->back);
161 	  for(j=1; j < menu->spc_rows;j++)
162 	    {
163 	      if ((item_y+j) < getmaxy(menu->win))
164 		{
165 		  wmove (menu->win,item_y+j,item_x);
166 		  for(k=0;k<count;k++)
167 		    waddch(menu->win,' ');
168 		}
169 	      if ((cy+j) < getmaxy(menu->win))
170 		mvwaddch(menu->win,cy+j,cx-1,menu->pad);
171 	    }
172 	  wmove(menu->win,ncy,ncx);
173 	  if (!isback)
174 	    wattroff(menu->win,menu->back);
175 	}
176     }
177 
178   /* Remove attributes */
179   if (isfore)
180     wattroff(menu->win,menu->fore);
181   if (isback)
182     wattroff(menu->win,menu->back);
183   if (isgrey)
184     wattroff(menu->win,menu->grey);
185 }
186 
187 /*---------------------------------------------------------------------------
188 |   Facility      :  libnmenu
189 |   Function      :  void _nc_Draw_Menu(const MENU *)
190 |
191 |   Description   :  Display the menu in its windows
192 |
193 |   Return Values :  -
194 +--------------------------------------------------------------------------*/
195 NCURSES_EXPORT(void)
196 _nc_Draw_Menu (const MENU * menu)
197 {
198   ITEM *item = menu->items[0];
199   ITEM *lasthor, *lastvert;
200   ITEM *hitem;
201   int y = 0;
202   chtype s_bkgd;
203 
204   assert(item && menu->win);
205 
206   s_bkgd = getbkgd(menu->win);
207   wbkgdset(menu->win,menu->back);
208   werase(menu->win);
209   wbkgdset(menu->win,s_bkgd);
210 
211   lastvert = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : item;
212 
213   do
214     {
215       wmove(menu->win,y,0);
216 
217       hitem   = item;
218       lasthor = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : hitem;
219 
220       do
221 	{
222 	  _nc_Post_Item( menu, hitem);
223 
224 	  wattron(menu->win,menu->back);
225 	  if ( ((hitem = hitem->right) != lasthor) && hitem )
226 	    {
227 	      int i,j, cy, cx;
228 	      chtype ch = ' ';
229 
230 	      getyx(menu->win,cy,cx);
231 	      for(j=0;j<menu->spc_rows;j++)
232 		{
233 		  wmove(menu->win,cy+j,cx);
234 		  for(i=0; i < menu->spc_cols; i++)
235 		    {
236 		      waddch( menu->win,ch);
237 		    }
238 		}
239 	      wmove(menu->win,cy,cx+menu->spc_cols);
240 	    }
241 	} while (hitem && (hitem != lasthor));
242       wattroff(menu->win,menu->back);
243 
244       item = item->down;
245       y += menu->spc_rows;
246 
247     } while( item && (item != lastvert) );
248 }
249 
250 /*---------------------------------------------------------------------------
251 |   Facility      :  libnmenu
252 |   Function      :  int post_menu(MENU *)
253 |
254 |   Description   :  Post a menu to the screen. This makes it visible.
255 |
256 |   Return Values :  E_OK                - success
257 |                    E_BAD_ARGUMENT      - not a valid menu pointer
258 |                    E_SYSTEM_ERROR      - error in lower layers
259 |                    E_NO_ROOM           - Menu to large for screen
260 |                    E_NOT_CONNECTED     - No items connected to menu
261 |                    E_BAD_STATE         - Menu in userexit routine
262 |                    E_POSTED            - Menu already posted
263 +--------------------------------------------------------------------------*/
264 NCURSES_EXPORT(int)
265 post_menu (MENU * menu)
266 {
267   if (!menu)
268     RETURN(E_BAD_ARGUMENT);
269 
270   if ( menu->status & _IN_DRIVER )
271     RETURN(E_BAD_STATE);
272 
273   if ( menu->status & _POSTED )
274     RETURN(E_POSTED);
275 
276   if (menu->items && *(menu->items))
277     {
278       int y;
279       int h = 1 + menu->spc_rows * (menu->rows - 1);
280 
281       WINDOW *win = Get_Menu_Window(menu);
282       int maxy = getmaxy(win);
283       int maxx = getmaxx(win);
284 
285       if (maxx < menu->width || maxy < menu->height)
286 	RETURN(E_NO_ROOM);
287 
288       if ( (menu->win = newpad(h,menu->width)) )
289 	{
290 	  y = (maxy >= h) ? h : maxy;
291 	  if (y>=menu->height)
292 	    y = menu->height;
293 	  if(!(menu->sub = subpad(menu->win,y,menu->width,0,0)))
294 	    RETURN(E_SYSTEM_ERROR);
295 	}
296       else
297 	RETURN(E_SYSTEM_ERROR);
298 
299       if (menu->status & _LINK_NEEDED)
300 	_nc_Link_Items(menu);
301     }
302   else
303     RETURN(E_NOT_CONNECTED);
304 
305   menu->status |= _POSTED;
306 
307   if (!(menu->opt&O_ONEVALUE))
308     {
309       ITEM **items;
310 
311       for(items=menu->items;*items;items++)
312 	{
313 	  (*items)->value = FALSE;
314 	}
315     }
316 
317   _nc_Draw_Menu(menu);
318 
319   Call_Hook(menu,menuinit);
320   Call_Hook(menu,iteminit);
321 
322   _nc_Show_Menu(menu);
323 
324   RETURN(E_OK);
325 }
326 
327 /*---------------------------------------------------------------------------
328 |   Facility      :  libnmenu
329 |   Function      :  int unpost_menu(MENU *)
330 |
331 |   Description   :  Detach menu from screen
332 |
333 |   Return Values :  E_OK              - success
334 |                    E_BAD_ARGUMENT    - not a valid menu pointer
335 |                    E_BAD_STATE       - menu in userexit routine
336 |                    E_NOT_POSTED      - menu is not posted
337 +--------------------------------------------------------------------------*/
338 NCURSES_EXPORT(int)
339 unpost_menu (MENU * menu)
340 {
341   WINDOW *win;
342 
343   if (!menu)
344     RETURN(E_BAD_ARGUMENT);
345 
346   if ( menu->status & _IN_DRIVER )
347     RETURN(E_BAD_STATE);
348 
349   if ( !( menu->status & _POSTED ) )
350     RETURN(E_NOT_POSTED);
351 
352   Call_Hook(menu,itemterm);
353   Call_Hook(menu,menuterm);
354 
355   win = Get_Menu_Window(menu);
356   werase(win);
357   wsyncup(win);
358 
359   assert(menu->sub);
360   delwin(menu->sub);
361   menu->sub = (WINDOW *)0;
362 
363   assert(menu->win);
364   delwin(menu->win);
365   menu->win = (WINDOW *)0;
366 
367   menu->status &= ~_POSTED;
368 
369   RETURN(E_OK);
370 }
371 
372 /* m_post.c ends here */
373