4 Copyright (C) 1994-2016
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 (!menubar
->is_active
)
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 (menubar
->is_active
? 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 menubar
->is_dropped
= FALSE
;
308 menubar
->is_active
= 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 dlg_set_bottom_widget (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_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
579 WMenuBar
*menubar
= MENUBAR (w
);
583 /* We do not want the focus unless we have been activated */
585 if (!menubar
->is_active
)
586 return MSG_NOT_HANDLED
;
588 /* Trick to get all the mouse events */
591 /* Trick to get all of the hotkeys */
592 widget_want_hotkey (w
, TRUE
);
593 menubar_draw (menubar
);
596 /* We don't want the buttonbar to activate while using the menubar */
599 if (menubar
->is_active
)
601 menubar_handle_key (menubar
, parm
);
604 return MSG_NOT_HANDLED
;
607 /* Put the cursor in a suitable place */
608 return MSG_NOT_HANDLED
;
611 return menubar
->is_active
? MSG_NOT_HANDLED
: MSG_HANDLED
;
614 if (menubar
->is_visible
)
616 menubar_draw (menubar
);
622 /* try show menu after screen resize */
623 send_message (w
, sender
, MSG_FOCUS
, 0, data
);
628 menubar_set_menu (menubar
, NULL
);
632 return widget_default_callback (w
, sender
, msg
, parm
, data
);
636 /* --------------------------------------------------------------------------------------------- */
639 menubar_get_menu_by_x_coord (const WMenuBar
* menubar
, int x
)
644 for (i
= 0, menu
= menubar
->menu
;
645 menu
!= NULL
&& x
> MENU (menu
->data
)->start_x
; i
++, menu
= g_list_next (menu
))
648 /* Don't set the invalid value -1 */
655 /* --------------------------------------------------------------------------------------------- */
658 menubar_mouse_on_menu (const WMenuBar
* menubar
, int y
, int x
)
660 Widget
*w
= WIDGET (menubar
);
662 int left_x
, right_x
, bottom_y
;
664 if (!menubar
->is_dropped
)
667 menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
668 left_x
= menu
->start_x
;
669 right_x
= left_x
+ menu
->max_entry_len
+ 3;
670 if (right_x
> w
->cols
)
672 left_x
= w
->cols
- (menu
->max_entry_len
+ 3);
676 bottom_y
= g_list_length (menu
->entries
) + 2; /* skip bar and top frame */
678 return (x
>= left_x
&& x
< right_x
&& y
> 1 && y
< bottom_y
);
681 /* --------------------------------------------------------------------------------------------- */
684 menubar_change_selected_item (WMenuBar
* menubar
, int y
)
689 y
-= 2; /* skip bar and top frame */
690 menu
= MENU (g_list_nth_data (menubar
->menu
, menubar
->selected
));
691 entry
= MENUENTRY (g_list_nth_data (menu
->entries
, y
));
693 if (entry
!= NULL
&& entry
->command
!= CK_IgnoreKey
)
695 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
697 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
701 /* --------------------------------------------------------------------------------------------- */
704 menubar_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
706 static gboolean was_drag
= FALSE
;
708 WMenuBar
*menubar
= MENUBAR (w
);
709 gboolean mouse_on_drop
;
711 mouse_on_drop
= menubar_mouse_on_menu (menubar
, event
->y
, event
->x
);
720 /* events on menubar */
721 unsigned int selected
;
723 selected
= menubar_get_menu_by_x_coord (menubar
, event
->x
);
724 menubar_activate (menubar
, TRUE
, selected
);
725 menubar_remove (menubar
); /* if already shown */
726 menubar_drop (menubar
, selected
);
728 else if (mouse_on_drop
)
729 menubar_change_selected_item (menubar
, event
->y
);
732 /* mouse click outside menubar or dropdown -- close menu */
733 menubar_finish (menubar
);
738 * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
739 * events belonging to this click (in case the user drags the mouse,
742 * For the time being, we mark this with FIXME as this flag should
743 * preferably be regarded as "implementation detail" and not be
744 * touched by us. We should think of some other way of communicating
745 * this to the system.
747 w
->mouse
.capture
= FALSE
;
752 if (was_drag
&& mouse_on_drop
)
753 menubar_execute (menubar
);
757 case MSG_MOUSE_CLICK
:
760 if ((event
->buttons
& GPM_B_MIDDLE
) != 0 && event
->y
> 0 && menubar
->is_dropped
)
762 /* middle click -- everywhere */
763 menubar_execute (menubar
);
765 else if (mouse_on_drop
)
766 menubar_execute (menubar
);
767 else if (event
->y
> 0)
768 /* releasing the mouse button outside the menu -- close menu */
769 menubar_finish (menubar
);
775 menubar_remove (menubar
);
776 menubar_drop (menubar
, menubar_get_menu_by_x_coord (menubar
, event
->x
));
778 else if (mouse_on_drop
)
779 menubar_change_selected_item (menubar
, event
->y
);
784 case MSG_MOUSE_SCROLL_UP
:
785 case MSG_MOUSE_SCROLL_DOWN
:
788 if (menubar
->is_active
)
792 /* menubar: left/right */
793 if (msg
== MSG_MOUSE_SCROLL_UP
)
794 menubar_left (menubar
);
796 menubar_right (menubar
);
798 else if (mouse_on_drop
)
800 /* drop-down menu: up/down */
801 if (msg
== MSG_MOUSE_SCROLL_UP
)
802 menubar_up (menubar
);
804 menubar_down (menubar
);
815 /* --------------------------------------------------------------------------------------------- */
816 /*** public functions ****************************************************************************/
817 /* --------------------------------------------------------------------------------------------- */
820 menu_entry_create (const char *name
, long command
)
824 entry
= g_new (menu_entry_t
, 1);
825 entry
->first_letter
= ' ';
826 entry
->text
= parse_hotkey (name
);
827 entry
->command
= command
;
828 entry
->shortcut
= NULL
;
833 /* --------------------------------------------------------------------------------------------- */
836 menu_entry_free (menu_entry_t
* entry
)
840 release_hotkey (entry
->text
);
841 g_free (entry
->shortcut
);
846 /* --------------------------------------------------------------------------------------------- */
849 create_menu (const char *name
, GList
* entries
, const char *help_node
)
853 menu
= g_new (menu_t
, 1);
855 menu
->text
= parse_hotkey (name
);
856 menu
->entries
= entries
;
857 menu
->max_entry_len
= 1;
858 menu
->max_hotkey_len
= 0;
860 menu
->help_node
= g_strdup (help_node
);
865 /* --------------------------------------------------------------------------------------------- */
868 menu_set_name (menu_t
* menu
, const char *name
)
870 release_hotkey (menu
->text
);
871 menu
->text
= parse_hotkey (name
);
874 /* --------------------------------------------------------------------------------------------- */
877 destroy_menu (menu_t
* menu
)
879 release_hotkey (menu
->text
);
880 g_list_free_full (menu
->entries
, (GDestroyNotify
) menu_entry_free
);
881 g_free (menu
->help_node
);
885 /* --------------------------------------------------------------------------------------------- */
888 menubar_new (int y
, int x
, int cols
, GList
* menu
, gboolean visible
)
893 menubar
= g_new0 (WMenuBar
, 1);
894 w
= WIDGET (menubar
);
895 widget_init (w
, y
, x
, 1, cols
, menubar_callback
, menubar_mouse_callback
);
896 /* initially, menubar is not selectable */
897 widget_set_options (w
, WOP_SELECTABLE
, FALSE
);
898 w
->options
|= WOP_TOP_SELECT
;
899 menubar
->is_visible
= visible
;
900 menubar_set_menu (menubar
, menu
);
905 /* --------------------------------------------------------------------------------------------- */
908 menubar_set_menu (WMenuBar
* menubar
, GList
* menu
)
910 /* delete previous menu */
911 if (menubar
->menu
!= NULL
)
912 g_list_free_full (menubar
->menu
, (GDestroyNotify
) destroy_menu
);
915 menubar
->is_active
= FALSE
;
916 menubar
->is_dropped
= FALSE
;
917 menubar
->menu
= menu
;
918 menubar
->selected
= 0;
919 menubar_arrange (menubar
);
922 /* --------------------------------------------------------------------------------------------- */
925 menubar_add_menu (WMenuBar
* menubar
, menu_t
* menu
)
929 menu_arrange (menu
, WIDGET (menubar
)->owner
->get_shortcut
);
930 menubar
->menu
= g_list_append (menubar
->menu
, menu
);
933 menubar_arrange (menubar
);
936 /* --------------------------------------------------------------------------------------------- */
938 * Properly space menubar items. Should be called when menubar is created
939 * and also when widget width is changed (i.e. upon xterm resize).
943 menubar_arrange (WMenuBar
* menubar
)
949 if (menubar
->menu
== NULL
)
952 gap
= WIDGET (menubar
)->cols
- 2;
954 /* First, calculate gap between items... */
955 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
957 menu_t
*menu
= MENU (i
->data
);
959 /* preserve length here, to be used below */
960 menu
->start_x
= hotkey_width (menu
->text
) + 2;
961 gap
-= menu
->start_x
;
964 if (g_list_next (menubar
->menu
) == NULL
)
967 gap
/= (g_list_length (menubar
->menu
) - 1);
971 /* We are out of luck - window is too narrow... */
977 /* ...and now fix start positions of menubar items */
978 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
980 menu_t
*menu
= MENU (i
->data
);
981 int len
= menu
->start_x
;
983 menu
->start_x
= start_x
;
984 start_x
+= len
+ gap
;
988 /* --------------------------------------------------------------------------------------------- */
989 /** Find MenuBar widget in the dialog */
992 find_menubar (const WDialog
* h
)
994 return MENUBAR (find_widget_type (h
, menubar_callback
));
997 /* --------------------------------------------------------------------------------------------- */
1000 * Activate menu bar.
1002 * @param menubar menu bar object
1003 * @param dropped whether dropdown menus should be drooped or not
1004 * @which number of active dropdown menu
1007 menubar_activate (WMenuBar
* menubar
, gboolean dropped
, int which
)
1009 Widget
*w
= WIDGET (menubar
);
1011 if (!menubar
->is_active
)
1013 widget_set_options (w
, WOP_SELECTABLE
, TRUE
);
1015 menubar
->is_active
= TRUE
;
1016 menubar
->is_dropped
= dropped
;
1018 menubar
->selected
= (guint
) which
;
1020 menubar
->previous_widget
= dlg_get_current_widget_id (w
->owner
);
1022 /* Bring it to the top so it receives all mouse events before any other widget.
1023 * See also comment in menubar_finish(). */
1024 dlg_select_widget (w
);
1028 /* --------------------------------------------------------------------------------------------- */