menu.c 30 KB

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