Make menu always resizable.
[midnight-commander.git] / lib / widget / menu.c
blob545e49ec9239773886233318141e00eafd2c41d5
1 /*
2 Pulldown menu code
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software: you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation, either version 3 of the License,
13 or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /** \file menu.c
25 * \brief Source: pulldown menu code
28 #include <config.h>
30 #include <ctype.h>
31 #include <stdarg.h>
32 #include <string.h>
33 #include <sys/types.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/skin.h"
39 #include "lib/tty/mouse.h"
40 #include "lib/tty/key.h" /* key macros */
41 #include "lib/strutil.h"
42 #include "lib/widget.h"
43 #include "lib/event.h" /* mc_event_raise() */
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 #define RESIZABLE_MENUBAR 1
51 /*** file scope type declarations ****************************************************************/
53 /*** file scope variables ************************************************************************/
55 static cb_ret_t menubar_callback (Widget * w, widget_msg_t msg, int parm);
57 /*** file scope functions ************************************************************************/
58 /* --------------------------------------------------------------------------------------------- */
60 static void
61 menu_arrange (Menu * menu, dlg_shortcut_str get_shortcut)
63 if (menu != NULL)
65 GList *i;
66 size_t max_shortcut_len = 0;
68 menu->max_entry_len = 1;
69 menu->max_hotkey_len = 1;
71 for (i = menu->entries; i != NULL; i = g_list_next (i))
73 menu_entry_t *entry = i->data;
75 if (entry != NULL)
77 size_t len;
79 len = (size_t) hotkey_width (entry->text);
80 menu->max_hotkey_len = max (menu->max_hotkey_len, len);
82 if (get_shortcut != NULL)
83 entry->shortcut = get_shortcut (entry->command);
85 if (entry->shortcut != NULL)
87 len = (size_t) str_term_width1 (entry->shortcut);
88 max_shortcut_len = max (max_shortcut_len, len);
93 menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
97 /* --------------------------------------------------------------------------------------------- */
99 static void
100 menubar_paint_idx (WMenuBar * menubar, unsigned int idx, int color)
102 const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
103 const menu_entry_t *entry = g_list_nth_data (menu->entries, idx);
104 const int y = 2 + idx;
105 int x = menu->start_x;
107 if (x + menu->max_entry_len + 4 > (gsize) menubar->widget.cols)
108 x = menubar->widget.cols - menu->max_entry_len - 4;
110 if (entry == NULL)
112 /* menu separator */
113 tty_setcolor (MENU_ENTRY_COLOR);
115 widget_move (&menubar->widget, y, x - 1);
116 tty_print_alt_char (ACS_LTEE, FALSE);
118 tty_draw_hline (menubar->widget.y + y, menubar->widget.x + x,
119 ACS_HLINE, menu->max_entry_len + 3);
121 widget_move (&menubar->widget, y, x + menu->max_entry_len + 3);
122 tty_print_alt_char (ACS_RTEE, FALSE);
124 else
126 int yt, xt;
128 /* menu text */
129 tty_setcolor (color);
130 widget_move (&menubar->widget, y, x);
131 tty_print_char ((unsigned char) entry->first_letter);
132 tty_getyx (&yt, &xt);
133 tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2); /* clear line */
134 tty_print_string (entry->text.start);
136 if (entry->text.hotkey != NULL)
138 tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
139 tty_print_string (entry->text.hotkey);
140 tty_setcolor (color);
143 if (entry->text.end != NULL)
144 tty_print_string (entry->text.end);
146 if (entry->shortcut != NULL)
148 widget_move (&menubar->widget, y, x + menu->max_hotkey_len + 3);
149 tty_print_string (entry->shortcut);
152 /* move cursor to the start of entry text */
153 widget_move (&menubar->widget, y, x + 1);
157 /* --------------------------------------------------------------------------------------------- */
159 static void
160 menubar_draw_drop (WMenuBar * menubar)
162 const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
163 const unsigned int count = g_list_length (menu->entries);
164 int column = menu->start_x - 1;
165 unsigned int i;
167 if (column + menu->max_entry_len + 5 > (gsize) menubar->widget.cols)
168 column = menubar->widget.cols - menu->max_entry_len - 5;
170 tty_setcolor (MENU_ENTRY_COLOR);
171 draw_box (menubar->widget.owner,
172 menubar->widget.y + 1, menubar->widget.x + column,
173 count + 2, menu->max_entry_len + 5, FALSE);
175 for (i = 0; i < count; i++)
176 menubar_paint_idx (menubar, i,
177 i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
180 /* --------------------------------------------------------------------------------------------- */
182 static void
183 menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
185 if (!menubar->is_active)
186 tty_setcolor (MENU_INACTIVE_COLOR);
187 else if (current)
188 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
189 else
190 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
193 /* --------------------------------------------------------------------------------------------- */
195 static void
196 menubar_draw (WMenuBar * menubar)
198 GList *i;
200 /* First draw the complete menubar */
201 tty_setcolor (menubar->is_active ? MENU_ENTRY_COLOR : MENU_INACTIVE_COLOR);
202 tty_draw_hline (menubar->widget.y, menubar->widget.x, ' ', menubar->widget.cols);
204 /* Now each one of the entries */
205 for (i = menubar->menu; i != NULL; i = g_list_next (i))
207 Menu *menu = i->data;
208 gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
210 menubar_set_color (menubar, is_selected, FALSE);
211 widget_move (&menubar->widget, 0, menu->start_x);
213 tty_print_char (' ');
214 tty_print_string (menu->text.start);
216 if (menu->text.hotkey != NULL)
218 menubar_set_color (menubar, is_selected, TRUE);
219 tty_print_string (menu->text.hotkey);
220 menubar_set_color (menubar, is_selected, FALSE);
223 if (menu->text.end != NULL)
224 tty_print_string (menu->text.end);
226 tty_print_char (' ');
229 if (menubar->is_dropped)
230 menubar_draw_drop (menubar);
231 else
232 widget_move (&menubar->widget, 0,
233 ((Menu *) g_list_nth_data (menubar->menu, menubar->selected))->start_x);
236 /* --------------------------------------------------------------------------------------------- */
238 static void
239 menubar_remove (WMenuBar * menubar)
241 Dlg_head *h;
243 if (!menubar->is_dropped)
244 return;
246 /* HACK: before refresh the dialog, change the current widget to keep the order
247 of overlapped widgets. This is useful in multi-window editor.
248 In general, menubar should be a special object, not an ordinary widget
249 in the current dialog. */
250 h = menubar->widget.owner;
251 h->current = g_list_find (h->widgets, dlg_find_by_id (h, menubar->previous_widget));
253 menubar->is_dropped = FALSE;
254 do_refresh ();
255 menubar->is_dropped = TRUE;
257 /* restore current widget */
258 h->current = g_list_find (h->widgets, menubar);
261 /* --------------------------------------------------------------------------------------------- */
263 static void
264 menubar_left (WMenuBar * menubar)
266 menubar_remove (menubar);
267 if (menubar->selected == 0)
268 menubar->selected = g_list_length (menubar->menu) - 1;
269 else
270 menubar->selected--;
271 menubar_draw (menubar);
274 /* --------------------------------------------------------------------------------------------- */
276 static void
277 menubar_right (WMenuBar * menubar)
279 menubar_remove (menubar);
280 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
281 menubar_draw (menubar);
284 /* --------------------------------------------------------------------------------------------- */
286 static void
287 menubar_finish (WMenuBar * menubar)
289 menubar->is_dropped = FALSE;
290 menubar->is_active = FALSE;
291 menubar->widget.lines = 1;
292 widget_want_hotkey (menubar->widget, 0);
294 dlg_select_by_id (menubar->widget.owner, menubar->previous_widget);
295 do_refresh ();
298 /* --------------------------------------------------------------------------------------------- */
300 static void
301 menubar_drop (WMenuBar * menubar, unsigned int selected)
303 menubar->is_dropped = TRUE;
304 menubar->selected = selected;
305 menubar_draw (menubar);
308 /* --------------------------------------------------------------------------------------------- */
310 static void
311 menubar_execute (WMenuBar * menubar)
313 const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
314 const menu_entry_t *entry = g_list_nth_data (menu->entries, menu->selected);
316 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
318 mc_global.is_right = (menubar->selected != 0);
319 menubar_finish (menubar);
320 menubar->widget.owner->callback (menubar->widget.owner, &menubar->widget,
321 DLG_ACTION, entry->command, NULL);
322 do_refresh ();
326 /* --------------------------------------------------------------------------------------------- */
328 static void
329 menubar_down (WMenuBar * menubar)
331 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
332 const unsigned int len = g_list_length (menu->entries);
333 menu_entry_t *entry;
335 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
339 menu->selected = (menu->selected + 1) % len;
340 entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
342 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
344 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
347 /* --------------------------------------------------------------------------------------------- */
349 static void
350 menubar_up (WMenuBar * menubar)
352 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 if (menu->selected == 0)
361 menu->selected = len - 1;
362 else
363 menu->selected--;
364 entry = (menu_entry_t *) 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_first (WMenuBar * menubar)
376 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
377 menu_entry_t *entry;
379 if (menu->selected == 0)
380 return;
382 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
384 menu->selected = 0;
386 while (TRUE)
388 entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
390 if ((entry == NULL) || (entry->command == CK_IgnoreKey))
391 menu->selected++;
392 else
393 break;
396 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
399 /* --------------------------------------------------------------------------------------------- */
401 static void
402 menubar_last (WMenuBar * menubar)
404 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
405 const unsigned int len = g_list_length (menu->entries);
406 menu_entry_t *entry;
408 if (menu->selected == len - 1)
409 return;
411 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
413 menu->selected = len;
417 menu->selected--;
418 entry = (menu_entry_t *) g_list_nth_data (menu->entries, menu->selected);
420 while ((entry == NULL) || (entry->command == CK_IgnoreKey));
422 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
425 /* --------------------------------------------------------------------------------------------- */
427 static int
428 menubar_handle_key (WMenuBar * menubar, int key)
430 /* Lowercase */
431 if (isascii (key))
432 key = g_ascii_tolower (key);
434 if (is_abort_char (key))
436 menubar_finish (menubar);
437 return 1;
440 /* menubar help or menubar navigation */
441 switch (key)
443 case KEY_F (1):
445 ev_help_t event_data = { NULL, NULL };
447 if (menubar->is_dropped)
448 event_data.node =
449 ((Menu *) g_list_nth_data (menubar->menu, menubar->selected))->help_node;
450 else
451 event_data.node = "[Menu Bar]";
453 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
454 menubar_draw (menubar);
455 return 1;
457 case KEY_LEFT:
458 case XCTRL ('b'):
459 menubar_left (menubar);
460 return 1;
462 case KEY_RIGHT:
463 case XCTRL ('f'):
464 menubar_right (menubar);
465 return 1;
468 if (!menubar->is_dropped)
470 GList *i;
472 /* drop menu by hotkey */
473 for (i = menubar->menu; i != NULL; i = g_list_next (i))
475 Menu *menu = i->data;
477 if ((menu->text.hotkey != NULL) && (key == g_ascii_tolower (menu->text.hotkey[0])))
479 menubar_drop (menubar, g_list_position (menubar->menu, i));
480 return 1;
484 /* drop menu by Enter or Dowwn key */
485 if (key == KEY_ENTER || key == XCTRL ('n') || key == KEY_DOWN || key == '\n')
486 menubar_drop (menubar, menubar->selected);
488 return 1;
492 Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
493 GList *i;
495 /* execute menu command by hotkey */
496 for (i = menu->entries; i != NULL; i = g_list_next (i))
498 const menu_entry_t *entry = i->data;
500 if ((entry != NULL) && (entry->command != CK_IgnoreKey)
501 && (entry->text.hotkey != NULL) && (key == g_ascii_tolower (entry->text.hotkey[0])))
503 menu->selected = g_list_position (menu->entries, i);
504 menubar_execute (menubar);
505 return 1;
509 /* menu execute by Enter or menu navigation */
510 switch (key)
512 case KEY_ENTER:
513 case '\n':
514 menubar_execute (menubar);
515 return 1;
517 case KEY_HOME:
518 case ALT ('<'):
519 menubar_first (menubar);
520 break;
522 case KEY_END:
523 case ALT ('>'):
524 menubar_last (menubar);
525 break;
527 case KEY_DOWN:
528 case XCTRL ('n'):
529 menubar_down (menubar);
530 break;
532 case KEY_UP:
533 case XCTRL ('p'):
534 menubar_up (menubar);
535 break;
539 return 0;
542 /* --------------------------------------------------------------------------------------------- */
544 static cb_ret_t
545 menubar_callback (Widget * w, widget_msg_t msg, int parm)
547 WMenuBar *menubar = (WMenuBar *) w;
549 switch (msg)
551 /* We do not want the focus unless we have been activated */
552 case WIDGET_FOCUS:
553 if (!menubar->is_active)
554 return MSG_NOT_HANDLED;
556 /* Trick to get all the mouse events */
557 menubar->widget.lines = LINES;
559 /* Trick to get all of the hotkeys */
560 widget_want_hotkey (menubar->widget, 1);
561 menubar_draw (menubar);
562 return MSG_HANDLED;
564 /* We don't want the buttonbar to activate while using the menubar */
565 case WIDGET_HOTKEY:
566 case WIDGET_KEY:
567 if (menubar->is_active)
569 menubar_handle_key (menubar, parm);
570 return MSG_HANDLED;
572 return MSG_NOT_HANDLED;
574 case WIDGET_CURSOR:
575 /* Put the cursor in a suitable place */
576 return MSG_NOT_HANDLED;
578 case WIDGET_UNFOCUS:
579 return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
581 case WIDGET_DRAW:
582 if (menubar->is_visible)
584 menubar_draw (menubar);
585 return MSG_HANDLED;
587 /* fall through */
589 case WIDGET_RESIZED:
590 /* try show menu after screen resize */
591 send_message (w, WIDGET_FOCUS, 0);
592 return MSG_HANDLED;
595 case WIDGET_DESTROY:
596 menubar_set_menu (menubar, NULL);
597 return MSG_HANDLED;
599 default:
600 return default_proc (msg, parm);
604 /* --------------------------------------------------------------------------------------------- */
606 static int
607 menubar_event (Gpm_Event * event, void *data)
609 WMenuBar *menubar = (WMenuBar *) data;
610 Widget *w = (Widget *) data;
611 gboolean was_active = TRUE;
612 int left_x, right_x, bottom_y;
613 Menu *menu;
614 Gpm_Event local;
616 if (!mouse_global_in_widget (event, w))
617 return MOU_UNHANDLED;
619 /* ignore unsupported events */
620 if ((event->type & (GPM_UP | GPM_DOWN | GPM_DRAG)) == 0)
621 return MOU_NORMAL;
623 /* ignore wheel events if menu is inactive */
624 if (!menubar->is_active && ((event->buttons & (GPM_B_MIDDLE | GPM_B_UP | GPM_B_DOWN)) != 0))
625 return MOU_NORMAL;
627 if (!menubar->is_dropped)
629 menubar->previous_widget = dlg_get_current_widget_id (w->owner);
630 menubar->is_active = TRUE;
631 menubar->is_dropped = TRUE;
632 was_active = FALSE;
635 local = mouse_get_local (event, w);
637 /* Mouse operations on the menubar */
638 if (local.y == 1 || !was_active)
640 if ((local.type & GPM_UP) != 0)
641 return MOU_NORMAL;
643 /* wheel events on menubar */
644 if ((local.buttons & GPM_B_UP) != 0)
645 menubar_left (menubar);
646 else if ((local.buttons & GPM_B_DOWN) != 0)
647 menubar_right (menubar);
648 else
650 const unsigned int len = g_list_length (menubar->menu);
651 unsigned int new_selection = 0;
653 while ((new_selection < len)
654 && (local.x > ((Menu *) g_list_nth_data (menubar->menu,
655 new_selection))->start_x))
656 new_selection++;
658 if (new_selection != 0) /* Don't set the invalid value -1 */
659 new_selection--;
661 if (!was_active)
663 menubar->selected = new_selection;
664 dlg_select_widget (menubar);
666 else
668 menubar_remove (menubar);
669 menubar->selected = new_selection;
671 menubar_draw (menubar);
673 return MOU_NORMAL;
676 if (!menubar->is_dropped || (local.y < 2))
677 return MOU_NORMAL;
679 /* middle click -- everywhere */
680 if (((local.buttons & GPM_B_MIDDLE) != 0) && ((local.type & GPM_DOWN) != 0))
682 menubar_execute (menubar);
683 return MOU_NORMAL;
686 /* the mouse operation is on the menus or it is not */
687 menu = (Menu *) g_list_nth_data (menubar->menu, menubar->selected);
688 left_x = menu->start_x;
689 right_x = left_x + menu->max_entry_len + 3;
690 if (right_x > menubar->widget.cols)
692 left_x = menubar->widget.cols - menu->max_entry_len - 3;
693 right_x = menubar->widget.cols;
696 bottom_y = g_list_length (menu->entries) + 3;
698 if ((local.x >= left_x) && (local.x <= right_x) && (local.y <= bottom_y))
700 int pos = local.y - 3;
701 const menu_entry_t *entry = g_list_nth_data (menu->entries, pos);
703 /* mouse wheel */
704 if ((local.buttons & GPM_B_UP) != 0 && (local.type & GPM_DOWN) != 0)
706 menubar_up (menubar);
707 return MOU_NORMAL;
709 if ((local.buttons & GPM_B_DOWN) != 0 && (local.type & GPM_DOWN) != 0)
711 menubar_down (menubar);
712 return MOU_NORMAL;
715 /* ignore events above and below dropped down menu */
716 if ((pos < 0) || (pos >= bottom_y - 3))
717 return MOU_NORMAL;
719 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
721 menubar_paint_idx (menubar, menu->selected, MENU_ENTRY_COLOR);
722 menu->selected = pos;
723 menubar_paint_idx (menubar, menu->selected, MENU_SELECTED_COLOR);
725 if ((event->type & GPM_UP) != 0)
726 menubar_execute (menubar);
729 else if (((local.type & GPM_DOWN) != 0) && ((local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0))
731 /* use click not wheel to close menu */
732 menubar_finish (menubar);
735 return MOU_NORMAL;
738 /* --------------------------------------------------------------------------------------------- */
739 /*** public functions ****************************************************************************/
740 /* --------------------------------------------------------------------------------------------- */
742 menu_entry_t *
743 menu_entry_create (const char *name, unsigned long command)
745 menu_entry_t *entry;
747 entry = g_new (menu_entry_t, 1);
748 entry->first_letter = ' ';
749 entry->text = parse_hotkey (name);
750 entry->command = command;
751 entry->shortcut = NULL;
753 return entry;
756 /* --------------------------------------------------------------------------------------------- */
758 void
759 menu_entry_free (menu_entry_t * entry)
761 if (entry != NULL)
763 release_hotkey (entry->text);
764 g_free (entry->shortcut);
765 g_free (entry);
769 /* --------------------------------------------------------------------------------------------- */
771 Menu *
772 create_menu (const char *name, GList * entries, const char *help_node)
774 Menu *menu;
776 menu = g_new (Menu, 1);
777 menu->start_x = 0;
778 menu->text = parse_hotkey (name);
779 menu->entries = entries;
780 menu->max_entry_len = 1;
781 menu->max_hotkey_len = 0;
782 menu->selected = 0;
783 menu->help_node = g_strdup (help_node);
785 return menu;
788 /* --------------------------------------------------------------------------------------------- */
790 void
791 menu_set_name (Menu * menu, const char *name)
793 release_hotkey (menu->text);
794 menu->text = parse_hotkey (name);
797 /* --------------------------------------------------------------------------------------------- */
799 void
800 destroy_menu (Menu * menu)
802 release_hotkey (menu->text);
803 g_list_foreach (menu->entries, (GFunc) menu_entry_free, NULL);
804 g_list_free (menu->entries);
805 g_free (menu->help_node);
806 g_free (menu);
809 /* --------------------------------------------------------------------------------------------- */
811 WMenuBar *
812 menubar_new (int y, int x, int cols, GList * menu)
814 WMenuBar *menubar;
816 menubar = g_new0 (WMenuBar, 1);
817 init_widget (&menubar->widget, y, x, 1, cols, menubar_callback, menubar_event);
818 widget_want_cursor (menubar->widget, FALSE);
819 menubar->is_visible = TRUE; /* by default */
820 menubar_set_menu (menubar, menu);
822 return menubar;
825 /* --------------------------------------------------------------------------------------------- */
827 void
828 menubar_set_menu (WMenuBar * menubar, GList * menu)
830 /* delete previous menu */
831 if (menubar->menu != NULL)
833 g_list_foreach (menubar->menu, (GFunc) destroy_menu, NULL);
834 g_list_free (menubar->menu);
836 /* add new menu */
837 menubar->is_active = FALSE;
838 menubar->is_dropped = FALSE;
839 menubar->menu = menu;
840 menubar->selected = 0;
841 menubar_arrange (menubar);
844 /* --------------------------------------------------------------------------------------------- */
846 void
847 menubar_add_menu (WMenuBar * menubar, Menu * menu)
849 if (menu != NULL)
851 menu_arrange (menu, menubar->widget.owner->get_shortcut);
852 menubar->menu = g_list_append (menubar->menu, menu);
855 menubar_arrange (menubar);
858 /* --------------------------------------------------------------------------------------------- */
860 * Properly space menubar items. Should be called when menubar is created
861 * and also when widget width is changed (i.e. upon xterm resize).
864 void
865 menubar_arrange (WMenuBar * menubar)
867 int start_x = 1;
868 GList *i;
869 int gap;
871 if (menubar->menu == NULL)
872 return;
874 #ifndef RESIZABLE_MENUBAR
875 gap = 3;
877 for (i = menubar->menu; i != NULL; i = g_list_next (i))
879 Menu *menu = i->data;
880 int len = hotkey_width (menu->text) + 2;
882 menu->start_x = start_x;
883 start_x += len + gap;
885 #else /* RESIZABLE_MENUBAR */
886 gap = menubar->widget.cols - 2;
888 /* First, calculate gap between items... */
889 for (i = menubar->menu; i != NULL; i = g_list_next (i))
891 Menu *menu = i->data;
892 /* preserve length here, to be used below */
893 menu->start_x = hotkey_width (menu->text) + 2;
894 gap -= menu->start_x;
897 if (g_list_next (menubar->menu) == NULL)
898 gap = 1;
899 else
900 gap /= (g_list_length (menubar->menu) - 1);
902 if (gap <= 0)
904 /* We are out of luck - window is too narrow... */
905 gap = 1;
908 /* ...and now fix start positions of menubar items */
909 for (i = menubar->menu; i != NULL; i = g_list_next (i))
911 Menu *menu = i->data;
912 int len = menu->start_x;
914 menu->start_x = start_x;
915 start_x += len + gap;
917 #endif /* RESIZABLE_MENUBAR */
920 /* --------------------------------------------------------------------------------------------- */
921 /** Find MenuBar widget in the dialog */
923 WMenuBar *
924 find_menubar (const Dlg_head * h)
926 return (WMenuBar *) find_widget_type (h, menubar_callback);
929 /* --------------------------------------------------------------------------------------------- */