Add useful macros for widget type cast.
[midnight-commander.git] / lib / widget / menu.c
blob54f4251f0bc8818b92354a5fe1cecbd6192526e4
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 /*** file scope type declarations ****************************************************************/
54 /*** file scope variables ************************************************************************/
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
59 static void
60 menu_arrange (Menu * menu, dlg_shortcut_str get_shortcut)
62 if (menu != NULL)
64 GList *i;
65 size_t max_shortcut_len = 0;
67 menu->max_entry_len = 1;
68 menu->max_hotkey_len = 1;
70 for (i = menu->entries; i != NULL; i = g_list_next (i))
72 menu_entry_t *entry = i->data;
74 if (entry != NULL)
76 size_t len;
78 len = (size_t) hotkey_width (entry->text);
79 menu->max_hotkey_len = max (menu->max_hotkey_len, len);
81 if (get_shortcut != NULL)
82 entry->shortcut = get_shortcut (entry->command);
84 if (entry->shortcut != NULL)
86 len = (size_t) str_term_width1 (entry->shortcut);
87 max_shortcut_len = max (max_shortcut_len, len);
92 menu->max_entry_len = menu->max_hotkey_len + max_shortcut_len;
96 /* --------------------------------------------------------------------------------------------- */
98 static void
99 menubar_paint_idx (WMenuBar * menubar, unsigned int idx, int color)
101 Widget *w = WIDGET (menubar);
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) w->cols)
108 x = w->cols - menu->max_entry_len - 4;
110 if (entry == NULL)
112 /* menu separator */
113 tty_setcolor (MENU_ENTRY_COLOR);
115 widget_move (w, y, x - 1);
116 tty_print_alt_char (ACS_LTEE, FALSE);
117 tty_draw_hline (w->y + y, w->x + x, ACS_HLINE, menu->max_entry_len + 3);
118 widget_move (w, y, x + menu->max_entry_len + 3);
119 tty_print_alt_char (ACS_RTEE, FALSE);
121 else
123 int yt, xt;
125 /* menu text */
126 tty_setcolor (color);
127 widget_move (w, y, x);
128 tty_print_char ((unsigned char) entry->first_letter);
129 tty_getyx (&yt, &xt);
130 tty_draw_hline (yt, xt, ' ', menu->max_entry_len + 2); /* clear line */
131 tty_print_string (entry->text.start);
133 if (entry->text.hotkey != NULL)
135 tty_setcolor (color == MENU_SELECTED_COLOR ? MENU_HOTSEL_COLOR : MENU_HOT_COLOR);
136 tty_print_string (entry->text.hotkey);
137 tty_setcolor (color);
140 if (entry->text.end != NULL)
141 tty_print_string (entry->text.end);
143 if (entry->shortcut != NULL)
145 widget_move (w, y, x + menu->max_hotkey_len + 3);
146 tty_print_string (entry->shortcut);
149 /* move cursor to the start of entry text */
150 widget_move (w, y, x + 1);
154 /* --------------------------------------------------------------------------------------------- */
156 static void
157 menubar_draw_drop (WMenuBar * menubar)
159 Widget *w = WIDGET (menubar);
160 const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
161 const unsigned int count = g_list_length (menu->entries);
162 int column = menu->start_x - 1;
163 unsigned int i;
165 if (column + menu->max_entry_len + 5 > (gsize) w->cols)
166 column = w->cols - menu->max_entry_len - 5;
168 tty_setcolor (MENU_ENTRY_COLOR);
169 draw_box (w->owner, w->y + 1, w->x + column, count + 2, menu->max_entry_len + 5, FALSE);
171 for (i = 0; i < count; i++)
172 menubar_paint_idx (menubar, i,
173 i == menu->selected ? MENU_SELECTED_COLOR : MENU_ENTRY_COLOR);
176 /* --------------------------------------------------------------------------------------------- */
178 static void
179 menubar_set_color (WMenuBar * menubar, gboolean current, gboolean hotkey)
181 if (!menubar->is_active)
182 tty_setcolor (MENU_INACTIVE_COLOR);
183 else if (current)
184 tty_setcolor (hotkey ? MENU_HOTSEL_COLOR : MENU_SELECTED_COLOR);
185 else
186 tty_setcolor (hotkey ? MENU_HOT_COLOR : MENU_ENTRY_COLOR);
189 /* --------------------------------------------------------------------------------------------- */
191 static void
192 menubar_draw (WMenuBar * menubar)
194 Widget *w = WIDGET (menubar);
195 GList *i;
197 /* First draw the complete menubar */
198 tty_setcolor (menubar->is_active ? MENU_ENTRY_COLOR : MENU_INACTIVE_COLOR);
199 tty_draw_hline (w->y, w->x, ' ', w->cols);
201 /* Now each one of the entries */
202 for (i = menubar->menu; i != NULL; i = g_list_next (i))
204 Menu *menu = i->data;
205 gboolean is_selected = (menubar->selected == (gsize) g_list_position (menubar->menu, i));
207 menubar_set_color (menubar, is_selected, FALSE);
208 widget_move (w, 0, menu->start_x);
210 tty_print_char (' ');
211 tty_print_string (menu->text.start);
213 if (menu->text.hotkey != NULL)
215 menubar_set_color (menubar, is_selected, TRUE);
216 tty_print_string (menu->text.hotkey);
217 menubar_set_color (menubar, is_selected, FALSE);
220 if (menu->text.end != NULL)
221 tty_print_string (menu->text.end);
223 tty_print_char (' ');
226 if (menubar->is_dropped)
227 menubar_draw_drop (menubar);
228 else
229 widget_move (w, 0,
230 ((Menu *) g_list_nth_data (menubar->menu, menubar->selected))->start_x);
233 /* --------------------------------------------------------------------------------------------- */
235 static void
236 menubar_remove (WMenuBar * menubar)
238 WDialog *h;
240 if (!menubar->is_dropped)
241 return;
243 /* HACK: before refresh the dialog, change the current widget to keep the order
244 of overlapped widgets. This is useful in multi-window editor.
245 In general, menubar should be a special object, not an ordinary widget
246 in the current dialog. */
247 h = WIDGET (menubar)->owner;
248 h->current = g_list_find (h->widgets, dlg_find_by_id (h, menubar->previous_widget));
250 menubar->is_dropped = FALSE;
251 do_refresh ();
252 menubar->is_dropped = TRUE;
254 /* restore current widget */
255 h->current = g_list_find (h->widgets, menubar);
258 /* --------------------------------------------------------------------------------------------- */
260 static void
261 menubar_left (WMenuBar * menubar)
263 menubar_remove (menubar);
264 if (menubar->selected == 0)
265 menubar->selected = g_list_length (menubar->menu) - 1;
266 else
267 menubar->selected--;
268 menubar_draw (menubar);
271 /* --------------------------------------------------------------------------------------------- */
273 static void
274 menubar_right (WMenuBar * menubar)
276 menubar_remove (menubar);
277 menubar->selected = (menubar->selected + 1) % g_list_length (menubar->menu);
278 menubar_draw (menubar);
281 /* --------------------------------------------------------------------------------------------- */
283 static void
284 menubar_finish (WMenuBar * menubar)
286 Widget *w = WIDGET (menubar);
288 menubar->is_dropped = FALSE;
289 menubar->is_active = FALSE;
290 w->lines = 1;
291 widget_want_hotkey (w, 0);
293 dlg_select_by_id (w->owner, menubar->previous_widget);
294 do_refresh ();
297 /* --------------------------------------------------------------------------------------------- */
299 static void
300 menubar_drop (WMenuBar * menubar, unsigned int selected)
302 menubar->is_dropped = TRUE;
303 menubar->selected = selected;
304 menubar_draw (menubar);
307 /* --------------------------------------------------------------------------------------------- */
309 static void
310 menubar_execute (WMenuBar * menubar)
312 const Menu *menu = g_list_nth_data (menubar->menu, menubar->selected);
313 const menu_entry_t *entry = g_list_nth_data (menu->entries, menu->selected);
315 if ((entry != NULL) && (entry->command != CK_IgnoreKey))
317 Widget *w = WIDGET (menubar);
319 mc_global.widget.is_right = (menubar->selected != 0);
320 menubar_finish (menubar);
321 send_message (w->owner, w, MSG_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 * sender, widget_msg_t msg, int parm, void *data)
547 WMenuBar *menubar = MENUBAR (w);
549 switch (msg)
551 /* We do not want the focus unless we have been activated */
552 case MSG_FOCUS:
553 if (!menubar->is_active)
554 return MSG_NOT_HANDLED;
556 /* Trick to get all the mouse events */
557 w->lines = LINES;
559 /* Trick to get all of the hotkeys */
560 widget_want_hotkey (w, 1);
561 menubar_draw (menubar);
562 return MSG_HANDLED;
564 /* We don't want the buttonbar to activate while using the menubar */
565 case MSG_HOTKEY:
566 case MSG_KEY:
567 if (menubar->is_active)
569 menubar_handle_key (menubar, parm);
570 return MSG_HANDLED;
572 return MSG_NOT_HANDLED;
574 case MSG_CURSOR:
575 /* Put the cursor in a suitable place */
576 return MSG_NOT_HANDLED;
578 case MSG_UNFOCUS:
579 return menubar->is_active ? MSG_NOT_HANDLED : MSG_HANDLED;
581 case MSG_DRAW:
582 if (menubar->is_visible)
584 menubar_draw (menubar);
585 return MSG_HANDLED;
587 /* fall through */
589 case MSG_RESIZE:
590 /* try show menu after screen resize */
591 send_message (w, sender, MSG_FOCUS, 0, data);
592 return MSG_HANDLED;
595 case MSG_DESTROY:
596 menubar_set_menu (menubar, NULL);
597 return MSG_HANDLED;
599 default:
600 return widget_default_callback (w, sender, msg, parm, data);
604 /* --------------------------------------------------------------------------------------------- */
606 static int
607 menubar_event (Gpm_Event * event, void *data)
609 WMenuBar *menubar = MENUBAR (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 local = mouse_get_local (event, w);
629 if (local.y == 1 && (local.type & GPM_UP) != 0)
630 return MOU_NORMAL;
632 if (!menubar->is_dropped)
634 menubar->previous_widget = dlg_get_current_widget_id (w->owner);
635 menubar->is_active = TRUE;
636 menubar->is_dropped = TRUE;
637 was_active = FALSE;
640 /* Mouse operations on the menubar */
641 if (local.y == 1 || !was_active)
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 > w->cols)
692 left_x = w->cols - menu->max_entry_len - 3;
693 right_x = w->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;
815 Widget *w;
817 menubar = g_new0 (WMenuBar, 1);
818 w = WIDGET (menubar);
819 init_widget (w, y, x, 1, cols, menubar_callback, menubar_event);
821 menubar->is_visible = TRUE; /* by default */
822 widget_want_cursor (w, FALSE);
823 menubar_set_menu (menubar, menu);
825 return menubar;
828 /* --------------------------------------------------------------------------------------------- */
830 void
831 menubar_set_menu (WMenuBar * menubar, GList * menu)
833 /* delete previous menu */
834 if (menubar->menu != NULL)
836 g_list_foreach (menubar->menu, (GFunc) destroy_menu, NULL);
837 g_list_free (menubar->menu);
839 /* add new menu */
840 menubar->is_active = FALSE;
841 menubar->is_dropped = FALSE;
842 menubar->menu = menu;
843 menubar->selected = 0;
844 menubar_arrange (menubar);
847 /* --------------------------------------------------------------------------------------------- */
849 void
850 menubar_add_menu (WMenuBar * menubar, Menu * menu)
852 if (menu != NULL)
854 menu_arrange (menu, WIDGET (menubar)->owner->get_shortcut);
855 menubar->menu = g_list_append (menubar->menu, menu);
858 menubar_arrange (menubar);
861 /* --------------------------------------------------------------------------------------------- */
863 * Properly space menubar items. Should be called when menubar is created
864 * and also when widget width is changed (i.e. upon xterm resize).
867 void
868 menubar_arrange (WMenuBar * menubar)
870 int start_x = 1;
871 GList *i;
872 int gap;
874 if (menubar->menu == NULL)
875 return;
877 gap = WIDGET (menubar)->cols - 2;
879 /* First, calculate gap between items... */
880 for (i = menubar->menu; i != NULL; i = g_list_next (i))
882 Menu *menu = (Menu *) i->data;
883 /* preserve length here, to be used below */
884 menu->start_x = hotkey_width (menu->text) + 2;
885 gap -= menu->start_x;
888 if (g_list_next (menubar->menu) == NULL)
889 gap = 1;
890 else
891 gap /= (g_list_length (menubar->menu) - 1);
893 if (gap <= 0)
895 /* We are out of luck - window is too narrow... */
896 gap = 1;
898 else if (gap >= 3)
899 gap = 3;
901 /* ...and now fix start positions of menubar items */
902 for (i = menubar->menu; i != NULL; i = g_list_next (i))
904 Menu *menu = (Menu *) i->data;
905 int len = menu->start_x;
907 menu->start_x = start_x;
908 start_x += len + gap;
912 /* --------------------------------------------------------------------------------------------- */
913 /** Find MenuBar widget in the dialog */
915 WMenuBar *
916 find_menubar (const WDialog * h)
918 return MENUBAR (find_widget_type (h, menubar_callback));
921 /* --------------------------------------------------------------------------------------------- */