Internal menu structures are opaque now.
[midnight-commander.git] / lib / widget / menu.c
blobc6f67fe145bac3947852196d1239a5fee57bd1ff
1 /*
2 Pulldown menu code
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2009, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Andrew Borodin <aborodin@vmail.ru>, 2012
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file menu.c
28 * \brief Source: pulldown menu code
31 #include <config.h>
33 #include <ctype.h>
34 #include <stdarg.h>
35 #include <string.h>
36 #include <sys/types.h>
38 #include "lib/global.h"
40 #include "lib/tty/tty.h"
41 #include "lib/skin.h"
42 #include "lib/tty/mouse.h"
43 #include "lib/tty/key.h" /* key macros */
44 #include "lib/strutil.h"
45 #include "lib/widget.h"
46 #include "lib/event.h" /* mc_event_raise() */
48 /*** global variables ****************************************************************************/
50 /*** file scope macro definitions ****************************************************************/
52 #define MENUENTRY(x) ((menu_entry_t *)(x))
53 #define MENU(x) ((menu_t *)(x))
55 /*** file scope type declarations ****************************************************************/
57 struct menu_entry_t
59 unsigned char first_letter;
60 hotkey_t text;
61 unsigned long command;
62 char *shortcut;
65 struct menu_t
67 int start_x; /* position relative to menubar start */
68 hotkey_t text;
69 GList *entries;
70 size_t max_entry_len; /* cached max length of entry texts (text + shortcut) */
71 size_t max_hotkey_len; /* cached max length of shortcuts */
72 unsigned int selected; /* pointer to current menu entry */
73 char *help_node;
76 /*** file scope variables ************************************************************************/
78 /*** file scope functions ************************************************************************/
79 /* --------------------------------------------------------------------------------------------- */
81 static void
82 menu_arrange (menu_t * menu, dlg_shortcut_str get_shortcut)
84 if (menu != NULL)
86 GList *i;
87 size_t max_shortcut_len = 0;
89 menu->max_entry_len = 1;
90 menu->max_hotkey_len = 1;
92 for (i = menu->entries; i != NULL; i = g_list_next (i))
94 menu_entry_t *entry = MENUENTRY (i->data);
96 if (entry != NULL)
98 size_t len;
100 len = (size_t) hotkey_width (entry->text);
101 menu->max_hotkey_len = max (menu->max_hotkey_len, len);
103 if (get_shortcut != NULL)
104 entry->shortcut = get_shortcut (entry->command);
106 if (entry->shortcut != NULL)
108 len = (size_t) str_term_width1 (entry->shortcut);
109 max_shortcut_len = max (max_shortcut_len, len);
114 menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
118 /* --------------------------------------------------------------------------------------------- */
120 static void
121 menubar_paint_idx (WMenuBar * menubar, unsigned int idx, int color)
123 Widget *w = WIDGET (menubar);
124 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
125 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, idx));
126 const int y = 2 + idx;
127 int x = menu->start_x;
129 if (x + menu->max_entry_len + 4 > (gsize) w->cols)
130 x = w->cols - menu->max_entry_len - 4;
132 if (entry == NULL)
134 /* menu separator */
135 tty_setcolor (MENU_ENTRY_COLOR);
137 widget_move (w, y, x - 1);
138 tty_print_alt_char (ACS_LTEE, FALSE);
139 tty_draw_hline (w->y + y, w->x + x, ACS_HLINE, menu->max_entry_len + 3);
140 widget_move (w, y, x + menu->max_entry_len + 3);
141 tty_print_alt_char (ACS_RTEE, FALSE);
143 else
145 int yt, xt;
147 /* menu text */
148 tty_setcolor (color);
149 widget_move (w, y, x);
150 tty_print_char ((unsigned char) entry->first_letter);
151 tty_getyx (&yt, &xt);
152 tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2); /* clear line */
153 tty_print_string (entry->text.start);
155 if (entry->text.hotkey != NULL)
157 tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
158 tty_print_string (entry->text.hotkey);
159 tty_setcolor (color);
162 if (entry->text.end != NULL)
163 tty_print_string (entry->text.end);
165 if (entry->shortcut != NULL)
167 widget_move (w, y, x + menu->max_hotkey_len + 3);
168 tty_print_string (entry->shortcut);
171 /* move cursor to the start of entry text */
172 widget_move (w, y, x + 1);
176 /* --------------------------------------------------------------------------------------------- */
178 static void
179 menubar_draw_drop (WMenuBar * menubar)
181 Widget *w = WIDGET (menubar);
182 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
183 const unsigned int count = g_list_length (menu->entries);
184 int column = menu->start_x - 1;
185 unsigned int i;
187 if (column + menu->max_entry_len + 5 > (gsize) w->cols)
188 column = w->cols - menu->max_entry_len - 5;
190 tty_setcolor (MENU_ENTRY_COLOR);
191 draw_box (w->owner, w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
193 for (i = 0; i < count; i++)
194 menubar_paint_idx (menubar, i,
195 i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
198 /* --------------------------------------------------------------------------------------------- */
200 static void
201 menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
203 if (!menubar->is_active)
204 tty_setcolor (MENU_INACTIVE_COLOR);
205 else if (current)
206 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
207 else
208 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
211 /* --------------------------------------------------------------------------------------------- */
213 static void
214 menubar_draw (WMenuBar * menubar)
216 Widget *w = WIDGET (menubar);
217 GList *i;
219 /* First draw the complete menubar */
220 tty_setcolor (menubar->is_active ? MENU_ENTRY_COLOR : MENU_INACTIVE_COLOR);
221 tty_draw_hline (w->y, w->x, ' ', w->cols);
223 /* Now each one of the entries */
224 for (i = menubar->menu; i != NULL; i = g_list_next (i))
226 menu_t *menu = MENU (i->data);
227 gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
229 menubar_set_color (menubar, is_selected, FALSE);
230 widget_move (w, 0, menu->start_x);
232 tty_print_char (' ');
233 tty_print_string (menu->text.start);
235 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 (w, 0,
252 MENU (g_list_nth_data (menubar->menu, menubar->selected))->start_x);
255 /* --------------------------------------------------------------------------------------------- */
257 static void
258 menubar_remove (WMenuBar * menubar)
260 WDialog *h;
262 if (!menubar->is_dropped)
263 return;
265 /* HACK: before refresh the dialog, change the current widget to keep the order
266 of overlapped widgets. This is useful in multi-window editor.
267 In general, menubar should be a special object, not an ordinary widget
268 in the current dialog. */
269 h = WIDGET (menubar)->owner;
270 h->current = g_list_find (h->widgets, dlg_find_by_id (h, menubar->previous_widget));
272 menubar->is_dropped = FALSE;
273 do_refresh ();
274 menubar->is_dropped = TRUE;
276 /* restore current widget */
277 h->current = g_list_find (h->widgets, menubar);
280 /* --------------------------------------------------------------------------------------------- */
282 static void
283 menubar_left (WMenuBar * menubar)
285 menubar_remove (menubar);
286 if (menubar->selected == 0)
287 menubar->selected = g_list_length (menubar->menu) - 1;
288 else
289 menubar->selected--;
290 menubar_draw (menubar);
293 /* --------------------------------------------------------------------------------------------- */
295 static void
296 menubar_right (WMenuBar * menubar)
298 menubar_remove (menubar);
299 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
300 menubar_draw (menubar);
303 /* --------------------------------------------------------------------------------------------- */
305 static void
306 menubar_finish (WMenuBar * menubar)
308 Widget *w = WIDGET (menubar);
310 menubar->is_dropped = FALSE;
311 menubar->is_active = FALSE;
312 w->lines = 1;
313 widget_want_hotkey (w, 0);
315 dlg_select_by_id (w->owner, menubar->previous_widget);
316 do_refresh ();
319 /* --------------------------------------------------------------------------------------------- */
321 static void
322 menubar_drop (WMenuBar * menubar, unsigned int selected)
324 menubar->is_dropped = TRUE;
325 menubar->selected = selected;
326 menubar_draw (menubar);
329 /* --------------------------------------------------------------------------------------------- */
331 static void
332 menubar_execute (WMenuBar * menubar)
334 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
335 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
337 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
339 Widget *w = WIDGET (menubar);
341 mc_global.widget.is_right = (menubar->selected != 0);
342 menubar_finish (menubar);
343 send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
344 do_refresh ();
348 /* --------------------------------------------------------------------------------------------- */
350 static void
351 menubar_down (WMenuBar * menubar)
353 menu_t *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 menu->selected = (menu->selected + 1) % len;
362 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
364 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
366 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
369 /* --------------------------------------------------------------------------------------------- */
371 static void
372 menubar_up (WMenuBar * menubar)
374 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
375 const unsigned int len = g_list_length (menu->entries);
376 menu_entry_t *entry;
378 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
382 if (menu->selected == 0)
383 menu->selected = len - 1;
384 else
385 menu->selected--;
386 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
388 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
390 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
393 /* --------------------------------------------------------------------------------------------- */
395 static void
396 menubar_first (WMenuBar * menubar)
398 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
399 menu_entry_t *entry;
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 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
412 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
413 menu->selected++;
414 else
415 break;
418 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
421 /* --------------------------------------------------------------------------------------------- */
423 static void
424 menubar_last (WMenuBar * menubar)
426 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
427 const unsigned int len = g_list_length (menu->entries);
428 menu_entry_t *entry;
430 if (menu->selected == len - 1)
431 return;
433 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
435 menu->selected = len;
439 menu->selected--;
440 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
442 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
444 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
447 /* --------------------------------------------------------------------------------------------- */
449 static int
450 menubar_handle_key (WMenuBar * menubar, int key)
452 /* Lowercase */
453 if (isascii (key))
454 key = g_ascii_tolower (key);
456 if (is_abort_char (key))
458 menubar_finish (menubar);
459 return 1;
462 /* menubar help or menubar navigation */
463 switch (key)
465 case KEY_F (1):
467 ev_help_t event_data = { NULL, NULL };
469 if (menubar->is_dropped)
470 event_data.node =
471 MENU (g_list_nth_data (menubar->menu, menubar->selected))->help_node;
472 else
473 event_data.node = "[Menu Bar]";
475 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
476 menubar_draw (menubar);
477 return 1;
479 case KEY_LEFT:
480 case XCTRL ('b'):
481 menubar_left (menubar);
482 return 1;
484 case KEY_RIGHT:
485 case XCTRL ('f'):
486 menubar_right (menubar);
487 return 1;
490 if (!menubar->is_dropped)
492 GList *i;
494 /* drop menu by hotkey */
495 for (i = menubar->menu; i != NULL; i = g_list_next (i))
497 menu_t *menu = MENU (i->data);
499 if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
501 menubar_drop (menubar, g_list_position (menubar->menu, i));
502 return 1;
506 /* drop menu by Enter or Dowwn key */
507 if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
508 menubar_drop (menubar, menubar->selected);
510 return 1;
514 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
515 GList *i;
517 /* execute menu command by hotkey */
518 for (i = menu->entries; i != NULL; i = g_list_next (i))
520 const menu_entry_t *entry = MENUENTRY (i->data);
522 if ((entry != NULL) && (entry->command != CK_IgnoreKey)
523 && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
525 menu->selected = g_list_position (menu->entries, i);
526 menubar_execute (menubar);
527 return 1;
531 /* menu execute by Enter or menu navigation */
532 switch (key)
534 case KEY_ENTER:
535 case '\n':
536 menubar_execute (menubar);
537 return 1;
539 case KEY_HOME:
540 case ALT ('<'):
541 menubar_first (menubar);
542 break;
544 case KEY_END:
545 case ALT ('>'):
546 menubar_last (menubar);
547 break;
549 case KEY_DOWN:
550 case XCTRL ('n'):
551 menubar_down (menubar);
552 break;
554 case KEY_UP:
555 case XCTRL ('p'):
556 menubar_up (menubar);
557 break;
561 return 0;
564 /* --------------------------------------------------------------------------------------------- */
566 static cb_ret_t
567 menubar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
569 WMenuBar *menubar = MENUBAR (w);
571 switch (msg)
573 /* We do not want the focus unless we have been activated */
574 case MSG_FOCUS:
575 if (!menubar->is_active)
576 return MSG_NOT_HANDLED;
578 /* Trick to get all the mouse events */
579 w->lines = LINES;
581 /* Trick to get all of the hotkeys */
582 widget_want_hotkey (w, 1);
583 menubar_draw (menubar);
584 return MSG_HANDLED;
586 /* We don't want the buttonbar to activate while using the menubar */
587 case MSG_HOTKEY:
588 case MSG_KEY:
589 if (menubar->is_active)
591 menubar_handle_key (menubar, parm);
592 return MSG_HANDLED;
594 return MSG_NOT_HANDLED;
596 case MSG_CURSOR:
597 /* Put the cursor in a suitable place */
598 return MSG_NOT_HANDLED;
600 case MSG_UNFOCUS:
601 return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
603 case MSG_DRAW:
604 if (menubar->is_visible)
606 menubar_draw (menubar);
607 return MSG_HANDLED;
609 /* fall through */
611 case MSG_RESIZE:
612 /* try show menu after screen resize */
613 send_message (w, sender, MSG_FOCUS, 0, data);
614 return MSG_HANDLED;
617 case MSG_DESTROY:
618 menubar_set_menu (menubar, NULL);
619 return MSG_HANDLED;
621 default:
622 return widget_default_callback (w, sender, msg, parm, data);
626 /* --------------------------------------------------------------------------------------------- */
628 static int
629 menubar_event (Gpm_Event * event, void *data)
631 WMenuBar *menubar = MENUBAR (data);
632 Widget *w = WIDGET (data);
633 gboolean was_active = TRUE;
634 int left_x, right_x, bottom_y;
635 menu_t *menu;
636 Gpm_Event local;
638 if (!mouse_global_in_widget (event, w))
639 return MOU_UNHANDLED;
641 /* ignore unsupported events */
642 if ((event->type & (GPM_UP | GPM_DOWN | GPM_DRAG)) == 0)
643 return MOU_NORMAL;
645 /* ignore wheel events if menu is inactive */
646 if (!menubar->is_active && ((event->buttons & (GPM_B_MIDDLE | GPM_B_UP | GPM_B_DOWN)) != 0))
647 return MOU_NORMAL;
649 local = mouse_get_local (event, w);
651 if (local.y == 1 && (local.type & GPM_UP) != 0)
652 return MOU_NORMAL;
654 if (!menubar->is_dropped)
656 menubar->previous_widget = dlg_get_current_widget_id (w->owner);
657 menubar->is_active = TRUE;
658 menubar->is_dropped = TRUE;
659 was_active = FALSE;
662 /* Mouse operations on the menubar */
663 if (local.y == 1 || !was_active)
665 /* wheel events on menubar */
666 if ((local.buttons & GPM_B_UP) != 0)
667 menubar_left (menubar);
668 else if ((local.buttons & GPM_B_DOWN) != 0)
669 menubar_right (menubar);
670 else
672 const unsigned int len = g_list_length (menubar->menu);
673 unsigned int new_selection = 0;
675 while ((new_selection < len)
676 && (local.x > MENU (g_list_nth_data (menubar->menu, new_selection))->start_x))
677 new_selection++;
679 if (new_selection != 0) /* Don't set the invalid value -1 */
680 new_selection--;
682 if (!was_active)
684 menubar->selected = new_selection;
685 dlg_select_widget (menubar);
687 else
689 menubar_remove (menubar);
690 menubar->selected = new_selection;
692 menubar_draw (menubar);
694 return MOU_NORMAL;
697 if (!menubar->is_dropped || (local.y < 2))
698 return MOU_NORMAL;
700 /* middle click -- everywhere */
701 if (((local.buttons & GPM_B_MIDDLE) != 0) && ((local.type & GPM_DOWN) != 0))
703 menubar_execute (menubar);
704 return MOU_NORMAL;
707 /* the mouse operation is on the menus or it is not */
708 menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
709 left_x = menu->start_x;
710 right_x = left_x + menu->max_entry_len + 3;
711 if (right_x > w->cols)
713 left_x = w->cols - menu->max_entry_len - 3;
714 right_x = w->cols;
717 bottom_y = g_list_length (menu->entries) + 3;
719 if ((local.x >= left_x) && (local.x <= right_x) && (local.y <= bottom_y))
721 int pos = local.y - 3;
722 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, pos));
724 /* mouse wheel */
725 if ((local.buttons & GPM_B_UP) != 0 && (local.type & GPM_DOWN) != 0)
727 menubar_up (menubar);
728 return MOU_NORMAL;
730 if ((local.buttons & GPM_B_DOWN) != 0 && (local.type & GPM_DOWN) != 0)
732 menubar_down (menubar);
733 return MOU_NORMAL;
736 /* ignore events above and below dropped down menu */
737 if ((pos < 0) || (pos >= bottom_y - 3))
738 return MOU_NORMAL;
740 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
742 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
743 menu->selected = pos;
744 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
746 if ((event->type & GPM_UP) != 0)
747 menubar_execute (menubar);
750 else if (((local.type & GPM_DOWN) != 0) && ((local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0))
752 /* use click not wheel to close menu */
753 menubar_finish (menubar);
756 return MOU_NORMAL;
759 /* --------------------------------------------------------------------------------------------- */
760 /*** public functions ****************************************************************************/
761 /* --------------------------------------------------------------------------------------------- */
763 menu_entry_t *
764 menu_entry_create (const char *name, unsigned long command)
766 menu_entry_t *entry;
768 entry = g_new (menu_entry_t, 1);
769 entry->first_letter = ' ';
770 entry->text = parse_hotkey (name);
771 entry->command = command;
772 entry->shortcut = NULL;
774 return entry;
777 /* --------------------------------------------------------------------------------------------- */
779 void
780 menu_entry_free (menu_entry_t * entry)
782 if (entry != NULL)
784 release_hotkey (entry->text);
785 g_free (entry->shortcut);
786 g_free (entry);
790 /* --------------------------------------------------------------------------------------------- */
792 menu_t *
793 create_menu (const char *name, GList * entries, const char *help_node)
795 menu_t *menu;
797 menu = g_new (menu_t, 1);
798 menu->start_x = 0;
799 menu->text = parse_hotkey (name);
800 menu->entries = entries;
801 menu->max_entry_len = 1;
802 menu->max_hotkey_len = 0;
803 menu->selected = 0;
804 menu->help_node = g_strdup (help_node);
806 return menu;
809 /* --------------------------------------------------------------------------------------------- */
811 void
812 menu_set_name (menu_t * menu, const char *name)
814 release_hotkey (menu->text);
815 menu->text = parse_hotkey (name);
818 /* --------------------------------------------------------------------------------------------- */
820 void
821 destroy_menu (menu_t * menu)
823 release_hotkey (menu->text);
824 g_list_foreach (menu->entries, (GFunc) menu_entry_free, NULL);
825 g_list_free (menu->entries);
826 g_free (menu->help_node);
827 g_free (menu);
830 /* --------------------------------------------------------------------------------------------- */
832 WMenuBar *
833 menubar_new (int y, int x, int cols, GList * menu)
835 WMenuBar *menubar;
836 Widget *w;
838 menubar = g_new0 (WMenuBar, 1);
839 w = WIDGET (menubar);
840 init_widget (w, y, x, 1, cols, menubar_callback, menubar_event);
842 menubar->is_visible = TRUE; /* by default */
843 widget_want_cursor (w, FALSE);
844 menubar_set_menu (menubar, menu);
846 return menubar;
849 /* --------------------------------------------------------------------------------------------- */
851 void
852 menubar_set_menu (WMenuBar * menubar, GList * menu)
854 /* delete previous menu */
855 if (menubar->menu != NULL)
857 g_list_foreach (menubar->menu, (GFunc) destroy_menu, NULL);
858 g_list_free (menubar->menu);
860 /* add new menu */
861 menubar->is_active = FALSE;
862 menubar->is_dropped = FALSE;
863 menubar->menu = menu;
864 menubar->selected = 0;
865 menubar_arrange (menubar);
868 /* --------------------------------------------------------------------------------------------- */
870 void
871 menubar_add_menu (WMenuBar * menubar, menu_t * menu)
873 if (menu != NULL)
875 menu_arrange (menu, WIDGET (menubar)->owner->get_shortcut);
876 menubar->menu = g_list_append (menubar->menu, menu);
879 menubar_arrange (menubar);
882 /* --------------------------------------------------------------------------------------------- */
884 * Properly space menubar items. Should be called when menubar is created
885 * and also when widget width is changed (i.e. upon xterm resize).
888 void
889 menubar_arrange (WMenuBar * menubar)
891 int start_x = 1;
892 GList *i;
893 int gap;
895 if (menubar->menu == NULL)
896 return;
898 gap = WIDGET (menubar)->cols - 2;
900 /* First, calculate gap between items... */
901 for (i = menubar->menu; i != NULL; i = g_list_next (i))
903 menu_t *menu = MENU (i->data);
905 /* preserve length here, to be used below */
906 menu->start_x = hotkey_width (menu->text) + 2;
907 gap -= menu->start_x;
910 if (g_list_next (menubar->menu) == NULL)
911 gap = 1;
912 else
913 gap /= (g_list_length (menubar->menu) - 1);
915 if (gap <= 0)
917 /* We are out of luck - window is too narrow... */
918 gap = 1;
920 else if (gap >= 3)
921 gap = 3;
923 /* ...and now fix start positions of menubar items */
924 for (i = menubar->menu; i != NULL; i = g_list_next (i))
926 menu_t *menu = MENU (i->data);
927 int len = menu->start_x;
929 menu->start_x = start_x;
930 start_x += len + gap;
934 /* --------------------------------------------------------------------------------------------- */
935 /** Find MenuBar widget in the dialog */
937 WMenuBar *
938 find_menubar (const WDialog * h)
940 return MENUBAR (find_widget_type (h, menubar_callback));
943 /* --------------------------------------------------------------------------------------------- */