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 for (i
= 0; i
< count
; i
++)
209 menubar_paint_idx (menubar
, i
,
210 i
== menu
->selected
? MENU_SELECTED_COLOR
: MENU_ENTRY_COLOR
);
214 menubar_set_color (WMenuBar
* menubar
, gboolean current
, gboolean hotkey
)
216 if (!menubar
->is_active
)
217 tty_setcolor (MENU_INACTIVE_COLOR
);
219 tty_setcolor (hotkey
? MENU_HOTSEL_COLOR
: MENU_SELECTED_COLOR
);
221 tty_setcolor (hotkey
? MENU_HOT_COLOR
: MENU_ENTRY_COLOR
);
225 menubar_draw (WMenuBar
* menubar
)
229 /* First draw the complete menubar */
230 tty_setcolor (menubar
->is_active
? MENU_ENTRY_COLOR
: MENU_INACTIVE_COLOR
);
231 tty_draw_hline (menubar
->widget
.y
, menubar
->widget
.x
, ' ', menubar
->widget
.cols
);
233 /* Now each one of the entries */
234 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
236 Menu
*menu
= i
->data
;
237 gboolean is_selected
= (menubar
->selected
== (gsize
) g_list_position (menubar
->menu
, i
));
239 menubar_set_color (menubar
, is_selected
, FALSE
);
240 widget_move (&menubar
->widget
, 0, menu
->start_x
);
242 tty_print_char (' ');
243 tty_print_string (menu
->text
.start
);
245 if (menu
->text
.hotkey
!= NULL
)
247 menubar_set_color (menubar
, is_selected
, TRUE
);
248 tty_print_string (menu
->text
.hotkey
);
249 menubar_set_color (menubar
, is_selected
, FALSE
);
252 if (menu
->text
.end
!= NULL
)
253 tty_print_string (menu
->text
.end
);
255 tty_print_char (' ');
258 if (menubar
->is_dropped
)
259 menubar_draw_drop (menubar
);
261 widget_move (&menubar
->widget
, 0,
262 ((Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
))->start_x
);
266 menubar_remove (WMenuBar
* menubar
)
268 if (menubar
->is_dropped
)
270 menubar
->is_dropped
= FALSE
;
272 menubar
->is_dropped
= TRUE
;
277 menubar_left (WMenuBar
* menubar
)
279 menubar_remove (menubar
);
280 if (menubar
->selected
== 0)
281 menubar
->selected
= g_list_length (menubar
->menu
) - 1;
284 menubar_draw (menubar
);
288 menubar_right (WMenuBar
* menubar
)
290 menubar_remove (menubar
);
291 menubar
->selected
= (menubar
->selected
+ 1) % g_list_length (menubar
->menu
);
292 menubar_draw (menubar
);
296 menubar_finish (WMenuBar
* menubar
)
298 menubar
->is_dropped
= FALSE
;
299 menubar
->is_active
= FALSE
;
300 menubar
->widget
.lines
= 1;
301 widget_want_hotkey (menubar
->widget
, 0);
303 dlg_select_by_id (menubar
->widget
.parent
, menubar
->previous_widget
);
308 menubar_drop (WMenuBar
* menubar
, unsigned int selected
)
310 menubar
->is_dropped
= TRUE
;
311 menubar
->selected
= selected
;
312 menubar_draw (menubar
);
316 menubar_execute (WMenuBar
* menubar
)
318 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
319 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, menu
->selected
);
321 if ((entry
!= NULL
) && (entry
->command
!= CK_Ignore_Key
))
323 is_right
= (menubar
->selected
!= 0);
324 menubar_finish (menubar
);
325 menubar
->widget
.parent
->callback (menubar
->widget
.parent
, &menubar
->widget
,
326 DLG_ACTION
, entry
->command
, NULL
);
332 menubar_down (WMenuBar
* menubar
)
334 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
335 const unsigned int len
= g_list_length (menu
->entries
);
338 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
342 menu
->selected
= (menu
->selected
+ 1) % len
;
343 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
345 while ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
));
347 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
351 menubar_up (WMenuBar
* menubar
)
353 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
354 const unsigned int len
= g_list_length (menu
->entries
);
357 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
361 if (menu
->selected
== 0)
362 menu
->selected
= len
- 1;
365 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
367 while ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
));
369 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
373 menubar_first (WMenuBar
* menubar
)
375 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
378 if (menu
->selected
== 0)
381 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
387 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
389 if ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
))
395 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
399 menubar_last (WMenuBar
* menubar
)
401 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
402 const unsigned int len
= g_list_length (menu
->entries
);
405 if (menu
->selected
== len
- 1)
408 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
410 menu
->selected
= len
;
415 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
417 while ((entry
== NULL
) || (entry
->command
== CK_Ignore_Key
));
419 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
423 menubar_handle_key (WMenuBar
* menubar
, int key
)
427 key
= g_ascii_tolower (key
);
429 if (is_abort_char (key
))
431 menubar_finish (menubar
);
435 /* menubar help or menubar navigation */
439 if (menubar
->is_dropped
)
440 interactive_display (NULL
,
441 ((Menu
*) g_list_nth_data (menubar
->menu
,
442 menubar
->selected
))->help_node
);
444 interactive_display (NULL
, "[Menu Bar]");
445 menubar_draw (menubar
);
450 menubar_left (menubar
);
455 menubar_right (menubar
);
459 if (!menubar
->is_dropped
)
463 /* drop menu by hotkey */
464 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
466 Menu
*menu
= i
->data
;
468 if ((menu
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (menu
->text
.hotkey
[0])))
470 menubar_drop (menubar
, g_list_position (menubar
->menu
, i
));
475 /* drop menu by Enter or Dowwn key */
476 if (key
== KEY_ENTER
|| key
== XCTRL ('n') || key
== KEY_DOWN
|| key
== '\n')
477 menubar_drop (menubar
, menubar
->selected
);
483 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
486 /* execute menu command by hotkey */
487 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
))
489 const menu_entry_t
*entry
= i
->data
;
491 if ((entry
!= NULL
) && (entry
->command
!= CK_Ignore_Key
)
492 && (entry
->text
.hotkey
!= NULL
) && (key
== g_ascii_tolower (entry
->text
.hotkey
[0])))
494 menu
->selected
= g_list_position (menu
->entries
, i
);
495 menubar_execute (menubar
);
500 /* menu execute by Enter or menu navigation */
505 menubar_execute (menubar
);
510 menubar_first (menubar
);
515 menubar_last (menubar
);
520 menubar_down (menubar
);
525 menubar_up (menubar
);
534 menubar_callback (Widget
* w
, widget_msg_t msg
, int parm
)
536 WMenuBar
*menubar
= (WMenuBar
*) w
;
540 /* We do not want the focus unless we have been activated */
542 if (!menubar
->is_active
)
543 return MSG_NOT_HANDLED
;
545 /* Trick to get all the mouse events */
546 menubar
->widget
.lines
= LINES
;
548 /* Trick to get all of the hotkeys */
549 widget_want_hotkey (menubar
->widget
, 1);
550 menubar_draw (menubar
);
553 /* We don't want the buttonbar to activate while using the menubar */
556 if (menubar
->is_active
)
558 menubar_handle_key (menubar
, parm
);
561 return MSG_NOT_HANDLED
;
564 /* Put the cursor in a suitable place */
565 return MSG_NOT_HANDLED
;
568 return menubar
->is_active
? MSG_NOT_HANDLED
: MSG_HANDLED
;
573 menubar_draw (menubar
);
579 /* try show menu after screen resize */
580 send_message (w
, WIDGET_FOCUS
, 0);
585 menubar_set_menu (menubar
, NULL
);
589 return default_proc (msg
, parm
);
594 menubar_event (Gpm_Event
* event
, void *data
)
596 WMenuBar
*menubar
= data
;
597 gboolean was_active
= TRUE
;
598 int left_x
, right_x
, bottom_y
;
601 /* ignore unsupported events */
602 if ((event
->type
& (GPM_UP
| GPM_DOWN
| GPM_DRAG
)) == 0)
605 /* ignore wheel events if menu is inactive */
606 if (!menubar
->is_active
&& ((event
->buttons
& (GPM_B_MIDDLE
| GPM_B_UP
| GPM_B_DOWN
)) != 0))
609 if (!menubar
->is_dropped
)
611 menubar
->previous_widget
= menubar
->widget
.parent
->current
->dlg_id
;
612 menubar
->is_active
= TRUE
;
613 menubar
->is_dropped
= TRUE
;
617 /* Mouse operations on the menubar */
618 if (event
->y
== 1 || !was_active
)
620 if ((event
->type
& GPM_UP
) != 0)
623 /* wheel events on menubar */
624 if (event
->buttons
& GPM_B_UP
)
625 menubar_left (menubar
);
626 else if (event
->buttons
& GPM_B_DOWN
)
627 menubar_right (menubar
);
630 const unsigned int len
= g_list_length (menubar
->menu
);
631 unsigned int new_selection
= 0;
633 while ((new_selection
< len
)
634 && (event
->x
> ((Menu
*) g_list_nth_data (menubar
->menu
,
635 new_selection
))->start_x
))
638 if (new_selection
!= 0) /* Don't set the invalid value -1 */
643 menubar
->selected
= new_selection
;
644 dlg_select_widget (menubar
);
648 menubar_remove (menubar
);
649 menubar
->selected
= new_selection
;
651 menubar_draw (menubar
);
656 if (!menubar
->is_dropped
|| (event
->y
< 2))
659 /* middle click -- everywhere */
660 if (((event
->buttons
& GPM_B_MIDDLE
) != 0) && ((event
->type
& GPM_DOWN
) != 0))
662 menubar_execute (menubar
);
666 /* the mouse operation is on the menus or it is not */
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
> menubar
->widget
.cols
)
672 left_x
= menubar
->widget
.cols
- menu
->max_entry_len
- 3;
673 right_x
= menubar
->widget
.cols
;
676 bottom_y
= g_list_length (menu
->entries
) + 3;
678 if ((event
->x
>= left_x
) && (event
->x
<= right_x
) && (event
->y
<= bottom_y
))
680 int pos
= event
->y
- 3;
681 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, pos
);
684 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
))
686 menubar_up (menubar
);
689 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
))
691 menubar_down (menubar
);
695 /* ignore events above and below dropped down menu */
696 if ((pos
< 0) || (pos
>= bottom_y
- 3))
699 if ((entry
!= NULL
) && (entry
->command
!= CK_Ignore_Key
))
701 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
702 menu
->selected
= pos
;
703 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
705 if ((event
->type
& GPM_UP
) != 0)
706 menubar_execute (menubar
);
710 /* use click not wheel to close menu */
711 if (((event
->type
& GPM_DOWN
) != 0) && ((event
->buttons
& (GPM_B_UP
| GPM_B_DOWN
)) == 0))
712 menubar_finish (menubar
);
718 menubar_new (int y
, int x
, int cols
, GList
* menu
)
720 WMenuBar
*menubar
= g_new0 (WMenuBar
, 1);
722 init_widget (&menubar
->widget
, y
, x
, 1, cols
, menubar_callback
, menubar_event
);
723 widget_want_cursor (menubar
->widget
, 0);
724 menubar_set_menu (menubar
, menu
);
729 menubar_set_menu (WMenuBar
* menubar
, GList
* menu
)
731 /* delete previous menu */
732 if (menubar
->menu
!= NULL
)
734 g_list_foreach (menubar
->menu
, (GFunc
) destroy_menu
, NULL
);
735 g_list_free (menubar
->menu
);
738 menubar
->is_active
= FALSE
;
739 menubar
->is_dropped
= FALSE
;
740 menubar
->menu
= menu
;
741 menubar
->selected
= 0;
742 menubar_arrange (menubar
);
746 menubar_add_menu (WMenuBar
* menubar
, Menu
* menu
)
750 menu_arrange (menu
, menubar
->widget
.parent
->get_shortcut
);
751 menubar
->menu
= g_list_append (menubar
->menu
, menu
);
754 menubar_arrange (menubar
);
758 * Properly space menubar items. Should be called when menubar is created
759 * and also when widget width is changed (i.e. upon xterm resize).
762 menubar_arrange (WMenuBar
* menubar
)
768 if (menubar
->menu
== NULL
)
771 #ifndef RESIZABLE_MENUBAR
774 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
))
776 Menu
*menu
= i
->data
;
777 int len
= hotkey_width (menu
->text
) + 2;
779 menu
->start_x
= start_x
;
780 start_x
+= len
+ gap
;
782 #else /* RESIZABLE_MENUBAR */
783 gap
= menubar
->widget
.cols
- 2;
785 /* First, calculate gap between items... */
786 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_nwxt (i
))
788 Menu
*menu
= i
->data
;
789 /* preserve length here, to be used below */
790 menu
->start_x
= hotkey_width (menu
->text
) + 2;
791 gap
-= menu
->start_x
;
794 gap
/= (menubar
->menu
->len
- 1);
798 /* We are out of luck - window is too narrow... */
802 /* ...and now fix start positions of menubar items */
803 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_nwxt (i
))
805 Menu
*menu
= i
->data
;
806 int len
= menu
->start_x
;
808 menu
->start_x
= start_x
;
809 start_x
+= len
+ gap
;
811 #endif /* RESIZABLE_MENUBAR */
814 /* Find MenuBar widget in the dialog */
816 find_menubar (const Dlg_head
* h
)
818 return (WMenuBar
*) find_widget_type (h
, menubar_callback
);