/
MPfmTabButton.cs
168 lines (147 loc) · 6.02 KB
/
MPfmTabButton.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright © 2011-2013 Yanick Castonguay
//
// This file is part of MPfm.
//
// MPfm 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 3 of the License, or
// (at your option) any later version.
//
// MPfm 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 MPfm. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Reflection;
using MonoMac.AppKit;
using MonoMac.CoreGraphics;
using MonoMac.Foundation;
using MPfm.Mac.Classes.Helpers;
using MPfm.Mac.Classes.Objects;
namespace MPfm.Mac.Classes.Controls
{
[Register("MPfmTabButton")]
public class MPfmTabButton : NSButton
{
bool _isMouseDown = false;
bool _isMouseOver = false;
public CGColor TextColor { get; set; }
public CGColor BackgroundColor { get; set; }
public CGColor BackgroundMouseDownColor { get; set; }
public CGColor BackgroundMouseOverColor { get; set; }
public CGColor BorderColor { get; set; }
private bool _isSelected = false;
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
_isSelected = value;
SetNeedsDisplay();
}
}
public delegate void TabButtonSelected(MPfmTabButton button);
public event TabButtonSelected OnTabButtonSelected;
[Export("init")]
public MPfmTabButton() : base(NSObjectFlag.Empty)
{
Initialize();
}
// Called when created from unmanaged code
public MPfmTabButton(IntPtr handle) : base (handle)
{
Initialize();
}
private void Initialize()
{
TextColor = GlobalTheme.ButtonTextColor;
//BackgroundColor = GlobalTheme.ButtonToolbarBackgroundColor;
BackgroundColor = GlobalTheme.PanelHeaderColor1;
BackgroundMouseDownColor = GlobalTheme.ButtonToolbarBackgroundMouseOverColor;
BackgroundMouseOverColor = GlobalTheme.ButtonToolbarBackgroundMouseDownColor;
BorderColor = GlobalTheme.ButtonToolbarBorderColor;
// This allows MouseEntered and MouseExit to work
AddTrackingRect(Bounds, this, IntPtr.Zero, false);
}
[Export("mouseDown:")]
public override void MouseDown(NSEvent theEvent)
{
_isMouseDown = true;
base.MouseDown(theEvent);
this.MouseUp(theEvent);
}
[Export("mouseUp:")]
public override void MouseUp(NSEvent theEvent)
{
base.MouseUp(theEvent);
_isMouseDown = false;
IsSelected = true;
if(OnTabButtonSelected != null)
OnTabButtonSelected(this);
SetNeedsDisplay();
}
[Export("mouseEntered:")]
public override void MouseEntered(NSEvent theEvent)
{
base.MouseEntered(theEvent);
_isMouseOver = true;
SetNeedsDisplay();
}
[Export("mouseExited:")]
public override void MouseExited(NSEvent theEvent)
{
base.MouseExited(theEvent);
_isMouseOver = false;
SetNeedsDisplay();
}
public override void DrawRect(RectangleF dirtyRect)
{
float padding = 4;
CGContext context = NSGraphicsContext.CurrentContext.GraphicsPort;
context.SaveState();
context.TranslateCTM(0, Bounds.Height);
context.ScaleCTM(1, -1);
if (_isMouseDown)
CocoaHelper.FillRect(context, Bounds, BackgroundMouseDownColor);
else if (_isMouseOver)
CocoaHelper.FillRect(context, Bounds, BackgroundMouseOverColor);
else
CocoaHelper.FillRect(context, Bounds, BackgroundColor);
if(IsSelected)
CocoaHelper.DrawLine(context, new PointF[2] { new PointF(0, 1), new PointF(Bounds.Width, 1) }, 0.5f, new CGColor(0.4f, 1, 1, 1));
//CocoaHelper.DrawRect(context, Bounds, BorderColor);
//RectangleF rectTextSize = CocoaHelper.MeasureString(Bounds.Size, Title, "Junction", 11);
RectangleF rectTextSize = CocoaHelper.MeasureString(Bounds.Size, Title, "TitilliumText25L-800wt", 12);
RectangleF rectText;
if (Image != null)
{
float xImage = ((Bounds.Width - rectTextSize.Width - (padding * 2) - Image.Size.Width) / 2);
RectangleF rectImage = new RectangleF(xImage, (Bounds.Height - Image.Size.Height) / 2, Image.Size.Width, Image.Size.Height);
Image.DrawInRect(rectImage, new RectangleF(0, 0, Image.Size.Width, Image.Size.Height), NSCompositingOperation.SourceOver, 1.0f);
float xText = xImage + padding + Image.Size.Width + padding;
rectText = new RectangleF(xText, (Bounds.Height - rectTextSize.Height) / 2, rectTextSize.Width, rectTextSize.Height);
}
else
{
rectText = new RectangleF((Bounds.Width - rectTextSize.Width) / 2, (Bounds.Height - rectTextSize.Height) / 2, rectTextSize.Width, rectTextSize.Height);
}
context.RestoreState();
//CocoaHelper.DrawText(rectText, 0, 0, Title, "Junction", 11, NSColor.White);
CocoaHelper.DrawText(rectText, 0, 0, Title, "TitilliumText25L-800wt", 12, NSColor.White);
}
RectangleF Get1pxRect(RectangleF rect)
{
RectangleF newRect = new RectangleF(rect.X + 0.5f, rect.Y + 0.5f, rect.Width - 1, rect.Height - 1);
return newRect;
}
}
}