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