1 /**************************************************************************** 2 * Copyright (c) 1998,2000 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.12 2000/12/10 02:16:48 tom 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 NCURSES_EXPORT(ITEM *) 76 new_item (const char *name, const char *description) 77 { 78 ITEM *item; 79 80 if ( !name || (*name == '\0') || !Is_Printable_String(name) ) 81 { 82 item = (ITEM *)0; 83 SET_ERROR( E_BAD_ARGUMENT ); 84 } 85 else 86 { 87 item = (ITEM *)calloc(1,sizeof(ITEM)); 88 if (item) 89 { 90 *item = _nc_Default_Item; /* hope we have struct assignment */ 91 92 item->name.length = strlen(name); 93 item->name.str = name; 94 95 if (description && (*description != '\0') && 96 Is_Printable_String(description)) 97 { 98 item->description.length = strlen(description); 99 item->description.str = description; 100 } 101 else 102 { 103 item->description.length = 0; 104 item->description.str = (char *)0; 105 } 106 } 107 else 108 SET_ERROR( E_SYSTEM_ERROR ); 109 } 110 return(item); 111 } 112 113 /*--------------------------------------------------------------------------- 114 | Facility : libnmenu 115 | Function : int free_item(ITEM *item) 116 | 117 | Description : Free the allocated storage for this item. 118 | N.B.: a connected item can't be freed. 119 | 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) 126 { 127 if (!item) 128 RETURN( E_BAD_ARGUMENT ); 129 130 if (item->imenu) 131 RETURN( E_CONNECTED ); 132 133 free(item); 134 135 RETURN( E_OK ); 136 } 137 138 /*--------------------------------------------------------------------------- 139 | Facility : libnmenu 140 | Function : int set_menu_mark( MENU *menu, const char *mark ) 141 | 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. 150 | 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) 157 { 158 int l; 159 160 if ( mark && (*mark != '\0') && Is_Printable_String(mark) ) 161 l = strlen(mark); 162 else 163 l = 0; 164 165 if ( menu ) 166 { 167 char *old_mark = menu->mark; 168 unsigned short old_status = menu->status; 169 170 if (menu->status & _POSTED) 171 { 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); 176 } 177 menu->marklen = l; 178 if (l) 179 { 180 menu->mark = (char *)malloc(l+1); 181 if (menu->mark) 182 { 183 strcpy(menu->mark, mark); 184 if (menu != &_nc_Default_Menu) 185 menu->status |= _MARK_ALLOCATED; 186 } 187 else 188 { 189 menu->mark = old_mark; 190 RETURN(E_SYSTEM_ERROR); 191 } 192 } 193 else 194 menu->mark = (char *)0; 195 196 if ((old_status & _MARK_ALLOCATED) && old_mark) 197 free(old_mark); 198 199 if (menu->status & _POSTED) 200 { 201 _nc_Draw_Menu( menu ); 202 _nc_Show_Menu( menu ); 203 } 204 else 205 { 206 /* Recalculate the geometry */ 207 _nc_Calculate_Item_Length_and_Width( menu ); 208 } 209 } 210 else 211 { 212 return set_menu_mark(&_nc_Default_Menu, mark); 213 } 214 RETURN(E_OK); 215 } 216 217 /*--------------------------------------------------------------------------- 218 | Facility : libnmenu 219 | Function : char *menu_mark(const MENU *menu) 220 | 221 | Description : Return a pointer to the marker string 222 | 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) 227 { 228 return Normalize_Menu( menu )->mark; 229 } 230 231 /* m_item_new.c */ 232