yangyin
2025-02-24 b3b65a002fe68b1383314098790f5a153b87765f
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Child.Crown;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using static DPumpHydr.WinFrmUI.RLT.Helper.CrownHelper;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region CrownListView
 
    public class CrownListView : CrownScrollView
    {
        #region Event Region
 
        public event EventHandler SelectedIndicesChanged;
 
        #endregion
 
        #region Field Region
 
        private int _itemHeight = 20;
        private readonly int _iconSize = 16;
 
        private ObservableCollection<CrownListItem> _items;
        private int _anchoredItemStart = -1;
        private int _anchoredItemEnd = -1;
 
        #endregion
 
        #region Property Region
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public ObservableCollection<CrownListItem> Items
        {
            get => _items;
            set
            {
                if (_items != null)
                {
                    _items.CollectionChanged -= Items_CollectionChanged;
                }
 
                _items = value;
 
                _items.CollectionChanged += Items_CollectionChanged;
 
                UpdateListBox();
            }
        }
 
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public List<int> SelectedIndices { get; }
 
        [Category("Appearance")]
        [Description("Determines the height of the individual list view items.")]
        [DefaultValue(20)]
        public int ItemHeight
        {
            get => _itemHeight;
            set
            {
                _itemHeight = value;
                UpdateListBox();
            }
        }
 
        [Category("Behaviour")]
        [Description("Determines whether multiple list view items can be selected at once.")]
        [DefaultValue(false)]
        public bool MultiSelect { get; set; }
 
        [Category("Appearance")]
        [Description("Determines whether icons are rendered with the list items.")]
        [DefaultValue(false)]
        public bool ShowIcons { get; set; }
 
        #endregion
 
        #region Constructor Region
 
        public CrownListView()
        {
            Items = new ObservableCollection<CrownListItem>();
            SelectedIndices = new List<int>();
        }
 
        #endregion
 
        #region Event Handler Region
 
        private void Items_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                using (Graphics g = CreateGraphics())
                {
                    // Set the area size of all new items
                    foreach (CrownListItem item in e.NewItems)
                    {
                        item.TextChanged += Item_TextChanged;
                        UpdateItemSize(item, g);
                    }
                }
 
                // Find the starting index of the new item list and update anything past that
                if (e.NewStartingIndex < (Items.Count - 1))
                {
                    for (int i = e.NewStartingIndex; i <= Items.Count - 1; i++)
                    {
                        UpdateItemPosition(Items[i], i);
                    }
                }
            }
 
            if (e.OldItems != null)
            {
                foreach (CrownListItem item in e.OldItems)
                {
                    item.TextChanged -= Item_TextChanged;
                }
 
                // Find the starting index of the old item list and update anything past that
                if (e.OldStartingIndex < (Items.Count - 1))
                {
                    for (int i = e.OldStartingIndex; i <= Items.Count - 1; i++)
                    {
                        UpdateItemPosition(Items[i], i);
                    }
                }
            }
 
            if (Items.Count == 0)
            {
                if (SelectedIndices.Count > 0)
                {
                    SelectedIndices.Clear();
 
                    SelectedIndicesChanged?.Invoke(this, null);
                }
            }
 
            UpdateContentSize();
        }
 
        private void Item_TextChanged(object sender, EventArgs e)
        {
            CrownListItem item = (CrownListItem)sender;
 
            UpdateItemSize(item);
            UpdateContentSize(item);
            Invalidate();
        }
 
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
 
            if (Items.Count == 0)
            {
                return;
            }
 
            if (e.Button is not MouseButtons.Left and not MouseButtons.Right)
            {
                return;
            }
 
            Point pos = OffsetMousePosition;
 
            List<int> range = ItemIndexesInView().ToList();
 
            int top = range.Min();
            int bottom = range.Max();
            int width = Math.Max(ContentSize.Width, Viewport.Width);
 
            for (int i = top; i <= bottom; i++)
            {
                Rectangle rect = new(0, i * ItemHeight, width, ItemHeight);
 
                if (rect.Contains(pos))
                {
                    if (MultiSelect && ModifierKeys == Keys.Shift)
                    {
                        SelectAnchoredRange(i);
                    }
                    else if (MultiSelect && ModifierKeys == Keys.Control)
                    {
                        ToggleItem(i);
                    }
                    else
                    {
                        SelectItem(i);
                    }
                }
            }
        }
 
        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
 
            if (Items.Count == 0)
            {
                return;
            }
 
            if (e.KeyCode is not Keys.Down and not Keys.Up)
            {
                return;
            }
 
            if (MultiSelect && ModifierKeys == Keys.Shift)
            {
                if (e.KeyCode == Keys.Up)
                {
                    if (_anchoredItemEnd - 1 >= 0)
                    {
                        SelectAnchoredRange(_anchoredItemEnd - 1);
                        EnsureVisible();
                    }
                }
                else if (e.KeyCode == Keys.Down)
                {
                    if (_anchoredItemEnd + 1 <= Items.Count - 1)
                    {
                        SelectAnchoredRange(_anchoredItemEnd + 1);
                    }
                }
            }
            else
            {
                if (e.KeyCode == Keys.Up)
                {
                    if (_anchoredItemEnd - 1 >= 0)
                    {
                        SelectItem(_anchoredItemEnd - 1);
                    }
                }
                else if (e.KeyCode == Keys.Down)
                {
                    if (_anchoredItemEnd + 1 <= Items.Count - 1)
                    {
                        SelectItem(_anchoredItemEnd + 1);
                    }
                }
            }
 
            EnsureVisible();
        }
 
        #endregion
 
        #region Method Region
 
        public int GetItemIndex(CrownListItem item)
        {
            return Items.IndexOf(item);
        }
 
        public void SelectItem(int index)
        {
            if (index < 0 || index > Items.Count - 1)
            {
                throw new IndexOutOfRangeException($"Value '{index}' is outside of valid range.");
            }
 
            SelectedIndices.Clear();
            SelectedIndices.Add(index);
 
            SelectedIndicesChanged?.Invoke(this, null);
 
            _anchoredItemStart = index;
            _anchoredItemEnd = index;
 
            Invalidate();
        }
 
        public void SelectItems(IEnumerable<int> indexes)
        {
            SelectedIndices.Clear();
 
            List<int> list = indexes.ToList();
 
            foreach (int index in list)
            {
                if (index < 0 || index > Items.Count - 1)
                {
                    throw new IndexOutOfRangeException($"Value '{index}' is outside of valid range.");
                }
 
                SelectedIndices.Add(index);
            }
 
            SelectedIndicesChanged?.Invoke(this, null);
 
            _anchoredItemStart = list[list.Count - 1];
            _anchoredItemEnd = list[list.Count - 1];
 
            Invalidate();
        }
 
        public void ToggleItem(int index)
        {
            if (SelectedIndices.Contains(index))
            {
                SelectedIndices.Remove(index);
 
                // If we just removed both the anchor start AND end then reset them
                if (_anchoredItemStart == index && _anchoredItemEnd == index)
                {
                    if (SelectedIndices.Count > 0)
                    {
                        _anchoredItemStart = SelectedIndices[0];
                        _anchoredItemEnd = SelectedIndices[0];
                    }
                    else
                    {
                        _anchoredItemStart = -1;
                        _anchoredItemEnd = -1;
                    }
                }
 
                // If we just removed the anchor start then update it accordingly
                if (_anchoredItemStart == index)
                {
                    if (_anchoredItemEnd < index)
                    {
                        _anchoredItemStart = index - 1;
                    }
                    else if (_anchoredItemEnd > index)
                    {
                        _anchoredItemStart = index + 1;
                    }
                    else
                    {
                        _anchoredItemStart = _anchoredItemEnd;
                    }
                }
 
                // If we just removed the anchor end then update it accordingly
                if (_anchoredItemEnd == index)
                {
                    if (_anchoredItemStart < index)
                    {
                        _anchoredItemEnd = index - 1;
                    }
                    else if (_anchoredItemStart > index)
                    {
                        _anchoredItemEnd = index + 1;
                    }
                    else
                    {
                        _anchoredItemEnd = _anchoredItemStart;
                    }
                }
            }
            else
            {
                SelectedIndices.Add(index);
                _anchoredItemStart = index;
                _anchoredItemEnd = index;
            }
 
            SelectedIndicesChanged?.Invoke(this, null);
 
            Invalidate();
        }
 
        public void SelectItems(int startRange, int endRange)
        {
            SelectedIndices.Clear();
 
            if (startRange == endRange)
            {
                SelectedIndices.Add(startRange);
            }
 
            if (startRange < endRange)
            {
                for (int i = startRange; i <= endRange; i++)
                {
                    SelectedIndices.Add(i);
                }
            }
            else if (startRange > endRange)
            {
                for (int i = startRange; i >= endRange; i--)
                {
                    SelectedIndices.Add(i);
                }
            }
 
            SelectedIndicesChanged?.Invoke(this, null);
 
            Invalidate();
        }
 
        private void SelectAnchoredRange(int index)
        {
            _anchoredItemEnd = index;
            SelectItems(_anchoredItemStart, index);
        }
 
        private void UpdateListBox()
        {
            using (Graphics g = CreateGraphics())
            {
                for (int i = 0; i <= Items.Count - 1; i++)
                {
                    CrownListItem item = Items[i];
                    UpdateItemSize(item, g);
                    UpdateItemPosition(item, i);
                }
            }
 
            UpdateContentSize();
        }
 
        private void UpdateItemSize(CrownListItem item)
        {
            using Graphics g = CreateGraphics();
            UpdateItemSize(item, g);
        }
 
        private void UpdateItemSize(CrownListItem item, Graphics g)
        {
            SizeF size = g.MeasureString(item.Text, Font);
            size.Width++;
 
            if (ShowIcons)
            {
                size.Width += _iconSize + 8;
            }
 
            item.Area = new(item.Area.Left, item.Area.Top, (int)size.Width, item.Area.Height);
        }
 
        private void UpdateItemPosition(CrownListItem item, int index)
        {
            item.Area = new(2, index * ItemHeight, item.Area.Width, ItemHeight);
        }
 
        private void UpdateContentSize()
        {
            int highestWidth = 0;
 
            foreach (CrownListItem item in Items)
            {
                if (item.Area.Right + 1 > highestWidth)
                {
                    highestWidth = item.Area.Right + 1;
                }
            }
 
            int width = highestWidth;
            int height = Items.Count * ItemHeight;
 
            if (ContentSize.Width != width || ContentSize.Height != height)
            {
                ContentSize = new(width, height);
                Invalidate();
            }
        }
 
        private void UpdateContentSize(CrownListItem item)
        {
            int itemWidth = item.Area.Right + 1;
 
            if (itemWidth == ContentSize.Width)
            {
                UpdateContentSize();
                return;
            }
 
            if (itemWidth > ContentSize.Width)
            {
                ContentSize = new(itemWidth, ContentSize.Height);
                Invalidate();
            }
        }
 
        public void EnsureVisible()
        {
            if (SelectedIndices.Count == 0)
            {
                return;
            }
 
            int itemTop = -1;
 
            if (!MultiSelect)
            {
                itemTop = SelectedIndices[0] * ItemHeight;
            }
            else
            {
                itemTop = _anchoredItemEnd * ItemHeight;
            }
 
            int itemBottom = itemTop + ItemHeight;
 
            if (itemTop < Viewport.Top)
            {
                VScrollTo(itemTop);
            }
 
            if (itemBottom > Viewport.Bottom)
            {
                VScrollTo(itemBottom - Viewport.Height);
            }
        }
 
        private IEnumerable<int> ItemIndexesInView()
        {
            int top = (Viewport.Top / ItemHeight) - 1;
 
            if (top < 0)
            {
                top = 0;
            }
 
            int bottom = ((Viewport.Top + Viewport.Height) / ItemHeight) + 1;
 
            if (bottom > Items.Count)
            {
                bottom = Items.Count;
            }
 
            IEnumerable<int> result = Enumerable.Range(top, bottom - top);
            return result;
        }
 
        private IEnumerable<CrownListItem> ItemsInView()
        {
            IEnumerable<int> indexes = ItemIndexesInView();
            List<CrownListItem> result = indexes.Select(index => Items[index]).ToList();
            return result;
        }
 
        #endregion
 
        #region Paint Region
 
        protected override void PaintContent(Graphics g)
        {
            List<int> range = ItemIndexesInView().ToList();
 
            if (range.Count == 0)
            {
                return;
            }
 
            int top = range.Min();
            int bottom = range.Max();
 
            for (int i = top; i <= bottom; i++)
            {
                int width = Math.Max(ContentSize.Width, Viewport.Width);
                Rectangle rect = new(0, i * ItemHeight, width, ItemHeight);
 
                // Background
                bool odd = i % 2 != 0;
                Color bgColor = !odd ? ThemeProvider.Theme.Colors.HeaderBackground : ThemeProvider.Theme.Colors.GreyBackground;
 
                if (SelectedIndices.Count > 0 && SelectedIndices.Contains(i))
                {
                    bgColor = Focused ? ThemeProvider.Theme.Colors.BlueSelection : ThemeProvider.Theme.Colors.GreySelection;
                }
 
                using (SolidBrush b = new(bgColor))
                {
                    g.FillRectangle(b, rect);
                }
 
                // DEBUG: Border
                /*using (var p = new(ThemeProvider.Theme.Colors.DarkBorder))
                {
                    g.DrawLine(p, new Point(rect.Left, rect.Bottom - 1), new Point(rect.Right, rect.Bottom - 1));
                }*/
 
                // Icon
                if (ShowIcons && Items[i].Icon != null)
                {
                    g.DrawImageUnscaled(Items[i].Icon, new Point(rect.Left + 5, rect.Top + (rect.Height / 2) - (_iconSize / 2)));
                }
 
                // Text
                using (SolidBrush b = new(ThemeProvider.Theme.Colors.LightText)) //Items[i].TextColor
                {
                    StringFormat stringFormat = new()
                    {
                        Alignment = StringAlignment.Near,
                        LineAlignment = StringAlignment.Center
                    };
 
                    Font modFont = new(Font, Items[i].FontStyle);
 
                    Rectangle modRect = new(rect.Left + 2, rect.Top, rect.Width, rect.Height);
 
                    if (ShowIcons)
                    {
                        modRect.X += _iconSize + 8;
                    }
 
                    g.DrawString(Items[i].Text, modFont, b, modRect, stringFormat);
                }
            }
        }
 
        #endregion
    }
 
    #endregion
}