menu.c 26 KB

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