1 /*-----------------------------------------------------------------------------+
2 | The ncurses menu library is Copyright (C) 1995-1997 |
3 | by Juergen Pfeifer <Juergen.Pfeifer@T-Online.de> |
4 | All Rights Reserved. |
6 | Permission to use, copy, modify, and distribute this software and its |
7 | documentation for any purpose and without fee is hereby granted, provided |
8 | that the above copyright notice appear in all copies and that both that |
9 | copyright notice and this permission notice appear in supporting |
10 | documentation, and that the name of the above listed copyright holder(s) not |
11 | be used in advertising or publicity pertaining to distribution of the |
12 | software without specific, written prior permission. |
14 | THE ABOVE LISTED COPYRIGHT HOLDER(S) DISCLAIM ALL WARRANTIES WITH REGARD TO |
15 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FIT- |
16 | NESS, IN NO EVENT SHALL THE ABOVE LISTED COPYRIGHT HOLDER(S) BE LIABLE FOR |
17 | ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RE- |
18 | SULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, |
19 | NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH |
20 | THE USE OR PERFORMANCE OF THIS SOFTWARE. |
21 +-----------------------------------------------------------------------------*/
23 /***************************************************************************
24 * Module menu_item_new *
25 * Create and destroy menu items *
26 * Set and get marker string for menu
27 ***************************************************************************/
29 #include "menu.priv.h"
31 MODULE_ID("$Id: m_item_new.c,v 1.5 1997/05/01 16:47:26 juergen Exp $")
33 /*---------------------------------------------------------------------------
35 | Function : bool Is_Printable_String(const char *s)
37 | Description : Checks whether or not the string contains only printable
40 | Return Values : TRUE - if string is printable
41 | FALSE - if string contains non-printable characters
42 +--------------------------------------------------------------------------*/
43 static bool Is_Printable_String(const char *s)
48 if (!isprint((unsigned char)*s))
55 /*---------------------------------------------------------------------------
57 | Function : ITEM *new_item(char *name, char *description)
59 | Description : Create a new item with name and description. Return
60 | a pointer to this new item.
61 | N.B.: an item must(!) have a name.
63 | Return Values : The item pointer or NULL if creation failed.
64 +--------------------------------------------------------------------------*/
65 ITEM *new_item(const char *name, const char *description)
69 if ( !name || (*name == '\0') || !Is_Printable_String(name) )
72 SET_ERROR( E_BAD_ARGUMENT );
76 item = (ITEM *)calloc(1,sizeof(ITEM));
79 *item = _nc_Default_Item; /* hope we have struct assignment */
81 item->name.length = strlen(name);
82 item->name.str = (char *)malloc(1 + item->name.length);
85 strcpy(item->name.str, name);
90 SET_ERROR( E_SYSTEM_ERROR );
94 if (description && (*description != '\0') &&
95 Is_Printable_String(description))
97 item->description.length = strlen(description);
98 item->description.str =
99 (char *)malloc(1 + item->description.length);
100 if (item->description.str)
102 strcpy(item->description.str, description);
106 free(item->name.str);
108 SET_ERROR( E_SYSTEM_ERROR );
114 item->description.length = 0;
115 item->description.str = (char *)0;
119 SET_ERROR( E_SYSTEM_ERROR );
124 /*---------------------------------------------------------------------------
125 | Facility : libnmenu
126 | Function : int free_item(ITEM *item)
128 | Description : Free the allocated storage for this item.
129 | N.B.: a connected item can't be freed.
131 | Return Values : E_OK - success
132 | E_BAD_ARGUMENT - invalid value has been passed
133 | E_CONNECTED - item is still connected to a menu
134 +--------------------------------------------------------------------------*/
135 int free_item(ITEM * item)
138 RETURN( E_BAD_ARGUMENT );
141 RETURN( E_CONNECTED );
144 free(item->name.str);
145 if (item->description.str)
146 free (item->description.str);
152 /*---------------------------------------------------------------------------
153 | Facility : libnmenu
154 | Function : int set_menu_mark( MENU *menu, const char *mark )
156 | Description : Set the mark string used to indicate the current
157 | item (single-valued menu) or the selected items
158 | (multi-valued menu).
159 | The mark argument may be NULL, in which case no
161 | This might be a little bit tricky, because this may
162 | affect the geometry of the menu, which we don't allow
163 | if it is already posted.
165 | Return Values : E_OK - success
166 | E_BAD_ARGUMENT - an invalid value has been passed
167 | E_SYSTEM_ERROR - no memory to store mark
168 +--------------------------------------------------------------------------*/
169 int set_menu_mark(MENU * menu, const char * mark)
173 if ( mark && (*mark != '\0') && Is_Printable_String(mark) )
180 char *old_mark = menu->mark;
181 unsigned short old_status = menu->status;
183 if (menu->status & _POSTED)
185 /* If the menu is already posted, the geometry is fixed. Then
186 we can only accept a mark with exactly the same length */
187 if (menu->marklen != l)
188 RETURN(E_BAD_ARGUMENT);
193 menu->mark = (char *)malloc(l+1);
196 strcpy(menu->mark, mark);
197 menu->status |= _MARK_ALLOCATED;
201 menu->mark = old_mark;
202 RETURN(E_SYSTEM_ERROR);
206 menu->mark = (char *)0;
208 if ((old_status & _MARK_ALLOCATED) && old_mark)
211 if (menu->status & _POSTED)
213 _nc_Draw_Menu( menu );
214 _nc_Show_Menu( menu );
218 /* Recalculate the geometry */
219 _nc_Calculate_Item_Length_and_Width( menu );
224 return set_menu_mark(&_nc_Default_Menu, mark);
229 /*---------------------------------------------------------------------------
230 | Facility : libnmenu
231 | Function : char *menu_mark(const MENU *menu)
233 | Description : Return a pointer to the marker string
235 | Return Values : The marker string pointer or NULL if no marker defined
236 +--------------------------------------------------------------------------*/
237 const char *menu_mark(const MENU * menu)
239 return Normalize_Menu( menu )->mark;