menu.c 27 KB

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