1 /* $OpenBSD: m_new.c,v 1.9 2023/10/17 09:52:10 nicm Exp $ */
2
3 /****************************************************************************
4 * Copyright 2020,2021 Thomas E. Dickey *
5 * Copyright 1998-2009,2010 Free Software Foundation, Inc. *
6 * *
7 * Permission is hereby granted, free of charge, to any person obtaining a *
8 * copy of this software and associated documentation files (the *
9 * "Software"), to deal in the Software without restriction, including *
10 * without limitation the rights to use, copy, modify, merge, publish, *
11 * distribute, distribute with modifications, sublicense, and/or sell *
12 * copies of the Software, and to permit persons to whom the Software is *
13 * furnished to do so, subject to the following conditions: *
14 * *
15 * The above copyright notice and this permission notice shall be included *
16 * in all copies or substantial portions of the Software. *
17 * *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
25 * *
26 * Except as contained in this notice, the name(s) of the above copyright *
27 * holders shall not be used in advertising or otherwise to promote the *
28 * sale, use or other dealings in this Software without prior written *
29 * authorization. *
30 ****************************************************************************/
31
32 /****************************************************************************
33 * Author: Juergen Pfeifer, 1995,1997 *
34 ****************************************************************************/
35
36 /***************************************************************************
37 * Module m_new *
38 * Creation and destruction of new menus *
39 ***************************************************************************/
40
41 #include "menu.priv.h"
42
43 MODULE_ID("$Id: m_new.c,v 1.9 2023/10/17 09:52:10 nicm Exp $")
44
45 /*---------------------------------------------------------------------------
46 | Facility : libnmenu
47 | Function : MENU* _nc_new_menu(SCREEN*, ITEM **items)
48 |
49 | Description : Creates a new menu connected to the item pointer
50 | array items and returns a pointer to the new menu.
51 | The new menu is initialized with the values from the
52 | default menu.
53 |
54 | Return Values : NULL on error
55 +--------------------------------------------------------------------------*/
MENU_EXPORT(MENU *)56 MENU_EXPORT(MENU *)
57 NCURSES_SP_NAME(new_menu) (NCURSES_SP_DCLx ITEM **items)
58 {
59 int err = E_SYSTEM_ERROR;
60 MENU *menu = typeCalloc(MENU, 1);
61
62 T((T_CALLED("new_menu(%p,%p)"), (void *)SP_PARM, (void *)items));
63 if (menu)
64 {
65 T((T_CREATE("menu %p"), (void *)menu));
66 *menu = _nc_Default_Menu;
67 menu->status = 0;
68 menu->rows = menu->frows;
69 menu->cols = menu->fcols;
70 #if NCURSES_SP_FUNCS
71 /* This ensures userwin and usersub are always non-null,
72 so we can derive always the SCREEN that this menu is
73 running on. */
74 menu->userwin = SP_PARM->_stdscr;
75 menu->usersub = SP_PARM->_stdscr;
76 #endif
77 if (items && *items)
78 {
79 if (!_nc_Connect_Items(menu, items))
80 {
81 err = E_NOT_CONNECTED;
82 free(menu);
83 menu = (MENU *)0;
84 }
85 else
86 err = E_OK;
87 }
88 }
89
90 if (!menu)
91 SET_ERROR(err);
92
93 returnMenu(menu);
94 }
95
96 /*---------------------------------------------------------------------------
97 | Facility : libnmenu
98 | Function : MENU *new_menu(ITEM **items)
99 |
100 | Description : Creates a new menu connected to the item pointer
101 | array items and returns a pointer to the new menu.
102 | The new menu is initialized with the values from the
103 | default menu.
104 |
105 | Return Values : NULL on error
106 +--------------------------------------------------------------------------*/
107 #if NCURSES_SP_FUNCS
108 MENU_EXPORT(MENU *)
new_menu(ITEM ** items)109 new_menu(ITEM **items)
110 {
111 return NCURSES_SP_NAME(new_menu) (CURRENT_SCREEN, items);
112 }
113 #endif
114
115 /*---------------------------------------------------------------------------
116 | Facility : libnmenu
117 | Function : int free_menu(MENU *menu)
118 |
119 | Description : Disconnects menu from its associated item pointer
120 | array and frees the storage allocated for the menu.
121 |
122 | Return Values : E_OK - success
123 | E_BAD_ARGUMENT - Invalid menu pointer passed
124 | E_POSTED - Menu is already posted
125 +--------------------------------------------------------------------------*/
126 MENU_EXPORT(int)
free_menu(MENU * menu)127 free_menu(MENU *menu)
128 {
129 T((T_CALLED("free_menu(%p)"), (void *)menu));
130 if (!menu)
131 RETURN(E_BAD_ARGUMENT);
132
133 if (menu->status & _POSTED)
134 RETURN(E_POSTED);
135
136 if (menu->items)
137 _nc_Disconnect_Items(menu);
138
139 if ((menu->status & _MARK_ALLOCATED) && menu->mark)
140 free(menu->mark);
141
142 free(menu);
143 RETURN(E_OK);
144 }
145
146 /* m_new.c ends here */
147