menu.c 30 KB

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