Beispiel #1
0
        void treeView_ContextMenuOpening(object sender, ContextMenuEventArgs e)
        {
            SharpTreeNode[] selectedNodes = treeView.GetTopLevelSelection().ToArray();
            if (selectedNodes.Length == 0)
            {
                e.Handled = true;                 // don't show the menu
                return;
            }
            ContextMenu menu = new ContextMenu();

            foreach (var category in entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.Category))
            {
                bool needSeparatorForCategory = true;
                foreach (var entryPair in category)
                {
                    IContextMenuEntry entry = entryPair.Value;
                    if (entry.IsVisible(selectedNodes))
                    {
                        if (needSeparatorForCategory && menu.Items.Count > 0)
                        {
                            menu.Items.Add(new Separator());
                            needSeparatorForCategory = false;
                        }
                        MenuItem menuItem = new MenuItem();
                        menuItem.Header = entryPair.Metadata.Header;
                        if (!string.IsNullOrEmpty(entryPair.Metadata.Icon))
                        {
                            menuItem.Icon = new Image {
                                Width  = 16,
                                Height = 16,
                                Source = Images.LoadImage(entry, entryPair.Metadata.Icon)
                            };
                        }
                        if (entryPair.Value.IsEnabled(selectedNodes))
                        {
                            menuItem.Click += delegate
                            {
                                entry.Execute(selectedNodes);
                            };
                        }
                        else
                        {
                            menuItem.IsEnabled = false;
                        }
                        menu.Items.Add(menuItem);
                    }
                }
            }
            if (menu.Items.Count > 0)
            {
                treeView.ContextMenu = menu;
            }
            else
            {
                // hide the context menu.
                e.Handled = true;
            }
        }
Beispiel #2
0
 bool ShowContextMenu(ContextMenuEntryContext context, out ContextMenu menu)
 {
     menu = new ContextMenu();
     foreach (var category in MefState.Instance.entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.Category))
     {
         bool needSeparatorForCategory = menu.Items.Count > 0;
         foreach (var entryPair in category)
         {
             IContextMenuEntry entry = entryPair.Value;
             if (entry.IsVisible(context))
             {
                 if (needSeparatorForCategory)
                 {
                     menu.Items.Add(new Separator());
                     needSeparatorForCategory = false;
                 }
                 MenuItem menuItem = new MenuItem();
                 menuItem.Header = entryPair.Metadata.Header;
                 bool isEnabled;
                 if (entryPair.Value.IsEnabled(context))
                 {
                     menuItem.Click += (s, e) => {
                         // Clear this before executing the command since MainWindow might
                         // fail to give focus to new elements if it still thinks the menu
                         // is opened.
                         isMenuOpened = false;
                         entry.Execute(context);
                     };
                     isEnabled = true;
                 }
                 else
                 {
                     menuItem.IsEnabled = false;
                     isEnabled          = false;
                 }
                 if (!string.IsNullOrEmpty(entryPair.Metadata.Icon))
                 {
                     MainWindow.CreateMenuItemImage(menuItem, entry, entryPair.Metadata.Icon, BackgroundType.ContextMenuItem, isEnabled);
                 }
                 menuItem.InputGestureText = entryPair.Metadata.InputGestureText ?? string.Empty;
                 var entry2 = entry as IContextMenuEntry2;
                 if (entry2 != null)
                 {
                     entry2.Initialize(context, menuItem);
                 }
                 menu.Items.Add(menuItem);
             }
         }
     }
     menu.Opened += (s, e) => isMenuOpened = true;
     menu.Closed += (s, e) => { isMenuOpened = false; ClearReferences(); };
     return(menu.Items.Count > 0);
 }
Beispiel #3
0
 bool ShowContextMenu(TextViewContext context, out ContextMenu menu)
 {
     menu = new ContextMenu();
     foreach (var category in MefState.Instance.entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.Category))
     {
         bool needSeparatorForCategory = menu.Items.Count > 0;
         foreach (var entryPair in category)
         {
             IContextMenuEntry entry = entryPair.Value;
             if (entry.IsVisible(context))
             {
                 if (needSeparatorForCategory)
                 {
                     menu.Items.Add(new Separator());
                     needSeparatorForCategory = false;
                 }
                 MenuItem menuItem = new MenuItem();
                 menuItem.Header = entryPair.Metadata.Header;
                 bool isEnabled;
                 if (entryPair.Value.IsEnabled(context))
                 {
                     menuItem.Click += delegate { entry.Execute(context); };
                     isEnabled       = true;
                 }
                 else
                 {
                     menuItem.IsEnabled = false;
                     isEnabled          = false;
                 }
                 if (!string.IsNullOrEmpty(entryPair.Metadata.Icon))
                 {
                     MainWindow.CreateMenuItemImage(menuItem, entry, entryPair.Metadata.Icon, BackgroundType.ContextMenuItem, isEnabled);
                 }
                 menuItem.InputGestureText = entryPair.Metadata.InputGestureText ?? string.Empty;
                 var entry2 = entry as IContextMenuEntry2;
                 if (entry2 != null)
                 {
                     entry2.Initialize(context, menuItem);
                 }
                 menu.Items.Add(menuItem);
             }
         }
     }
     menu.Opened += (s, e) => Interlocked.Increment(ref menuCount);
     menu.Closed += (s, e) => { Interlocked.Decrement(ref menuCount); ClearReferences(); };
     return(menu.Items.Count > 0);
 }
