xref: /freebsd/contrib/ncurses/menu/m_new.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /****************************************************************************
2  * Copyright (c) 1998 Free Software Foundation, Inc.                        *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *   Author: Juergen Pfeifer <juergen.pfeifer@gmx.net> 1995,1997            *
31  ****************************************************************************/
32 
33 /***************************************************************************
34 * Module m_new                                                             *
35 * Creation and destruction of new menus                                    *
36 ***************************************************************************/
37 
38 #include "menu.priv.h"
39 
40 MODULE_ID("$Id: m_new.c,v 1.10 1999/05/16 17:26:59 juergen Exp $")
41 
42 /*---------------------------------------------------------------------------
43 |   Facility      :  libnmenu
44 |   Function      :  MENU *new_menu(ITEM **items)
45 |
46 |   Description   :  Creates a new menu connected to the item pointer
47 |                    array items and returns a pointer to the new menu.
48 |                    The new menu is initialized with the values from the
49 |                    default menu.
50 |
51 |   Return Values :  NULL on error
52 +--------------------------------------------------------------------------*/
53 MENU *new_menu(ITEM ** items)
54 {
55   MENU *menu = (MENU *)calloc(1,sizeof(MENU));
56 
57   if (menu)
58     {
59       *menu = _nc_Default_Menu;
60       menu->status = 0;
61       menu->rows = menu->frows;
62       menu->cols = menu->fcols;
63       if (items && *items)
64 	{
65 	  if (!_nc_Connect_Items(menu,items))
66 	    {
67 	      free(menu);
68 	      menu = (MENU *)0;
69 	    }
70 	}
71     }
72 
73   if (!menu)
74     SET_ERROR(E_SYSTEM_ERROR);
75 
76   return(menu);
77 }
78 
79 /*---------------------------------------------------------------------------
80 |   Facility      :  libnmenu
81 |   Function      :  int free_menu(MENU *menu)
82 |
83 |   Description   :  Disconnects menu from its associated item pointer
84 |                    array and frees the storage allocated for the menu.
85 |
86 |   Return Values :  E_OK               - success
87 |                    E_BAD_ARGUMENT     - Invalid menu pointer passed
88 |                    E_POSTED           - Menu is already posted
89 +--------------------------------------------------------------------------*/
90 int free_menu(MENU * menu)
91 {
92   if (!menu)
93     RETURN(E_BAD_ARGUMENT);
94 
95   if ( menu->status & _POSTED )
96     RETURN(E_POSTED);
97 
98   if (menu->items)
99     _nc_Disconnect_Items(menu);
100 
101   if ((menu->status & _MARK_ALLOCATED) && menu->mark)
102     free(menu->mark);
103 
104   free(menu);
105   RETURN(E_OK);
106 }
107 
108 /* m_new.c ends here */
109