1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2010,2012 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer, 1995,1997 *
32 ****************************************************************************/
34 /***************************************************************************
36 * Write or erase menus from associated subwindows *
37 ***************************************************************************/
39 #include "menu.priv.h"
41 MODULE_ID("$Id: m_post.c,v 1.36 2021/05/08 20:20:01 tom Exp $")
43 /*---------------------------------------------------------------------------
45 | Function : void _nc_Post_Item(MENU *menu, ITEM *item)
47 | Description : Draw the item in the menus window at the current
51 +--------------------------------------------------------------------------*/
53 _nc_Post_Item(const MENU *menu, const ITEM *item)
59 bool isfore = FALSE, isback = FALSE, isgrey = FALSE;
64 getyx(menu->win, item_y, item_x);
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
70 wattron(menu->win, (int)menu->back);
71 if (item->value || (item == menu->curitem))
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
80 if (!(menu->opt & O_ONEVALUE) && item->value && item != menu->curitem)
82 wattron(menu->win, (int)menu->fore);
85 waddstr(menu->win, menu->mark);
88 wattron(menu->win, (int)menu->fore);
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, (int)menu->back);
97 count += menu->marklen;
99 /* First we have to calculate the attribute depending on selectability
102 if (!(item->opt & O_SELECTABLE))
104 wattron(menu->win, (int)menu->grey);
109 if (item->value || item == menu->curitem)
111 wattron(menu->win, (int)menu->fore);
116 wattron(menu->win, (int)menu->back);
121 waddnstr(menu->win, item->name.str, item->name.length);
122 name_len = _nc_Calculate_Text_Width(&(item->name));
123 for (ch = ' ', i = menu->namelen - name_len; i > 0; i--)
125 waddch(menu->win, ch);
127 count += menu->namelen;
129 /* Show description if required and available */
130 if ((menu->opt & O_SHOWDESC) && menu->desclen > 0)
132 int m = menu->spc_desc / 2;
133 int cy = -1, cx = -1;
136 for (ch = ' ', i = 0; i < menu->spc_desc; i++)
140 waddch(menu->win, menu->pad);
141 getyx(menu->win, cy, cx);
144 waddch(menu->win, ch);
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--)
151 waddch(menu->win, ch);
153 count += menu->desclen + menu->spc_desc;
155 if (menu->spc_rows > 1)
159 assert(cx >= 0 && cy >= 0);
160 getyx(menu->win, ncy, ncx);
162 wattroff(menu->win, (int)menu->grey);
164 wattroff(menu->win, (int)menu->fore);
165 wattron(menu->win, (int)menu->back);
166 for (j = 1; j < menu->spc_rows; j++)
168 if ((item_y + j) < getmaxy(menu->win))
170 wmove(menu->win, item_y + j, item_x);
171 for (k = 0; k < count; k++)
172 waddch(menu->win, ' ');
174 if ((cy + j) < getmaxy(menu->win))
175 (void)mvwaddch(menu->win, cy + j, cx - 1, menu->pad);
177 wmove(menu->win, ncy, ncx);
179 wattroff(menu->win, (int)menu->back);
183 /* Remove attributes */
185 wattroff(menu->win, (int)menu->fore);
187 wattroff(menu->win, (int)menu->back);
189 wattroff(menu->win, (int)menu->grey);
192 /*---------------------------------------------------------------------------
193 | Facility : libnmenu
194 | Function : void _nc_Draw_Menu(const MENU *)
196 | Description : Display the menu in its windows
199 +--------------------------------------------------------------------------*/
201 _nc_Draw_Menu(const MENU *menu)
203 ITEM *item = menu->items[0];
209 assert(item && menu->win);
211 s_bkgd = getbkgd(menu->win);
212 wbkgdset(menu->win, menu->back);
214 wbkgdset(menu->win, s_bkgd);
216 lastvert = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : item;
224 wmove(menu->win, y, 0);
227 lasthor = (menu->opt & O_NONCYCLIC) ? (ITEM *)0 : hitem;
231 _nc_Post_Item(menu, hitem);
233 wattron(menu->win, (int)menu->back);
234 if (((hitem = hitem->right) != lasthor) && hitem)
239 getyx(menu->win, cy, cx);
240 for (j = 0; j < menu->spc_rows; j++)
242 wmove(menu->win, cy + j, cx);
243 for (i = 0; i < menu->spc_cols; i++)
245 waddch(menu->win, ch);
248 wmove(menu->win, cy, cx + menu->spc_cols);
251 while (hitem && (hitem != lasthor));
252 wattroff(menu->win, (int)menu->back);
258 while (item && (item != lastvert));
262 /*---------------------------------------------------------------------------
263 | Facility : libnmenu
264 | Function : int post_menu(MENU* menu)
266 | Description : Post a menu to the screen. This makes it visible.
268 | Return Values : E_OK - success
269 | E_BAD_ARGUMENT - not a valid menu pointer
270 | E_SYSTEM_ERROR - error in lower layers
271 | E_NOT_CONNECTED - No items connected to menu
272 | E_BAD_STATE - Menu in userexit routine
273 | E_POSTED - Menu already posted
274 +--------------------------------------------------------------------------*/
276 post_menu(MENU *menu)
278 T((T_CALLED("post_menu(%p)"), (void *)menu));
281 RETURN(E_BAD_ARGUMENT);
283 if (menu->status & _IN_DRIVER)
286 if (menu->status & _POSTED)
289 if (menu->items && *(menu->items))
291 int h = 1 + menu->spc_rows * (menu->rows - 1);
293 WINDOW *win = Get_Menu_Window(menu);
294 int maxy = getmaxy(win);
296 if ((menu->win = newpad(h, menu->width)))
298 int y = (maxy >= h) ? h : maxy;
300 if (y >= menu->height)
302 if (!(menu->sub = subpad(menu->win, y, menu->width, 0, 0)))
303 RETURN(E_SYSTEM_ERROR);
306 RETURN(E_SYSTEM_ERROR);
308 if (menu->status & _LINK_NEEDED)
309 _nc_Link_Items(menu);
312 RETURN(E_NOT_CONNECTED);
314 SetStatus(menu, _POSTED);
316 if (!(menu->opt & O_ONEVALUE))
320 for (items = menu->items; *items; items++)
322 (*items)->value = FALSE;
328 Call_Hook(menu, menuinit);
329 Call_Hook(menu, iteminit);
336 /*---------------------------------------------------------------------------
337 | Facility : libnmenu
338 | Function : int unpost_menu(MENU*)
340 | Description : Detach menu from screen
342 | Return Values : E_OK - success
343 | E_BAD_ARGUMENT - not a valid menu pointer
344 | E_BAD_STATE - menu in userexit routine
345 | E_NOT_POSTED - menu is not posted
346 +--------------------------------------------------------------------------*/
348 unpost_menu(MENU *menu)
352 T((T_CALLED("unpost_menu(%p)"), (void *)menu));
355 RETURN(E_BAD_ARGUMENT);
357 if (menu->status & _IN_DRIVER)
360 if (!(menu->status & _POSTED))
361 RETURN(E_NOT_POSTED);
363 Call_Hook(menu, itemterm);
364 Call_Hook(menu, menuterm);
366 win = Get_Menu_Window(menu);
372 menu->sub = (WINDOW *)0;
376 menu->win = (WINDOW *)0;
378 ClrStatus(menu, _POSTED);
383 /* m_post.c ends here */