4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
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/>.
27 * \brief Source: pulldown menu code
35 #include <sys/types.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.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 ****************************************************************/
59 unsigned char first_letter
;
67 int start_x
; /* position relative to menubar start */
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 */
76 /*** forward declarations (file scope functions) *************************************************/
78 /*** file scope variables ************************************************************************/
80 /* --------------------------------------------------------------------------------------------- */
81 /*** file scope functions ************************************************************************/
82 /* --------------------------------------------------------------------------------------------- */
85 menu_arrange (menu_t
*menu
, dlg_shortcut_str get_shortcut
)
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
);
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 /* --------------------------------------------------------------------------------------------- */
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;
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
);
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 /* --------------------------------------------------------------------------------------------- */
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;
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,
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 /* --------------------------------------------------------------------------------------------- */
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
);
212 tty_setcolor (hotkey
? MENU_HOTSEL_COLOR
: MENU_SELECTED_COLOR
);
214 tty_setcolor (hotkey
? MENU_HOT_COLOR
: MENU_ENTRY_COLOR
);
217 /* --------------------------------------------------------------------------------------------- */
220 menubar_draw (const WMenuBar
*menubar
)
222 const WRect
*w
= &CONST_WIDGET (menubar
)->rect
;
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
);
258 widget_gotoyx (menubar
, 0,
259 MENU (g_list_nth_data (menubar
->menu
, menubar
->current
))->start_x
);
262 /* --------------------------------------------------------------------------------------------- */
265 menubar_remove (WMenuBar
*menubar
)
269 if (!menubar
->is_dropped
)
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
;
281 menubar
->is_dropped
= TRUE
;
283 /* restore current widget */
284 GROUP (g
)->current
= widget_find (g
, WIDGET (menubar
));
287 /* --------------------------------------------------------------------------------------------- */
290 menubar_left (WMenuBar
*menubar
)
292 menubar_remove (menubar
);
293 if (menubar
->current
== 0)
294 menubar
->current
= g_list_length (menubar
->menu
) - 1;
297 menubar_draw (menubar
);
300 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
313 menubar_finish (WMenuBar
*menubar
)
315 Widget
*w
= WIDGET (menubar
);
317 menubar
->is_dropped
= FALSE
;
319 widget_want_hotkey (w
, FALSE
);
320 widget_set_options (w
, WOP_SELECTABLE
, FALSE
);
322 if (!mc_global
.keybar_visible
)
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
);
339 /* --------------------------------------------------------------------------------------------- */
342 menubar_drop (WMenuBar
*menubar
, unsigned int selected
)
344 menubar
->is_dropped
= TRUE
;
345 menubar
->current
= selected
;
346 menubar_draw (menubar
);
349 /* --------------------------------------------------------------------------------------------- */
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
);
368 /* --------------------------------------------------------------------------------------------- */
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
);
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 /* --------------------------------------------------------------------------------------------- */
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
);
398 menubar_paint_idx (menubar
, menu
->current
, MENU_ENTRY_COLOR
);
402 if (menu
->current
== 0)
403 menu
->current
= len
- 1;
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 /* --------------------------------------------------------------------------------------------- */
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)
425 menubar_paint_idx (menubar
, menu
->current
, MENU_ENTRY_COLOR
);
433 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, menu
->current
));
435 if ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
))
441 menubar_paint_idx (menubar
, menu
->current
, MENU_SELECTED_COLOR
);
445 menubar
->current
= 0;
446 menubar_draw (menubar
);
450 /* --------------------------------------------------------------------------------------------- */
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
);
461 if (menu
->current
== len
- 1)
464 menubar_paint_idx (menubar
, menu
->current
, MENU_ENTRY_COLOR
);
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
);
479 menubar
->current
= g_list_length (menubar
->menu
) - 1;
480 menubar_draw (menubar
);
484 /* --------------------------------------------------------------------------------------------- */
487 menubar_try_drop_menu (WMenuBar
*menubar
, int hotkey
)
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
));
502 return MSG_NOT_HANDLED
;
505 /* --------------------------------------------------------------------------------------------- */
508 menubar_try_exec_menu (WMenuBar
*menubar
, int hotkey
)
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
);
528 return MSG_NOT_HANDLED
;
531 /* --------------------------------------------------------------------------------------------- */
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
;
543 event_data
.node
= "[Menu Bar]";
545 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
546 menubar_draw (menubar
);
549 /* --------------------------------------------------------------------------------------------- */
552 menubar_execute_cmd (WMenuBar
*menubar
, long command
)
554 cb_ret_t ret
= MSG_HANDLED
;
559 menubar_help (menubar
);
563 menubar_left (menubar
);
566 menubar_right (menubar
);
569 if (menubar
->is_dropped
)
570 menubar_up (menubar
);
573 if (menubar
->is_dropped
)
574 menubar_down (menubar
);
576 menubar_drop (menubar
, menubar
->current
);
579 menubar_first (menubar
);
582 menubar_last (menubar
);
586 if (menubar
->is_dropped
)
587 menubar_execute (menubar
);
589 menubar_drop (menubar
, menubar
->current
);
592 menubar_finish (menubar
);
596 ret
= MSG_NOT_HANDLED
;
603 /* --------------------------------------------------------------------------------------------- */
606 menubar_handle_key (WMenuBar
*menubar
, int key
)
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
);
621 ret
= menubar_try_drop_menu (menubar
, key
);
627 /* --------------------------------------------------------------------------------------------- */
630 menubar_refresh (WMenuBar
*menubar
)
632 Widget
*w
= WIDGET (menubar
);
634 if (!widget_get_state (w
, WST_FOCUSED
))
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
);
645 /* --------------------------------------------------------------------------------------------- */
648 menubar_free_menu (WMenuBar
*menubar
)
650 g_clear_list (&menubar
->menu
, (GDestroyNotify
) menu_free
);
653 /* --------------------------------------------------------------------------------------------- */
656 menubar_callback (Widget
*w
, Widget
*sender
, widget_msg_t msg
, int parm
, void *data
)
658 WMenuBar
*menubar
= MENUBAR (w
);
662 /* We do not want the focus unless we have been activated */
664 if (menubar_refresh (menubar
))
666 menubar_draw (menubar
);
669 return MSG_NOT_HANDLED
;
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 */
677 if (widget_get_state (w
, WST_FOCUSED
))
679 menubar_handle_key (menubar
, parm
);
682 return MSG_NOT_HANDLED
;
685 /* Put the cursor in a suitable place */
686 return MSG_NOT_HANDLED
;
689 if (widget_get_state (w
, WST_VISIBLE
) || menubar_refresh (menubar
))
690 menubar_draw (menubar
);
694 /* try show menu after screen resize */
695 widget_default_callback (w
, NULL
, MSG_RESIZE
, 0, data
);
696 menubar_refresh (menubar
);
700 menubar_free_menu (menubar
);
704 return widget_default_callback (w
, sender
, msg
, parm
, data
);
708 /* --------------------------------------------------------------------------------------------- */
711 menubar_get_menu_by_x_coord (const WMenuBar
*menubar
, int x
)
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 */
727 /* --------------------------------------------------------------------------------------------- */
730 menubar_mouse_on_menu (const WMenuBar
*menubar
, int y
, int x
)
732 const WRect
*w
= &CONST_WIDGET (menubar
)->rect
;
734 int left_x
, right_x
, bottom_y
;
736 if (!menubar
->is_dropped
)
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);
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 /* --------------------------------------------------------------------------------------------- */
756 menubar_change_selected_item (WMenuBar
*menubar
, int y
)
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
);
769 menubar_paint_idx (menubar
, menu
->current
, MENU_SELECTED_COLOR
);
773 /* --------------------------------------------------------------------------------------------- */
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
);
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
);
804 /* mouse click outside menubar or dropdown -- close menu */
805 menubar_finish (menubar
);
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,
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
;
824 if (was_drag
&& mouse_on_drop
)
825 menubar_execute (menubar
);
829 case MSG_MOUSE_CLICK
:
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
);
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
);
856 case MSG_MOUSE_SCROLL_UP
:
857 case MSG_MOUSE_SCROLL_DOWN
:
860 if (widget_get_state (w
, WST_FOCUSED
))
864 /* menubar: left/right */
865 if (msg
== MSG_MOUSE_SCROLL_UP
)
866 menubar_left (menubar
);
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
);
876 menubar_down (menubar
);
887 /* --------------------------------------------------------------------------------------------- */
888 /*** public functions ****************************************************************************/
889 /* --------------------------------------------------------------------------------------------- */
892 menu_entry_new (const char *name
, long command
)
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
;
905 /* --------------------------------------------------------------------------------------------- */
908 menu_entry_free (menu_entry_t
*entry
)
912 hotkey_free (entry
->text
);
913 g_free (entry
->shortcut
);
918 /* --------------------------------------------------------------------------------------------- */
921 menu_new (const char *name
, GList
*entries
, const char *help_node
)
925 menu
= g_new (menu_t
, 1);
927 menu
->text
= hotkey_new (name
);
928 menu
->entries
= entries
;
929 menu
->max_entry_len
= 1;
930 menu
->max_hotkey_len
= 0;
932 menu
->help_node
= g_strdup (help_node
);
937 /* --------------------------------------------------------------------------------------------- */
940 menu_set_name (menu_t
*menu
, const char *name
)
942 hotkey_free (menu
->text
);
943 menu
->text
= hotkey_new (name
);
946 /* --------------------------------------------------------------------------------------------- */
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
);
957 /* --------------------------------------------------------------------------------------------- */
960 menubar_new (GList
*menu
)
962 WRect r
= { 0, 0, 1, COLS
};
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
);
977 /* --------------------------------------------------------------------------------------------- */
980 menubar_set_menu (WMenuBar
*menubar
, GList
*menu
)
982 /* delete previous menu */
983 menubar_free_menu (menubar
);
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 /* --------------------------------------------------------------------------------------------- */
995 menubar_add_menu (WMenuBar
*menubar
, menu_t
*menu
)
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).
1013 menubar_arrange (WMenuBar
*menubar
)
1019 if (menubar
->menu
== NULL
)
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
)
1037 gap
/= (g_list_length (menubar
->menu
) - 1);
1041 /* We are out of luck - window is too narrow... */
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 */
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
1077 menubar_activate (WMenuBar
*menubar
, gboolean dropped
, int which
)
1079 Widget
*w
= WIDGET (menubar
);
1083 if (!widget_get_state (w
, WST_FOCUSED
))
1085 widget_set_options (w
, WOP_SELECTABLE
, TRUE
);
1087 menubar
->is_dropped
= dropped
;
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(). */
1099 /* --------------------------------------------------------------------------------------------- */