(dlg_set_bottom_widget): rename to widget_set_bottom.
[midnight-commander.git] / lib / widget / menu.c
blobc8d9bb8aeb7da5f4bd034c8f71a7029dc6872ecd
1 /*
2 Pulldown menu code
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2012, 2013, 2016
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** \file menu.c
27 * \brief Source: pulldown menu code
30 #include <config.h>
32 #include <ctype.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <sys/types.h>
37 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/skin.h"
41 #include "lib/tty/key.h" /* key macros */
42 #include "lib/strutil.h"
43 #include "lib/widget.h"
44 #include "lib/event.h" /* mc_event_raise() */
46 /*** global variables ****************************************************************************/
48 /*** file scope macro definitions ****************************************************************/
50 #define MENUENTRY(x) ((menu_entry_t *)(x))
51 #define MENU(x) ((menu_t *)(x))
53 /*** file scope type declarations ****************************************************************/
55 struct menu_entry_t
57 unsigned char first_letter;
58 hotkey_t text;
59 long command;
60 char *shortcut;
63 struct menu_t
65 int start_x; /* position relative to menubar start */
66 hotkey_t text;
67 GList *entries;
68 size_t max_entry_len; /* cached max length of entry texts (text + shortcut) */
69 size_t max_hotkey_len; /* cached max length of shortcuts */
70 unsigned int selected; /* pointer to current menu entry */
71 char *help_node;
74 /*** file scope variables ************************************************************************/
76 /*** file scope functions ************************************************************************/
77 /* --------------------------------------------------------------------------------------------- */
79 static void
80 menu_arrange (menu_t * menu, dlg_shortcut_str get_shortcut)
82 if (menu != NULL)
84 GList *i;
85 size_t max_shortcut_len = 0;
87 menu->max_entry_len = 1;
88 menu->max_hotkey_len = 1;
90 for (i = menu->entries; i != NULL; i = g_list_next (i))
92 menu_entry_t *entry = MENUENTRY (i->data);
94 if (entry != NULL)
96 size_t len;
98 len = (size_t) hotkey_width (entry->text);
99 menu->max_hotkey_len = MAX (menu->max_hotkey_len, len);
101 if (get_shortcut != NULL)
102 entry->shortcut = get_shortcut (entry->command);
104 if (entry->shortcut != NULL)
106 len = (size_t) str_term_width1 (entry->shortcut);
107 max_shortcut_len = MAX (max_shortcut_len, len);
112 menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
116 /* --------------------------------------------------------------------------------------------- */
118 static void
119 menubar_paint_idx (WMenuBar * menubar, unsigned int idx, int color)
121 Widget *w = WIDGET (menubar);
122 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
123 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, idx));
124 const int y = 2 + idx;
125 int x = menu->start_x;
127 if (x + menu->max_entry_len + 4 > (gsize) w->cols)
128 x = w->cols - menu->max_entry_len - 4;
130 if (entry == NULL)
132 /* menu separator */
133 tty_setcolor (MENU_ENTRY_COLOR);
135 widget_move (w, y, x - 1);
136 tty_print_alt_char (ACS_LTEE, FALSE);
137 tty_draw_hline (w->y + y, w->x + x, ACS_HLINE, menu->max_entry_len + 3);
138 widget_move (w, y, x + menu->max_entry_len + 3);
139 tty_print_alt_char (ACS_RTEE, FALSE);
141 else
143 int yt, xt;
145 /* menu text */
146 tty_setcolor (color);
147 widget_move (w, y, x);
148 tty_print_char ((unsigned char) entry->first_letter);
149 tty_getyx (&yt, &xt);
150 tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2); /* clear line */
151 tty_print_string (entry->text.start);
153 if (entry->text.hotkey != NULL)
155 tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
156 tty_print_string (entry->text.hotkey);
157 tty_setcolor (color);
160 if (entry->text.end != NULL)
161 tty_print_string (entry->text.end);
163 if (entry->shortcut != NULL)
165 widget_move (w, y, x + menu->max_hotkey_len + 3);
166 tty_print_string (entry->shortcut);
169 /* move cursor to the start of entry text */
170 widget_move (w, y, x + 1);
174 /* --------------------------------------------------------------------------------------------- */
176 static void
177 menubar_draw_drop (WMenuBar * menubar)
179 Widget *w = WIDGET (menubar);
180 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
181 const unsigned int count = g_list_length (menu->entries);
182 int column = menu->start_x - 1;
183 unsigned int i;
185 if (column + menu->max_entry_len + 5 > (gsize) w->cols)
186 column = w->cols - menu->max_entry_len - 5;
188 tty_setcolor (MENU_ENTRY_COLOR);
189 tty_draw_box (w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
191 for (i = 0; i < count; i++)
192 menubar_paint_idx (menubar, i,
193 i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
196 /* --------------------------------------------------------------------------------------------- */
198 static void
199 menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
201 if (!widget_get_state (WIDGET (menubar), WST_FOCUSED))
202 tty_setcolor (MENU_INACTIVE_COLOR);
203 else if (current)
204 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
205 else
206 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
209 /* --------------------------------------------------------------------------------------------- */
211 static void
212 menubar_draw (WMenuBar * menubar)
214 Widget *w = WIDGET (menubar);
215 GList *i;
217 /* First draw the complete menubar */
218 tty_setcolor (widget_get_state (w, WST_FOCUSED) ? MENU_ENTRY_COLOR : MENU_INACTIVE_COLOR);
219 tty_draw_hline (w->y, w->x, ' ', w->cols);
221 /* Now each one of the entries */
222 for (i = menubar->menu; i != NULL; i = g_list_next (i))
224 menu_t *menu = MENU (i->data);
225 gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
227 menubar_set_color (menubar, is_selected, FALSE);
228 widget_move (w, 0, menu->start_x);
230 tty_print_char (' ');
231 tty_print_string (menu->text.start);
233 if (menu->text.hotkey != NULL)
235 menubar_set_color (menubar, is_selected, TRUE);
236 tty_print_string (menu->text.hotkey);
237 menubar_set_color (menubar, is_selected, FALSE);
240 if (menu->text.end != NULL)
241 tty_print_string (menu->text.end);
243 tty_print_char (' ');
246 if (menubar->is_dropped)
247 menubar_draw_drop (menubar);
248 else
249 widget_move (w, 0, MENU (g_list_nth_data (menubar->menu, menubar->selected))->start_x);
252 /* --------------------------------------------------------------------------------------------- */
254 static void
255 menubar_remove (WMenuBar * menubar)
257 WDialog *h;
259 if (!menubar->is_dropped)
260 return;
262 /* HACK: before refresh the dialog, change the current widget to keep the order
263 of overlapped widgets. This is useful in multi-window editor.
264 In general, menubar should be a special object, not an ordinary widget
265 in the current dialog. */
266 h = WIDGET (menubar)->owner;
267 h->current = g_list_find (h->widgets, dlg_find_by_id (h, menubar->previous_widget));
269 menubar->is_dropped = FALSE;
270 do_refresh ();
271 menubar->is_dropped = TRUE;
273 /* restore current widget */
274 h->current = g_list_find (h->widgets, menubar);
277 /* --------------------------------------------------------------------------------------------- */
279 static void
280 menubar_left (WMenuBar * menubar)
282 menubar_remove (menubar);
283 if (menubar->selected == 0)
284 menubar->selected = g_list_length (menubar->menu) - 1;
285 else
286 menubar->selected--;
287 menubar_draw (menubar);
290 /* --------------------------------------------------------------------------------------------- */
292 static void
293 menubar_right (WMenuBar * menubar)
295 menubar_remove (menubar);
296 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
297 menubar_draw (menubar);
300 /* --------------------------------------------------------------------------------------------- */
302 static void
303 menubar_finish (WMenuBar * menubar)
305 Widget *w = WIDGET (menubar);
307 widget_set_state (w, WST_FOCUSED, FALSE);
308 menubar->is_dropped = FALSE;
309 w->lines = 1;
310 widget_want_hotkey (w, FALSE);
311 widget_set_options (w, WOP_SELECTABLE, FALSE);
313 /* Move the menubar to the bottom so that widgets displayed on top of
314 * an "invisible" menubar get the first chance to respond to mouse events. */
315 widget_set_bottom (w);
317 dlg_select_by_id (w->owner, menubar->previous_widget);
318 do_refresh ();
321 /* --------------------------------------------------------------------------------------------- */
323 static void
324 menubar_drop (WMenuBar * menubar, unsigned int selected)
326 menubar->is_dropped = TRUE;
327 menubar->selected = selected;
328 menubar_draw (menubar);
331 /* --------------------------------------------------------------------------------------------- */
333 static void
334 menubar_execute (WMenuBar * menubar)
336 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
337 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
339 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
341 Widget *w = WIDGET (menubar);
343 mc_global.widget.is_right = (menubar->selected != 0);
344 menubar_finish (menubar);
345 send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
346 do_refresh ();
350 /* --------------------------------------------------------------------------------------------- */
352 static void
353 menubar_down (WMenuBar * menubar)
355 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
356 const unsigned int len = g_list_length (menu->entries);
357 menu_entry_t *entry;
359 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
363 menu->selected = (menu->selected + 1) % len;
364 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
366 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
368 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
371 /* --------------------------------------------------------------------------------------------- */
373 static void
374 menubar_up (WMenuBar * menubar)
376 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
377 const unsigned int len = g_list_length (menu->entries);
378 menu_entry_t *entry;
380 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
384 if (menu->selected == 0)
385 menu->selected = len - 1;
386 else
387 menu->selected--;
388 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
390 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
392 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
395 /* --------------------------------------------------------------------------------------------- */
397 static void
398 menubar_first (WMenuBar * menubar)
400 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
402 if (menu->selected == 0)
403 return;
405 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
407 menu->selected = 0;
409 while (TRUE)
411 menu_entry_t *entry;
413 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
415 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
416 menu->selected++;
417 else
418 break;
421 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
424 /* --------------------------------------------------------------------------------------------- */
426 static void
427 menubar_last (WMenuBar * menubar)
429 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
430 const unsigned int len = g_list_length (menu->entries);
431 menu_entry_t *entry;
433 if (menu->selected == len - 1)
434 return;
436 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
438 menu->selected = len;
442 menu->selected--;
443 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
445 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
447 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
450 /* --------------------------------------------------------------------------------------------- */
452 static int
453 menubar_handle_key (WMenuBar * menubar, int key)
455 /* Lowercase */
456 if (isascii (key))
457 key = g_ascii_tolower (key);
459 if (is_abort_char (key))
461 menubar_finish (menubar);
462 return 1;
465 /* menubar help or menubar navigation */
466 switch (key)
468 case KEY_F (1):
470 ev_help_t event_data = { NULL, NULL };
472 if (menubar->is_dropped)
473 event_data.node =
474 MENU (g_list_nth_data (menubar->menu, menubar->selected))->help_node;
475 else
476 event_data.node = "[Menu Bar]";
478 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
479 menubar_draw (menubar);
480 return 1;
483 case KEY_LEFT:
484 case XCTRL ('b'):
485 menubar_left (menubar);
486 return 1;
488 case KEY_RIGHT:
489 case XCTRL ('f'):
490 menubar_right (menubar);
491 return 1;
493 default:
494 break;
497 if (!menubar->is_dropped)
499 GList *i;
501 /* drop menu by hotkey */
502 for (i = menubar->menu; i != NULL; i = g_list_next (i))
504 menu_t *menu = MENU (i->data);
506 if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
508 menubar_drop (menubar, g_list_position (menubar->menu, i));
509 return 1;
513 /* drop menu by Enter or Dowwn key */
514 if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
515 menubar_drop (menubar, menubar->selected);
517 return 1;
521 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
522 GList *i;
524 /* execute menu command by hotkey */
525 for (i = menu->entries; i != NULL; i = g_list_next (i))
527 const menu_entry_t *entry = MENUENTRY (i->data);
529 if ((entry != NULL) && (entry->command != CK_IgnoreKey)
530 && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
532 menu->selected = g_list_position (menu->entries, i);
533 menubar_execute (menubar);
534 return 1;
538 /* menu execute by Enter or menu navigation */
539 switch (key)
541 case KEY_ENTER:
542 case '\n':
543 menubar_execute (menubar);
544 return 1;
546 case KEY_HOME:
547 case ALT ('<'):
548 menubar_first (menubar);
549 break;
551 case KEY_END:
552 case ALT ('>'):
553 menubar_last (menubar);
554 break;
556 case KEY_DOWN:
557 case XCTRL ('n'):
558 menubar_down (menubar);
559 break;
561 case KEY_UP:
562 case XCTRL ('p'):
563 menubar_up (menubar);
564 break;
566 default:
567 break;
571 return 0;
574 /* --------------------------------------------------------------------------------------------- */
576 static gboolean
577 menubar_refresh (WMenuBar * menubar)
579 Widget *w = WIDGET (menubar);
581 if (!widget_get_state (w, WST_FOCUSED))
582 return FALSE;
584 /* Trick to get all the mouse events */
585 w->lines = LINES;
587 /* Trick to get all of the hotkeys */
588 widget_want_hotkey (w, TRUE);
589 return TRUE;
592 /* --------------------------------------------------------------------------------------------- */
594 static void
595 menubar_free_menu (WMenuBar * menubar)
597 if (menubar->menu != NULL)
598 g_list_free_full (menubar->menu, (GDestroyNotify) destroy_menu);
601 /* --------------------------------------------------------------------------------------------- */
603 static cb_ret_t
604 menubar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
606 WMenuBar *menubar = MENUBAR (w);
608 switch (msg)
610 /* We do not want the focus unless we have been activated */
611 case MSG_FOCUS:
612 if (menubar_refresh (menubar))
614 menubar_draw (menubar);
615 return MSG_HANDLED;
617 return MSG_NOT_HANDLED;
619 case MSG_UNFOCUS:
620 return widget_get_state (w, WST_FOCUSED) ? MSG_NOT_HANDLED : MSG_HANDLED;
622 /* We don't want the buttonbar to activate while using the menubar */
623 case MSG_HOTKEY:
624 case MSG_KEY:
625 if (widget_get_state (w, WST_FOCUSED))
627 menubar_handle_key (menubar, parm);
628 return MSG_HANDLED;
630 return MSG_NOT_HANDLED;
632 case MSG_CURSOR:
633 /* Put the cursor in a suitable place */
634 return MSG_NOT_HANDLED;
636 case MSG_DRAW:
637 if (menubar->is_visible || menubar_refresh (menubar))
638 menubar_draw (menubar);
639 return MSG_HANDLED;
641 case MSG_RESIZE:
642 /* try show menu after screen resize */
643 menubar_refresh (menubar);
644 return MSG_HANDLED;
646 case MSG_DESTROY:
647 menubar_free_menu (menubar);
648 return MSG_HANDLED;
650 default:
651 return widget_default_callback (w, sender, msg, parm, data);
655 /* --------------------------------------------------------------------------------------------- */
657 static unsigned int
658 menubar_get_menu_by_x_coord (const WMenuBar * menubar, int x)
660 unsigned int i;
661 GList *menu;
663 for (i = 0, menu = menubar->menu;
664 menu != NULL && x > MENU (menu->data)->start_x; i++, menu = g_list_next (menu))
667 /* Don't set the invalid value -1 */
668 if (i != 0)
669 i--;
671 return i;
674 /* --------------------------------------------------------------------------------------------- */
676 static gboolean
677 menubar_mouse_on_menu (const WMenuBar * menubar, int y, int x)
679 Widget *w = WIDGET (menubar);
680 menu_t *menu;
681 int left_x, right_x, bottom_y;
683 if (!menubar->is_dropped)
684 return FALSE;
686 menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
687 left_x = menu->start_x;
688 right_x = left_x + menu->max_entry_len + 3;
689 if (right_x > w->cols)
691 left_x = w->cols - (menu->max_entry_len + 3);
692 right_x = w->cols;
695 bottom_y = g_list_length (menu->entries) + 2; /* skip bar and top frame */
697 return (x >= left_x && x < right_x && y > 1 && y < bottom_y);
700 /* --------------------------------------------------------------------------------------------- */
702 static void
703 menubar_change_selected_item (WMenuBar * menubar, int y)
705 menu_t *menu;
706 menu_entry_t *entry;
708 y -= 2; /* skip bar and top frame */
709 menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
710 entry = MENUENTRY (g_list_nth_data (menu->entries, y));
712 if (entry != NULL && entry->command != CK_IgnoreKey)
714 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
715 menu->selected = y;
716 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
720 /* --------------------------------------------------------------------------------------------- */
722 static void
723 menubar_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
725 static gboolean was_drag = FALSE;
727 WMenuBar *menubar = MENUBAR (w);
728 gboolean mouse_on_drop;
730 mouse_on_drop = menubar_mouse_on_menu (menubar, event->y, event->x);
732 switch (msg)
734 case MSG_MOUSE_DOWN:
735 was_drag = FALSE;
737 if (event->y == 0)
739 /* events on menubar */
740 unsigned int selected;
742 selected = menubar_get_menu_by_x_coord (menubar, event->x);
743 menubar_activate (menubar, TRUE, selected);
744 menubar_remove (menubar); /* if already shown */
745 menubar_drop (menubar, selected);
747 else if (mouse_on_drop)
748 menubar_change_selected_item (menubar, event->y);
749 else
751 /* mouse click outside menubar or dropdown -- close menu */
752 menubar_finish (menubar);
755 * @FIXME.
757 * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
758 * events belonging to this click (in case the user drags the mouse,
759 * of course).
761 * For the time being, we mark this with FIXME as this flag should
762 * preferably be regarded as "implementation detail" and not be
763 * touched by us. We should think of some other way of communicating
764 * this to the system.
766 w->mouse.capture = FALSE;
768 break;
770 case MSG_MOUSE_UP:
771 if (was_drag && mouse_on_drop)
772 menubar_execute (menubar);
773 was_drag = FALSE;
774 break;
776 case MSG_MOUSE_CLICK:
777 was_drag = FALSE;
779 if ((event->buttons & GPM_B_MIDDLE) != 0 && event->y > 0 && menubar->is_dropped)
781 /* middle click -- everywhere */
782 menubar_execute (menubar);
784 else if (mouse_on_drop)
785 menubar_execute (menubar);
786 else if (event->y > 0)
787 /* releasing the mouse button outside the menu -- close menu */
788 menubar_finish (menubar);
789 break;
791 case MSG_MOUSE_DRAG:
792 if (event->y == 0)
794 menubar_remove (menubar);
795 menubar_drop (menubar, menubar_get_menu_by_x_coord (menubar, event->x));
797 else if (mouse_on_drop)
798 menubar_change_selected_item (menubar, event->y);
800 was_drag = TRUE;
801 break;
803 case MSG_MOUSE_SCROLL_UP:
804 case MSG_MOUSE_SCROLL_DOWN:
805 was_drag = FALSE;
807 if (widget_get_state (w, WST_FOCUSED))
809 if (event->y == 0)
811 /* menubar: left/right */
812 if (msg == MSG_MOUSE_SCROLL_UP)
813 menubar_left (menubar);
814 else
815 menubar_right (menubar);
817 else if (mouse_on_drop)
819 /* drop-down menu: up/down */
820 if (msg == MSG_MOUSE_SCROLL_UP)
821 menubar_up (menubar);
822 else
823 menubar_down (menubar);
826 break;
828 default:
829 was_drag = FALSE;
830 break;
834 /* --------------------------------------------------------------------------------------------- */
835 /*** public functions ****************************************************************************/
836 /* --------------------------------------------------------------------------------------------- */
838 menu_entry_t *
839 menu_entry_create (const char *name, long command)
841 menu_entry_t *entry;
843 entry = g_new (menu_entry_t, 1);
844 entry->first_letter = ' ';
845 entry->text = parse_hotkey (name);
846 entry->command = command;
847 entry->shortcut = NULL;
849 return entry;
852 /* --------------------------------------------------------------------------------------------- */
854 void
855 menu_entry_free (menu_entry_t * entry)
857 if (entry != NULL)
859 release_hotkey (entry->text);
860 g_free (entry->shortcut);
861 g_free (entry);
865 /* --------------------------------------------------------------------------------------------- */
867 menu_t *
868 create_menu (const char *name, GList * entries, const char *help_node)
870 menu_t *menu;
872 menu = g_new (menu_t, 1);
873 menu->start_x = 0;
874 menu->text = parse_hotkey (name);
875 menu->entries = entries;
876 menu->max_entry_len = 1;
877 menu->max_hotkey_len = 0;
878 menu->selected = 0;
879 menu->help_node = g_strdup (help_node);
881 return menu;
884 /* --------------------------------------------------------------------------------------------- */
886 void
887 menu_set_name (menu_t * menu, const char *name)
889 release_hotkey (menu->text);
890 menu->text = parse_hotkey (name);
893 /* --------------------------------------------------------------------------------------------- */
895 void
896 destroy_menu (menu_t * menu)
898 release_hotkey (menu->text);
899 g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free);
900 g_free (menu->help_node);
901 g_free (menu);
904 /* --------------------------------------------------------------------------------------------- */
906 WMenuBar *
907 menubar_new (int y, int x, int cols, GList * menu, gboolean visible)
909 WMenuBar *menubar;
910 Widget *w;
912 menubar = g_new0 (WMenuBar, 1);
913 w = WIDGET (menubar);
914 widget_init (w, y, x, 1, cols, menubar_callback, menubar_mouse_callback);
915 /* initially, menubar is not selectable */
916 widget_set_options (w, WOP_SELECTABLE, FALSE);
917 w->options |= WOP_TOP_SELECT;
918 menubar->is_visible = visible;
919 menubar_set_menu (menubar, menu);
921 return menubar;
924 /* --------------------------------------------------------------------------------------------- */
926 void
927 menubar_set_menu (WMenuBar * menubar, GList * menu)
929 /* delete previous menu */
930 menubar_free_menu (menubar);
931 /* add new menu */
932 menubar->is_dropped = FALSE;
933 menubar->menu = menu;
934 menubar->selected = 0;
935 menubar_arrange (menubar);
936 widget_set_state (WIDGET (menubar), WST_FOCUSED, FALSE);
939 /* --------------------------------------------------------------------------------------------- */
941 void
942 menubar_add_menu (WMenuBar * menubar, menu_t * menu)
944 if (menu != NULL)
946 menu_arrange (menu, WIDGET (menubar)->owner->get_shortcut);
947 menubar->menu = g_list_append (menubar->menu, menu);
950 menubar_arrange (menubar);
953 /* --------------------------------------------------------------------------------------------- */
955 * Properly space menubar items. Should be called when menubar is created
956 * and also when widget width is changed (i.e. upon xterm resize).
959 void
960 menubar_arrange (WMenuBar * menubar)
962 int start_x = 1;
963 GList *i;
964 int gap;
966 if (menubar->menu == NULL)
967 return;
969 gap = WIDGET (menubar)->cols - 2;
971 /* First, calculate gap between items... */
972 for (i = menubar->menu; i != NULL; i = g_list_next (i))
974 menu_t *menu = MENU (i->data);
976 /* preserve length here, to be used below */
977 menu->start_x = hotkey_width (menu->text) + 2;
978 gap -= menu->start_x;
981 if (g_list_next (menubar->menu) == NULL)
982 gap = 1;
983 else
984 gap /= (g_list_length (menubar->menu) - 1);
986 if (gap <= 0)
988 /* We are out of luck - window is too narrow... */
989 gap = 1;
991 else if (gap >= 3)
992 gap = 3;
994 /* ...and now fix start positions of menubar items */
995 for (i = menubar->menu; i != NULL; i = g_list_next (i))
997 menu_t *menu = MENU (i->data);
998 int len = menu->start_x;
1000 menu->start_x = start_x;
1001 start_x += len + gap;
1005 /* --------------------------------------------------------------------------------------------- */
1006 /** Find MenuBar widget in the dialog */
1008 WMenuBar *
1009 find_menubar (const WDialog * h)
1011 return MENUBAR (find_widget_type (h, menubar_callback));
1014 /* --------------------------------------------------------------------------------------------- */
1017 * Activate menu bar.
1019 * @param menubar menu bar object
1020 * @param dropped whether dropdown menus should be drooped or not
1021 * @which number of active dropdown menu
1023 void
1024 menubar_activate (WMenuBar * menubar, gboolean dropped, int which)
1026 Widget *w = WIDGET (menubar);
1028 if (!widget_get_state (w, WST_FOCUSED))
1030 widget_set_options (w, WOP_SELECTABLE, TRUE);
1032 widget_set_state (w, WST_FOCUSED, TRUE); /* FIXME: unneeded? */
1033 menubar->is_dropped = dropped;
1034 if (which >= 0)
1035 menubar->selected = (guint) which;
1037 menubar->previous_widget = dlg_get_current_widget_id (w->owner);
1039 /* Bring it to the top so it receives all mouse events before any other widget.
1040 * See also comment in menubar_finish(). */
1041 widget_select (w);
1045 /* --------------------------------------------------------------------------------------------- */