Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 1 | /**************************************************************************** |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 2 | * Copyright 2020-2021,2023 Thomas E. Dickey * |
| 3 | * Copyright 1998-2012,2014 Free Software Foundation, Inc. * |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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_global * |
| 36 | * Globally used internal routines and the default menu and item structures * |
| 37 | ***************************************************************************/ |
| 38 | |
| 39 | #include "menu.priv.h" |
| 40 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 41 | MODULE_ID("$Id: m_global.c,v 1.34 2023/09/16 16:39:26 tom Exp $") |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 42 | |
| 43 | static char mark[] = "-"; |
| 44 | /* *INDENT-OFF* */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 45 | MENU_EXPORT_VAR(MENU) _nc_Default_Menu = { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 57 | 1, /* Spacing for descriptor */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 72 | (chtype)A_UNDERLINE, /* Attribute for inactive */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 */ |
| 79 | mark, /* mark */ |
| 80 | ALL_MENU_OPTS, /* options */ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 81 | 0 /* status */ |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 82 | }; |
| 83 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 84 | MENU_EXPORT_VAR(ITEM) _nc_Default_Item = { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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 */ |
| 90 | 0, /* Item Nr. */ |
| 91 | 0, /* y */ |
| 92 | 0, /* x */ |
| 93 | FALSE, /* value */ |
| 94 | (ITEM *)0, /* left */ |
| 95 | (ITEM *)0, /* right */ |
| 96 | (ITEM *)0, /* up */ |
| 97 | (ITEM *)0 /* down */ |
| 98 | }; |
| 99 | /* *INDENT-ON* */ |
| 100 | |
| 101 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 102 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 103 | | Function : static void ComputeMaximum_NameDesc_Lenths(MENU *menu) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 104 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 105 | | Description : Calculates the maximum name and description lengths |
| 106 | | of the items connected to the menu |
| 107 | | |
| 108 | | Return Values : - |
| 109 | +--------------------------------------------------------------------------*/ |
| 110 | NCURSES_INLINE static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 111 | ComputeMaximum_NameDesc_Lengths(MENU *menu) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 112 | { |
| 113 | unsigned MaximumNameLength = 0; |
| 114 | unsigned MaximumDescriptionLength = 0; |
| 115 | ITEM **items; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 116 | |
| 117 | assert(menu && menu->items); |
| 118 | for (items = menu->items; *items; items++) |
| 119 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 120 | unsigned check = (unsigned)_nc_Calculate_Text_Width(&((*items)->name)); |
| 121 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 122 | if (check > MaximumNameLength) |
| 123 | MaximumNameLength = check; |
| 124 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 125 | check = (unsigned)_nc_Calculate_Text_Width(&((*items)->description)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 126 | if (check > MaximumDescriptionLength) |
| 127 | MaximumDescriptionLength = check; |
| 128 | } |
| 129 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 130 | menu->namelen = (short)MaximumNameLength; |
| 131 | menu->desclen = (short)MaximumDescriptionLength; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 132 | T(("ComputeMaximum_NameDesc_Lengths %d,%d", menu->namelen, menu->desclen)); |
| 133 | } |
| 134 | |
| 135 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 136 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 137 | | Function : static void ResetConnectionInfo(MENU *, ITEM **) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 138 | | |
| 139 | | Description : Reset all information in the menu and the items in |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 140 | | the item array that indicates a connection |
| 141 | | |
| 142 | | Return Values : - |
| 143 | +--------------------------------------------------------------------------*/ |
| 144 | NCURSES_INLINE static void |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 145 | ResetConnectionInfo(MENU *menu, ITEM **items) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 146 | { |
| 147 | ITEM **item; |
| 148 | |
| 149 | assert(menu && items); |
| 150 | for (item = items; *item; item++) |
| 151 | { |
| 152 | (*item)->index = 0; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 153 | (*item)->imenu = (MENU *)0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 154 | } |
| 155 | if (menu->pattern) |
| 156 | free(menu->pattern); |
| 157 | menu->pattern = (char *)0; |
| 158 | menu->pindex = 0; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 159 | menu->items = (ITEM **)0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 160 | menu->nitems = 0; |
| 161 | } |
| 162 | |
| 163 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 164 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 165 | | Function : bool _nc_Connect_Items(MENU *menu, ITEM **items) |
| 166 | | |
| 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. |
| 170 | | |
| 171 | | Return Values : TRUE - successful connection |
| 172 | | FALSE - connection failed |
| 173 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 174 | MENU_EXPORT(bool) |
| 175 | _nc_Connect_Items(MENU *menu, ITEM **items) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 176 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 177 | unsigned int ItemCount = 0; |
| 178 | |
| 179 | if (menu && items) |
| 180 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 181 | ITEM **item; |
| 182 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 183 | for (item = items; *item; item++) |
| 184 | { |
| 185 | if ((*item)->imenu) |
| 186 | { |
| 187 | /* if a item is already connected, reject connection */ |
| 188 | break; |
| 189 | } |
| 190 | } |
| 191 | if (!(*item)) |
| 192 | /* we reached the end, so there was no connected item */ |
| 193 | { |
| 194 | for (item = items; *item; item++) |
| 195 | { |
| 196 | if (menu->opt & O_ONEVALUE) |
| 197 | { |
| 198 | (*item)->value = FALSE; |
| 199 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 200 | (*item)->index = (short)ItemCount++; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 201 | (*item)->imenu = menu; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | else |
| 206 | return (FALSE); |
| 207 | |
| 208 | if (ItemCount != 0) |
| 209 | { |
| 210 | menu->items = items; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 211 | menu->nitems = (short)ItemCount; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 212 | ComputeMaximum_NameDesc_Lengths(menu); |
| 213 | if ((menu->pattern = typeMalloc(char, (unsigned)(1 + menu->namelen)))) |
| 214 | { |
| 215 | Reset_Pattern(menu); |
| 216 | set_menu_format(menu, menu->frows, menu->fcols); |
| 217 | menu->curitem = *items; |
| 218 | menu->toprow = 0; |
| 219 | return (TRUE); |
| 220 | } |
| 221 | } |
| 222 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 223 | /* If we fall through to this point, we have to reset all items connection |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 224 | and inform about a reject connection */ |
| 225 | ResetConnectionInfo(menu, items); |
| 226 | return (FALSE); |
| 227 | } |
| 228 | |
| 229 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 230 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 231 | | Function : void _nc_Disconnect_Items(MENU *menu) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 232 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 233 | | Description : Disconnect the menus item array from the menu |
| 234 | | |
| 235 | | Return Values : - |
| 236 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 237 | MENU_EXPORT(void) |
| 238 | _nc_Disconnect_Items(MENU *menu) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 239 | { |
| 240 | if (menu && menu->items) |
| 241 | ResetConnectionInfo(menu, menu->items); |
| 242 | } |
| 243 | |
| 244 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 245 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 246 | | Function : int _nc_Calculate_Text_Width(const TEXT * item) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 247 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 248 | | Description : Calculate the number of columns for a TEXT. |
| 249 | | |
| 250 | | Return Values : the width |
| 251 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 252 | MENU_EXPORT(int) |
| 253 | _nc_Calculate_Text_Width(const TEXT *item /*FIXME: limit length */ ) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 254 | { |
| 255 | #if USE_WIDEC_SUPPORT |
| 256 | int result = item->length; |
| 257 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 258 | T((T_CALLED("_nc_menu_text_width(%p)"), (const void *)item)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 259 | if (result != 0 && item->str != 0) |
| 260 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 261 | int count = (int)mbstowcs(0, item->str, 0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 262 | wchar_t *temp = 0; |
| 263 | |
| 264 | if (count > 0 |
| 265 | && (temp = typeMalloc(wchar_t, 2 + count)) != 0) |
| 266 | { |
| 267 | int n; |
| 268 | |
| 269 | result = 0; |
| 270 | mbstowcs(temp, item->str, (unsigned)count); |
| 271 | for (n = 0; n < count; ++n) |
| 272 | { |
| 273 | int test = wcwidth(temp[n]); |
| 274 | |
| 275 | if (test <= 0) |
| 276 | test = 1; |
| 277 | result += test; |
| 278 | } |
| 279 | free(temp); |
| 280 | } |
| 281 | } |
| 282 | returnCode(result); |
| 283 | #else |
| 284 | return item->length; |
| 285 | #endif |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Calculate the actual width of a menu entry for wide-characters. |
| 290 | */ |
| 291 | #if USE_WIDEC_SUPPORT |
| 292 | static int |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 293 | calculate_actual_width(MENU *menu, bool name) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 294 | { |
| 295 | int width = 0; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 296 | |
| 297 | assert(menu && menu->items); |
| 298 | |
| 299 | if (menu->items != 0) |
| 300 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 301 | ITEM **items; |
| 302 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 303 | for (items = menu->items; *items; items++) |
| 304 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 305 | int check = (name |
| 306 | ? _nc_Calculate_Text_Width(&((*items)->name)) |
| 307 | : _nc_Calculate_Text_Width(&((*items)->description))); |
| 308 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 309 | if (check > width) |
| 310 | width = check; |
| 311 | } |
| 312 | } |
| 313 | else |
| 314 | { |
| 315 | width = (name ? menu->namelen : menu->desclen); |
| 316 | } |
| 317 | |
| 318 | T(("calculate_actual_width %s = %d/%d", |
| 319 | name ? "name" : "desc", |
| 320 | width, |
| 321 | name ? menu->namelen : menu->desclen)); |
| 322 | return width; |
| 323 | } |
| 324 | #else |
| 325 | #define calculate_actual_width(menu, name) (name ? menu->namelen : menu->desclen) |
| 326 | #endif |
| 327 | |
| 328 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 329 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 330 | | Function : void _nc_Calculate_Item_Length_and_Width(MENU *menu) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 331 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 332 | | Description : Calculate the length of an item and the width of the |
| 333 | | whole menu. |
| 334 | | |
| 335 | | Return Values : - |
| 336 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 337 | MENU_EXPORT(void) |
| 338 | _nc_Calculate_Item_Length_and_Width(MENU *menu) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 339 | { |
| 340 | int l; |
| 341 | |
| 342 | assert(menu); |
| 343 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 344 | menu->height = (short)(1 + menu->spc_rows * (menu->arows - 1)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 345 | |
| 346 | l = calculate_actual_width(menu, TRUE); |
| 347 | l += menu->marklen; |
| 348 | |
| 349 | if ((menu->opt & O_SHOWDESC) && (menu->desclen > 0)) |
| 350 | { |
| 351 | l += calculate_actual_width(menu, FALSE); |
| 352 | l += menu->spc_desc; |
| 353 | } |
| 354 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 355 | menu->itemlen = (short)l; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 356 | l *= menu->cols; |
| 357 | l += (menu->cols - 1) * menu->spc_cols; /* for the padding between the columns */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 358 | menu->width = (short)l; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 359 | |
| 360 | T(("_nc_CalculateItem_Length_and_Width columns %d, item %d, width %d", |
| 361 | menu->cols, |
| 362 | menu->itemlen, |
| 363 | menu->width)); |
| 364 | } |
| 365 | |
| 366 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 367 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 368 | | Function : void _nc_Link_Item(MENU *menu) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 369 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 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. |
| 373 | | |
| 374 | | Return Values : - |
| 375 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 376 | MENU_EXPORT(void) |
| 377 | _nc_Link_Items(MENU *menu) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 378 | { |
| 379 | if (menu && menu->items && *(menu->items)) |
| 380 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 381 | int i; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 382 | ITEM *item; |
| 383 | int Number_Of_Items = menu->nitems; |
| 384 | int col = 0, row = 0; |
| 385 | int Last_in_Row; |
| 386 | int Last_in_Column; |
| 387 | bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE; |
| 388 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 389 | ClrStatus(menu, _LINK_NEEDED); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 390 | |
| 391 | if (menu->opt & O_ROWMAJOR) |
| 392 | { |
| 393 | int Number_Of_Columns = menu->cols; |
| 394 | |
| 395 | for (i = 0; i < Number_Of_Items; i++) |
| 396 | { |
| 397 | item = menu->items[i]; |
| 398 | |
| 399 | Last_in_Row = row * Number_Of_Columns + (Number_Of_Columns - 1); |
| 400 | |
| 401 | item->left = (col) ? |
| 402 | /* if we are not in the leftmost column, we can use the |
| 403 | predecessor in the items array */ |
| 404 | menu->items[i - 1] : |
| 405 | (cycle ? menu->items[(Last_in_Row >= Number_Of_Items) ? |
| 406 | Number_Of_Items - 1 : |
| 407 | Last_in_Row] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 408 | (ITEM *)0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 409 | |
| 410 | item->right = ((col < (Number_Of_Columns - 1)) && |
| 411 | ((i + 1) < Number_Of_Items) |
| 412 | )? |
| 413 | menu->items[i + 1] : |
| 414 | (cycle ? menu->items[row * Number_Of_Columns] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 415 | (ITEM *)0 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 416 | ); |
| 417 | |
| 418 | Last_in_Column = (menu->rows - 1) * Number_Of_Columns + col; |
| 419 | |
| 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 : |
| 423 | Last_in_Column] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 424 | (ITEM *)0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 425 | |
| 426 | item->down = ((i + Number_Of_Columns) < Number_Of_Items) |
| 427 | ? |
| 428 | menu->items[i + Number_Of_Columns] : |
| 429 | (cycle ? menu->items[(row + 1) < menu->rows ? |
| 430 | Number_Of_Items - 1 : col] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 431 | (ITEM *)0); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 432 | item->x = (short)col; |
| 433 | item->y = (short)row; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 434 | if (++col == Number_Of_Columns) |
| 435 | { |
| 436 | row++; |
| 437 | col = 0; |
| 438 | } |
| 439 | } |
| 440 | } |
| 441 | else |
| 442 | { |
| 443 | int Number_Of_Rows = menu->rows; |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 444 | int j; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 445 | |
| 446 | for (j = 0; j < Number_Of_Items; j++) |
| 447 | { |
| 448 | item = menu->items[i = (col * Number_Of_Rows + row)]; |
| 449 | |
| 450 | Last_in_Column = (menu->cols - 1) * Number_Of_Rows + row; |
| 451 | |
| 452 | item->left = (col) ? |
| 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] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 457 | (ITEM *)0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 458 | |
| 459 | item->right = ((i + Number_Of_Rows) < Number_Of_Items) |
| 460 | ? |
| 461 | menu->items[i + Number_Of_Rows] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 462 | (cycle ? menu->items[row] : (ITEM *)0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 463 | |
| 464 | Last_in_Row = col * Number_Of_Rows + (Number_Of_Rows - 1); |
| 465 | |
| 466 | item->up = (row) ? |
| 467 | menu->items[i - 1] : |
| 468 | (cycle ? |
| 469 | menu->items[(Last_in_Row >= Number_Of_Items) ? |
| 470 | Number_Of_Items - 1 : |
| 471 | Last_in_Row] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 472 | (ITEM *)0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 473 | |
| 474 | item->down = (row < (Number_Of_Rows - 1)) |
| 475 | ? |
| 476 | (menu->items[((i + 1) < Number_Of_Items) ? |
| 477 | i + 1 : |
| 478 | (col - 1) * Number_Of_Rows + row + 1]) : |
| 479 | (cycle ? |
| 480 | menu->items[col * Number_Of_Rows] : |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 481 | (ITEM *)0 |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 482 | ); |
| 483 | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 484 | item->x = (short)col; |
| 485 | item->y = (short)row; |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 486 | if ((++row) == Number_Of_Rows) |
| 487 | { |
| 488 | col++; |
| 489 | row = 0; |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 497 | | Facility : libnmenu |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 498 | | Function : void _nc_Show_Menu(const MENU* menu) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 499 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 500 | | Description : Update the window that is associated with the menu |
| 501 | | |
| 502 | | Return Values : - |
| 503 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 504 | MENU_EXPORT(void) |
| 505 | _nc_Show_Menu(const MENU *menu) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 506 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 507 | assert(menu); |
| 508 | if ((menu->status & _POSTED) && !(menu->status & _IN_DRIVER)) |
| 509 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 510 | WINDOW *win; |
| 511 | int maxy, maxx; |
| 512 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 513 | /* adjust the internal subwindow to start on the current top */ |
| 514 | assert(menu->sub); |
| 515 | mvderwin(menu->sub, menu->spc_rows * menu->toprow, 0); |
| 516 | win = Get_Menu_Window(menu); |
| 517 | |
| 518 | maxy = getmaxy(win); |
| 519 | maxx = getmaxx(win); |
| 520 | |
| 521 | if (menu->height < maxy) |
| 522 | maxy = menu->height; |
| 523 | if (menu->width < maxx) |
| 524 | maxx = menu->width; |
| 525 | |
| 526 | copywin(menu->sub, win, 0, 0, 0, 0, maxy - 1, maxx - 1, 0); |
| 527 | pos_menu_cursor(menu); |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 532 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 533 | | Function : void _nc_New_TopRow_and_CurrentItem( |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 534 | | MENU *menu, |
| 535 | | int new_toprow, |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 536 | | ITEM *new_current_item) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 537 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 538 | | Description : Redisplay the menu so that the given row becomes the |
| 539 | | top row and the given item becomes the new current |
| 540 | | item. |
| 541 | | |
| 542 | | Return Values : - |
| 543 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 544 | MENU_EXPORT(void) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 545 | _nc_New_TopRow_and_CurrentItem( |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 546 | MENU *menu, |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 547 | int new_toprow, |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 548 | ITEM *new_current_item) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 549 | { |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 550 | assert(menu); |
| 551 | if (menu->status & _POSTED) |
| 552 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 553 | ITEM *cur_item; |
| 554 | bool mterm_called = FALSE; |
| 555 | bool iterm_called = FALSE; |
| 556 | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 557 | if (new_current_item != menu->curitem) |
| 558 | { |
| 559 | Call_Hook(menu, itemterm); |
| 560 | iterm_called = TRUE; |
| 561 | } |
| 562 | if (new_toprow != menu->toprow) |
| 563 | { |
| 564 | Call_Hook(menu, menuterm); |
| 565 | mterm_called = TRUE; |
| 566 | } |
| 567 | |
| 568 | cur_item = menu->curitem; |
| 569 | assert(cur_item); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 570 | menu->toprow = (short)(((menu->rows - menu->frows) >= 0) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 571 | ? Min(menu->rows - menu->frows, new_toprow) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 572 | : 0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 573 | menu->curitem = new_current_item; |
| 574 | |
| 575 | if (mterm_called) |
| 576 | { |
| 577 | Call_Hook(menu, menuinit); |
| 578 | } |
| 579 | if (iterm_called) |
| 580 | { |
| 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); |
| 584 | } |
| 585 | if (mterm_called || iterm_called) |
| 586 | { |
| 587 | _nc_Show_Menu(menu); |
| 588 | } |
| 589 | else |
| 590 | pos_menu_cursor(menu); |
| 591 | } |
| 592 | else |
| 593 | { /* if we are not posted, this is quite simple */ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 594 | menu->toprow = (short)(((menu->rows - menu->frows) >= 0) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 595 | ? Min(menu->rows - menu->frows, new_toprow) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 596 | : 0); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 597 | menu->curitem = new_current_item; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* m_global.c ends here */ |