1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 ****************************************************************************/
33 /***************************************************************************
35 * Create and destroy menu items *
36 * Set and get marker string for menu *
37 ***************************************************************************/
39 #include "menu.priv.h"
47 MODULE_ID("$Id: m_item_new.c,v 1.30 2010/01/23 21:20:11 tom Exp $")
49 /*---------------------------------------------------------------------------
51 | Function : bool Is_Printable_String(const char *s)
53 | Description : Checks whether or not the string contains only printable
56 | Return Values : TRUE - if string is printable
57 | FALSE - if string contains non-printable characters
58 +--------------------------------------------------------------------------*/
60 Is_Printable_String(const char *s)
65 int count = mbstowcs(0, s, 0);
71 && (temp = typeCalloc(wchar_t, (2 + (unsigned)count))) != 0)
75 mbstowcs(temp, s, (unsigned)count);
76 for (n = 0; n < count; ++n)
77 if (!iswprint((wint_t) temp[n]))
88 if (!isprint(UChar(*s)))
99 /*---------------------------------------------------------------------------
100 | Facility : libnmenu
101 | Function : ITEM *new_item(char *name, char *description)
103 | Description : Create a new item with name and description. Return
104 | a pointer to this new item.
105 | N.B.: an item must(!) have a name.
107 | Return Values : The item pointer or NULL if creation failed.
108 +--------------------------------------------------------------------------*/
109 NCURSES_EXPORT(ITEM *)
110 new_item(const char *name, const char *description)
114 T((T_CALLED("new_item(\"%s\", \"%s\")"),
116 description ? description : ""));
118 if (!name || (*name == '\0') || !Is_Printable_String(name))
121 SET_ERROR(E_BAD_ARGUMENT);
125 item = typeCalloc(ITEM, 1);
128 *item = _nc_Default_Item; /* hope we have struct assignment */
130 item->name.length = strlen(name);
131 item->name.str = name;
133 if (description && (*description != '\0') &&
134 Is_Printable_String(description))
136 item->description.length = strlen(description);
137 item->description.str = description;
141 item->description.length = 0;
142 item->description.str = (char *)0;
146 SET_ERROR(E_SYSTEM_ERROR);
151 /*---------------------------------------------------------------------------
152 | Facility : libnmenu
153 | Function : int free_item(ITEM *item)
155 | Description : Free the allocated storage for this item.
156 | N.B.: a connected item can't be freed.
158 | Return Values : E_OK - success
159 | E_BAD_ARGUMENT - invalid value has been passed
160 | E_CONNECTED - item is still connected to a menu
161 +--------------------------------------------------------------------------*/
163 free_item(ITEM * item)
165 T((T_CALLED("free_item(%p)"), (void *)item));
168 RETURN(E_BAD_ARGUMENT);
178 /*---------------------------------------------------------------------------
179 | Facility : libnmenu
180 | Function : int set_menu_mark( MENU *menu, const char *mark )
182 | Description : Set the mark string used to indicate the current
183 | item (single-valued menu) or the selected items
184 | (multi-valued menu).
185 | The mark argument may be NULL, in which case no
187 | This might be a little bit tricky, because this may
188 | affect the geometry of the menu, which we don't allow
189 | if it is already posted.
191 | Return Values : E_OK - success
192 | E_BAD_ARGUMENT - an invalid value has been passed
193 | E_SYSTEM_ERROR - no memory to store mark
194 +--------------------------------------------------------------------------*/
196 set_menu_mark(MENU * menu, const char *mark)
200 T((T_CALLED("set_menu_mark(%p,%s)"), (void *)menu, _nc_visbuf(mark)));
202 if (mark && (*mark != '\0') && Is_Printable_String(mark))
209 char *old_mark = menu->mark;
210 unsigned short old_status = menu->status;
212 if (menu->status & _POSTED)
214 /* If the menu is already posted, the geometry is fixed. Then
215 we can only accept a mark with exactly the same length */
216 if (menu->marklen != (int)l)
217 RETURN(E_BAD_ARGUMENT);
222 menu->mark = strdup(mark);
225 strcpy(menu->mark, mark);
226 if (menu != &_nc_Default_Menu)
227 menu->status |= _MARK_ALLOCATED;
231 menu->mark = old_mark;
232 menu->marklen = (old_mark != 0) ? strlen(old_mark) : 0;
233 RETURN(E_SYSTEM_ERROR);
237 menu->mark = (char *)0;
239 if ((old_status & _MARK_ALLOCATED) && old_mark)
242 if (menu->status & _POSTED)
249 /* Recalculate the geometry */
250 _nc_Calculate_Item_Length_and_Width(menu);
255 returnCode(set_menu_mark(&_nc_Default_Menu, mark));
260 /*---------------------------------------------------------------------------
261 | Facility : libnmenu
262 | Function : char *menu_mark(const MENU *menu)
264 | Description : Return a pointer to the marker string
266 | Return Values : The marker string pointer or NULL if no marker defined
267 +--------------------------------------------------------------------------*/
268 NCURSES_EXPORT(const char *)
269 menu_mark(const MENU * menu)
271 T((T_CALLED("menu_mark(%p)"), (const void *)menu));
272 returnPtr(Normalize_Menu(menu)->mark);