]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - menu/m_global.c
ncurses 6.1 - patch 20180825
[ncurses.git] / menu / m_global.c
index 5aeb021f7edd442afd2e822f11668f6641d113c2..fc4103c2b88c0f4631493b861beb03be57d9e929 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2012,2014 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            *
@@ -37,7 +37,7 @@
 
 #include "menu.priv.h"
 
-MODULE_ID("$Id: m_global.c,v 1.20 2005/04/16 17:30:57 tom Exp $")
+MODULE_ID("$Id: m_global.c,v 1.28 2014/03/15 20:37:22 tom Exp $")
 
 static char mark[] = "-";
 /* *INDENT-OFF* */
@@ -106,25 +106,28 @@ NCURSES_EXPORT_VAR(ITEM) _nc_Default_Item = {
 |
 |   Return Values :  -
 +--------------------------------------------------------------------------*/
-INLINE static void
+NCURSES_INLINE static void
 ComputeMaximum_NameDesc_Lengths(MENU * menu)
 {
   unsigned MaximumNameLength = 0;
   unsigned MaximumDescriptionLength = 0;
   ITEM **items;
+  unsigned check;
 
   assert(menu && menu->items);
   for (items = menu->items; *items; items++)
     {
-      if (items[0]->name.length > MaximumNameLength)
-       MaximumNameLength = items[0]->name.length;
+      check = (unsigned)_nc_Calculate_Text_Width(&((*items)->name));
+      if (check > MaximumNameLength)
+       MaximumNameLength = check;
 
-      if (items[0]->description.length > MaximumDescriptionLength)
-       MaximumDescriptionLength = items[0]->description.length;
+      check = (unsigned)_nc_Calculate_Text_Width(&((*items)->description));
+      if (check > MaximumDescriptionLength)
+       MaximumDescriptionLength = check;
     }
 
-  menu->namelen = MaximumNameLength;
-  menu->desclen = MaximumDescriptionLength;
+  menu->namelen = (short)MaximumNameLength;
+  menu->desclen = (short)MaximumDescriptionLength;
   T(("ComputeMaximum_NameDesc_Lengths %d,%d", menu->namelen, menu->desclen));
 }
 
@@ -137,7 +140,7 @@ ComputeMaximum_NameDesc_Lengths(MENU * menu)
 |
 |   Return Values :  -
 +--------------------------------------------------------------------------*/
-INLINE static void
+NCURSES_INLINE static void
 ResetConnectionInfo(MENU * menu, ITEM ** items)
 {
   ITEM **item;
@@ -192,7 +195,7 @@ _nc_Connect_Items(MENU * menu, ITEM ** items)
                {
                  (*item)->value = FALSE;
                }
-             (*item)->index = ItemCount++;
+             (*item)->index = (short)ItemCount++;
              (*item)->imenu = menu;
            }
        }
