1 /****************************************************************************
2 * Copyright 2020,2021 Thomas E. Dickey *
3 * Copyright 1998-2012,2016 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Juergen Pfeifer, 1995,1997 *
32 ****************************************************************************/
34 /***************************************************************************
36 * Central dispatching routine *
37 ***************************************************************************/
39 #include "menu.priv.h"
41 MODULE_ID("$Id: m_driver.c,v 1.37 2021/03/27 23:46:29 tom Exp $")
45 /* Remove the last character from the match pattern buffer */
46 #define Remove_Character_From_Pattern(menu) \
47 (menu)->pattern[--((menu)->pindex)] = '\0'
49 /* Add a new character to the match pattern buffer */
50 #define Add_Character_To_Pattern(menu,ch) \
51 { (menu)->pattern[((menu)->pindex)++] = (char) (ch);\
52 (menu)->pattern[(menu)->pindex] = '\0'; }
54 /*---------------------------------------------------------------------------
56 | Function : static bool Is_Sub_String(
57 | bool IgnoreCaseFlag,
61 | Description : Checks whether or not part is a substring of string.
63 | Return Values : TRUE - if it is a substring
64 | FALSE - if it is not a substring
65 +--------------------------------------------------------------------------*/
73 assert(part && string);
76 while (*string && *part)
78 if (toupper(UChar(*string++)) != toupper(UChar(*part)))
85 while (*string && *part)
86 if (*part != *string++)
90 return ((*part) ? FALSE : TRUE);
93 /*---------------------------------------------------------------------------
95 | Function : int _nc_Match_Next_Character_In_Item_Name(
100 | Description : This internal routine is called for a menu positioned
101 | at an item with three different classes of characters:
102 | - a printable character; the character is added to
103 | the current pattern and the next item matching
104 | this pattern is searched.
105 | - NUL; the pattern stays as it is and the next item
106 | matching the pattern is searched
107 | - BS; the pattern stays as it is and the previous
108 | item matching the pattern is searched
110 | The item parameter contains on call a pointer to
111 | the item where the search starts. On return - if
112 | a match was found - it contains a pointer to the
115 | Return Values : E_OK - an item matching the pattern was found
116 | E_NO_MATCH - nothing found
117 +--------------------------------------------------------------------------*/
119 _nc_Match_Next_Character_In_Item_Name
120 (MENU *menu, int ch, ITEM **item)
122 bool found = FALSE, passed = FALSE;
125 T((T_CALLED("_nc_Match_Next_Character(%p,%d,%p)"),
126 (void *)menu, ch, (void *)item));
128 assert(menu && item && *item);
129 idx = (*item)->index;
133 /* if we become to long, we need no further checking : there can't be
135 if ((menu->pindex + 1) > menu->namelen)
138 Add_Character_To_Pattern(menu, ch);
139 /* we artificially position one item back, because in the do...while
140 loop we start with the next item. This means, that with a new
141 pattern search we always start the scan with the actual item. If
142 we do a NEXT_PATTERN or PREV_PATTERN search, we start with the
143 one after or before the actual item. */
145 idx = menu->nitems - 1;
148 last = idx; /* this closes the cycle */
153 { /* we have to go backward */
155 idx = menu->nitems - 1;
158 { /* otherwise we always go forward */
159 if (++idx >= menu->nitems)
162 if (Is_Sub_String((bool)((menu->opt & O_IGNORECASE) != 0),
164 menu->items[idx]->name.str)
170 while (!found && (idx != last));
174 if (!((idx == (*item)->index) && passed))
176 *item = menu->items[idx];
179 /* This point is reached, if we fully cycled through the item list
180 and the only match we found is the starting item. With a NEXT_PATTERN
181 or PREV_PATTERN scan this means, that there was no additional match.
182 If we searched with an expanded new pattern, we should never reach
183 this point, because if the expanded pattern matches also the actual
184 item we will find it in the first attempt (passed==FALSE) and we
185 will never cycle through the whole item array.
187 assert(ch == 0 || ch == BS);
191 if (ch && ch != BS && menu->pindex > 0)
193 /* if we had no match with a new pattern, we have to restore it */
194 Remove_Character_From_Pattern(menu);
200 /*---------------------------------------------------------------------------
201 | Facility : libnmenu
202 | Function : int menu_driver(MENU* menu, int c)
204 | Description : Central dispatcher for the menu. Translates the logical
205 | request 'c' into a menu action.
207 | Return Values : E_OK - success
208 | E_BAD_ARGUMENT - invalid menu pointer
209 | E_BAD_STATE - menu is in user hook routine
210 | E_NOT_POSTED - menu is not posted
211 +--------------------------------------------------------------------------*/
213 menu_driver(MENU *menu, int c)
215 #define NAVIGATE(dir) \
217 result = E_REQUEST_DENIED;\
225 T((T_CALLED("menu_driver(%p,%d)"), (void *)menu, c));
228 RETURN(E_BAD_ARGUMENT);
230 if (menu->status & _IN_DRIVER)
232 if (!(menu->status & _POSTED))
233 RETURN(E_NOT_POSTED);
235 item = menu->curitem;
237 my_top_row = menu->toprow;
240 if ((c > KEY_MAX) && (c <= MAX_MENU_COMMAND))
244 if (!((c == REQ_BACK_PATTERN)
245 || (c == REQ_NEXT_MATCH) || (c == REQ_PREV_MATCH)))
247 assert(menu->pattern);
254 /*=================*/
259 /*==================*/
269 /*=================*/
274 /*=================*/
275 if (my_top_row == 0 || !(item->up))
276 result = E_REQUEST_DENIED;
285 /*=================*/
286 if ((my_top_row + menu->arows >= menu->rows) || !(item->down))
288 /* only if the menu has less items than rows, we can deny the
289 request. Otherwise the epilogue of this routine adjusts the
290 top row if necessary */
291 result = E_REQUEST_DENIED;
301 /*=================*/
302 rdiff = menu->rows - (menu->arows + my_top_row);
303 if (rdiff > menu->arows)
306 result = E_REQUEST_DENIED;
310 while (rdiff-- > 0 && item != 0 && item->down != 0)
316 /*=================*/
317 rdiff = (menu->arows < my_top_row) ? menu->arows : my_top_row;
319 result = E_REQUEST_DENIED;
323 while (rdiff-- > 0 && item != 0 && item->up != 0)
329 /*==================*/
330 item = menu->items[0];
334 /*=================*/
335 item = menu->items[menu->nitems - 1];
339 /*=================*/
340 if ((item->index + 1) >= menu->nitems)
342 if (menu->opt & O_NONCYCLIC)
343 result = E_REQUEST_DENIED;
345 item = menu->items[0];
348 item = menu->items[item->index + 1];
352 /*=================*/
353 if (item->index <= 0)
355 if (menu->opt & O_NONCYCLIC)
356 result = E_REQUEST_DENIED;
358 item = menu->items[menu->nitems - 1];
361 item = menu->items[item->index - 1];
364 case REQ_TOGGLE_ITEM:
365 /*===================*/
366 if (menu->opt & O_ONEVALUE)
368 result = E_REQUEST_DENIED;
372 if (menu->curitem->opt & O_SELECTABLE)
374 menu->curitem->value = !menu->curitem->value;
375 Move_And_Post_Item(menu, menu->curitem);
379 result = E_NOT_SELECTABLE;
383 case REQ_CLEAR_PATTERN:
384 /*=====================*/
385 /* already cleared in prologue */
388 case REQ_BACK_PATTERN:
389 /*====================*/
390 if (menu->pindex > 0)
392 assert(menu->pattern);
393 Remove_Character_From_Pattern(menu);
394 pos_menu_cursor(menu);
397 result = E_REQUEST_DENIED;
401 /*==================*/
402 assert(menu->pattern);
403 if (menu->pattern[0])
404 result = _nc_Match_Next_Character_In_Item_Name(menu, 0, &item);
407 if ((item->index + 1) < menu->nitems)
408 item = menu->items[item->index + 1];
411 if (menu->opt & O_NONCYCLIC)
412 result = E_REQUEST_DENIED;
414 item = menu->items[0];
420 /*==================*/
421 assert(menu->pattern);
422 if (menu->pattern[0])
423 result = _nc_Match_Next_Character_In_Item_Name(menu, BS, &item);
427 item = menu->items[item->index - 1];
430 if (menu->opt & O_NONCYCLIC)
431 result = E_REQUEST_DENIED;
433 item = menu->items[menu->nitems - 1];
440 result = E_UNKNOWN_COMMAND;
445 { /* not a command */
446 if (!(c & ~((int)MAX_REGULAR_CHARACTER)) && isprint(UChar(c)))
447 result = _nc_Match_Next_Character_In_Item_Name(menu, c, &item);
448 #ifdef NCURSES_MOUSE_VERSION
449 else if (KEY_MOUSE == c)
452 WINDOW *uwin = Get_Menu_UserWin(menu);
455 if ((event.bstate & (BUTTON1_CLICKED |
456 BUTTON1_DOUBLE_CLICKED |
457 BUTTON1_TRIPLE_CLICKED))
458 && wenclose(uwin, event.y, event.x))
459 { /* we react only if the click was in the userwin, that means
460 * inside the menu display area or at the decoration window.
462 WINDOW *sub = Get_Menu_Window(menu);
463 int ry = event.y, rx = event.x; /* screen coordinates */
465 result = E_REQUEST_DENIED;
466 if (mouse_trafo(&ry, &rx, FALSE))
467 { /* rx, ry are now "curses" coordinates */
469 { /* we clicked above the display region; this is
470 * interpreted as "scroll up" request
472 if (event.bstate & BUTTON1_CLICKED)
473 result = menu_driver(menu, REQ_SCR_ULINE);
474 else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
475 result = menu_driver(menu, REQ_SCR_UPAGE);
476 else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
477 result = menu_driver(menu, REQ_FIRST_ITEM);
480 else if (ry > sub->_begy + sub->_maxy)
481 { /* we clicked below the display region; this is
482 * interpreted as "scroll down" request
484 if (event.bstate & BUTTON1_CLICKED)
485 result = menu_driver(menu, REQ_SCR_DLINE);
486 else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
487 result = menu_driver(menu, REQ_SCR_DPAGE);
488 else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
489 result = menu_driver(menu, REQ_LAST_ITEM);
492 else if (wenclose(sub, event.y, event.x))
493 { /* Inside the area we try to find the hit item */
498 if (wmouse_trafo(sub, &ry, &rx, FALSE))
502 for (i = 0; i < menu->nitems; i++)
504 int err = _nc_menu_cursor_pos(menu,
512 (rx < x + menu->itemlen))
514 item = menu->items[i];
521 { /* We found an item, now we can handle the click.
522 * A single click just positions the menu cursor
523 * to the clicked item. A double click toggles
526 if (event.bstate & BUTTON1_DOUBLE_CLICKED)
528 _nc_New_TopRow_and_CurrentItem(menu,
531 menu_driver(menu, REQ_TOGGLE_ITEM);
532 result = E_UNKNOWN_COMMAND;
541 if (menu->opt & O_MOUSE_MENU)
542 ungetmouse(&event); /* let someone else handle this */
543 result = E_REQUEST_DENIED;
546 #endif /* NCURSES_MOUSE_VERSION */
548 result = E_UNKNOWN_COMMAND;
553 result = E_BAD_STATE;
555 else if (E_OK == result)
557 /* Adjust the top row if it turns out that the current item unfortunately
558 doesn't appear in the menu window */
559 if (item->y < my_top_row)
560 my_top_row = item->y;
561 else if (item->y >= (my_top_row + menu->arows))
562 my_top_row = item->y - menu->arows + 1;
564 _nc_New_TopRow_and_CurrentItem(menu, my_top_row, item);
571 /* m_driver.c ends here */