/// <summary> /// Adds new button as <see cref="ToolStripSplitButton"/> with a drop-down menu. /// </summary> /// <param name="text">Text.</param> /// <param name="menu">Callback function that adds menu items. Called when need the menu first time (when opening it, or when clicked the button if <i>onClick</i> null). If sets <see cref="AMenu.MultiShow"/> = false, called each time.</param> /// <param name="icon"></param> /// <param name="tooltip">Tooltip text.</param> /// <param name="onClick">Callback function. Called when the button clicked. If null, will execute the first menu item.</param> /// <param name="l"><see cref="CallerLineNumberAttribute"/></param> /// <example><see cref="AToolbar"/></example> public ToolStripSplitButton SplitButton(string text, Action <AMenu> menu, MTImage icon = default, string tooltip = null, Action <MTClickArgs> onClick = null, [CallerLineNumber] int l = 0) { var item = new _SplitButton(this, text, menu, onClick); _Add(false, item, onClick, icon, tooltip, l); return(item); }
/// <summary> /// Adds new button as <see cref="ToolStripDropDownButton"/> with a drop-down menu. /// </summary> /// <param name="text">Text.</param> /// <param name="menu">Callback function that adds menu items. Called when opening the menu first time. If sets <see cref="AMenu.MultiShow"/> = false, called each time.</param> /// <param name="icon"></param> /// <param name="tooltip">Tooltip text.</param> /// <param name="l"><see cref="CallerLineNumberAttribute"/></param> /// <example><see cref="AToolbar"/></example> public ToolStripDropDownButton MenuButton(string text, Action <AMenu> menu, MTImage icon = default, string tooltip = null, [CallerLineNumber] int l = 0) { var item = new _MenuButton(this, text, menu); _Add(true, item, null, icon, tooltip, l); return(item); }
/// <summary> /// Adds new button as <see cref="ToolStripButton"/>. /// </summary> /// <param name="text">Text.</param> /// <param name="onClick">Callback function. Called when the button clicked.</param> /// <param name="icon"></param> /// <param name="tooltip">Tooltip text.</param> /// <param name="l"><see cref="CallerLineNumberAttribute"/></param> /// <remarks> /// Sets button text, icon and <b>Click</b> event handler. Other properties can be specified later. /// /// Code <c>t.Add("text", o => AOutput.Write(o));</c> is the same as <c>t["text"] = o => AOutput.Write(o);</c>. See <see cref="this[string, MTImage, string, int]"/>. /// </remarks> public ToolStripButton Add(string text, Action <MTClickArgs> onClick, MTImage icon = default, string tooltip = null, [CallerLineNumber] int l = 0) { var item = new ToolStripButton(text); _Add(false, item, onClick, icon, tooltip, l); return(item); }
void _Add(bool isSub, ToolStripItem item, MTImage icon, Action <MTClickArgs> onClick, int l) { var dd = CurrentAddMenu; dd.SuspendLayout(); //makes adding items much faster. It's OK to suspend/resume when already suspended; .NET uses a layoutSuspendCount. dd.Items.Add(item); _SetItemProp(false, isSub, item, onClick, icon, l); _OnItemAdded(item); dd.ResumeLayout(false); }
/// <summary> /// Adds item of any <b>ToolStripItem</b>-based type, for example <b>ToolStripLabel</b>, <b>ToolStripTextBox</b>, <b>ToolStripComboBox</b>. /// </summary> /// <param name="item"></param> /// <param name="icon"></param> /// <param name="tooltip">Tooltip text.</param> /// <param name="onClick">Callback function. Called when the item clicked. Not useful with most item types.</param> /// <param name="l"><see cref="CallerLineNumberAttribute"/></param> public void Add(ToolStripItem item, MTImage icon = default, string tooltip = null, Action <MTClickArgs> onClick = null, [CallerLineNumber] int l = 0) { _Add(false, item, onClick, icon, tooltip, l, true); //AOutput.Write(item.Padding, item.Margin); //Activate window when a child control clicked, or something may not work, eg cannot enter text in Edit control. if (item is ToolStripControlHost h && h.CanSelect) //combo, edit, progress { h.GotFocus += (unu, sed) => { //info: before MouseDown, which does not work well with combo box Api.SetForegroundWindow((AWnd)_c); //does not fail, probably after a mouse click this process is allowed to activate windows, even if the click did not activate because of the window style }; h.KeyDown += (_, e) => { if (e.KeyData == Keys.Escape && ((AWnd)_c).IsActive) { _c.Focus(); } }; } }
/// <summary> /// Adds new item as <see cref="ToolStripMenuItem"/>. /// </summary> /// <param name="text">Text. If contains a tab character, like "Open\tCtrl+O", displays text after it as shortcut keys (right-aligned).</param> /// <param name="onClick">Callback function. Called when clicked the menu item.</param> /// <param name="icon"></param> /// <param name="l"><see cref="CallerLineNumberAttribute"/></param> /// <remarks> /// Sets menu item text, icon and <b>Click</b> event handler. Other properties can be specified later. See example. /// /// Code <c>m.Add("text", o => AOutput.Write(o));</c> is the same as <c>m["text"] = o => AOutput.Write(o);</c>. See <see cref="this[string, MTImage, int]"/>. /// </remarks> /// <example> /// <code><![CDATA[ /// var m = new AMenu(); /// m.Add("One", o => AOutput.Write(o), icon: AFolders.System + "shell32.dll,9"); /// var mi = m.Add("Two", o => { AOutput.Write(o.MenuItem.Checked); ADialog.Show(o.ToString()); }); /// mi.Checked = true; /// m.ExtractIconPathFromCode = true; /// m.Add("notepad", o => AExec.TryRun(AFolders.System + "notepad.exe")); /// m.Show(); /// ]]></code> /// </example> public ToolStripMenuItem Add(string text, Action <MTClickArgs> onClick, MTImage icon = default, [CallerLineNumber] int l = 0) { string sk = null; if (!text.NE()) { int i = text.IndexOf('\t'); if (i >= 0) { sk = text.Substring(i + 1); text = text.Remove(i); } } var item = new ToolStripMenuItem(text); if (sk != null) { item.ShortcutKeyDisplayString = sk; } _Add(false, item, icon, onClick, l); return(item); }
void _Add(bool isSub, ToolStripItem item, Action <MTClickArgs> onClick, MTImage icon, string tooltip, int sourceLine, bool custom = false) { if (!custom) { item.Margin = default; //default top 1, bottom 2 //rejected: make min button height like of native toolbars //item.Padding = new Padding(1); //default 0 //because of this ToolStripSplitButton bug: ignores vertical padding. Then split buttons are smaller. if (item is ToolStripSplitButton sb) { //sb.AutoSize = false; sb.DropDownButtonWidth += 4; } } if (tooltip != null) { item.ToolTipText = tooltip; } _c.Items.Add(item); _SetItemProp(true, isSub, item, onClick, icon, sourceLine); bool onlyImage = NoText && (item.Image != null || item.ImageIndex >= 0 || !item.ImageKey.NE()); if (onlyImage) { item.DisplayStyle = ToolStripItemDisplayStyle.Image; //default ImageAndText } else { item.AutoToolTip = false; //default true } _OnItemAdded(item); }
/// <summary> /// Adds item of any <b>ToolStripItem</b>-based type, for example <b>ToolStripLabel</b>, <b>ToolStripTextBox</b>, <b>ToolStripComboBox</b>. /// </summary> /// <param name="item">An already created item of any supported type.</param> /// <param name="icon"></param> /// <param name="onClick">Callback function. Called when the item clicked. Not useful with most item types.</param> /// <param name="l"><see cref="CallerLineNumberAttribute"/></param> public void Add(ToolStripItem item, MTImage icon = default, Action <MTClickArgs> onClick = null, [CallerLineNumber] int l = 0) => _Add(false, item, icon, onClick, l);
/* * CONSIDER: * Specialized AddX methods. * * For example, instead of * m["Label"] = o => AExec.TryRun("notepad.exe"); * can use * m.Run("notepad.exe", "label"); //label is optional * Then can auto-get icon without disassembling the callback. * * Another example: instead of * m["Label"] = o => AClipboard.Paste("notepad.exe"); * can use * m.Paste("notepad.exe", "label"); //label is optional * * Can use extension methods for this. Example: * public static void Run(this AMenu m, string path, string label = null) * { * m[label ?? path, path] = o => AExec.TryRun(path); * } * */ /// <summary> /// Adds new item. /// The same as <see cref="Add(string, Action{MTClickArgs}, MTImage, int)"/>. /// </summary> /// <example> /// <code><![CDATA[ /// var m = new AMenu(); /// m["One"] = o => AOutput.Write(o); /// m["Two", @"icon file path"] = o => { AOutput.Write(o); ADialog.Show(o.ToString()); }; /// m.LastItem.ToolTipText = "tooltip"; /// m["Three"] = o => { AOutput.Write(o.MenuItem.Checked); }; /// m.LastMenuItem.Checked = true; /// m.ExtractIconPathFromCode = true; /// m["notepad"] = o => AExec.TryRun(AFolders.System + "notepad.exe"); /// m.Show(); /// ]]></code> /// </example> public Action <MTClickArgs> this[string text, MTImage icon = default, [CallerLineNumber] int l = 0] { set { Add(text, value, icon, l); } }