MoonTabs.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using System.Drawing.Drawing2D;
  5. using System.Runtime.InteropServices;
  6. using System.Security.Permissions;
  7. using System.Windows.Forms;
  8. namespace Optimizer
  9. {
  10. [
  11. ComVisible(true), ClassInterface(ClassInterfaceType.AutoDispatch),
  12. DefaultProperty("TabPages"), DefaultEvent("SelectedIndexChanged")
  13. ]
  14. public sealed class MoonTabs : TabControl
  15. {
  16. [System.Runtime.InteropServices.DllImport("user32.dll")]
  17. private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
  18. [Category("Custom"), Description("Indicates whether or not you the components Tabpages Headers have border edges."), DefaultValue(false)]
  19. public bool BorderEdges { get; set; } = false;
  20. private int _BorderSize = 0;
  21. [Category("Custom"), Description("The size of the components border."), DefaultValue(0)]
  22. public int BorderSize
  23. {
  24. get => _BorderSize;
  25. set => _BorderSize = 0;
  26. }
  27. [Category("Custom"), Description("The size of the components Indicator."), DefaultValue(0)]
  28. public int IndicatorSize { get; set; } = 0;
  29. private int _DividerSize = 0;
  30. [Category("Custom"), Description("The size of the components Divider."), DefaultValue(0)]
  31. public int DividerSize
  32. {
  33. get => _DividerSize;
  34. set => _DividerSize = value.LimitToRange(0, 0);
  35. }
  36. [Category("Custom"), Description("Indicates whether or not you can rearrange the components Tabpages. CLICK on the Tabpages HEADER with the LEFT mousebutton and HOLD DOWN the <SHIFT> KEY, to drag it LEFT or RIGHT."), DefaultValue(false)]
  37. public bool CanDrag { get; set; }
  38. StringFormat format = new StringFormat { Alignment = StringAlignment.Center };
  39. private Bitmap bitDrag = default(Bitmap);
  40. private bool bDrag, bMouseDown, bShiftKey;
  41. private Point ptPreviousLocation, ptMaxDrag;
  42. private int DraggedIndex = -1;
  43. protected override CreateParams CreateParams
  44. {
  45. [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  46. get
  47. {
  48. CreateParams cp = base.CreateParams;
  49. cp.ExStyle |= 0x02000000;
  50. return cp;
  51. }
  52. }
  53. // OVERRIDE TAB HEADER WIDTH
  54. protected override void OnHandleCreated(EventArgs e)
  55. {
  56. base.OnHandleCreated(e);
  57. // Send TCM_SETMINTABWIDTH
  58. string maxTitle = string.Empty;
  59. foreach (TabPage x in this.TabPages)
  60. {
  61. if (x.Text.Length > maxTitle.Length) maxTitle = x.Text;
  62. }
  63. Size textSize = TextRenderer.MeasureText(maxTitle, this.Font);
  64. SendMessage(this.Handle, 0x1300 + 49, IntPtr.Zero, (IntPtr)textSize.Width);
  65. }
  66. public MoonTabs()
  67. {
  68. SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer |
  69. ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.CacheText, true);
  70. Alignment = TabAlignment.Top;
  71. Margin = new Padding(0);
  72. Padding = new Point(0, 0);
  73. this.SizeMode = TabSizeMode.Fixed;
  74. }
  75. private void SetDragState() => bDrag = (CanDrag && bMouseDown && bShiftKey);
  76. protected override void OnMouseDown(MouseEventArgs e)
  77. {
  78. bMouseDown = true;
  79. SetDragState();
  80. if (SelectedIndex == -1) return;
  81. Rectangle rectDrag = GetTabRect(SelectedIndex);
  82. ptPreviousLocation = new Point(rectDrag.X, rectDrag.Y);
  83. rectDrag.Width += 1; rectDrag.Height += 1;
  84. Bitmap src = new Bitmap(Width, Height);
  85. DrawToBitmap(src, ClientRectangle);
  86. using (Graphics g = Graphics.FromImage(bitDrag = new Bitmap(rectDrag.Width, rectDrag.Height)))
  87. {
  88. g.DrawImage(src, new Rectangle(0, 0, bitDrag.Width, bitDrag.Height), rectDrag, GraphicsUnit.Pixel);
  89. }
  90. }
  91. protected override void OnMouseMove(MouseEventArgs e)
  92. {
  93. if (bDrag)
  94. {
  95. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  96. {
  97. ptPreviousLocation = new Point(((e.X < 0) ? 0 : (e.X > ptMaxDrag.X) ? ptMaxDrag.X : e.X), (Alignment == TabAlignment.Top ? BorderSize : ptMaxDrag.Y));
  98. }
  99. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  100. {
  101. ptPreviousLocation = new Point(ptMaxDrag.X, ((e.Y < 0) ? 0 : (e.Y > ptMaxDrag.Y) ? ptMaxDrag.Y : e.Y));
  102. }
  103. for (int i = 0; i < TabCount; i++)
  104. {
  105. if (GetTabRect(i).Contains(PointToClient(Cursor.Position))) { DraggedIndex = i; break; }
  106. }
  107. Invalidate();
  108. }
  109. }
  110. protected override void OnMouseUp(MouseEventArgs e)
  111. {
  112. void SwapTabPages(TabPage inDestTab)
  113. {
  114. int SourceIndex = TabPages.IndexOf(SelectedTab);
  115. int DestinationIndex = TabPages.IndexOf(inDestTab);
  116. TabPages[DestinationIndex] = SelectedTab;
  117. TabPages[SourceIndex] = inDestTab;
  118. if (SelectedIndex == SourceIndex) { SelectedIndex = DestinationIndex; }
  119. else if (SelectedIndex == DestinationIndex) { SelectedIndex = SourceIndex; }
  120. }
  121. bDrag = bMouseDown = false;
  122. if (DraggedIndex > -1)
  123. {
  124. SwapTabPages(TabPages[DraggedIndex]);
  125. DraggedIndex = -1;
  126. }
  127. SetDragState();
  128. Invalidate();
  129. }
  130. protected override void OnKeyDown(KeyEventArgs ke) { bShiftKey = ke.Shift; SetDragState(); }
  131. protected override void OnKeyUp(KeyEventArgs e) { bDrag = bShiftKey = false; SetDragState(); }
  132. protected override void OnPaint(PaintEventArgs e)
  133. {
  134. if (DesignMode) return;
  135. e.Graphics.Clear(Color.FromArgb(40, 40, 40));
  136. Rectangle container = new Rectangle(0, 0, Width - (BorderSize % 2), Height - (BorderSize % 2));
  137. Rectangle containerHead = default(Rectangle);
  138. if (TabCount > 0)
  139. {
  140. using (SolidBrush brushBackgroundTab = new SolidBrush(Color.FromArgb(40, 40, 40)))
  141. using (SolidBrush brushDivider = new SolidBrush(OptionsHelper.ForegroundColor))
  142. {
  143. {
  144. e.Graphics.FillRectangle(brushBackgroundTab, DisplayRectangle);
  145. }
  146. {
  147. if (SelectedIndex == -1) return;
  148. Rectangle rectDivider = GetTabRect(SelectedIndex);
  149. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  150. {
  151. e.Graphics.FillRectangle(brushDivider,
  152. 0,
  153. (((Alignment == TabAlignment.Top) ? (TabPages[SelectedIndex].Top - DividerSize - (DividerSize % 2)) :
  154. (TabPages[SelectedIndex].Bottom + (DividerSize % 2)))),
  155. (Width - BorderSize), DividerSize
  156. );
  157. }
  158. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  159. {
  160. e.Graphics.FillRectangle(brushDivider,
  161. ((Alignment == TabAlignment.Right) ? (TabPages[SelectedIndex].Right + (DividerSize % 2)) : TabPages[SelectedIndex].Left - DividerSize - (DividerSize % 2)),
  162. BorderSize,
  163. DividerSize,
  164. (Height - (BorderSize * 2))
  165. );
  166. }
  167. }
  168. }
  169. }
  170. SolidBrush brushActiveText;
  171. using (Pen penActive = new Pen(OptionsHelper.ForegroundColor))
  172. using (Pen penBorder = new Pen(Color.FromArgb(40, 40, 40), 0))
  173. using (SolidBrush brushActive = new SolidBrush(OptionsHelper.ForegroundColor))
  174. using (SolidBrush brushInActive = new SolidBrush(Color.FromArgb(40, 40, 40)))
  175. using (SolidBrush brushAlternative = new SolidBrush(OptionsHelper.ForegroundColor))
  176. using (SolidBrush brushActiveIndicator = new SolidBrush(ControlPaint.Light(OptionsHelper.ForegroundColor)))
  177. using (SolidBrush brushInActiveIndicator = new SolidBrush(OptionsHelper.ForegroundColor))
  178. using (brushActiveText = new SolidBrush(OptionsHelper.TextColor))
  179. using (SolidBrush brushInActiveText = new SolidBrush(Color.White))
  180. using (SolidBrush brushDrag = new SolidBrush(ControlPaint.Dark(OptionsHelper.ForegroundColor)))
  181. {
  182. //if (MoonManager.THEME_PREFERENCE == THEME.LIGHT) brushActiveText = new SolidBrush(Color.White);
  183. penBorder.Alignment = penActive.Alignment = PenAlignment.Inset;
  184. e.Graphics.DrawRectangle(penBorder, container);
  185. if (TabCount > 0)
  186. {
  187. ptMaxDrag = new Point(0, 0);
  188. for (int i = 0; i < TabCount; i++)
  189. {
  190. containerHead = GetTabRect(i);
  191. e.Graphics.FillRectangle((SelectedIndex == i) ? (bDrag ? brushDrag : brushActive) : brushInActive, containerHead);
  192. if (BorderEdges && (i == SelectedIndex))
  193. {
  194. Point ptA = new Point(0, 0); Point ptB = new Point(0, 0);
  195. Point ptC = new Point(0, 0); Point ptD = new Point(0, 0);
  196. ptA.X = ptB.X = ptD.X = containerHead.X;
  197. ptA.Y = ptB.Y = ptC.Y = containerHead.Y;
  198. ptA.Y = ptC.Y = ptD.Y = containerHead.Y + containerHead.Height - 1;
  199. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  200. {
  201. ptD.X = ptC.X = containerHead.X + containerHead.Width;
  202. ptC.Y = containerHead.Y;
  203. if (Alignment == TabAlignment.Bottom)
  204. {
  205. MoonTabHelper.Swap(ref ptA, ref ptB); MoonTabHelper.Swap(ref ptC, ref ptD);
  206. }
  207. }
  208. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  209. {
  210. ptA.Y = containerHead.Y;
  211. ptB.X = ptC.X = containerHead.X + containerHead.Width - 1;
  212. if (Alignment == TabAlignment.Left)
  213. {
  214. MoonTabHelper.Swap(ref ptA, ref ptC); MoonTabHelper.Swap(ref ptB, ref ptD);
  215. }
  216. }
  217. e.Graphics.DrawLine(new Pen(ControlPaint.Light(brushActive.Color)), ptA, ptB);
  218. e.Graphics.DrawLine(new Pen(ControlPaint.Light(brushActive.Color)), ptB, ptC);
  219. e.Graphics.DrawLine(new Pen(ControlPaint.Dark(brushActive.Color)), ptC, ptD);
  220. }
  221. {
  222. Rectangle rectDivider = default(Rectangle);
  223. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom)
  224. {
  225. rectDivider = new Rectangle(containerHead.X, containerHead.Y + ((Alignment == TabAlignment.Top) ? containerHead.Height : -DividerSize), containerHead.Width, DividerSize);
  226. }
  227. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left)
  228. {
  229. rectDivider = new Rectangle(containerHead.X - ((Alignment == TabAlignment.Right) ? DividerSize : -containerHead.Width), containerHead.Y, DividerSize, containerHead.Height);
  230. }
  231. e.Graphics.FillRectangle(((MoonTabHelper.TagToInt(TabPages[i]) == 1) ? brushAlternative : ((i == SelectedIndex) ? brushActiveIndicator : brushInActiveIndicator)), rectDivider);
  232. }
  233. if (!(bDrag && i == SelectedIndex))
  234. {
  235. int angle = 0;
  236. {
  237. if (Alignment == TabAlignment.Right) angle = 90;
  238. if (Alignment == TabAlignment.Left) angle = 270;
  239. }
  240. float w, h;
  241. w = h = 0f;
  242. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom) { w = containerHead.X + (containerHead.Width / 2); }
  243. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left) { w = containerHead.X; h = containerHead.Y + (containerHead.Height / 2); }
  244. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom) { h = containerHead.Y + ((Alignment == TabAlignment.Top) ? IndicatorSize : 0) + ((containerHead.Height - IndicatorSize) / 2); }
  245. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left) { w += (((Alignment == TabAlignment.Right) ? 0 : IndicatorSize) + ((containerHead.Width - IndicatorSize) / 2)); }
  246. e.Graphics.TranslateTransform(w, h);
  247. {
  248. Size textSize = e.Graphics.MeasureString(TabPages[i].Text, Font).ToSize();
  249. e.Graphics.RotateTransform(angle);
  250. e.Graphics.DrawString(TabPages[i].Text, Font, ((SelectedIndex == i) ? brushActiveText : brushInActiveText), new PointF((-textSize.Width / 2f), (-textSize.Height / 2f)));
  251. }
  252. e.Graphics.ResetTransform();
  253. }
  254. if (bMouseDown)
  255. {
  256. if (Alignment == TabAlignment.Top || Alignment == TabAlignment.Bottom) { if (i > 0) { ptMaxDrag.X += GetTabRect(i).Width; } }
  257. if (Alignment == TabAlignment.Top) { ptMaxDrag.Y = BorderSize; }
  258. if (Alignment == TabAlignment.Bottom) { ptMaxDrag.Y = containerHead.Y; };
  259. if (Alignment == TabAlignment.Right || Alignment == TabAlignment.Left) { ptMaxDrag.X = containerHead.X; if (i > 0) { ptMaxDrag.Y += containerHead.Height; } }
  260. }
  261. if (bDrag && (bitDrag != null)) { e.Graphics.DrawImage(bitDrag, new Point(ptPreviousLocation.X, ptPreviousLocation.Y)); }
  262. }
  263. }
  264. }
  265. }
  266. }
  267. public static class MoonTabHelper
  268. {
  269. public static int LimitToRange(this int value, int inclusiveMinimum, int inclusiveMaximum)
  270. {
  271. if (value < inclusiveMinimum) { return inclusiveMinimum; }
  272. if (value > inclusiveMaximum) { return inclusiveMaximum; }
  273. return value;
  274. }
  275. public static void Swap<T>(ref T a, ref T b)
  276. {
  277. T temp = a;
  278. a = b; b = temp;
  279. }
  280. public static int TagToInt(object inObject) => Convert.ToInt32((inObject as Control).Tag);
  281. }
  282. }