Beispiel #1
0
        private void OnButtonClick(object sender, MouseEventArgs e)
        {
            // Only allow a single context menu at a time
            if (!_showingContextMenu)
            {
                // Get access to the controller, view and crumb item
                ViewDrawButton        viewButton = sender as ViewDrawButton;
                ButtonController      controller = viewButton.MouseController as ButtonController;
                KryptonBreadCrumbItem breadCrumb = controller.Tag as KryptonBreadCrumbItem;

                // Do we need to show a drop down menu?
                if (viewButton.DropDown && viewButton.SplitRectangle.Contains(e.Location))
                {
                    // Create a context menu with a items collection
                    KryptonContextMenu kcm = new KryptonContextMenu();

                    // Use same palette settings for context menu as the main control
                    kcm.Palette = _kryptonBreadCrumb.Palette;
                    if (kcm.Palette == null)
                    {
                        kcm.PaletteMode = _kryptonBreadCrumb.PaletteMode;
                    }

                    // Add an items collection as the root item of the context menu
                    KryptonContextMenuItems items = new KryptonContextMenuItems();
                    kcm.Items.Add(items);

                    // Store lookup between each menu item and the crumb it represents. Prevents
                    // needing to use the menu item tag for remembering association. Leaving the
                    // tag free for use by the user.
                    _menuItemToCrumb = new MenuItemToCrumb();

                    // Create a new menu item to represent each child crumb
                    foreach (KryptonBreadCrumbItem childCrumb in breadCrumb.Items)
                    {
                        KryptonContextMenuItem childMenu = new KryptonContextMenuItem();

                        // Store 1-to-1 association
                        _menuItemToCrumb.Add(childMenu, childCrumb);

                        // Copy across the display details of the child crumb item
                        childMenu.Text                  = childCrumb.ShortText;
                        childMenu.ExtraText             = childCrumb.LongText;
                        childMenu.Image                 = childCrumb.Image;
                        childMenu.ImageTransparentColor = childCrumb.ImageTransparentColor;
                        childMenu.Click                += new EventHandler(OnChildCrumbClick);

                        items.Items.Add(childMenu);
                    }

                    // Allow the user a chance to alter the menu contents or cancel it entirely
                    BreadCrumbMenuArgs bcma = new BreadCrumbMenuArgs(breadCrumb, kcm, KryptonContextMenuPositionH.Left, KryptonContextMenuPositionV.Below);
                    _kryptonBreadCrumb.OnCrumbDropDown(bcma);

                    // Is there still the need to show a menu that is not empty?
                    if (!bcma.Cancel &&
                        (bcma.KryptonContextMenu != null) &&
                        CommonHelper.ValidKryptonContextMenu(bcma.KryptonContextMenu))
                    {
                        // Cache the controller for use in menu close processing, prevents the need to
                        // store anything in the KryptonContextMenu tag and so free up its use to the user.
                        _pressedButtonController = controller;

                        // Show the context menu so user can select the next item for selection
                        bcma.KryptonContextMenu.Closed += new ToolStripDropDownClosedEventHandler(OnKryptonContextMenuClosed);
                        bcma.KryptonContextMenu.Show(_kryptonBreadCrumb, _kryptonBreadCrumb.RectangleToScreen(new Rectangle(viewButton.SplitRectangle.X - viewButton.SplitRectangle.Width,
                                                                                                                            viewButton.SplitRectangle.Y,
                                                                                                                            viewButton.SplitRectangle.Width * 2,
                                                                                                                            viewButton.SplitRectangle.Height)),
                                                     bcma.PositionH,
                                                     bcma.PositionV);

                        // do not show another context menu whilst this one is visible
                        _showingContextMenu = true;
                    }
                    else
                    {
                        // Button gives a fixed appearance when pressed, without a context menu that is not necessary
                        controller.RemoveFixed();
                    }
                }
                else
                {
                    // Button gives a fixed appearance when pressed, without a context menu that is not necessary
                    controller.RemoveFixed();

                    // Clicking item makes it become the selected crumb
                    _kryptonBreadCrumb.SelectedItem = breadCrumb;
                }
            }
        }
