Drop old mouse API and use the new one.
[midnight-commander.git] / lib / widget / menu.c
blobd5d609a0f1003da760f45d3f6b09321312439003
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 (!menubar->is_active)
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 (menubar->is_active ? 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 menubar->is_dropped = FALSE;
308 menubar->is_active = FALSE;
309 w->lines = 1;
310 widget_want_hotkey (w, 0);
312 /* Move the menubar to the bottom so that widgets displayed on top of
313 * an "invisible" menubar get the first chance to respond to mouse events. */
314 dlg_set_bottom_widget (w);
316 dlg_select_by_id (w->owner, menubar->previous_widget);
317 do_refresh ();
320 /* --------------------------------------------------------------------------------------------- */
322 static void
323 menubar_drop (WMenuBar * menubar, unsigned int selected)
325 menubar->is_dropped = TRUE;
326 menubar->selected = selected;
327 menubar_draw (menubar);
330 /* --------------------------------------------------------------------------------------------- */
332 static void
333 menubar_execute (WMenuBar * menubar)
335 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
336 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
338 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
340 Widget *w = WIDGET (menubar);
342 mc_global.widget.is_right = (menubar->selected != 0);
343 menubar_finish (menubar);
344 send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
345 do_refresh ();
349 /* --------------------------------------------------------------------------------------------- */
351 static void
352 menubar_down (WMenuBar * menubar)
354 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
355 const unsigned int len = g_list_length (menu->entries);
356 menu_entry_t *entry;
358 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
362 menu->selected = (menu->selected + 1) % len;
363 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
365 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
367 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
370 /* --------------------------------------------------------------------------------------------- */
372 static void
373 menubar_up (WMenuBar * menubar)
375 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
376 const unsigned int len = g_list_length (menu->entries);
377 menu_entry_t *entry;
379 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
383 if (menu->selected == 0)
384 menu->selected = len - 1;
385 else
386 menu->selected--;
387 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
389 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
391 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
394 /* --------------------------------------------------------------------------------------------- */
396 static void
397 menubar_first (WMenuBar * menubar)
399 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
401 if (menu->selected == 0)
402 return;
404 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
406 menu->selected = 0;
408 while (TRUE)
410 menu_entry_t *entry;
412 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
414 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
415 menu->selected++;
416 else
417 break;
420 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
423 /* --------------------------------------------------------------------------------------------- */
425 static void
426 menubar_last (WMenuBar * menubar)
428 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
429 const unsigned int len = g_list_length (menu->entries);
430 menu_entry_t *entry;
432 if (menu->selected == len - 1)
433 return;
435 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
437 menu->selected = len;
441 menu->selected--;
442 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
444 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
446 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
449 /* --------------------------------------------------------------------------------------------- */
451 static int
452 menubar_handle_key (WMenuBar * menubar, int key)
454 /* Lowercase */
455 if (isascii (key))
456 key = g_ascii_tolower (key);
458 if (is_abort_char (key))
460 menubar_finish (menubar);
461 return 1;
464 /* menubar help or menubar navigation */
465 switch (key)
467 case KEY_F (1):
469 ev_help_t event_data = { NULL, NULL };
471 if (menubar->is_dropped)
472 event_data.node =
473 MENU (g_list_nth_data (menubar->menu, menubar->selected))->help_node;
474 else
475 event_data.node = "[Menu Bar]";
477 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
478 menubar_draw (menubar);
479 return 1;
482 case KEY_LEFT:
483 case XCTRL ('b'):
484 menubar_left (menubar);
485 return 1;
487 case KEY_RIGHT:
488 case XCTRL ('f'):
489 menubar_right (menubar);
490 return 1;
492 default:
493 break;
496 if (!menubar->is_dropped)
498 GList *i;
500 /* drop menu by hotkey */
501 for (i = menubar->menu; i != NULL; i = g_list_next (i))
503 menu_t *menu = MENU (i->data);
505 if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
507 menubar_drop (menubar, g_list_position (menubar->menu, i));
508 return 1;
512 /* drop menu by Enter or Dowwn key */
513 if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
514 menubar_drop (menubar, menubar->selected);
516 return 1;
520 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
521 GList *i;
523 /* execute menu command by hotkey */
524 for (i = menu->entries; i != NULL; i = g_list_next (i))
526 const menu_entry_t *entry = MENUENTRY (i->data);
528 if ((entry != NULL) && (entry->command != CK_IgnoreKey)
529 && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
531 menu->selected = g_list_position (menu->entries, i);
532 menubar_execute (menubar);
533 return 1;
537 /* menu execute by Enter or menu navigation */
538 switch (key)
540 case KEY_ENTER:
541 case '\n':
542 menubar_execute (menubar);
543 return 1;
545 case KEY_HOME:
546 case ALT ('<'):
547 menubar_first (menubar);
548 break;
550 case KEY_END:
551 case ALT ('>'):
552 menubar_last (menubar);
553 break;
555 case KEY_DOWN:
556 case XCTRL ('n'):
557 menubar_down (menubar);
558 break;
560 case KEY_UP:
561 case XCTRL ('p'):
562 menubar_up (menubar);
563 break;
565 default:
566 break;
570 return 0;
573 /* --------------------------------------------------------------------------------------------- */
575 static cb_ret_t
576 menubar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
578 WMenuBar *menubar = MENUBAR (w);
580 switch (msg)
582 /* We do not want the focus unless we have been activated */
583 case MSG_FOCUS:
584 if (!menubar->is_active)
585 return MSG_NOT_HANDLED;
587 /* Trick to get all the mouse events */
588 w->lines = LINES;
590 /* Trick to get all of the hotkeys */
591 widget_want_hotkey (w, 1);
592 menubar_draw (menubar);
593 return MSG_HANDLED;
595 /* We don't want the buttonbar to activate while using the menubar */
596 case MSG_HOTKEY:
597 case MSG_KEY:
598 if (menubar->is_active)
600 menubar_handle_key (menubar, parm);
601 return MSG_HANDLED;
603 return MSG_NOT_HANDLED;
605 case MSG_CURSOR:
606 /* Put the cursor in a suitable place */
607 return MSG_NOT_HANDLED;
609 case MSG_UNFOCUS:
610 return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
612 case MSG_DRAW:
613 if (menubar->is_visible)
615 menubar_draw (menubar);
616 return MSG_HANDLED;
618 /* fall through */
620 case MSG_RESIZE:
621 /* try show menu after screen resize */
622 send_message (w, sender, MSG_FOCUS, 0, data);
623 return MSG_HANDLED;
626 case MSG_DESTROY:
627 menubar_set_menu (menubar, NULL);
628 return MSG_HANDLED;
630 default:
631 return widget_default_callback (w, sender, msg, parm, data);
635 /* --------------------------------------------------------------------------------------------- */
637 static unsigned int
638 menubar_get_menu_by_x_coord (const WMenuBar * menubar, int x)
640 unsigned int i;
641 GList *menu;
643 for (i = 0, menu = menubar->menu;
644 menu != NULL && x > MENU (menu->data)->start_x; i++, menu = g_list_next (menu))
647 /* Don't set the invalid value -1 */
648 if (i != 0)
649 i--;
651 return i;
654 /* --------------------------------------------------------------------------------------------- */
656 static gboolean
657 menubar_mouse_on_menu (const WMenuBar * menubar, int y, int x)
659 Widget *w = WIDGET (menubar);
660 menu_t *menu;
661 int left_x, right_x, bottom_y;
663 if (!menubar->is_dropped)
664 return FALSE;
666 menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
667 left_x = menu->start_x;
668 right_x = left_x + menu->max_entry_len + 3;
669 if (right_x > w->cols)
671 left_x = w->cols - (menu->max_entry_len + 3);
672 right_x = w->cols;
675 bottom_y = g_list_length (menu->entries) + 2; /* skip bar and top frame */
677 return (x >= left_x && x < right_x && y > 1 && y < bottom_y);
680 /* --------------------------------------------------------------------------------------------- */
682 static void
683 menubar_change_selected_item (WMenuBar * menubar, int y)
685 menu_t *menu;
686 menu_entry_t *entry;
688 y -= 2; /* skip bar and top frame */
689 menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
690 entry = MENUENTRY (g_list_nth_data (menu->entries, y));
692 if (entry != NULL && entry->command != CK_IgnoreKey)
694 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
695 menu->selected = y;
696 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
700 /* --------------------------------------------------------------------------------------------- */
702 static void
703 menubar_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
705 static gboolean was_drag = FALSE;
707 WMenuBar *menubar = MENUBAR (w);
708 gboolean mouse_on_drop;
710 mouse_on_drop = menubar_mouse_on_menu (menubar, event->y, event->x);
712 switch (msg)
714 case MSG_MOUSE_DOWN:
715 was_drag = FALSE;
717 if (event->y == 0)
719 /* events on menubar */
720 unsigned int selected;
722 selected = menubar_get_menu_by_x_coord (menubar, event->x);
723 menubar_activate (menubar, TRUE, selected);
724 menubar_remove (menubar); /* if already shown */
725 menubar_drop (menubar, selected);
727 else if (mouse_on_drop)
728 menubar_change_selected_item (menubar, event->y);
729 else
731 /* mouse click outside menubar or dropdown -- close menu */
732 menubar_finish (menubar);
735 * @FIXME.
737 * Unless we clear the 'capture' flag, we'll receive MSG_MOUSE_DRAG
738 * events belonging to this click (in case the user drags the mouse,
739 * of course).
741 * For the time being, we mark this with FIXME as this flag should
742 * preferably be regarded as "implementation detail" and not be
743 * touched by us. We should think of some other way of communicating
744 * this to the system.
746 w->mouse.capture = FALSE;
748 break;
750 case MSG_MOUSE_UP:
751 if (was_drag && mouse_on_drop)
752 menubar_execute (menubar);
753 was_drag = FALSE;
754 break;
756 case MSG_MOUSE_CLICK:
757 was_drag = FALSE;
759 if ((event->buttons & GPM_B_MIDDLE) != 0 && event->y > 0 && menubar->is_dropped)
761 /* middle click -- everywhere */
762 menubar_execute (menubar);
764 else if (mouse_on_drop)
765 menubar_execute (menubar);
766 else if (event->y > 0)
767 /* releasing the mouse button outside the menu -- close menu */
768 menubar_finish (menubar);
769 break;
771 case MSG_MOUSE_DRAG:
772 if (event->y == 0)
774 menubar_remove (menubar);
775 menubar_drop (menubar, menubar_get_menu_by_x_coord (menubar, event->x));
777 else if (mouse_on_drop)
778 menubar_change_selected_item (menubar, event->y);
780 was_drag = TRUE;
781 break;
783 case MSG_MOUSE_SCROLL_UP:
784 case MSG_MOUSE_SCROLL_DOWN:
785 was_drag = FALSE;
787 if (menubar->is_active)
789 if (event->y == 0)
791 /* menubar: left/right */
792 if (msg == MSG_MOUSE_SCROLL_UP)
793 menubar_left (menubar);
794 else
795 menubar_right (menubar);
797 else if (mouse_on_drop)
799 /* drop-down menu: up/down */
800 if (msg == MSG_MOUSE_SCROLL_UP)
801 menubar_up (menubar);
802 else
803 menubar_down (menubar);
806 break;
808 default:
809 was_drag = FALSE;
810 break;
814 /* --------------------------------------------------------------------------------------------- */
815 /*** public functions ****************************************************************************/
816 /* --------------------------------------------------------------------------------------------- */
818 menu_entry_t *
819 menu_entry_create (const char *name, long command)
821 menu_entry_t *entry;
823 entry = g_new (menu_entry_t, 1);
824 entry->first_letter = ' ';
825 entry->text = parse_hotkey (name);
826 entry->command = command;
827 entry->shortcut = NULL;
829 return entry;
832 /* --------------------------------------------------------------------------------------------- */
834 void
835 menu_entry_free (menu_entry_t * entry)
837 if (entry != NULL)
839 release_hotkey (entry->text);
840 g_free (entry->shortcut);
841 g_free (entry);
845 /* --------------------------------------------------------------------------------------------- */
847 menu_t *
848 create_menu (const char *name, GList * entries, const char *help_node)
850 menu_t *menu;
852 menu = g_new (menu_t, 1);
853 menu->start_x = 0;
854 menu->text = parse_hotkey (name);
855 menu->entries = entries;
856 menu->max_entry_len = 1;
857 menu->max_hotkey_len = 0;
858 menu->selected = 0;
859 menu->help_node = g_strdup (help_node);
861 return menu;
864 /* --------------------------------------------------------------------------------------------- */
866 void
867 menu_set_name (menu_t * menu, const char *name)
869 release_hotkey (menu->text);
870 menu->text = parse_hotkey (name);
873 /* --------------------------------------------------------------------------------------------- */
875 void
876 destroy_menu (menu_t * menu)
878 release_hotkey (menu->text);
879 g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free);
880 g_free (menu->help_node);
881 g_free (menu);
884 /* --------------------------------------------------------------------------------------------- */
886 WMenuBar *
887 menubar_new (int y, int x, int cols, GList * menu, gboolean visible)
889 WMenuBar *menubar;
890 Widget *w;
892 menubar = g_new0 (WMenuBar, 1);
893 w = WIDGET (menubar);
894 widget_init (w, y, x, 1, cols, menubar_callback, menubar_mouse_callback);
896 menubar->is_visible = visible;
897 widget_want_cursor (w, FALSE);
898 menubar_set_menu (menubar, menu);
900 return menubar;
903 /* --------------------------------------------------------------------------------------------- */
905 void
906 menubar_set_menu (WMenuBar * menubar, GList * menu)
908 /* delete previous menu */
909 if (menubar->menu != NULL)
910 g_list_free_full (menubar->menu, (GDestroyNotify) destroy_menu);
912 /* add new menu */
913 menubar->is_active = FALSE;
914 menubar->is_dropped = FALSE;
915 menubar->menu = menu;
916 menubar->selected = 0;
917 menubar_arrange (menubar);
920 /* --------------------------------------------------------------------------------------------- */
922 void
923 menubar_add_menu (WMenuBar * menubar, menu_t * menu)
925 if (menu != NULL)
927 menu_arrange (menu, WIDGET (menubar)->owner->get_shortcut);
928 menubar->menu = g_list_append (menubar->menu, menu);
931 menubar_arrange (menubar);
934 /* --------------------------------------------------------------------------------------------- */
936 * Properly space menubar items. Should be called when menubar is created
937 * and also when widget width is changed (i.e. upon xterm resize).
940 void
941 menubar_arrange (WMenuBar * menubar)
943 int start_x = 1;
944 GList *i;
945 int gap;
947 if (menubar->menu == NULL)
948 return;
950 gap = WIDGET (menubar)->cols - 2;
952 /* First, calculate gap between items... */
953 for (i = menubar->menu; i != NULL; i = g_list_next (i))
955 menu_t *menu = MENU (i->data);
957 /* preserve length here, to be used below */
958 menu->start_x = hotkey_width (menu->text) + 2;
959 gap -= menu->start_x;
962 if (g_list_next (menubar->menu) == NULL)
963 gap = 1;
964 else
965 gap /= (g_list_length (menubar->menu) - 1);
967 if (gap <= 0)
969 /* We are out of luck - window is too narrow... */
970 gap = 1;
972 else if (gap >= 3)
973 gap = 3;
975 /* ...and now fix start positions of menubar items */
976 for (i = menubar->menu; i != NULL; i = g_list_next (i))
978 menu_t *menu = MENU (i->data);
979 int len = menu->start_x;
981 menu->start_x = start_x;
982 start_x += len + gap;
986 /* --------------------------------------------------------------------------------------------- */
987 /** Find MenuBar widget in the dialog */
989 WMenuBar *
990 find_menubar (const WDialog * h)
992 return MENUBAR (find_widget_type (h, menubar_callback));
995 /* --------------------------------------------------------------------------------------------- */
998 * Activate menu bar.
1000 * @param menubar menu bar object
1001 * @param dropped whether dropdown menus should be drooped or not
1002 * @which number of active dropdown menu
1004 void
1005 menubar_activate (WMenuBar * menubar, gboolean dropped, int which)
1007 Widget *w = WIDGET (menubar);
1009 if (!menubar->is_active)
1011 menubar->is_active = TRUE;
1012 menubar->is_dropped = dropped;
1013 if (which >= 0)
1014 menubar->selected = (guint) which;
1016 menubar->previous_widget = dlg_get_current_widget_id (w->owner);
1018 /* Bring it to the top so it receives all mouse events before any other widget.
1019 * See also comment in menubar_finish(). */
1020 dlg_set_top_widget (w);
1024 /* --------------------------------------------------------------------------------------------- */