MenuButton.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // The Command & Conquer Map Editor and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // The Command & Conquer Map Editor and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. using System.ComponentModel;
  15. using System.Drawing;
  16. using System.Drawing.Drawing2D;
  17. using System.Windows.Forms;
  18. namespace MobiusEditor.Controls
  19. {
  20. public partial class MenuButton : Button
  21. {
  22. public const int DefaultSplitWidth = 20;
  23. [DefaultValue(null), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  24. public ContextMenuStrip Menu { get; set; }
  25. [DefaultValue(DefaultSplitWidth), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  26. public int SplitWidth { get; set; } = DefaultSplitWidth;
  27. public MenuButton()
  28. {
  29. InitializeComponent();
  30. }
  31. protected override void OnMouseDown(MouseEventArgs mevent)
  32. {
  33. var splitRect = new Rectangle(Width - SplitWidth, 0, SplitWidth, Height);
  34. if ((Menu != null) && (mevent.Button == MouseButtons.Left) && splitRect.Contains(mevent.Location))
  35. {
  36. Menu.Show(this, 0, Height);
  37. }
  38. else
  39. {
  40. base.OnMouseDown(mevent);
  41. }
  42. }
  43. protected override void OnPaint(PaintEventArgs pevent)
  44. {
  45. base.OnPaint(pevent);
  46. if ((Menu != null) && (SplitWidth > 0))
  47. {
  48. int arrowX = ClientRectangle.Width - 14;
  49. int arrowY = ClientRectangle.Height / 2 - 1;
  50. var arrowBrush = Enabled ? SystemBrushes.ControlText : SystemBrushes.ButtonShadow;
  51. var arrows = new[] { new Point(arrowX, arrowY), new Point(arrowX + 7, arrowY), new Point(arrowX + 3, arrowY + 4) };
  52. pevent.Graphics.FillPolygon(arrowBrush, arrows);
  53. int lineX = ClientRectangle.Width - SplitWidth;
  54. int lineYFrom = arrowY - 4;
  55. int lineYTo = arrowY + 8;
  56. using (var separatorPen = new Pen(Brushes.DarkGray) { DashStyle = DashStyle.Dot })
  57. {
  58. pevent.Graphics.DrawLine(separatorPen, lineX, lineYFrom, lineX, lineYTo);
  59. }
  60. }
  61. }
  62. }
  63. }