Beispiel #2
0
        private void OnOverflowButtonClick(object sender, MouseEventArgs e)
        {
            // Only allow a single context menu at a time
            if (!_showingContextMenu)
            {
                // Get access to the controller, view and crumb item
                ViewDrawButton   viewButton = sender as ViewDrawButton;
                ButtonController controller = viewButton.MouseController as ButtonController;

                // Create a context menu with a items collection
                KryptonContextMenu kcm = new KryptonContextMenu();

                // Use same palette settings for context menu as the main control
                kcm.Palette = _kryptonBreadCrumb.Palette;
                if (kcm.Palette == null)
                {
                    kcm.PaletteMode = _kryptonBreadCrumb.PaletteMode;
                }

                // Add an items collection as the root item of the context menu
                KryptonContextMenuItems items = new KryptonContextMenuItems();
                kcm.Items.Add(items);

                // Store lookup between each menu item and the crumb it represents. Prevents
                // needing to use the menu item tag for remembering association. Leaving the
                // tag free for use by the user.
                _menuItemToCrumb = new MenuItemToCrumb();

                // Create a new menu item to represent each of the invisible crumbs not children of the root
                // (item 0=overflow button, 1=root; 2=child of root, so we start at index 3)
                for (int i = 3; i < Count; i++)
                {
                    if (!this[i].Visible)
                    {
                        KryptonBreadCrumbItem  childCrumb = _buttonToCrumb[(ViewDrawButton)this[i]];
                        KryptonContextMenuItem childMenu  = new KryptonContextMenuItem();

                        // Store 1-to-1 association
                        _menuItemToCrumb.Add(childMenu, childCrumb);

                        // Copy across the display details of the child crumb item
                        childMenu.Text                  = childCrumb.ShortText;
                        childMenu.ExtraText             = childCrumb.LongText;
                        childMenu.Image                 = childCrumb.Image;
                        childMenu.ImageTransparentColor = childCrumb.ImageTransparentColor;
                        childMenu.Click                += new EventHandler(OnChildCrumbClick);

                        items.Items.Add(childMenu);
                    }
                }

                // Create a new menu item to represent each of the roots children
                bool firstRoot = true;
                foreach (KryptonBreadCrumbItem childCrumb in _kryptonBreadCrumb.RootItem.Items)
                {
                    // The first time we add an entry
                    if (firstRoot)
                    {
                        // Add a separator if entries already exist
                        if (items.Items.Count > 0)
                        {
                            items.Items.Add(new KryptonContextMenuSeparator());
                        }

                        firstRoot = false;
                    }

                    KryptonContextMenuItem childMenu = new KryptonContextMenuItem();

                    // Store 1-to-1 association
                    _menuItemToCrumb.Add(childMenu, childCrumb);

                    // Copy across the display details of the child crumb item
                    childMenu.Text                  = childCrumb.ShortText;
                    childMenu.ExtraText             = childCrumb.LongText;
                    childMenu.Image                 = childCrumb.Image;
                    childMenu.ImageTransparentColor = childCrumb.ImageTransparentColor;
                    childMenu.Click                += new EventHandler(OnChildCrumbClick);

                    items.Items.Add(childMenu);
                }

                // Allow the user a chance to alter the menu contents or cancel it entirely
                ContextPositionMenuArgs cpma = new ContextPositionMenuArgs(kcm, KryptonContextMenuPositionH.Left, KryptonContextMenuPositionV.Below);
                _kryptonBreadCrumb.OnOverflowDropDown(cpma);

                // Is there still the need to show a menu that is not empty?
                if (!cpma.Cancel &&
                    (cpma.KryptonContextMenu != null) &&
                    CommonHelper.ValidKryptonContextMenu(cpma.KryptonContextMenu))
                {
                    // Cache the controller for use in menu close processing, prevents the need to
                    // store anything in the KryptonContextMenu tag and so free up its use to the user.
                    _pressedButtonController = controller;

                    // Show the context menu so user can select the next item for selection
                    cpma.KryptonContextMenu.Closed += new ToolStripDropDownClosedEventHandler(OnKryptonContextMenuClosed);
                    cpma.KryptonContextMenu.Show(_kryptonBreadCrumb, _kryptonBreadCrumb.RectangleToScreen(new Rectangle(viewButton.ClientRectangle.X,
                                                                                                                        viewButton.ClientRectangle.Y,
                                                                                                                        viewButton.ClientRectangle.Width * 2,
                                                                                                                        viewButton.ClientRectangle.Height)),
                                                 cpma.PositionH,
                                                 cpma.PositionV);

                    // do not show another context menu whilst this one is visible
                    _showingContextMenu = true;
                }
                else
                {
                    // Button gives a fixed appearance when pressed, without a context menu that is not necessary
                    controller.RemoveFixed();

                    // Clicking item makes it become the selected crumb
                    _kryptonBreadCrumb.SelectedItem = _kryptonBreadCrumb.RootItem;
                }
            }
        }
        /// <summary>
        /// Initialize a new instance of the ViewDrawMenuItem class.
        /// </summary>
        /// <param name="provider">Provider of context menu information.</param>
        /// <param name="menuItem">Menu item definition.</param>
        /// <param name="columns">Containing columns.</param>
        /// <param name="standardStyle">Draw items with standard or alternate style.</param>
        /// <param name="imageColumn">Draw an image background for the item images.</param>
        public ViewDrawMenuItem(IContextMenuProvider provider,
                                KryptonContextMenuItem menuItem,
                                ViewLayoutStack columns,
                                bool standardStyle,
                                bool imageColumn)
            : base(menuItem.StateNormal.ItemHighlight.Back,
                   menuItem.StateNormal.ItemHighlight.Border,
                   menuItem.StateNormal.ItemHighlight,
                   PaletteMetricPadding.ContextMenuItemHighlight,
                   VisualOrientation.Top)
        {
            // Remember values
            _provider      = provider;
            _menuItem      = menuItem;
            _imageColumn   = imageColumn;
            _standardStyle = standardStyle;

            // Give the item object the redirector to use when inheriting values
            _menuItem.SetPaletteRedirect(provider);

            // Create a stack of horizontal items inside the item
            ViewLayoutDocker docker = new ViewLayoutDocker();

            // Decide on the enabled state of the display
            _itemEnabled = provider.ProviderEnabled && ResolveEnabled;
            PaletteContextMenuItemState menuItemState = (_itemEnabled ? _menuItem.StateNormal : _menuItem.StateDisabled);

            // Calculate the image to show inside in the image column
            Image itemColumnImage      = ResolveImage;
            Color itemImageTransparent = ResolveImageTransparentColor;

            // If no image found then...
            if (itemColumnImage != null)
            {
                // Ensure we have a fixed size if we are showing an image column
                if (_imageColumn)
                {
                    itemColumnImage      = _empty16x16;
                    itemImageTransparent = Color.Magenta;
                }

                switch (ResolveCheckState)
                {
                case CheckState.Checked:
                    itemColumnImage      = provider.ProviderImages.GetContextMenuCheckedImage();
                    itemImageTransparent = Color.Empty;
                    break;

                case CheckState.Indeterminate:
                    itemColumnImage      = provider.ProviderImages.GetContextMenuIndeterminateImage();
                    itemImageTransparent = Color.Empty;
                    break;
                }
            }

            // Column Image
            PaletteTripleJustImage justImage = (ResolveChecked ? _menuItem.StateChecked.ItemImage : menuItemState.ItemImage);

            _fixedImage   = new FixedContentValue(null, null, itemColumnImage, itemImageTransparent);
            _imageContent = new ViewDrawContent(justImage.Content, _fixedImage, VisualOrientation.Top);
            _imageCanvas  = new ViewDrawMenuImageCanvas(justImage.Back, justImage.Border, 0, false);
            _imageCanvas.Add(_imageContent);
            docker.Add(new ViewLayoutCenter(_imageCanvas), ViewDockStyle.Left);
            _imageContent.Enabled = _itemEnabled;

            // Text/Extra Text
            PaletteContentJustText menuItemStyle = (standardStyle ? menuItemState.ItemTextStandard : menuItemState.ItemTextAlternate);

            _fixedTextExtraText = new FixedContentValue(ResolveText, ResolveExtraText, null, Color.Empty);
            _textContent        = new ViewDrawMenuItemContent(menuItemStyle, _fixedTextExtraText, 1);
            docker.Add(_textContent, ViewDockStyle.Fill);
            _textContent.Enabled = _itemEnabled;

            // Shortcut
            if (_menuItem.ShowShortcutKeys)
            {
                string shortcutString = _menuItem.ShortcutKeyDisplayString;
                if (string.IsNullOrEmpty(shortcutString))
                {
                    shortcutString = (_menuItem.ShortcutKeys != Keys.None) ? new KeysConverter().ConvertToString(_menuItem.ShortcutKeys) : string.Empty;
                }
                if (shortcutString.Length > 0)
                {
                    _shortcutContent = new ViewDrawMenuItemContent(menuItemState.ItemShortcutText, new FixedContentValue(shortcutString, null, null, Color.Empty), 2);
                    docker.Add(_shortcutContent, ViewDockStyle.Right);
                    _shortcutContent.Enabled = _itemEnabled;
                }
            }

            // Add split item separator
            _splitSeparator = new ViewDrawMenuSeparator(menuItemState.ItemSplit);
            docker.Add(_splitSeparator, ViewDockStyle.Right);
            _splitSeparator.Enabled = _itemEnabled;
            _splitSeparator.Draw    = (_menuItem.Items.Count > 0) && _menuItem.SplitSubMenu;

            // SubMenu Indicator
            _hasSubMenu     = (_menuItem.Items.Count > 0);
            _subMenuContent = new ViewDrawMenuItemContent(menuItemState.ItemImage.Content, new FixedContentValue(null, null, (!_hasSubMenu ? _empty16x16 : provider.ProviderImages.GetContextMenuSubMenuImage()), (_menuItem.Items.Count == 0 ? Color.Magenta : Color.Empty)), 3);
            docker.Add(new ViewLayoutCenter(_subMenuContent), ViewDockStyle.Right);
            _subMenuContent.Enabled = _itemEnabled;

            Add(docker);

            // Add a controller for handing mouse and keyboard events
            MenuItemController mic = new MenuItemController(provider.ProviderViewManager, this, provider.ProviderNeedPaintDelegate);

            MouseController = mic;
            KeyController   = mic;

            // Want to know when a property changes whilst displayed
            _menuItem.PropertyChanged += new PropertyChangedEventHandler(OnPropertyChanged);

            // We need to know if a property of the command changes
            if (_menuItem.KryptonCommand != null)
            {
                _cachedCommand = _menuItem.KryptonCommand;
                _menuItem.KryptonCommand.PropertyChanged += new PropertyChangedEventHandler(OnCommandPropertyChanged);
            }
        }