Exemple #1
0
        /// <summary>
        /// Recalculates menu size based on the menu items.
        /// </summary>
        ///
        private void ResizeToFitContents()
        {
            if (!MenuItems.StructureChanged)
            {
                return;
            }

            // Remove improperly placed separators
            //
            if (IsMainMenu)
            {
                // Remove all separators from horizontal menu
                //
                while (true)
                {
                    MenuItem mi = MenuItems.Find(item => item.Text == Separator);
                    if (mi == null)
                    {
                        break;
                    }

                    MenuItems.Remove(mi);
                }
            }
            else
            {
                // Remove separators at the first and last positions in vertical menu
                //
                while (MenuItems.Count > 0 && MenuItems[0].Text == Separator)
                {
                    MenuItems.RemoveAt(0);
                }

                while (MenuItems.Count > 0 &&
                       MenuItems[MenuItems.Count - 1].Text == Separator)
                {
                    MenuItems.RemoveAt(MenuItems.Count - 1);
                }
            }

            Current += 0; // revalidate current

            MenuItems.AcknolwedgeStructureChange();

            // Calculate maximum dimensions of menu items
            //
            TextWidth      = 0;
            ShortcutWidth  = 0;
            HasSubsubItems = false;
            int position = 0;

            foreach (MenuItem mi in MenuItems)
            {
                HasSubsubItems = HasSubsubItems || mi.MenuItems.Count > 0;

                if (IsMainMenu && mi.Text != Separator)
                {
                    position += mi.Text.Length
                                + LeftPadding + RightPadding + HorizontalSpacing;
                    TextWidth = position;
                }
                else if (!IsMainMenu)
                {
                    ++position;
                    TextWidth     = Math.Max(TextWidth, mi.Text.Length);
                    ShortcutWidth = Math.Max(ShortcutWidth, mi.VerboseShortcut.Length);
                }
            }

            // Reset position relative to master, if any
            //
            if (Master != null && Master.CurrentItem != null)
            {
                if (Master.IsMainMenu)
                {
                    Left = Master.Left + Master.GetPosition(Master.Current);
                    Top  = Master.Top + 2;
                }
                else
                {
                    Left = Master.Left + Master.Width + 1;
                    Top  = Master.Top + Master.GetPosition(Master.Current);
                }
            }

            // Finally, resize window to reflect menu items' dimensions
            //
            if (IsMainMenu)
            {
                Width  = Math.Max(Width, Math.Max(position, 1));
                Height = Math.Max(Height, 1);
            }
            else
            {
                int width = TextWidth + ShortcutWidth + LeftPadding + RightPadding
                            + (ShortcutWidth > 0 ? HorizontalSpacing : 0)
                            + (HasSubsubItems ? 1 : 0);

                if (AutoSize)
                {
                    Width  = Math.Max(Width, Math.Max(width, 1));
                    Height = Math.Max(Height, Math.Max(position, 1));
                }
                else
                {
                    ShortcutWidth = Math.Max(0, Width - width - HorizontalSpacing);
                }
            }
        }