2014-08-11 22:40:14 +12:00
|
|
|
|
#region License Information (GPL v3)
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
ShareX - A program that allows you to take screenshots and share any file type
|
2019-01-02 20:43:52 +13:00
|
|
|
|
Copyright (c) 2007-2019 ShareX Team
|
2014-08-11 22:40:14 +12:00
|
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
|
|
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#endregion License Information (GPL v3)
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
using ShareX.HelpersLib.Properties;
|
2017-08-19 07:50:47 +12:00
|
|
|
|
using System.Collections.Generic;
|
2014-08-11 03:36:08 +12:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
2014-12-11 09:25:20 +13:00
|
|
|
|
namespace ShareX.HelpersLib
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
|
|
|
|
public static class CodeMenu
|
|
|
|
|
{
|
2018-11-21 05:22:12 +13:00
|
|
|
|
public static ContextMenuStrip Create<TEntry>(TextBoxBase tb, params TEntry[] ignoreList) where TEntry : CodeMenuEntry
|
2017-08-19 07:50:47 +12:00
|
|
|
|
{
|
|
|
|
|
return Create(tb, ignoreList, (CodeMenuItem[])null);
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-21 05:22:12 +13:00
|
|
|
|
public static ContextMenuStrip Create<TEntry>(TextBoxBase tb, TEntry[] ignoreList, CodeMenuItem[] extraItems) where TEntry : CodeMenuEntry
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
2014-09-14 00:07:05 +12:00
|
|
|
|
ContextMenuStrip cms = new ContextMenuStrip
|
|
|
|
|
{
|
2014-08-11 03:36:08 +12:00
|
|
|
|
Font = new Font("Lucida Console", 8),
|
|
|
|
|
AutoClose = false,
|
|
|
|
|
Opacity = 0.9,
|
|
|
|
|
ShowImageMargin = false
|
|
|
|
|
};
|
|
|
|
|
|
2017-08-19 07:50:47 +12:00
|
|
|
|
List<CodeMenuItem> items = new List<CodeMenuItem>();
|
|
|
|
|
|
|
|
|
|
if (extraItems != null)
|
|
|
|
|
{
|
|
|
|
|
items.AddRange(extraItems);
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 01:32:03 +13:00
|
|
|
|
IEnumerable<CodeMenuItem> variables = Helpers.GetValueFields<TEntry>().Where(x => !ignoreList.Contains(x)).
|
2017-08-19 07:50:47 +12:00
|
|
|
|
Select(x => new CodeMenuItem(x.ToPrefixString(), x.Description, x.Category));
|
|
|
|
|
|
|
|
|
|
items.AddRange(variables);
|
2014-08-11 03:36:08 +12:00
|
|
|
|
|
2018-10-18 01:32:03 +13:00
|
|
|
|
foreach (CodeMenuItem item in items)
|
2014-09-14 00:07:05 +12:00
|
|
|
|
{
|
2017-08-19 07:50:47 +12:00
|
|
|
|
ToolStripMenuItem tsmi = new ToolStripMenuItem { Text = $"{item.Name} - {item.Description}", Tag = item.Name };
|
2014-08-11 03:36:08 +12:00
|
|
|
|
tsmi.Click += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
string text = ((ToolStripMenuItem)sender).Tag.ToString();
|
|
|
|
|
tb.AppendTextToSelection(text);
|
|
|
|
|
};
|
2015-09-23 15:01:59 +12:00
|
|
|
|
|
2017-08-19 07:50:47 +12:00
|
|
|
|
if (string.IsNullOrWhiteSpace(item.Category))
|
2015-09-23 15:01:59 +12:00
|
|
|
|
{
|
|
|
|
|
cms.Items.Add(tsmi);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem tsmiParent;
|
2017-08-19 07:50:47 +12:00
|
|
|
|
int index = cms.Items.IndexOfKey(item.Category);
|
2018-05-16 20:23:13 +12:00
|
|
|
|
if (index < 0)
|
2015-09-23 15:01:59 +12:00
|
|
|
|
{
|
2017-08-19 07:50:47 +12:00
|
|
|
|
tsmiParent = new ToolStripMenuItem { Text = item.Category, Tag = item.Category, Name = item.Category };
|
2015-09-24 06:23:25 +12:00
|
|
|
|
tsmiParent.HideImageMargin();
|
2015-09-23 15:01:59 +12:00
|
|
|
|
cms.Items.Add(tsmiParent);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
tsmiParent = cms.Items[index] as ToolStripMenuItem;
|
|
|
|
|
}
|
|
|
|
|
tsmiParent.DropDownItems.Add(tsmi);
|
|
|
|
|
}
|
2014-08-11 03:36:08 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cms.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ToolStripMenuItem tsmiClose = new ToolStripMenuItem(Resources.CodeMenu_Create_Close);
|
2014-08-11 03:36:08 +12:00
|
|
|
|
tsmiClose.Click += (sender, e) => cms.Close();
|
|
|
|
|
cms.Items.Add(tsmiClose);
|
|
|
|
|
|
|
|
|
|
tb.MouseDown += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (cms.Items.Count > 0) cms.Show(tb, new Point(tb.Width + 1, 0));
|
|
|
|
|
};
|
|
|
|
|
|
2015-08-20 10:28:45 +12:00
|
|
|
|
tb.GotFocus += (sender, e) =>
|
|
|
|
|
{
|
|
|
|
|
if (cms.Items.Count > 0) cms.Show(tb, new Point(tb.Width + 1, 0));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tb.LostFocus += (sender, e) =>
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
|
|
|
|
if (cms.Visible) cms.Close();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tb.KeyDown += (sender, e) =>
|
|
|
|
|
{
|
2014-09-14 00:07:05 +12:00
|
|
|
|
if ((e.KeyCode == Keys.Enter || e.KeyCode == Keys.Escape) && cms.Visible)
|
|
|
|
|
{
|
2014-08-11 03:36:08 +12:00
|
|
|
|
cms.Close();
|
|
|
|
|
e.SuppressKeyPress = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
tb.Disposed += (sender, e) => cms.Dispose();
|
|
|
|
|
|
|
|
|
|
return cms;
|
|
|
|
|
}
|
2014-11-20 07:27:46 +13:00
|
|
|
|
|
2016-06-23 00:30:37 +12:00
|
|
|
|
public static ContextMenuStrip Create<TEntry>(params TEntry[] ignoreList) where TEntry : CodeMenuEntry
|
2014-11-20 07:27:46 +13:00
|
|
|
|
{
|
|
|
|
|
ContextMenuStrip cms = new ContextMenuStrip
|
|
|
|
|
{
|
|
|
|
|
Font = new Font("Lucida Console", 8),
|
|
|
|
|
AutoClose = true,
|
|
|
|
|
Opacity = 0.9,
|
|
|
|
|
ShowImageMargin = false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var variables = Helpers.GetValueFields<TEntry>().Where(x => !ignoreList.Contains(x)).
|
|
|
|
|
Select(x => new
|
|
|
|
|
{
|
|
|
|
|
Name = x.ToPrefixString(),
|
|
|
|
|
Description = x.Description
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
foreach (var variable in variables)
|
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem tsmi = new ToolStripMenuItem { Text = string.Format("{0} - {1}", variable.Name, variable.Description), Tag = variable.Name };
|
|
|
|
|
cms.Items.Add(tsmi);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cms.Items.Add(new ToolStripSeparator());
|
|
|
|
|
|
|
|
|
|
ToolStripMenuItem tsmiClose = new ToolStripMenuItem(Resources.CodeMenu_Create_Close);
|
|
|
|
|
tsmiClose.Click += (sender, e) => cms.Close();
|
|
|
|
|
cms.Items.Add(tsmiClose);
|
|
|
|
|
|
|
|
|
|
return cms;
|
|
|
|
|
}
|
2014-08-11 03:36:08 +12:00
|
|
|
|
}
|
2014-08-11 22:40:14 +12:00
|
|
|
|
}
|