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 Thomas E. Dickey * |
| 3 | * Copyright 1998-2009,2010 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_new * |
| 36 | * Creation and destruction of new menus * |
| 37 | ***************************************************************************/ |
| 38 | |
| 39 | #include "menu.priv.h" |
| 40 | |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 41 | MODULE_ID("$Id: m_new.c,v 1.27 2021/06/17 21:26:02 tom Exp $") |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 42 | |
| 43 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 44 | | Facility : libnmenu |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 45 | | Function : MENU* _nc_new_menu(SCREEN*, ITEM **items) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 46 | | |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 47 | | Description : Creates a new menu connected to the item pointer |
| 48 | | array items and returns a pointer to the new menu. |
| 49 | | The new menu is initialized with the values from the |
| 50 | | default menu. |
| 51 | | |
| 52 | | Return Values : NULL on error |
| 53 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 54 | MENU_EXPORT(MENU *) |
| 55 | NCURSES_SP_NAME(new_menu) (NCURSES_SP_DCLx ITEM **items) |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 56 | { |
| 57 | int err = E_SYSTEM_ERROR; |
| 58 | MENU *menu = typeCalloc(MENU, 1); |
| 59 | |
| 60 | T((T_CALLED("new_menu(%p,%p)"), (void *)SP_PARM, (void *)items)); |
| 61 | if (menu) |
| 62 | { |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 63 | T((T_CREATE("menu %p"), (void *)menu)); |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 64 | *menu = _nc_Default_Menu; |
| 65 | menu->status = 0; |
| 66 | menu->rows = menu->frows; |
| 67 | menu->cols = menu->fcols; |
| 68 | #if NCURSES_SP_FUNCS |
| 69 | /* This ensures userwin and usersub are always non-null, |
| 70 | so we can derive always the SCREEN that this menu is |
| 71 | running on. */ |
| 72 | menu->userwin = SP_PARM->_stdscr; |
| 73 | menu->usersub = SP_PARM->_stdscr; |
| 74 | #endif |
| 75 | if (items && *items) |
| 76 | { |
| 77 | if (!_nc_Connect_Items(menu, items)) |
| 78 | { |
| 79 | err = E_NOT_CONNECTED; |
| 80 | free(menu); |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 81 | menu = (MENU *)0; |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 82 | } |
| 83 | else |
| 84 | err = E_OK; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (!menu) |
| 89 | SET_ERROR(err); |
| 90 | |
| 91 | returnMenu(menu); |
| 92 | } |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 93 | |
| 94 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 95 | | Facility : libnmenu |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 96 | | Function : MENU *new_menu(ITEM **items) |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 97 | | |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 98 | | Description : Creates a new menu connected to the item pointer |
| 99 | | array items and returns a pointer to the new menu. |
| 100 | | The new menu is initialized with the values from the |
| 101 | | default menu. |
| 102 | | |
| 103 | | Return Values : NULL on error |
| 104 | +--------------------------------------------------------------------------*/ |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 105 | #if NCURSES_SP_FUNCS |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 106 | MENU_EXPORT(MENU *) |
| 107 | new_menu(ITEM **items) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 108 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 109 | return NCURSES_SP_NAME(new_menu) (CURRENT_SCREEN, items); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 110 | } |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 111 | #endif |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 112 | |
| 113 | /*--------------------------------------------------------------------------- |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 114 | | Facility : libnmenu |
| 115 | | Function : int free_menu(MENU *menu) |
| 116 | | |
| 117 | | Description : Disconnects menu from its associated item pointer |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 118 | | array and frees the storage allocated for the menu. |
| 119 | | |
| 120 | | Return Values : E_OK - success |
| 121 | | E_BAD_ARGUMENT - Invalid menu pointer passed |
| 122 | | E_POSTED - Menu is already posted |
| 123 | +--------------------------------------------------------------------------*/ |
micky387 | 9b9f5e7 | 2025-07-08 18:04:53 -0400 | [diff] [blame] | 124 | MENU_EXPORT(int) |
| 125 | free_menu(MENU *menu) |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 126 | { |
Steve Kondik | ae271bc | 2015-11-15 02:50:53 +0100 | [diff] [blame] | 127 | T((T_CALLED("free_menu(%p)"), (void *)menu)); |
Amit Daniel Kachhap | e6a01f5 | 2011-07-20 11:45:59 +0530 | [diff] [blame] | 128 | if (!menu) |
| 129 | RETURN(E_BAD_ARGUMENT); |
| 130 | |
| 131 | if (menu->status & _POSTED) |
| 132 | RETURN(E_POSTED); |
| 133 | |
| 134 | if (menu->items) |
| 135 | _nc_Disconnect_Items(menu); |
| 136 | |
| 137 | if ((menu->status & _MARK_ALLOCATED) && menu->mark) |
| 138 | free(menu->mark); |
| 139 | |
| 140 | free(menu); |
| 141 | RETURN(E_OK); |
| 142 | } |
| 143 | |
| 144 | /* m_new.c ends here */ |