#region License // Copyright 2005-2019 Paul Kohler (https://github.com/paulkohler/minisqlquery). All rights reserved. // This source code is made available under the terms of the GNU Lesser General Public License v3.0 // https://github.com/paulkohler/minisqlquery/blob/master/LICENSE #endregion using System; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; namespace MiniSqlQuery.Core { /// /// Helper class for building controls out of objects. /// public class CommandControlBuilder { /// /// Handles the click event of a tool strip item, if the is /// an instance the action is executed. /// /// The sender. /// The instance containing the event data. [DebuggerNonUserCode] public static void CommandItemClick(object sender, EventArgs e) { var item = sender as ToolStripItem; if (item != null) { var cmd = item.Tag as ICommand; if (cmd != null) { cmd.Execute(); } } } /// /// Creates a link label given the definition. /// /// The type of the command. /// A link label wired to the commands method. public static LinkLabel CreateLinkLabel() where TCommand : ICommand, new() { var linkLabel = new LinkLabel(); var cmd = CommandManager.GetCommandInstance(); linkLabel.AutoSize = true; linkLabel.Name = cmd.GetType().Name + "LinkLabel"; linkLabel.TabStop = true; linkLabel.Text = cmd.Name.Replace("&", string.Empty); linkLabel.Tag = cmd; linkLabel.Padding = new Padding(4); linkLabel.LinkClicked += LinkLabelLinkClicked; return linkLabel; } /// /// Creates a tool strip button given the definition. /// /// The type of the command. /// A tool strip button public static ToolStripButton CreateToolStripButton() where TCommand : ICommand, new() { var button = new ToolStripButton(); var cmd = CommandManager.GetCommandInstance(); button.DisplayStyle = ToolStripItemDisplayStyle.Image; button.Image = cmd.SmallImage; button.ImageTransparentColor = Color.Magenta; button.Name = cmd.GetType().Name + "ToolStripButton"; button.Tag = cmd; button.Text = cmd.Name; button.Click += CommandItemClick; return button; } /// /// Creates a tool strip menu item given the definition. /// /// The type of the command. /// A tool strip menu item wired to the commands method. public static ToolStripMenuItem CreateToolStripMenuItem() where TCommand : ICommand, new() { var menuItem = new ToolStripMenuItem(); var cmd = CommandManager.GetCommandInstance(); menuItem.Name = cmd.GetType().Name + "ToolStripMenuItem"; menuItem.Text = cmd.Name; menuItem.Tag = cmd; menuItem.ShortcutKeys = cmd.ShortcutKeys; menuItem.Image = cmd.SmallImage; menuItem.Click += CommandItemClick; // store the host for callback cmd.Host = menuItem; // todo... // if (!string.IsNullOrEmpty(cmd.ShortcutKeysText)) // { // menuItem.ToolTipText = string.Format("{0} ({1})", cmd.Name, cmd.ShortcutKeysText); // } return menuItem; } /// /// Creates a tool strip menu item seperator. /// /// A tool strip seperator. public static ToolStripSeparator CreateToolStripMenuItemSeparator() { return new ToolStripSeparator(); } /// /// Assigns an event handler () to the opening event /// for menu strip items which in turn hadles enableing and disabling. /// /// The menu strip to monitor. public static void MonitorMenuItemsOpeningForEnabling(ToolStrip menuStrip) { if (menuStrip is ContextMenuStrip || menuStrip is MenuStrip) { foreach (ToolStripItem item in menuStrip.Items) { var topLevelMenu = item as ToolStripMenuItem; if (topLevelMenu != null) { // Debug.WriteLine("MonitorMenuItemsOpeningForEnabling :: " + topLevelMenu.Text); topLevelMenu.DropDownOpening += TopLevelMenuDropDownOpening; topLevelMenu.DropDownClosed += TopLevelMenuDropDownClosed; } } } } /// /// Used when a menu is opening, handles enabling/disabling of items for display. /// /// The sender. /// The instance containing the event data. [DebuggerNonUserCode] public static void TopLevelMenuDropDownOpening(object sender, EventArgs e) { var menuItem = sender as ToolStripMenuItem; if (menuItem != null) { foreach (ToolStripItem item in menuItem.DropDownItems) { var cmd = item.Tag as ICommand; if (cmd != null) { item.Enabled = cmd.Enabled; } } } } /// /// The link label link clicked. /// /// The sender. /// The instance containing the event data. private static void LinkLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { var linkLabel = sender as Control; if (linkLabel != null) { var cmd = linkLabel.Tag as ICommand; if (cmd != null) { cmd.Execute(); } } } /// /// We need to re-enable all the menu items so that the shortcut keys are available. /// This is because the model uses a continuous check approach rather than individual events /// for the enabling. /// /// The sender. /// The instance containing the event data. [DebuggerNonUserCode] private static void TopLevelMenuDropDownClosed(object sender, EventArgs e) { var menuItem = sender as ToolStripMenuItem; if (menuItem != null) { foreach (ToolStripItem item in menuItem.DropDownItems) { var cmd = item.Tag as ICommand; if (cmd != null) { item.Enabled = true; } } } } } }