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
|
2020-02-05 20:19:48 +13:00
|
|
|
|
Copyright (c) 2007-2020 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
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
public class CodeMenu : ContextMenuStrip
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
public Point MenuLocation
|
2017-08-19 07:50:47 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
get
|
2017-08-19 07:50:47 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (MenuLocationBottom)
|
|
|
|
|
{
|
|
|
|
|
return new Point(0, textBoxBase.Height + 1);
|
|
|
|
|
}
|
2017-08-19 07:50:47 +12:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
return new Point(textBoxBase.Width + 1, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-08-19 07:50:47 +12:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
public bool MenuLocationBottom { get; set; }
|
2014-08-11 03:36:08 +12:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
private TextBoxBase textBoxBase;
|
2019-01-22 20:38:04 +13:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
public CodeMenu(TextBoxBase tbb, CodeMenuItem[] items)
|
2019-01-22 20:38:04 +13:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase = tbb;
|
|
|
|
|
|
|
|
|
|
Font = new Font("Lucida Console", 8);
|
|
|
|
|
AutoClose = textBoxBase == null;
|
|
|
|
|
ShowImageMargin = false;
|
2019-01-22 20:38:04 +13:00
|
|
|
|
|
2020-02-09 04:07:17 +13:00
|
|
|
|
if (ShareXResources.ExperimentalCustomTheme)
|
2019-06-25 07:52:20 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
Opacity = ShareXResources.Theme.ContextMenuOpacityDouble;
|
|
|
|
|
Renderer = new ToolStripDarkRenderer();
|
2019-06-25 07:52:20 +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 };
|
2020-04-28 13:44:37 +12:00
|
|
|
|
|
2019-01-25 02:53:00 +13:00
|
|
|
|
tsmi.MouseUp += (sender, e) =>
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (textBoxBase != null && e.Button == MouseButtons.Left)
|
2019-01-25 02:53:00 +13:00
|
|
|
|
{
|
|
|
|
|
string text = ((ToolStripMenuItem)sender).Tag.ToString();
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase.AppendTextToSelection(text);
|
2019-01-25 02:53:00 +13:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
Close();
|
2019-01-25 02:53:00 +13:00
|
|
|
|
}
|
2014-08-11 03:36:08 +12:00
|
|
|
|
};
|
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
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
Items.Add(tsmi);
|
2015-09-23 15:01:59 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ToolStripMenuItem tsmiParent;
|
2020-04-28 13:44:37 +12:00
|
|
|
|
int index = 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();
|
2020-04-28 13:44:37 +12:00
|
|
|
|
Items.Add(tsmiParent);
|
2015-09-23 15:01:59 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
tsmiParent = Items[index] as ToolStripMenuItem;
|
2015-09-23 15:01:59 +12:00
|
|
|
|
}
|
|
|
|
|
tsmiParent.DropDownItems.Add(tsmi);
|
|
|
|
|
}
|
2014-08-11 03:36:08 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
Items.Add(new ToolStripSeparator());
|
2014-08-11 03:36:08 +12:00
|
|
|
|
|
2014-10-18 00:08:33 +13:00
|
|
|
|
ToolStripMenuItem tsmiClose = new ToolStripMenuItem(Resources.CodeMenu_Create_Close);
|
2020-04-28 13:44:37 +12:00
|
|
|
|
tsmiClose.Click += (sender, e) => Close();
|
|
|
|
|
Items.Add(tsmiClose);
|
2014-08-11 03:36:08 +12:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (textBoxBase != null)
|
2014-08-11 03:36:08 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase.MouseDown += (sender, e) =>
|
2014-09-14 00:07:05 +12:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (Items.Count > 0) Show(textBoxBase, MenuLocation);
|
2019-01-25 04:05:24 +13:00
|
|
|
|
};
|
2014-08-11 03:36:08 +12:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase.GotFocus += (sender, e) =>
|
2019-01-25 04:05:24 +13:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (Items.Count > 0) Show(textBoxBase, MenuLocation);
|
2019-01-25 04:05:24 +13:00
|
|
|
|
};
|
2014-11-20 07:27:46 +13:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase.LostFocus += (sender, e) =>
|
2019-01-25 04:05:24 +13:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (Visible) Close();
|
2019-01-25 04:05:24 +13:00
|
|
|
|
};
|
2014-11-20 07:27:46 +13:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase.KeyDown += (sender, e) =>
|
2014-11-20 07:27:46 +13:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if ((e.KeyCode == Keys.Enter || e.KeyCode == Keys.Escape) && Visible)
|
2019-01-25 04:05:24 +13:00
|
|
|
|
{
|
2020-04-28 13:44:37 +12:00
|
|
|
|
Close();
|
2019-01-25 04:05:24 +13:00
|
|
|
|
e.SuppressKeyPress = true;
|
|
|
|
|
}
|
|
|
|
|
};
|
2014-11-20 07:27:46 +13:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
textBoxBase.Disposed += (sender, e) => Dispose();
|
2014-11-20 07:27:46 +13:00
|
|
|
|
}
|
2020-04-28 13:44:37 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CodeMenu Create<TEntry>(TextBoxBase tb, TEntry[] ignoreList, CodeMenuItem[] extraItems) where TEntry : CodeMenuEntry
|
|
|
|
|
{
|
|
|
|
|
List<CodeMenuItem> items = new List<CodeMenuItem>();
|
2014-11-20 07:27:46 +13:00
|
|
|
|
|
2020-04-28 13:44:37 +12:00
|
|
|
|
if (extraItems != null)
|
|
|
|
|
{
|
|
|
|
|
items.AddRange(extraItems);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerable<CodeMenuItem> codeMenuItems = Helpers.GetValueFields<TEntry>().Where(x => !ignoreList.Contains(x)).
|
|
|
|
|
Select(x => new CodeMenuItem(x.ToPrefixString(), x.Description, x.Category));
|
|
|
|
|
|
|
|
|
|
items.AddRange(codeMenuItems);
|
|
|
|
|
|
|
|
|
|
return new CodeMenu(tb, items.ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CodeMenu Create<TEntry>(TextBoxBase tb, params TEntry[] ignoreList) where TEntry : CodeMenuEntry
|
|
|
|
|
{
|
|
|
|
|
return Create(tb, ignoreList, (CodeMenuItem[])null);
|
2014-11-20 07:27:46 +13:00
|
|
|
|
}
|
2014-08-11 03:36:08 +12:00
|
|
|
|
}
|
2014-08-11 22:40:14 +12:00
|
|
|
|
}
|