Merge branch '4561_tar_segfault'
[midnight-commander.git] / lib / widget / menu.c
blob0f968fe15bf7c123c90d33268bad372f15d18378
1 /*
2 Pulldown menu code
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2012-2022
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file menu.c
27 * \brief Source: pulldown menu code
30 #include <config.h>
32 #include <ctype.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <sys/types.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/skin.h"
41 #include "lib/tty/key.h" /* key macros */
42 #include "lib/strutil.h"
43 #include "lib/widget.h"
44 #include "lib/event.h" /* mc_event_raise() */
46 /*** global variables ****************************************************************************/
48 const global_keymap_t *menu_map = NULL;
50 /*** file scope macro definitions ****************************************************************/
52 #define MENUENTRY(x) ((menu_entry_t *)(x))
53 #define MENU(x) ((menu_t *)(x))
55 /*** file scope type declarations ****************************************************************/
57 struct menu_entry_t
59 unsigned char first_letter;
60 hotkey_t text;
61 long command;
62 char *shortcut;
65 struct menu_t
67 int start_x; /* position relative to menubar start */
68 hotkey_t text;
69 GList *entries;
70 size_t max_entry_len; /* cached max length of entry texts (text + shortcut) */
71 size_t max_hotkey_len; /* cached max length of shortcuts */
72 unsigned int current; /* pointer to current menu entry */
73 char *help_node;
76 /*** forward declarations (file scope functions) *************************************************/
78 /*** file scope variables ************************************************************************/
80 /* --------------------------------------------------------------------------------------------- */
81 /*** file scope functions ************************************************************************/
82 /* --------------------------------------------------------------------------------------------- */
84 static void
85 menu_arrange (menu_t *menu, dlg_shortcut_str get_shortcut)
87 if (menu != NULL)
89 GList *i;
90 size_t max_shortcut_len = 0;
92 menu->max_entry_len = 1;
93 menu->max_hotkey_len = 1;
95 for (i = menu->entries; i != NULL; i = g_list_next (i))
97 menu_entry_t *entry = MENUENTRY (i->data);
99 if (entry != NULL)
101 size_t len;
103 len = (size_t) hotkey_width (entry->text);
104 menu->max_hotkey_len = MAX (menu->max_hotkey_len, len);
106 if (get_shortcut != NULL)
107 entry->shortcut = get_shortcut (entry->command);
109 if (entry->shortcut != NULL)
111 len = (size_t) str_term_width1 (entry->shortcut);
112 max_shortcut_len = MAX (max_shortcut_len, len);
117 menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
121 /* --------------------------------------------------------------------------------------------- */
123 static void
124 menubar_paint_idx (const WMenuBar *menubar, unsigned int idx, int color)
126 const WRect *w = &CONST_WIDGET (menubar)->rect;
127 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
128 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, idx));
129 const int y = 2 + idx;
130 int x = menu->start_x;
132 if (x + menu->max_entry_len + 4 > (gsize) w->cols)
133 x = w->cols - menu->max_entry_len - 4;
135 if (entry == NULL)
137 /* menu separator */
138 tty_setcolor (MENU_ENTRY_COLOR);
140 widget_gotoyx (menubar, y, x - 1);
141 tty_print_alt_char (ACS_LTEE, FALSE);
142 tty_draw_hline (w->y + y, w->x + x, ACS_HLINE, menu->max_entry_len + 3);
143 widget_gotoyx (menubar, y, x + menu->max_entry_len + 3);
144 tty_print_alt_char (ACS_RTEE, FALSE);
146 else
148 int yt, xt;
150 /* menu text */
151 tty_setcolor (color);
152 widget_gotoyx (menubar, y, x);
153 tty_print_char ((unsigned char) entry->first_letter);
154 tty_getyx (&yt, &xt);
155 tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2); /* clear line */
156 tty_print_string (entry->text.start);
158 if (entry->text.hotkey != NULL)
160 tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
161 tty_print_string (entry->text.hotkey);
162 tty_setcolor (color);
165 if (entry->text.end != NULL)
166 tty_print_string (entry->text.end);
168 if (entry->shortcut != NULL)
170 widget_gotoyx (menubar, y, x + menu->max_hotkey_len + 3);
171 tty_print_string (entry->shortcut);
174 /* move cursor to the start of entry text */
175 widget_gotoyx (menubar, y, x + 1);
179 /* --------------------------------------------------------------------------------------------- */
181 static void
182 menubar_draw_drop (const WMenuBar *menubar)
184 const WRect *w = &CONST_WIDGET (menubar)->rect;
185 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
186 const unsigned int count = g_list_length (menu->entries);
187 int column = menu->start_x - 1;
188 unsigned int i;
190 if (column + menu->max_entry_len + 5 > (gsize) w->cols)
191 column = w->cols - menu->max_entry_len - 5;
193 if (mc_global.tty.shadows)
194 tty_draw_box_shadow (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5,
195 SHADOW_COLOR);
197 tty_setcolor (MENU_ENTRY_COLOR);
198 tty_draw_box (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
200 for (i = 0; i < count; i++)
201 menubar_paint_idx (menubar, i, i == menu->current ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
204 /* --------------------------------------------------------------------------------------------- */
206 static void
207 menubar_set_color (const WMenuBar *menubar, gboolean current, gboolean hotkey)
209 if (!widget_get_state (CONST_WIDGET (menubar), WST_FOCUSED))
210 tty_setcolor (MENU_INACTIVE_COLOR);
211 else if (current)
212 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
213 else
214 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
217 /* --------------------------------------------------------------------------------------------- */
219 static void
220 menubar_draw (const WMenuBar *menubar)
222 const WRect *w = &CONST_WIDGET (menubar)->rect;
223 GList *i;
225 /* First draw the complete menubar */
226 tty_setcolor (widget_get_state (WIDGET (menubar), WST_FOCUSED) ? MENU_ENTRY_COLOR :
227 MENU_INACTIVE_COLOR);
228 tty_draw_hline (w->y, w->x, ' ', w->cols);
230 /* Now each one of the entries */
231 for (i = menubar->menu; i != NULL; i = g_list_next (i))
233 menu_t *menu = MENU (i->data);
234 gboolean is_selected = (menubar->current == (gsize) g_list_position (menubar->menu, i));
236 menubar_set_color (menubar, is_selected, FALSE);
237 widget_gotoyx (menubar, 0, menu->start_x);
239 tty_print_char (' ');
240 tty_print_string (menu->text.start);
242 if (menu->text.hotkey != NULL)
244 menubar_set_color (menubar, is_selected, TRUE);
245 tty_print_string (menu->text.hotkey);
246 menubar_set_color (menubar, is_selected, FALSE);
249 if (menu->text.end != NULL)
250 tty_print_string (menu->text.end);
252 tty_print_char (' ');
255 if (menubar->is_dropped)
256 menubar_draw_drop (menubar);
257 else
258 widget_gotoyx (menubar, 0,
259 MENU (g_list_nth_data (menubar->menu, menubar->current))->start_x);
262 /* --------------------------------------------------------------------------------------------- */
264 static void
265 menubar_remove (WMenuBar *menubar)
267 Widget *g;
269 if (!menubar->is_dropped)
270 return;
272 /* HACK: before refresh the dialog, change the current widget to keep the order
273 of overlapped widgets. This is useful in multi-window editor.
274 In general, menubar should be a special object, not an ordinary widget
275 in the current dialog. */
276 g = WIDGET (WIDGET (menubar)->owner);
277 GROUP (g)->current = widget_find (g, widget_find_by_id (g, menubar->previous_widget));
279 menubar->is_dropped = FALSE;
280 do_refresh ();
281 menubar->is_dropped = TRUE;
283 /* restore current widget */
284 GROUP (g)->current = widget_find (g, WIDGET (menubar));
287 /* --------------------------------------------------------------------------------------------- */
289 static void
290 menubar_left (WMenuBar *menubar)
292 menubar_remove (menubar);
293 if (menubar->current == 0)
294 menubar->current = g_list_length (menubar->menu) - 1;
295 else
296 menubar->current--;
297 menubar_draw (menubar);
300 /* --------------------------------------------------------------------------------------------- */
302 static void
303 menubar_right (WMenuBar *menubar)
305 menubar_remove (menubar);
306 menubar->current = (menubar->current + 1) % g_list_length (menubar->menu);
307 menubar_draw (menubar);
310 /* --------------------------------------------------------------------------------------------- */
312 static void
313 menubar_finish (WMenuBar *menubar)
315 Widget *w = WIDGET (menubar);
317 menubar->is_dropped = FALSE;
318 w->rect.lines = 1;
319 widget_want_hotkey (w, FALSE);
320 widget_set_options (w, WOP_SELECTABLE, FALSE);
322 if (!mc_global.keybar_visible)
323 widget_hide (w);
324 else
326 /* Move the menubar to the bottom so that widgets displayed on top of
327 * an "invisible" menubar get the first chance to respond to mouse events. */
328 widget_set_bottom (w);
331 /* background must be bottom */
332 if (DIALOG (w->owner)->bg != NULL)
333 widget_set_bottom (WIDGET (DIALOG (w->owner)->bg));
335 group_select_widget_by_id (w->owner, menubar->previous_widget);
336 do_refresh ();
339 /* --------------------------------------------------------------------------------------------- */
341 static void
342 menubar_drop (WMenuBar *menubar, unsigned int selected)
344 menubar->is_dropped = TRUE;
345 menubar->current = selected;
346 menubar_draw (menubar);
349 /* --------------------------------------------------------------------------------------------- */
351 static void
352 menubar_execute (WMenuBar *menubar)
354 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
355 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
357 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
359 Widget *w = WIDGET (menubar);
361 mc_global.widget.is_right = (menubar->current != 0);
362 menubar_finish (menubar);
363 send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
364 do_refresh ();
368 /* --------------------------------------------------------------------------------------------- */
370 static void
371 menubar_down (WMenuBar *menubar)
373 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
374 const unsigned int len = g_list_length (menu->entries);
375 menu_entry_t *entry;
377 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
381 menu->current = (menu->current + 1) % len;
382 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
384 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
386 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
389 /* --------------------------------------------------------------------------------------------- */
391 static void
392 menubar_up (WMenuBar *menubar)
394 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
395 const unsigned int len = g_list_length (menu->entries);
396 menu_entry_t *entry;
398 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
402 if (menu->current == 0)
403 menu->current = len - 1;
404 else
405 menu->current--;
406 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
408 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
410 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
413 /* --------------------------------------------------------------------------------------------- */
415 static void
416 menubar_first (WMenuBar *menubar)
418 if (menubar->is_dropped)
420 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
422 if (menu->current == 0)
423 return;
425 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
427 menu->current = 0;
429 while (TRUE)
431 menu_entry_t *entry;
433 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
435 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
436 menu->current++;
437 else
438 break;
441 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
443 else
445 menubar->current = 0;
446 menubar_draw (menubar);
450 /* --------------------------------------------------------------------------------------------- */
452 static void
453 menubar_last (WMenuBar *menubar)
455 if (menubar->is_dropped)
457 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
458 const unsigned int len = g_list_length (menu->entries);
459 menu_entry_t *entry;
461 if (menu->current == len - 1)
462 return;
464 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
466 menu->current = len;
470 menu->current--;
471 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->current));
473 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
475 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
477 else
479 menubar->current = g_list_length (menubar->menu) - 1;
480 menubar_draw (menubar);
484 /* --------------------------------------------------------------------------------------------- */
486 static cb_ret_t
487 menubar_try_drop_menu (WMenuBar *menubar, int hotkey)
489 GList *i;
491 for (i = menubar->menu; i != NULL; i = g_list_next (i))
493 menu_t *menu = MENU (i->data);
495 if (menu->text.hotkey != NULL && hotkey == g_ascii_tolower (menu->text.hotkey[0]))
497 menubar_drop (menubar, g_list_position (menubar->menu, i));
498 return MSG_HANDLED;
502 return MSG_NOT_HANDLED;
505 /* --------------------------------------------------------------------------------------------- */
507 static cb_ret_t
508 menubar_try_exec_menu (WMenuBar *menubar, int hotkey)
510 menu_t *menu;
511 GList *i;
513 menu = g_list_nth_data (menubar->menu, menubar->current);
515 for (i = menu->entries; i != NULL; i = g_list_next (i))
517 const menu_entry_t *entry = MENUENTRY (i->data);
519 if (entry != NULL && entry->text.hotkey != NULL
520 && hotkey == g_ascii_tolower (entry->text.hotkey[0]))
522 menu->current = g_list_position (menu->entries, i);
523 menubar_execute (menubar);
524 return MSG_HANDLED;
528 return MSG_NOT_HANDLED;
531 /* --------------------------------------------------------------------------------------------- */
533 static void
534 menubar_help (const WMenuBar *menubar)
536 ev_help_t event_data;
538 event_data.filename = NULL;
540 if (menubar->is_dropped)
541 event_data.node = MENU (g_list_nth_data (menubar->menu, menubar->current))->help_node;
542 else
543 event_data.node = "[Menu Bar]";
545 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
546 menubar_draw (menubar);
549 /* --------------------------------------------------------------------------------------------- */
551 static cb_ret_t
552 menubar_execute_cmd (WMenuBar *menubar, long command)
554 cb_ret_t ret = MSG_HANDLED;
556 switch (command)
558 case CK_Help:
559 menubar_help (menubar);
560 break;
562 case CK_Left:
563 menubar_left (menubar);
564 break;
565 case CK_Right:
566 menubar_right (menubar);
567 break;
568 case CK_Up:
569 if (menubar->is_dropped)
570 menubar_up (menubar);
571 break;
572 case CK_Down:
573 if (menubar->is_dropped)
574 menubar_down (menubar);
575 else
576 menubar_drop (menubar, menubar->current);
577 break;
578 case CK_Home:
579 menubar_first (menubar);
580 break;
581 case CK_End:
582 menubar_last (menubar);
583 break;
585 case CK_Enter:
586 if (menubar->is_dropped)
587 menubar_execute (menubar);
588 else
589 menubar_drop (menubar, menubar->current);
590 break;
591 case CK_Quit:
592 menubar_finish (menubar);
593 break;
595 default:
596 ret = MSG_NOT_HANDLED;
597 break;
600 return ret;
603 /* --------------------------------------------------------------------------------------------- */
605 static int
606 menubar_handle_key (WMenuBar *menubar, int key)
608 long cmd;
609 cb_ret_t ret = MSG_NOT_HANDLED;
611 cmd = widget_lookup_key (WIDGET (menubar), key);
613 if (cmd != CK_IgnoreKey)
614 ret = menubar_execute_cmd (menubar, cmd);
616 if (ret != MSG_HANDLED)
618 if (menubar->is_dropped)
619 ret = menubar_try_exec_menu (menubar, key);
620 else
621 ret = menubar_try_drop_menu (menubar, key);
624 return ret;
627 /* --------------------------------------------------------------------------------------------- */
629 static gboolean
630 menubar_refresh (WMenuBar *menubar)
632 Widget *w = WIDGET (menubar);
634 if (!widget_get_state (w, WST_FOCUSED))
635 return FALSE;
637 /* Trick to get all the mouse events */
638 w->rect.lines = LINES;
640 /* Trick to get all of the hotkeys */
641 widget_want_hotkey (w, TRUE);
642 return TRUE;
645 /* --------------------------------------------------------------------------------------------- */
647 static inline void
648 menubar_free_menu (WMenuBar *menubar)
650 g_clear_list (&menubar->menu, (GDestroyNotify) menu_free);
653 /* --------------------------------------------------------------------------------------------- */
655 static cb_ret_t
656 menubar_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
658 WMenuBar *menubar = MENUBAR (w);
660 switch (msg)
662 /* We do not want the focus unless we have been activated */
663 case MSG_FOCUS:
664 if (menubar_refresh (menubar))
666 menubar_draw (menubar);
667 return MSG_HANDLED;
669 return MSG_NOT_HANDLED;
671 case MSG_UNFOCUS:
672 return widget_get_state (w, WST_FOCUSED) ? MSG_NOT_HANDLED : MSG_HANDLED;
674 /* We don't want the buttonbar to activate while using the menubar */
675 case MSG_HOTKEY:
676 case MSG_KEY:
677 if (widget_get_state (w, WST_FOCUSED))
679 menubar_handle_key (menubar, parm);
680 return MSG_HANDLED;
682 return MSG_NOT_HANDLED;
684 case MSG_CURSOR:
685 /* Put the cursor in a suitable place */
686 return MSG_NOT_HANDLED;
688 case MSG_DRAW:
689 if (widget_get_state (w, WST_VISIBLE) || menubar_refresh (menubar))
690 menubar_draw (menubar);
691 return MSG_HANDLED;
693 case MSG_RESIZE:
694 /* try show menu after screen resize */
695 widget_default_callback (w, NULL, MSG_RESIZE, 0, data);
696 menubar_refresh (menubar);
697 return MSG_HANDLED;
699 case MSG_DESTROY:
700 menubar_free_menu (menubar);
701 return MSG_HANDLED;
703 default:
704 return widget_default_callback (w, sender, msg, parm, data);
708 /* --------------------------------------------------------------------------------------------- */
710 static unsigned int
711 menubar_get_menu_by_x_coord (const WMenuBar *menubar, int x)
713 unsigned int i;
714 GList *menu;
716 for (i = 0, menu = menubar->menu;
717 menu != NULL && x >= MENU (menu->data)->start_x; i++, menu = g_list_next (menu))
720 /* Don't set the invalid value -1 */
721 if (i != 0)
722 i--;
724 return i;
727 /* --------------------------------------------------------------------------------------------- */
729 static gboolean
730 menubar_mouse_on_menu (const WMenuBar *menubar, int y, int x)
732 const WRect *w = &CONST_WIDGET (menubar)->rect;
733 menu_t *menu;
734 int left_x, right_x, bottom_y;
736 if (!menubar->is_dropped)
737 return FALSE;
739 menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
740 left_x = menu->start_x;
741 right_x = left_x + menu->max_entry_len + 3;
742 if (right_x > w->cols)
744 left_x = w->cols - (menu->max_entry_len + 3);
745 right_x = w->cols;
748 bottom_y = g_list_length (menu->entries) + 2; /* skip bar and top frame */
750 return (x >= left_x && x < right_x && y > 1 && y < bottom_y);
753 /* --------------------------------------------------------------------------------------------- */
755 static void
756 menubar_change_selected_item (WMenuBar *menubar, int y)
758 menu_t *menu;
759 menu_entry_t *entry;
761 y -= 2; /* skip bar and top frame */
762 menu = MENU (g_list_nth_data (menubar->menu, menubar->current));
763 entry = MENUENTRY (g_list_nth_data (menu->entries, y));
765 if (entry != NULL && entry->command != CK_IgnoreKey)
767 menubar_paint_idx (menubar, menu->current, MENU_ENTRY_COLOR);
768 menu->current = y;
769 menubar_paint_idx (menubar, menu->current, MENU_SELECTED_COLOR);
773 /* --------------------------------------------------------------------------------------------- */
775 static void
776 menubar_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
778 static gboolean was_drag = FALSE;
780 WMenuBar *menubar = MENUBAR (w);
781 gboolean mouse_on_drop;
783 mouse_on_drop = menubar_mouse_on_menu (menubar, event->y, event->x);
785 switch (msg)
787 case MSG_MOUSE_DOWN:
788 was_drag = FALSE;
790 if (event->y == 0)
792 /* events on menubar */
793 unsigned int selected;
795 selected = menubar_get_menu_by_x_coord (menubar, event->x);
796 menubar_activate (menubar, TRUE, selected);
797 menubar_remove (menubar); /* if already shown */
798 menubar_drop (menubar, selected);
800 else if (mouse_on_drop)
801 menubar_change_selected_item (menubar, event->y);
802 else
804 /* mouse click outside menubar or dropdown -- close menu */
805 menubar_finish (menubar);
808 * @FIXME.
810 * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
811 * events belonging to this click (in case the user drags the mouse,
812 * of course).
814 * For the time being, we mark this with FIXME as this flag should
815 * preferably be regarded as "implementation detail" and not be
816 * touched by us. We should think of some other way of communicating
817 * this to the system.
819 w->mouse.capture = FALSE;
821 break;
823 case MSG_MOUSE_UP:
824 if (was_drag && mouse_on_drop)
825 menubar_execute (menubar);
826 was_drag = FALSE;
827 break;
829 case MSG_MOUSE_CLICK:
830 was_drag = FALSE;
832 if ((event->buttons & GPM_B_MIDDLE) != 0 && event->y > 0 && menubar->is_dropped)
834 /* middle click -- everywhere */
835 menubar_execute (menubar);
837 else if (mouse_on_drop)
838 menubar_execute (menubar);
839 else if (event->y > 0)
840 /* releasing the mouse button outside the menu -- close menu */
841 menubar_finish (menubar);
842 break;
844 case MSG_MOUSE_DRAG:
845 if (event->y == 0)
847 menubar_remove (menubar);
848 menubar_drop (menubar, menubar_get_menu_by_x_coord (menubar, event->x));
850 else if (mouse_on_drop)
851 menubar_change_selected_item (menubar, event->y);
853 was_drag = TRUE;
854 break;
856 case MSG_MOUSE_SCROLL_UP:
857 case MSG_MOUSE_SCROLL_DOWN:
858 was_drag = FALSE;
860 if (widget_get_state (w, WST_FOCUSED))
862 if (event->y == 0)
864 /* menubar: left/right */
865 if (msg == MSG_MOUSE_SCROLL_UP)
866 menubar_left (menubar);
867 else
868 menubar_right (menubar);
870 else if (mouse_on_drop)
872 /* drop-down menu: up/down */
873 if (msg == MSG_MOUSE_SCROLL_UP)
874 menubar_up (menubar);
875 else
876 menubar_down (menubar);
879 break;
881 default:
882 was_drag = FALSE;
883 break;
887 /* --------------------------------------------------------------------------------------------- */
888 /*** public functions ****************************************************************************/
889 /* --------------------------------------------------------------------------------------------- */
891 menu_entry_t *
892 menu_entry_new (const char *name, long command)
894 menu_entry_t *entry;
896 entry = g_new (menu_entry_t, 1);
897 entry->first_letter = ' ';
898 entry->text = hotkey_new (name);
899 entry->command = command;
900 entry->shortcut = NULL;
902 return entry;
905 /* --------------------------------------------------------------------------------------------- */
907 void
908 menu_entry_free (menu_entry_t *entry)
910 if (entry != NULL)
912 hotkey_free (entry->text);
913 g_free (entry->shortcut);
914 g_free (entry);
918 /* --------------------------------------------------------------------------------------------- */
920 menu_t *
921 menu_new (const char *name, GList *entries, const char *help_node)
923 menu_t *menu;
925 menu = g_new (menu_t, 1);
926 menu->start_x = 0;
927 menu->text = hotkey_new (name);
928 menu->entries = entries;
929 menu->max_entry_len = 1;
930 menu->max_hotkey_len = 0;
931 menu->current = 0;
932 menu->help_node = g_strdup (help_node);
934 return menu;
937 /* --------------------------------------------------------------------------------------------- */
939 void
940 menu_set_name (menu_t *menu, const char *name)
942 hotkey_free (menu->text);
943 menu->text = hotkey_new (name);
946 /* --------------------------------------------------------------------------------------------- */
948 void
949 menu_free (menu_t *menu)
951 hotkey_free (menu->text);
952 g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free);
953 g_free (menu->help_node);
954 g_free (menu);
957 /* --------------------------------------------------------------------------------------------- */
959 WMenuBar *
960 menubar_new (GList *menu)
962 WRect r = { 0, 0, 1, COLS };
963 WMenuBar *menubar;
964 Widget *w;
966 menubar = g_new0 (WMenuBar, 1);
967 w = WIDGET (menubar);
968 widget_init (w, &r, menubar_callback, menubar_mouse_callback);
969 w->pos_flags = WPOS_KEEP_HORZ | WPOS_KEEP_TOP;
970 w->options |= WOP_TOP_SELECT;
971 w->keymap = menu_map;
972 menubar_set_menu (menubar, menu);
974 return menubar;
977 /* --------------------------------------------------------------------------------------------- */
979 void
980 menubar_set_menu (WMenuBar *menubar, GList *menu)
982 /* delete previous menu */
983 menubar_free_menu (menubar);
984 /* add new menu */
985 menubar->is_dropped = FALSE;
986 menubar->menu = menu;
987 menubar->current = 0;
988 menubar_arrange (menubar);
989 widget_set_state (WIDGET (menubar), WST_FOCUSED, FALSE);
992 /* --------------------------------------------------------------------------------------------- */
994 void
995 menubar_add_menu (WMenuBar *menubar, menu_t *menu)
997 if (menu != NULL)
999 menu_arrange (menu, DIALOG (WIDGET (menubar)->owner)->get_shortcut);
1000 menubar->menu = g_list_append (menubar->menu, menu);
1003 menubar_arrange (menubar);
1006 /* --------------------------------------------------------------------------------------------- */
1008 * Properly space menubar items. Should be called when menubar is created
1009 * and also when widget width is changed (i.e. upon xterm resize).
1012 void
1013 menubar_arrange (WMenuBar *menubar)
1015 int start_x = 1;
1016 GList *i;
1017 int gap;
1019 if (menubar->menu == NULL)
1020 return;
1022 gap = WIDGET (menubar)->rect.cols - 2;
1024 /* First, calculate gap between items... */
1025 for (i = menubar->menu; i != NULL; i = g_list_next (i))
1027 menu_t *menu = MENU (i->data);
1029 /* preserve length here, to be used below */
1030 menu->start_x = hotkey_width (menu->text) + 2;
1031 gap -= menu->start_x;
1034 if (g_list_next (menubar->menu) == NULL)
1035 gap = 1;
1036 else
1037 gap /= (g_list_length (menubar->menu) - 1);
1039 if (gap <= 0)
1041 /* We are out of luck - window is too narrow... */
1042 gap = 1;
1044 else if (gap >= 3)
1045 gap = 3;
1047 /* ...and now fix start positions of menubar items */
1048 for (i = menubar->menu; i != NULL; i = g_list_next (i))
1050 menu_t *menu = MENU (i->data);
1051 int len = menu->start_x;
1053 menu->start_x = start_x;
1054 start_x += len + gap;
1058 /* --------------------------------------------------------------------------------------------- */
1059 /** Find MenuBar widget in the dialog */
1061 WMenuBar *
1062 menubar_find (const WDialog *h)
1064 return MENUBAR (widget_find_by_type (CONST_WIDGET (h), menubar_callback));
1067 /* --------------------------------------------------------------------------------------------- */
1070 * Activate menu bar.
1072 * @param menubar menu bar object
1073 * @param dropped whether dropdown menus should be drooped or not
1074 * @which number of active dropdown menu
1076 void
1077 menubar_activate (WMenuBar *menubar, gboolean dropped, int which)
1079 Widget *w = WIDGET (menubar);
1081 widget_show (w);
1083 if (!widget_get_state (w, WST_FOCUSED))
1085 widget_set_options (w, WOP_SELECTABLE, TRUE);
1087 menubar->is_dropped = dropped;
1088 if (which >= 0)
1089 menubar->current = (guint) which;
1091 menubar->previous_widget = group_get_current_widget_id (w->owner);
1093 /* Bring it to the top so it receives all mouse events before any other widget.
1094 * See also comment in menubar_finish(). */
1095 widget_select (w);
1099 /* --------------------------------------------------------------------------------------------- */