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>
31 #include "../src/tty/tty.h"
32 #include "../src/skin/skin.h"
33 #include "../src/tty/mouse.h"
34 #include "../src/tty/key.h" /* key macros */
40 #include "main.h" /* is_right */
43 int menubar_visible
= 1; /* This is the new default */
46 menu_entry_create (const char *name
, int command
)
50 entry
= g_new (menu_entry_t
, 1);
51 entry
->first_letter
= ' ';
52 entry
->text
= parse_hotkey (name
);
53 entry
->command
= command
;
54 entry
->shortcut
= NULL
;
60 menu_entry_free (menu_entry_t
*entry
)
63 release_hotkey (entry
->text
);
64 g_free (entry
->shortcut
);
70 menu_arrange (Menu
*menu
, dlg_shortcut_str get_shortcut
)
74 size_t max_shortcut_len
= 0;
76 menu
->max_entry_len
= 1;
77 menu
->max_hotkey_len
= 1;
79 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
)) {
80 menu_entry_t
*entry
= i
->data
;
85 len
= (size_t) hotkey_width (entry
->text
);
86 menu
->max_hotkey_len
= max (menu
->max_hotkey_len
, len
);
88 if (get_shortcut
!= NULL
)
89 entry
->shortcut
= get_shortcut (entry
->command
);
91 if (entry
->shortcut
!= NULL
) {
92 len
= (size_t) str_term_width1 (entry
->shortcut
);
93 max_shortcut_len
= max (max_shortcut_len
, len
);
98 menu
->max_entry_len
= menu
->max_hotkey_len
+ max_shortcut_len
;
103 create_menu (const char *name
, GList
*entries
, const char *help_node
)
107 menu
= g_new (Menu
, 1);
109 menu
->text
= parse_hotkey (name
);
110 menu
->entries
= entries
;
111 menu
->max_entry_len
= 1;
112 menu
->max_hotkey_len
= 0;
114 menu
->help_node
= g_strdup (help_node
);
120 destroy_menu (Menu
*menu
)
122 release_hotkey (menu
->text
);
123 g_list_foreach (menu
->entries
, (GFunc
) menu_entry_free
, NULL
);
124 g_list_free (menu
->entries
);
125 g_free (menu
->help_node
);
130 menubar_paint_idx (WMenuBar
*menubar
, unsigned int idx
, int color
)
132 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
133 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, idx
);
134 const int y
= 2 + idx
;
135 int x
= menu
->start_x
;
137 if (x
+ menu
->max_entry_len
+ 4 > (gsize
) menubar
->widget
.cols
)
138 x
= menubar
->widget
.cols
- menu
->max_entry_len
- 4;
142 tty_setcolor (MENU_ENTRY_COLOR
);
144 widget_move (&menubar
->widget
, y
, x
- 1);
145 tty_print_alt_char (ACS_LTEE
);
147 tty_draw_hline (menubar
->widget
.y
+ y
, menubar
->widget
.x
+ x
,
148 ACS_HLINE
, menu
->max_entry_len
+ 3);
150 widget_move (&menubar
->widget
, y
, x
+ menu
->max_entry_len
+ 3);
151 tty_print_alt_char (ACS_RTEE
);
154 tty_setcolor (color
);
155 widget_move (&menubar
->widget
, y
, x
);
156 tty_print_char ((unsigned char) entry
->first_letter
);
157 tty_draw_hline (-1, -1, ' ', menu
->max_entry_len
+ 2); /* clear line */
158 tty_print_string (entry
->text
.start
);
160 if (entry
->text
.hotkey
!= NULL
) {
161 tty_setcolor (color
== MENU_SELECTED_COLOR
?
162 MENU_HOTSEL_COLOR
: MENU_HOT_COLOR
);
163 tty_print_string (entry
->text
.hotkey
);
164 tty_setcolor (color
);
167 if (entry
->text
.end
!= NULL
)
168 tty_print_string (entry
->text
.end
);
170 if (entry
->shortcut
!= NULL
) {
171 widget_move (&menubar
->widget
, y
, x
+ menu
->max_hotkey_len
+ 3);
172 tty_print_string (entry
->shortcut
);
175 /* move cursor to the start of entry text */
176 widget_move (&menubar
->widget
, y
, x
+ 1);
181 menubar_draw_drop (WMenuBar
*menubar
)
183 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
184 const unsigned int count
= g_list_length (menu
->entries
);
185 int column
= menu
->start_x
- 1;
188 if (column
+ menu
->max_entry_len
+ 5 > (gsize
) menubar
->widget
.cols
)
189 column
= menubar
->widget
.cols
- menu
->max_entry_len
- 5;
191 tty_setcolor (MENU_ENTRY_COLOR
);
192 draw_box (menubar
->widget
.parent
,
193 menubar
->widget
.y
+ 1, menubar
->widget
.x
+ column
,
194 count
+ 2, menu
->max_entry_len
+ 5);
196 /* draw items except selected */
197 for (i
= 0; i
< count
; i
++)
198 if (i
!= menu
->selected
)
199 menubar_paint_idx (menubar
, i
, MENU_ENTRY_COLOR
);
201 /* draw selected item at last to move cursor to the nice location */
202 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
206 menubar_set_color (WMenuBar
*menubar
, gboolean current
, gboolean hotkey
)
208 if (!menubar
->is_active
)
209 tty_setcolor (hotkey
? COLOR_HOT_FOCUS
: SELECTED_COLOR
);
211 tty_setcolor (hotkey
? MENU_HOTSEL_COLOR
: MENU_SELECTED_COLOR
);
213 tty_setcolor (hotkey
? MENU_HOT_COLOR
: MENU_ENTRY_COLOR
);
217 menubar_draw (WMenuBar
*menubar
)
221 /* First draw the complete menubar */
222 tty_setcolor (menubar
->is_active
? MENU_ENTRY_COLOR
: SELECTED_COLOR
);
223 tty_draw_hline (menubar
->widget
.y
, menubar
->widget
.x
, ' ', menubar
->widget
.cols
);
225 /* Now each one of the entries */
226 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
)) {
227 Menu
*menu
= i
->data
;
228 gboolean is_selected
= (menubar
->selected
== (gsize
) g_list_position (menubar
->menu
, i
));
230 menubar_set_color (menubar
, is_selected
, FALSE
);
231 widget_move (&menubar
->widget
, 0, menu
->start_x
);
233 tty_print_char (' ');
234 tty_print_string (menu
->text
.start
);
236 if (menu
->text
.hotkey
!= NULL
) {
237 menubar_set_color (menubar
, is_selected
, TRUE
);
238 tty_print_string (menu
->text
.hotkey
);
239 menubar_set_color (menubar
, is_selected
, FALSE
);
242 if (menu
->text
.end
!= NULL
)
243 tty_print_string (menu
->text
.end
);
245 tty_print_char (' ');
248 if (menubar
->is_dropped
)
249 menubar_draw_drop (menubar
);
251 widget_move (&menubar
->widget
, 0,
252 ((Menu
*) g_list_nth_data (menubar
->menu
,
253 menubar
->selected
))->start_x
);
257 menubar_remove (WMenuBar
*menubar
)
259 if (menubar
->is_dropped
) {
260 menubar
->is_dropped
= FALSE
;
262 menubar
->is_dropped
= TRUE
;
267 menubar_left (WMenuBar
*menubar
)
269 menubar_remove (menubar
);
270 if (menubar
->selected
== 0)
271 menubar
->selected
= g_list_length (menubar
->menu
) - 1;
274 menubar_draw (menubar
);
278 menubar_right (WMenuBar
*menubar
)
280 menubar_remove (menubar
);
281 menubar
->selected
= (menubar
->selected
+ 1) % g_list_length (menubar
->menu
);
282 menubar_draw (menubar
);
286 menubar_finish (WMenuBar
*menubar
)
288 menubar
->is_dropped
= FALSE
;
289 menubar
->is_active
= FALSE
;
290 menubar
->widget
.lines
= 1;
291 widget_want_hotkey (menubar
->widget
, 0);
293 dlg_select_by_id (menubar
->widget
.parent
, menubar
->previous_widget
);
298 menubar_drop (WMenuBar
*menubar
, unsigned int selected
)
300 menubar
->is_dropped
= TRUE
;
301 menubar
->selected
= selected
;
302 menubar_draw (menubar
);
306 menubar_execute (WMenuBar
*menubar
)
308 const Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
309 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, menu
->selected
);
311 if ((entry
!= NULL
) && (entry
->command
!= 0)) {
312 is_right
= (menubar
->selected
!= 0);
313 menubar_finish (menubar
);
314 menubar
->widget
.parent
->menu_executor (entry
->command
);
320 menubar_down (WMenuBar
*menubar
)
322 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
323 const unsigned int len
= g_list_length (menu
->entries
);
326 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
329 menu
->selected
= (menu
->selected
+ 1) % len
;
330 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
331 } while ((entry
== NULL
) || (entry
->command
== 0));
333 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
337 menubar_up (WMenuBar
*menubar
)
339 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
340 const unsigned int len
= g_list_length (menu
->entries
);
343 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
346 if (menu
->selected
== 0)
347 menu
->selected
= len
- 1;
350 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
351 } while ((entry
== NULL
) || (entry
->command
== 0));
353 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
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
);
370 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
372 if ((entry
== NULL
) || (entry
->command
== 0))
378 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
382 menubar_last (WMenuBar
*menubar
)
384 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
385 const unsigned int len
= g_list_length (menu
->entries
);
388 if (menu
->selected
== len
- 1)
391 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
393 menu
->selected
= len
;
397 entry
= (menu_entry_t
*) g_list_nth_data (menu
->entries
, menu
->selected
);
398 } while ((entry
== NULL
) || (entry
->command
== 0));
400 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
404 menubar_handle_key (WMenuBar
*menubar
, int key
)
408 key
= g_ascii_tolower (key
);
410 if (is_abort_char (key
)) {
411 menubar_finish (menubar
);
415 /* menubar help or menubar navigation */
418 if (menubar
->is_dropped
)
419 interactive_display (NULL
,
420 ((Menu
*) g_list_nth_data (menubar
->menu
,
421 menubar
->selected
))->help_node
);
423 interactive_display (NULL
, "[Menu Bar]");
424 menubar_draw (menubar
);
429 menubar_left (menubar
);
434 menubar_right (menubar
);
438 if (!menubar
->is_dropped
) {
441 /* drop menu by hotkey */
442 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
)) {
443 Menu
*menu
= i
->data
;
445 if ((menu
->text
.hotkey
!= NULL
)
446 && (key
== g_ascii_tolower (menu
->text
.hotkey
[0]))) {
447 menubar_drop (menubar
, g_list_position (menubar
->menu
, i
));
452 /* drop menu by Enter or Dowwn key */
453 if (key
== KEY_ENTER
|| key
== XCTRL ('n')
454 || key
== KEY_DOWN
|| key
== '\n')
455 menubar_drop (menubar
, menubar
->selected
);
461 Menu
*menu
= g_list_nth_data (menubar
->menu
, menubar
->selected
);
464 /* execute menu command by hotkey */
465 for (i
= menu
->entries
; i
!= NULL
; i
= g_list_next (i
)) {
466 const menu_entry_t
*entry
= i
->data
;
468 if ((entry
!= NULL
) && (entry
->command
!= 0)
469 && (entry
->text
.hotkey
!= NULL
)
470 && (key
== g_ascii_tolower (entry
->text
.hotkey
[0]))) {
471 menu
->selected
= g_list_position (menu
->entries
, i
);
472 menubar_execute (menubar
);
477 /* menu execute by Enter or menu navigation */
481 menubar_execute (menubar
);
486 menubar_first (menubar
);
491 menubar_last (menubar
);
496 menubar_down (menubar
);
501 menubar_up (menubar
);
510 menubar_callback (Widget
*w
, widget_msg_t msg
, int parm
)
512 WMenuBar
*menubar
= (WMenuBar
*) w
;
515 /* We do not want the focus unless we have been activated */
517 if (!menubar
->is_active
)
518 return MSG_NOT_HANDLED
;
520 widget_want_cursor (menubar
->widget
, 1);
522 /* Trick to get all the mouse events */
523 menubar
->widget
.lines
= LINES
;
525 /* Trick to get all of the hotkeys */
526 widget_want_hotkey (menubar
->widget
, 1);
527 menubar_draw (menubar
);
530 /* We don't want the buttonbar to activate while using the menubar */
533 if (menubar
->is_active
) {
534 menubar_handle_key (menubar
, parm
);
537 return MSG_NOT_HANDLED
;
540 /* Put the cursor in a suitable place */
541 return MSG_NOT_HANDLED
;
544 if (menubar
->is_active
)
545 return MSG_NOT_HANDLED
;
547 widget_want_cursor (menubar
->widget
, 0);
551 if (menubar_visible
) {
552 menubar_draw (menubar
);
558 /* try show menu after screen resize */
559 send_message (w
, WIDGET_FOCUS
, 0);
564 menubar_set_menu (menubar
, NULL
);
568 return default_proc (msg
, parm
);
573 menubar_event (Gpm_Event
*event
, void *data
)
575 WMenuBar
*menubar
= data
;
576 gboolean was_active
= TRUE
;
577 int left_x
, right_x
, bottom_y
;
580 /* ignore unsupported events */
581 if ((event
->type
& (GPM_UP
| GPM_DOWN
| GPM_DRAG
)) == 0)
584 /* ignore wheel events if menu is inactive */
585 if (!menubar
->is_active
586 && ((event
->buttons
& (GPM_B_MIDDLE
| GPM_B_UP
| GPM_B_DOWN
)) != 0))
589 if (!menubar
->is_dropped
) {
590 menubar
->previous_widget
= menubar
->widget
.parent
->current
->dlg_id
;
591 menubar
->is_active
= TRUE
;
592 menubar
->is_dropped
= TRUE
;
596 /* Mouse operations on the menubar */
597 if (event
->y
== 1 || !was_active
) {
598 if ((event
->type
& GPM_UP
) != 0)
601 /* wheel events on menubar */
602 if (event
->buttons
& GPM_B_UP
)
603 menubar_left (menubar
);
604 else if (event
->buttons
& GPM_B_DOWN
)
605 menubar_right (menubar
);
607 const unsigned int len
= g_list_length (menubar
->menu
);
608 unsigned int new_selection
= 0;
610 while ((new_selection
< len
)
611 && (event
->x
> ((Menu
*) g_list_nth_data (menubar
->menu
,
612 new_selection
))->start_x
))
615 if (new_selection
!= 0) /* Don't set the invalid value -1 */
619 menubar
->selected
= new_selection
;
620 dlg_select_widget (menubar
);
622 menubar_remove (menubar
);
623 menubar
->selected
= new_selection
;
625 menubar_draw (menubar
);
630 if (!menubar
->is_dropped
|| (event
->y
< 2))
633 /* middle click -- everywhere */
634 if (((event
->buttons
& GPM_B_MIDDLE
) != 0)
635 && ((event
->type
& GPM_DOWN
) != 0)) {
636 menubar_execute (menubar
);
640 /* the mouse operation is on the menus or it is not */
641 menu
= (Menu
*) g_list_nth_data (menubar
->menu
, menubar
->selected
);
642 left_x
= menu
->start_x
;
643 right_x
= left_x
+ menu
->max_entry_len
+ 3;
644 if (right_x
> menubar
->widget
.cols
) {
645 left_x
= menubar
->widget
.cols
- menu
->max_entry_len
- 3;
646 right_x
= menubar
->widget
.cols
;
649 bottom_y
= g_list_length (menu
->entries
) + 3;
651 if ((event
->x
>= left_x
) && (event
->x
<= right_x
) && (event
->y
<= bottom_y
)){
652 int pos
= event
->y
- 3;
653 const menu_entry_t
*entry
= g_list_nth_data (menu
->entries
, pos
);
656 if ((event
->buttons
& GPM_B_UP
) && (event
->type
& GPM_DOWN
)) {
657 menubar_up (menubar
);
660 if ((event
->buttons
& GPM_B_DOWN
) && (event
->type
& GPM_DOWN
)) {
661 menubar_down (menubar
);
665 /* ignore events above and below dropped down menu */
666 if ((pos
< 0) || (pos
>= bottom_y
- 3))
669 if ((entry
!= NULL
) && (entry
->command
!= 0)) {
670 menubar_paint_idx (menubar
, menu
->selected
, MENU_ENTRY_COLOR
);
671 menu
->selected
= pos
;
672 menubar_paint_idx (menubar
, menu
->selected
, MENU_SELECTED_COLOR
);
674 if ((event
->type
& GPM_UP
) != 0)
675 menubar_execute (menubar
);
678 /* use click not wheel to close menu */
679 if (((event
->type
& GPM_DOWN
) != 0)
680 && ((event
->buttons
& (GPM_B_UP
| GPM_B_DOWN
)) == 0))
681 menubar_finish (menubar
);
687 menubar_new (int y
, int x
, int cols
, GList
*menu
)
689 WMenuBar
*menubar
= g_new0 (WMenuBar
, 1);
691 init_widget (&menubar
->widget
, y
, x
, 1, cols
,
692 menubar_callback
, menubar_event
);
693 widget_want_cursor (menubar
->widget
, 0);
694 menubar_set_menu (menubar
, menu
);
699 menubar_set_menu (WMenuBar
*menubar
, GList
*menu
)
701 /* delete previous menu */
702 if (menubar
->menu
!= NULL
) {
703 g_list_foreach (menubar
->menu
, (GFunc
) destroy_menu
, NULL
);
704 g_list_free (menubar
->menu
);
707 menubar
->is_active
= FALSE
;
708 menubar
->is_dropped
= FALSE
;
709 menubar
->menu
= menu
;
710 menubar
->selected
= 0;
711 menubar_arrange (menubar
);
715 menubar_add_menu (WMenuBar
*menubar
, Menu
*menu
)
718 menu_arrange (menu
, menubar
->widget
.parent
->get_shortcut
);
719 menubar
->menu
= g_list_append (menubar
->menu
, menu
);
722 menubar_arrange (menubar
);
726 * Properly space menubar items. Should be called when menubar is created
727 * and also when widget width is changed (i.e. upon xterm resize).
730 menubar_arrange (WMenuBar
* menubar
)
736 if (menubar
->menu
== NULL
)
739 #ifndef RESIZABLE_MENUBAR
742 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_next (i
)) {
743 Menu
*menu
= i
->data
;
744 int len
= hotkey_width (menu
->text
) + 2;
746 menu
->start_x
= start_x
;
747 start_x
+= len
+ gap
;
749 #else /* RESIZABLE_MENUBAR */
750 gap
= menubar
->widget
.cols
- 2;
752 /* First, calculate gap between items... */
753 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_nwxt (i
)) {
754 Menu
*menu
= i
->data
;
755 /* preserve length here, to be used below */
756 menu
->start_x
= hotkey_width (menu
->text
) + 2;
757 gap
-= menu
->start_x
;
760 gap
/= (menubar
->menu
->len
- 1);
763 /* We are out of luck - window is too narrow... */
767 /* ...and now fix start positions of menubar items */
768 for (i
= menubar
->menu
; i
!= NULL
; i
= g_list_nwxt (i
)) {
769 Menu
*menu
= i
->data
;
770 int len
= menu
->start_x
;
772 menu
->start_x
= start_x
;
773 start_x
+= len
+ gap
;
775 #endif /* RESIZABLE_MENUBAR */