menu.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. Pulldown menu code
  3. Copyright (C) 1994-2017
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2012, 2013, 2016
  7. This file is part of the Midnight Commander.
  8. The Midnight Commander is free software: you can redistribute it
  9. and/or modify it under the terms of the GNU General Public License as
  10. published by the Free Software Foundation, either version 3 of the License,
  11. or (at your option) any later version.
  12. The Midnight Commander is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /** \file menu.c
  20. * \brief Source: pulldown menu code
  21. */
  22. #include <config.h>
  23. #include <ctype.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include "lib/global.h"
  28. #include "lib/tty/tty.h"
  29. #include "lib/skin.h"
  30. #include "lib/tty/key.h" /* key macros */
  31. #include "lib/strutil.h"
  32. #include "lib/widget.h"
  33. #include "lib/event.h" /* mc_event_raise() */
  34. /*** global variables ****************************************************************************/
  35. /*** file scope macro definitions ****************************************************************/
  36. #define MENUENTRY(x) ((menu_entry_t *)(x))
  37. #define MENU(x) ((menu_t *)(x))
  38. /*** file scope type declarations ****************************************************************/
  39. struct menu_entry_t
  40. {
  41. unsigned char first_letter;
  42. hotkey_t text;
  43. long command;
  44. char *shortcut;
  45. };
  46. struct menu_t
  47. {
  48. int start_x; /* position relative to menubar start */
  49. hotkey_t text;
  50. GList *entries;
  51. size_t max_entry_len; /* cached max length of entry texts (text + shortcut) */
  52. size_t max_hotkey_len; /* cached max length of shortcuts */
  53. unsigned int selected; /* pointer to current menu entry */
  54. char *help_node;
  55. };
  56. /*** file scope variables ************************************************************************/
  57. /*** file scope functions ************************************************************************/
  58. /* --------------------------------------------------------------------------------------------- */
  59. static void
  60. menu_arrange (menu_t * menu, dlg_shortcut_str get_shortcut)
  61. {
  62. if (menu != NULL)
  63. {
  64. GList *i;
  65. size_t max_shortcut_len = 0;
  66. menu->max_entry_len = 1;
  67. menu->max_hotkey_len = 1;
  68. for (i = menu->entries; i != NULL; i = g_list_next (i))
  69. {
  70. menu_entry_t *entry = MENUENTRY (i->data);
  71. if (entry != NULL)
  72. {
  73. size_t len;
  74. len = (size_t) hotkey_width (entry->text);
  75. menu->max_hotkey_len = MAX (menu->max_hotkey_len, len);
  76. if (get_shortcut != NULL)
  77. entry->shortcut = get_shortcut (entry->command);
  78. if (entry->shortcut != NULL)
  79. {
  80. len = (size_t) str_term_width1 (entry->shortcut);
  81. max_shortcut_len = MAX (max_shortcut_len, len);
  82. }
  83. }
  84. }
  85. menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
  86. }
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. static void
  90. menubar_paint_idx (WMenuBar * menubar, unsigned int idx, int color)
  91. {
  92. Widget *w = WIDGET (menubar);
  93. const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  94. const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, idx));
  95. const int y = 2 + idx;
  96. int x = menu->start_x;
  97. if (x + menu->max_entry_len + 4 > (gsize) w->cols)
  98. x = w->cols - menu->max_entry_len - 4;
  99. if (entry == NULL)
  100. {
  101. /* menu separator */
  102. tty_setcolor (MENU_ENTRY_COLOR);
  103. widget_move (w, y, x - 1);
  104. tty_print_alt_char (ACS_LTEE, FALSE);
  105. tty_draw_hline (w->y + y, w->x + x, ACS_HLINE, menu->max_entry_len + 3);
  106. widget_move (w, y, x + menu->max_entry_len + 3);
  107. tty_print_alt_char (ACS_RTEE, FALSE);
  108. }
  109. else
  110. {
  111. int yt, xt;
  112. /* menu text */
  113. tty_setcolor (color);
  114. widget_move (w, y, x);
  115. tty_print_char ((unsigned char) entry->first_letter);
  116. tty_getyx (&yt, &xt);
  117. tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2); /* clear line */
  118. tty_print_string (entry->text.start);
  119. if (entry->text.hotkey != NULL)
  120. {
  121. tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
  122. tty_print_string (entry->text.hotkey);
  123. tty_setcolor (color);
  124. }
  125. if (entry->text.end != NULL)
  126. tty_print_string (entry->text.end);
  127. if (entry->shortcut != NULL)
  128. {
  129. widget_move (w, y, x + menu->max_hotkey_len + 3);
  130. tty_print_string (entry->shortcut);
  131. }
  132. /* move cursor to the start of entry text */
  133. widget_move (w, y, x + 1);
  134. }
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. static void
  138. menubar_draw_drop (WMenuBar * menubar)
  139. {
  140. Widget *w = WIDGET (menubar);
  141. const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  142. const unsigned int count = g_list_length (menu->entries);
  143. int column = menu->start_x - 1;
  144. unsigned int i;
  145. if (column + menu->max_entry_len + 5 > (gsize) w->cols)
  146. column = w->cols - menu->max_entry_len - 5;
  147. tty_setcolor (MENU_ENTRY_COLOR);
  148. tty_draw_box (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
  149. for (i = 0; i < count; i++)
  150. menubar_paint_idx (menubar, i,
  151. i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
  152. }
  153. /* --------------------------------------------------------------------------------------------- */
  154. static void
  155. menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
  156. {
  157. if (!widget_get_state (WIDGET (menubar), WST_FOCUSED))
  158. tty_setcolor (MENU_INACTIVE_COLOR);
  159. else if (current)
  160. tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
  161. else
  162. tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. static void
  166. menubar_draw (WMenuBar * menubar)
  167. {
  168. Widget *w = WIDGET (menubar);
  169. GList *i;
  170. /* First draw the complete menubar */
  171. tty_setcolor (widget_get_state (w, WST_FOCUSED) ? MENU_ENTRY_COLOR : MENU_INACTIVE_COLOR);
  172. tty_draw_hline (w->y, w->x, ' ', w->cols);
  173. /* Now each one of the entries */
  174. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  175. {
  176. menu_t *menu = MENU (i->data);
  177. gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
  178. menubar_set_color (menubar, is_selected, FALSE);
  179. widget_move (w, 0, menu->start_x);
  180. tty_print_char (' ');
  181. tty_print_string (menu->text.start);
  182. if (menu->text.hotkey != NULL)
  183. {
  184. menubar_set_color (menubar, is_selected, TRUE);
  185. tty_print_string (menu->text.hotkey);
  186. menubar_set_color (menubar, is_selected, FALSE);
  187. }
  188. if (menu->text.end != NULL)
  189. tty_print_string (menu->text.end);
  190. tty_print_char (' ');
  191. }
  192. if (menubar->is_dropped)
  193. menubar_draw_drop (menubar);
  194. else
  195. widget_move (w, 0, MENU (g_list_nth_data (menubar->menu, menubar->selected))->start_x);
  196. }
  197. /* --------------------------------------------------------------------------------------------- */
  198. static void
  199. menubar_remove (WMenuBar * menubar)
  200. {
  201. WDialog *h;
  202. if (!menubar->is_dropped)
  203. return;
  204. /* HACK: before refresh the dialog, change the current widget to keep the order
  205. of overlapped widgets. This is useful in multi-window editor.
  206. In general, menubar should be a special object, not an ordinary widget
  207. in the current dialog. */
  208. h = WIDGET (menubar)->owner;
  209. h->current = g_list_find (h->widgets, dlg_find_by_id (h, menubar->previous_widget));
  210. menubar->is_dropped = FALSE;
  211. do_refresh ();
  212. menubar->is_dropped = TRUE;
  213. /* restore current widget */
  214. h->current = g_list_find (h->widgets, menubar);
  215. }
  216. /* --------------------------------------------------------------------------------------------- */
  217. static void
  218. menubar_left (WMenuBar * menubar)
  219. {
  220. menubar_remove (menubar);
  221. if (menubar->selected == 0)
  222. menubar->selected = g_list_length (menubar->menu) - 1;
  223. else
  224. menubar->selected--;
  225. menubar_draw (menubar);
  226. }
  227. /* --------------------------------------------------------------------------------------------- */
  228. static void
  229. menubar_right (WMenuBar * menubar)
  230. {
  231. menubar_remove (menubar);
  232. menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
  233. menubar_draw (menubar);
  234. }
  235. /* --------------------------------------------------------------------------------------------- */
  236. static void
  237. menubar_finish (WMenuBar * menubar)
  238. {
  239. Widget *w = WIDGET (menubar);
  240. widget_set_state (w, WST_FOCUSED, FALSE);
  241. menubar->is_dropped = FALSE;
  242. w->lines = 1;
  243. widget_want_hotkey (w, FALSE);
  244. widget_set_options (w, WOP_SELECTABLE, FALSE);
  245. /* Move the menubar to the bottom so that widgets displayed on top of
  246. * an "invisible" menubar get the first chance to respond to mouse events. */
  247. widget_set_bottom (w);
  248. dlg_select_by_id (w->owner, menubar->previous_widget);
  249. do_refresh ();
  250. }
  251. /* --------------------------------------------------------------------------------------------- */
  252. static void
  253. menubar_drop (WMenuBar * menubar, unsigned int selected)
  254. {
  255. menubar->is_dropped = TRUE;
  256. menubar->selected = selected;
  257. menubar_draw (menubar);
  258. }
  259. /* --------------------------------------------------------------------------------------------- */
  260. static void
  261. menubar_execute (WMenuBar * menubar)
  262. {
  263. const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  264. const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  265. if ((entry != NULL) && (entry->command != CK_IgnoreKey))
  266. {
  267. Widget *w = WIDGET (menubar);
  268. mc_global.widget.is_right = (menubar->selected != 0);
  269. menubar_finish (menubar);
  270. send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
  271. do_refresh ();
  272. }
  273. }
  274. /* --------------------------------------------------------------------------------------------- */
  275. static void
  276. menubar_down (WMenuBar * menubar)
  277. {
  278. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  279. const unsigned int len = g_list_length (menu->entries);
  280. menu_entry_t *entry;
  281. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  282. do
  283. {
  284. menu->selected = (menu->selected + 1) % len;
  285. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  286. }
  287. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  288. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  289. }
  290. /* --------------------------------------------------------------------------------------------- */
  291. static void
  292. menubar_up (WMenuBar * menubar)
  293. {
  294. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  295. const unsigned int len = g_list_length (menu->entries);
  296. menu_entry_t *entry;
  297. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  298. do
  299. {
  300. if (menu->selected == 0)
  301. menu->selected = len - 1;
  302. else
  303. menu->selected--;
  304. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  305. }
  306. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  307. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  308. }
  309. /* --------------------------------------------------------------------------------------------- */
  310. static void
  311. menubar_first (WMenuBar * menubar)
  312. {
  313. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  314. if (menu->selected == 0)
  315. return;
  316. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  317. menu->selected = 0;
  318. while (TRUE)
  319. {
  320. menu_entry_t *entry;
  321. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  322. if ((entry == NULL) || (entry->command == CK_IgnoreKey))
  323. menu->selected++;
  324. else
  325. break;
  326. }
  327. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  328. }
  329. /* --------------------------------------------------------------------------------------------- */
  330. static void
  331. menubar_last (WMenuBar * menubar)
  332. {
  333. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  334. const unsigned int len = g_list_length (menu->entries);
  335. menu_entry_t *entry;
  336. if (menu->selected == len - 1)
  337. return;
  338. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  339. menu->selected = len;
  340. do
  341. {
  342. menu->selected--;
  343. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  344. }
  345. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  346. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  347. }
  348. /* --------------------------------------------------------------------------------------------- */
  349. static int
  350. menubar_handle_key (WMenuBar * menubar, int key)
  351. {
  352. /* Lowercase */
  353. if (isascii (key))
  354. key = g_ascii_tolower (key);
  355. if (is_abort_char (key))
  356. {
  357. menubar_finish (menubar);
  358. return 1;
  359. }
  360. /* menubar help or menubar navigation */
  361. switch (key)
  362. {
  363. case KEY_F (1):
  364. {
  365. ev_help_t event_data = { NULL, NULL };
  366. if (menubar->is_dropped)
  367. event_data.node =
  368. MENU (g_list_nth_data (menubar->menu, menubar->selected))->help_node;
  369. else
  370. event_data.node = "[Menu Bar]";
  371. mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
  372. menubar_draw (menubar);
  373. return 1;
  374. }
  375. case KEY_LEFT:
  376. case XCTRL ('b'):
  377. menubar_left (menubar);
  378. return 1;
  379. case KEY_RIGHT:
  380. case XCTRL ('f'):
  381. menubar_right (menubar);
  382. return 1;
  383. default:
  384. break;
  385. }
  386. if (!menubar->is_dropped)
  387. {
  388. GList *i;
  389. /* drop menu by hotkey */
  390. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  391. {
  392. menu_t *menu = MENU (i->data);
  393. if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
  394. {
  395. menubar_drop (menubar, g_list_position (menubar->menu, i));
  396. return 1;
  397. }
  398. }
  399. /* drop menu by Enter or Dowwn key */
  400. if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
  401. menubar_drop (menubar, menubar->selected);
  402. return 1;
  403. }
  404. {
  405. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  406. GList *i;
  407. /* execute menu command by hotkey */
  408. for (i = menu->entries; i != NULL; i = g_list_next (i))
  409. {
  410. const menu_entry_t *entry = MENUENTRY (i->data);
  411. if ((entry != NULL) && (entry->command != CK_IgnoreKey)
  412. && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
  413. {
  414. menu->selected = g_list_position (menu->entries, i);
  415. menubar_execute (menubar);
  416. return 1;
  417. }
  418. }
  419. /* menu execute by Enter or menu navigation */
  420. switch (key)
  421. {
  422. case KEY_ENTER:
  423. case '\n':
  424. menubar_execute (menubar);
  425. return 1;
  426. case KEY_HOME:
  427. case ALT ('<'):
  428. menubar_first (menubar);
  429. break;
  430. case KEY_END:
  431. case ALT ('>'):
  432. menubar_last (menubar);
  433. break;
  434. case KEY_DOWN:
  435. case XCTRL ('n'):
  436. menubar_down (menubar);
  437. break;
  438. case KEY_UP:
  439. case XCTRL ('p'):
  440. menubar_up (menubar);
  441. break;
  442. default:
  443. break;
  444. }
  445. }
  446. return 0;
  447. }
  448. /* --------------------------------------------------------------------------------------------- */
  449. static gboolean
  450. menubar_refresh (WMenuBar * menubar)
  451. {
  452. Widget *w = WIDGET (menubar);
  453. if (!widget_get_state (w, WST_FOCUSED))
  454. return FALSE;
  455. /* Trick to get all the mouse events */
  456. w->lines = LINES;
  457. /* Trick to get all of the hotkeys */
  458. widget_want_hotkey (w, TRUE);
  459. return TRUE;
  460. }
  461. /* --------------------------------------------------------------------------------------------- */
  462. static void
  463. menubar_free_menu (WMenuBar * menubar)
  464. {
  465. if (menubar->menu != NULL)
  466. g_list_free_full (menubar->menu, (GDestroyNotify) destroy_menu);
  467. }
  468. /* --------------------------------------------------------------------------------------------- */
  469. static cb_ret_t
  470. menubar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  471. {
  472. WMenuBar *menubar = MENUBAR (w);
  473. switch (msg)
  474. {
  475. /* We do not want the focus unless we have been activated */
  476. case MSG_FOCUS:
  477. if (menubar_refresh (menubar))
  478. {
  479. menubar_draw (menubar);
  480. return MSG_HANDLED;
  481. }
  482. return MSG_NOT_HANDLED;
  483. case MSG_UNFOCUS:
  484. return widget_get_state (w, WST_FOCUSED) ? MSG_NOT_HANDLED : MSG_HANDLED;
  485. /* We don't want the buttonbar to activate while using the menubar */
  486. case MSG_HOTKEY:
  487. case MSG_KEY:
  488. if (widget_get_state (w, WST_FOCUSED))
  489. {
  490. menubar_handle_key (menubar, parm);
  491. return MSG_HANDLED;
  492. }
  493. return MSG_NOT_HANDLED;
  494. case MSG_CURSOR:
  495. /* Put the cursor in a suitable place */
  496. return MSG_NOT_HANDLED;
  497. case MSG_DRAW:
  498. if (menubar->is_visible || menubar_refresh (menubar))
  499. menubar_draw (menubar);
  500. return MSG_HANDLED;
  501. case MSG_RESIZE:
  502. /* try show menu after screen resize */
  503. menubar_refresh (menubar);
  504. return MSG_HANDLED;
  505. case MSG_DESTROY:
  506. menubar_free_menu (menubar);
  507. return MSG_HANDLED;
  508. default:
  509. return widget_default_callback (w, sender, msg, parm, data);
  510. }
  511. }
  512. /* --------------------------------------------------------------------------------------------- */
  513. static unsigned int
  514. menubar_get_menu_by_x_coord (const WMenuBar * menubar, int x)
  515. {
  516. unsigned int i;
  517. GList *menu;
  518. for (i = 0, menu = menubar->menu;
  519. menu != NULL && x > MENU (menu->data)->start_x; i++, menu = g_list_next (menu))
  520. ;
  521. /* Don't set the invalid value -1 */
  522. if (i != 0)
  523. i--;
  524. return i;
  525. }
  526. /* --------------------------------------------------------------------------------------------- */
  527. static gboolean
  528. menubar_mouse_on_menu (const WMenuBar * menubar, int y, int x)
  529. {
  530. Widget *w = WIDGET (menubar);
  531. menu_t *menu;
  532. int left_x, right_x, bottom_y;
  533. if (!menubar->is_dropped)
  534. return FALSE;
  535. menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  536. left_x = menu->start_x;
  537. right_x = left_x + menu->max_entry_len + 3;
  538. if (right_x > w->cols)
  539. {
  540. left_x = w->cols - (menu->max_entry_len + 3);
  541. right_x = w->cols;
  542. }
  543. bottom_y = g_list_length (menu->entries) + 2; /* skip bar and top frame */
  544. return (x >= left_x && x < right_x && y > 1 && y < bottom_y);
  545. }
  546. /* --------------------------------------------------------------------------------------------- */
  547. static void
  548. menubar_change_selected_item (WMenuBar * menubar, int y)
  549. {
  550. menu_t *menu;
  551. menu_entry_t *entry;
  552. y -= 2; /* skip bar and top frame */
  553. menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  554. entry = MENUENTRY (g_list_nth_data (menu->entries, y));
  555. if (entry != NULL && entry->command != CK_IgnoreKey)
  556. {
  557. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  558. menu->selected = y;
  559. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  560. }
  561. }
  562. /* --------------------------------------------------------------------------------------------- */
  563. static void
  564. menubar_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  565. {
  566. static gboolean was_drag = FALSE;
  567. WMenuBar *menubar = MENUBAR (w);
  568. gboolean mouse_on_drop;
  569. mouse_on_drop = menubar_mouse_on_menu (menubar, event->y, event->x);
  570. switch (msg)
  571. {
  572. case MSG_MOUSE_DOWN:
  573. was_drag = FALSE;
  574. if (event->y == 0)
  575. {
  576. /* events on menubar */
  577. unsigned int selected;
  578. selected = menubar_get_menu_by_x_coord (menubar, event->x);
  579. menubar_activate (menubar, TRUE, selected);
  580. menubar_remove (menubar); /* if already shown */
  581. menubar_drop (menubar, selected);
  582. }
  583. else if (mouse_on_drop)
  584. menubar_change_selected_item (menubar, event->y);
  585. else
  586. {
  587. /* mouse click outside menubar or dropdown -- close menu */
  588. menubar_finish (menubar);
  589. /*
  590. * @FIXME.
  591. *
  592. * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
  593. * events belonging to this click (in case the user drags the mouse,
  594. * of course).
  595. *
  596. * For the time being, we mark this with FIXME as this flag should
  597. * preferably be regarded as "implementation detail" and not be
  598. * touched by us. We should think of some other way of communicating
  599. * this to the system.
  600. */
  601. w->mouse.capture = FALSE;
  602. }
  603. break;
  604. case MSG_MOUSE_UP:
  605. if (was_drag && mouse_on_drop)
  606. menubar_execute (menubar);
  607. was_drag = FALSE;
  608. break;
  609. case MSG_MOUSE_CLICK:
  610. was_drag = FALSE;
  611. if ((event->buttons & GPM_B_MIDDLE) != 0 && event->y > 0 && menubar->is_dropped)
  612. {
  613. /* middle click -- everywhere */
  614. menubar_execute (menubar);
  615. }
  616. else if (mouse_on_drop)
  617. menubar_execute (menubar);
  618. else if (event->y > 0)
  619. /* releasing the mouse button outside the menu -- close menu */
  620. menubar_finish (menubar);
  621. break;
  622. case MSG_MOUSE_DRAG:
  623. if (event->y == 0)
  624. {
  625. menubar_remove (menubar);
  626. menubar_drop (menubar, menubar_get_menu_by_x_coord (menubar, event->x));
  627. }
  628. else if (mouse_on_drop)
  629. menubar_change_selected_item (menubar, event->y);
  630. was_drag = TRUE;
  631. break;
  632. case MSG_MOUSE_SCROLL_UP:
  633. case MSG_MOUSE_SCROLL_DOWN:
  634. was_drag = FALSE;
  635. if (widget_get_state (w, WST_FOCUSED))
  636. {
  637. if (event->y == 0)
  638. {
  639. /* menubar: left/right */
  640. if (msg == MSG_MOUSE_SCROLL_UP)
  641. menubar_left (menubar);
  642. else
  643. menubar_right (menubar);
  644. }
  645. else if (mouse_on_drop)
  646. {
  647. /* drop-down menu: up/down */
  648. if (msg == MSG_MOUSE_SCROLL_UP)
  649. menubar_up (menubar);
  650. else
  651. menubar_down (menubar);
  652. }
  653. }
  654. break;
  655. default:
  656. was_drag = FALSE;
  657. break;
  658. }
  659. }
  660. /* --------------------------------------------------------------------------------------------- */
  661. /*** public functions ****************************************************************************/
  662. /* --------------------------------------------------------------------------------------------- */
  663. menu_entry_t *
  664. menu_entry_create (const char *name, long command)
  665. {
  666. menu_entry_t *entry;
  667. entry = g_new (menu_entry_t, 1);
  668. entry->first_letter = ' ';
  669. entry->text = parse_hotkey (name);
  670. entry->command = command;
  671. entry->shortcut = NULL;
  672. return entry;
  673. }
  674. /* --------------------------------------------------------------------------------------------- */
  675. void
  676. menu_entry_free (menu_entry_t * entry)
  677. {
  678. if (entry != NULL)
  679. {
  680. release_hotkey (entry->text);
  681. g_free (entry->shortcut);
  682. g_free (entry);
  683. }
  684. }
  685. /* --------------------------------------------------------------------------------------------- */
  686. menu_t *
  687. create_menu (const char *name, GList * entries, const char *help_node)
  688. {
  689. menu_t *menu;
  690. menu = g_new (menu_t, 1);
  691. menu->start_x = 0;
  692. menu->text = parse_hotkey (name);
  693. menu->entries = entries;
  694. menu->max_entry_len = 1;
  695. menu->max_hotkey_len = 0;
  696. menu->selected = 0;
  697. menu->help_node = g_strdup (help_node);
  698. return menu;
  699. }
  700. /* --------------------------------------------------------------------------------------------- */
  701. void
  702. menu_set_name (menu_t * menu, const char *name)
  703. {
  704. release_hotkey (menu->text);
  705. menu->text = parse_hotkey (name);
  706. }
  707. /* --------------------------------------------------------------------------------------------- */
  708. void
  709. destroy_menu (menu_t * menu)
  710. {
  711. release_hotkey (menu->text);
  712. g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free);
  713. g_free (menu->help_node);
  714. g_free (menu);
  715. }
  716. /* --------------------------------------------------------------------------------------------- */
  717. WMenuBar *
  718. menubar_new (int y, int x, int cols, GList * menu, gboolean visible)
  719. {
  720. WMenuBar *menubar;
  721. Widget *w;
  722. menubar = g_new0 (WMenuBar, 1);
  723. w = WIDGET (menubar);
  724. widget_init (w, y, x, 1, cols, menubar_callback, menubar_mouse_callback);
  725. /* initially, menubar is not selectable */
  726. widget_set_options (w, WOP_SELECTABLE, FALSE);
  727. w->options |= WOP_TOP_SELECT;
  728. menubar->is_visible = visible;
  729. menubar_set_menu (menubar, menu);
  730. return menubar;
  731. }
  732. /* --------------------------------------------------------------------------------------------- */
  733. void
  734. menubar_set_menu (WMenuBar * menubar, GList * menu)
  735. {
  736. /* delete previous menu */
  737. menubar_free_menu (menubar);
  738. /* add new menu */
  739. menubar->is_dropped = FALSE;
  740. menubar->menu = menu;
  741. menubar->selected = 0;
  742. menubar_arrange (menubar);
  743. widget_set_state (WIDGET (menubar), WST_FOCUSED, FALSE);
  744. }
  745. /* --------------------------------------------------------------------------------------------- */
  746. void
  747. menubar_add_menu (WMenuBar * menubar, menu_t * menu)
  748. {
  749. if (menu != NULL)
  750. {
  751. menu_arrange (menu, WIDGET (menubar)->owner->get_shortcut);
  752. menubar->menu = g_list_append (menubar->menu, menu);
  753. }
  754. menubar_arrange (menubar);
  755. }
  756. /* --------------------------------------------------------------------------------------------- */
  757. /**
  758. * Properly space menubar items. Should be called when menubar is created
  759. * and also when widget width is changed (i.e. upon xterm resize).
  760. */
  761. void
  762. menubar_arrange (WMenuBar * menubar)
  763. {
  764. int start_x = 1;
  765. GList *i;
  766. int gap;
  767. if (menubar->menu == NULL)
  768. return;
  769. gap = WIDGET (menubar)->cols - 2;
  770. /* First, calculate gap between items... */
  771. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  772. {
  773. menu_t *menu = MENU (i->data);
  774. /* preserve length here, to be used below */
  775. menu->start_x = hotkey_width (menu->text) + 2;
  776. gap -= menu->start_x;
  777. }
  778. if (g_list_next (menubar->menu) == NULL)
  779. gap = 1;
  780. else
  781. gap /= (g_list_length (menubar->menu) - 1);
  782. if (gap <= 0)
  783. {
  784. /* We are out of luck - window is too narrow... */
  785. gap = 1;
  786. }
  787. else if (gap >= 3)
  788. gap = 3;
  789. /* ...and now fix start positions of menubar items */
  790. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  791. {
  792. menu_t *menu = MENU (i->data);
  793. int len = menu->start_x;
  794. menu->start_x = start_x;
  795. start_x += len + gap;
  796. }
  797. }
  798. /* --------------------------------------------------------------------------------------------- */
  799. /** Find MenuBar widget in the dialog */
  800. WMenuBar *
  801. find_menubar (const WDialog * h)
  802. {
  803. return MENUBAR (find_widget_type (h, menubar_callback));
  804. }
  805. /* --------------------------------------------------------------------------------------------- */
  806. /**
  807. * Activate menu bar.
  808. *
  809. * @param menubar menu bar object
  810. * @param dropped whether dropdown menus should be drooped or not
  811. * @which number of active dropdown menu
  812. */
  813. void
  814. menubar_activate (WMenuBar * menubar, gboolean dropped, int which)
  815. {
  816. Widget *w = WIDGET (menubar);
  817. if (!widget_get_state (w, WST_FOCUSED))
  818. {
  819. widget_set_options (w, WOP_SELECTABLE, TRUE);
  820. widget_set_state (w, WST_FOCUSED, TRUE); /* FIXME: unneeded? */
  821. menubar->is_dropped = dropped;
  822. if (which >= 0)
  823. menubar->selected = (guint) which;
  824. menubar->previous_widget = dlg_get_current_widget_id (w->owner);
  825. /* Bring it to the top so it receives all mouse events before any other widget.
  826. * See also comment in menubar_finish(). */
  827. widget_select (w);
  828. }
  829. }
  830. /* --------------------------------------------------------------------------------------------- */