protected override void GetComponentDesignerActions(IComponent component, DesignerActionListCollection actionLists)
        {
            if (component == null)
            {
                throw new ArgumentNullException("component");
            }
            if (actionLists == null)
            {
                throw new ArgumentNullException("actionLists");
            }
            IServiceContainer site = component.Site as IServiceContainer;

            if (site != null)
            {
                DesignerCommandSet service = (DesignerCommandSet)site.GetService(typeof(DesignerCommandSet));
                if (service != null)
                {
                    DesignerActionListCollection lists = service.ActionLists;
                    if (lists != null)
                    {
                        actionLists.AddRange(lists);
                    }
                }
                if ((actionLists.Count == 0) || ((actionLists.Count == 1) && (actionLists[0] is ControlDesigner.ControlDesignerActionList)))
                {
                    DesignerVerbCollection verbs = service.Verbs;
                    if ((verbs != null) && (verbs.Count != 0))
                    {
                        DesignerVerb[] array = new DesignerVerb[verbs.Count];
                        verbs.CopyTo(array, 0);
                        actionLists.Add(new DesignerActionVerbList(array));
                    }
                }
            }
        }
        public void CopyTo_ValidDestination_Success()
        {
            var verb       = new DesignerVerb("Text", null);
            var collection = new DesignerVerbCollection {
                verb, verb
            };

            var destination = new DesignerVerb[3];

            collection.CopyTo(destination, 1);
            Assert.Equal(new DesignerVerb[] { null, verb, verb }, destination);
        }
示例#3
0
        // DesignerVerbCollection
        public void DesignerVerbCollectionExample()
        {
            //<Snippet1>
            //<Snippet2>
            // Creates an empty DesignerVerbCollection.
            DesignerVerbCollection collection = new DesignerVerbCollection();

            //</Snippet2>

            //<Snippet3>
            // Adds a DesignerVerb to the collection.
            collection.Add(new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent)));
            //</Snippet3>

            //<Snippet4>
            // Adds an array of DesignerVerb objects to the collection.
            DesignerVerb[] verbs = { new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent)), new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent)) };
            collection.AddRange(verbs);

            // Adds a collection of DesignerVerb objects to the collection.
            DesignerVerbCollection verbsCollection = new DesignerVerbCollection();

            verbsCollection.Add(new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent)));
            verbsCollection.Add(new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent)));
            collection.AddRange(verbsCollection);
            //</Snippet4>

            //<Snippet5>
            // Tests for the presence of a DesignerVerb in the collection,
            // and retrieves its index if it is found.
            DesignerVerb testVerb  = new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent));
            int          itemIndex = -1;

            if (collection.Contains(testVerb))
            {
                itemIndex = collection.IndexOf(testVerb);
            }
            //</Snippet5>

            //<Snippet6>
            // Copies the contents of the collection, beginning at index 0,
            // to the specified DesignerVerb array.
            // 'verbs' is a DesignerVerb array.
            collection.CopyTo(verbs, 0);
            //</Snippet6>

            //<Snippet7>
            // Retrieves the count of the items in the collection.
            int collectionCount = collection.Count;

            //</Snippet7>

            //<Snippet8>
            // Inserts a DesignerVerb at index 0 of the collection.
            collection.Insert(0, new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent)));
            //</Snippet8>

            //<Snippet9>
            // Removes the specified DesignerVerb from the collection.
            DesignerVerb verb = new DesignerVerb("Example designer verb", new EventHandler(this.ExampleEvent));

            collection.Remove(verb);
            //</Snippet9>

            //<Snippet10>
            // Removes the DesignerVerb at index 0.
            collection.RemoveAt(0);
            //</Snippet10>
            //</Snippet1>
        }
