menu.c 25 KB

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