1 /****************************************************************************
2 * Copyright (c) 1998-2002,2003 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, 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.14 2003/10/25 15:23:42 tom 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 NCURSES_EXPORT(ITEM
*)
76 new_item (const char *name
, const char *description
)
80 if ( !name
|| (*name
== '\0') || !Is_Printable_String(name
) )
83 SET_ERROR( E_BAD_ARGUMENT
);
87 item
= (ITEM
*)calloc(1,sizeof(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
;
103 item
->description
.length
= 0;
104 item
->description
.str
= (char *)0;
108 SET_ERROR( E_SYSTEM_ERROR
);
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 +--------------------------------------------------------------------------*/
125 free_item (ITEM
* item
)
128 RETURN( E_BAD_ARGUMENT
);
131 RETURN( E_CONNECTED
);
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
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 +--------------------------------------------------------------------------*/
156 set_menu_mark (MENU
* menu
, const char * mark
)
160 if ( mark
&& (*mark
!= '\0') && Is_Printable_String(mark
) )
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
);
180 menu
->mark
= (char *)malloc(l
+1);
183 strcpy(menu
->mark
, mark
);
184 if (menu
!= &_nc_Default_Menu
)
185 menu
->status
|= _MARK_ALLOCATED
;
189 menu
->mark
= old_mark
;
190 RETURN(E_SYSTEM_ERROR
);
194 menu
->mark
= (char *)0;
196 if ((old_status
& _MARK_ALLOCATED
) && old_mark
)
199 if (menu
->status
& _POSTED
)
201 _nc_Draw_Menu( menu
);
202 _nc_Show_Menu( menu
);
206 /* Recalculate the geometry */
207 _nc_Calculate_Item_Length_and_Width( menu
);
212 return set_menu_mark(&_nc_Default_Menu
, mark
);
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
;