]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - menu/m_item_new.c
ncurses 5.6 - patch 20061230
[ncurses.git] / menu / m_item_new.c
index 21caaa14934fa729c5eda25a70942b83e982c38b..d7fe1f8542c2f12d33b5da1dff75d30b19cbce2c 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998 Free Software Foundation, Inc.                        *
+ * Copyright (c) 1998-2005,2006 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -27,7 +27,7 @@
  ****************************************************************************/
 
 /****************************************************************************
- *   Author: Juergen Pfeifer <Juergen.Pfeifer@T-Online.de> 1995,1997        *
+ *   Author:  Juergen Pfeifer, 1995,1997                                    *
  ****************************************************************************/
 
 /***************************************************************************
 
 #include "menu.priv.h"
 
-MODULE_ID("$Id: m_item_new.c,v 1.8 1998/02/11 12:13:50 tom Exp $")
+#if USE_WIDEC_SUPPORT
+#if HAVE_WCTYPE_H
+#include <wctype.h>
+#endif
+#endif
+
+MODULE_ID("$Id: m_item_new.c,v 1.27 2006/12/17 19:47:09 tom Exp $")
 
 /*---------------------------------------------------------------------------
 |   Facility      :  libnmenu  
@@ -50,16 +56,44 @@ MODULE_ID("$Id: m_item_new.c,v 1.8 1998/02/11 12:13:50 tom Exp $")
 |   Return Values :  TRUE     - if string is printable
 |                    FALSE    - if string contains non-printable characters
 +--------------------------------------------------------------------------*/
-static bool Is_Printable_String(const char *s)
+static bool
+Is_Printable_String(const char *s)
 {
+  int result = TRUE;
+
+#if USE_WIDEC_SUPPORT
+  int count = mbstowcs(0, s, 0);
+  wchar_t *temp = 0;
+
+  assert(s);
+
+  if (count > 0
+      && (temp = typeCalloc(wchar_t, (2 + (unsigned)count))) != 0)
+    {
+      int n;
+
+      mbstowcs(temp, s, (unsigned)count);
+      for (n = 0; n < count; ++n)
+       if (!iswprint((wint_t) temp[n]))
+         {
+           result = FALSE;
+           break;
+         }
+      free(temp);
+    }
+#else
   assert(s);
-  while(*s)
+  while (*s)
     {
-      if (!isprint((unsigned char)*s))
-       return FALSE;
+      if (!isprint(UChar(*s)))
+       {
+         result = FALSE;
+         break;
+       }
       s++;
     }
-  return TRUE;
+#endif
+  return result;
 }
 
 /*---------------------------------------------------------------------------
@@ -72,41 +106,46 @@ static bool Is_Printable_String(const char *s)
 |
 |   Return Values :  The item pointer or NULL if creation failed.
 +--------------------------------------------------------------------------*/
-ITEM *new_item(const char *name, const char *description)
+NCURSES_EXPORT(ITEM *)
+new_item(const char *name, const char *description)
 {
   ITEM *item;
-  
-  if ( !name || (*name == '\0') || !Is_Printable_String(name) )
+
+  T((T_CALLED("new_item(\"%s\", \"%s\")"),
+     name ? name : "",
+     description ? description : ""));
+
+  if (!name || (*name == '\0') || !Is_Printable_String(name))
     {
-      item = (ITEM *)0;
-      SET_ERROR( E_BAD_ARGUMENT );
+      item = (ITEM *) 0;
+      SET_ERROR(E_BAD_ARGUMENT);
     }
   else
     {
-      item = (ITEM *)calloc(1,sizeof(ITEM));
+      item = (ITEM *) calloc(1, sizeof(ITEM));
       if (item)
        {
-         *item  = _nc_Default_Item; /* hope we have struct assignment */
-         
-         item->name.length        = strlen(name);
-         item->name.str           = name;
+         *item = _nc_Default_Item;     /* hope we have struct assignment */
+
+         item->name.length = strlen(name);
+         item->name.str = name;
 
-         if (description && (*description != '\0') && 
+         if (description && (*description != '\0') &&
              Is_Printable_String(description))
            {
-             item->description.length = strlen(description);         
-             item->description.str    = description;
+             item->description.length = strlen(description);
+             item->description.str = description;
            }
          else
            {
              item->description.length = 0;
-             item->description.str    = (char *)0;
+             item->description.str = (char *)0;
            }
        }
       else
-       SET_ERROR( E_SYSTEM_ERROR );
-    }  
-  return(item);
+       SET_ERROR(E_SYSTEM_ERROR);
+    }
+  returnItem(item);
 }
 
 /*---------------------------------------------------------------------------
@@ -120,17 +159,20 @@ ITEM *new_item(const char *name, const char *description)
 |                    E_BAD_ARGUMENT    - invalid value has been passed
 |                    E_CONNECTED       - item is still connected to a menu    
 +--------------------------------------------------------------------------*/
