xref: /freebsd/contrib/ncurses/menu/m_new.c (revision c66ec88fed842fbaad62c30d510644ceb7bd2d71)
1 /****************************************************************************
2  * Copyright 2020 Thomas E. Dickey                                          *
3  * Copyright 1998-2009,2010 Free Software Foundation, Inc.                  *
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 
41 MODULE_ID("$Id: m_new.c,v 1.22 2020/02/02 23:34:34 tom Exp $")
42 
43 /*---------------------------------------------------------------------------
44 |   Facility      :  libnmenu
45 |   Function      :  MENU* _nc_new_menu(SCREEN*, ITEM **items)
46 |
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 +--------------------------------------------------------------------------*/
54 NCURSES_EXPORT(MENU *)
55 NCURSES_SP_NAME(new_menu) (NCURSES_SP_DCLx ITEM ** items)
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     {
63       *menu = _nc_Default_Menu;
64       menu->status = 0;
65       menu->rows = menu->frows;
66       menu->cols = menu->fcols;
67 #if NCURSES_SP_FUNCS
68       /* This ensures userwin and usersub are always non-null,
69          so we can derive always the SCREEN that this menu is
70          running on. */
71       menu->userwin = SP_PARM->_stdscr;
72       menu->usersub = SP_PARM->_stdscr;
73 #endif
74       if (items && *items)
75 	{
76 	  if (!_nc_Connect_Items(menu, items))
77 	    {
78 	      err = E_NOT_CONNECTED;
79 	      free(menu);
80 	      menu = (MENU *) 0;
81 	    }
82 	  else
83 	    err = E_OK;
84 	}
85     }
86 
87   if (!menu)
88     SET_ERROR(err);
89 
90   returnMenu(menu);
91 }
92 
93 /*---------------------------------------------------------------------------
94 |   Facility      :  libnmenu
95 |   Function      :  MENU *new_menu(ITEM **items)
96 |
97 |   Description   :  Creates a new menu connected to the item pointer
98 |                    array items and returns a pointer to the new menu.
99 |                    The new menu is initialized with the values from the
100 |                    default menu.
101 |
102 |   Return Values :  NULL on error
103 +--------------------------------------------------------------------------*/
104 #if NCURSES_SP_FUNCS
105 NCURSES_EXPORT(MENU *)
106 new_menu(ITEM ** items)
107 {
108   return NCURSES_SP_NAME(new_menu) (CURRENT_SCREEN, items);
109 }
110 #endif
111 
112 /*---------------------------------------------------------------------------
113 |   Facility      :  libnmenu
114 |   Function      :  int free_menu(MENU *menu)
115 |
116 |   Description   :  Disconnects menu from its associated item pointer
117 |                    array and frees the storage allocated for the menu.
118 |
119 |   Return Values :  E_OK               - success
120 |                    E_BAD_ARGUMENT     - Invalid menu pointer passed
121 |                    E_POSTED           - Menu is already posted
122 +--------------------------------------------------------------------------*/
123 NCURSES_EXPORT(int)
124 free_menu(MENU * menu)
125 {
126   T((T_CALLED("free_menu(%p)"), (void *)menu));
127   if (!menu)
128     RETURN(E_BAD_ARGUMENT);
129 
130   if (menu->status & _POSTED)
131     RETURN(E_POSTED);
132 
133   if (menu->items)
134     _nc_Disconnect_Items(menu);
135 
136   if ((menu->status & _MARK_ALLOCATED) && menu->mark)
137     free(menu->mark);
138 
139   free(menu);
140   RETURN(E_OK);
141 }
142 
143 /* m_new.c ends here */
144