home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / ncurses-1.9.9e-src.tgz / tar.out / fsf / ncurses / menu / m_item_new.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  7KB  |  189 lines

  1.  
  2. /***************************************************************************
  3. *                            COPYRIGHT NOTICE                              *
  4. ****************************************************************************
  5. *                ncurses is copyright (C) 1992-1995                        *
  6. *                          Zeyd M. Ben-Halim                               *
  7. *                          zmbenhal@netcom.com                             *
  8. *                          Eric S. Raymond                                 *
  9. *                          esr@snark.thyrsus.com                           *
  10. *                                                                          *
  11. *        Permission is hereby granted to reproduce and distribute ncurses  *
  12. *        by any means and for any fee, whether alone or as part of a       *
  13. *        larger distribution, in source or in binary form, PROVIDED        *
  14. *        this notice is included with any such distribution, and is not    *
  15. *        removed from any of its header files. Mention of ncurses in any   *
  16. *        applications linked with it is highly appreciated.                *
  17. *                                                                          *
  18. *        ncurses comes AS IS with no warranty, implied or expressed.       *
  19. *                                                                          *
  20. ***************************************************************************/
  21.  
  22. /***************************************************************************
  23. * Module menu_item_new                                                     *
  24. * Create and destroy menu items                                            *
  25. * Set and get marker string for menu
  26. ***************************************************************************/
  27.  
  28. #include "menu.priv.h"
  29.  
  30. /*---------------------------------------------------------------------------
  31. |   Facility      :  libnmenu  
  32. |   Function      :  bool Is_Printable_String(const char *s)
  33. |   
  34. |   Description   :  Checks whether or not the string contains only printable
  35. |                    characters.
  36. |
  37. |   Return Values :  TRUE     - if string is printable
  38. |                    FALSE    - if string contains non-printable characters
  39. +--------------------------------------------------------------------------*/
  40. static bool Is_Printable_String(const char *s)
  41. {
  42.   assert(s);
  43.   while(*s)
  44.     {
  45.       if (!isprint((unsigned char)*s))
  46.     return FALSE;
  47.       s++;
  48.     }
  49.   return TRUE;
  50. }
  51.  
  52. /*---------------------------------------------------------------------------
  53. |   Facility      :  libnmenu  
  54. |   Function      :  ITEM *new_item(char *name, char *description)
  55. |   
  56. |   Description   :  Create a new item with name and description. Return
  57. |                    a pointer to this new item.
  58. |                    N.B.: an item must(!) have a name.
  59. |
  60. |   Return Values :  The item pointer or NULL if creation failed.
  61. +--------------------------------------------------------------------------*/
  62. ITEM *new_item(char *name, char *description)
  63. {
  64.   ITEM *item;
  65.   
  66.   if ( !name || (*name == '\0') || !Is_Printable_String(name) )
  67.     {
  68.       item = (ITEM *)0;
  69.       SET_ERROR( E_BAD_ARGUMENT );
  70.     }
  71.   else
  72.     {
  73.       item = (ITEM *)calloc(1,sizeof(ITEM));
  74.       if (item)
  75.     {
  76.       *item  = _nc_Default_Item; /* hope we have struct assignment */
  77.       
  78.       item->name.str        = name;
  79.       item->name.length       = strlen(name);
  80.       
  81.       item->description.str    = description;
  82.       if (description && Is_Printable_String(description))
  83.         item->description.length = strlen(description);
  84.       else
  85.         {
  86.           item->description.length = 0;
  87.           item->description.str    = (char *)0;
  88.         }
  89.     }
  90.       else
  91.     SET_ERROR( E_SYSTEM_ERROR );
  92.     }  
  93.   return(item);
  94. }
  95.  
  96. /*---------------------------------------------------------------------------
  97. |   Facility      :  libnmenu  
  98. |   Function      :  int free_item(ITEM *item)
  99. |   
  100. |   Description   :  Free the allocated storage for this item. 
  101. |                    N.B.: a connected item can't be freed.
  102. |
  103. |   Return Values :  E_OK              - success
  104. |                    E_BAD_ARGUMENT    - invalid value has been passed
  105. |                    E_CONNECTED       - item is still connected to a menu    
  106. +--------------------------------------------------------------------------*/
  107. int free_item(ITEM * item)
  108. {
  109.   if (!item)
  110.     RETURN( E_BAD_ARGUMENT );
  111.  
  112.   if (item->imenu)
  113.     RETURN( E_CONNECTED );
  114.   
  115.   free(item);
  116.   RETURN( E_OK );
  117. }
  118.  
  119. /*---------------------------------------------------------------------------
  120. |   Facility      :  libnmenu  
  121. |   Function      :  int set_menu_mark( MENU *menu, char *mark )
  122. |   
  123. |   Description   :  Set the mark string used to indicate the current
  124. |                    item (single-valued menu) or the selected items
  125. |                    (multi-valued menu).
  126. |                    The mark argument may be NULL, in which case no 
  127. |                    marker is used.
  128. |                    This might be a little bit tricky, because this may 
  129. |                    affect the geometry of the menu, which we don't allow 
  130. |                    if it is already posted.
  131. |
  132. |   Return Values :  E_OK               - success
  133. |                    E_BAD_ARGUMENT     - an invalid value has been passed
  134. +--------------------------------------------------------------------------*/
  135. int set_menu_mark(MENU * menu, char * mark)
  136. {
  137.   int l;
  138.   
  139.   if ( mark && *mark && Is_Printable_String(mark) )
  140.     l = strlen(mark);
  141.   else
  142.     l = 0;
  143.   
  144.   if ( menu )
  145.     {
  146.       if (menu->status & _POSTED)
  147.     {
  148.       /* If the menu is already posted, the geometry is fixed. Then
  149.          we can only accept a mark with exactly the same length */
  150.       if (menu->marklen != l) 
  151.         RETURN(E_BAD_ARGUMENT);
  152.     }    
  153.       menu->mark    = l ? mark : (char *)0;
  154.       menu->marklen = l;
  155.       
  156.       if (menu->status & _POSTED)
  157.     {
  158.       _nc_Draw_Menu( menu );
  159.       _nc_Show_Menu( menu );
  160.     }
  161.       else
  162.     {
  163.       /* Recalculate the geometry */
  164.       _nc_Calculate_Item_Length_and_Width( menu );            
  165.     }
  166.     }
  167.   else
  168.     {
  169.       _nc_Default_Menu.mark    = l ? mark : (char *)0;
  170.       _nc_Default_Menu.marklen = l;
  171.     }
  172.   RETURN(E_OK);
  173. }
  174.  
  175. /*---------------------------------------------------------------------------
  176. |   Facility      :  libnmenu  
  177. |   Function      :  char *menu_mark(const MENU *menu)
  178. |   
  179. |   Description   :  Return a pointer to the marker string
  180. |
  181. |   Return Values :  The marker string pointer or NULL if no marker defined
  182. +--------------------------------------------------------------------------*/
  183. char *menu_mark(const MENU * menu)
  184. {
  185.   return Normalize_Menu( menu )->mark;
  186. }
  187.  
  188. /* m_item_new.c */
  189.