-int free_item(ITEM * item)
+NCURSES_EXPORT(int)
+free_item(ITEM * item)
 {
+  T((T_CALLED("free_item(%p)"), item));
+
   if (!item)
-    RETURN( E_BAD_ARGUMENT );
+    RETURN(E_BAD_ARGUMENT);
 
   if (item->imenu)
-    RETURN( E_CONNECTED );
-  
+    RETURN(E_CONNECTED);
+
   free(item);
 
-  RETURN( E_OK );
+  RETURN(E_OK);
 }
 
 /*---------------------------------------------------------------------------
@@ -150,16 +192,19 @@ int free_item(ITEM * item)
 |                    E_BAD_ARGUMENT     - an invalid value has been passed
 |                    E_SYSTEM_ERROR     - no memory to store mark
 +--------------------------------------------------------------------------*/
-int set_menu_mark(MENU * menu, const char * mark)
+NCURSES_EXPORT(int)
+set_menu_mark(MENU * menu, const char *mark)
 {
-  int l;
+  unsigned l;
+
+  T((T_CALLED("set_menu_mark(%p,%s)"), menu, _nc_visbuf(mark)));
 
-  if ( mark && (*mark != '\0') && Is_Printable_String(mark) )
+  if (mark && (*mark != '\0') && Is_Printable_String(mark))
     l = strlen(mark);
   else
     l = 0;
 
-  if ( menu )
+  if (menu)
     {
       char *old_mark = menu->mark;
       unsigned short old_status = menu->status;
@@ -168,17 +213,18 @@ int set_menu_mark(MENU * menu, const char * mark)
        {
          /* If the menu is already posted, the geometry is fixed. Then
             we can only accept a mark with exactly the same length */
-         if (menu->marklen != l) 
+         if (menu->marklen != (int)l)
            RETURN(E_BAD_ARGUMENT);
-       }       
+       }
       menu->marklen = l;
       if (l)
        {
-         menu->mark = (char *)malloc(l+1);
+         menu->mark = (char *)malloc(l + 1);
          if (menu->mark)
            {
              strcpy(menu->mark, mark);
-             menu->status |= _MARK_ALLOCATED;
+             if (menu != &_nc_Default_Menu)
+               menu->status |= _MARK_ALLOCATED;
            }
          else
            {
@@ -188,24 +234,24 @@ int set_menu_mark(MENU * menu, const char * mark)
        }
       else
        menu->mark = (char *)0;
-      
+
       if ((old_status & _MARK_ALLOCATED) && old_mark)
        free(old_mark);
 
       if (menu->status & _POSTED)
        {
-         _nc_Draw_Menu( menu );
-         _nc_Show_Menu( menu );
+         _nc_Draw_Menu(menu);
+         _nc_Show_Menu(menu);
        }
       else
        {
          /* Recalculate the geometry */
-         _nc_Calculate_Item_Length_and_Width( menu );                  
+         _nc_Calculate_Item_Length_and_Width(menu);
        }
     }
   else
     {
-      return set_menu_mark(&_nc_Default_Menu, mark);
+      returnCode(set_menu_mark(&_nc_Default_Menu, mark));
     }
   RETURN(E_OK);
 }
@@ -218,9 +264,11 @@ int set_menu_mark(MENU * menu, const char * mark)
 |
 |   Return Values :  The marker string pointer or NULL if no marker defined
 +--------------------------------------------------------------------------*/
-const char *menu_mark(const MENU * menu)
+NCURSES_EXPORT(const char *)
+menu_mark(const MENU * menu)
 {
-  return Normalize_Menu( menu )->mark;
+  T((T_CALLED("menu_mark(%p)"), menu));
+  returnPtr(Normalize_Menu(menu)->mark);
 }
 
 /* m_item_new.c */