示例#1
0
        private WriterCommandDescriptor GetCommandDescriptor(ITypeDescriptorContext context, string name)
        {
            WriterCommandDescriptor result = null;

            foreach (object item in GetCommandDescriptors(context))
            {
                if (item is WriterCommandDescriptor)
                {
                    if (string.Compare(((WriterCommandDescriptor)item).CommandName, name, true) == 0)
                    {
                        result = (WriterCommandDescriptor)item;
                        break;
                    }
                }
                else if (item is WriterCommandModuleDescriptor)
                {
                    WriterCommandModuleDescriptor module = (WriterCommandModuleDescriptor)item;
                    foreach (WriterCommandDescriptor d in module.Commands)
                    {
                        if (string.Compare(d.CommandName, name, true) == 0)
                        {
                            result = d;
                            break;
                        }
                    }
                    if (result != null)
                    {
                        break;
                    }
                }
            }
            return(result);
        }
示例#2
0
        public override void PaintValue(PaintValueEventArgs e)
        {
            WriterCommandDescriptor cmd = GetCommandDescriptor(e.Context, Convert.ToString(e.Value));

            if (cmd != null && cmd.Image != null)
            {
                e.Graphics.DrawImage(cmd.Image, e.Bounds.Left, e.Bounds.Top);
            }
            base.PaintValue(e);
        }
示例#3
0
        private void OnValueChanged(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object NewValue)
        {
            string name = Convert.ToString(NewValue);
            WriterCommandDescriptor cmd = GetCommandDescriptor(context, name);

            if (cmd != null)
            {
                if (context.Instance is ToolStripItem ||
                    context.Instance is Button ||
                    context.Instance is MenuItem)
                {
                    IComponentChangeService svc = (IComponentChangeService)context.GetService(
                        typeof(IComponentChangeService));
                    if (cmd.Image != null)
                    {
                        PropertyDescriptor prop = TypeDescriptor.GetProperties(context.Instance)["Image"];
                        svc.OnComponentChanging(
                            context.Instance,
                            prop);
                        object oldValue = prop.GetValue(context.Instance);
                        prop.SetValue(context.Instance, cmd.Image);
                        svc.OnComponentChanged(
                            context.Instance,
                            prop,
                            oldValue,
                            cmd.Image);
                    }
                    if (context.Instance.GetType().GetProperty("Text") != null)
                    {
                        PropertyDescriptor prop = TypeDescriptor.GetProperties(context.Instance)["Text"];

                        string desc = cmd.Description;
                        if (desc != null && desc.Trim().Length > 0)
                        {
                            svc.OnComponentChanging(
                                context.Instance,
                                prop);
                            object oldValue = prop.GetValue(context.Instance);
                            prop.SetValue(context.Instance, desc);
                            svc.OnComponentChanged(
                                context.Instance,
                                prop,
                                oldValue,
                                desc);
                        }
                    }
                }
            }
        }
示例#4
0
        public static ArrayList GetCommandDescriptors(Type[] types)
        {
            ArrayList cmds = new ArrayList();
            ArrayList mdls = new ArrayList();

            //StringBuilder str = new StringBuilder();
            //str.AppendLine("-------------");
            //foreach (Type t in types)
            //{
            //    str.AppendLine("#" + t.FullName);
            //}
            //str.AppendLine("-------------");
            //MessageBox.Show(str.ToString());

            foreach (Type type in types)
            {
                if (type.IsSubclassOf(typeof(WriterCommand)) &&
                    type.Equals(typeof(WriterCommandDelegate)) == false)
                {
                    WriterCommandDescriptor cmd = WriterCommandDescriptor.Create(type, false);
                    if (cmd != null)
                    {
                        cmds.Add(cmd);
                    }
                }
                else if (type.IsSubclassOf(typeof(CSWriterCommandModule)))
                {
                    WriterCommandModuleDescriptor mdl = WriterCommandModuleDescriptor.Create(type, false);
                    if (mdl != null)
                    {
                        mdls.Add(mdl);
                    }
                }
            }
            cmds.Sort(new CommandDescriptorNameComparer());
            mdls.Sort(new CommandDescriptorNameComparer());
            ArrayList result = new ArrayList();

            result.AddRange(cmds);
            result.AddRange(mdls);

            return(result);
        }
        public static void FillTreeView(TreeView tvw, IEnumerable descriptors)
        {
            ImageList images = new ImageList();

            images.Images.Add(CommandUtils.GetResourceImage(
                                  typeof(dlgCommandNameEditor).Assembly,
                                  "DCSoft.CSharpWriter.Commands.Images.CommandModule.bmp"));
            images.Images.Add(CommandUtils.GetResourceImage(
                                  typeof(dlgCommandNameEditor).Assembly,
                                  "DCSoft.CSharpWriter.Commands.Images.CommandDefault.bmp"));
            Dictionary <object, int> indexs = new Dictionary <object, int>();

            // 初始化图标列表
            foreach (object item in descriptors)
            {
                if (item is WriterCommandDescriptor)
                {
                    WriterCommandDescriptor d = (WriterCommandDescriptor)item;
                    if (d.Image != null)
                    {
                        images.Images.Add(d.Image);
                        indexs[d] = images.Images.Count - 1;
                    }
                    else
                    {
                        indexs[d] = 1;
                    }
                }
                else if (item is WriterCommandModuleDescriptor)
                {
                    WriterCommandModuleDescriptor m = (WriterCommandModuleDescriptor)item;
                    if (m.Image != null)
                    {
                        images.Images.Add(m.Image);
                        indexs[m] = images.Images.Count - 1;
                    }
                    else
                    {
                        indexs[m] = 0;
                    }
                    foreach (WriterCommandDescriptor d in m.Commands)
                    {
                        if (d.Image != null)
                        {
                            images.Images.Add(d.Image);
                            indexs[d] = images.Images.Count - 1;
                        }
                        else
                        {
                            indexs[d] = 1;
                        }
                    }
                }
            }//foreach

            tvw.ImageList = images;
            foreach (object item in descriptors)
            {
                if (item is WriterCommandDescriptor)
                {
                    WriterCommandDescriptor d = (WriterCommandDescriptor)item;
                    TreeNode cmdNode          = new TreeNode(d.CommandName);
                    cmdNode.Tag = item;
                    if (indexs.ContainsKey(item))
                    {
                        cmdNode.ImageIndex         = indexs[item];
                        cmdNode.SelectedImageIndex = cmdNode.ImageIndex;
                    }
                    tvw.Nodes.Add(cmdNode);
                }
                else if (item is WriterCommandModuleDescriptor)
                {
                    WriterCommandModuleDescriptor m = (WriterCommandModuleDescriptor)item;
                    TreeNode moduleNode             = new TreeNode(m.Name);
                    moduleNode.Tag = m;
                    if (indexs.ContainsKey(item))
                    {
                        moduleNode.ImageIndex         = indexs[item];
                        moduleNode.SelectedImageIndex = moduleNode.ImageIndex;
                    }
                    foreach (WriterCommandDescriptor d in m.Commands)
                    {
                        TreeNode cmdNode = new TreeNode(d.CommandName);
                        cmdNode.Tag = d;
                        if (indexs.ContainsKey(d))
                        {
                            cmdNode.ImageIndex         = indexs[d];
                            cmdNode.SelectedImageIndex = cmdNode.ImageIndex;
                        }
                        moduleNode.Nodes.Add(cmdNode);
                    }
                    tvw.Nodes.Add(moduleNode);
                }
            }
        }