@@ -203,7 +206,7 @@ _nc_Connect_Items(MENU * menu, ITEM ** items)
   if (ItemCount != 0)
     {
       menu->items = items;
-      menu->nitems = ItemCount;
+      menu->nitems = (short)ItemCount;
       ComputeMaximum_NameDesc_Lengths(menu);
       if ((menu->pattern = typeMalloc(char, (unsigned)(1 + menu->namelen))))
        {
@@ -249,26 +252,30 @@ _nc_Calculate_Text_Width(const TEXT * item /*FIXME: limit length */ )
 {
 #if USE_WIDEC_SUPPORT
   int result = item->length;
-  int count = mbstowcs(0, item->str, 0);
-  wchar_t *temp = 0;
 
-  T((T_CALLED("_nc_menu_text_width(%p)"), item));
-  if (count > 0
-      && (temp = typeMalloc(wchar_t, 2 + count)) != 0)
+  T((T_CALLED("_nc_menu_text_width(%p)"), (const void *)item));
+  if (result != 0 && item->str != 0)
     {
-      int n;
+      int count = (int)mbstowcs(0, item->str, 0);
+      wchar_t *temp = 0;
 
-      result = 0;
-      mbstowcs(temp, item->str, (unsigned)count);
-      for (n = 0; n < count; ++n)
+      if (count > 0
+         && (temp = typeMalloc(wchar_t, 2 + count)) != 0)
        {
-         int test = wcwidth(temp[n]);
+         int n;
 
-         if (test <= 0)
-           test = 1;
-         result += test;
+         result = 0;
+         mbstowcs(temp, item->str, (unsigned)count);
+         for (n = 0; n < count; ++n)
+           {
+             int test = wcwidth(temp[n]);
+
+             if (test <= 0)
+               test = 1;
+             result += test;
+           }
+         free(temp);
        }
-      free(temp);
     }
   returnCode(result);
 #else
@@ -276,10 +283,10 @@ _nc_Calculate_Text_Width(const TEXT * item /*FIXME: limit length */ )
 #endif
 }
 
-/* FIXME: this is experimental, should cache the results but don't want to
- * modify the MENU struct to do this until it's complete.
+/*
+ * Calculate the actual width of a menu entry for wide-characters.
  */
-#if 0                          /* USE_WIDEC_SUPPORT */
+#if USE_WIDEC_SUPPORT
 static int
 calculate_actual_width(MENU * menu, bool name)
 {
@@ -288,25 +295,32 @@ calculate_actual_width(MENU * menu, bool name)
   ITEM **items;
 
   assert(menu && menu->items);
-  for (items = menu->items; *items; items++)
+
+  if (menu->items != 0)
     {
-      if (name)
-       {
-         check = _nc_Calculate_Text_Width(&((*items)->name));
-       }
-      else
+      for (items = menu->items; *items; items++)
        {
-         check = _nc_Calculate_Text_Width(&((*items)->description));
+         if (name)
+           {
+             check = _nc_Calculate_Text_Width(&((*items)->name));
+           }
+         else
+           {
+             check = _nc_Calculate_Text_Width(&((*items)->description));
+           }
+         if (check > width)
+           width = check;
        }
-      if (check > width)
-       width = check;
+    }
+  else
+    {
+      width = (name ? menu->namelen : menu->desclen);
     }
 
   T(("calculate_actual_width %s = %d/%d",
      name ? "name" : "desc",
      width,
      name ? menu->namelen : menu->desclen));
-  width += 2;                  /* FIXME - need this? */
   return width;
 }
 #else
@@ -329,7 +343,7 @@ _nc_Calculate_Item_Length_and_Width(MENU * menu)
 
   assert(menu);
 
-  menu->height = 1 + menu->spc_rows * (menu->arows - 1);
+  menu->height = (short)(1 + menu->spc_rows * (menu->arows - 1));
 
   l = calculate_actual_width(menu, TRUE);
   l += menu->marklen;
@@ -340,10 +354,10 @@ _nc_Calculate_Item_Length_and_Width(MENU * menu)
       l += menu->spc_desc;
     }
 
-  menu->itemlen = l;
+  menu->itemlen = (short)l;
   l *= menu->cols;
   l += (menu->cols - 1) * menu->spc_cols;      /* for the padding between the columns */
-  menu->width = l;
+  menu->width = (short)l;
 
   T(("_nc_CalculateItem_Length_and_Width columns %d, item %d, width %d",
      menu->cols,
@@ -374,7 +388,7 @@ _nc_Link_Items(MENU * menu)
       int Last_in_Column;
       bool cycle = (menu->opt & O_NONCYCLIC) ? FALSE : TRUE;
 
-      menu->status &= ~_LINK_NEEDED;
+      ClrStatus(menu, _LINK_NEEDED);
 
       if (menu->opt & O_ROWMAJOR)
        {
@@ -417,8 +431,8 @@ _nc_Link_Items(MENU * menu)
                (cycle ? menu->items[(row + 1) < menu->rows ?
                                     Number_Of_Items - 1 : col] :
                 (ITEM *) 0);
-             item->x = col;
-             item->y = row;
+             item->x = (short)col;
+             item->y = (short)row;
              if (++col == Number_Of_Columns)
                {
                  row++;
@@ -468,8 +482,8 @@ _nc_Link_Items(MENU * menu)
                 (ITEM *) 0
                );
 
-             item->x = col;
-             item->y = row;
+             item->x = (short)col;
+             item->y = (short)row;
              if ((++row) == Number_Of_Rows)
                {
                  col++;
@@ -482,7 +496,7 @@ _nc_Link_Items(MENU * menu)
 
 /*---------------------------------------------------------------------------
 |   Facility      :  libnmenu  
-|   Function      :  void _nc_Show_Menu(const MENU *menu)
+|   Function      :  void _nc_Show_Menu(const MENUmenu)
 |   
 |   Description   :  Update the window that is associated with the menu
 |
@@ -529,8 +543,10 @@ _nc_Show_Menu(const MENU * menu)
 |   Return Values :  -
 +--------------------------------------------------------------------------*/
 NCURSES_EXPORT(void)
-  _nc_New_TopRow_and_CurrentItem
-  (MENU * menu, int new_toprow, ITEM * new_current_item)
+_nc_New_TopRow_and_CurrentItem(
+                               MENU * menu,
+                               int new_toprow,
+                               ITEM * new_current_item)
 {
   ITEM *cur_item;
   bool mterm_called = FALSE;
@@ -552,7 +568,9 @@ NCURSES_EXPORT(void)
 
       cur_item = menu->curitem;
       assert(cur_item);
-      menu->toprow = new_toprow;
+      menu->toprow = (short)(((menu->rows - menu->frows) >= 0)
+                            ? min(menu->rows - menu->frows, new_toprow)
+                            : 0);
       menu->curitem = new_current_item;
 
       if (mterm_called)
@@ -574,7 +592,9 @@ NCURSES_EXPORT(void)
     }
   else
     {                          /* if we are not posted, this is quite simple */
-      menu->toprow = new_toprow;
+      menu->toprow = (short)(((menu->rows - menu->frows) >= 0)
+                            ? min(menu->rows - menu->frows, new_toprow)
+                            : 0);
       menu->curitem = new_current_item;
     }
 }