Merge branch '2944_cleanup'
[midnight-commander.git] / lib / widget / menu.c
blob0aa9445f681464282480f76d525e46590919f87f
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, MENU (g_list_nth_data (menubar->menu, menubar->selected))->start_x);
254 /* --------------------------------------------------------------------------------------------- */
256 static void
257 menubar_remove (WMenuBar * menubar)
259 WDialog *h;
261 if (!menubar->is_dropped)
262 return;
264 /* HACK: before refresh the dialog, change the current widget to keep the order
265 of overlapped widgets. This is useful in multi-window editor.
266 In general, menubar should be a special object, not an ordinary widget
267 in the current dialog. */
268 h = WIDGET (menubar)->owner;
269 h->current = g_list_find (h->widgets, dlg_find_by_id (h, menubar->previous_widget));
271 menubar->is_dropped = FALSE;
272 do_refresh ();
273 menubar->is_dropped = TRUE;
275 /* restore current widget */
276 h->current = g_list_find (h->widgets, menubar);
279 /* --------------------------------------------------------------------------------------------- */
281 static void
282 menubar_left (WMenuBar * menubar)
284 menubar_remove (menubar);
285 if (menubar->selected == 0)
286 menubar->selected = g_list_length (menubar->menu) - 1;
287 else
288 menubar->selected--;
289 menubar_draw (menubar);
292 /* --------------------------------------------------------------------------------------------- */
294 static void
295 menubar_right (WMenuBar * menubar)
297 menubar_remove (menubar);
298 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
299 menubar_draw (menubar);
302 /* --------------------------------------------------------------------------------------------- */
304 static void
305 menubar_finish (WMenuBar * menubar)
307 Widget *w = WIDGET (menubar);
309 menubar->is_dropped = FALSE;
310 menubar->is_active = FALSE;
311 w->lines = 1;
312 widget_want_hotkey (w, 0);
314 dlg_select_by_id (w->owner, menubar->previous_widget);
315 do_refresh ();
318 /* --------------------------------------------------------------------------------------------- */
320 static void
321 menubar_drop (WMenuBar * menubar, unsigned int selected)
323 menubar->is_dropped = TRUE;
324 menubar->selected = selected;
325 menubar_draw (menubar);
328 /* --------------------------------------------------------------------------------------------- */
330 static void
331 menubar_execute (WMenuBar * menubar)
333 const menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
334 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
336 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
338 Widget *w = WIDGET (menubar);
340 mc_global.widget.is_right = (menubar->selected != 0);
341 menubar_finish (menubar);
342 send_message (w->owner, w, MSG_ACTION, entry->command, NULL);
343 do_refresh ();
347 /* --------------------------------------------------------------------------------------------- */
349 static void
350 menubar_down (WMenuBar * menubar)
352 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
353 const unsigned int len = g_list_length (menu->entries);
354 menu_entry_t *entry;
356 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
360 menu->selected = (menu->selected + 1) % len;
361 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
363 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
365 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
368 /* --------------------------------------------------------------------------------------------- */
370 static void
371 menubar_up (WMenuBar * menubar)
373 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
374 const unsigned int len = g_list_length (menu->entries);
375 menu_entry_t *entry;
377 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
381 if (menu->selected == 0)
382 menu->selected = len - 1;
383 else
384 menu->selected--;
385 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
387 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
389 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
392 /* --------------------------------------------------------------------------------------------- */
394 static void
395 menubar_first (WMenuBar * menubar)
397 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
398 menu_entry_t *entry;
400 if (menu->selected == 0)
401 return;
403 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
405 menu->selected = 0;
407 while (TRUE)
409 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
411 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
412 menu->selected++;
413 else
414 break;
417 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
420 /* --------------------------------------------------------------------------------------------- */
422 static void
423 menubar_last (WMenuBar * menubar)
425 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
426 const unsigned int len = g_list_length (menu->entries);
427 menu_entry_t *entry;
429 if (menu->selected == len - 1)
430 return;
432 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
434 menu->selected = len;
438 menu->selected--;
439 entry = MENUENTRY (g_list_nth_data (menu->entries, menu->selected));
441 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
443 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
446 /* --------------------------------------------------------------------------------------------- */
448 static int
449 menubar_handle_key (WMenuBar * menubar, int key)
451 /* Lowercase */
452 if (isascii (key))
453 key = g_ascii_tolower (key);
455 if (is_abort_char (key))
457 menubar_finish (menubar);
458 return 1;
461 /* menubar help or menubar navigation */
462 switch (key)
464 case KEY_F (1):
466 ev_help_t event_data = { NULL, NULL };
468 if (menubar->is_dropped)
469 event_data.node =
470 MENU (g_list_nth_data (menubar->menu, menubar->selected))->help_node;
471 else
472 event_data.node = "[Menu Bar]";
474 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
475 menubar_draw (menubar);
476 return 1;
478 case KEY_LEFT:
479 case XCTRL ('b'):
480 menubar_left (menubar);
481 return 1;
483 case KEY_RIGHT:
484 case XCTRL ('f'):
485 menubar_right (menubar);
486 return 1;
489 if (!menubar->is_dropped)
491 GList *i;
493 /* drop menu by hotkey */
494 for (i = menubar->menu; i != NULL; i = g_list_next (i))
496 menu_t *menu = MENU (i->data);
498 if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
500 menubar_drop (menubar, g_list_position (menubar->menu, i));
501 return 1;
505 /* drop menu by Enter or Dowwn key */
506 if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
507 menubar_drop (menubar, menubar->selected);
509 return 1;
513 menu_t *menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
514 GList *i;
516 /* execute menu command by hotkey */
517 for (i = menu->entries; i != NULL; i = g_list_next (i))
519 const menu_entry_t *entry = MENUENTRY (i->data);
521 if ((entry != NULL) && (entry->command != CK_IgnoreKey)
522 && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
524 menu->selected = g_list_position (menu->entries, i);
525 menubar_execute (menubar);
526 return 1;
530 /* menu execute by Enter or menu navigation */
531 switch (key)
533 case KEY_ENTER:
534 case '\n':
535 menubar_execute (menubar);
536 return 1;
538 case KEY_HOME:
539 case ALT ('<'):
540 menubar_first (menubar);
541 break;
543 case KEY_END:
544 case ALT ('>'):
545 menubar_last (menubar);
546 break;
548 case KEY_DOWN:
549 case XCTRL ('n'):
550 menubar_down (menubar);
551 break;
553 case KEY_UP:
554 case XCTRL ('p'):
555 menubar_up (menubar);
556 break;
560 return 0;
563 /* --------------------------------------------------------------------------------------------- */
565 static cb_ret_t
566 menubar_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
568 WMenuBar *menubar = MENUBAR (w);
570 switch (msg)
572 /* We do not want the focus unless we have been activated */
573 case MSG_FOCUS:
574 if (!menubar->is_active)
575 return MSG_NOT_HANDLED;
577 /* Trick to get all the mouse events */
578 w->lines = LINES;
580 /* Trick to get all of the hotkeys */
581 widget_want_hotkey (w, 1);
582 menubar_draw (menubar);
583 return MSG_HANDLED;
585 /* We don't want the buttonbar to activate while using the menubar */
586 case MSG_HOTKEY:
587 case MSG_KEY:
588 if (menubar->is_active)
590 menubar_handle_key (menubar, parm);
591 return MSG_HANDLED;
593 return MSG_NOT_HANDLED;
595 case MSG_CURSOR:
596 /* Put the cursor in a suitable place */
597 return MSG_NOT_HANDLED;
599 case MSG_UNFOCUS:
600 return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
602 case MSG_DRAW:
603 if (menubar->is_visible)
605 menubar_draw (menubar);
606 return MSG_HANDLED;
608 /* fall through */
610 case MSG_RESIZE:
611 /* try show menu after screen resize */
612 send_message (w, sender, MSG_FOCUS, 0, data);
613 return MSG_HANDLED;
616 case MSG_DESTROY:
617 menubar_set_menu (menubar, NULL);
618 return MSG_HANDLED;
620 default:
621 return widget_default_callback (w, sender, msg, parm, data);
625 /* --------------------------------------------------------------------------------------------- */
627 static int
628 menubar_event (Gpm_Event * event, void *data)
630 WMenuBar *menubar = MENUBAR (data);
631 Widget *w = WIDGET (data);
632 gboolean was_active = TRUE;
633 int left_x, right_x, bottom_y;
634 menu_t *menu;
635 Gpm_Event local;
637 if (!mouse_global_in_widget (event, w))
638 return MOU_UNHANDLED;
640 /* ignore unsupported events */
641 if ((event->type & (GPM_UP | GPM_DOWN | GPM_DRAG)) == 0)
642 return MOU_NORMAL;
644 /* ignore wheel events if menu is inactive */
645 if (!menubar->is_active && ((event->buttons & (GPM_B_MIDDLE | GPM_B_UP | GPM_B_DOWN)) != 0))
646 return MOU_NORMAL;
648 local = mouse_get_local (event, w);
650 if (local.y == 1 && (local.type & GPM_UP) != 0)
651 return MOU_NORMAL;
653 if (!menubar->is_dropped)
655 menubar->previous_widget = dlg_get_current_widget_id (w->owner);
656 menubar->is_active = TRUE;
657 menubar->is_dropped = TRUE;
658 was_active = FALSE;
661 /* Mouse operations on the menubar */
662 if (local.y == 1 || !was_active)
664 /* wheel events on menubar */
665 if ((local.buttons & GPM_B_UP) != 0)
666 menubar_left (menubar);
667 else if ((local.buttons & GPM_B_DOWN) != 0)
668 menubar_right (menubar);
669 else
671 const unsigned int len = g_list_length (menubar->menu);
672 unsigned int new_selection = 0;
674 while ((new_selection < len)
675 && (local.x > MENU (g_list_nth_data (menubar->menu, new_selection))->start_x))
676 new_selection++;
678 if (new_selection != 0) /* Don't set the invalid value -1 */
679 new_selection--;
681 if (!was_active)
683 menubar->selected = new_selection;
684 dlg_select_widget (menubar);
686 else
688 menubar_remove (menubar);
689 menubar->selected = new_selection;
691 menubar_draw (menubar);
693 return MOU_NORMAL;
696 if (!menubar->is_dropped || (local.y < 2))
697 return MOU_NORMAL;
699 /* middle click -- everywhere */
700 if (((local.buttons & GPM_B_MIDDLE) != 0) && ((local.type & GPM_DOWN) != 0))
702 menubar_execute (menubar);
703 return MOU_NORMAL;
706 /* the mouse operation is on the menus or it is not */
707 menu = MENU (g_list_nth_data (menubar->menu, menubar->selected));
708 left_x = menu->start_x;
709 right_x = left_x + menu->max_entry_len + 3;
710 if (right_x > w->cols)
712 left_x = w->cols - menu->max_entry_len - 3;
713 right_x = w->cols;
716 bottom_y = g_list_length (menu->entries) + 3;
718 if ((local.x >= left_x) && (local.x <= right_x) && (local.y <= bottom_y))
720 int pos = local.y - 3;
721 const menu_entry_t *entry = MENUENTRY (g_list_nth_data (menu->entries, pos));
723 /* mouse wheel */
724 if ((local.buttons & GPM_B_UP) != 0 && (local.type & GPM_DOWN) != 0)
726 menubar_up (menubar);
727 return MOU_NORMAL;
729 if ((local.buttons & GPM_B_DOWN) != 0 && (local.type & GPM_DOWN) != 0)
731 menubar_down (menubar);
732 return MOU_NORMAL;
735 /* ignore events above and below dropped down menu */
736 if ((pos < 0) || (pos >= bottom_y - 3))
737 return MOU_NORMAL;
739 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
741 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
742 menu->selected = pos;
743 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
745 if ((event->type & GPM_UP) != 0)
746 menubar_execute (menubar);
749 else if (((local.type & GPM_DOWN) != 0) && ((local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0))
751 /* use click not wheel to close menu */
752 menubar_finish (menubar);
755 return MOU_NORMAL;
758 /* --------------------------------------------------------------------------------------------- */
759 /*** public functions ****************************************************************************/
760 /* --------------------------------------------------------------------------------------------- */
762 menu_entry_t *
763 menu_entry_create (const char *name, unsigned long command)
765 menu_entry_t *entry;
767 entry = g_new (menu_entry_t, 1);
768 entry->first_letter = ' ';
769 entry->text = parse_hotkey (name);
770 entry->command = command;
771 entry->shortcut = NULL;
773 return entry;
776 /* --------------------------------------------------------------------------------------------- */
778 void
779 menu_entry_free (menu_entry_t * entry)
781 if (entry != NULL)
783 release_hotkey (entry->text);
784 g_free (entry->shortcut);
785 g_free (entry);
789 /* --------------------------------------------------------------------------------------------- */
791 menu_t *
792 create_menu (const char *name, GList * entries, const char *help_node)
794 menu_t *menu;
796 menu = g_new (menu_t, 1);
797 menu->start_x = 0;
798 menu->text = parse_hotkey (name);
799 menu->entries = entries;
800 menu->max_entry_len = 1;
801 menu->max_hotkey_len = 0;
802 menu->selected = 0;
803 menu->help_node = g_strdup (help_node);
805 return menu;
808 /* --------------------------------------------------------------------------------------------- */
810 void
811 menu_set_name (menu_t * menu, const char *name)
813 release_hotkey (menu->text);
814 menu->text = parse_hotkey (name);
817 /* --------------------------------------------------------------------------------------------- */
819 void
820 destroy_menu (menu_t * menu)
822 release_hotkey (menu->text);
823 g_list_foreach (menu->entries, (GFunc) menu_entry_free, NULL);
824 g_list_free (menu->entries);
825 g_free (menu->help_node);
826 g_free (menu);
829 /* --------------------------------------------------------------------------------------------- */
831 WMenuBar *
832 menubar_new (int y, int x, int cols, GList * menu)
834 WMenuBar *menubar;
835 Widget *w;
837 menubar = g_new0 (WMenuBar, 1);
838 w = WIDGET (menubar);
839 init_widget (w, y, x, 1, cols, menubar_callback, menubar_event);
841 menubar->is_visible = TRUE; /* by default */
842 widget_want_cursor (w, FALSE);
843 menubar_set_menu (menubar, menu);
845 return menubar;
848 /* --------------------------------------------------------------------------------------------- */
850 void
851 menubar_set_menu (WMenuBar * menubar, GList * menu)
853 /* delete previous menu */
854 if (menubar->menu != NULL)
856 g_list_foreach (menubar->menu, (GFunc) destroy_menu, NULL);
857 g_list_free (menubar->menu);
859 /* add new menu */
860 menubar->is_active = FALSE;
861 menubar->is_dropped = FALSE;
862 menubar->menu = menu;
863 menubar->selected = 0;
864 menubar_arrange (menubar);
867 /* --------------------------------------------------------------------------------------------- */
869 void
870 menubar_add_menu (WMenuBar * menubar, menu_t * menu)
872 if (menu != NULL)
874 menu_arrange (menu, WIDGET (menubar)->owner->get_shortcut);
875 menubar->menu = g_list_append (menubar->menu, menu);
878 menubar_arrange (menubar);
881 /* --------------------------------------------------------------------------------------------- */
883 * Properly space menubar items. Should be called when menubar is created
884 * and also when widget width is changed (i.e. upon xterm resize).
887 void
888 menubar_arrange (WMenuBar * menubar)
890 int start_x = 1;
891 GList *i;
892 int gap;
894 if (menubar->menu == NULL)
895 return;
897 gap = WIDGET (menubar)->cols - 2;
899 /* First, calculate gap between items... */
900 for (i = menubar->menu; i != NULL; i = g_list_next (i))
902 menu_t *menu = MENU (i->data);
904 /* preserve length here, to be used below */
905 menu->start_x = hotkey_width (menu->text) + 2;
906 gap -= menu->start_x;
909 if (g_list_next (menubar->menu) == NULL)
910 gap = 1;
911 else
912 gap /= (g_list_length (menubar->menu) - 1);
914 if (gap <= 0)
916 /* We are out of luck - window is too narrow... */
917 gap = 1;
919 else if (gap >= 3)
920 gap = 3;
922 /* ...and now fix start positions of menubar items */
923 for (i = menubar->menu; i != NULL; i = g_list_next (i))
925 menu_t *menu = MENU (i->data);
926 int len = menu->start_x;
928 menu->start_x = start_x;
929 start_x += len + gap;
933 /* --------------------------------------------------------------------------------------------- */
934 /** Find MenuBar widget in the dialog */
936 WMenuBar *
937 find_menubar (const WDialog * h)
939 return MENUBAR (find_widget_type (h, menubar_callback));
942 /* --------------------------------------------------------------------------------------------- */