1 /* Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
2 2007, 2009 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 * \brief Source: pulldown menu code
27 #include <sys/types.h>
29 #include "lib/global.h"
31 #include "lib/tty/tty.h"
33 #include "lib/tty/mouse.h"
34 #include "lib/tty/key.h" /* key macros */
35 #include "lib/strutil.h"
37 #include "cmddef.h" /* CK_Ignore_Key */
41 #include "main.h" /* is_right */
44 int menubar_visible
= 1; /* This is the new default */
46 static cb_ret_t
menubar_callback (Widget
* w
, widget_msg_t msg
, int parm
);
49 menu_entry_create (const char *name
, unsigned long command
)
53 entry
= g_new (menu_entry_t
, 1);
54 entry
->first_letter
= ' ';
55 entry
->text
= parse_hotkey (name
);
56 entry
->command
= command
;
57 entry
->shortcut
= NULL
;
63 menu_entry_free (menu_entry_t
* entry
)
67 release_hotkey (entry
->text
);
68 g_free (entry
->shortcut
);
74 menu_arrange (Menu
* menu
, dlg_shortcut_str get_shortcut
)
79 size_t max_shortcut_len
= 0;
81 menu
->max_entry_len
= 1;
82 menu
->max_hotkey_len
= 1;
84 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
86 menu_entry_t
*entry
= i
->data
;
92 len
= (size_t) hotkey_width (entry
->text
);
93 menu
->max_hotkey_len
= max (menu
->max_hotkey_len
, len
);
95 if (get_shortcut
!= NULL
)
96 entry
->shortcut
= get_shortcut (entry
->command
);
98 if (entry
->shortcut
!= NULL
)
100 len
= (size_t) str_term_width1 (entry
->shortcut
);
101 max_shortcut_len
= max (max_shortcut_len
, len
);
106 menu
->max_entry_len
= menu
->max_hotkey_len
+ max_shortcut_len
;
111 create_menu (const char *name
, GList
* entries
, const char *help_node
)
115 menu
= g_new (Menu
, 1);
117 menu
->text
= parse_hotkey (name
);
118 menu
->entries
= entries
;
119 menu
->max_entry_len
= 1;
120 menu
->max_hotkey_len
= 0;
122 menu
->help_node
= g_strdup (help_node
);
128 destroy_menu (Menu
* menu
)
130 release_hotkey (menu
->text
);
131 g_list_foreach (menu
->entries
, (GFunc
) menu_entry_free
, NULL
);
132 g_list_free (menu
->entries
);
133 g_free (menu
->help_node
);
138 menubar_paint_idx (WMenuBar
* menubar
, unsigned int idx
, int color
)
140 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
141 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, idx
);
142 const int y
= 2 + idx
;
143 int x
= menu
->start_x
;
145 if (x
+ menu
->max_entry_len
+ 4 > (gsize
) menubar
->widget
.cols
)
146 x
= menubar
->widget
.cols
- menu
->max_entry_len
- 4;
151 tty_setcolor (MENU_ENTRY_COLOR
);
153 widget_move (&menubar
->widget
, y
, x
- 1);
154 tty_print_alt_char (ACS_LTEE
, FALSE
);
156 tty_draw_hline (menubar
->widget
.y
+ y
, menubar
->widget
.x
+ x
,
157 ACS_HLINE
, menu
->max_entry_len
+ 3);
159 widget_move (&menubar
->widget
, y
, x
+ menu
->max_entry_len
+ 3);
160 tty_print_alt_char (ACS_RTEE
, FALSE
);
165 tty_setcolor (color
);
166 widget_move (&menubar
->widget
, y
, x
);
167 tty_print_char ((unsigned char) entry
->first_letter
);
168 tty_draw_hline (-1, -1, ' ', menu
->max_entry_len
+ 2); /* clear line */
169 tty_print_string (entry
->text
.start
);
171 if (entry
->text
.hotkey
!= NULL
)
173 tty_setcolor (color
== MENU_SELECTED_COLOR
? MENU_HOTSEL_COLOR
: MENU_HOT_COLOR
);
174 tty_print_string (entry
->text
.hotkey
);
175 tty_setcolor (color
);
178 if (entry
->text
.end
!= NULL
)
179 tty_print_string (entry
->text
.end
);
181 if (entry
->shortcut
!= NULL
)
183 widget_move (&menubar
->widget
, y
, x
+ menu
->max_hotkey_len
+ 3);
184 tty_print_string (entry
->shortcut
);
187 /* move cursor to the start of entry text */
188 widget_move (&menubar
->widget
, y
, x
+ 1);
193 menubar_draw_drop (WMenuBar
* menubar
)
195 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
196 const unsigned int count
= g_list_length (menu
->entries
);
197 int column
= menu
->start_x
- 1;
200 if (column
+ menu
->max_entry_len
+ 5 > (gsize
) menubar
->widget
.cols
)
201 column
= menubar
->widget
.cols
- menu
->max_entry_len
- 5;
203 tty_setcolor (MENU_ENTRY_COLOR
);
204 draw_box (menubar
->widget
.parent
,
205 menubar
->widget
.y
+ 1, menubar
->widget
.x
+ column
,
206 count
+ 2, menu
->max_entry_len
+ 5, FALSE
);
208 /* draw items except selected */
209 for (i
= 0; i
< count
; i
++)
210 if (i
!= menu
->selected
)
211 menubar_paint_idx (menubar
, i
, MENU_ENTRY_COLOR
);
213 /* draw selected item at last to move cursor to the nice location */
214 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
218 menubar_set_color (WMenuBar
* menubar
, gboolean current
, gboolean hotkey
)
220 if (!menubar
->is_active
)
221 tty_setcolor (MENU_INACTIVE_COLOR
);
223 tty_setcolor (hotkey
? MENU_HOTSEL_COLOR
: MENU_SELECTED_COLOR
);
225 tty_setcolor (hotkey
? MENU_HOT_COLOR
: MENU_ENTRY_COLOR
);
229 menubar_draw (WMenuBar
* menubar
)
233 /* First draw the complete menubar */
234 tty_setcolor (menubar
->is_active
? MENU_ENTRY_COLOR
: MENU_INACTIVE_COLOR
);
235 tty_draw_hline (menubar
->widget
.y
, menubar
->widget
.x
, ' ', menubar
->widget
.cols
);
237 /* Now each one of the entries */
238 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
240 Menu
*menu
= i
->data
;
241 gboolean is_selected
= (menubar
->selected
== (gsize
) g_list_position (menubar
->menu
, i
));
243 menubar_set_color (menubar
, is_selected
, FALSE
);
244 widget_move (&menubar
->widget
, 0, menu
->start_x
);
246 tty_print_char (' ');
247 tty_print_string (menu
->text
.start
);
249 if (menu
->text
.hotkey
!= NULL
)
251 menubar_set_color (menubar
, is_selected
, TRUE
);
252 tty_print_string (menu
->text
.hotkey
);
253 menubar_set_color (menubar
, is_selected
, FALSE
);
256 if (menu
->text
.end
!= NULL
)
257 tty_print_string (menu
->text
.end
);
259 tty_print_char (' ');
262 if (menubar
->is_dropped
)
263 menubar_draw_drop (menubar
);
265 widget_move (&menubar
->widget
, 0,
266 ((Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
))->start_x
);
270 menubar_remove (WMenuBar
* menubar
)
272 if (menubar
->is_dropped
)
274 menubar
->is_dropped
= FALSE
;
276 menubar
->is_dropped
= TRUE
;
281 menubar_left (WMenuBar
* menubar
)
283 menubar_remove (menubar
);
284 if (menubar
->selected
== 0)
285 menubar
->selected
= g_list_length (menubar
->menu
) - 1;
288 menubar_draw (menubar
);
292 menubar_right (WMenuBar
* menubar
)
294 menubar_remove (menubar
);
295 menubar
->selected
= (menubar
->selected
+ 1) % g_list_length (menubar
->menu
);
296 menubar_draw (menubar
);
300 menubar_finish (WMenuBar
* menubar
)
302 menubar
->is_dropped
= FALSE
;
303 menubar
->is_active
= FALSE
;
304 menubar
->widget
.lines
= 1;
305 widget_want_hotkey (menubar
->widget
, 0);
307 dlg_select_by_id (menubar
->widget
.parent
, menubar
->previous_widget
);
312 menubar_drop (WMenuBar
* menubar
, unsigned int selected
)
314 menubar
->is_dropped
= TRUE
;
315 menubar
->selected
= selected
;
316 menubar_draw (menubar
);
320 menubar_execute (WMenuBar
* menubar
)
322 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
323 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, menu
->selected
);
325 if ((entry
!= NULL
) && (entry
->command
!= CK_Ignore_Key
))
327 is_right
= (menubar
->selected
!= 0);
328 menubar_finish (menubar
);
329 menubar
->widget
.parent
->callback (menubar
->widget
.parent
, &menubar
->widget
,
330 DLG_ACTION
, entry
->command
, NULL
);
336 menubar_down (WMenuBar
* menubar
)
338 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
339 const unsigned int len
= g_list_length (menu
->entries
);
342 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
346 menu
->selected
= (menu
->selected
+ 1) % len
;
347 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
349 while ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
));
351 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
355 menubar_up (WMenuBar
* menubar
)
357 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
358 const unsigned int len
= g_list_length (menu
->entries
);
361 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
365 if (menu
->selected
== 0)
366 menu
->selected
= len
- 1;
369 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
371 while ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
));
373 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
377 menubar_first (WMenuBar
* menubar
)
379 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
382 if (menu
->selected
== 0)
385 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
391 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
393 if ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
))
399 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
403 menubar_last (WMenuBar
* menubar
)
405 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
406 const unsigned int len
= g_list_length (menu
->entries
);
409 if (menu
->selected
== len
- 1)
412 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
414 menu
->selected
= len
;
419 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
421 while ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
));
423 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
427 menubar_handle_key (WMenuBar
* menubar
, int key
)
431 key
= g_ascii_tolower (key
);
433 if (is_abort_char (key
))
435 menubar_finish (menubar
);
439 /* menubar help or menubar navigation */
443 if (menubar
->is_dropped
)
444 interactive_display (NULL
,
445 ((Menu
*) g_list_nth_data (menubar
->menu
,
446 menubar
->selected
))->help_node
);
448 interactive_display (NULL
, "[Menu Bar]");
449 menubar_draw (menubar
);
454 menubar_left (menubar
);
459 menubar_right (menubar
);
463 if (!menubar
->is_dropped
)
467 /* drop menu by hotkey */
468 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
470 Menu
*menu
= i
->data
;
472 if ((menu
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (menu
->text
.hotkey
[0])))
474 menubar_drop (menubar
, g_list_position (menubar
->menu
, i
));
479 /* drop menu by Enter or Dowwn key */
480 if (key
== KEY_ENTER
|| key
== XCTRL ('n') || key
== KEY_DOWN
|| key
== '\n')
481 menubar_drop (menubar
, menubar
->selected
);
487 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
490 /* execute menu command by hotkey */
491 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
493 const menu_entry_t
*entry
= i
->data
;
495 if ((entry
!= NULL
) && (entry
->command
!= CK_Ignore_Key
)
496 && (entry
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (entry
->text
.hotkey
[0])))
498 menu
->selected
= g_list_position (menu
->entries
, i
);
499 menubar_execute (menubar
);
504 /* menu execute by Enter or menu navigation */
509 menubar_execute (menubar
);
514 menubar_first (menubar
);
519 menubar_last (menubar
);
524 menubar_down (menubar
);
529 menubar_up (menubar
);
538 menubar_callback (Widget
* w
, widget_msg_t msg
, int parm
)
540 WMenuBar
*menubar
= (WMenuBar
*) w
;
544 /* We do not want the focus unless we have been activated */
546 if (!menubar
->is_active
)
547 return MSG_NOT_HANDLED
;
549 widget_want_cursor (menubar
->widget
, 1);
551 /* Trick to get all the mouse events */
552 menubar
->widget
.lines
= LINES
;
554 /* Trick to get all of the hotkeys */
555 widget_want_hotkey (menubar
->widget
, 1);
556 menubar_draw (menubar
);
559 /* We don't want the buttonbar to activate while using the menubar */
562 if (menubar
->is_active
)
564 menubar_handle_key (menubar
, parm
);
567 return MSG_NOT_HANDLED
;
570 /* Put the cursor in a suitable place */
571 return MSG_NOT_HANDLED
;
574 if (menubar
->is_active
)
575 return MSG_NOT_HANDLED
;
577 widget_want_cursor (menubar
->widget
, 0);
583 menubar_draw (menubar
);
589 /* try show menu after screen resize */
590 send_message (w
, WIDGET_FOCUS
, 0);
595 menubar_set_menu (menubar
, NULL
);
599 return default_proc (msg
, parm
);
604 menubar_event (Gpm_Event
* event
, void *data
)
606 WMenuBar
*menubar
= data
;
607 gboolean was_active
= TRUE
;
608 int left_x
, right_x
, bottom_y
;
611 /* ignore unsupported events */
612 if ((event
->type
& (GPM_UP
| GPM_DOWN
| GPM_DRAG
)) == 0)
615 /* ignore wheel events if menu is inactive */
616 if (!menubar
->is_active
&& ((event
->buttons
& (GPM_B_MIDDLE
| GPM_B_UP
| GPM_B_DOWN
)) != 0))
619 if (!menubar
->is_dropped
)
621 menubar
->previous_widget
= menubar
->widget
.parent
->current
->dlg_id
;
622 menubar
->is_active
= TRUE
;
623 menubar
->is_dropped
= TRUE
;
627 /* Mouse operations on the menubar */
628 if (event
->y
== 1 || !was_active
)
630 if ((event
->type
& GPM_UP
) != 0)
633 /* wheel events on menubar */
634 if (event
->buttons
& GPM_B_UP
)
635 menubar_left (menubar
);
636 else if (event
->buttons
& GPM_B_DOWN
)
637 menubar_right (menubar
);
640 const unsigned int len
= g_list_length (menubar
->menu
);
641 unsigned int new_selection
= 0;
643 while ((new_selection
< len
)
644 && (event
->x
> ((Menu
*) g_list_nth_data (menubar
->menu
,
645 new_selection
))->start_x
))
648 if (new_selection
!= 0) /* Don't set the invalid value -1 */
653 menubar
->selected
= new_selection
;
654 dlg_select_widget (menubar
);
658 menubar_remove (menubar
);
659 menubar
->selected
= new_selection
;
661 menubar_draw (menubar
);
666 if (!menubar
->is_dropped
|| (event
->y
< 2))
669 /* middle click -- everywhere */
670 if (((event
->buttons
& GPM_B_MIDDLE
) != 0) && ((event
->type
& GPM_DOWN
) != 0))
672 menubar_execute (menubar
);
676 /* the mouse operation is on the menus or it is not */
677 menu
= (Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
);
678 left_x
= menu
->start_x
;
679 right_x
= left_x
+ menu
->max_entry_len
+ 3;
680 if (right_x
> menubar
->widget
.cols
)
682 left_x
= menubar
->widget
.cols
- menu
->max_entry_len
- 3;
683 right_x
= menubar
->widget
.cols
;
686 bottom_y
= g_list_length (menu
->entries
) + 3;
688 if ((event
->x
>= left_x
) && (event
->x
<= right_x
) && (event
->y
<= bottom_y
))
690 int pos
= event
->y
- 3;
691 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, pos
);
694 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
))
696 menubar_up (menubar
);
699 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
))
701 menubar_down (menubar
);
705 /* ignore events above and below dropped down menu */
706 if ((pos
< 0) || (pos
>= bottom_y
- 3))
709 if ((entry
!= NULL
) && (entry
->command
!= CK_Ignore_Key
))
711 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
712 menu
->selected
= pos
;
713 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
715 if ((event
->type
& GPM_UP
) != 0)
716 menubar_execute (menubar
);
720 /* use click not wheel to close menu */
721 if (((event
->type
& GPM_DOWN
) != 0) && ((event
->buttons
& (GPM_B_UP
| GPM_B_DOWN
)) == 0))
722 menubar_finish (menubar
);
728 menubar_new (int y
, int x
, int cols
, GList
* menu
)
730 WMenuBar
*menubar
= g_new0 (WMenuBar
, 1);
732 init_widget (&menubar
->widget
, y
, x
, 1, cols
, menubar_callback
, menubar_event
);
733 widget_want_cursor (menubar
->widget
, 0);
734 menubar_set_menu (menubar
, menu
);
739 menubar_set_menu (WMenuBar
* menubar
, GList
* menu
)
741 /* delete previous menu */
742 if (menubar
->menu
!= NULL
)
744 g_list_foreach (menubar
->menu
, (GFunc
) destroy_menu
, NULL
);
745 g_list_free (menubar
->menu
);
748 menubar
->is_active
= FALSE
;
749 menubar
->is_dropped
= FALSE
;
750 menubar
->menu
= menu
;
751 menubar
->selected
= 0;
752 menubar_arrange (menubar
);
756 menubar_add_menu (WMenuBar
* menubar
, Menu
* menu
)
760 menu_arrange (menu
, menubar
->widget
.parent
->get_shortcut
);
761 menubar
->menu
= g_list_append (menubar
->menu
, menu
);
764 menubar_arrange (menubar
);
768 * Properly space menubar items. Should be called when menubar is created
769 * and also when widget width is changed (i.e. upon xterm resize).
772 menubar_arrange (WMenuBar
* menubar
)
778 if (menubar
->menu
== NULL
)
781 #ifndef RESIZABLE_MENUBAR
784 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
786 Menu
*menu
= i
->data
;
787 int len
= hotkey_width (menu
->text
) + 2;
789 menu
->start_x
= start_x
;
790 start_x
+= len
+ gap
;
792 #else /* RESIZABLE_MENUBAR */
793 gap
= menubar
->widget
.cols
- 2;
795 /* First, calculate gap between items... */
796 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_nwxt (i
))
798 Menu
*menu
= i
->data
;
799 /* preserve length here, to be used below */
800 menu
->start_x
= hotkey_width (menu
->text
) + 2;
801 gap
-= menu
->start_x
;
804 gap
/= (menubar
->menu
->len
- 1);
808 /* We are out of luck - window is too narrow... */
812 /* ...and now fix start positions of menubar items */
813 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_nwxt (i
))
815 Menu
*menu
= i
->data
;
816 int len
= menu
->start_x
;
818 menu
->start_x
= start_x
;
819 start_x
+= len
+ gap
;
821 #endif /* RESIZABLE_MENUBAR */
824 /* Find MenuBar widget in the dialog */
826 find_menubar (const Dlg_head
* h
)
828 return (WMenuBar
*) find_widget_type (h
, menubar_callback
);