m_new.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /****************************************************************************
  2. * Copyright (c) 1998-2004,2006 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. * Author: Juergen Pfeifer, 1995,1997 *
  30. ****************************************************************************/
  31. /***************************************************************************
  32. * Module m_new *
  33. * Creation and destruction of new menus *
  34. ***************************************************************************/
  35. #include "menu.priv.h"
  36. MODULE_ID("$Id: m_new.c,v 1.18 2006/11/04 19:04:06 tom Exp $")
  37. /*---------------------------------------------------------------------------
  38. | Facility : libnmenu
  39. | Function : MENU *new_menu(ITEM **items)
  40. |
  41. | Description : Creates a new menu connected to the item pointer
  42. | array items and returns a pointer to the new menu.
  43. | The new menu is initialized with the values from the
  44. | default menu.
  45. |
  46. | Return Values : NULL on error
  47. +--------------------------------------------------------------------------*/
  48. NCURSES_EXPORT(MENU *)
  49. new_menu(ITEM ** items)
  50. {
  51. int err = E_SYSTEM_ERROR;
  52. MENU *menu = (MENU *) calloc(1, sizeof(MENU));
  53. T((T_CALLED("new_menu(%p)"), items));
  54. if (menu)
  55. {
  56. *menu = _nc_Default_Menu;
  57. menu->status = 0;
  58. menu->rows = menu->frows;
  59. menu->cols = menu->fcols;
  60. if (items && *items)
  61. {
  62. if (!_nc_Connect_Items(menu, items))
  63. {
  64. err = E_NOT_CONNECTED;
  65. free(menu);
  66. menu = (MENU *) 0;
  67. }
  68. }
  69. }
  70. if (!menu)
  71. SET_ERROR(err);
  72. returnMenu(menu);
  73. }
  74. /*---------------------------------------------------------------------------
  75. | Facility : libnmenu
  76. | Function : int free_menu(MENU *menu)
  77. |
  78. | Description : Disconnects menu from its associated item pointer
  79. | array and frees the storage allocated for the menu.
  80. |
  81. | Return Values : E_OK - success
  82. | E_BAD_ARGUMENT - Invalid menu pointer passed
  83. | E_POSTED - Menu is already posted
  84. +--------------------------------------------------------------------------*/
  85. NCURSES_EXPORT(int)
  86. free_menu(MENU * menu)
  87. {
  88. T((T_CALLED("free_menu(%p)"), menu));
  89. if (!menu)
  90. RETURN(E_BAD_ARGUMENT);
  91. if (menu->status & _POSTED)
  92. RETURN(E_POSTED);
  93. if (menu->items)
  94. _nc_Disconnect_Items(menu);
  95. if ((menu->status & _MARK_ALLOCATED) && menu->mark)
  96. free(menu->mark);
  97. free(menu);
  98. RETURN(E_OK);
  99. }
  100. /* m_new.c ends here */