m4.include/mc-use-termcap.m4: fixed const char* issue which triggers break on -Wall
[free-mc.git] / src / menu.c
blobbe48166801e55ac55770bc6a8552f7ae96f78073
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 "global.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 */
36 #include "menu.h"
37 #include "help.h"
38 #include "dialog.h"
39 #include "widget.h"
40 #include "main.h" /* is_right */
41 #include "strutil.h"
43 int menubar_visible = 1; /* This is the new default */
45 menu_entry_t *
46 menu_entry_create (const char *name, int command)
48 menu_entry_t *entry;
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;
56 return entry;
59 void
60 menu_entry_free (menu_entry_t *entry)
62 if (entry != NULL) {
63 release_hotkey (entry->text);
64 g_free (entry->shortcut);
65 g_free (entry);
69 static void
70 menu_arrange (Menu *menu, dlg_shortcut_str get_shortcut)
72 if (menu != NULL) {
73 GList *i;
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;
82 if (entry != NULL) {
83 size_t len;
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;
102 Menu *
103 create_menu (const char *name, GList *entries, const char *help_node)
105 Menu *menu;
107 menu = g_new (Menu, 1);
108 menu->start_x = 0;
109 menu->text = parse_hotkey (name);
110 menu->entries = entries;
111 menu->max_entry_len = 1;
112 menu->max_hotkey_len = 0;
113 menu->selected = 0;
114 menu->help_node = g_strdup (help_node);
116 return menu;
119 void
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);
126 g_free (menu);
129 static void
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;
140 if (entry == NULL) {
141 /* menu separator */
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);
152 } else {
153 /* menu text */
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);
180 static void
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;
186 unsigned int i;
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);
205 static void
206 menubar_set_color (WMenuBar *menubar, gboolean current, gboolean hotkey)
208 if (!menubar->is_active)
209 tty_setcolor (hotkey ? COLOR_HOT_FOCUS : SELECTED_COLOR);
210 else if (current)
211 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
212 else
213 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
216 static void
217 menubar_draw (WMenuBar *menubar)
219 GList *i;
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);
250 else
251 widget_move (&menubar->widget, 0,
252 ((Menu *) g_list_nth_data (menubar->menu,
253 menubar->selected))->start_x);
256 static void
257 menubar_remove (WMenuBar *menubar)
259 if (menubar->is_dropped) {
260 menubar->is_dropped = FALSE;
261 do_refresh ();
262 menubar->is_dropped = TRUE;
266 static void
267 menubar_left (WMenuBar *menubar)
269 menubar_remove (menubar);
270 if (menubar->selected == 0)
271 menubar->selected = g_list_length (menubar->menu) - 1;
272 else
273 menubar->selected--;
274 menubar_draw (menubar);
277 static void
278 menubar_right (WMenuBar *menubar)
280 menubar_remove (menubar);
281 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
282 menubar_draw (menubar);
285 static void
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);
294 do_refresh ();
297 static void
298 menubar_drop (WMenuBar *menubar, unsigned int selected)
300 menubar->is_dropped = TRUE;
301 menubar->selected = selected;
302 menubar_draw (menubar);
305 static void
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);
315 do_refresh ();
319 static void
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);
324 menu_entry_t *entry;
326 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
328 do {
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);
336 static void
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);
341 menu_entry_t *entry;
343 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
345 do {
346 if (menu->selected == 0)
347 menu->selected = len - 1;
348 else
349 menu->selected--;
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);
356 static void
357 menubar_first (WMenuBar *menubar)
359 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
360 menu_entry_t *entry;
362 if (menu->selected == 0)
363 return;
365 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
367 menu->selected = 0;
369 while (TRUE) {
370 entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
372 if ((entry == NULL) || (entry->command == 0))
373 menu->selected++;
374 else
375 break;
378 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
381 static void
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);
386 menu_entry_t *entry;
388 if (menu->selected == len - 1)
389 return;
391 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
393 menu->selected = len;
395 do {
396 menu->selected--;
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);
403 static int
404 menubar_handle_key (WMenuBar *menubar, int key)
406 /* Lowercase */
407 if (isascii (key))
408 key = g_ascii_tolower (key);
410 if (is_abort_char (key)) {
411 menubar_finish (menubar);
412 return 1;
415 /* menubar help or menubar navigation */
416 switch (key) {
417 case KEY_F(1):
418 if (menubar->is_dropped)
419 interactive_display (NULL,
420 ((Menu *) g_list_nth_data (menubar->menu,
421 menubar->selected))->help_node);
422 else
423 interactive_display (NULL, "[Menu Bar]");
424 menubar_draw (menubar);
425 return 1;
427 case KEY_LEFT:
428 case XCTRL('b'):
429 menubar_left (menubar);
430 return 1;
432 case KEY_RIGHT:
433 case XCTRL ('f'):
434 menubar_right (menubar);
435 return 1;
438 if (!menubar->is_dropped) {
439 GList *i;
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));
448 return 1;
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);
457 return 1;
461 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
462 GList *i;
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);
473 return 1;
477 /* menu execute by Enter or menu navigation */
478 switch (key) {
479 case KEY_ENTER:
480 case '\n':
481 menubar_execute (menubar);
482 return 1;
484 case KEY_HOME:
485 case ALT ('<'):
486 menubar_first (menubar);
487 break;
489 case KEY_END:
490 case ALT ('>'):
491 menubar_last (menubar);
492 break;
494 case KEY_DOWN:
495 case XCTRL ('n'):
496 menubar_down (menubar);
497 break;
499 case KEY_UP:
500 case XCTRL ('p'):
501 menubar_up (menubar);
502 break;
506 return 0;
509 static cb_ret_t
510 menubar_callback (Widget *w, widget_msg_t msg, int parm)
512 WMenuBar *menubar = (WMenuBar *) w;
514 switch (msg) {
515 /* We do not want the focus unless we have been activated */
516 case WIDGET_FOCUS:
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);
528 return MSG_HANDLED;
530 /* We don't want the buttonbar to activate while using the menubar */
531 case WIDGET_HOTKEY:
532 case WIDGET_KEY:
533 if (menubar->is_active) {
534 menubar_handle_key (menubar, parm);
535 return MSG_HANDLED;
537 return MSG_NOT_HANDLED;
539 case WIDGET_CURSOR:
540 /* Put the cursor in a suitable place */
541 return MSG_NOT_HANDLED;
543 case WIDGET_UNFOCUS:
544 if (menubar->is_active)
545 return MSG_NOT_HANDLED;
547 widget_want_cursor (menubar->widget, 0);
548 return MSG_HANDLED;
550 case WIDGET_DRAW:
551 if (menubar_visible) {
552 menubar_draw (menubar);
553 return MSG_HANDLED;
555 /* fall through */
557 case WIDGET_RESIZED:
558 /* try show menu after screen resize */
559 send_message (w, WIDGET_FOCUS, 0);
560 return MSG_HANDLED;
563 case WIDGET_DESTROY:
564 menubar_set_menu (menubar, NULL);
565 return MSG_HANDLED;
567 default:
568 return default_proc (msg, parm);
572 static int
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;
578 Menu *menu;
580 /* ignore unsupported events */
581 if ((event->type & (GPM_UP | GPM_DOWN | GPM_DRAG)) == 0)
582 return MOU_NORMAL;
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))
587 return MOU_NORMAL;
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;
593 was_active = FALSE;
596 /* Mouse operations on the menubar */
597 if (event->y == 1 || !was_active) {
598 if ((event->type & GPM_UP) != 0)
599 return MOU_NORMAL;
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);
606 else {
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))
613 new_selection++;
615 if (new_selection != 0) /* Don't set the invalid value -1 */
616 new_selection--;
618 if (!was_active) {
619 menubar->selected = new_selection;
620 dlg_select_widget (menubar);
621 } else {
622 menubar_remove (menubar);
623 menubar->selected = new_selection;
625 menubar_draw (menubar);
627 return MOU_NORMAL;
630 if (!menubar->is_dropped || (event->y < 2))
631 return MOU_NORMAL;
633 /* middle click -- everywhere */
634 if (((event->buttons & GPM_B_MIDDLE) != 0)
635 && ((event->type & GPM_DOWN) != 0)) {
636 menubar_execute (menubar);
637 return MOU_NORMAL;
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);
655 /* mouse wheel */
656 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN)) {
657 menubar_up (menubar);
658 return MOU_NORMAL;
660 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN)) {
661 menubar_down (menubar);
662 return MOU_NORMAL;
665 /* ignore events above and below dropped down menu */
666 if ((pos < 0) || (pos >= bottom_y - 3))
667 return MOU_NORMAL;
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);
677 } else
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);
683 return MOU_NORMAL;
686 WMenuBar *
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);
695 return menubar;
698 void
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);
706 /* add new menu */
707 menubar->is_active = FALSE;
708 menubar->is_dropped = FALSE;
709 menubar->menu = menu;
710 menubar->selected = 0;
711 menubar_arrange (menubar);
714 void
715 menubar_add_menu (WMenuBar *menubar, Menu *menu)
717 if (menu != NULL) {
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).
729 void
730 menubar_arrange (WMenuBar* menubar)
732 int start_x = 1;
733 GList *i;
734 int gap;
736 if (menubar->menu == NULL)
737 return;
739 #ifndef RESIZABLE_MENUBAR
740 gap = 3;
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);
762 if (gap <= 0) {
763 /* We are out of luck - window is too narrow... */
764 gap = 1;
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 */