Exemple #1
0
        /// <summary>
        /// Show the context menu relative to the provided screen rectangle.
        /// </summary>
        /// <param name="caller">Reference to object causing the context menu to be shown.</param>
        /// <param name="screenRect">Screen rectangle.</param>
        /// <param name="horz">Horizontal location relative to screen rectangle.</param>
        /// <param name="vert">Vertical location relative to screen rectangle.</param>
        /// <param name="keyboardActivated">Was context menu initiated via a keyboard action.</param>
        /// <param name="constrain">Should size and position of menu be constrained by display size.</param>
        /// <returns>Has the context menu become displayed.</returns>
        public bool Show(object caller,
                         Rectangle screenRect,
                         KryptonContextMenuPositionH horz,
                         KryptonContextMenuPositionV vert,
                         bool keyboardActivated,
                         bool constrain)
        {
            bool displayed = false;

            // Only need to show if not already displaying it
            if (_contextMenu == null)
            {
                // Remember the caller for us in events
                _caller = caller;

                // Give event handler a change to cancel the open request
                CancelEventArgs cea = new CancelEventArgs();
                OnOpening(cea);

                if (!cea.Cancel)
                {
                    // Set a default reason for the menu being dismissed
                    _closeReason = ToolStripDropDownCloseReason.AppFocusChange;

                    // Create the actual control used to show the context menu
                    _contextMenu = CreateContextMenu(this, _localPalette, _paletteMode,
                                                     _redirector, _redirectorImages,
                                                     Items, Enabled, keyboardActivated);

                    // Need to know when the visual control is removed
                    _contextMenu.Disposed += new EventHandler(OnContextMenuDisposed);

                    // Request the menu be shown immediately
                    _contextMenu.Show(screenRect, horz, vert, false, constrain);

                    // Override the horz, vert setting so that sub menus appear right and below
                    _contextMenu.ShowHorz = KryptonContextMenuPositionH.After;
                    _contextMenu.ShowVert = KryptonContextMenuPositionV.Top;

                    // Indicate the context menu is fully constructed and displayed
                    OnOpened(EventArgs.Empty);

                    // The menu has actually become displayed
                    displayed = true;
                }
            }

            return(displayed);
        }
        private void OnContextMenuDisposed(object sender, EventArgs e)
        {
            // Should still be caching a reference to actual display control
            if (_contextMenu != null)
            {
                // Unhook from control, so it can be garbage collected
                _contextMenu.Disposed -= new EventHandler(OnContextMenuDisposed);

                // No longer need to cache reference
                _contextMenu = null;

                // Tell our view manager that we no longer show a sub menu
                _provider.ProviderViewManager.ClearTargetSubMenu((IContextMenuTarget)KeyController);
            }
        }
Exemple #3
0
        private void OnContextMenuDisposed(object sender, EventArgs e)
        {
            // Should still be caching a reference to actual display control
            if (_contextMenu != null)
            {
                // Unhook from control, so it can be garbage collected
                _contextMenu.Disposed -= new EventHandler(OnContextMenuDisposed);

                // Copy to ourself the close reason
                if (_contextMenu.CloseReason.HasValue)
                {
                    CloseReason = _contextMenu.CloseReason.Value;
                }

                // No longer need to cache reference
                _contextMenu = null;

                // Notify event handlers the context menu has been closed and why it closed
                OnClosed(new ToolStripDropDownClosedEventArgs(CloseReason));
            }
        }
        /// <summary>
        /// Ask the menu item to show the associated child collection as a menu.
        /// </summary>
        public void ShowSubMenu(bool keyboardActivated)
        {
            // Only need to show if not already doing so
            if ((_contextMenu == null) || (_contextMenu.IsDisposed))
            {
                // No need for the sub menu timer anymore, we are showing
                _provider.ProviderViewManager.SetTargetSubMenu((IContextMenuTarget)KeyController);

                // Only show a sub menu if there is one to be shown!
                if (HasSubMenu)
                {
                    // Create the actual control used to show the context menu
                    _contextMenu = new VisualContextMenu(_provider, _menuItem.Items, keyboardActivated);

                    // Need to know when the visual control is removed
                    _contextMenu.Disposed += new EventHandler(OnContextMenuDisposed);

                    // Get the screen rectangle for the drawing element
                    Rectangle menuDrawRect = this.OwningControl.RectangleToScreen(ClientRectangle);

                    // Should this menu item be shown at a fixed screen rectangle?
                    if (_provider.ProviderShowSubMenuFixed(_menuItem))
                    {
                        // Request the menu be shown at fixed screen rectangle
                        _contextMenu.ShowFixed(_provider.ProviderShowSubMenuFixedRect(_menuItem),
                                               _provider.ProviderShowHorz,
                                               _provider.ProviderShowVert);
                    }
                    else
                    {
                        // Request the menu be shown immediately
                        _contextMenu.Show(menuDrawRect,
                                          _provider.ProviderShowHorz,
                                          _provider.ProviderShowVert,
                                          true, false);
                    }
                }
            }
        }
        /// <summary>
        /// Get the renderer.
        /// </summary>
        /// <returns>Render instance.</returns>
        public IRenderer GetRenderer()
        {
            VisualContextMenu contextMenu = (VisualContextMenu)_provider.ProviderViewManager.Control;

            return(contextMenu.Renderer);
        }