menu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. /* menu text */
  96. tty_setcolor (color);
  97. widget_move (&menubar->widget, y, x);
  98. tty_print_char ((unsigned char) entry->first_letter);
  99. tty_draw_hline (-1, -1, ' ', menu->max_entry_len + 2); /* clear line */
  100. tty_print_string (entry->text.start);
  101. if (entry->text.hotkey != NULL)
  102. {
  103. tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
  104. tty_print_string (entry->text.hotkey);
  105. tty_setcolor (color);
  106. }
  107. if (entry->text.end != NULL)
  108. tty_print_string (entry->text.end);
  109. if (entry->shortcut != NULL)
  110. {
  111. widget_move (&menubar->widget, y, x + menu->max_hotkey_len + 3);
  112. tty_print_string (entry->shortcut);
  113. }
  114. /* move cursor to the start of entry text */
  115. widget_move (&menubar->widget, y, x + 1);
  116. }
  117. }
  118. /* --------------------------------------------------------------------------------------------- */
  119. static void
  120. menubar_draw_drop (WMenuBar * menubar)
  121. {
  122. const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  123. const unsigned int count = g_list_length (menu->entries);
  124. int column = menu->start_x - 1;
  125. unsigned int i;
  126. if (column + menu->max_entry_len + 5 > (gsize) menubar->widget.cols)
  127. column = menubar->widget.cols - menu->max_entry_len - 5;
  128. tty_setcolor (MENU_ENTRY_COLOR);
  129. draw_box (menubar->widget.owner,
  130. menubar->widget.y + 1, menubar->widget.x + column,
  131. count + 2, menu->max_entry_len + 5, FALSE);
  132. for (i = 0; i < count; i++)
  133. menubar_paint_idx (menubar, i,
  134. i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. static void
  138. menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
  139. {
  140. if (!menubar->is_active)
  141. tty_setcolor (MENU_INACTIVE_COLOR);
  142. else if (current)
  143. tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
  144. else
  145. tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. static void
  149. menubar_draw (WMenuBar * menubar)
  150. {
  151. GList *i;
  152. /* First draw the complete menubar */
  153. tty_setcolor (menubar->is_active ? MENU_ENTRY_COLOR : MENU_INACTIVE_COLOR);
  154. tty_draw_hline (menubar->widget.y, menubar->widget.x, ' ', menubar->widget.cols);
  155. /* Now each one of the entries */
  156. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  157. {
  158. Menu *menu = i->data;
  159. gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
  160. menubar_set_color (menubar, is_selected, FALSE);
  161. widget_move (&menubar->widget, 0, menu->start_x);
  162. tty_print_char (' ');
  163. tty_print_string (menu->text.start);
  164. if (menu->text.hotkey != NULL)
  165. {
  166. menubar_set_color (menubar, is_selected, TRUE);
  167. tty_print_string (menu->text.hotkey);
  168. menubar_set_color (menubar, is_selected, FALSE);
  169. }
  170. if (menu->text.end != NULL)
  171. tty_print_string (menu->text.end);
  172. tty_print_char (' ');
  173. }
  174. if (menubar->is_dropped)
  175. menubar_draw_drop (menubar);
  176. else
  177. widget_move (&menubar->widget, 0,
  178. ((Menu *) g_list_nth_data (menubar->menu, menubar->selected))->start_x);
  179. }
  180. /* --------------------------------------------------------------------------------------------- */
  181. static void
  182. menubar_remove (WMenuBar * menubar)
  183. {
  184. if (menubar->is_dropped)
  185. {
  186. menubar->is_dropped = FALSE;
  187. do_refresh ();
  188. menubar->is_dropped = TRUE;
  189. }
  190. }
  191. /* --------------------------------------------------------------------------------------------- */
  192. static void
  193. menubar_left (WMenuBar * menubar)
  194. {
  195. menubar_remove (menubar);
  196. if (menubar->selected == 0)
  197. menubar->selected = g_list_length (menubar->menu) - 1;
  198. else
  199. menubar->selected--;
  200. menubar_draw (menubar);
  201. }
  202. /* --------------------------------------------------------------------------------------------- */
  203. static void
  204. menubar_right (WMenuBar * menubar)
  205. {
  206. menubar_remove (menubar);
  207. menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
  208. menubar_draw (menubar);
  209. }
  210. /* --------------------------------------------------------------------------------------------- */
  211. static void
  212. menubar_finish (WMenuBar * menubar)
  213. {
  214. menubar->is_dropped = FALSE;
  215. menubar->is_active = FALSE;
  216. menubar->widget.lines = 1;
  217. widget_want_hotkey (menubar->widget, 0);
  218. dlg_select_by_id (menubar->widget.owner, menubar->previous_widget);
  219. do_refresh ();
  220. }
  221. /* --------------------------------------------------------------------------------------------- */
  222. static void
  223. menubar_drop (WMenuBar * menubar, unsigned int selected)
  224. {
  225. menubar->is_dropped = TRUE;
  226. menubar->selected = selected;
  227. menubar_draw (menubar);
  228. }
  229. /* --------------------------------------------------------------------------------------------- */
  230. static void
  231. menubar_execute (WMenuBar * menubar)
  232. {
  233. const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  234. const menu_entry_t *entry = g_list_nth_data (menu->entries, menu->selected);
  235. if ((entry != NULL) && (entry->command != CK_IgnoreKey))
  236. {
  237. mc_global.widget.is_right = (menubar->selected != 0);
  238. menubar_finish (menubar);
  239. menubar->widget.owner->callback (menubar->widget.owner, &menubar->widget,
  240. DLG_ACTION, entry->command, NULL);
  241. do_refresh ();
  242. }
  243. }
  244. /* --------------------------------------------------------------------------------------------- */
  245. static void
  246. menubar_down (WMenuBar * menubar)
  247. {
  248. Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  249. const unsigned int len = g_list_length (menu->entries);
  250. menu_entry_t *entry;
  251. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  252. do
  253. {
  254. menu->selected = (menu->selected + 1) % len;
  255. entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
  256. }
  257. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  258. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  259. }
  260. /* --------------------------------------------------------------------------------------------- */
  261. static void
  262. menubar_up (WMenuBar * menubar)
  263. {
  264. Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  265. const unsigned int len = g_list_length (menu->entries);
  266. menu_entry_t *entry;
  267. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  268. do
  269. {
  270. if (menu->selected == 0)
  271. menu->selected = len - 1;
  272. else
  273. menu->selected--;
  274. entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
  275. }
  276. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  277. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  278. }
  279. /* --------------------------------------------------------------------------------------------- */
  280. static void
  281. menubar_first (WMenuBar * menubar)
  282. {
  283. Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  284. menu_entry_t *entry;
  285. if (menu->selected == 0)
  286. return;
  287. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  288. menu->selected = 0;
  289. while (TRUE)
  290. {
  291. entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
  292. if ((entry == NULL) || (entry->command == CK_IgnoreKey))
  293. menu->selected++;
  294. else
  295. break;
  296. }
  297. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  298. }
  299. /* --------------------------------------------------------------------------------------------- */
  300. static void
  301. menubar_last (WMenuBar * menubar)
  302. {
  303. Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  304. const unsigned int len = g_list_length (menu->entries);
  305. menu_entry_t *entry;
  306. if (menu->selected == len - 1)
  307. return;
  308. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  309. menu->selected = len;
  310. do
  311. {
  312. menu->selected--;
  313. entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
  314. }
  315. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  316. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  317. }
  318. /* --------------------------------------------------------------------------------------------- */
  319. static int
  320. menubar_handle_key (WMenuBar * menubar, int key)
  321. {
  322. /* Lowercase */
  323. if (isascii (key))
  324. key = g_ascii_tolower (key);
  325. if (is_abort_char (key))
  326. {
  327. menubar_finish (menubar);
  328. return 1;
  329. }
  330. /* menubar help or menubar navigation */
  331. switch (key)
  332. {
  333. case KEY_F (1):
  334. {
  335. ev_help_t event_data = { NULL, NULL };
  336. if (menubar->is_dropped)
  337. event_data.node =
  338. ((Menu *) g_list_nth_data (menubar->menu, menubar->selected))->help_node;
  339. else
  340. event_data.node = "[Menu Bar]";
  341. mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
  342. menubar_draw (menubar);
  343. return 1;
  344. }
  345. case KEY_LEFT:
  346. case XCTRL ('b'):
  347. menubar_left (menubar);
  348. return 1;
  349. case KEY_RIGHT:
  350. case XCTRL ('f'):
  351. menubar_right (menubar);
  352. return 1;
  353. }
  354. if (!menubar->is_dropped)
  355. {
  356. GList *i;
  357. /* drop menu by hotkey */
  358. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  359. {
  360. Menu *menu = i->data;
  361. if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
  362. {
  363. menubar_drop (menubar, g_list_position (menubar->menu, i));
  364. return 1;
  365. }
  366. }
  367. /* drop menu by Enter or Dowwn key */
  368. if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
  369. menubar_drop (menubar, menubar->selected);
  370. return 1;
  371. }
  372. {
  373. Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
  374. GList *i;
  375. /* execute menu command by hotkey */
  376. for (i = menu->entries; i != NULL; i = g_list_next (i))
  377. {
  378. const menu_entry_t *entry = i->data;
  379. if ((entry != NULL) && (entry->command != CK_IgnoreKey)
  380. && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
  381. {
  382. menu->selected = g_list_position (menu->entries, i);
  383. menubar_execute (menubar);
  384. return 1;
  385. }
  386. }
  387. /* menu execute by Enter or menu navigation */
  388. switch (key)
  389. {
  390. case KEY_ENTER:
  391. case '\n':
  392. menubar_execute (menubar);
  393. return 1;
  394. case KEY_HOME:
  395. case ALT ('<'):
  396. menubar_first (menubar);
  397. break;
  398. case KEY_END:
  399. case ALT ('>'):
  400. menubar_last (menubar);
  401. break;
  402. case KEY_DOWN:
  403. case XCTRL ('n'):
  404. menubar_down (menubar);
  405. break;
  406. case KEY_UP:
  407. case XCTRL ('p'):
  408. menubar_up (menubar);
  409. break;
  410. }
  411. }
  412. return 0;
  413. }
  414. /* --------------------------------------------------------------------------------------------- */
  415. static cb_ret_t
  416. menubar_callback (Widget * w, widget_msg_t msg, int parm)
  417. {
  418. WMenuBar *menubar = (WMenuBar *) w;
  419. switch (msg)
  420. {
  421. /* We do not want the focus unless we have been activated */
  422. case WIDGET_FOCUS:
  423. if (!menubar->is_active)
  424. return MSG_NOT_HANDLED;
  425. /* Trick to get all the mouse events */
  426. menubar->widget.lines = LINES;
  427. /* Trick to get all of the hotkeys */
  428. widget_want_hotkey (menubar->widget, 1);
  429. menubar_draw (menubar);
  430. return MSG_HANDLED;
  431. /* We don't want the buttonbar to activate while using the menubar */
  432. case WIDGET_HOTKEY:
  433. case WIDGET_KEY:
  434. if (menubar->is_active)
  435. {
  436. menubar_handle_key (menubar, parm);
  437. return MSG_HANDLED;
  438. }
  439. return MSG_NOT_HANDLED;
  440. case WIDGET_CURSOR:
  441. /* Put the cursor in a suitable place */
  442. return MSG_NOT_HANDLED;
  443. case WIDGET_UNFOCUS:
  444. return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
  445. case WIDGET_DRAW:
  446. if (menubar->is_visible)
  447. {
  448. menubar_draw (menubar);
  449. return MSG_HANDLED;
  450. }
  451. /* fall through */
  452. case WIDGET_RESIZED:
  453. /* try show menu after screen resize */
  454. send_message (w, WIDGET_FOCUS, 0);
  455. return MSG_HANDLED;
  456. case WIDGET_DESTROY:
  457. menubar_set_menu (menubar, NULL);
  458. return MSG_HANDLED;
  459. default:
  460. return default_proc (msg, parm);
  461. }
  462. }
  463. /* --------------------------------------------------------------------------------------------- */
  464. static int
  465. menubar_event (Gpm_Event * event, void *data)
  466. {
  467. WMenuBar *menubar = data;
  468. gboolean was_active = TRUE;
  469. int left_x, right_x, bottom_y;
  470. Menu *menu;
  471. /* ignore unsupported events */
  472. if ((event->type & (GPM_UP | GPM_DOWN | GPM_DRAG)) == 0)
  473. return MOU_NORMAL;
  474. /* ignore wheel events if menu is inactive */
  475. if (!menubar->is_active && ((event->buttons & (GPM_B_MIDDLE | GPM_B_UP | GPM_B_DOWN)) != 0))
  476. return MOU_NORMAL;
  477. if (event->y == 1 && (event->type & GPM_UP) != 0)
  478. return MOU_NORMAL;
  479. if (!menubar->is_dropped)
  480. {
  481. menubar->previous_widget = dlg_get_current_widget_id (menubar->widget.owner);
  482. menubar->is_active = TRUE;
  483. menubar->is_dropped = TRUE;
  484. was_active = FALSE;
  485. }
  486. /* Mouse operations on the menubar */
  487. if (event->y == 1 || !was_active)
  488. {
  489. /* wheel events on menubar */
  490. if (event->buttons & GPM_B_UP)
  491. menubar_left (menubar);
  492. else if (event->buttons & GPM_B_DOWN)
  493. menubar_right (menubar);
  494. else
  495. {
  496. const unsigned int len = g_list_length (menubar->menu);
  497. unsigned int new_selection = 0;
  498. while ((new_selection < len)
  499. && (event->x > ((Menu *) g_list_nth_data (menubar->menu,
  500. new_selection))->start_x))
  501. new_selection++;
  502. if (new_selection != 0) /* Don't set the invalid value -1 */
  503. new_selection--;
  504. if (!was_active)
  505. {
  506. menubar->selected = new_selection;
  507. dlg_select_widget (menubar);
  508. }
  509. else
  510. {
  511. menubar_remove (menubar);
  512. menubar->selected = new_selection;
  513. }
  514. menubar_draw (menubar);
  515. }
  516. return MOU_NORMAL;
  517. }
  518. if (!menubar->is_dropped || (event->y < 2))
  519. return MOU_NORMAL;
  520. /* middle click -- everywhere */
  521. if (((event->buttons & GPM_B_MIDDLE) != 0) && ((event->type & GPM_DOWN) != 0))
  522. {
  523. menubar_execute (menubar);
  524. return MOU_NORMAL;
  525. }
  526. /* the mouse operation is on the menus or it is not */
  527. menu = (Menu *) g_list_nth_data (menubar->menu, menubar->selected);
  528. left_x = menu->start_x;
  529. right_x = left_x + menu->max_entry_len + 3;
  530. if (right_x > menubar->widget.cols)
  531. {
  532. left_x = menubar->widget.cols - menu->max_entry_len - 3;
  533. right_x = menubar->widget.cols;
  534. }
  535. bottom_y = g_list_length (menu->entries) + 3;
  536. if ((event->x >= left_x) && (event->x <= right_x) && (event->y <= bottom_y))
  537. {
  538. int pos = event->y - 3;
  539. const menu_entry_t *entry = g_list_nth_data (menu->entries, pos);
  540. /* mouse wheel */
  541. if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
  542. {
  543. menubar_up (menubar);
  544. return MOU_NORMAL;
  545. }
  546. if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
  547. {
  548. menubar_down (menubar);
  549. return MOU_NORMAL;
  550. }
  551. /* ignore events above and below dropped down menu */
  552. if ((pos < 0) || (pos >= bottom_y - 3))
  553. return MOU_NORMAL;
  554. if ((entry != NULL) && (entry->command != CK_IgnoreKey))
  555. {
  556. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  557. menu->selected = pos;
  558. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  559. if ((event->type & GPM_UP) != 0)
  560. menubar_execute (menubar);
  561. }
  562. }
  563. else
  564. /* use click not wheel to close menu */
  565. if (((event->type & GPM_DOWN) != 0) && ((event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0))
  566. menubar_finish (menubar);
  567. return MOU_NORMAL;
  568. }
  569. /* --------------------------------------------------------------------------------------------- */
  570. /*** public functions ****************************************************************************/
  571. /* --------------------------------------------------------------------------------------------- */
  572. menu_entry_t *
  573. menu_entry_create (const char *name, unsigned long command)
  574. {
  575. menu_entry_t *entry;
  576. entry = g_new (menu_entry_t, 1);
  577. entry->first_letter = ' ';
  578. entry->text = parse_hotkey (name);
  579. entry->command = command;
  580. entry->shortcut = NULL;
  581. return entry;
  582. }
  583. /* --------------------------------------------------------------------------------------------- */
  584. void
  585. menu_entry_free (menu_entry_t * entry)
  586. {
  587. if (entry != NULL)
  588. {
  589. release_hotkey (entry->text);
  590. g_free (entry->shortcut);
  591. g_free (entry);
  592. }
  593. }
  594. /* --------------------------------------------------------------------------------------------- */
  595. Menu *
  596. create_menu (const char *name, GList * entries, const char *help_node)
  597. {
  598. Menu *menu;
  599. menu = g_new (Menu, 1);
  600. menu->start_x = 0;
  601. menu->text = parse_hotkey (name);
  602. menu->entries = entries;
  603. menu->max_entry_len = 1;
  604. menu->max_hotkey_len = 0;
  605. menu->selected = 0;
  606. menu->help_node = g_strdup (help_node);
  607. return menu;
  608. }
  609. /* --------------------------------------------------------------------------------------------- */
  610. void
  611. menu_set_name (Menu * menu, const char *name)
  612. {
  613. release_hotkey (menu->text);
  614. menu->text = parse_hotkey (name);
  615. }
  616. /* --------------------------------------------------------------------------------------------- */
  617. void
  618. destroy_menu (Menu * menu)
  619. {
  620. release_hotkey (menu->text);
  621. g_list_foreach (menu->entries, (GFunc) menu_entry_free, NULL);
  622. g_list_free (menu->entries);
  623. g_free (menu->help_node);
  624. g_free (menu);
  625. }
  626. /* --------------------------------------------------------------------------------------------- */
  627. WMenuBar *
  628. menubar_new (int y, int x, int cols, GList * menu)
  629. {
  630. WMenuBar *menubar;
  631. menubar = g_new0 (WMenuBar, 1);
  632. init_widget (&menubar->widget, y, x, 1, cols, menubar_callback, menubar_event);
  633. widget_want_cursor (menubar->widget, FALSE);
  634. menubar->is_visible = TRUE; /* by default */
  635. menubar_set_menu (menubar, menu);
  636. return menubar;
  637. }
  638. /* --------------------------------------------------------------------------------------------- */
  639. void
  640. menubar_set_menu (WMenuBar * menubar, GList * menu)
  641. {
  642. /* delete previous menu */
  643. if (menubar->menu != NULL)
  644. {
  645. g_list_foreach (menubar->menu, (GFunc) destroy_menu, NULL);
  646. g_list_free (menubar->menu);
  647. }
  648. /* add new menu */
  649. menubar->is_active = FALSE;
  650. menubar->is_dropped = FALSE;
  651. menubar->menu = menu;
  652. menubar->selected = 0;
  653. menubar_arrange (menubar);
  654. }
  655. /* --------------------------------------------------------------------------------------------- */
  656. void
  657. menubar_add_menu (WMenuBar * menubar, Menu * menu)
  658. {
  659. if (menu != NULL)
  660. {
  661. menu_arrange (menu, menubar->widget.owner->get_shortcut);
  662. menubar->menu = g_list_append (menubar->menu, menu);
  663. }
  664. menubar_arrange (menubar);
  665. }
  666. /* --------------------------------------------------------------------------------------------- */
  667. /**
  668. * Properly space menubar items. Should be called when menubar is created
  669. * and also when widget width is changed (i.e. upon xterm resize).
  670. */
  671. void
  672. menubar_arrange (WMenuBar * menubar)
  673. {
  674. int start_x = 1;
  675. GList *i;
  676. int gap;
  677. if (menubar->menu == NULL)
  678. return;
  679. #ifndef RESIZABLE_MENUBAR
  680. gap = 3;
  681. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  682. {
  683. Menu *menu = i->data;
  684. int len = hotkey_width (menu->text) + 2;
  685. menu->start_x = start_x;
  686. start_x += len + gap;
  687. }
  688. #else /* RESIZABLE_MENUBAR */
  689. gap = menubar->widget.cols - 2;
  690. /* First, calculate gap between items... */
  691. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  692. {
  693. Menu *menu = i->data;
  694. /* preserve length here, to be used below */
  695. menu->start_x = hotkey_width (menu->text) + 2;
  696. gap -= menu->start_x;
  697. }
  698. gap /= (menubar->menu->len - 1);
  699. if (gap <= 0)
  700. {
  701. /* We are out of luck - window is too narrow... */
  702. gap = 1;
  703. }
  704. /* ...and now fix start positions of menubar items */
  705. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  706. {
  707. Menu *menu = i->data;
  708. int len = menu->start_x;
  709. menu->start_x = start_x;
  710. start_x += len + gap;
  711. }
  712. #endif /* RESIZABLE_MENUBAR */
  713. }
  714. /* --------------------------------------------------------------------------------------------- */
  715. /** Find MenuBar widget in the dialog */
  716. WMenuBar *
  717. find_menubar (const Dlg_head * h)
  718. {
  719. return (WMenuBar *) find_widget_type (h, menubar_callback);
  720. }
  721. /* --------------------------------------------------------------------------------------------- */