MoonTabs.cs 15 KB

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