xref: /freebsd/contrib/ncurses/menu/m_post.c (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2010,2012 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *   Author:  Juergen Pfeifer, 1995,1997                                    *
32  ****************************************************************************/
33 
34 /***************************************************************************
35 * Module m_post                                                            *
36 * Write or erase menus from associated subwindows                          *
37 ***************************************************************************/
38 
39 #include "menu.priv.h"
40 
41 MODULE_ID("$Id: m_post.c,v 1.32 2020/02/02 23:34:34 tom Exp $")
42 
43 /*---------------------------------------------------------------------------
44 |   Facility      :  libnmenu
45 |   Function      :  void _nc_Post_Item(MENU *menu, ITEM *item)
46 |
47 |   Description   :  Draw the item in the menus window at the current
48 |                    window position
49 |
50 |   Return Values :  -
51 +--------------------------------------------------------------------------*/
52 NCURSES_EXPORT(void)
53 _nc_Post_Item(const MENU * menu, const ITEM * item)
54 {
55   int i;
56   chtype ch;
57   int item_x, item_y;
58   int count = 0;
59   bool isfore = FALSE, isback = FALSE, isgrey = FALSE;
60   int name_len;
61   int desc_len;
62 
63   assert(menu->win);
64 
65   getyx(menu->win, item_y, item_x);
66 
67   /* We need a marker iff
68      - it is a onevalued menu and it is the current item
69      - or it has a selection value
70    */
71   wattron(menu->win, (int)menu->back);
72   if (item->value || (item == menu->curitem))
73     {
74       if (menu->marklen)
75 	{
76 	  /* In a multi selection menu we use the fore attribute
77 	     for a selected marker that is not the current one.
78 	     This improves visualization of the menu, because now
79 	     always the 'normal' marker denotes the current
80 	     item. */
81 	  if (!(menu->opt & O_ONEVALUE) && item->value && item != menu->curitem)
82 	    {
83 	      wattron(menu->win, (int)menu->fore);
84 	      isfore = TRUE;
85 	    }
86 	  waddstr(menu->win, menu->mark);
87 	  if (isfore)
88 	    {
89 	      wattron(menu->win, (int)menu->fore);
90 	      isfore = FALSE;
91 	    }
92 	}
93     }
94   else				/* otherwise we have to wipe out the marker area */
95     for (ch = ' ', i = menu->marklen; i > 0; i--)
96       waddch(menu->win, ch);
97   wattroff(menu->win, (int)menu->back);
98   count += menu->marklen;
99 
100   /* First we have to calculate the attribute depending on selectability
101      and selection status
102    */
103   if (!(item->opt & O_SELECTABLE))
104     {
105       wattron(menu->win, (int)menu->grey);
106       isgrey = TRUE;
107     }
108   else
109     {
110       if (item->value || item == menu->curitem)
111 	{
112 	  wattron(menu->win, (int)menu->fore);
113 	  isfore = TRUE;
114 	}
115       else
116 	{
117 	  wattron(menu->win, (int)menu->back);
118 	  isback = TRUE;
119 	}
120     }
121 
122   waddnstr(menu->win, item->name.str, item->name.length);
123   name_len = _nc_Calculate_Text_Width(&(item->name));
124   for (ch = ' ', i = menu->namelen - name_len; i > 0; i--)
125     {
126       waddch(menu->win, ch);
127     }
128   count += menu->namelen;
129 
130   /* Show description if required and available */
131   if ((menu->opt & O_SHOWDESC) && menu->desclen > 0)
132     {
133       int m = menu->spc_desc / 2;
134       int cy = -1, cx = -1;
135 
136       for (ch = ' ', i = 0; i < menu->spc_desc; i++)
137 	{
138 	  if (i == m)
139 	    {
140 	      waddch(menu->win, menu->pad);
141 	      getyx(menu->win, cy, cx);
142 	    }
143 	  else
144 	    waddch(menu->win, ch);
145 	}
146       if (item->description.length)
147 	waddnstr(menu->win, item->description.str, item->description.length);
148       desc_len = _nc_Calculate_Text_Width(&(item->description));
149       for (ch = ' ', i = menu->desclen - desc_len; i > 0; i--)
150 	{
151 	  waddch(menu->win, ch);
152 	}
153       count += menu->desclen + menu->spc_desc;
154 
155       if (menu->spc_rows > 1)
156 	{
157 	  int j, k, ncy, ncx;
158 
159 	  assert(cx >= 0 && cy >= 0);
160 	  getyx(menu->win, ncy, ncx);
161 	  if (isgrey)
162 	    wattroff(menu->win, (int)menu->grey);
163 	  else if (isfore)
164 	    wattroff(menu->win, (int)menu->fore);
165 	  wattron(menu->win, (int)menu->back);
166 	  for (j = 1; j < menu->spc_rows; j++)
167 	    {
168 	      if ((item_y + j) < getmaxy(menu->win))
169 		{
170 		  wmove(menu->win, item_y + j, item_x);
171 		  for (k = 0; k < count; k++)
172 		    waddch(menu->win, ' ');
173 		}
174 	      if ((cy + j) < getmaxy(menu->win))
175 		(void)mvwaddch(menu->win, cy + j, cx - 1, menu->pad);
176 	    }
177 	  wmove(menu->win, ncy, ncx);
178 	  if (!isback)
179 	    wattroff(menu->win, (int)menu->back);
180 	}
181     }
182 
183   /* Remove attributes */
184   if (isfore)
185     wattroff(menu->win, (int)menu->fore);
186   if (isback)
187     wattroff(menu->win, (int)menu->back);
188   if (isgrey)
189     wattroff(menu->win, (int)menu->grey);
190 }
191 
192 /*---------------------------------------------------------------------------
193 |   Facility      :  libnmenu
194 |   Function      :  void _nc_Draw_Menu(const MENU *)
195 |
196 |   Description   :  Display the menu in its windows
197 |
198 |   Return Values :  -
199 +--------------------------------------------------------------------------*/
200 NCURSES_EXPORT(void)
201 _nc_Draw_Menu(const MENU * menu)
202 {
203   ITEM *item = menu->items[0];
204   ITEM *lasthor, *lastvert;
205   ITEM *hitem;
206   int y = 0;
207   chtype s_bkgd;
208 
209   assert(item && menu->win);
210 
211   s_bkgd = getbkgd(menu->win);
212   wbkgdset(menu->win, menu->back);
213   werase(menu->win);
214   wbkgdset(menu->win, s_bkgd);
215 
216   lastvert = (menu->opt & O_NONCYCLIC) ? (ITEM *) 0 : item;
217 
218   do
219     {
220       wmove(menu->win, y, 0);
221 
222       hitem = item;
223       lasthor = (menu->opt & O_NONCYCLIC) ? (ITEM *) 0 : hitem;
224 
225       do
226 	{
227 	  _nc_Post_Item(menu, hitem);
228 
229 	  wattron(menu->win, (int)menu->back);
230 	  if (((hitem = hitem->right) != lasthor) && hitem)
231 	    {
232 	      int i, j, cy, cx;
233 	      chtype ch = ' ';
234 
235 	      getyx(menu->win, cy, cx);
236 	      for (j = 0; j < menu->spc_rows; j++)
237 		{
238 		  wmove(menu->win, cy + j, cx);
239 		  for (i = 0; i < menu->spc_cols; i++)
240 		    {
241 		      waddch(menu->win, ch);
242 		    }
243 		}
244 	      wmove(menu->win, cy, cx + menu->spc_cols);
245 	    }
246 	}
247       while (hitem && (hitem != lasthor));
248       wattroff(menu->win, (int)menu->back);
249 
250       item = item->down;
251       y += menu->spc_rows;
252 
253     }
254   while (item && (item != lastvert));
255 }
256 
257 /*---------------------------------------------------------------------------
258 |   Facility      :  libnmenu
259 |   Function      :  int post_menu(MENU* menu)
260 |
261 |   Description   :  Post a menu to the screen. This makes it visible.
262 |
263 |   Return Values :  E_OK                - success
264 |                    E_BAD_ARGUMENT      - not a valid menu pointer
265 |                    E_SYSTEM_ERROR      - error in lower layers
266 |                    E_NOT_CONNECTED     - No items connected to menu
267 |                    E_BAD_STATE         - Menu in userexit routine
268 |                    E_POSTED            - Menu already posted
269 +--------------------------------------------------------------------------*/
270 NCURSES_EXPORT(int)
271 post_menu(MENU * menu)
272 {
273   T((T_CALLED("post_menu(%p)"), (void *)menu));
274 
275   if (!menu)
276     RETURN(E_BAD_ARGUMENT);
277 
278   if (menu->status & _IN_DRIVER)
279     RETURN(E_BAD_STATE);
280 
281   if (menu->status & _POSTED)
282     RETURN(E_POSTED);
283 
284   if (menu->items && *(menu->items))
285     {
286       int y;
287       int h = 1 + menu->spc_rows * (menu->rows - 1);
288 
289       WINDOW *win = Get_Menu_Window(menu);
290       int maxy = getmaxy(win);
291 
292       if ((menu->win = newpad(h, menu->width)))
293 	{
294 	  y = (maxy >= h) ? h : maxy;
295 	  if (y >= menu->height)
296 	    y = menu->height;
297 	  if (!(menu->sub = subpad(menu->win, y, menu->width, 0, 0)))
298 	    RETURN(E_SYSTEM_ERROR);
299 	}
300       else
301 	RETURN(E_SYSTEM_ERROR);
302 
303       if (menu->status & _LINK_NEEDED)
304 	_nc_Link_Items(menu);
305     }
306   else
307     RETURN(E_NOT_CONNECTED);
308 
309   SetStatus(menu, _POSTED);
310 
311   if (!(menu->opt & O_ONEVALUE))
312     {
313       ITEM **items;
314 
315       for (items = menu->items; *items; items++)
316 	{
317 	  (*items)->value = FALSE;
318 	}
319     }
320 
321   _nc_Draw_Menu(menu);
322 
323   Call_Hook(menu, menuinit);
324   Call_Hook(menu, iteminit);
325 
326   _nc_Show_Menu(menu);
327 
328   RETURN(E_OK);
329 }
330 
331 /*---------------------------------------------------------------------------
332 |   Facility      :  libnmenu
333 |   Function      :  int unpost_menu(MENU*)
334 |
335 |   Description   :  Detach menu from screen
336 |
337 |   Return Values :  E_OK              - success
338 |                    E_BAD_ARGUMENT    - not a valid menu pointer
339 |                    E_BAD_STATE       - menu in userexit routine
340 |                    E_NOT_POSTED      - menu is not posted
341 +--------------------------------------------------------------------------*/
342 NCURSES_EXPORT(int)
343 unpost_menu(MENU * menu)
344 {
345   WINDOW *win;
346 
347   T((T_CALLED("unpost_menu(%p)"), (void *)menu));
348 
349   if (!menu)
350     RETURN(E_BAD_ARGUMENT);
351 
352   if (menu->status & _IN_DRIVER)
353     RETURN(E_BAD_STATE);
354 
355   if (!(menu->status & _POSTED))
356     RETURN(E_NOT_POSTED);
357 
358   Call_Hook(menu, itemterm);
359   Call_Hook(menu, menuterm);
360 
361   win = Get_Menu_Window(menu);
362   werase(win);
363   wsyncup(win);
364 
365   assert(menu->sub);
366   delwin(menu->sub);
367   menu->sub = (WINDOW *)0;
368 
369   assert(menu->win);
370   delwin(menu->win);
371   menu->win = (WINDOW *)0;
372 
373   ClrStatus(menu, _POSTED);
374 
375   RETURN(E_OK);
376 }
377 
378 /* m_post.c ends here */
379