menu.c 25 KB

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