RCSID updates, warning fixes
[dfdiff.git] / contrib / ncurses-5.4 / menu / m_item_new.c
blob788f53eebd784329ca86de7224cc98ca4e21e45f
1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 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 ****************************************************************************/
29 /****************************************************************************
30 * Author: Juergen Pfeifer, 1995,1997 *
31 ****************************************************************************/
33 /***************************************************************************
34 * Module m_item_new *
35 * Create and destroy menu items *
36 * Set and get marker string for menu *
37 ***************************************************************************/
39 #include "menu.priv.h"
41 MODULE_ID("$Id: m_item_new.c,v 1.14 2003/10/25 15:23:42 tom Exp $")
43 /*---------------------------------------------------------------------------
44 | Facility : libnmenu
45 | Function : bool Is_Printable_String(const char *s)
47 | Description : Checks whether or not the string contains only printable
48 | characters.
50 | Return Values : TRUE - if string is printable
51 | FALSE - if string contains non-printable characters
52 +--------------------------------------------------------------------------*/
53 static bool Is_Printable_String(const char *s)
55 assert(s);
56 while(*s)
58 if (!isprint((unsigned char)*s))
59 return FALSE;
60 s++;
62 return TRUE;
65 /*---------------------------------------------------------------------------
66 | Facility : libnmenu
67 | Function : ITEM *new_item(char *name, char *description)
69 | Description : Create a new item with name and description. Return
70 | a pointer to this new item.
71 | N.B.: an item must(!) have a name.
73 | Return Values : The item pointer or NULL if creation failed.
74 +--------------------------------------------------------------------------*/
75 NCURSES_EXPORT(ITEM *)
76 new_item (const char *name, const char *description)
78 ITEM *item;
80 if ( !name || (*name == '\0') || !Is_Printable_String(name) )
82 item = (ITEM *)0;
83 SET_ERROR( E_BAD_ARGUMENT );
85 else
87 item = (ITEM *)calloc(1,sizeof(ITEM));
88 if (item)
90 *item = _nc_Default_Item; /* hope we have struct assignment */
92 item->name.length = strlen(name);
93 item->name.str = name;
95 if (description && (*description != '\0') &&
96 Is_Printable_String(description))
98 item->description.length = strlen(description);
99 item->description.str = description;
101 else
103 item->description.length = 0;
104 item->description.str = (char *)0;
107 else
108 SET_ERROR( E_SYSTEM_ERROR );
110 return(item);
113 /*---------------------------------------------------------------------------
114 | Facility : libnmenu
115 | Function : int free_item(ITEM *item)
117 | Description : Free the allocated storage for this item.
118 | N.B.: a connected item can't be freed.
120 | Return Values : E_OK - success
121 | E_BAD_ARGUMENT - invalid value has been passed
122 | E_CONNECTED - item is still connected to a menu
123 +--------------------------------------------------------------------------*/
124 NCURSES_EXPORT(int)
125 free_item (ITEM * item)
127 if (!item)
128 RETURN( E_BAD_ARGUMENT );
130 if (item->imenu)
131 RETURN( E_CONNECTED );
133 free(item);
135 RETURN( E_OK );
138 /*---------------------------------------------------------------------------
139 | Facility : libnmenu
140 | Function : int set_menu_mark( MENU *menu, const char *mark )
142 | Description : Set the mark string used to indicate the current
143 | item (single-valued menu) or the selected items
144 | (multi-valued menu).
145 | The mark argument may be NULL, in which case no
146 | marker is used.
147 | This might be a little bit tricky, because this may
148 | affect the geometry of the menu, which we don't allow
149 | if it is already posted.
151 | Return Values : E_OK - success
152 | E_BAD_ARGUMENT - an invalid value has been passed
153 | E_SYSTEM_ERROR - no memory to store mark
154 +--------------------------------------------------------------------------*/
155 NCURSES_EXPORT(int)
156 set_menu_mark (MENU * menu, const char * mark)
158 int l;
160 if ( mark && (*mark != '\0') && Is_Printable_String(mark) )
161 l = strlen(mark);
162 else
163 l = 0;
165 if ( menu )
167 char *old_mark = menu->mark;
168 unsigned short old_status = menu->status;
170 if (menu->status & _POSTED)
172 /* If the menu is already posted, the geometry is fixed. Then
173 we can only accept a mark with exactly the same length */
174 if (menu->marklen != l)
175 RETURN(E_BAD_ARGUMENT);
177 menu->marklen = l;
178 if (l)
180 menu->mark = (char *)malloc(l+1);
181 if (menu->mark)
183 strcpy(menu->mark, mark);
184 if (menu != &_nc_Default_Menu)
185 menu->status |= _MARK_ALLOCATED;
187 else
189 menu->mark = old_mark;
190 RETURN(E_SYSTEM_ERROR);
193 else
194 menu->mark = (char *)0;
196 if ((old_status & _MARK_ALLOCATED) && old_mark)
197 free(old_mark);
199 if (menu->status & _POSTED)
201 _nc_Draw_Menu( menu );
202 _nc_Show_Menu( menu );
204 else
206 /* Recalculate the geometry */
207 _nc_Calculate_Item_Length_and_Width( menu );
210 else
212 return set_menu_mark(&_nc_Default_Menu, mark);
214 RETURN(E_OK);
217 /*---------------------------------------------------------------------------
218 | Facility : libnmenu
219 | Function : char *menu_mark(const MENU *menu)
221 | Description : Return a pointer to the marker string
223 | Return Values : The marker string pointer or NULL if no marker defined
224 +--------------------------------------------------------------------------*/
225 NCURSES_EXPORT(const char *)
226 menu_mark (const MENU * menu)
228 return Normalize_Menu( menu )->mark;
231 /* m_item_new.c */