/// <summary>
        /// Called when the context menu is being populated.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The event arguments.</param>
        private void OnPopulateContextMenu(
            object sender,
            PopulateContextMenuArgs args)
        {
            // Add a separator and our custom "function".
            var menuItem = new MenuItem("Reverse Line");

            menuItem.Activated += OnReverseLine;

            args.Menu.Add(new SeparatorMenuItem());
            args.Menu.Add(menuItem);
        }
        /// <summary>
        /// Creates the context menu for the caret position.
        /// </summary>
        /// <returns></returns>
        public Menu CreateContextMenu()
        {
            // Create a new menu and add the three basic controls.
            var menu = new Menu();

            var cutMenuItem = new ImageMenuItem(Stock.Cut, null);

            cutMenuItem.Name       = "Cut";
            cutMenuItem.Activated += OnContextMenuItemActivated;
            menu.Append(cutMenuItem);

            var copyMenuItem = new ImageMenuItem(Stock.Copy, null);

            copyMenuItem.Name       = "Copy";
            copyMenuItem.Activated += OnContextMenuItemActivated;
            menu.Add(copyMenuItem);

            var pasteMenuItem = new ImageMenuItem(Stock.Paste, null);

            pasteMenuItem.Name       = "Paste";
            pasteMenuItem.Activated += OnContextMenuItemActivated;
            menu.Add(pasteMenuItem);

            // Allow a callback to manipulate the menu.
            if (PopulateContextMenu != null)
            {
                // Create the arguments for the event.
                var args = new PopulateContextMenuArgs
                {
                    Menu       = menu,
                    Controller = this
                };

                // Trigger the event.
                PopulateContextMenu(this, args);
            }

            // If we have no items, then there is no menu.
            if (menu.Children.Length == 0)
            {
                return(null);
            }

            // Return the resulting menu.
            return(menu);
        }
        /// <summary>
        /// Called when the context menu is being populated.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The event arguments.</param>
        private void OnPopulateContextMenu(
			object sender,
			PopulateContextMenuArgs args)
        {
            // Remove the existing items from the list.
            var menuItems = new HashSet<Widget>(args.Menu.Children);

            foreach (Widget menuItem in menuItems)
            {
                args.Menu.Remove(menuItem);
            }

            // We need a read lock on both the collection and the specific block
            // for the given line index.
            TextPosition position = editorView.Caret.Position;
            int blockIndex =
                position.LinePosition.GetLineIndex(editorView.LineBuffer.LineCount);
            int characterIndex = position.GetCharacterIndex(editorView.LineBuffer);
            Block block;

            var context = new BlockCommandContext(project);

            using (blocks.AcquireBlockLock(RequestLock.Read, blockIndex, out block))
            {
                // Figure out if we have any spans for this position.
                if (!block.TextSpans.Contains(characterIndex))
                {
                    // Nothing to add, so we can stop processing.
                    return;
                }

                // Gather up all the text spans for the current position in the line.
                var textSpans = new List<TextSpan>();

                textSpans.AddRange(block.TextSpans.GetAll(characterIndex));

                // Gather up the menu items for this point.
                bool firstItem = true;

                foreach (TextSpan textSpan in textSpans)
                {
                    IList<IEditorAction> actions = textSpan.Controller.GetEditorActions(
                        block, textSpan);

                    foreach (IEditorAction action in actions)
                    {
                        // Create a menu item and hook up events.
                        IEditorAction doAction = action;

                        var menuItem = new MenuItem(action.DisplayName);

                        menuItem.Activated += delegate
                        {
                            doAction.Do(context);
                            RaiseLineChanged(new LineChangedArgs(blockIndex));
                        };

                        // If this is the first item, then make the initial
                        // selection to avoid scrolling.
                        if (firstItem)
                        {
                            menuItem.Select();
                        }

                        firstItem = false;

                        // Add the item to the popup menu.
                        args.Menu.Add(menuItem);
                    }
                }
            }
        }
        /// <summary>
        /// Called when the context menu is being populated.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="args">The event arguments.</param>
        private void OnPopulateContextMenu(
            object sender,
            PopulateContextMenuArgs args)
        {
            // Remove the existing items from the list.
            var menuItems = new HashSet <Widget>(args.Menu.Children);

            foreach (Widget menuItem in menuItems)
            {
                args.Menu.Remove(menuItem);
            }

            // We need a read lock on both the collection and the specific block
            // for the given line index.
            TextPosition position   = editorView.Caret.Position;
            int          blockIndex =
                position.LinePosition.GetLineIndex(editorView.LineBuffer.LineCount);
            int   characterIndex = position.GetCharacterIndex(editorView.LineBuffer);
            Block block;

            var context = new BlockCommandContext(project);

            using (blocks.AcquireBlockLock(RequestLock.Read, blockIndex, out block))
            {
                // Figure out if we have any spans for this position.
                if (!block.TextSpans.Contains(characterIndex))
                {
                    // Nothing to add, so we can stop processing.
                    return;
                }

                // Gather up all the text spans for the current position in the line.
                var textSpans = new List <TextSpan>();

                textSpans.AddRange(block.TextSpans.GetAll(characterIndex));

                // Gather up the menu items for this point.
                bool firstItem = true;

                foreach (TextSpan textSpan in textSpans)
                {
                    IList <IEditorAction> actions = textSpan.Controller.GetEditorActions(
                        block, textSpan);

                    foreach (IEditorAction action in actions)
                    {
                        // Create a menu item and hook up events.
                        IEditorAction doAction = action;

                        var menuItem = new MenuItem(action.DisplayName);

                        menuItem.Activated += delegate
                        {
                            doAction.Do(context);
                            RaiseLineChanged(new LineChangedArgs(blockIndex));
                        };

                        // If this is the first item, then make the initial
                        // selection to avoid scrolling.
                        if (firstItem)
                        {
                            menuItem.Select();
                        }

                        firstItem = false;

                        // Add the item to the popup menu.
                        args.Menu.Add(menuItem);
                    }
                }
            }
        }