Beispiel #4
0
 bool ShowContextMenu(TextViewContext context, out ContextMenu menu)
 {
     menu = new ContextMenu();
     foreach (var category in entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.Category))
     {
         bool needSeparatorForCategory = menu.Items.Count > 0;
         foreach (var entryPair in category)
         {
             IContextMenuEntry entry = entryPair.Value;
             if (entry.IsVisible(context))
             {
                 if (needSeparatorForCategory)
                 {
                     menu.Items.Add(new Separator());
                     needSeparatorForCategory = false;
                 }
                 MenuItem menuItem = new MenuItem();
                 menuItem.Header           = MainWindow.GetResourceString(entryPair.Metadata.Header);
                 menuItem.InputGestureText = entryPair.Metadata.InputGestureText;
                 if (!string.IsNullOrEmpty(entryPair.Metadata.Icon))
                 {
                     object image = Images.Load(entryPair.Value, entryPair.Metadata.Icon);
                     if (!(image is Viewbox))
                     {
                         image = new Image {
                             Width  = 16,
                             Height = 16,
                             Source = (ImageSource)image
                         };
                     }
                     menuItem.Icon = image;
                 }
                 if (entryPair.Value.IsEnabled(context))
                 {
                     menuItem.Click += delegate { entry.Execute(context); };
                 }
                 else
                 {
                     menuItem.IsEnabled = false;
                 }
                 menu.Items.Add(menuItem);
             }
         }
     }
     return(menu.Items.Count > 0);
 }
Beispiel #5
0
        bool ShowContextMenu(TextViewContext context, out ContextMenu menu)
        {
            IList <IControl> items = new List <IControl>();

            foreach (var category in entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.Category))
            {
                bool needSeparatorForCategory = items.Count > 0;
                foreach (var entryPair in category)
                {
                    IContextMenuEntry entry = entryPair.Value;
                    if (entry.IsVisible(context))
                    {
                        if (needSeparatorForCategory)
                        {
                            items.Add(new Separator());
                            needSeparatorForCategory = false;
                        }
                        MenuItem menuItem = new MenuItem();
                        menuItem.Header = entryPair.Metadata.Header;
                        if (!string.IsNullOrEmpty(entryPair.Metadata.Icon))
                        {
                            menuItem.Icon = new Image {
                                Width  = 16,
                                Height = 16,
                                Source = Images.LoadImage(entry, entryPair.Metadata.Icon)
                            };
                        }
                        if (entryPair.Value.IsEnabled(context))
                        {
                            menuItem.Click += delegate { entry.Execute(context); };
                        }
                        else
                        {
                            menuItem.IsEnabled = false;
                        }
                        items.Add(menuItem);
                    }
                }
            }
            menu       = new ContextMenu();
            menu.Items = items;
            return(items.Count > 0);
        }
Beispiel #6
0
        bool ShowContextMenu(TextViewContext context, out ContextMenu menu)
        {
            menu = new ContextMenu();
            var menuGroups = new Dictionary <string, Lazy <IContextMenuEntry, IContextMenuEntryMetadata>[]>();

            Lazy <IContextMenuEntry, IContextMenuEntryMetadata>[] topLevelGroup = null;
            foreach (var group in entries.OrderBy(c => c.Metadata.Order).GroupBy(c => c.Metadata.ParentMenuID))
            {
                if (group.Key == null)
                {
                    topLevelGroup = group.ToArray();
                }
                else
                {
                    menuGroups.Add(group.Key, group.ToArray());
                }
            }
            BuildMenu(topLevelGroup ?? Array.Empty <Lazy <IContextMenuEntry, IContextMenuEntryMetadata> >(), menu.Items);
            return(menu.Items.Count > 0);

            void BuildMenu(Lazy <IContextMenuEntry, IContextMenuEntryMetadata>[] menuGroup, ItemCollection parent)
            {
                foreach (var category in menuGroup.GroupBy(c => c.Metadata.Category))
                {
                    bool needSeparatorForCategory = parent.Count > 0;
                    foreach (var entryPair in category)
                    {
                        IContextMenuEntry entry = entryPair.Value;
                        if (entry.IsVisible(context))
                        {
                            if (needSeparatorForCategory)
                            {
                                parent.Add(new Separator());
                                needSeparatorForCategory = false;
                            }
                            MenuItem menuItem = new MenuItem();
                            menuItem.Header           = MainWindow.GetResourceString(entryPair.Metadata.Header);
                            menuItem.InputGestureText = entryPair.Metadata.InputGestureText;
                            if (!string.IsNullOrEmpty(entryPair.Metadata.Icon))
                            {
                                menuItem.Icon = new Image {
                                    Width  = 16,
                                    Height = 16,
                                    Source = Images.Load(entryPair.Value, entryPair.Metadata.Icon)
                                };
                            }
                            if (entryPair.Value.IsEnabled(context))
                            {
                                menuItem.Click += delegate { entry.Execute(context); };
                            }
                            else
                            {
                                menuItem.IsEnabled = false;
                            }
                            parent.Add(menuItem);

                            if (entryPair.Metadata.MenuID != null && menuGroups.TryGetValue(entryPair.Metadata.MenuID, out var group))
                            {
                                BuildMenu(group, menuItem.Items);
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        public bool CanExecute(object parameter)
        {
            var ctx = CreateContext();

            return(ctx != null && cmd.IsVisible(ctx) && cmd.IsEnabled(ctx));
        }