menu.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  1. /*
  2. Pulldown menu code
  3. Copyright (C) 1994-2023
  4. Free Software Foundation, Inc.
  5. Written by:
  6. Andrew Borodin <aborodin@vmail.ru>, 2012-2022
  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/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. const global_keymap_t *menu_map = NULL;
  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. 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 (const WMenuBar * menubar, unsigned int idx, int color)
  92. {
  93. const WRect *w = &CONST_WIDGET (menubar)->rect;
  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_gotoyx (menubar, 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_gotoyx (menubar, 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_gotoyx (menubar, 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_gotoyx (menubar, 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_gotoyx (menubar, y, x + 1);
  135. }
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. static void
  139. menubar_draw_drop (const WMenuBar * menubar)
  140. {
  141. const WRect *w = &CONST_WIDGET (menubar)->rect;
  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. if (mc_global.tty.shadows)
  149. tty_draw_box_shadow (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5,
  150. SHADOW_COLOR);
  151. tty_setcolor (MENU_ENTRY_COLOR);
  152. tty_draw_box (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
  153. for (i = 0; i < count; i++)
  154. menubar_paint_idx (menubar, i,
  155. i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
  156. }
  157. /* --------------------------------------------------------------------------------------------- */
  158. static void
  159. menubar_set_color (const WMenuBar * menubar, gboolean current, gboolean hotkey)
  160. {
  161. if (!widget_get_state (CONST_WIDGET (menubar), WST_FOCUSED))
  162. tty_setcolor (MENU_INACTIVE_COLOR);
  163. else if (current)
  164. tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
  165. else
  166. tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
  167. }
  168. /* --------------------------------------------------------------------------------------------- */
  169. static void
  170. menubar_draw (const WMenuBar * menubar)
  171. {
  172. const WRect *w = &CONST_WIDGET (menubar)->rect;
  173. GList *i;
  174. /* First draw the complete menubar */
  175. tty_setcolor (widget_get_state (WIDGET (menubar), WST_FOCUSED) ? MENU_ENTRY_COLOR :
  176. MENU_INACTIVE_COLOR);
  177. tty_draw_hline (w->y, w->x, ' ', w->cols);
  178. /* Now each one of the entries */
  179. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  180. {
  181. menu_t *menu = MENU (i->data);
  182. gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
  183. menubar_set_color (menubar, is_selected, FALSE);
  184. widget_gotoyx (menubar, 0, menu->start_x);
  185. tty_print_char (' ');
  186. tty_print_string (menu->text.start);
  187. if (menu->text.hotkey != NULL)
  188. {
  189. menubar_set_color (menubar, is_selected, TRUE);
  190. tty_print_string (menu->text.hotkey);
  191. menubar_set_color (menubar, is_selected, FALSE);
  192. }
  193. if (menu->text.end != NULL)
  194. tty_print_string (menu->text.end);
  195. tty_print_char (' ');
  196. }
  197. if (menubar->is_dropped)
  198. menubar_draw_drop (menubar);
  199. else
  200. widget_gotoyx (menubar, 0,
  201. MENU (g_list_nth_data (menubar->menu, menubar->selected))->start_x);
  202. }
  203. /* --------------------------------------------------------------------------------------------- */
  204. static void
  205. menubar_remove (WMenuBar * menubar)
  206. {
  207. Widget *g;
  208. if (!menubar->is_dropped)
  209. return;
  210. /* HACK: before refresh the dialog, change the current widget to keep the order
  211. of overlapped widgets. This is useful in multi-window editor.
  212. In general, menubar should be a special object, not an ordinary widget
  213. in the current dialog. */
  214. g = WIDGET (WIDGET (menubar)->owner);
  215. GROUP (g)->current = widget_find (g, widget_find_by_id (g, menubar->previous_widget));
  216. menubar->is_dropped = FALSE;
  217. do_refresh ();
  218. menubar->is_dropped = TRUE;
  219. /* restore current widget */
  220. GROUP (g)->current = widget_find (g, WIDGET (menubar));
  221. }
  222. /* --------------------------------------------------------------------------------------------- */
  223. static void
  224. menubar_left (WMenuBar * menubar)
  225. {
  226. menubar_remove (menubar);
  227. if (menubar->selected == 0)
  228. menubar->selected = g_list_length (menubar->menu) - 1;
  229. else
  230. menubar->selected--;
  231. menubar_draw (menubar);
  232. }
  233. /* --------------------------------------------------------------------------------------------- */
  234. static void
  235. menubar_right (WMenuBar * menubar)
  236. {
  237. menubar_remove (menubar);
  238. menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
  239. menubar_draw (menubar);
  240. }
  241. /* --------------------------------------------------------------------------------------------- */
  242. static void
  243. menubar_finish (WMenuBar * menubar)
  244. {
  245. Widget *w = WIDGET (menubar);
  246. menubar->is_dropped = FALSE;
  247. w->rect.lines = 1;
  248. widget_want_hotkey (w, FALSE);
  249. widget_set_options (w, WOP_SELECTABLE, FALSE);
  250. if (!mc_global.keybar_visible)
  251. widget_hide (w);
  252. else
  253. {
  254. /* Move the menubar to the bottom so that widgets displayed on top of
  255. * an "invisible" menubar get the first chance to respond to mouse events. */
  256. widget_set_bottom (w);
  257. }
  258. /* background must be bottom */
  259. if (DIALOG (w->owner)->bg != NULL)
  260. widget_set_bottom (WIDGET (DIALOG (w->owner)->bg));
  261. group_select_widget_by_id (w->owner, menubar->previous_widget);
  262. do_refresh ();
  263. }
  264. /* --------------------------------------------------------------------------------------------- */
  265. static void
  266. menubar_drop (WMenuBar * menubar, unsigned int selected)
  267. {
  268. menubar->is_dropped = TRUE;
  269. menubar->selected = selected;
  270. menubar_draw (menubar);
  271. }
  272. /* --------------------------------------------------------------------------------------------- */
  273. static void
  274. menubar_execute (WMenuBar * menubar)
  275. {
  276. const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  277. const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  278. if ((entry != NULL) && (entry->command != CK_IgnoreKey))
  279. {
  280. Widget *w = WIDGET (menubar);
  281. mc_global.widget.is_right = (menubar->selected != 0);
  282. menubar_finish (menubar);
  283. send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
  284. do_refresh ();
  285. }
  286. }
  287. /* --------------------------------------------------------------------------------------------- */
  288. static void
  289. menubar_down (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. menu->selected = (menu->selected + 1) % len;
  298. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  299. }
  300. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  301. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  302. }
  303. /* --------------------------------------------------------------------------------------------- */
  304. static void
  305. menubar_up (WMenuBar * menubar)
  306. {
  307. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  308. const unsigned int len = g_list_length (menu->entries);
  309. menu_entry_t *entry;
  310. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  311. do
  312. {
  313. if (menu->selected == 0)
  314. menu->selected = len - 1;
  315. else
  316. menu->selected--;
  317. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  318. }
  319. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  320. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  321. }
  322. /* --------------------------------------------------------------------------------------------- */
  323. static void
  324. menubar_first (WMenuBar * menubar)
  325. {
  326. if (menubar->is_dropped)
  327. {
  328. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  329. if (menu->selected == 0)
  330. return;
  331. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  332. menu->selected = 0;
  333. while (TRUE)
  334. {
  335. menu_entry_t *entry;
  336. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  337. if ((entry == NULL) || (entry->command == CK_IgnoreKey))
  338. menu->selected++;
  339. else
  340. break;
  341. }
  342. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  343. }
  344. else
  345. {
  346. menubar->selected = 0;
  347. menubar_draw (menubar);
  348. }
  349. }
  350. /* --------------------------------------------------------------------------------------------- */
  351. static void
  352. menubar_last (WMenuBar * menubar)
  353. {
  354. if (menubar->is_dropped)
  355. {
  356. menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  357. const unsigned int len = g_list_length (menu->entries);
  358. menu_entry_t *entry;
  359. if (menu->selected == len - 1)
  360. return;
  361. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  362. menu->selected = len;
  363. do
  364. {
  365. menu->selected--;
  366. entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
  367. }
  368. while ((entry == NULL) || (entry->command == CK_IgnoreKey));
  369. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  370. }
  371. else
  372. {
  373. menubar->selected = g_list_length (menubar->menu) - 1;
  374. menubar_draw (menubar);
  375. }
  376. }
  377. /* --------------------------------------------------------------------------------------------- */
  378. static cb_ret_t
  379. menubar_try_drop_menu (WMenuBar * menubar, int hotkey)
  380. {
  381. GList *i;
  382. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  383. {
  384. menu_t *menu = MENU (i->data);
  385. if (menu->text.hotkey != NULL && hotkey == g_ascii_tolower (menu->text.hotkey[0]))
  386. {
  387. menubar_drop (menubar, g_list_position (menubar->menu, i));
  388. return MSG_HANDLED;
  389. }
  390. }
  391. return MSG_NOT_HANDLED;
  392. }
  393. /* --------------------------------------------------------------------------------------------- */
  394. static cb_ret_t
  395. menubar_try_exec_menu (WMenuBar * menubar, int hotkey)
  396. {
  397. menu_t *menu;
  398. GList *i;
  399. menu = g_list_nth_data (menubar->menu, menubar->selected);
  400. for (i = menu->entries; i != NULL; i = g_list_next (i))
  401. {
  402. const menu_entry_t *entry = MENUENTRY (i->data);
  403. if (entry != NULL && entry->text.hotkey != NULL
  404. && hotkey == g_ascii_tolower (entry->text.hotkey[0]))
  405. {
  406. menu->selected = g_list_position (menu->entries, i);
  407. menubar_execute (menubar);
  408. return MSG_HANDLED;
  409. }
  410. }
  411. return MSG_NOT_HANDLED;
  412. }
  413. /* --------------------------------------------------------------------------------------------- */
  414. static cb_ret_t
  415. menubar_execute_cmd (WMenuBar * menubar, long command)
  416. {
  417. cb_ret_t ret = MSG_HANDLED;
  418. switch (command)
  419. {
  420. case CK_Help:
  421. {
  422. ev_help_t event_data = { NULL, NULL };
  423. if (menubar->is_dropped)
  424. event_data.node =
  425. MENU (g_list_nth_data (menubar->menu, menubar->selected))->help_node;
  426. else
  427. event_data.node = "[Menu Bar]";
  428. mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
  429. menubar_draw (menubar);
  430. }
  431. break;
  432. case CK_Left:
  433. menubar_left (menubar);
  434. break;
  435. case CK_Right:
  436. menubar_right (menubar);
  437. break;
  438. case CK_Up:
  439. if (menubar->is_dropped)
  440. menubar_up (menubar);
  441. break;
  442. case CK_Down:
  443. if (menubar->is_dropped)
  444. menubar_down (menubar);
  445. else
  446. menubar_drop (menubar, menubar->selected);
  447. break;
  448. case CK_Home:
  449. menubar_first (menubar);
  450. break;
  451. case CK_End:
  452. menubar_last (menubar);
  453. break;
  454. case CK_Enter:
  455. if (menubar->is_dropped)
  456. menubar_execute (menubar);
  457. else
  458. menubar_drop (menubar, menubar->selected);
  459. break;
  460. case CK_Quit:
  461. menubar_finish (menubar);
  462. break;
  463. default:
  464. ret = MSG_NOT_HANDLED;
  465. break;
  466. }
  467. return ret;
  468. }
  469. /* --------------------------------------------------------------------------------------------- */
  470. static int
  471. menubar_handle_key (WMenuBar * menubar, int key)
  472. {
  473. long cmd;
  474. cb_ret_t ret = MSG_NOT_HANDLED;
  475. cmd = widget_lookup_key (WIDGET (menubar), key);
  476. if (cmd != CK_IgnoreKey)
  477. ret = menubar_execute_cmd (menubar, cmd);
  478. if (ret != MSG_HANDLED)
  479. {
  480. if (menubar->is_dropped)
  481. ret = menubar_try_exec_menu (menubar, key);
  482. else
  483. ret = menubar_try_drop_menu (menubar, key);
  484. }
  485. return ret;
  486. }
  487. /* --------------------------------------------------------------------------------------------- */
  488. static gboolean
  489. menubar_refresh (WMenuBar * menubar)
  490. {
  491. Widget *w = WIDGET (menubar);
  492. if (!widget_get_state (w, WST_FOCUSED))
  493. return FALSE;
  494. /* Trick to get all the mouse events */
  495. w->rect.lines = LINES;
  496. /* Trick to get all of the hotkeys */
  497. widget_want_hotkey (w, TRUE);
  498. return TRUE;
  499. }
  500. /* --------------------------------------------------------------------------------------------- */
  501. static inline void
  502. menubar_free_menu (WMenuBar * menubar)
  503. {
  504. g_clear_list (&menubar->menu, (GDestroyNotify) destroy_menu);
  505. }
  506. /* --------------------------------------------------------------------------------------------- */
  507. static cb_ret_t
  508. menubar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  509. {
  510. WMenuBar *menubar = MENUBAR (w);
  511. switch (msg)
  512. {
  513. /* We do not want the focus unless we have been activated */
  514. case MSG_FOCUS:
  515. if (menubar_refresh (menubar))
  516. {
  517. menubar_draw (menubar);
  518. return MSG_HANDLED;
  519. }
  520. return MSG_NOT_HANDLED;
  521. case MSG_UNFOCUS:
  522. return widget_get_state (w, WST_FOCUSED) ? MSG_NOT_HANDLED : MSG_HANDLED;
  523. /* We don't want the buttonbar to activate while using the menubar */
  524. case MSG_HOTKEY:
  525. case MSG_KEY:
  526. if (widget_get_state (w, WST_FOCUSED))
  527. {
  528. menubar_handle_key (menubar, parm);
  529. return MSG_HANDLED;
  530. }
  531. return MSG_NOT_HANDLED;
  532. case MSG_CURSOR:
  533. /* Put the cursor in a suitable place */
  534. return MSG_NOT_HANDLED;
  535. case MSG_DRAW:
  536. if (widget_get_state (w, WST_VISIBLE) || menubar_refresh (menubar))
  537. menubar_draw (menubar);
  538. return MSG_HANDLED;
  539. case MSG_RESIZE:
  540. /* try show menu after screen resize */
  541. widget_default_callback (w, NULL, MSG_RESIZE, 0, data);
  542. menubar_refresh (menubar);
  543. return MSG_HANDLED;
  544. case MSG_DESTROY:
  545. menubar_free_menu (menubar);
  546. return MSG_HANDLED;
  547. default:
  548. return widget_default_callback (w, sender, msg, parm, data);
  549. }
  550. }
  551. /* --------------------------------------------------------------------------------------------- */
  552. static unsigned int
  553. menubar_get_menu_by_x_coord (const WMenuBar * menubar, int x)
  554. {
  555. unsigned int i;
  556. GList *menu;
  557. for (i = 0, menu = menubar->menu;
  558. menu != NULL && x >= MENU (menu->data)->start_x; i++, menu = g_list_next (menu))
  559. ;
  560. /* Don't set the invalid value -1 */
  561. if (i != 0)
  562. i--;
  563. return i;
  564. }
  565. /* --------------------------------------------------------------------------------------------- */
  566. static gboolean
  567. menubar_mouse_on_menu (const WMenuBar * menubar, int y, int x)
  568. {
  569. const WRect *w = &CONST_WIDGET (menubar)->rect;
  570. menu_t *menu;
  571. int left_x, right_x, bottom_y;
  572. if (!menubar->is_dropped)
  573. return FALSE;
  574. menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  575. left_x = menu->start_x;
  576. right_x = left_x + menu->max_entry_len + 3;
  577. if (right_x > w->cols)
  578. {
  579. left_x = w->cols - (menu->max_entry_len + 3);
  580. right_x = w->cols;
  581. }
  582. bottom_y = g_list_length (menu->entries) + 2; /* skip bar and top frame */
  583. return (x >= left_x && x < right_x && y > 1 && y < bottom_y);
  584. }
  585. /* --------------------------------------------------------------------------------------------- */
  586. static void
  587. menubar_change_selected_item (WMenuBar * menubar, int y)
  588. {
  589. menu_t *menu;
  590. menu_entry_t *entry;
  591. y -= 2; /* skip bar and top frame */
  592. menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
  593. entry = MENUENTRY (g_list_nth_data (menu->entries, y));
  594. if (entry != NULL && entry->command != CK_IgnoreKey)
  595. {
  596. menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
  597. menu->selected = y;
  598. menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
  599. }
  600. }
  601. /* --------------------------------------------------------------------------------------------- */
  602. static void
  603. menubar_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  604. {
  605. static gboolean was_drag = FALSE;
  606. WMenuBar *menubar = MENUBAR (w);
  607. gboolean mouse_on_drop;
  608. mouse_on_drop = menubar_mouse_on_menu (menubar, event->y, event->x);
  609. switch (msg)
  610. {
  611. case MSG_MOUSE_DOWN:
  612. was_drag = FALSE;
  613. if (event->y == 0)
  614. {
  615. /* events on menubar */
  616. unsigned int selected;
  617. selected = menubar_get_menu_by_x_coord (menubar, event->x);
  618. menubar_activate (menubar, TRUE, selected);
  619. menubar_remove (menubar); /* if already shown */
  620. menubar_drop (menubar, selected);
  621. }
  622. else if (mouse_on_drop)
  623. menubar_change_selected_item (menubar, event->y);
  624. else
  625. {
  626. /* mouse click outside menubar or dropdown -- close menu */
  627. menubar_finish (menubar);
  628. /*
  629. * @FIXME.
  630. *
  631. * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
  632. * events belonging to this click (in case the user drags the mouse,
  633. * of course).
  634. *
  635. * For the time being, we mark this with FIXME as this flag should
  636. * preferably be regarded as "implementation detail" and not be
  637. * touched by us. We should think of some other way of communicating
  638. * this to the system.
  639. */
  640. w->mouse.capture = FALSE;
  641. }
  642. break;
  643. case MSG_MOUSE_UP:
  644. if (was_drag && mouse_on_drop)
  645. menubar_execute (menubar);
  646. was_drag = FALSE;
  647. break;
  648. case MSG_MOUSE_CLICK:
  649. was_drag = FALSE;
  650. if ((event->buttons & GPM_B_MIDDLE) != 0 && event->y > 0 && menubar->is_dropped)
  651. {
  652. /* middle click -- everywhere */
  653. menubar_execute (menubar);
  654. }
  655. else if (mouse_on_drop)
  656. menubar_execute (menubar);
  657. else if (event->y > 0)
  658. /* releasing the mouse button outside the menu -- close menu */
  659. menubar_finish (menubar);
  660. break;
  661. case MSG_MOUSE_DRAG:
  662. if (event->y == 0)
  663. {
  664. menubar_remove (menubar);
  665. menubar_drop (menubar, menubar_get_menu_by_x_coord (menubar, event->x));
  666. }
  667. else if (mouse_on_drop)
  668. menubar_change_selected_item (menubar, event->y);
  669. was_drag = TRUE;
  670. break;
  671. case MSG_MOUSE_SCROLL_UP:
  672. case MSG_MOUSE_SCROLL_DOWN:
  673. was_drag = FALSE;
  674. if (widget_get_state (w, WST_FOCUSED))
  675. {
  676. if (event->y == 0)
  677. {
  678. /* menubar: left/right */
  679. if (msg == MSG_MOUSE_SCROLL_UP)
  680. menubar_left (menubar);
  681. else
  682. menubar_right (menubar);
  683. }
  684. else if (mouse_on_drop)
  685. {
  686. /* drop-down menu: up/down */
  687. if (msg == MSG_MOUSE_SCROLL_UP)
  688. menubar_up (menubar);
  689. else
  690. menubar_down (menubar);
  691. }
  692. }
  693. break;
  694. default:
  695. was_drag = FALSE;
  696. break;
  697. }
  698. }
  699. /* --------------------------------------------------------------------------------------------- */
  700. /*** public functions ****************************************************************************/
  701. /* --------------------------------------------------------------------------------------------- */
  702. menu_entry_t *
  703. menu_entry_create (const char *name, long command)
  704. {
  705. menu_entry_t *entry;
  706. entry = g_new (menu_entry_t, 1);
  707. entry->first_letter = ' ';
  708. entry->text = hotkey_new (name);
  709. entry->command = command;
  710. entry->shortcut = NULL;
  711. return entry;
  712. }
  713. /* --------------------------------------------------------------------------------------------- */
  714. void
  715. menu_entry_free (menu_entry_t * entry)
  716. {
  717. if (entry != NULL)
  718. {
  719. hotkey_free (entry->text);
  720. g_free (entry->shortcut);
  721. g_free (entry);
  722. }
  723. }
  724. /* --------------------------------------------------------------------------------------------- */
  725. menu_t *
  726. create_menu (const char *name, GList * entries, const char *help_node)
  727. {
  728. menu_t *menu;
  729. menu = g_new (menu_t, 1);
  730. menu->start_x = 0;
  731. menu->text = hotkey_new (name);
  732. menu->entries = entries;
  733. menu->max_entry_len = 1;
  734. menu->max_hotkey_len = 0;
  735. menu->selected = 0;
  736. menu->help_node = g_strdup (help_node);
  737. return menu;
  738. }
  739. /* --------------------------------------------------------------------------------------------- */
  740. void
  741. menu_set_name (menu_t * menu, const char *name)
  742. {
  743. hotkey_free (menu->text);
  744. menu->text = hotkey_new (name);
  745. }
  746. /* --------------------------------------------------------------------------------------------- */
  747. void
  748. destroy_menu (menu_t * menu)
  749. {
  750. hotkey_free (menu->text);
  751. g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free);
  752. g_free (menu->help_node);
  753. g_free (menu);
  754. }
  755. /* --------------------------------------------------------------------------------------------- */
  756. WMenuBar *
  757. menubar_new (GList * menu)
  758. {
  759. WRect r = { 0, 0, 1, COLS };
  760. WMenuBar *menubar;
  761. Widget *w;
  762. menubar = g_new0 (WMenuBar, 1);
  763. w = WIDGET (menubar);
  764. widget_init (w, &r, menubar_callback, menubar_mouse_callback);
  765. w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_TOP;
  766. w->options |= WOP_TOP_SELECT;
  767. w->keymap = menu_map;
  768. menubar_set_menu (menubar, menu);
  769. return menubar;
  770. }
  771. /* --------------------------------------------------------------------------------------------- */
  772. void
  773. menubar_set_menu (WMenuBar * menubar, GList * menu)
  774. {
  775. /* delete previous menu */
  776. menubar_free_menu (menubar);
  777. /* add new menu */
  778. menubar->is_dropped = FALSE;
  779. menubar->menu = menu;
  780. menubar->selected = 0;
  781. menubar_arrange (menubar);
  782. widget_set_state (WIDGET (menubar), WST_FOCUSED, FALSE);
  783. }
  784. /* --------------------------------------------------------------------------------------------- */
  785. void
  786. menubar_add_menu (WMenuBar * menubar, menu_t * menu)
  787. {
  788. if (menu != NULL)
  789. {
  790. menu_arrange (menu, DIALOG (WIDGET (menubar)->owner)->get_shortcut);
  791. menubar->menu = g_list_append (menubar->menu, menu);
  792. }
  793. menubar_arrange (menubar);
  794. }
  795. /* --------------------------------------------------------------------------------------------- */
  796. /**
  797. * Properly space menubar items. Should be called when menubar is created
  798. * and also when widget width is changed (i.e. upon xterm resize).
  799. */
  800. void
  801. menubar_arrange (WMenuBar * menubar)
  802. {
  803. int start_x = 1;
  804. GList *i;
  805. int gap;
  806. if (menubar->menu == NULL)
  807. return;
  808. gap = WIDGET (menubar)->rect.cols - 2;
  809. /* First, calculate gap between items... */
  810. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  811. {
  812. menu_t *menu = MENU (i->data);
  813. /* preserve length here, to be used below */
  814. menu->start_x = hotkey_width (menu->text) + 2;
  815. gap -= menu->start_x;
  816. }
  817. if (g_list_next (menubar->menu) == NULL)
  818. gap = 1;
  819. else
  820. gap /= (g_list_length (menubar->menu) - 1);
  821. if (gap <= 0)
  822. {
  823. /* We are out of luck - window is too narrow... */
  824. gap = 1;
  825. }
  826. else if (gap >= 3)
  827. gap = 3;
  828. /* ...and now fix start positions of menubar items */
  829. for (i = menubar->menu; i != NULL; i = g_list_next (i))
  830. {
  831. menu_t *menu = MENU (i->data);
  832. int len = menu->start_x;
  833. menu->start_x = start_x;
  834. start_x += len + gap;
  835. }
  836. }
  837. /* --------------------------------------------------------------------------------------------- */
  838. /** Find MenuBar widget in the dialog */
  839. WMenuBar *
  840. find_menubar (const WDialog * h)
  841. {
  842. return MENUBAR (widget_find_by_type (CONST_WIDGET (h), menubar_callback));
  843. }
  844. /* --------------------------------------------------------------------------------------------- */
  845. /**
  846. * Activate menu bar.
  847. *
  848. * @param menubar menu bar object
  849. * @param dropped whether dropdown menus should be drooped or not
  850. * @which number of active dropdown menu
  851. */
  852. void
  853. menubar_activate (WMenuBar * menubar, gboolean dropped, int which)
  854. {
  855. Widget *w = WIDGET (menubar);
  856. widget_show (w);
  857. if (!widget_get_state (w, WST_FOCUSED))
  858. {
  859. widget_set_options (w, WOP_SELECTABLE, TRUE);
  860. menubar->is_dropped = dropped;
  861. if (which >= 0)
  862. menubar->selected = (guint) which;
  863. menubar->previous_widget = group_get_current_widget_id (w->owner);
  864. /* Bring it to the top so it receives all mouse events before any other widget.
  865. * See also comment in menubar_finish(). */
  866. widget_select (w);
  867. }
  868. }
  869. /* --------------------------------------------------------------------------------------------- */