示例#1
0
        public bool Add(ToolStripMenuItem parent, MenuItemAttribute mia)
        {
            if (!map.ContainsKey(parent))
            {
                ToolStrip ts = new ToolStrip();
                //ts.Stretch = false;
                ts.GripStyle = ToolStripGripStyle.Visible;
                ts.Dock      = DockStyle.None;
                //ts.Anchor = AnchorStyles.None;
                //ts.Dock = DockStyle.Top;
                ts.Name      = MnemonicEscape(parent.Text);
                ts.ImageList = ServiceHost.ImageListProvider.ImageList;
                ts.TabIndex  = (map[parent] = toplevel.Count) + 1;
                toplevel.Add(ts);
                ts.Visible     = false;
                ts.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
                //((HorLayoutSettings)ts.LayoutSettings).FlowDirection = FlowDirection.LeftToRight;
                toolbar.TopToolStripPanel.Controls.Add(ts);
            }

            if (mia != null)
            {
                ToolStrip       sm  = toplevel[map[parent]];
                ToolStripButton tbb = new ToolStripButton();
                tbb.Click      += new EventHandler(ButtonDefaultHandler);
                tbb.ImageIndex  = ServiceHost.ImageListProvider[mia.Image];
                tbb.Tag         = mia;
                tbb.ToolTipText = mia.Text;
                sm.Items.Add(tbb);
                sm.Visible = true;
            }
            return(true);
        }
示例#2
0
        void pmi_DropDownOpening(object sender, EventArgs e)
        {
            ToolStripMenuItem pmi = sender as ToolStripMenuItem;

            pmi.DropDownItems.Clear();

            MenuItemAttribute mia = attrmap[pmi.clonedfrom] as MenuItemAttribute; // eek??

            PropertyInfo pi = mia.invoke as PropertyInfo;

            string v = pi.GetValue(this, null) as string;

            TypeConverter tc = Activator.CreateInstance(mia.Converter) as TypeConverter;

            foreach (string file in tc.GetStandardValues())
            {
                ToolStripMenuItem smi = new ToolStripMenuItem(file);
                pmi.DropDownItems.Add(smi);

                smi.Enabled = pmi.Enabled;

                if (file == v)
                {
                    smi.Checked = true;
                }

                smi.Click += new EventHandler(pmi_Click);
            }
        }
示例#3
0
        void ButtonDefaultHandler(object sender, EventArgs e)
        {
            MenuItemAttribute mia = (sender as ToolStripButton).Tag as MenuItemAttribute;

            if (mia != null)
            {
                mia.ctr.DefaultHandler(mia.mi, EventArgs.Empty);
            }
        }
示例#4
0
        int IComparable.CompareTo(object obj)
        {
            MenuItemAttribute b = obj  as MenuItemAttribute;

            if (b == null)
            {
                return(-1);
            }
            return(Index - b.Index);
        }
示例#5
0
        void ValidateMenuState(ToolStripMenuItem toplevel)
        {
            Hashtable attrmap = GetAttributeMap(toplevel);

            foreach (ToolStripItem pmi in toplevel.DropDownItems)
            {
                MenuItemAttribute mia = attrmap[pmi] as MenuItemAttribute;
                if (mia == null) // in case its a seperator or submenu
                {
                    if (!(pmi is ToolStripSeparator))
                    {
                        foreach (ToolStripMenuItem spmi in (pmi as ToolStripMenuItem).DropDownItems)
                        {
                            MenuItemAttribute smia = attrmap[spmi] as MenuItemAttribute;

                            ServiceBase ctr = smia.ctr;
                            spmi.Enabled = ((ctr.MenuState & smia.State) == smia.State);

                            if (smia.istogglemenu)
                            {
                                try
                                {
                                    spmi.Checked = (bool)((PropertyInfo)smia.invoke).GetValue(smia.ctr, new object[0]);
                                }
                                catch
                                {
                                    //something not ready, sorts itself out
                                }
                            }
                        }
                    }
                }
                else
                {
                    ServiceBase ctr = mia.ctr;
                    pmi.Enabled = ((ctr.MenuState & mia.State) == mia.State);

                    if (mia.istogglemenu)
                    {
                        try
                        {
                            (pmi as ToolStripMenuItem).Checked = (bool)((PropertyInfo)mia.invoke).GetValue(mia.ctr, new object[0]);
                        }
                        catch
                        {
                            //something not ready, sorts itself out
                        }
                    }
                }
            }
        }
