1 /**************************************************************************** 2 * Copyright (c) 1998 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 ****************************************************************************/ 28 29 /**************************************************************************** 30 * Author: Juergen Pfeifer <juergen.pfeifer@gmx.net> 1995,1997 * 31 ****************************************************************************/ 32 33 /*************************************************************************** 34 * Module m_item_new * 35 * Create and destroy menu items * 36 * Set and get marker string for menu * 37 ***************************************************************************/ 38 39 #include "menu.priv.h" 40 41 MODULE_ID("$Id: m_item_new.c,v 1.11 1999/05/16 17:28:49 juergen Exp $") 42 43 /*--------------------------------------------------------------------------- 44 | Facility : libnmenu 45 | Function : bool Is_Printable_String(const char *s) 46 | 47 | Description : Checks whether or not the string contains only printable 48 | characters. 49 | 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) 54 { 55 assert(s); 56 while(*s) 57 { 58 if (!isprint((unsigned char)*s)) 59 return FALSE; 60 s++; 61 } 62 return TRUE; 63 } 64 65 /*--------------------------------------------------------------------------- 66 | Facility : libnmenu 67 | Function : ITEM *new_item(char *name, char *description) 68 | 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. 72 | 73 | Return Values : The item pointer or NULL if creation failed. 74 +--------------------------------------------------------------------------*/ 75 ITEM *new_item(const char *name, const char *description) 76 { 77 ITEM *item; 78 79 if ( !name || (*name == '\0') || !Is_Printable_String(name) ) 80 { 81 item = (ITEM *)0; 82 SET_ERROR( E_BAD_ARGUMENT ); 83 } 84 else 85 { 86 item = (ITEM *)calloc(1,sizeof(ITEM)); 87 if (item) 88 { 89 *item = _nc_Default_Item; /* hope we have struct assignment */ 90 91 item->name.length = strlen(name); 92 item->name.str = name; 93 94 if (description && (*description != '\0') && 95 Is_Printable_String(description)) 96 { 97 item->description.length = strlen(description); 98 item->description.str = description; 99 } 100 else 101 { 102 item->description.length = 0; 103 item->description.str = (char *)0; 104 } 105 } 106 else 107 SET_ERROR( E_SYSTEM_ERROR ); 108 } 109 return(item); 110 } 111 112 /*--------------------------------------------------------------------------- 113 | Facility : libnmenu 114 | Function : int free_item(ITEM *item) 115 | 116 | Description : Free the allocated storage for this item. 117 | N.B.: a connected item can't be freed. 118 | 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) 124 { 125 if (!item) 126 RETURN( E_BAD_ARGUMENT ); 127 128 if (item->imenu) 129 RETURN( E_CONNECTED ); 130 131 free(item); 132 133 RETURN( E_OK ); 134 } 135 136 /*--------------------------------------------------------------------------- 137 | Facility : libnmenu 138 | Function : int set_menu_mark( MENU *menu, const char *mark ) 139 | 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 144 | marker is used. 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. 148 | 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) 154 { 155 int l; 156 157 if ( mark && (*mark != '\0') && Is_Printable_String(mark) ) 158 l = strlen(mark); 159 else 160 l = 0; 161 162 if ( menu ) 163 { 164 char *old_mark = menu->mark; 165 unsigned short old_status = menu->status; 166 167 if (menu->status & _POSTED) 168 { 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); 173 } 174 menu->marklen = l; 175 if (l) 176 { 177 menu->mark = (char *)malloc(l+1); 178 if (menu->mark) 179 { 180 strcpy(menu->mark, mark); 181 if (menu != &_nc_Default_Menu) 182 menu->status |= _MARK_ALLOCATED; 183 } 184 else 185 { 186 menu->mark = old_mark; 187 RETURN(E_SYSTEM_ERROR); 188 } 189 } 190 else 191 menu->mark = (char *)0; 192 193 if ((old_status & _MARK_ALLOCATED) && old_mark) 194 free(old_mark); 195 196 if (menu->status & _POSTED) 197 { 198 _nc_Draw_Menu( menu ); 199 _nc_Show_Menu( menu ); 200 } 201 else 202 { 203 /* Recalculate the geometry */ 204 _nc_Calculate_Item_Length_and_Width( menu ); 205 } 206 } 207 else 208 { 209 return set_menu_mark(&_nc_Default_Menu, mark); 210 } 211 RETURN(E_OK); 212 } 213 214 /*--------------------------------------------------------------------------- 215 | Facility : libnmenu 216 | Function : char *menu_mark(const MENU *menu) 217 | 218 | Description : Return a pointer to the marker string 219 | 220 | Return Values : The marker string pointer or NULL if no marker defined 221 +--------------------------------------------------------------------------*/ 222 const char *menu_mark(const MENU * menu) 223 { 224 return Normalize_Menu( menu )->mark; 225 } 226 227 /* m_item_new.c */ 228