forked from ShareX/ShareX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistoryImageListViewRenderer.cs
177 lines (154 loc) · 7.7 KB
/
HistoryImageListViewRenderer.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
169
170
171
172
173
174
175
176
177
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2022 ShareX Team
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)
using Manina.Windows.Forms;
using ShareX.HelpersLib;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace ShareX.HistoryLib
{
public class HistoryImageListViewRenderer : ImageListView.ImageListViewRenderer
{
public override void InitializeGraphics(Graphics g)
{
base.InitializeGraphics(g);
ItemDrawOrder = ItemDrawOrder.NormalSelectedHovered;
if (ShareXResources.UseCustomTheme)
{
ImageListView.BackColor = ShareXResources.Theme.BackgroundColor;
ImageListView.Colors.BackColor = ShareXResources.Theme.LightBackgroundColor;
ImageListView.Colors.BorderColor = ShareXResources.Theme.BorderColor;
ImageListView.Colors.ForeColor = ShareXResources.Theme.TextColor;
ImageListView.Colors.SelectedForeColor = ShareXResources.Theme.TextColor;
ImageListView.Colors.UnFocusedForeColor = ShareXResources.Theme.TextColor;
Color hoverColor;
if (ShareXResources.IsDarkTheme)
{
hoverColor = ColorHelpers.LighterColor(ShareXResources.Theme.LightBackgroundColor, 0.1f);
}
else
{
hoverColor = ColorHelpers.DarkerColor(ShareXResources.Theme.LightBackgroundColor, 0.1f);
}
ImageListView.Colors.SelectedColor1 = ImageListView.Colors.HoverColor1 = ImageListView.Colors.UnFocusedColor1 =
ImageListView.Colors.SelectedColor2 = ImageListView.Colors.HoverColor2 = ImageListView.Colors.UnFocusedColor2 = hoverColor;
}
else
{
ImageListView.Colors.BackColor = SystemColors.Control;
ImageListView.Colors.SelectedColor1 = ImageListView.Colors.HoverColor1 = ImageListView.Colors.UnFocusedColor1 =
ImageListView.Colors.SelectedColor2 = ImageListView.Colors.HoverColor2 = ImageListView.Colors.UnFocusedColor2 = SystemColors.ControlLight;
}
}
public override void DrawItem(Graphics g, ImageListViewItem item, ItemState state, Rectangle bounds)
{
Clip = false;
Size itemPadding = new Size(4, 4);
Rectangle imageBounds = bounds;
string text = Path.GetFileNameWithoutExtension(item.Text);
Size szt = TextRenderer.MeasureText(text, ImageListView.Font);
int textWidth = szt.Width + (itemPadding.Width * 2);
if ((state & ItemState.Hovered) != ItemState.None && textWidth > bounds.Width)
{
bounds = new Rectangle(bounds.X + (bounds.Width / 2) - (textWidth / 2), bounds.Y, textWidth, bounds.Height);
}
// Paint background
if (ImageListView.Enabled)
{
using (Brush bItemBack = new SolidBrush(ImageListView.Colors.BackColor))
{
g.FillRectangle(bItemBack, bounds);
}
}
else
{
using (Brush bItemBack = new SolidBrush(ImageListView.Colors.DisabledBackColor))
{
g.FillRectangle(bItemBack, bounds);
}
}
if ((state & ItemState.Disabled) != ItemState.None) // Paint background Disabled
{
using (Brush bDisabled = new LinearGradientBrush(bounds.Offset(1), ImageListView.Colors.DisabledColor1, ImageListView.Colors.DisabledColor2, LinearGradientMode.Vertical))
{
g.FillRectangle(bDisabled, bounds);
}
}
else if ((ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None)) ||
(!ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None) && ((state & ItemState.Hovered) != ItemState.None))) // Paint background Selected
{
using (Brush bSelected = new LinearGradientBrush(bounds.Offset(1), ImageListView.Colors.SelectedColor1, ImageListView.Colors.SelectedColor2, LinearGradientMode.Vertical))
{
g.FillRectangle(bSelected, bounds);
}
}
else if (!ImageListView.Focused && ((state & ItemState.Selected) != ItemState.None)) // Paint background unfocused
{
using (Brush bGray64 = new LinearGradientBrush(bounds.Offset(1), ImageListView.Colors.UnFocusedColor1, ImageListView.Colors.UnFocusedColor2, LinearGradientMode.Vertical))
{
g.FillRectangle(bGray64, bounds);
}
}
// Paint background Hovered
if ((state & ItemState.Hovered) != ItemState.None)
{
using (Brush bHovered = new LinearGradientBrush(bounds.Offset(1), ImageListView.Colors.HoverColor1, ImageListView.Colors.HoverColor2, LinearGradientMode.Vertical))
{
g.FillRectangle(bHovered, bounds);
}
}
// Draw the image
Image img = item.GetCachedImage(CachedImageType.Thumbnail);
if (img != null)
{
Rectangle pos = Utility.GetSizedImageBounds(img, new Rectangle(imageBounds.Location + itemPadding, ImageListView.ThumbnailSize));
g.DrawImage(img, pos);
}
// Draw item text
Color foreColor = ImageListView.Colors.ForeColor;
if ((state & ItemState.Disabled) != ItemState.None)
{
foreColor = ImageListView.Colors.DisabledForeColor;
}
else if ((state & ItemState.Selected) != ItemState.None)
{
if (ImageListView.Focused)
{
foreColor = ImageListView.Colors.SelectedForeColor;
}
else
{
foreColor = ImageListView.Colors.UnFocusedForeColor;
}
}
Rectangle rt = new Rectangle(bounds.Left, bounds.Top + (2 * itemPadding.Height) + ImageListView.ThumbnailSize.Height, bounds.Width, szt.Height);
TextFormatFlags flags;
if ((state & ItemState.Hovered) != ItemState.None)
{
flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.NoClipping;
}
else
{
flags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter | TextFormatFlags.SingleLine | TextFormatFlags.WordEllipsis;
}
TextRenderer.DrawText(g, text, ImageListView.Font, rt, foreColor, flags);
}
}
}