示例#6
0
 internal void ValidateToolBarButtons()
 {
     foreach (ToolStrip ts in toplevel)
     {
         foreach (ToolStripItem tbb in ts.Items)
         {
             MenuItemAttribute mia = tbb.Tag as MenuItemAttribute;
             if (mia != null)
             {
                 tbb.Enabled = ((mia.ctr.MenuState & mia.State) == mia.State);
             }
         }
     }
 }
示例#7
0
        void pmi_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem pmi = sender as ToolStripMenuItem;

            string v = pmi.Text;

            MenuItemAttribute mia = attrmap[((ToolStripMenuItem)pmi.OwnerItem).clonedfrom] as MenuItemAttribute;

            PropertyInfo pi = mia.invoke as PropertyInfo;

            try
            {
                pi.SetValue(this, v, null);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex);
            }
        }
示例#8
0
        internal void DefaultHandler(object sender, EventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler(DefaultHandler), new object[] { sender, e });
                return;
            }
            try
            {
                ToolStripMenuItem menu = sender as ToolStripMenuItem;
                MenuItemAttribute mia  = attrmap[menu] as MenuItemAttribute;

                if (mia == null)
                {
                    menu = menu.clonedfrom;
                    mia  = attrmap[menu] as MenuItemAttribute;
                }

                if (mia.istogglemenu)
                {
                    PropertyInfo pi = mia.invoke as PropertyInfo;
                    menu.Checked = !menu.Checked;
                    pi.SetValue(this, menu.Checked, new object[0]);
                }
                else
                {
                    MethodInfo mi = mia.invoke as MethodInfo;
                    mi.Invoke(this, new object[0]);
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Trace.WriteLine(ex);
#else
                throw ex;
#endif
            }
        }
