1 /****************************************************************************
2 * Copyright (c) 1998 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 <juergen.pfeifer@gmx.net> 1995,1997 *
31 ****************************************************************************/
33 /***************************************************************************
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.11 1999/05/16 17:28:49 juergen Exp $")
43 /*---------------------------------------------------------------------------
45 | Function : bool Is_Printable_String(const char *s)
47 | Description : Checks whether or not the string contains only printable
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)
58 if (!isprint((unsigned char)*s))
65 /*---------------------------------------------------------------------------
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 ITEM *new_item(const char *name, const char *description)
79 if ( !name || (*name == '\0') || !Is_Printable_String(name) )
82 SET_ERROR( E_BAD_ARGUMENT );
86 item = (ITEM *)calloc(1,sizeof(ITEM));
89 *item = _nc_Default_Item; /* hope we have struct assignment */
91 item->name.length = strlen(name);
92 item->name.str = name;
94 if (description && (*description != '\0') &&
95 Is_Printable_String(description))
97 item->description.length = strlen(description);
98 item->description.str = description;
102 item->description.length = 0;
103 item->description.str = (char *)0;
107 SET_ERROR( E_SYSTEM_ERROR );
112 /*---------------------------------------------------------------------------
113 | Facility : libnmenu
114 | Function : int free_item(ITEM *item)
116 | Description : Free the allocated storage for this item.
117 | N.B.: a connected item can't be freed.
119 | Return Values : E_OK - success
120 | E_BAD_ARGUMENT - invalid value has been passed
121 | E_CONNECTED - item is still connected to a menu
122 +--------------------------------------------------------------------------*/
123 int free_item(ITEM * item)
126 RETURN( E_BAD_ARGUMENT );
129 RETURN( E_CONNECTED );
136 /*---------------------------------------------------------------------------
137 | Facility : libnmenu
138 | Function : int set_menu_mark( MENU *menu, const char *mark )
140 | Description : Set the mark string used to indicate the current
141 | item (single-valued menu) or the selected items
142 | (multi-valued menu).
143 | The mark argument may be NULL, in which case no
145 | This might be a little bit tricky, because this may
146 | affect the geometry of the menu, which we don't allow
147 | if it is already posted.
149 | Return Values : E_OK - success
150 | E_BAD_ARGUMENT - an invalid value has been passed
151 | E_SYSTEM_ERROR - no memory to store mark
152 +--------------------------------------------------------------------------*/
153 int set_menu_mark(MENU * menu, const char * mark)
157 if ( mark && (*mark != '\0') && Is_Printable_String(mark) )
164 char *old_mark = menu->mark;
165 unsigned short old_status = menu->status;
167 if (menu->status & _POSTED)
169 /* If the menu is already posted, the geometry is fixed. Then
170 we can only accept a mark with exactly the same length */
171 if (menu->marklen != l)
172 RETURN(E_BAD_ARGUMENT);
177 menu->mark = (char *)malloc(l+1);
180 strcpy(menu->mark, mark);
181 if (menu != &_nc_Default_Menu)
182 menu->status |= _MARK_ALLOCATED;
186 menu->mark = old_mark;
187 RETURN(E_SYSTEM_ERROR);
191 menu->mark = (char *)0;
193 if ((old_status & _MARK_ALLOCATED) && old_mark)
196 if (menu->status & _POSTED)
198 _nc_Draw_Menu( menu );
199 _nc_Show_Menu( menu );
203 /* Recalculate the geometry */
204 _nc_Calculate_Item_Length_and_Width( menu );
209 return set_menu_mark(&_nc_Default_Menu, mark);
214 /*---------------------------------------------------------------------------
215 | Facility : libnmenu
216 | Function : char *menu_mark(const MENU *menu)
218 | Description : Return a pointer to the marker string
220 | Return Values : The marker string pointer or NULL if no marker defined
221 +--------------------------------------------------------------------------*/
222 const char *menu_mark(const MENU * menu)
224 return Normalize_Menu( menu )->mark;