m_item_new.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /****************************************************************************
  2. * Copyright (c) 1998-2005,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_item_new *
  33. * Create and destroy menu items *
  34. * Set and get marker string for menu *
  35. ***************************************************************************/
  36. #include "menu.priv.h"
  37. #if USE_WIDEC_SUPPORT
  38. #if HAVE_WCTYPE_H
  39. #include <wctype.h>
  40. #endif
  41. #endif
  42. MODULE_ID("$Id: m_item_new.c,v 1.27 2006/12/17 19:47:09 tom Exp $")
  43. /*---------------------------------------------------------------------------
  44. | Facility : libnmenu
  45. | Function : bool Is_Printable_String(const char *s)
  46. |
  47. | Description : Checks whether or not the string contains only printable
  48. | characters.
  49. |
  50. | Return Values : TRUE - if string is printable
  51. | FALSE - if string contains non-printable characters
  52. +--------------------------------------------------------------------------*/
  53. static bool
  54. Is_Printable_String(const char *s)
  55. {
  56. int result = TRUE;
  57. #if USE_WIDEC_SUPPORT
  58. int count = mbstowcs(0, s, 0);
  59. wchar_t *temp = 0;
  60. assert(s);
  61. if (count > 0
  62. && (temp = typeCalloc(wchar_t, (2 + (unsigned)count))) != 0)
  63. {
  64. int n;
  65. mbstowcs(temp, s, (unsigned)count);
  66. for (n = 0; n < count; ++n)
  67. if (!iswprint((wint_t) temp[n]))
  68. {
  69. result = FALSE;
  70. break;
  71. }
  72. free(temp);
  73. }
  74. #else
  75. assert(s);
  76. while (*s)
  77. {
  78. if (!isprint(UChar(*s)))
  79. {
  80. result = FALSE;
  81. break;
  82. }
  83. s++;
  84. }
  85. #endif
  86. return result;
  87. }
  88. /*---------------------------------------------------------------------------
  89. | Facility : libnmenu
  90. | Function : ITEM *new_item(char *name, char *description)
  91. |
  92. | Description : Create a new item with name and description. Return
  93. | a pointer to this new item.
  94. | N.B.: an item must(!) have a name.
  95. |
  96. | Return Values : The item pointer or NULL if creation failed.
  97. +--------------------------------------------------------------------------*/
  98. NCURSES_EXPORT(ITEM *)
  99. new_item(const char *name, const char *description)
  100. {
  101. ITEM *item;
  102. T((T_CALLED("new_item(\"%s\", \"%s\")"),
  103. name ? name : "",
  104. description ? description : ""));
  105. if (!name || (*name == '\0') || !Is_Printable_String(name))
  106. {
  107. item = (ITEM *) 0;
  108. SET_ERROR(E_BAD_ARGUMENT);
  109. }
  110. else
  111. {
  112. item = (ITEM *) calloc(1, sizeof(ITEM));
  113. if (item)
  114. {
  115. *item = _nc_Default_Item; /* hope we have struct assignment */
  116. item->name.length = strlen(name);
  117. item->name.str = name;
  118. if (description && (*description != '\0') &&
  119. Is_Printable_String(description))
  120. {
  121. item->description.length = strlen(description);
  122. item->description.str = description;
  123. }
  124. else
  125. {
  126. item->description.length = 0;
  127. item->description.str = (char *)0;
  128. }
  129. }
  130. else
  131. SET_ERROR(E_SYSTEM_ERROR);
  132. }
  133. returnItem(item);
  134. }
  135. /*---------------------------------------------------------------------------
  136. | Facility : libnmenu
  137. | Function : int free_item(ITEM *item)
  138. |
  139. | Description : Free the allocated storage for this item.
  140. | N.B.: a connected item can't be freed.
  141. |
  142. | Return Values : E_OK - success
  143. | E_BAD_ARGUMENT - invalid value has been passed
  144. | E_CONNECTED - item is still connected to a menu
  145. +--------------------------------------------------------------------------*/
  146. NCURSES_EXPORT(int)
  147. free_item(ITEM * item)
  148. {
  149. T((T_CALLED("free_item(%p)"), item));
  150. if (!item)
  151. RETURN(E_BAD_ARGUMENT);
  152. if (item->imenu)
  153. RETURN(E_CONNECTED);
  154. free(item);
  155. RETURN(E_OK);
  156. }
  157. /*---------------------------------------------------------------------------
  158. | Facility : libnmenu
  159. | Function : int set_menu_mark( MENU *menu, const char *mark )
  160. |
  161. | Description : Set the mark string used to indicate the current
  162. | item (single-valued menu) or the selected items
  163. | (multi-valued menu).
  164. | The mark argument may be NULL, in which case no
  165. | marker is used.
  166. | This might be a little bit tricky, because this may
  167. | affect the geometry of the menu, which we don't allow
  168. | if it is already posted.
  169. |
  170. | Return Values : E_OK - success
  171. | E_BAD_ARGUMENT - an invalid value has been passed
  172. | E_SYSTEM_ERROR - no memory to store mark
  173. +--------------------------------------------------------------------------*/
  174. NCURSES_EXPORT(int)
  175. set_menu_mark(MENU * menu, const char *mark)
  176. {
  177. unsigned l;
  178. T((T_CALLED("set_menu_mark(%p,%s)"), menu, _nc_visbuf(mark)));
  179. if (mark && (*mark != '\0') && Is_Printable_String(mark))
  180. l = strlen(mark);
  181. else
  182. l = 0;
  183. if (menu)
  184. {
  185. char *old_mark = menu->mark;
  186. unsigned short old_status = menu->status;
  187. if (menu->status & _POSTED)
  188. {
  189. /* If the menu is already posted, the geometry is fixed. Then
  190. we can only accept a mark with exactly the same length */
  191. if (menu->marklen != (int)l)
  192. RETURN(E_BAD_ARGUMENT);
  193. }
  194. menu->marklen = l;
  195. if (l)
  196. {
  197. menu->mark = (char *)malloc(l + 1);
  198. if (menu->mark)
  199. {
  200. strcpy(menu->mark, mark);
  201. if (menu != &_nc_Default_Menu)
  202. menu->status |= _MARK_ALLOCATED;
  203. }
  204. else
  205. {
  206. menu->mark = old_mark;
  207. RETURN(E_SYSTEM_ERROR);
  208. }
  209. }
  210. else
  211. menu->mark = (char *)0;
  212. if ((old_status & _MARK_ALLOCATED) && old_mark)
  213. free(old_mark);
  214. if (menu->status & _POSTED)
  215. {
  216. _nc_Draw_Menu(menu);
  217. _nc_Show_Menu(menu);
  218. }
  219. else
  220. {
  221. /* Recalculate the geometry */
  222. _nc_Calculate_Item_Length_and_Width(menu);
  223. }
  224. }
  225. else
  226. {
  227. returnCode(set_menu_mark(&_nc_Default_Menu, mark));
  228. }
  229. RETURN(E_OK);
  230. }
  231. /*---------------------------------------------------------------------------
  232. | Facility : libnmenu
  233. | Function : char *menu_mark(const MENU *menu)
  234. |
  235. | Description : Return a pointer to the marker string
  236. |
  237. | Return Values : The marker string pointer or NULL if no marker defined
  238. +--------------------------------------------------------------------------*/
  239. NCURSES_EXPORT(const char *)
  240. menu_mark(const MENU * menu)
  241. {
  242. T((T_CALLED("menu_mark(%p)"), menu));
  243. returnPtr(Normalize_Menu(menu)->mark);
  244. }
  245. /* m_item_new.c */