示例#9
0
        void ISupportInitialize.BeginInit()
        {
            const BindingFlags BF = BindingFlags.Public | BindingFlags.DeclaredOnly
                                    | BindingFlags.NonPublic | BindingFlags.Instance;

            Type        tt = GetType();
            MenuService ms = ServiceHost.Menu as MenuService;

            if (Attribute.IsDefined(tt, typeof(MenuAttribute), false))
            {
                MenuAttribute mat = tt.GetCustomAttributes(typeof(MenuAttribute), false)[0] as MenuAttribute;

                toplevel = ms[mat.Text];

                if (toplevel == null)
                {
                    toplevel = new ToolStripMenuItem(mat.Text);
                    ms.AddTopLevel(toplevel);
                }

                attrmap = ms.GetAttributeMap(toplevel);

                EventHandler ev = new EventHandler(DefaultHandler);

                ArrayList submenus = new ArrayList();

                foreach (MethodInfo mi in tt.GetMethods(BF))
                {
                    if (mi.GetParameters().Length == 0)
                    {
                        bool hasat = Attribute.IsDefined(mi, typeof(MenuItemAttribute));

                        if (mi.ReturnType == typeof(void) && (!mi.IsPrivate || hasat) &&
                            !Attribute.IsDefined(mi, typeof(SuppressMenuAttribute), true))
                        {
                            (ServiceHost.Keyboard as KeyboardHandler).AddTarget(this, mi);
                        }
                        if (hasat)
                        {
                            MenuItemAttribute mia = mi.GetCustomAttributes(typeof(MenuItemAttribute), false)[0] as MenuItemAttribute;
                            mia.invoke = mi;
                            mia.ctr    = this;
                            submenus.Add(mia);
                        }
                    }
                }

                foreach (PropertyInfo pi in tt.GetProperties(BF))
                {
                    if (pi.PropertyType == typeof(bool) && pi.CanRead && pi.CanWrite)
                    {
                        bool hasat = Attribute.IsDefined(pi, typeof(MenuItemAttribute));

                        if (!Attribute.IsDefined(pi, typeof(SuppressMenuAttribute), true))
                        {
                            (ServiceHost.Keyboard as KeyboardHandler).AddToggle(this, pi);
                        }

                        if (hasat)
                        {
                            MenuItemAttribute mia = pi.GetCustomAttributes(typeof(MenuItemAttribute), false)[0] as MenuItemAttribute;
                            mia.invoke       = pi;
                            mia.istogglemenu = true;
                            mia.ctr          = this;
                            submenus.Add(mia);
                        }
                    }
                    else if (pi.PropertyType == typeof(string) && pi.CanRead && pi.CanWrite)
                    {
                        if (Attribute.IsDefined(pi, typeof(MenuItemAttribute)))
                        {
                            MenuItemAttribute mia = pi.GetCustomAttributes(typeof(MenuItemAttribute), false)[0] as MenuItemAttribute;
                            mia.invoke = pi;
                            mia.ctr    = this;
                            submenus.Add(mia);
                        }
                    }
                }

                foreach (ToolStripMenuItem mi in toplevel.DropDownItems)
                {
                    object mia = attrmap[mi];
                    if (mia != null)
                    {
                        submenus.Add(mia);
                    }
                }

                submenus.Sort();
                int previdx = -1;

                int counter = 0;

                ToolBarService tbs = ServiceHost.ToolBar as ToolBarService;

                foreach (MenuItemAttribute mia in submenus)
                {
                    ToolStripMenuItem pmi = null;
                    if (mia.mi == null)
                    {
                        if (mia.Converter == null)
                        {
                            pmi        = new ToolStripMenuItem(mia.Text);
                            pmi.Click += ev;
                        }
                        else
                        {
                            pmi = new ToolStripMenuItem(mia.Text);

                            PropertyInfo pi = mia.invoke as PropertyInfo;

                            string v = pi.GetValue(this, null) as string;

                            pmi.DropDownOpening += new EventHandler(pmi_DropDownOpening);
                            TypeConverter tc = Activator.CreateInstance(mia.Converter) as TypeConverter;

                            foreach (string file in tc.GetStandardValues())
                            {
                                ToolStripMenuItem smi = new ToolStripMenuItem(file);
                                pmi.DropDownItems.Add(smi);

                                if (file == v)
                                {
                                    smi.Checked = true;
                                }

                                smi.Click += new EventHandler(pmi_Click);
                            }
                        }
                    }
                    else
                    {
                        pmi = mia.mi as ToolStripMenuItem;
                    }

                    if (mia.istogglemenu)
                    {
                        PropertyInfo pi = mia.invoke as PropertyInfo;
                        try
                        {
                            bool v = (bool)pi.GetValue(this, new object[0]);
                            pmi.Checked = v;
                        }
                        catch
                        {
                            //something not ready, sorts itself out
                            Debugger.Break();
                        }
                    }

                    if (previdx != -1 && mia.Index > previdx + 1)
                    {
                        toplevel.DropDownItems.Add("-");
                        counter++;
                    }
                    int imgidx = -1;
                    if (mia.Image != null)
                    {
                        pmi.Tag = mia.Image;
                        imgidx  = ServiceHost.ImageListProvider[mia.Image];
                    }
                    mia.mi = pmi;

                    ToolStripMenuItem top = toplevel;

                    string miaText = mia.Text;

                    //if (miaText.IndexOf(':') > 0)
                    //{
                    //  string[] quals = miaText.Split(':');

                    //  if (quals.Length > 0)
                    //  {
                    //    top = ServiceHost.Menu[quals[0]];
                    //    miaText = miaText.Replace(quals[0] + ":", string.Empty);
                    //  }
                    //}

                    // check text
                    string[] tokens = miaText.Split('\\');
                    if (tokens.Length > 1)
                    {
                        ToolStripMenuItem sub = this.submenus[tokens[0]] as ToolStripMenuItem;
                        if (sub == null)
                        {
                            this.submenus[tokens[0]] = sub = new ToolStripMenuItem(tokens[0]);
                            top.DropDownItems.Add(sub);
                        }
                        top = sub;

                        pmi.Text = tokens[1];
                        top.DropDownItems.Add(pmi);
                        counter--;
                    }
                    else
                    {
                        string miatext = miaText;
                        top.DropDownOpening += delegate(object sender, EventArgs e)
                        {
                            bool   haderror;
                            string t = GetText(miatext, out haderror);
                            if (haderror)
                            {
                                pmi.Enabled = false;
                            }
                            pmi.Text = t;
                        };
                        top.DropDownItems.Add(pmi);
                    }

                    attrmap[pmi] = mia;

                    if (SettingsService.idemode)
                    {
                        if (mia.AllowToolBar)
                        {
                            tbs.Add(toplevel, mia);
                        }
                    }

                    previdx = mia.Index;
                    counter++;
                }
            }

            Initialize();
        }