1 /****************************************************************************
2 * Copyright (c) 1998,2000 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 * Contact: http://www.familiepfeifer.de/Contact.aspx?Lang=en *
32 ****************************************************************************/
34 /***************************************************************************
36 * Create and destroy menu items *
37 * Set and get marker string for menu *
38 ***************************************************************************/
40 #include "menu.priv.h"
42 MODULE_ID("$Id: m_item_new.c,v 1.13 2002/07/06 15:22:16 juergen Exp $")
44 /*---------------------------------------------------------------------------
46 | Function : bool Is_Printable_String(const char *s)
48 | Description : Checks whether or not the string contains only printable
51 | Return Values : TRUE - if string is printable
52 | FALSE - if string contains non-printable characters
53 +--------------------------------------------------------------------------*/
54 static bool Is_Printable_String(const char *s)
59 if (!isprint((unsigned char)*s))
66 /*---------------------------------------------------------------------------
68 | Function : ITEM *new_item(char *name, char *description)
70 | Description : Create a new item with name and description. Return
71 | a pointer to this new item.
72 | N.B.: an item must(!) have a name.
74 | Return Values : The item pointer or NULL if creation failed.
75 +--------------------------------------------------------------------------*/
76 NCURSES_EXPORT(ITEM *)
77 new_item (const char *name, const char *description)
81 if ( !name || (*name == '\0') || !Is_Printable_String(name) )
84 SET_ERROR( E_BAD_ARGUMENT );
88 item = (ITEM *)calloc(1,sizeof(ITEM));
91 *item = _nc_Default_Item; /* hope we have struct assignment */
93 item->name.length = strlen(name);
94 item->name.str = name;
96 if (description && (*description != '\0') &&
97 Is_Printable_String(description))
99 item->description.length = strlen(description);
100 item->description.str = description;
104 item->description.length = 0;
105 item->description.str = (char *)0;
109 SET_ERROR( E_SYSTEM_ERROR );
114 /*---------------------------------------------------------------------------
115 | Facility : libnmenu
116 | Function : int free_item(ITEM *item)
118 | Description : Free the allocated storage for this item.
119 | N.B.: a connected item can't be freed.
121 | Return Values : E_OK - success
122 | E_BAD_ARGUMENT - invalid value has been passed
123 | E_CONNECTED - item is still connected to a menu
124 +--------------------------------------------------------------------------*/
126 free_item (ITEM * item)
129 RETURN( E_BAD_ARGUMENT );
132 RETURN( E_CONNECTED );
139 /*---------------------------------------------------------------------------
140 | Facility : libnmenu
141 | Function : int set_menu_mark( MENU *menu, const char *mark )
143 | Description : Set the mark string used to indicate the current
144 | item (single-valued menu) or the selected items
145 | (multi-valued menu).
146 | The mark argument may be NULL, in which case no
148 | This might be a little bit tricky, because this may
149 | affect the geometry of the menu, which we don't allow
150 | if it is already posted.
152 | Return Values : E_OK - success
153 | E_BAD_ARGUMENT - an invalid value has been passed
154 | E_SYSTEM_ERROR - no memory to store mark
155 +--------------------------------------------------------------------------*/
157 set_menu_mark (MENU * menu, const char * mark)
161 if ( mark && (*mark != '\0') && Is_Printable_String(mark) )
168 char *old_mark = menu->mark;
169 unsigned short old_status = menu->status;
171 if (menu->status & _POSTED)
173 /* If the menu is already posted, the geometry is fixed. Then
174 we can only accept a mark with exactly the same length */
175 if (menu->marklen != l)
176 RETURN(E_BAD_ARGUMENT);
181 menu->mark = (char *)malloc(l+1);
184 strcpy(menu->mark, mark);
185 if (menu != &_nc_Default_Menu)
186 menu->status |= _MARK_ALLOCATED;
190 menu->mark = old_mark;
191 RETURN(E_SYSTEM_ERROR);
195 menu->mark = (char *)0;
197 if ((old_status & _MARK_ALLOCATED) && old_mark)
200 if (menu->status & _POSTED)
202 _nc_Draw_Menu( menu );
203 _nc_Show_Menu( menu );
207 /* Recalculate the geometry */
208 _nc_Calculate_Item_Length_and_Width( menu );
213 return set_menu_mark(&_nc_Default_Menu, mark);
218 /*---------------------------------------------------------------------------
219 | Facility : libnmenu
220 | Function : char *menu_mark(const MENU *menu)
222 | Description : Return a pointer to the marker string
224 | Return Values : The marker string pointer or NULL if no marker defined
225 +--------------------------------------------------------------------------*/
226 NCURSES_EXPORT(const char *)
227 menu_mark (const MENU * menu)
229 return Normalize_Menu( menu )->mark;