m_driver.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /****************************************************************************
  2. * Copyright (c) 1998-2005,2008 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Juergen Pfeifer, 1995,1997 *
  30. ****************************************************************************/
  31. /***************************************************************************
  32. * Module m_driver *
  33. * Central dispatching routine *
  34. ***************************************************************************/
  35. #include "menu.priv.h"
  36. MODULE_ID("$Id: m_driver.c,v 1.27 2008/08/03 22:08:22 tom Exp $")
  37. /* Macros */
  38. /* Remove the last character from the match pattern buffer */
  39. #define Remove_Character_From_Pattern(menu) \
  40. (menu)->pattern[--((menu)->pindex)] = '\0'
  41. /* Add a new character to the match pattern buffer */
  42. #define Add_Character_To_Pattern(menu,ch) \
  43. { (menu)->pattern[((menu)->pindex)++] = (ch);\
  44. (menu)->pattern[(menu)->pindex] = '\0'; }
  45. /*---------------------------------------------------------------------------
  46. | Facility : libnmenu
  47. | Function : static bool Is_Sub_String(
  48. | bool IgnoreCaseFlag,
  49. | const char *part,
  50. | const char *string)
  51. |
  52. | Description : Checks whether or not part is a substring of string.
  53. |
  54. | Return Values : TRUE - if it is a substring
  55. | FALSE - if it is not a substring
  56. +--------------------------------------------------------------------------*/
  57. static bool
  58. Is_Sub_String(
  59. bool IgnoreCaseFlag,
  60. const char *part,
  61. const char *string
  62. )
  63. {
  64. assert(part && string);
  65. if (IgnoreCaseFlag)
  66. {
  67. while (*string && *part)
  68. {
  69. if (toupper(UChar(*string++)) != toupper(UChar(*part)))
  70. break;
  71. part++;
  72. }
  73. }
  74. else
  75. {
  76. while (*string && *part)
  77. if (*part != *string++)
  78. break;
  79. part++;
  80. }
  81. return ((*part) ? FALSE : TRUE);
  82. }
  83. /*---------------------------------------------------------------------------
  84. | Facility : libnmenu
  85. | Function : int _nc_Match_Next_Character_In_Item_Name(
  86. | MENU *menu,
  87. | int ch,
  88. | ITEM **item)
  89. |
  90. | Description : This internal routine is called for a menu positioned
  91. | at an item with three different classes of characters:
  92. | - a printable character; the character is added to
  93. | the current pattern and the next item matching
  94. | this pattern is searched.
  95. | - NUL; the pattern stays as it is and the next item
  96. | matching the pattern is searched
  97. | - BS; the pattern stays as it is and the previous
  98. | item matching the pattern is searched
  99. |
  100. | The item parameter contains on call a pointer to
  101. | the item where the search starts. On return - if
  102. | a match was found - it contains a pointer to the
  103. | matching item.
  104. |
  105. | Return Values : E_OK - an item matching the pattern was found
  106. | E_NO_MATCH - nothing found
  107. +--------------------------------------------------------------------------*/
  108. NCURSES_EXPORT(int)
  109. _nc_Match_Next_Character_In_Item_Name
  110. (MENU * menu, int ch, ITEM ** item)
  111. {
  112. bool found = FALSE, passed = FALSE;
  113. int idx, last;
  114. T((T_CALLED("_nc_Match_Next_Character(%p,%d,%p)"), menu, ch, item));
  115. assert(menu && item && *item);
  116. idx = (*item)->index;
  117. if (ch && ch != BS)
  118. {
  119. /* if we become to long, we need no further checking : there can't be
  120. a match ! */
  121. if ((menu->pindex + 1) > menu->namelen)
  122. RETURN(E_NO_MATCH);
  123. Add_Character_To_Pattern(menu, ch);
  124. /* we artificially position one item back, because in the do...while
  125. loop we start with the next item. This means, that with a new
  126. pattern search we always start the scan with the actual item. If
  127. we do a NEXT_PATTERN oder PREV_PATTERN search, we start with the
  128. one after or before the actual item. */
  129. if (--idx < 0)
  130. idx = menu->nitems - 1;
  131. }
  132. last = idx; /* this closes the cycle */
  133. do
  134. {
  135. if (ch == BS)
  136. { /* we have to go backward */
  137. if (--idx < 0)
  138. idx = menu->nitems - 1;
  139. }
  140. else
  141. { /* otherwise we always go forward */
  142. if (++idx >= menu->nitems)
  143. idx = 0;
  144. }
  145. if (Is_Sub_String((bool)((menu->opt & O_IGNORECASE) != 0),
  146. menu->pattern,
  147. menu->items[idx]->name.str)
  148. )
  149. found = TRUE;
  150. else
  151. passed = TRUE;
  152. }
  153. while (!found && (idx != last));
  154. if (found)
  155. {
  156. if (!((idx == (*item)->index) && passed))
  157. {
  158. *item = menu->items[idx];
  159. RETURN(E_OK);
  160. }
  161. /* This point is reached, if we fully cycled through the item list
  162. and the only match we found is the starting item. With a NEXT_PATTERN
  163. or PREV_PATTERN scan this means, that there was no additional match.
  164. If we searched with an expanded new pattern, we should never reach
  165. this point, because if the expanded pattern matches also the actual
  166. item we will find it in the first attempt (passed==FALSE) and we
  167. will never cycle through the whole item array.
  168. */
  169. assert(ch == 0 || ch == BS);
  170. }
  171. else
  172. {
  173. if (ch && ch != BS && menu->pindex > 0)
  174. {
  175. /* if we had no match with a new pattern, we have to restore it */
  176. Remove_Character_From_Pattern(menu);
  177. }
  178. }
  179. RETURN(E_NO_MATCH);
  180. }
  181. /*---------------------------------------------------------------------------
  182. | Facility : libnmenu
  183. | Function : int menu_driver(MENU *menu, int c)
  184. |
  185. | Description : Central dispatcher for the menu. Translates the logical
  186. | request 'c' into a menu action.
  187. |
  188. | Return Values : E_OK - success
  189. | E_BAD_ARGUMENT - invalid menu pointer
  190. | E_BAD_STATE - menu is in user hook routine
  191. | E_NOT_POSTED - menu is not posted
  192. +--------------------------------------------------------------------------*/
  193. NCURSES_EXPORT(int)
  194. menu_driver(MENU * menu, int c)
  195. {
  196. #define NAVIGATE(dir) \
  197. if (!item->dir)\
  198. result = E_REQUEST_DENIED;\
  199. else\
  200. item = item->dir
  201. int result = E_OK;
  202. ITEM *item;
  203. int my_top_row, rdiff;
  204. T((T_CALLED("menu_driver(%p,%d)"), menu, c));
  205. if (!menu)
  206. RETURN(E_BAD_ARGUMENT);
  207. if (menu->status & _IN_DRIVER)
  208. RETURN(E_BAD_STATE);
  209. if (!(menu->status & _POSTED))
  210. RETURN(E_NOT_POSTED);
  211. item = menu->curitem;
  212. my_top_row = menu->toprow;
  213. assert(item);
  214. if ((c > KEY_MAX) && (c <= MAX_MENU_COMMAND))
  215. {
  216. if (!((c == REQ_BACK_PATTERN)
  217. || (c == REQ_NEXT_MATCH) || (c == REQ_PREV_MATCH)))
  218. {
  219. assert(menu->pattern);
  220. Reset_Pattern(menu);
  221. }
  222. switch (c)
  223. {
  224. case REQ_LEFT_ITEM:
  225. /*=================*/
  226. NAVIGATE(left);
  227. break;
  228. case REQ_RIGHT_ITEM:
  229. /*==================*/
  230. NAVIGATE(right);
  231. break;
  232. case REQ_UP_ITEM:
  233. /*===============*/
  234. NAVIGATE(up);
  235. break;
  236. case REQ_DOWN_ITEM:
  237. /*=================*/
  238. NAVIGATE(down);
  239. break;
  240. case REQ_SCR_ULINE:
  241. /*=================*/
  242. if (my_top_row == 0 || !(item->up))
  243. result = E_REQUEST_DENIED;
  244. else
  245. {
  246. --my_top_row;
  247. item = item->up;
  248. }
  249. break;
  250. case REQ_SCR_DLINE:
  251. /*=================*/
  252. if ((my_top_row + menu->arows >= menu->rows) || !(item->down))
  253. {
  254. /* only if the menu has less items than rows, we can deny the
  255. request. Otherwise the epilogue of this routine adjusts the
  256. top row if necessary */
  257. result = E_REQUEST_DENIED;
  258. }
  259. else
  260. {
  261. my_top_row++;
  262. item = item->down;
  263. }
  264. break;
  265. case REQ_SCR_DPAGE:
  266. /*=================*/
  267. rdiff = menu->rows - (menu->arows + my_top_row);
  268. if (rdiff > menu->arows)
  269. rdiff = menu->arows;
  270. if (rdiff <= 0)
  271. result = E_REQUEST_DENIED;
  272. else
  273. {
  274. my_top_row += rdiff;
  275. while (rdiff-- > 0 && item != 0 && item->down != 0)
  276. item = item->down;
  277. }
  278. break;
  279. case REQ_SCR_UPAGE:
  280. /*=================*/
  281. rdiff = (menu->arows < my_top_row) ? menu->arows : my_top_row;
  282. if (rdiff <= 0)
  283. result = E_REQUEST_DENIED;
  284. else
  285. {
  286. my_top_row -= rdiff;
  287. while (rdiff-- > 0 && item != 0 && item->up != 0)
  288. item = item->up;
  289. }
  290. break;
  291. case REQ_FIRST_ITEM:
  292. /*==================*/
  293. item = menu->items[0];
  294. break;
  295. case REQ_LAST_ITEM:
  296. /*=================*/
  297. item = menu->items[menu->nitems - 1];
  298. break;
  299. case REQ_NEXT_ITEM:
  300. /*=================*/
  301. if ((item->index + 1) >= menu->nitems)
  302. {
  303. if (menu->opt & O_NONCYCLIC)
  304. result = E_REQUEST_DENIED;
  305. else
  306. item = menu->items[0];
  307. }
  308. else
  309. item = menu->items[item->index + 1];
  310. break;
  311. case REQ_PREV_ITEM:
  312. /*=================*/
  313. if (item->index <= 0)
  314. {
  315. if (menu->opt & O_NONCYCLIC)
  316. result = E_REQUEST_DENIED;
  317. else
  318. item = menu->items[menu->nitems - 1];
  319. }
  320. else
  321. item = menu->items[item->index - 1];
  322. break;
  323. case REQ_TOGGLE_ITEM:
  324. /*===================*/
  325. if (menu->opt & O_ONEVALUE)
  326. {
  327. result = E_REQUEST_DENIED;
  328. }
  329. else
  330. {
  331. if (menu->curitem->opt & O_SELECTABLE)
  332. {
  333. menu->curitem->value = !menu->curitem->value;
  334. Move_And_Post_Item(menu, menu->curitem);
  335. _nc_Show_Menu(menu);
  336. }
  337. else
  338. result = E_NOT_SELECTABLE;
  339. }
  340. break;
  341. case REQ_CLEAR_PATTERN:
  342. /*=====================*/
  343. /* already cleared in prologue */
  344. break;
  345. case REQ_BACK_PATTERN:
  346. /*====================*/
  347. if (menu->pindex > 0)
  348. {
  349. assert(menu->pattern);
  350. Remove_Character_From_Pattern(menu);
  351. pos_menu_cursor(menu);
  352. }
  353. else
  354. result = E_REQUEST_DENIED;
  355. break;
  356. case REQ_NEXT_MATCH:
  357. /*==================*/
  358. assert(menu->pattern);
  359. if (menu->pattern[0])
  360. result = _nc_Match_Next_Character_In_Item_Name(menu, 0, &item);
  361. else
  362. {
  363. if ((item->index + 1) < menu->nitems)
  364. item = menu->items[item->index + 1];
  365. else
  366. {
  367. if (menu->opt & O_NONCYCLIC)
  368. result = E_REQUEST_DENIED;
  369. else
  370. item = menu->items[0];
  371. }
  372. }
  373. break;
  374. case REQ_PREV_MATCH:
  375. /*==================*/
  376. assert(menu->pattern);
  377. if (menu->pattern[0])
  378. result = _nc_Match_Next_Character_In_Item_Name(menu, BS, &item);
  379. else
  380. {
  381. if (item->index)
  382. item = menu->items[item->index - 1];
  383. else
  384. {
  385. if (menu->opt & O_NONCYCLIC)
  386. result = E_REQUEST_DENIED;
  387. else
  388. item = menu->items[menu->nitems - 1];
  389. }
  390. }
  391. break;
  392. default:
  393. /*======*/
  394. result = E_UNKNOWN_COMMAND;
  395. break;
  396. }
  397. }
  398. else
  399. { /* not a command */
  400. if (!(c & ~((int)MAX_REGULAR_CHARACTER)) && isprint(UChar(c)))
  401. result = _nc_Match_Next_Character_In_Item_Name(menu, c, &item);
  402. #ifdef NCURSES_MOUSE_VERSION
  403. else if (KEY_MOUSE == c)
  404. {
  405. MEVENT event;
  406. WINDOW *uwin = Get_Menu_UserWin(menu);
  407. getmouse(&event);
  408. if ((event.bstate & (BUTTON1_CLICKED |
  409. BUTTON1_DOUBLE_CLICKED |
  410. BUTTON1_TRIPLE_CLICKED))
  411. && wenclose(uwin, event.y, event.x))
  412. { /* we react only if the click was in the userwin, that means
  413. * inside the menu display area or at the decoration window.
  414. */
  415. WINDOW *sub = Get_Menu_Window(menu);
  416. int ry = event.y, rx = event.x; /* screen coordinates */
  417. result = E_REQUEST_DENIED;
  418. if (mouse_trafo(&ry, &rx, FALSE))
  419. { /* rx, ry are now "curses" coordinates */
  420. if (ry < sub->_begy)
  421. { /* we clicked above the display region; this is
  422. * interpreted as "scroll up" request
  423. */
  424. if (event.bstate & BUTTON1_CLICKED)
  425. result = menu_driver(menu, REQ_SCR_ULINE);
  426. else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
  427. result = menu_driver(menu, REQ_SCR_UPAGE);
  428. else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
  429. result = menu_driver(menu, REQ_FIRST_ITEM);
  430. RETURN(result);
  431. }
  432. else if (ry > sub->_begy + sub->_maxy)
  433. { /* we clicked below the display region; this is
  434. * interpreted as "scroll down" request
  435. */
  436. if (event.bstate & BUTTON1_CLICKED)
  437. result = menu_driver(menu, REQ_SCR_DLINE);
  438. else if (event.bstate & BUTTON1_DOUBLE_CLICKED)
  439. result = menu_driver(menu, REQ_SCR_DPAGE);
  440. else if (event.bstate & BUTTON1_TRIPLE_CLICKED)
  441. result = menu_driver(menu, REQ_LAST_ITEM);
  442. RETURN(result);
  443. }
  444. else if (wenclose(sub, event.y, event.x))
  445. { /* Inside the area we try to find the hit item */
  446. int i, x, y, err;
  447. ry = event.y;
  448. rx = event.x;
  449. if (wmouse_trafo(sub, &ry, &rx, FALSE))
  450. {
  451. for (i = 0; i < menu->nitems; i++)
  452. {
  453. err = _nc_menu_cursor_pos(menu, menu->items[i],
  454. &y, &x);
  455. if (E_OK == err)
  456. {
  457. if ((ry == y) &&
  458. (rx >= x) &&
  459. (rx < x + menu->itemlen))
  460. {
  461. item = menu->items[i];
  462. result = E_OK;
  463. break;
  464. }
  465. }
  466. }
  467. if (E_OK == result)
  468. { /* We found an item, now we can handle the click.
  469. * A single click just positions the menu cursor
  470. * to the clicked item. A double click toggles
  471. * the item.
  472. */
  473. if (event.bstate & BUTTON1_DOUBLE_CLICKED)
  474. {
  475. _nc_New_TopRow_and_CurrentItem(menu,
  476. my_top_row,
  477. item);
  478. menu_driver(menu, REQ_TOGGLE_ITEM);
  479. result = E_UNKNOWN_COMMAND;
  480. }
  481. }
  482. }
  483. }
  484. }
  485. }
  486. else
  487. result = E_REQUEST_DENIED;
  488. }
  489. #endif /* NCURSES_MOUSE_VERSION */
  490. else
  491. result = E_UNKNOWN_COMMAND;
  492. }
  493. if (E_OK == result)
  494. {
  495. /* Adjust the top row if it turns out that the current item unfortunately
  496. doesn't appear in the menu window */
  497. if (item->y < my_top_row)
  498. my_top_row = item->y;
  499. else if (item->y >= (my_top_row + menu->arows))
  500. my_top_row = item->y - menu->arows + 1;
  501. _nc_New_TopRow_and_CurrentItem(menu, my_top_row, item);
  502. }
  503. RETURN(result);
  504. }
  505. /* m_driver.c ends here */