Exemple #1
0
        /// <summary>
        /// Populates menu control and toolbar with all registered menus and commands</summary>
        public void BuildDefaultMenusAndToolbars()
        {
            // try to get toolstrip container for toolbars
            if (m_toolStripContainer == null)
            {
                foreach (Control control in m_mainForm.Controls)
                {
                    m_toolStripContainer = control as ToolStripContainer;
                    if (m_toolStripContainer != null)
                    {
                        break;
                    }
                }
            }

            // build menus (but not their contents)
            m_mainMenuStrip.SuspendLayout();
            m_mainMenuStrip.Items.Clear();

            foreach (MenuInfo menuInfo in m_menus)
            {
                ToolStripMenuItem menuItem = m_menuToolStripItems[menuInfo];
                menuItem.DropDownItems.Add("Dummy"); // to trigger drop down
                // we will build the menu just-in-time, when its drop down is opening
                menuItem.DropDownOpening += menuItem_DropDownOpening;
                // we will clear the menu when it's closed
                menuItem.DropDownClosed += menuItem_DropDownClosed;
                m_mainMenuStrip.Items.Add(menuItem);
            }

            m_mainMenuStrip.ResumeLayout();
            m_mainMenuStrip.PerformLayout();

            // build toolbars
            var toolStrips = new List <ToolStrip>();

            for (int i = m_menus.Count - 1; i >= 0; i--)
            {
                MenuInfo menuInfo  = m_menus[i];
                var      toolStrip = menuInfo.GetToolStrip();
                toolStrips.Add(toolStrip);

                foreach (CommandInfo commandInfo in m_commands)
                {
                    if (TagsEqual(menuInfo.MenuTag, commandInfo.MenuTag))
                    {
                        if ((commandInfo.Visibility & CommandVisibility.Toolbar) != 0 &&
                            GetClient(commandInfo.CommandTag) != null)
                        {
                            var btn = commandInfo.GetButton();
                            if (commandInfo.CheckOnClick)
                            {
                                btn.CheckOnClick = true;
                                commandInfo.GetMenuItem().CheckOnClick = true;
                                btn.CheckedChanged += SynchronizeCheckedState;
                                commandInfo.GetMenuItem().CheckedChanged += SynchronizeCheckedState;
                            }
                            toolStrip.Items.Add(btn);
                        }
                    }
                }

                toolStrip.Dock = DockStyle.None;
                AddCustomizationDropDown(toolStrip);
                toolStrip.Visible = (toolStrip.Items.Count > 1);
            }

            if (m_toolStripContainer != null)
            {
                m_toolStripContainer.TopToolStripPanel.Controls.AddRange(toolStrips.ToArray());
                m_toolStripContainer.TopToolStripPanel.Controls.Add(m_mainMenuStrip);
            }
            else
            {
                m_mainForm.Controls.Add(m_mainMenuStrip);
            }
        }