forked from stevencohn/OneMore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDetailView.cs
362 lines (283 loc) · 9.53 KB
/
DetailView.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//************************************************************************************************
// Copyright © 2022 Steven M. Cohn. All Rights Reserved.
//************************************************************************************************
#pragma warning disable CS0067 // The event is never used
namespace OneMoreCalendar
{
using River.OneMoreAddIn;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
internal partial class DetailView : ThemedUserControl, ICalendarView
{
private sealed class DayItem
{
public Rectangle Bounds { get; set; } = Rectangle.Empty;
public DateTime Date { get; set; }
public CalendarPages Pages { get; set; }
}
private const int HeadWidth = 170; // day
private const int PathWidth = 250; // path
private const int DateWidth = 170; // created, modified
private const int VPadding = 6;
private DayItem hotday;
private CalendarPage hotpage;
private readonly IntPtr hand;
private readonly Font hotFont;
private readonly Font deletedFont;
private readonly StringFormat format;
public DetailView()
{
InitializeComponent();
hand = Native.LoadCursor(IntPtr.Zero, Native.IDC_HAND);
hotFont = new Font(listbox.Font, FontStyle.Regular | FontStyle.Underline);
deletedFont = new Font(listbox.Font, FontStyle.Regular | FontStyle.Strikeout);
format = new StringFormat
{
Trimming = StringTrimming.EllipsisCharacter,
FormatFlags = StringFormatFlags.LineLimit | StringFormatFlags.NoWrap
};
}
public override void OnThemeChange()
{
BackColor = Theme.BackColor;
listbox.BackColor = BackColor;
}
public event CalendarDayHandler ClickedDay;
public event CalendarHoverHandler HoverPage;
public event CalendarPageHandler ClickedPage;
public event CalendarSnapshotHandler SnappedPage;
public void SetRange(DateTime startDate, DateTime endDate, CalendarPages pages)
{
SuspendLayout();
listbox.Items.Clear();
var date = startDate;
while (date <= endDate)
{
var settings = new SettingsProvider();
var daypages = new CalendarPages();
// filtering prioritizes modified over created and prevent pages from being
// displayed twice in the month if both created and modified in the same month
daypages.AddRange(pages.Where(p =>
(settings.Modified && p.Modified.Date.Equals(date)) ||
(!settings.Modified && p.Created.Date.Equals(date))
));
if (daypages.Any() || settings.Empty)
{
var item = new ListViewItem
{
Tag = new DayItem
{
Date = date,
Pages = daypages
}
};
listbox.Items.Add(item);
}
date = date.AddDays(1);
}
Invalidate();
ResumeLayout();
}
private void HeaderPanelPaint(object sender, PaintEventArgs e)
{
e.Graphics.Clear(BackColor);
using var font = new Font("Segoe UI Light", 10.0f, FontStyle.Regular);
headerPanel.Height = font.Height + VPadding;
var y = (headerPanel.Height - font.Height) / 2;
var size = e.Graphics.MeasureString("DATE", font);
var width = e.ClipRectangle.Width - SystemInformation.VerticalScrollBarWidth;
using var brush = new SolidBrush(Theme.MonthDayFore);
e.Graphics.DrawString("DATE", font, brush, (HeadWidth - size.Width) / 2, y);
e.Graphics.DrawString("SECTION", font, brush, HeadWidth + 20, y);
e.Graphics.DrawString("PAGE", font, brush, HeadWidth + PathWidth + 40, y);
e.Graphics.DrawString("CREATED", font, brush, width - DateWidth * 2, y);
e.Graphics.DrawString("MODIFIED", font, brush, width - DateWidth, y);
}
private void ListBoxMeasureItem(object sender, MeasureItemEventArgs e)
{
if (listbox.Items[e.Index] is ListViewItem item && item.Tag is DayItem day)
{
e.ItemHeight = day.Pages.Count > 1
? (day.Pages.Count * listbox.Font.Height) + (VPadding * 3)
: listbox.Font.Height + (VPadding * 2);
}
else
{
e.ItemHeight = listbox.Font.Height + (VPadding * 2);
}
// entire width of grid
e.ItemWidth = Width;
}
private void ListBoxDrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0)
{
return;
}
using var fill = new SolidBrush(e.Index % 2 == 1 ? Theme.DetailOddBack : Theme.DetailEvenBack);
e.Graphics.FillRectangle(fill, e.Bounds);
using var line = new Pen(Theme.MonthGrid);
e.Graphics.DrawLine(line, e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Top);
//e.Graphics.DrawLine(Pens.LightGray, HeadWidth, e.Bounds.Top, HeadWidth, e.Bounds.Bottom);
if (listbox.Items[e.Index] is ListViewItem item && item.Tag is DayItem day)
{
// set every time to handle scrolled view
day.Bounds = e.Bounds;
// header
var head = day.Date.ToString("ddd, MMM d");
var size = e.Graphics.MeasureString(head, listbox.Font);
using var fore = new SolidBrush(Theme.ForeColor);
using var gray = new SolidBrush(Color.Gray);
e.Graphics.DrawString(head, listbox.Font, fore,
(HeadWidth - size.Width) / 2,
e.Bounds.Top + (e.Bounds.Height - size.Height) / 2);
// pages
var top = e.Bounds.Top + VPadding;
foreach (var page in day.Pages)
{
// predict width of page title
size = e.Graphics.MeasureString(page.Title, listbox.Font);
var color = page.IsDeleted ? gray : fore;
// section
e.Graphics.DrawString(page.Path,
listbox.Font, color, HeadWidth + 20, top, format);
// title
var bounds = new Rectangle(
HeadWidth + PathWidth + 40, top, (int)size.Width + 2, (int)size.Height);
e.Graphics.DrawString(page.Title,
page.IsDeleted ? deletedFont : listbox.Font,
color, bounds, format);
page.Bounds = bounds;
// created
e.Graphics.DrawString(page.Created.ToShortFriendlyString(),
listbox.Font, color, e.Bounds.Width - DateWidth * 2, top);
// modified
e.Graphics.DrawString(page.Modified.ToShortFriendlyString(),
listbox.Font, color, e.Bounds.Width - DateWidth, top);
top += (int)size.Height;
}
}
}
private void ListBoxKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down)
{
listbox.ScrollDown();
//var index = listbox.IndexFromPoint(new Point(listbox.Left + 5, listbox.Bottom - 5));
//if (index < listbox.Items.Count - 1)
//{
// listbox.TopIndex = index + 1;
//}
}
else if (e.KeyCode == Keys.Up)
{
listbox.ScrollUp();
//var index = listbox.IndexFromPoint(new Point(listbox.Left + 5, listbox.Top + 5));
//if (index > 0)
//{
// listbox.TopIndex = index - 1;
//}
}
}
private void ListBoxMouseMove(object sender, MouseEventArgs e)
{
//Logger.Current.WriteLine($"moveto {e.Location}");
if (listbox.Items.OfType<ListViewItem>()
.FirstOrDefault(d =>
listbox.GetItemRectangle(listbox.Items.IndexOf(d)).Contains(e.Location))?
.Tag is not DayItem day)
{
return;
}
//Logger.Current.WriteLine($"day bounds {day.Bounds}");
var page = day.Pages.FirstOrDefault(p => p.Bounds.Contains(e.Location));
//Logger.Current.WriteLine($"page bounds {page.Bounds}");
if (page == hotpage)
{
if (hotpage != null)
{
Native.SetCursor(hand);
}
return;
}
using var g = listbox.CreateGraphics();
int index;
if (hotpage != null)
{
index = listbox.Items.OfType<ListViewItem>()
.Where(item => item.Tag == hotday)
.Select(item => listbox.Items.IndexOf(item))
.FirstOrDefault();
using var fill = new SolidBrush(index % 2 == 1 ? Theme.DetailOddBack : Theme.DetailEvenBack);
g.FillRectangle(fill, hotpage.Bounds);
using var fore = new SolidBrush(hotpage.IsDeleted ? Color.Gray : Theme.ForeColor);
g.DrawString(hotpage.Title,
hotpage.IsDeleted ? deletedFont : listbox.Font,
fore,
hotpage.Bounds, format);
HoverPage?.Invoke(this, new CalendarPageEventArgs(null));
hotday = null;
hotpage = null;
}
if (page != null)
{
index = listbox.Items.OfType<ListViewItem>()
.Where(item => item.Tag == day)
.Select(item => listbox.Items.IndexOf(item))
.FirstOrDefault();
using var fill2 = new SolidBrush(index % 2 == 1 ? Theme.DetailOddBack : Theme.DetailEvenBack);
g.FillRectangle(fill2, page.Bounds);
using var fore2 = new SolidBrush(Theme.Highlight);
g.DrawString(page.Title,
page.IsDeleted ? deletedFont : hotFont,
fore2, page.Bounds, format);
HoverPage?.Invoke(this, new CalendarPageEventArgs(page));
hotday = day;
hotpage = page;
Native.SetCursor(hand);
}
}
private void ListBoxResize(object sender, EventArgs e)
{
headerPanel.Invalidate();
listbox.Invalidate();
}
/*
* Note that MouseClick event doesn't capture right-clicks but MouseUp does.
* Also note that if the control overrides OnMouseClick then that *will* capture both
* left and right buttons. Windows Forms is fun!
*
*/
private void ListBoxMouseUp(object sender, MouseEventArgs e)
{
if (listbox.Items.OfType<ListViewItem>()
.FirstOrDefault(d =>
listbox.GetItemRectangle(listbox.Items.IndexOf(d)).Contains(e.Location))?
.Tag is not DayItem day)
{
return;
}
var page = day.Pages.FirstOrDefault(p => p.Bounds.Contains(e.Location));
if (page == null)
{
return;
}
if (e.Button == MouseButtons.Right)
{
SnappedPage?.Invoke(this, new CalendarSnapshotEventArgs(page, page.Bounds));
}
else
{
ClickedPage?.Invoke(this, new CalendarPageEventArgs(page));
}
}
private void ListBoxScrolled(object sender, ScrollEventArgs e)
{
listbox.Invalidate();
Logger.Current.WriteLine(
$"scrolled {e.Type} @ {e.ScrollOrientation}, {e.OldValue} >> {e.NewValue}");
}
}
}