]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - menu/m_driver.c
ncurses 6.2 - patch 20210828
[ncurses.git] / menu / m_driver.c
index d29c6024a7e3a0c235a2ed0973b4b5a15ab6762f..cf2ef2fe05c63864855d382dff372fb91fefed21 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc.              *
+ * Copyright 2020,2021 Thomas E. Dickey                                     *
+ * Copyright 1998-2012,2016 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 +38,7 @@
 
 #include "menu.priv.h"
 
-MODULE_ID("$Id: m_driver.c,v 1.25 2005/11/26 20:46:59 tom Exp $")
+MODULE_ID("$Id: m_driver.c,v 1.37 2021/03/27 23:46:29 tom Exp $")
 
 /* Macros */
 
@@ -47,7 +48,7 @@ MODULE_ID("$Id: m_driver.c,v 1.25 2005/11/26 20:46:59 tom Exp $")
 
 /* Add a new character to the match pattern buffer */
 #define Add_Character_To_Pattern(menu,ch) \
-  { (menu)->pattern[((menu)->pindex)++] = (ch);\
+  { (menu)->pattern[((menu)->pindex)++] = (char) (ch);\
     (menu)->pattern[(menu)->pindex] = '\0'; }
 
 /*---------------------------------------------------------------------------
@@ -74,7 +75,7 @@ Is_Sub_String(
     {
       while (*string && *part)
        {
-         if (toupper(*string++) != toupper(*part))
+         if (toupper(UChar(*string++)) != toupper(UChar(*part)))
            break;
          part++;
        }
@@ -114,14 +115,15 @@ Is_Sub_String(
 |   Return Values :  E_OK        - an item matching the pattern was found
 |                    E_NO_MATCH  - nothing found
 +--------------------------------------------------------------------------*/
-NCURSES_EXPORT(int)
+MENU_EXPORT(int)
 _nc_Match_Next_Character_In_Item_Name
-(MENU * menu, int ch, ITEM ** item)
+(MENU *menu, int ch, ITEM **item)
 {
   bool found = FALSE, passed = FALSE;
   int idx, last;
 
-  T((T_CALLED("_nc_Match_Next_Character(%p,%d,%p)"), menu, ch, item));
+  T((T_CALLED("_nc_Match_Next_Character(%p,%d,%p)"),
+     (void *)menu, ch, (void *)item));
 
   assert(menu && item && *item);
   idx = (*item)->index;
@@ -137,7 +139,7 @@ _nc_Match_Next_Character_In_Item_Name
       /* we artificially position one item back, because in the do...while
          loop we start with the next item. This means, that with a new
          pattern search we always start the scan with the actual item. If
-         we do a NEXT_PATTERN oder PREV_PATTERN search, we start with the
+         we do a NEXT_PATTERN or PREV_PATTERN search, we start with the
          one after or before the actual item. */
       if (--idx < 0)
        idx = menu->nitems - 1;
@@ -197,7 +199,7 @@ _nc_Match_Next_Character_In_Item_Name
 
 /*---------------------------------------------------------------------------
 |   Facility      :  libnmenu
-|   Function      :  int menu_driver(MENU *menu, int c)
+|   Function      :  int menu_driver(MENUmenu, int c)
 |
 |   Description   :  Central dispatcher for the menu. Translates the logical
 |                    request 'c' into a menu action.
@@ -207,8 +209,8 @@ _nc_Match_Next_Character_In_Item_Name
 |                    E_BAD_STATE     - menu is in user hook routine
 |                    E_NOT_POSTED    - menu is not posted
 +--------------------------------------------------------------------------*/
-NCURSES_EXPORT(int)
-menu_driver(MENU * menu, int c)
+MENU_EXPORT(int)
+menu_driver(MENU *menu, int c)
 {
 #define NAVIGATE(dir) \
   if (!item->dir)\
@@ -218,9 +220,9 @@ menu_driver(MENU * menu, int c)
 
   int result = E_OK;
   ITEM *item;
-  int my_top_row, rdiff;
+  int my_top_row;
 
-  T((T_CALLED("menu_driver(%p,%d)"), menu, c));
+  T((T_CALLED("menu_driver(%p,%d)"), (void *)menu, c));
 
   if (!menu)
     RETURN(E_BAD_ARGUMENT);
@@ -237,6 +239,8 @@ menu_driver(MENU * menu, int c)
 
   if ((c > KEY_MAX) && (c <= MAX_MENU_COMMAND))
     {
+      int rdiff;
+
       if (!((c == REQ_BACK_PATTERN)
            || (c == REQ_NEXT_MATCH) || (c == REQ_PREV_MATCH)))
        {
@@ -303,7 +307,7 @@ menu_driver(MENU * menu, int c)
          else
            {
              my_top_row += rdiff;
-             while (rdiff-- > 0 && item != (ITEM *) 0)
+             while (rdiff-- > 0 && item != 0 && item->down != 0)
                item = item->down;
            }
          break;
@@ -316,7 +320,7 @@ menu_driver(MENU * menu, int c)
          else
            {
              my_top_row -= rdiff;
-             while (rdiff-- && item != (ITEM *) 0)
+             while (rdiff-- > 0 && item != 0 && item->up != 0)
                item = item->up;
            }
          break;
@@ -487,16 +491,20 @@ menu_driver(MENU * menu, int c)
                    }
                  else if (wenclose(sub, event.y, event.x))
                    {           /* Inside the area we try to find the hit item */
-                     int i, x, y, err;
+                     int x, y;
 
                      ry = event.y;
                      rx = event.x;
                      if (wmouse_trafo(sub, &ry, &rx, FALSE))
                        {
+                         int i;
+
                          for (i = 0; i < menu->nitems; i++)
                            {
-                             err = _nc_menu_cursor_pos(menu, menu->items[i],
-                                                       &y, &x);
+                             int err = _nc_menu_cursor_pos(menu,
+                                                           menu->items[i],
+                                                           &y, &x);
+
                              if (E_OK == err)
                                {
                                  if ((ry == y) &&
@@ -529,14 +537,22 @@ menu_driver(MENU * menu, int c)
                }
            }
          else
-           result = E_REQUEST_DENIED;
+           {
+             if (menu->opt & O_MOUSE_MENU)
+               ungetmouse(&event);     /* let someone else handle this */
+             result = E_REQUEST_DENIED;
+           }
        }
 #endif /* NCURSES_MOUSE_VERSION */
       else
        result = E_UNKNOWN_COMMAND;
     }
 
-  if (E_OK == result)
+  if (item == 0)
+    {
+      result = E_BAD_STATE;
+    }
+  else if (E_OK == result)
     {
       /* Adjust the top row if it turns out that the current item unfortunately
          doesn't appear in the menu window */