示例#4
0
        /// <include file='doc\MenuCommandService.uex' path='docs/doc[@for="MenuCommandService.EnsureVerbs"]/*' />
        /// <devdoc>
        ///      Ensures that the verb list has been created.
        /// </devdoc>
        private void EnsureVerbs()
        {
            // We apply global verbs only if the base component is the
            // currently selected object.
            //
            bool useGlobalVerbs = false;

            if (verbs == null)
            {
                DesignerVerb[] buildVerbs = null;

                // Demand get the selection service to get the current selection.
                //
                if (selectionService == null && serviceProvider != null)
                {
                    selectionService = (ISelectionService)serviceProvider.GetService(typeof(ISelectionService));
                    designerHost     = (IDesignerHost)serviceProvider.GetService(typeof(IDesignerHost));

                    if (selectionService != null)
                    {
                        selectionService.SelectionChanging += new EventHandler(this.OnSelectionChanging);
                    }
                }

                int verbCount = 0;
                DesignerVerbCollection localVerbs = null;

                if (selectionService != null && designerHost != null && selectionService.SelectionCount == 1)
                {
                    object selectedComponent = selectionService.PrimarySelection;
                    if (selectedComponent is IComponent &&
                        !TypeDescriptor.GetAttributes(selectedComponent).Contains(InheritanceAttribute.InheritedReadOnly))
                    {
                        useGlobalVerbs = (selectedComponent == designerHost.RootComponent);

                        IDesigner designer = designerHost.GetDesigner((IComponent)selectedComponent);
                        if (designer != null)
                        {
                            localVerbs = designer.Verbs;
                            if (localVerbs != null)
                            {
                                verbCount     += localVerbs.Count;
                                verbSourceType = selectedComponent.GetType();
                            }
                            else
                            {
                                verbSourceType = null;
                            }
                        }
                    }
                }

                if (useGlobalVerbs)
                {
                    verbCount += globalVerbCount;
                }

                buildVerbs = new DesignerVerb[verbCount];

                if (useGlobalVerbs && globalVerbs != null)
                {
                    Array.Copy(globalVerbs, 0, buildVerbs, 0, globalVerbCount);
                }

                if (localVerbs != null && localVerbs.Count > 0)
                {
                    int localStart = 0;
                    if (useGlobalVerbs)
                    {
                        localStart = globalVerbCount;
                    }
                    localVerbs.CopyTo(buildVerbs, localStart);

                    if (useGlobalVerbs)
                    {
                        // Now we must look for verbs with conflicting names.  We do a case insensitive
                        // search on the name here (users will never understand if two verbs differ only
                        // in case).  We also do a simple n^2 algorithm here; I expect it to be rare
                        // for there to be over 5 verbs, and even more rare for there to be a name
                        // conflict, so we can make the conflict reslution a bit more expensive and
                        // keep the normal case fast.
                        //
                        for (int gv = globalVerbCount - 1; gv >= 0; gv--)
                        {
                            for (int lv = localVerbs.Count - 1; lv >= 0; lv--)
                            {
                                if (string.Compare(globalVerbs[gv].Text, localVerbs[lv].Text, true, CultureInfo.CurrentCulture) == 0)
                                {
                                    // Conflict.  We decrement globalVerbCount and NULL the slot.
                                    //
                                    Debug.Assert(buildVerbs[gv].Equals(globalVerbs[gv]), "We depend on these arrays matching");
                                    buildVerbs[gv] = null;
                                    verbCount--;
                                }
                            }
                        }

                        if (buildVerbs.Length != verbCount)
                        {
                            // We encountered one or more conflicts.  Remove them by creating a new array.
                            //
                            DesignerVerb[] newVerbs = new DesignerVerb[verbCount];
                            int            index    = 0;

                            for (int i = 0; i < buildVerbs.Length; i++)
                            {
                                if (buildVerbs[i] != null)
                                {
                                    newVerbs[index++] = buildVerbs[i];
                                }
                            }

                            Debug.Assert(index == verbCount, "We miscounted verbs somewhere");
                            buildVerbs = newVerbs;
                        }
                    }
                }

                verbs = new DesignerVerbCollection(buildVerbs);
            }
        }