menu.c 26 KB

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