Merge branch '2161_del_clear_dialog_field'
[midnight-commander.git] / src / menu.c
blob4021200f20b07a9d5593225d426a59aca09f4ae7
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. */
18 /** \file menu.c
19 * \brief Source: pulldown menu code
22 #include <config.h>
24 #include <ctype.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <sys/types.h>
29 #include "lib/global.h"
31 #include "lib/tty/tty.h"
32 #include "lib/skin.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 */
38 #include "help.h"
39 #include "dialog.h"
40 #include "widget.h"
41 #include "main.h" /* is_right */
42 #include "menu.h"
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);
48 menu_entry_t *
49 menu_entry_create (const char *name, unsigned long command)
51 menu_entry_t *entry;
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;
59 return entry;
62 void
63 menu_entry_free (menu_entry_t * entry)
65 if (entry != NULL)
67 release_hotkey (entry->text);
68 g_free (entry->shortcut);
69 g_free (entry);
73 static void
74 menu_arrange (Menu * menu, dlg_shortcut_str get_shortcut)
76 if (menu != NULL)
78 GList *i;
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;
88 if (entry != NULL)
90 size_t len;
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;
110 Menu *
111 create_menu (const char *name, GList * entries, const char *help_node)
113 Menu *menu;
115 menu = g_new (Menu, 1);
116 menu->start_x = 0;
117 menu->text = parse_hotkey (name);
118 menu->entries = entries;
119 menu->max_entry_len = 1;
120 menu->max_hotkey_len = 0;
121 menu->selected = 0;
122 menu->help_node = g_strdup (help_node);
124 return menu;
127 void
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);
134 g_free (menu);
137 static void
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;
148 if (entry == NULL)
150 /* menu separator */
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);
162 else
164 /* menu text */
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);
192 static void
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;
198 unsigned int i;
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);
213 static void
214 menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
216 if (!menubar->is_active)
217 tty_setcolor (MENU_INACTIVE_COLOR);
218 else if (current)
219 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
220 else
221 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
224 static void
225 menubar_draw (WMenuBar * menubar)
227 GList *i;
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);
260 else
261 widget_move (&menubar->widget, 0,
262 ((Menu *) g_list_nth_data (menubar->menu, menubar->selected))->start_x);
265 static void
266 menubar_remove (WMenuBar * menubar)
268 if (menubar->is_dropped)
270 menubar->is_dropped = FALSE;
271 do_refresh ();
272 menubar->is_dropped = TRUE;
276 static void
277 menubar_left (WMenuBar * menubar)
279 menubar_remove (menubar);
280 if (menubar->selected == 0)
281 menubar->selected = g_list_length (menubar->menu) - 1;
282 else
283 menubar->selected--;
284 menubar_draw (menubar);
287 static void
288 menubar_right (WMenuBar * menubar)
290 menubar_remove (menubar);
291 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
292 menubar_draw (menubar);
295 static void
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);
304 do_refresh ();
307 static void
308 menubar_drop (WMenuBar * menubar, unsigned int selected)
310 menubar->is_dropped = TRUE;
311 menubar->selected = selected;
312 menubar_draw (menubar);
315 static void
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);
327 do_refresh ();
331 static void
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);
336 menu_entry_t *entry;
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);
350 static void
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);
355 menu_entry_t *entry;
357 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
361 if (menu->selected == 0)
362 menu->selected = len - 1;
363 else
364 menu->selected--;
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);
372 static void
373 menubar_first (WMenuBar * menubar)
375 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
376 menu_entry_t *entry;
378 if (menu->selected == 0)
379 return;
381 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
383 menu->selected = 0;
385 while (TRUE)
387 entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
389 if ((entry == NULL) || (entry->command == CK_Ignore_Key))
390 menu->selected++;
391 else
392 break;
395 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
398 static void
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);
403 menu_entry_t *entry;
405 if (menu->selected == len - 1)
406 return;
408 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
410 menu->selected = len;
414 menu->selected--;
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);
422 static int
423 menubar_handle_key (WMenuBar * menubar, int key)
425 /* Lowercase */
426 if (isascii (key))
427 key = g_ascii_tolower (key);
429 if (is_abort_char (key))
431 menubar_finish (menubar);
432 return 1;
435 /* menubar help or menubar navigation */
436 switch (key)
438 case KEY_F (1):
439 if (menubar->is_dropped)
440 interactive_display (NULL,
441 ((Menu *) g_list_nth_data (menubar->menu,
442 menubar->selected))->help_node);
443 else
444 interactive_display (NULL, "[Menu Bar]");
445 menubar_draw (menubar);
446 return 1;
448 case KEY_LEFT:
449 case XCTRL ('b'):
450 menubar_left (menubar);
451 return 1;
453 case KEY_RIGHT:
454 case XCTRL ('f'):
455 menubar_right (menubar);
456 return 1;
459 if (!menubar->is_dropped)
461 GList *i;
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));
471 return 1;
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);
479 return 1;
483 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
484 GList *i;
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);
496 return 1;
500 /* menu execute by Enter or menu navigation */
501 switch (key)
503 case KEY_ENTER:
504 case '\n':
505 menubar_execute (menubar);
506 return 1;
508 case KEY_HOME:
509 case ALT ('<'):
510 menubar_first (menubar);
511 break;
513 case KEY_END:
514 case ALT ('>'):
515 menubar_last (menubar);
516 break;
518 case KEY_DOWN:
519 case XCTRL ('n'):
520 menubar_down (menubar);
521 break;
523 case KEY_UP:
524 case XCTRL ('p'):
525 menubar_up (menubar);
526 break;
530 return 0;
533 static cb_ret_t
534 menubar_callback (Widget * w, widget_msg_t msg, int parm)
536 WMenuBar *menubar = (WMenuBar *) w;
538 switch (msg)
540 /* We do not want the focus unless we have been activated */
541 case WIDGET_FOCUS:
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);
551 return MSG_HANDLED;
553 /* We don't want the buttonbar to activate while using the menubar */
554 case WIDGET_HOTKEY:
555 case WIDGET_KEY:
556 if (menubar->is_active)
558 menubar_handle_key (menubar, parm);
559 return MSG_HANDLED;
561 return MSG_NOT_HANDLED;
563 case WIDGET_CURSOR:
564 /* Put the cursor in a suitable place */
565 return MSG_NOT_HANDLED;
567 case WIDGET_UNFOCUS:
568 return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
570 case WIDGET_DRAW:
571 if (menubar_visible)
573 menubar_draw (menubar);
574 return MSG_HANDLED;
576 /* fall through */
578 case WIDGET_RESIZED:
579 /* try show menu after screen resize */
580 send_message (w, WIDGET_FOCUS, 0);
581 return MSG_HANDLED;
584 case WIDGET_DESTROY:
585 menubar_set_menu (menubar, NULL);
586 return MSG_HANDLED;
588 default:
589 return default_proc (msg, parm);
593 static int
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;
599 Menu *menu;
601 /* ignore unsupported events */
602 if ((event->type & (GPM_UP | GPM_DOWN | GPM_DRAG)) == 0)
603 return MOU_NORMAL;
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))
607 return MOU_NORMAL;
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;
614 was_active = FALSE;
617 /* Mouse operations on the menubar */
618 if (event->y == 1 || !was_active)
620 if ((event->type & GPM_UP) != 0)
621 return MOU_NORMAL;
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);
628 else
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))
636 new_selection++;
638 if (new_selection != 0) /* Don't set the invalid value -1 */
639 new_selection--;
641 if (!was_active)
643 menubar->selected = new_selection;
644 dlg_select_widget (menubar);
646 else
648 menubar_remove (menubar);
649 menubar->selected = new_selection;
651 menubar_draw (menubar);
653 return MOU_NORMAL;
656 if (!menubar->is_dropped || (event->y < 2))
657 return MOU_NORMAL;
659 /* middle click -- everywhere */
660 if (((event->buttons & GPM_B_MIDDLE) != 0) && ((event->type & GPM_DOWN) != 0))
662 menubar_execute (menubar);
663 return MOU_NORMAL;
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);
683 /* mouse wheel */
684 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
686 menubar_up (menubar);
687 return MOU_NORMAL;
689 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
691 menubar_down (menubar);
692 return MOU_NORMAL;
695 /* ignore events above and below dropped down menu */
696 if ((pos < 0) || (pos >= bottom_y - 3))
697 return MOU_NORMAL;
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);
709 else
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);
714 return MOU_NORMAL;
717 WMenuBar *
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);
725 return menubar;
728 void
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);
737 /* add new menu */
738 menubar->is_active = FALSE;
739 menubar->is_dropped = FALSE;
740 menubar->menu = menu;
741 menubar->selected = 0;
742 menubar_arrange (menubar);
745 void
746 menubar_add_menu (WMenuBar * menubar, Menu * menu)
748 if (menu != NULL)
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).
761 void
762 menubar_arrange (WMenuBar * menubar)
764 int start_x = 1;
765 GList *i;
766 int gap;
768 if (menubar->menu == NULL)
769 return;
771 #ifndef RESIZABLE_MENUBAR
772 gap = 3;
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);
796 if (gap <= 0)
798 /* We are out of luck - window is too narrow... */
799 gap = 1;
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 */
815 WMenuBar *
816 find_menubar (const Dlg_head * h)
818 return (WMenuBar *) find_widget_type (h, menubar_callback);