4 Copyright (C) 1994-2019
5 Free Software Foundation, Inc.
8 Andrew Borodin <aborodin@vmail.ru>, 2012, 2013, 2016
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 /*** file scope macro definitions ****************************************************************/
50 #define MENUENTRY(x) ((menu_entry_t *)(x))
51 #define MENU(x) ((menu_t *)(x))
53 /*** file scope type declarations ****************************************************************/
57 unsigned char first_letter
;
65 int start_x
; /* position relative to menubar start */
68 size_t max_entry_len
; /* cached max length of entry texts (text + shortcut) */
69 size_t max_hotkey_len
; /* cached max length of shortcuts */
70 unsigned int selected
; /* pointer to current menu entry */
74 /*** file scope variables ************************************************************************/
76 /*** file scope functions ************************************************************************/
77 /* --------------------------------------------------------------------------------------------- */
80 menu_arrange (menu_t
* menu
, dlg_shortcut_str get_shortcut
)
85 size_t max_shortcut_len
= 0;
87 menu
->max_entry_len
= 1;
88 menu
->max_hotkey_len
= 1;
90 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
92 menu_entry_t
*entry
= MENUENTRY (i
->data
);
98 len
= (size_t) hotkey_width (entry
->text
);
99 menu
->max_hotkey_len
= MAX (menu
->max_hotkey_len
, len
);
101 if (get_shortcut
!= NULL
)
102 entry
->shortcut
= get_shortcut (entry
->command
);
104 if (entry
->shortcut
!= NULL
)
106 len
= (size_t) str_term_width1 (entry
->shortcut
);
107 max_shortcut_len
= MAX (max_shortcut_len
, len
);
112 menu
->max_entry_len
= menu
->max_hotkey_len
+ max_shortcut_len
;
116 /* --------------------------------------------------------------------------------------------- */
119 menubar_paint_idx (WMenuBar
* menubar
, unsigned int idx
, int color
)
121 Widget
*w
= WIDGET (menubar
);
122 const menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
123 const menu_entry_t
*entry
= MENUENTRY (g_list_nth_data (menu
->entries
, idx
));
124 const int y
= 2 + idx
;
125 int x
= menu
->start_x
;
127 if (x
+ menu
->max_entry_len
+ 4 > (gsize
) w
->cols
)
128 x
= w
->cols
- menu
->max_entry_len
- 4;
133 tty_setcolor (MENU_ENTRY_COLOR
);
135 widget_move (w
, y
, x
- 1);
136 tty_print_alt_char (ACS_LTEE
, FALSE
);
137 tty_draw_hline (w
->y
+ y
, w
->x
+ x
, ACS_HLINE
, menu
->max_entry_len
+ 3);
138 widget_move (w
, y
, x
+ menu
->max_entry_len
+ 3);
139 tty_print_alt_char (ACS_RTEE
, FALSE
);
146 tty_setcolor (color
);
147 widget_move (w
, y
, x
);
148 tty_print_char ((unsigned char) entry
->first_letter
);
149 tty_getyx (&yt
, &xt
);
150 tty_draw_hline (yt
, xt
, ' ', menu
->max_entry_len
+ 2); /* clear line */
151 tty_print_string (entry
->text
.start
);
153 if (entry
->text
.hotkey
!= NULL
)
155 tty_setcolor (color
== MENU_SELECTED_COLOR
? MENU_HOTSEL_COLOR
: MENU_HOT_COLOR
);
156 tty_print_string (entry
->text
.hotkey
);
157 tty_setcolor (color
);
160 if (entry
->text
.end
!= NULL
)
161 tty_print_string (entry
->text
.end
);
163 if (entry
->shortcut
!= NULL
)
165 widget_move (w
, y
, x
+ menu
->max_hotkey_len
+ 3);
166 tty_print_string (entry
->shortcut
);
169 /* move cursor to the start of entry text */
170 widget_move (w
, y
, x
+ 1);
174 /* --------------------------------------------------------------------------------------------- */
177 menubar_draw_drop (WMenuBar
* menubar
)
179 Widget
*w
= WIDGET (menubar
);
180 const menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
181 const unsigned int count
= g_list_length (menu
->entries
);
182 int column
= menu
->start_x
- 1;
185 if (column
+ menu
->max_entry_len
+ 5 > (gsize
) w
->cols
)
186 column
= w
->cols
- menu
->max_entry_len
- 5;
188 tty_setcolor (MENU_ENTRY_COLOR
);
189 tty_draw_box (w
->y
+ 1, w
->x
+ column
, count
+ 2, menu
->max_entry_len
+ 5, FALSE
);
191 for (i
= 0; i
< count
; i
++)
192 menubar_paint_idx (menubar
, i
,
193 i
== menu
->selected
? MENU_SELECTED_COLOR
: MENU_ENTRY_COLOR
);
196 /* --------------------------------------------------------------------------------------------- */
199 menubar_set_color (WMenuBar
* menubar
, gboolean current
, gboolean hotkey
)
201 if (!widget_get_state (WIDGET (menubar
), WST_FOCUSED
))
202 tty_setcolor (MENU_INACTIVE_COLOR
);
204 tty_setcolor (hotkey
? MENU_HOTSEL_COLOR
: MENU_SELECTED_COLOR
);
206 tty_setcolor (hotkey
? MENU_HOT_COLOR
: MENU_ENTRY_COLOR
);
209 /* --------------------------------------------------------------------------------------------- */
212 menubar_draw (WMenuBar
* menubar
)
214 Widget
*w
= WIDGET (menubar
);
217 /* First draw the complete menubar */
218 tty_setcolor (widget_get_state (w
, WST_FOCUSED
) ? MENU_ENTRY_COLOR
: MENU_INACTIVE_COLOR
);
219 tty_draw_hline (w
->y
, w
->x
, ' ', w
->cols
);
221 /* Now each one of the entries */
222 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
224 menu_t
*menu
= MENU (i
->data
);
225 gboolean is_selected
= (menubar
->selected
== (gsize
) g_list_position (menubar
->menu
, i
));
227 menubar_set_color (menubar
, is_selected
, FALSE
);
228 widget_move (w
, 0, menu
->start_x
);
230 tty_print_char (' ');
231 tty_print_string (menu
->text
.start
);
233 if (menu
->text
.hotkey
!= NULL
)
235 menubar_set_color (menubar
, is_selected
, TRUE
);
236 tty_print_string (menu
->text
.hotkey
);
237 menubar_set_color (menubar
, is_selected
, FALSE
);
240 if (menu
->text
.end
!= NULL
)
241 tty_print_string (menu
->text
.end
);
243 tty_print_char (' ');
246 if (menubar
->is_dropped
)
247 menubar_draw_drop (menubar
);
249 widget_move (w
, 0, MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
))->start_x
);
252 /* --------------------------------------------------------------------------------------------- */
255 menubar_remove (WMenuBar
* menubar
)
259 if (!menubar
->is_dropped
)
262 /* HACK: before refresh the dialog, change the current widget to keep the order
263 of overlapped widgets. This is useful in multi-window editor.
264 In general, menubar should be a special object, not an ordinary widget
265 in the current dialog. */
266 h
= WIDGET (menubar
)->owner
;
267 h
->current
= g_list_find (h
->widgets
, dlg_find_by_id (h
, menubar
->previous_widget
));
269 menubar
->is_dropped
= FALSE
;
271 menubar
->is_dropped
= TRUE
;
273 /* restore current widget */
274 h
->current
= g_list_find (h
->widgets
, menubar
);
277 /* --------------------------------------------------------------------------------------------- */
280 menubar_left (WMenuBar
* menubar
)
282 menubar_remove (menubar
);
283 if (menubar
->selected
== 0)
284 menubar
->selected
= g_list_length (menubar
->menu
) - 1;
287 menubar_draw (menubar
);
290 /* --------------------------------------------------------------------------------------------- */
293 menubar_right (WMenuBar
* menubar
)
295 menubar_remove (menubar
);
296 menubar
->selected
= (menubar
->selected
+ 1) % g_list_length (menubar
->menu
);
297 menubar_draw (menubar
);
300 /* --------------------------------------------------------------------------------------------- */
303 menubar_finish (WMenuBar
* menubar
)
305 Widget
*w
= WIDGET (menubar
);
307 widget_set_state (w
, WST_FOCUSED
, FALSE
);
308 menubar
->is_dropped
= FALSE
;
310 widget_want_hotkey (w
, FALSE
);
311 widget_set_options (w
, WOP_SELECTABLE
, FALSE
);
313 /* Move the menubar to the bottom so that widgets displayed on top of
314 * an "invisible" menubar get the first chance to respond to mouse events. */
315 widget_set_bottom (w
);
317 dlg_select_by_id (w
->owner
, menubar
->previous_widget
);
321 /* --------------------------------------------------------------------------------------------- */
324 menubar_drop (WMenuBar
* menubar
, unsigned int selected
)
326 menubar
->is_dropped
= TRUE
;
327 menubar
->selected
= selected
;
328 menubar_draw (menubar
);
331 /* --------------------------------------------------------------------------------------------- */
334 menubar_execute (WMenuBar
* menubar
)
336 const menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
337 const menu_entry_t
*entry
= MENUENTRY (g_list_nth_data (menu
->entries
, menu
->selected
));
339 if ((entry
!= NULL
) && (entry
->command
!= CK_IgnoreKey
))
341 Widget
*w
= WIDGET (menubar
);
343 mc_global
.widget
.is_right
= (menubar
->selected
!= 0);
344 menubar_finish (menubar
);
345 send_message (w
->owner
, w
, MSG_ACTION
, entry
->command
, NULL
);
350 /* --------------------------------------------------------------------------------------------- */
353 menubar_down (WMenuBar
* menubar
)
355 menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
356 const unsigned int len
= g_list_length (menu
->entries
);
359 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
363 menu
->selected
= (menu
->selected
+ 1) % len
;
364 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, menu
->selected
));
366 while ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
));
368 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
371 /* --------------------------------------------------------------------------------------------- */
374 menubar_up (WMenuBar
* menubar
)
376 menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
377 const unsigned int len
= g_list_length (menu
->entries
);
380 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
384 if (menu
->selected
== 0)
385 menu
->selected
= len
- 1;
388 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, menu
->selected
));
390 while ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
));
392 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
395 /* --------------------------------------------------------------------------------------------- */
398 menubar_first (WMenuBar
* menubar
)
400 menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
402 if (menu
->selected
== 0)
405 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
413 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, menu
->selected
));
415 if ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
))
421 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
424 /* --------------------------------------------------------------------------------------------- */
427 menubar_last (WMenuBar
* menubar
)
429 menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
430 const unsigned int len
= g_list_length (menu
->entries
);
433 if (menu
->selected
== len
- 1)
436 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
438 menu
->selected
= len
;
443 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, menu
->selected
));
445 while ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
));
447 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
450 /* --------------------------------------------------------------------------------------------- */
453 menubar_handle_key (WMenuBar
* menubar
, int key
)
457 key
= g_ascii_tolower (key
);
459 if (is_abort_char (key
))
461 menubar_finish (menubar
);
465 /* menubar help or menubar navigation */
470 ev_help_t event_data
= { NULL
, NULL
};
472 if (menubar
->is_dropped
)
474 MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
))->help_node
;
476 event_data
.node
= "[Menu Bar]";
478 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
479 menubar_draw (menubar
);
485 menubar_left (menubar
);
490 menubar_right (menubar
);
497 if (!menubar
->is_dropped
)
501 /* drop menu by hotkey */
502 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
504 menu_t
*menu
= MENU (i
->data
);
506 if ((menu
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (menu
->text
.hotkey
[0])))
508 menubar_drop (menubar
, g_list_position (menubar
->menu
, i
));
513 /* drop menu by Enter or Dowwn key */
514 if (key
== KEY_ENTER
|| key
== XCTRL ('n') || key
== KEY_DOWN
|| key
== '\n')
515 menubar_drop (menubar
, menubar
->selected
);
521 menu_t
*menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
524 /* execute menu command by hotkey */
525 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
527 const menu_entry_t
*entry
= MENUENTRY (i
->data
);
529 if ((entry
!= NULL
) && (entry
->command
!= CK_IgnoreKey
)
530 && (entry
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (entry
->text
.hotkey
[0])))
532 menu
->selected
= g_list_position (menu
->entries
, i
);
533 menubar_execute (menubar
);
538 /* menu execute by Enter or menu navigation */
543 menubar_execute (menubar
);
548 menubar_first (menubar
);
553 menubar_last (menubar
);
558 menubar_down (menubar
);
563 menubar_up (menubar
);
574 /* --------------------------------------------------------------------------------------------- */
577 menubar_refresh (WMenuBar
* menubar
)
579 Widget
*w
= WIDGET (menubar
);
581 if (!widget_get_state (w
, WST_FOCUSED
))
584 /* Trick to get all the mouse events */
587 /* Trick to get all of the hotkeys */
588 widget_want_hotkey (w
, TRUE
);
592 /* --------------------------------------------------------------------------------------------- */
595 menubar_free_menu (WMenuBar
* menubar
)
597 if (menubar
->menu
!= NULL
)
598 g_list_free_full (menubar
->menu
, (GDestroyNotify
) destroy_menu
);
601 /* --------------------------------------------------------------------------------------------- */
604 menubar_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
606 WMenuBar
*menubar
= MENUBAR (w
);
610 /* We do not want the focus unless we have been activated */
612 if (menubar_refresh (menubar
))
614 menubar_draw (menubar
);
617 return MSG_NOT_HANDLED
;
620 return widget_get_state (w
, WST_FOCUSED
) ? MSG_NOT_HANDLED
: MSG_HANDLED
;
622 /* We don't want the buttonbar to activate while using the menubar */
625 if (widget_get_state (w
, WST_FOCUSED
))
627 menubar_handle_key (menubar
, parm
);
630 return MSG_NOT_HANDLED
;
633 /* Put the cursor in a suitable place */
634 return MSG_NOT_HANDLED
;
637 if (menubar
->is_visible
|| menubar_refresh (menubar
))
638 menubar_draw (menubar
);
642 /* try show menu after screen resize */
643 menubar_refresh (menubar
);
647 menubar_free_menu (menubar
);
651 return widget_default_callback (w
, sender
, msg
, parm
, data
);
655 /* --------------------------------------------------------------------------------------------- */
658 menubar_get_menu_by_x_coord (const WMenuBar
* menubar
, int x
)
663 for (i
= 0, menu
= menubar
->menu
;
664 menu
!= NULL
&& x
> MENU (menu
->data
)->start_x
; i
++, menu
= g_list_next (menu
))
667 /* Don't set the invalid value -1 */
674 /* --------------------------------------------------------------------------------------------- */
677 menubar_mouse_on_menu (const WMenuBar
* menubar
, int y
, int x
)
679 Widget
*w
= WIDGET (menubar
);
681 int left_x
, right_x
, bottom_y
;
683 if (!menubar
->is_dropped
)
686 menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
687 left_x
= menu
->start_x
;
688 right_x
= left_x
+ menu
->max_entry_len
+ 3;
689 if (right_x
> w
->cols
)
691 left_x
= w
->cols
- (menu
->max_entry_len
+ 3);
695 bottom_y
= g_list_length (menu
->entries
) + 2; /* skip bar and top frame */
697 return (x
>= left_x
&& x
< right_x
&& y
> 1 && y
< bottom_y
);
700 /* --------------------------------------------------------------------------------------------- */
703 menubar_change_selected_item (WMenuBar
* menubar
, int y
)
708 y
-= 2; /* skip bar and top frame */
709 menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
710 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, y
));
712 if (entry
!= NULL
&& entry
->command
!= CK_IgnoreKey
)
714 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
716 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
720 /* --------------------------------------------------------------------------------------------- */
723 menubar_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
725 static gboolean was_drag
= FALSE
;
727 WMenuBar
*menubar
= MENUBAR (w
);
728 gboolean mouse_on_drop
;
730 mouse_on_drop
= menubar_mouse_on_menu (menubar
, event
->y
, event
->x
);
739 /* events on menubar */
740 unsigned int selected
;
742 selected
= menubar_get_menu_by_x_coord (menubar
, event
->x
);
743 menubar_activate (menubar
, TRUE
, selected
);
744 menubar_remove (menubar
); /* if already shown */
745 menubar_drop (menubar
, selected
);
747 else if (mouse_on_drop
)
748 menubar_change_selected_item (menubar
, event
->y
);
751 /* mouse click outside menubar or dropdown -- close menu */
752 menubar_finish (menubar
);
757 * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
758 * events belonging to this click (in case the user drags the mouse,
761 * For the time being, we mark this with FIXME as this flag should
762 * preferably be regarded as "implementation detail" and not be
763 * touched by us. We should think of some other way of communicating
764 * this to the system.
766 w
->mouse
.capture
= FALSE
;
771 if (was_drag
&& mouse_on_drop
)
772 menubar_execute (menubar
);
776 case MSG_MOUSE_CLICK
:
779 if ((event
->buttons
& GPM_B_MIDDLE
) != 0 && event
->y
> 0 && menubar
->is_dropped
)
781 /* middle click -- everywhere */
782 menubar_execute (menubar
);
784 else if (mouse_on_drop
)
785 menubar_execute (menubar
);
786 else if (event
->y
> 0)
787 /* releasing the mouse button outside the menu -- close menu */
788 menubar_finish (menubar
);
794 menubar_remove (menubar
);
795 menubar_drop (menubar
, menubar_get_menu_by_x_coord (menubar
, event
->x
));
797 else if (mouse_on_drop
)
798 menubar_change_selected_item (menubar
, event
->y
);
803 case MSG_MOUSE_SCROLL_UP
:
804 case MSG_MOUSE_SCROLL_DOWN
:
807 if (widget_get_state (w
, WST_FOCUSED
))
811 /* menubar: left/right */
812 if (msg
== MSG_MOUSE_SCROLL_UP
)
813 menubar_left (menubar
);
815 menubar_right (menubar
);
817 else if (mouse_on_drop
)
819 /* drop-down menu: up/down */
820 if (msg
== MSG_MOUSE_SCROLL_UP
)
821 menubar_up (menubar
);
823 menubar_down (menubar
);
834 /* --------------------------------------------------------------------------------------------- */
835 /*** public functions ****************************************************************************/
836 /* --------------------------------------------------------------------------------------------- */
839 menu_entry_create (const char *name
, long command
)
843 entry
= g_new (menu_entry_t
, 1);
844 entry
->first_letter
= ' ';
845 entry
->text
= parse_hotkey (name
);
846 entry
->command
= command
;
847 entry
->shortcut
= NULL
;
852 /* --------------------------------------------------------------------------------------------- */
855 menu_entry_free (menu_entry_t
* entry
)
859 release_hotkey (entry
->text
);
860 g_free (entry
->shortcut
);
865 /* --------------------------------------------------------------------------------------------- */
868 create_menu (const char *name
, GList
* entries
, const char *help_node
)
872 menu
= g_new (menu_t
, 1);
874 menu
->text
= parse_hotkey (name
);
875 menu
->entries
= entries
;
876 menu
->max_entry_len
= 1;
877 menu
->max_hotkey_len
= 0;
879 menu
->help_node
= g_strdup (help_node
);
884 /* --------------------------------------------------------------------------------------------- */
887 menu_set_name (menu_t
* menu
, const char *name
)
889 release_hotkey (menu
->text
);
890 menu
->text
= parse_hotkey (name
);
893 /* --------------------------------------------------------------------------------------------- */
896 destroy_menu (menu_t
* menu
)
898 release_hotkey (menu
->text
);
899 g_list_free_full (menu
->entries
, (GDestroyNotify
) menu_entry_free
);
900 g_free (menu
->help_node
);
904 /* --------------------------------------------------------------------------------------------- */
907 menubar_new (int y
, int x
, int cols
, GList
* menu
, gboolean visible
)
912 menubar
= g_new0 (WMenuBar
, 1);
913 w
= WIDGET (menubar
);
914 widget_init (w
, y
, x
, 1, cols
, menubar_callback
, menubar_mouse_callback
);
915 /* initially, menubar is not selectable */
916 widget_set_options (w
, WOP_SELECTABLE
, FALSE
);
917 w
->options
|= WOP_TOP_SELECT
;
918 menubar
->is_visible
= visible
;
919 menubar_set_menu (menubar
, menu
);
924 /* --------------------------------------------------------------------------------------------- */
927 menubar_set_menu (WMenuBar
* menubar
, GList
* menu
)
929 /* delete previous menu */
930 menubar_free_menu (menubar
);
932 menubar
->is_dropped
= FALSE
;
933 menubar
->menu
= menu
;
934 menubar
->selected
= 0;
935 menubar_arrange (menubar
);
936 widget_set_state (WIDGET (menubar
), WST_FOCUSED
, FALSE
);
939 /* --------------------------------------------------------------------------------------------- */
942 menubar_add_menu (WMenuBar
* menubar
, menu_t
* menu
)
946 menu_arrange (menu
, WIDGET (menubar
)->owner
->get_shortcut
);
947 menubar
->menu
= g_list_append (menubar
->menu
, menu
);
950 menubar_arrange (menubar
);
953 /* --------------------------------------------------------------------------------------------- */
955 * Properly space menubar items. Should be called when menubar is created
956 * and also when widget width is changed (i.e. upon xterm resize).
960 menubar_arrange (WMenuBar
* menubar
)
966 if (menubar
->menu
== NULL
)
969 gap
= WIDGET (menubar
)->cols
- 2;
971 /* First, calculate gap between items... */
972 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
974 menu_t
*menu
= MENU (i
->data
);
976 /* preserve length here, to be used below */
977 menu
->start_x
= hotkey_width (menu
->text
) + 2;
978 gap
-= menu
->start_x
;
981 if (g_list_next (menubar
->menu
) == NULL
)
984 gap
/= (g_list_length (menubar
->menu
) - 1);
988 /* We are out of luck - window is too narrow... */
994 /* ...and now fix start positions of menubar items */
995 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
997 menu_t
*menu
= MENU (i
->data
);
998 int len
= menu
->start_x
;
1000 menu
->start_x
= start_x
;
1001 start_x
+= len
+ gap
;
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Find MenuBar widget in the dialog */
1009 find_menubar (const WDialog
* h
)
1011 return MENUBAR (find_widget_type (h
, menubar_callback
));
1014 /* --------------------------------------------------------------------------------------------- */
1017 * Activate menu bar.
1019 * @param menubar menu bar object
1020 * @param dropped whether dropdown menus should be drooped or not
1021 * @which number of active dropdown menu
1024 menubar_activate (WMenuBar
* menubar
, gboolean dropped
, int which
)
1026 Widget
*w
= WIDGET (menubar
);
1028 if (!widget_get_state (w
, WST_FOCUSED
))
1030 widget_set_options (w
, WOP_SELECTABLE
, TRUE
);
1032 widget_set_state (w
, WST_FOCUSED
, TRUE
); /* FIXME: unneeded? */
1033 menubar
->is_dropped
= dropped
;
1035 menubar
->selected
= (guint
) which
;
1037 menubar
->previous_widget
= dlg_get_current_widget_id (w
->owner
);
1039 /* Bring it to the top so it receives all mouse events before any other widget.
1040 * See also comment in menubar_finish(). */
1045 /* --------------------------------------------------------------------------------------------- */