1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2012,2014 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 * Globally used internal routines and the default menu and item structures *
37 ***************************************************************************/
39 #include "menu.priv.h"
41 MODULE_ID("$Id: m_global.c,v 1.33 2021/03/27 23:46:29 tom Exp $")
43 static char mark[] = "-";
45 MENU_EXPORT_VAR(MENU) _nc_Default_Menu = {
46 16, /* Nr. of chars high */
47 1, /* Nr. of chars wide */
48 16, /* Nr. of items high */
49 1, /* Nr. of items wide */
50 16, /* Nr. of formatted items high */
51 1, /* Nr. of formatted items wide */
52 16, /* Nr. of items high (actual) */
53 0, /* length of widest name */
54 0, /* length of widest description */
55 1, /* length of mark */
56 1, /* length of one item */
57 1, /* Spacing for descriptor */
58 1, /* Spacing for columns */
59 1, /* Spacing for rows */
60 (char *)0, /* buffer used to store match chars */
61 0, /* Index into pattern buffer */
62 (WINDOW *)0, /* Window containing entire menu */
63 (WINDOW *)0, /* Portion of menu displayed */
64 (WINDOW *)0, /* User's window */
65 (WINDOW *)0, /* User's subwindow */
66 (ITEM **)0, /* List of items */
67 0, /* Total Nr. of items in menu */
68 (ITEM *)0, /* Current item */
69 0, /* Top row of menu */
70 (chtype)A_REVERSE, /* Attribute for selection */
71 (chtype)A_NORMAL, /* Attribute for nonselection */
72 (chtype)A_UNDERLINE, /* Attribute for inactive */
73 ' ', /* Pad character */
74 (Menu_Hook)0, /* Menu init */
75 (Menu_Hook)0, /* Menu term */
76 (Menu_Hook)0, /* Item init */
77 (Menu_Hook)0, /* Item term */
78 (void *)0, /* userptr */
80 ALL_MENU_OPTS, /* options */
84 MENU_EXPORT_VAR(ITEM) _nc_Default_Item = {
85 { (char *)0, 0 }, /* name */
86 { (char *)0, 0 }, /* description */
87 (MENU *)0, /* Pointer to parent menu */
88 (char *)0, /* Userpointer */
89 ALL_ITEM_OPTS, /* options */
95 (ITEM *)0, /* right */
101 /*---------------------------------------------------------------------------
102 | Facility : libnmenu
103 | Function : static void ComputeMaximum_NameDesc_Lenths(MENU *menu)
105 | Description : Calculates the maximum name and description lengths
106 | of the items connected to the menu
109 +--------------------------------------------------------------------------*/
110 NCURSES_INLINE static void
111 ComputeMaximum_NameDesc_Lengths(MENU *menu)
113 unsigned MaximumNameLength = 0;
114 unsigned MaximumDescriptionLength = 0;
117 assert(menu && menu->items);
118 for (items = menu->items; *items; items++)
120 unsigned check = (unsigned)_nc_Calculate_Text_Width(&((*items)->name));
122 if (check > MaximumNameLength)
123 MaximumNameLength = check;
125 check = (unsigned)_nc_Calculate_Text_Width(&((*items)->description));
126 if (check > MaximumDescriptionLength)
127 MaximumDescriptionLength = check;
130 menu->namelen = (short)MaximumNameLength;
131 menu->desclen = (short)MaximumDescriptionLength;
132 T(("ComputeMaximum_NameDesc_Lengths %d,%d", menu->namelen, menu->desclen));
135 /*---------------------------------------------------------------------------
136 | Facility : libnmenu
137 | Function : static void ResetConnectionInfo(MENU *, ITEM **)
139 | Description : Reset all information in the menu and the items in
140 | the item array that indicates a connection
143 +--------------------------------------------------------------------------*/
144 NCURSES_INLINE static void
145 ResetConnectionInfo(MENU *menu, ITEM **items)
149 assert(menu && items);
150 for (item = items; *item; item++)
153 (*item)->imenu = (MENU *)0;
157 menu->pattern = (char *)0;
159 menu->items = (ITEM **)0;
163 /*---------------------------------------------------------------------------
164 | Facility : libnmenu
165 | Function : bool _nc_Connect_Items(MENU *menu, ITEM **items)
167 | Description : Connect the items in the item array to the menu.
168 | Decorate all the items with a number and a backward
169 | pointer to the menu.
171 | Return Values : TRUE - successful connection
172 | FALSE - connection failed
173 +--------------------------------------------------------------------------*/
175 _nc_Connect_Items(MENU *menu, ITEM **items)
177 unsigned int ItemCount = 0;
183 for (item = items; *item; item++)
187 /* if a item is already connected, reject connection */
192 /* we reached the end, so there was no connected item */
194 for (item = items; *item; item++)
196 if (menu->opt & O_ONEVALUE)
198 (*item)->value = FALSE;
200 (*item)->index = (short)ItemCount++;
201 (*item)->imenu = menu;
211 menu->nitems = (short)ItemCount;
212 ComputeMaximum_NameDesc_Lengths(menu);
213 if ((menu->pattern = typeMalloc(char, (unsigned)(1 + menu->namelen))))
216 set_menu_format(menu, menu->frows, menu->fcols);
217 menu->curitem = *items;
223 /* If we fall through to this point, we have to reset all items connection
224 and inform about a reject connection */
225 ResetConnectionInfo(menu, items);
229 /*---------------------------------------------------------------------------
230 | Facility : libnmenu
231 | Function : void _nc_Disconnect_Items(MENU *menu)
233 | Description : Disconnect the menus item array from the menu
236 +--------------------------------------------------------------------------*/
238 _nc_Disconnect_Items(MENU *menu)
240 if (menu && menu->items)
241 ResetConnectionInfo(menu, menu->items);
244 /*---------------------------------------------------------------------------
245 | Facility : libnmenu
246 | Function : int _nc_Calculate_Text_Width(const TEXT * item)
248 | Description : Calculate the number of columns for a TEXT.
250 | Return Values : the width
251 +--------------------------------------------------------------------------*/
253 _nc_Calculate_Text_Width(const TEXT *item /*FIXME: limit length */ )
255 #if USE_WIDEC_SUPPORT
256 int result = item->length;
258 T((T_CALLED("_nc_menu_text_width(%p)"), (const void *)item));
259 if (result != 0 && item->str != 0)
261 int count = (int)mbstowcs(0, item->str, 0);
265 && (temp = typeMalloc(wchar_t, 2 + count)) != 0)
270 mbstowcs(temp, item->str, (unsigned)count);
271 for (n = 0; n < count; ++n)
273 int test = wcwidth(temp[n]);
289 * Calculate the actual width of a menu entry for wide-characters.
291 #if USE_WIDEC_SUPPORT
293 calculate_actual_width(MENU *menu, bool name)
297 assert(menu && menu->items);
299 if (menu->items != 0)
303 for (items = menu->items; *items; items++)
306 ? _nc_Calculate_Text_Width(&((*items)->name))
307 : _nc_Calculate_Text_Width(&((*items)->description)));
315 width = (name ? menu->namelen : menu->desclen);
318 T(("calculate_actual_width %s = %d/%d",
319 name ? "name" : "desc",
321 name ? menu->namelen : menu->desclen));
325 #define calculate_actual_width(menu, name) (name ? menu->namelen : menu->desclen)
328 /*---------------------------------------------------------------------------
329 | Facility : libnmenu
330 | Function : void _nc_Calculate_Item_Length_and_Width(MENU *menu)
332 | Description : Calculate the length of an item and the width of the
336 +--------------------------------------------------------------------------*/
338 _nc_Calculate_Item_Length_and_Width(MENU *menu)
344 menu->height = (short)(1 + menu->spc_rows * (menu->arows - 1));
346 l = calculate_actual_width(menu, TRUE);
349 if ((menu->opt & O_SHOWDESC) && (menu->desclen > 0))
351 l += calculate_actual_width(menu, FALSE);
355 menu->itemlen = (short)l;
357 l += (menu->cols - 1) * menu->spc_cols; /* for the padding between the columns */
358 menu->width = (short)l;
360 T(("_nc_CalculateItem_Length_and_Width columns %d, item %d, width %d",
366 /*---------------------------------------------------------------------------
367 | Facility : libnmenu
368 | Function : void _nc_Link_Item(MENU *menu)
370 | Description : Statically calculate for every item its four neighbors.
371 | This depends on the orientation of the menu. This
372 | static approach simplifies navigation in the menu a lot.
375 +--------------------------------------------------------------------------*/
377 _nc_Link_Items(MENU *menu)
379 if (menu && menu->items && *(menu->items))
383 int Number_Of_Items = menu->nitems;
384 int col = 0, row = 0;
387 bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE;
389 ClrStatus(menu, _LINK_NEEDED);
391 if (menu->opt & O_ROWMAJOR)
393 int Number_Of_Columns = menu->cols;
395 for (i = 0; i < Number_Of_Items; i++)
397 item = menu->items[i];
399 Last_in_Row = row * Number_Of_Columns + (Number_Of_Columns - 1);
402 /* if we are not in the leftmost column, we can use the
403 predecessor in the items array */
405 (cycle ? menu->items[(Last_in_Row >= Number_Of_Items) ?
406 Number_Of_Items - 1 :
410 item->right = ((col < (Number_Of_Columns - 1)) &&
411 ((i + 1) < Number_Of_Items)
414 (cycle ? menu->items[row * Number_Of_Columns] :
418 Last_in_Column = (menu->rows - 1) * Number_Of_Columns + col;
420 item->up = (row) ? menu->items[i - Number_Of_Columns] :
421 (cycle ? menu->items[(Last_in_Column >= Number_Of_Items) ?
422 Number_Of_Items - 1 :
426 item->down = ((i + Number_Of_Columns) < Number_Of_Items)
428 menu->items[i + Number_Of_Columns] :
429 (cycle ? menu->items[(row + 1) < menu->rows ?
430 Number_Of_Items - 1 : col] :
432 item->x = (short)col;
433 item->y = (short)row;
434 if (++col == Number_Of_Columns)
443 int Number_Of_Rows = menu->rows;
446 for (j = 0; j < Number_Of_Items; j++)
448 item = menu->items[i = (col * Number_Of_Rows + row)];
450 Last_in_Column = (menu->cols - 1) * Number_Of_Rows + row;
453 menu->items[i - Number_Of_Rows] :
454 (cycle ? (Last_in_Column >= Number_Of_Items) ?
455 menu->items[Last_in_Column - Number_Of_Rows] :
456 menu->items[Last_in_Column] :
459 item->right = ((i + Number_Of_Rows) < Number_Of_Items)
461 menu->items[i + Number_Of_Rows] :
462 (cycle ? menu->items[row] : (ITEM *)0);
464 Last_in_Row = col * Number_Of_Rows + (Number_Of_Rows - 1);
469 menu->items[(Last_in_Row >= Number_Of_Items) ?
470 Number_Of_Items - 1 :
474 item->down = (row < (Number_Of_Rows - 1))
476 (menu->items[((i + 1) < Number_Of_Items) ?
478 (col - 1) * Number_Of_Rows + row + 1]) :
480 menu->items[col * Number_Of_Rows] :
484 item->x = (short)col;
485 item->y = (short)row;
486 if ((++row) == Number_Of_Rows)
496 /*---------------------------------------------------------------------------
497 | Facility : libnmenu
498 | Function : void _nc_Show_Menu(const MENU* menu)
500 | Description : Update the window that is associated with the menu
503 +--------------------------------------------------------------------------*/
505 _nc_Show_Menu(const MENU *menu)
508 if ((menu->status & _POSTED) && !(menu->status & _IN_DRIVER))
513 /* adjust the internal subwindow to start on the current top */
515 mvderwin(menu->sub, menu->spc_rows * menu->toprow, 0);
516 win = Get_Menu_Window(menu);
521 if (menu->height < maxy)
523 if (menu->width < maxx)
526 copywin(menu->sub, win, 0, 0, 0, 0, maxy - 1, maxx - 1, 0);
527 pos_menu_cursor(menu);
531 /*---------------------------------------------------------------------------
532 | Facility : libnmenu
533 | Function : void _nc_New_TopRow_and_CurrentItem(
536 | ITEM *new_current_item)
538 | Description : Redisplay the menu so that the given row becomes the
539 | top row and the given item becomes the new current
543 +--------------------------------------------------------------------------*/
545 _nc_New_TopRow_and_CurrentItem(
548 ITEM *new_current_item)
551 if (menu->status & _POSTED)
554 bool mterm_called = FALSE;
555 bool iterm_called = FALSE;
557 if (new_current_item != menu->curitem)
559 Call_Hook(menu, itemterm);
562 if (new_toprow != menu->toprow)
564 Call_Hook(menu, menuterm);
568 cur_item = menu->curitem;
570 menu->toprow = (short)(((menu->rows - menu->frows) >= 0)
571 ? min(menu->rows - menu->frows, new_toprow)
573 menu->curitem = new_current_item;
577 Call_Hook(menu, menuinit);
581 /* this means, move from the old current_item to the new one... */
582 Move_To_Current_Item(menu, cur_item);
583 Call_Hook(menu, iteminit);
585 if (mterm_called || iterm_called)
590 pos_menu_cursor(menu);
593 { /* if we are not posted, this is quite simple */
594 menu->toprow = (short)(((menu->rows - menu->frows) >= 0)
595 ? min(menu->rows - menu->frows, new_toprow)
597 menu->curitem = new_current_item;
601 /* m_global.c ends here */