]> ncurses.scripts.mit.edu Git - ncurses.git/blob - menu/m_item_new.c
ncurses 4.2
[ncurses.git] / menu / m_item_new.c
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@T-Online.de> 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.8 1998/02/11 12:13:50 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 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               menu->status |= _MARK_ALLOCATED;
182             }
183           else
184             {
185               menu->mark = old_mark;
186               RETURN(E_SYSTEM_ERROR);
187             }
188         }
189       else
190         menu->mark = (char *)0;
191       
192       if ((old_status & _MARK_ALLOCATED) && old_mark)
193         free(old_mark);
194
195       if (menu->status & _POSTED)
196         {
197           _nc_Draw_Menu( menu );
198           _nc_Show_Menu( menu );
199         }
200       else
201         {
202           /* Recalculate the geometry */
203           _nc_Calculate_Item_Length_and_Width( menu );                  
204         }
205     }
206   else
207     {
208       return set_menu_mark(&_nc_Default_Menu, mark);
209     }
210   RETURN(E_OK);
211 }
212
213 /*---------------------------------------------------------------------------
214 |   Facility      :  libnmenu  
215 |   Function      :  char *menu_mark(const MENU *menu)
216 |   
217 |   Description   :  Return a pointer to the marker string
218 |
219 |   Return Values :  The marker string pointer or NULL if no marker defined
220 +--------------------------------------------------------------------------*/
221 const char *menu_mark(const MENU * menu)
222 {
223   return Normalize_Menu( menu )->mark;
224 }
225
226 /* m_item_new.c */