4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 * \brief Source: pulldown menu code
33 #include <sys/types.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
39 #include "lib/tty/mouse.h"
40 #include "lib/tty/key.h" /* key macros */
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
43 #include "lib/event.h" /* mc_event_raise() */
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 static cb_ret_t
menubar_callback (Widget
* w
, widget_msg_t msg
, int parm
);
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
59 menu_arrange (Menu
* menu
, dlg_shortcut_str get_shortcut
)
64 size_t max_shortcut_len
= 0;
66 menu
->max_entry_len
= 1;
67 menu
->max_hotkey_len
= 1;
69 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
71 menu_entry_t
*entry
= i
->data
;
77 len
= (size_t) hotkey_width (entry
->text
);
78 menu
->max_hotkey_len
= max (menu
->max_hotkey_len
, len
);
80 if (get_shortcut
!= NULL
)
81 entry
->shortcut
= get_shortcut (entry
->command
);
83 if (entry
->shortcut
!= NULL
)
85 len
= (size_t) str_term_width1 (entry
->shortcut
);
86 max_shortcut_len
= max (max_shortcut_len
, len
);
91 menu
->max_entry_len
= menu
->max_hotkey_len
+ max_shortcut_len
;
95 /* --------------------------------------------------------------------------------------------- */
98 menubar_paint_idx (WMenuBar
* menubar
, unsigned int idx
, int color
)
100 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
101 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, idx
);
102 const int y
= 2 + idx
;
103 int x
= menu
->start_x
;
105 if (x
+ menu
->max_entry_len
+ 4 > (gsize
) menubar
->widget
.cols
)
106 x
= menubar
->widget
.cols
- menu
->max_entry_len
- 4;
111 tty_setcolor (MENU_ENTRY_COLOR
);
113 widget_move (&menubar
->widget
, y
, x
- 1);
114 tty_print_alt_char (ACS_LTEE
, FALSE
);
116 tty_draw_hline (menubar
->widget
.y
+ y
, menubar
->widget
.x
+ x
,
117 ACS_HLINE
, menu
->max_entry_len
+ 3);
119 widget_move (&menubar
->widget
, y
, x
+ menu
->max_entry_len
+ 3);
120 tty_print_alt_char (ACS_RTEE
, FALSE
);
125 tty_setcolor (color
);
126 widget_move (&menubar
->widget
, y
, x
);
127 tty_print_char ((unsigned char) entry
->first_letter
);
128 tty_draw_hline (-1, -1, ' ', menu
->max_entry_len
+ 2); /* clear line */
129 tty_print_string (entry
->text
.start
);
131 if (entry
->text
.hotkey
!= NULL
)
133 tty_setcolor (color
== MENU_SELECTED_COLOR
? MENU_HOTSEL_COLOR
: MENU_HOT_COLOR
);
134 tty_print_string (entry
->text
.hotkey
);
135 tty_setcolor (color
);
138 if (entry
->text
.end
!= NULL
)
139 tty_print_string (entry
->text
.end
);
141 if (entry
->shortcut
!= NULL
)
143 widget_move (&menubar
->widget
, y
, x
+ menu
->max_hotkey_len
+ 3);
144 tty_print_string (entry
->shortcut
);
147 /* move cursor to the start of entry text */
148 widget_move (&menubar
->widget
, y
, x
+ 1);
152 /* --------------------------------------------------------------------------------------------- */
155 menubar_draw_drop (WMenuBar
* menubar
)
157 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
158 const unsigned int count
= g_list_length (menu
->entries
);
159 int column
= menu
->start_x
- 1;
162 if (column
+ menu
->max_entry_len
+ 5 > (gsize
) menubar
->widget
.cols
)
163 column
= menubar
->widget
.cols
- menu
->max_entry_len
- 5;
165 tty_setcolor (MENU_ENTRY_COLOR
);
166 draw_box (menubar
->widget
.owner
,
167 menubar
->widget
.y
+ 1, menubar
->widget
.x
+ column
,
168 count
+ 2, menu
->max_entry_len
+ 5, FALSE
);
170 for (i
= 0; i
< count
; i
++)
171 menubar_paint_idx (menubar
, i
,
172 i
== menu
->selected
? MENU_SELECTED_COLOR
: MENU_ENTRY_COLOR
);
175 /* --------------------------------------------------------------------------------------------- */
178 menubar_set_color (WMenuBar
* menubar
, gboolean current
, gboolean hotkey
)
180 if (!menubar
->is_active
)
181 tty_setcolor (MENU_INACTIVE_COLOR
);
183 tty_setcolor (hotkey
? MENU_HOTSEL_COLOR
: MENU_SELECTED_COLOR
);
185 tty_setcolor (hotkey
? MENU_HOT_COLOR
: MENU_ENTRY_COLOR
);
188 /* --------------------------------------------------------------------------------------------- */
191 menubar_draw (WMenuBar
* menubar
)
195 /* First draw the complete menubar */
196 tty_setcolor (menubar
->is_active
? MENU_ENTRY_COLOR
: MENU_INACTIVE_COLOR
);
197 tty_draw_hline (menubar
->widget
.y
, menubar
->widget
.x
, ' ', menubar
->widget
.cols
);
199 /* Now each one of the entries */
200 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
202 Menu
*menu
= i
->data
;
203 gboolean is_selected
= (menubar
->selected
== (gsize
) g_list_position (menubar
->menu
, i
));
205 menubar_set_color (menubar
, is_selected
, FALSE
);
206 widget_move (&menubar
->widget
, 0, menu
->start_x
);
208 tty_print_char (' ');
209 tty_print_string (menu
->text
.start
);
211 if (menu
->text
.hotkey
!= NULL
)
213 menubar_set_color (menubar
, is_selected
, TRUE
);
214 tty_print_string (menu
->text
.hotkey
);
215 menubar_set_color (menubar
, is_selected
, FALSE
);
218 if (menu
->text
.end
!= NULL
)
219 tty_print_string (menu
->text
.end
);
221 tty_print_char (' ');
224 if (menubar
->is_dropped
)
225 menubar_draw_drop (menubar
);
227 widget_move (&menubar
->widget
, 0,
228 ((Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
))->start_x
);
231 /* --------------------------------------------------------------------------------------------- */
234 menubar_remove (WMenuBar
* menubar
)
236 if (menubar
->is_dropped
)
238 menubar
->is_dropped
= FALSE
;
240 menubar
->is_dropped
= TRUE
;
244 /* --------------------------------------------------------------------------------------------- */
247 menubar_left (WMenuBar
* menubar
)
249 menubar_remove (menubar
);
250 if (menubar
->selected
== 0)
251 menubar
->selected
= g_list_length (menubar
->menu
) - 1;
254 menubar_draw (menubar
);
257 /* --------------------------------------------------------------------------------------------- */
260 menubar_right (WMenuBar
* menubar
)
262 menubar_remove (menubar
);
263 menubar
->selected
= (menubar
->selected
+ 1) % g_list_length (menubar
->menu
);
264 menubar_draw (menubar
);
267 /* --------------------------------------------------------------------------------------------- */
270 menubar_finish (WMenuBar
* menubar
)
272 menubar
->is_dropped
= FALSE
;
273 menubar
->is_active
= FALSE
;
274 menubar
->widget
.lines
= 1;
275 widget_want_hotkey (menubar
->widget
, 0);
277 dlg_select_by_id (menubar
->widget
.owner
, menubar
->previous_widget
);
281 /* --------------------------------------------------------------------------------------------- */
284 menubar_drop (WMenuBar
* menubar
, unsigned int selected
)
286 menubar
->is_dropped
= TRUE
;
287 menubar
->selected
= selected
;
288 menubar_draw (menubar
);
291 /* --------------------------------------------------------------------------------------------- */
294 menubar_execute (WMenuBar
* menubar
)
296 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
297 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, menu
->selected
);
299 if ((entry
!= NULL
) && (entry
->command
!= CK_IgnoreKey
))
301 mc_global
.widget
.is_right
= (menubar
->selected
!= 0);
302 menubar_finish (menubar
);
303 menubar
->widget
.owner
->callback (menubar
->widget
.owner
, &menubar
->widget
,
304 DLG_ACTION
, entry
->command
, NULL
);
309 /* --------------------------------------------------------------------------------------------- */
312 menubar_down (WMenuBar
* menubar
)
314 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
315 const unsigned int len
= g_list_length (menu
->entries
);
318 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
322 menu
->selected
= (menu
->selected
+ 1) % len
;
323 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
325 while ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
));
327 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
330 /* --------------------------------------------------------------------------------------------- */
333 menubar_up (WMenuBar
* menubar
)
335 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
336 const unsigned int len
= g_list_length (menu
->entries
);
339 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
343 if (menu
->selected
== 0)
344 menu
->selected
= len
- 1;
347 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
349 while ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
));
351 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
354 /* --------------------------------------------------------------------------------------------- */
357 menubar_first (WMenuBar
* menubar
)
359 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
362 if (menu
->selected
== 0)
365 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
371 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
373 if ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
))
379 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
382 /* --------------------------------------------------------------------------------------------- */
385 menubar_last (WMenuBar
* menubar
)
387 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
388 const unsigned int len
= g_list_length (menu
->entries
);
391 if (menu
->selected
== len
- 1)
394 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
396 menu
->selected
= len
;
401 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
403 while ((entry
== NULL
) || (entry
->command
== CK_IgnoreKey
));
405 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
408 /* --------------------------------------------------------------------------------------------- */
411 menubar_handle_key (WMenuBar
* menubar
, int key
)
415 key
= g_ascii_tolower (key
);
417 if (is_abort_char (key
))
419 menubar_finish (menubar
);
423 /* menubar help or menubar navigation */
428 ev_help_t event_data
= { NULL
, NULL
};
430 if (menubar
->is_dropped
)
432 ((Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
))->help_node
;
434 event_data
.node
= "[Menu Bar]";
436 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
437 menubar_draw (menubar
);
442 menubar_left (menubar
);
447 menubar_right (menubar
);
451 if (!menubar
->is_dropped
)
455 /* drop menu by hotkey */
456 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
458 Menu
*menu
= i
->data
;
460 if ((menu
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (menu
->text
.hotkey
[0])))
462 menubar_drop (menubar
, g_list_position (menubar
->menu
, i
));
467 /* drop menu by Enter or Dowwn key */
468 if (key
== KEY_ENTER
|| key
== XCTRL ('n') || key
== KEY_DOWN
|| key
== '\n')
469 menubar_drop (menubar
, menubar
->selected
);
475 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
478 /* execute menu command by hotkey */
479 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
481 const menu_entry_t
*entry
= i
->data
;
483 if ((entry
!= NULL
) && (entry
->command
!= CK_IgnoreKey
)
484 && (entry
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (entry
->text
.hotkey
[0])))
486 menu
->selected
= g_list_position (menu
->entries
, i
);
487 menubar_execute (menubar
);
492 /* menu execute by Enter or menu navigation */
497 menubar_execute (menubar
);
502 menubar_first (menubar
);
507 menubar_last (menubar
);
512 menubar_down (menubar
);
517 menubar_up (menubar
);
525 /* --------------------------------------------------------------------------------------------- */
528 menubar_callback (Widget
* w
, widget_msg_t msg
, int parm
)
530 WMenuBar
*menubar
= (WMenuBar
*) w
;
534 /* We do not want the focus unless we have been activated */
536 if (!menubar
->is_active
)
537 return MSG_NOT_HANDLED
;
539 /* Trick to get all the mouse events */
540 menubar
->widget
.lines
= LINES
;
542 /* Trick to get all of the hotkeys */
543 widget_want_hotkey (menubar
->widget
, 1);
544 menubar_draw (menubar
);
547 /* We don't want the buttonbar to activate while using the menubar */
550 if (menubar
->is_active
)
552 menubar_handle_key (menubar
, parm
);
555 return MSG_NOT_HANDLED
;
558 /* Put the cursor in a suitable place */
559 return MSG_NOT_HANDLED
;
562 return menubar
->is_active
? MSG_NOT_HANDLED
: MSG_HANDLED
;
565 if (menubar
->is_visible
)
567 menubar_draw (menubar
);
573 /* try show menu after screen resize */
574 send_message (w
, WIDGET_FOCUS
, 0);
579 menubar_set_menu (menubar
, NULL
);
583 return default_proc (msg
, parm
);
587 /* --------------------------------------------------------------------------------------------- */
590 menubar_event (Gpm_Event
* event
, void *data
)
592 WMenuBar
*menubar
= data
;
593 gboolean was_active
= TRUE
;
594 int left_x
, right_x
, bottom_y
;
597 /* ignore unsupported events */
598 if ((event
->type
& (GPM_UP
| GPM_DOWN
| GPM_DRAG
)) == 0)
601 /* ignore wheel events if menu is inactive */
602 if (!menubar
->is_active
&& ((event
->buttons
& (GPM_B_MIDDLE
| GPM_B_UP
| GPM_B_DOWN
)) != 0))
605 if (!menubar
->is_dropped
)
607 menubar
->previous_widget
= dlg_get_current_widget_id (menubar
->widget
.owner
);
608 menubar
->is_active
= TRUE
;
609 menubar
->is_dropped
= TRUE
;
613 /* Mouse operations on the menubar */
614 if (event
->y
== 1 || !was_active
)
616 if ((event
->type
& GPM_UP
) != 0)
619 /* wheel events on menubar */
620 if (event
->buttons
& GPM_B_UP
)
621 menubar_left (menubar
);
622 else if (event
->buttons
& GPM_B_DOWN
)
623 menubar_right (menubar
);
626 const unsigned int len
= g_list_length (menubar
->menu
);
627 unsigned int new_selection
= 0;
629 while ((new_selection
< len
)
630 && (event
->x
> ((Menu
*) g_list_nth_data (menubar
->menu
,
631 new_selection
))->start_x
))
634 if (new_selection
!= 0) /* Don't set the invalid value -1 */
639 menubar
->selected
= new_selection
;
640 dlg_select_widget (menubar
);
644 menubar_remove (menubar
);
645 menubar
->selected
= new_selection
;
647 menubar_draw (menubar
);
652 if (!menubar
->is_dropped
|| (event
->y
< 2))
655 /* middle click -- everywhere */
656 if (((event
->buttons
& GPM_B_MIDDLE
) != 0) && ((event
->type
& GPM_DOWN
) != 0))
658 menubar_execute (menubar
);
662 /* the mouse operation is on the menus or it is not */
663 menu
= (Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
);
664 left_x
= menu
->start_x
;
665 right_x
= left_x
+ menu
->max_entry_len
+ 3;
666 if (right_x
> menubar
->widget
.cols
)
668 left_x
= menubar
->widget
.cols
- menu
->max_entry_len
- 3;
669 right_x
= menubar
->widget
.cols
;
672 bottom_y
= g_list_length (menu
->entries
) + 3;
674 if ((event
->x
>= left_x
) && (event
->x
<= right_x
) && (event
->y
<= bottom_y
))
676 int pos
= event
->y
- 3;
677 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, pos
);
680 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
))
682 menubar_up (menubar
);
685 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
))
687 menubar_down (menubar
);
691 /* ignore events above and below dropped down menu */
692 if ((pos
< 0) || (pos
>= bottom_y
- 3))
695 if ((entry
!= NULL
) && (entry
->command
!= CK_IgnoreKey
))
697 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
698 menu
->selected
= pos
;
699 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
701 if ((event
->type
& GPM_UP
) != 0)
702 menubar_execute (menubar
);
706 /* use click not wheel to close menu */
707 if (((event
->type
& GPM_DOWN
) != 0) && ((event
->buttons
& (GPM_B_UP
| GPM_B_DOWN
)) == 0))
708 menubar_finish (menubar
);
713 /* --------------------------------------------------------------------------------------------- */
714 /*** public functions ****************************************************************************/
715 /* --------------------------------------------------------------------------------------------- */
718 menu_entry_create (const char *name
, unsigned long command
)
722 entry
= g_new (menu_entry_t
, 1);
723 entry
->first_letter
= ' ';
724 entry
->text
= parse_hotkey (name
);
725 entry
->command
= command
;
726 entry
->shortcut
= NULL
;
731 /* --------------------------------------------------------------------------------------------- */
734 menu_entry_free (menu_entry_t
* entry
)
738 release_hotkey (entry
->text
);
739 g_free (entry
->shortcut
);
744 /* --------------------------------------------------------------------------------------------- */
747 create_menu (const char *name
, GList
* entries
, const char *help_node
)
751 menu
= g_new (Menu
, 1);
753 menu
->text
= parse_hotkey (name
);
754 menu
->entries
= entries
;
755 menu
->max_entry_len
= 1;
756 menu
->max_hotkey_len
= 0;
758 menu
->help_node
= g_strdup (help_node
);
763 /* --------------------------------------------------------------------------------------------- */
766 menu_set_name (Menu
* menu
, const char *name
)
768 release_hotkey (menu
->text
);
769 menu
->text
= parse_hotkey (name
);
772 /* --------------------------------------------------------------------------------------------- */
775 destroy_menu (Menu
* menu
)
777 release_hotkey (menu
->text
);
778 g_list_foreach (menu
->entries
, (GFunc
) menu_entry_free
, NULL
);
779 g_list_free (menu
->entries
);
780 g_free (menu
->help_node
);
784 /* --------------------------------------------------------------------------------------------- */
787 menubar_new (int y
, int x
, int cols
, GList
* menu
)
791 menubar
= g_new0 (WMenuBar
, 1);
792 init_widget (&menubar
->widget
, y
, x
, 1, cols
, menubar_callback
, menubar_event
);
793 widget_want_cursor (menubar
->widget
, FALSE
);
794 menubar
->is_visible
= TRUE
; /* by default */
795 menubar_set_menu (menubar
, menu
);
800 /* --------------------------------------------------------------------------------------------- */
803 menubar_set_menu (WMenuBar
* menubar
, GList
* menu
)
805 /* delete previous menu */
806 if (menubar
->menu
!= NULL
)
808 g_list_foreach (menubar
->menu
, (GFunc
) destroy_menu
, NULL
);
809 g_list_free (menubar
->menu
);
812 menubar
->is_active
= FALSE
;
813 menubar
->is_dropped
= FALSE
;
814 menubar
->menu
= menu
;
815 menubar
->selected
= 0;
816 menubar_arrange (menubar
);
819 /* --------------------------------------------------------------------------------------------- */
822 menubar_add_menu (WMenuBar
* menubar
, Menu
* menu
)
826 menu_arrange (menu
, menubar
->widget
.owner
->get_shortcut
);
827 menubar
->menu
= g_list_append (menubar
->menu
, menu
);
830 menubar_arrange (menubar
);
833 /* --------------------------------------------------------------------------------------------- */
835 * Properly space menubar items. Should be called when menubar is created
836 * and also when widget width is changed (i.e. upon xterm resize).
840 menubar_arrange (WMenuBar
* menubar
)
846 if (menubar
->menu
== NULL
)
849 #ifndef RESIZABLE_MENUBAR
852 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
854 Menu
*menu
= i
->data
;
855 int len
= hotkey_width (menu
->text
) + 2;
857 menu
->start_x
= start_x
;
858 start_x
+= len
+ gap
;
860 #else /* RESIZABLE_MENUBAR */
861 gap
= menubar
->widget
.cols
- 2;
863 /* First, calculate gap between items... */
864 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
866 Menu
*menu
= i
->data
;
867 /* preserve length here, to be used below */
868 menu
->start_x
= hotkey_width (menu
->text
) + 2;
869 gap
-= menu
->start_x
;
872 gap
/= (menubar
->menu
->len
- 1);
876 /* We are out of luck - window is too narrow... */
880 /* ...and now fix start positions of menubar items */
881 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
883 Menu
*menu
= i
->data
;
884 int len
= menu
->start_x
;
886 menu
->start_x
= start_x
;
887 start_x
+= len
+ gap
;
889 #endif /* RESIZABLE_MENUBAR */
892 /* --------------------------------------------------------------------------------------------- */
893 /** Find MenuBar widget in the dialog */
896 find_menubar (const Dlg_head
* h
)
898 return (WMenuBar
*) find_widget_type (h
, menubar_callback
);
901 /* --------------------------------------------------------------------------------------------- */